CustomizeThemeDialog::CustomizeThemeDialog(QWidget *parent) : QDialog(parent) , _targetedColor(nullptr) , _animation(new QPropertyAnimation(this, "windowOpacity")) , _timer(new QTimer(this)) { setupUi(this); for (QSpinBox *spinBox : this->findChildren<QSpinBox*>()) { spinBox->setAttribute(Qt::WA_MacShowFocusRect, false); } listWidget->verticalScrollBar()->deleteLater(); listWidget->setVerticalScrollBar(new ScrollBar(Qt::Vertical, this)); listWidget->setAttribute(Qt::WA_MacShowFocusRect, false); this->setWindowFlags(Qt::Tool); this->setAttribute(Qt::WA_DeleteOnClose); buttonsListBox->setVisible(false); spinBoxLibrary->setMouseTracking(true); // Animates this Dialog _timer->setInterval(3000); _timer->setSingleShot(true); _animation->setDuration(200); _animation->setTargetObject(this); // Custom colors in the whole application auto settingsPrivate = SettingsPrivate::instance(); bool customColors = settingsPrivate->isCustomColors(); if (customColors) { enableCustomColorsRadioButton->setChecked(true); } // Override text color or not which is usually computed automatically if (settingsPrivate->isCustomTextColorOverriden()) { enableCustomTextColorsRadioButton->setChecked(true); } this->setupActions(); SettingsPrivate *settings = SettingsPrivate::instance(); this->restoreGeometry(settings->value("customizeThemeDialogGeometry").toByteArray()); listWidget->setCurrentRow(settings->value("customizeThemeDialogCurrentTab").toInt()); this->loadTheme(); }
void SettingsPrivate::setCustomColorRole(QPalette::ColorRole cr, const QColor &color) { QPalette palette = this->customPalette(); if (cr == QPalette::Base) { palette.setColor(QPalette::Button, color); // Check if text color should be inverted when the base is too dark QColor text, alternateBase; if (color.value() < 128) { alternateBase = palette.base().color().lighter(110); QColor light = _standardPalette.light().color(); QColor midLight = _standardPalette.midlight().color(); QColor mid = _standardPalette.mid().color(); QColor dark = _standardPalette.dark().color(); QColor shadow = _standardPalette.shadow().color(); light.setRgb(255 - light.red(), 255 - light.green(), 255 - light.blue()); midLight.setRgb(255 - midLight.red(), 255 - midLight.green(), 255 - midLight.blue()); mid.setRgb(255 - mid.red(), 255 - mid.green(), 255 - mid.blue()); dark.setRgb(255 - dark.red(), 255 - dark.green(), 255 - dark.blue()); shadow.setRgb(255 - shadow.red(), 255 - shadow.green(), 255 - shadow.blue()); palette.setColor(QPalette::Light, light); palette.setColor(QPalette::Midlight, midLight); palette.setColor(QPalette::Mid, mid); palette.setColor(QPalette::Dark, dark); palette.setColor(QPalette::Shadow, shadow); text = Qt::white; } else { alternateBase = palette.base().color().darker(110); text = Qt::black; } palette.setColor(QPalette::AlternateBase, alternateBase); palette.setColor(QPalette::BrightText, text); palette.setColor(QPalette::ButtonText, text); palette.setColor(QPalette::Text, text); palette.setColor(QPalette::WindowText, text); // Automatically create a window color from the base one QColor windowColor = color; //windowColor.setAlphaF(0.5); if (text == Qt::white) { windowColor = color.lighter(130); } else { windowColor = color.darker(110); } palette.setColor(QPalette::Window, windowColor); } else if (cr == QPalette::Highlight) { if (!isCustomTextColorOverriden()) { QColor highlightedText; if (qAbs(color.value() - QColor(Qt::white).value()) < 128) { highlightedText = Qt::black; } else { highlightedText = Qt::white; } palette.setColor(QPalette::HighlightedText, highlightedText); } } else if (cr == QPalette::Text || cr == QPalette::HighlightedText) { if (!isCustomTextColorOverriden()) { QColor c1; if (cr == QPalette::Text) { c1 = palette.base().color(); } else { c1 = palette.highlight().color(); } QColor c2; if (qAbs(c1.value() - QColor(Qt::white).value()) < 128) { c2 = Qt::black; } else { c2 = Qt::white; } palette.setColor(cr, c2); } } /// XXX palette.setColor(cr, color); QApplication::setPalette(palette); this->setValue("customPalette", QVariant::fromValue<QPalette>(palette)); }