Skip to content

Commit

Permalink
[Spreadsheet] Use stylesheet for alias color
Browse files Browse the repository at this point in the history
When setting the text color for an invalid alias, use the Qt stylesheeet
mechanism rather than QPalette, so that it works correctly with
an applied QSS stylesheeet. Also attempt to detect a darkmode stylesheet
and use lighter shade of red so that the alias is more legible. Finally,
instead of explicitly setting the text color to black when it's valid,
reset to the original stylesheet (usually an empty string). This ensures
that in a dark stylesheet the text color is legible.

Fixes #4803.
  • Loading branch information
chennes committed Dec 30, 2021
1 parent 7b3c3e6 commit 4098c88
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp
Expand Up @@ -419,24 +419,27 @@ void SheetView::aliasChanged(const QString& text)
// check live the input and highlight if the user input invalid characters

bool aliasOk = true;
QPalette palette = ui->cellAlias->palette();
static auto originalStylesheet = ui->cellAlias->styleSheet();
QString warningColor;
if (qApp->styleSheet().contains(QLatin1String("dark"), Qt::CaseInsensitive))
warningColor = QLatin1String("rgb(255,90,90)"); // Light red for dark mode
else
warningColor = QLatin1String("rgb(200,0,0)"); // Dark red for light mode

if (!text.isEmpty() && !sheet->isValidAlias(Base::Tools::toStdString(text)))
aliasOk = false;

if (!aliasOk) {
// change tooltip and make text color red
ui->cellAlias->setToolTip(QObject::tr("Alias contains invalid characters!"));
palette.setColor(QPalette::Text, Qt::red);
ui->cellAlias->setStyleSheet(QLatin1String("color:") + warningColor);
}
else {
// go back to normal
ui->cellAlias->setToolTip(
QObject::tr("Refer to cell by alias, for example\nSpreadsheet.my_alias_name instead of Spreadsheet.B1"));
palette.setColor(QPalette::Text, Qt::black);
ui->cellAlias->setStyleSheet(originalStylesheet);
}
// apply the text color via the palette
ui->cellAlias->setPalette(palette);
}

void SheetView::currentChanged ( const QModelIndex & current, const QModelIndex & previous )
Expand Down

0 comments on commit 4098c88

Please sign in to comment.