diff --git a/src/frontends/qt4/ColorCache.cpp b/src/frontends/qt4/ColorCache.cpp index 26ee11b279..5b0e56d32a 100644 --- a/src/frontends/qt4/ColorCache.cpp +++ b/src/frontends/qt4/ColorCache.cpp @@ -17,65 +17,79 @@ namespace lyx { -void ColorCache::setColor(int col, QPalette::ColorRole cr) +namespace{ +// FIXME (later): Qt >= 4.4 has a proper QPalette::NoRole value. +QPalette::ColorRole const NoRole = static_cast(-1); + +QPalette::ColorRole role(ColorCode col) { - lcolors_[col] = pal_.brush(QPalette::Active, cr).color(); + switch (ColorCode(col)) { + case Color_background: + case Color_commentbg: + case Color_greyedoutbg: + case Color_mathbg: + case Color_graphicsbg: + case Color_mathmacrobg: + case Color_mathcorners: + return QPalette::Base; + break; + + case Color_foreground: + case Color_cursor: + case Color_preview: + case Color_tabularline: + case Color_previewframe: + return QPalette::Text; + break; + + case Color_selection: + return QPalette::Highlight; + break; + case Color_selectiontext: + return QPalette::HighlightedText; + break; + default: + return NoRole; + } +} + } void ColorCache::init() { - if (lyxrc.use_system_colors) { - for (int col = 0; col <= Color_ignore; ++col) { - switch (ColorCode(col)) { - case Color_background: - case Color_commentbg: - case Color_greyedoutbg: - case Color_mathbg: - case Color_graphicsbg: - case Color_mathmacrobg: - case Color_mathcorners: - setColor(col, QPalette::Base); - break; - - case Color_foreground: - case Color_cursor: - case Color_preview: - case Color_tabularline: - case Color_previewframe: - setColor(col, QPalette::Text); - break; - - case Color_selection: - setColor(col, QPalette::Highlight); - break; - case Color_selectiontext: - setColor(col, QPalette::HighlightedText); - break; - default: - lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str()); - } - } - } else { - for (int col = 0; col <= Color_ignore; ++col) - lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str()); + for (int col = 0; col <= Color_ignore; ++col) { + lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str()); } + initialized_ = true; } /// get the given color -QColor ColorCache::get(Color color) const +QColor ColorCache::get(Color const & color) const +{ + return get(color, lyxrc.use_system_colors); +} + + +/// get the given color +QColor ColorCache::get(Color const & color, bool syscolors) const { if (!initialized_) const_cast(this)->init(); - if (color <= Color_ignore && color.mergeColor == Color_ignore) - return lcolors_[color.baseColor]; + if (color <= Color_ignore && color.mergeColor == Color_ignore) { + QPalette::ColorRole cr = role(color.baseColor); + if (syscolors && cr != NoRole) + return pal_.brush(QPalette::Active, cr).color(); + else + return lcolors_[color.baseColor]; + } if (color.mergeColor != Color_ignore) { // FIXME: This would ideally be done in the Color class, but // that means that we'd have to use the Qt code in the core. - QColor base_color = get(color.baseColor).toRgb(); - QColor merge_color = get(color.mergeColor).toRgb(); + QColor base_color = get(color.baseColor, syscolors).toRgb(); + QColor merge_color = get(color.mergeColor, syscolors).toRgb(); return QColor( (base_color.red() + merge_color.red()) / 2, (base_color.green() + merge_color.green()) / 2, @@ -86,6 +100,12 @@ QColor ColorCache::get(Color color) const } +bool ColorCache::isSystem(ColorCode const color) const +{ + return role(color) != NoRole; +} + + QColor const rgb2qcolor(RGBColor const & rgb) { return QColor(rgb.r, rgb.g, rgb.b); diff --git a/src/frontends/qt4/ColorCache.h b/src/frontends/qt4/ColorCache.h index af767c3420..e1ad6aed5e 100644 --- a/src/frontends/qt4/ColorCache.h +++ b/src/frontends/qt4/ColorCache.h @@ -30,11 +30,17 @@ public: /// ColorCache() : initialized_(false) {} + /// get the given color (depends on LyXRC::use_system_color) + QColor get(Color const & color) const; + /// get the given color - QColor get(Color color) const; + QColor get(Color const & color, bool use_system_colors) const; + + /// is this color replaced when LyXRC::use_system_color is true? + bool isSystem(ColorCode color) const; /// change the undelying palette - void setPalette(QPalette const pal) { pal_ = pal; initialized_ = false; } + void setPalette(QPalette const pal) { pal_ = pal; clear(); } /// clear all colors void clear() { initialized_ = false; } @@ -43,8 +49,6 @@ private: /// void init(); /// - void setColor(int col, QPalette::ColorRole cr); - /// QColor lcolors_[Color_ignore + 1]; /// bool initialized_; diff --git a/src/frontends/qt4/GuiPrefs.cpp b/src/frontends/qt4/GuiPrefs.cpp index 6bd55e86e5..6ad92ba3fb 100644 --- a/src/frontends/qt4/GuiPrefs.cpp +++ b/src/frontends/qt4/GuiPrefs.cpp @@ -1090,6 +1090,8 @@ PrefColors::PrefColors(GuiPreferences * form) this, SLOT(changeColor())); connect(syscolorsCB, SIGNAL(toggled(bool)), this, SIGNAL(changed())); + connect(syscolorsCB, SIGNAL(toggled(bool)), + this, SLOT(changeSysColor())); } @@ -1110,7 +1112,7 @@ void PrefColors::apply(LyXRC & rc) const void PrefColors::update(LyXRC const & rc) { for (unsigned int i = 0; i < lcolors_.size(); ++i) { - QColor color = QColor(guiApp->colorCache().get(lcolors_[i])); + QColor color = QColor(guiApp->colorCache().get(lcolors_[i], false)); QPixmap coloritem(32, 32); coloritem.fill(color); lyxObjectsLW->item(i)->setIcon(QIcon(coloritem)); @@ -1142,6 +1144,18 @@ void PrefColors::changeColor() } } +void PrefColors::changeSysColor() +{ + for (int row = 0 ; row < lyxObjectsLW->count() ; ++row) { + // skip colors that are taken from system palette + bool const hide = syscolorsCB->isChecked() + && guiApp->colorCache().isSystem(lcolors_[row]); + + lyxObjectsLW->item(row)->setHidden(hide); + } + +} + void PrefColors::changeLyxObjectsSelection() { colorChangePB->setDisabled(lyxObjectsLW->currentRow() < 0); diff --git a/src/frontends/qt4/GuiPrefs.h b/src/frontends/qt4/GuiPrefs.h index 1256d603b3..1da64f87c9 100644 --- a/src/frontends/qt4/GuiPrefs.h +++ b/src/frontends/qt4/GuiPrefs.h @@ -254,6 +254,7 @@ public: private Q_SLOTS: void changeColor(); + void changeSysColor(); void changeLyxObjectsSelection(); private: