]> git.lyx.org Git - lyx.git/commitdiff
Finish implementation of "use system colors" checkbox.
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 9 Aug 2010 12:15:02 +0000 (12:15 +0000)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 9 Aug 2010 12:15:02 +0000 (12:15 +0000)
ColorCache:
  * method isSystem() allows to know whether a color can be overridden by system colors
  * method get() has a variant that accepts a bool (system color or not)
  * code simplifications
GuiPrefs:
  * hide colors set from the system when they are inherited
  * syncronize list of colors and checkbox

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35101 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/qt4/ColorCache.cpp
src/frontends/qt4/ColorCache.h
src/frontends/qt4/GuiPrefs.cpp
src/frontends/qt4/GuiPrefs.h

index 26ee11b279ecfaa3e067d29b44dfdeb243d72b60..5b0e56d32a4ad414b512317e6a62ac0b854cc633 100644 (file)
 
 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<QPalette::ColorRole>(-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<ColorCache *>(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);
index af767c3420614f3e2f710ec1b14a7e3a3b13fc8e..e1ad6aed5eaea665ca46ec07f6d2bb0b977cc111 100644 (file)
@@ -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_;
index 6bd55e86e506bdb833466e463b30489675ef0cc7..6ad92ba3fb5a96dc5dd45370d61939e305a79c77 100644 (file)
@@ -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);
index 1256d603b38782f968299505321b3fa378245332..1da64f87c9f2193196dbd9ad8837dcc86c47cac8 100644 (file)
@@ -254,6 +254,7 @@ public:
 
 private Q_SLOTS:
        void changeColor();
+       void changeSysColor();
        void changeLyxObjectsSelection();
 
 private: