Finish implementation of "use system colors" checkbox.

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
This commit is contained in:
Jean-Marc Lasgouttes 2010-08-09 12:15:02 +00:00
parent aba279a928
commit b01fcee92d
4 changed files with 85 additions and 46 deletions

View File

@ -17,16 +17,12 @@
namespace lyx { namespace lyx {
void ColorCache::setColor(int col, QPalette::ColorRole cr) namespace{
{ // FIXME (later): Qt >= 4.4 has a proper QPalette::NoRole value.
lcolors_[col] = pal_.brush(QPalette::Active, cr).color(); QPalette::ColorRole const NoRole = static_cast<QPalette::ColorRole>(-1);
}
QPalette::ColorRole role(ColorCode col)
void ColorCache::init()
{ {
if (lyxrc.use_system_colors) {
for (int col = 0; col <= Color_ignore; ++col) {
switch (ColorCode(col)) { switch (ColorCode(col)) {
case Color_background: case Color_background:
case Color_commentbg: case Color_commentbg:
@ -35,7 +31,7 @@ void ColorCache::init()
case Color_graphicsbg: case Color_graphicsbg:
case Color_mathmacrobg: case Color_mathmacrobg:
case Color_mathcorners: case Color_mathcorners:
setColor(col, QPalette::Base); return QPalette::Base;
break; break;
case Color_foreground: case Color_foreground:
@ -43,39 +39,57 @@ void ColorCache::init()
case Color_preview: case Color_preview:
case Color_tabularline: case Color_tabularline:
case Color_previewframe: case Color_previewframe:
setColor(col, QPalette::Text); return QPalette::Text;
break; break;
case Color_selection: case Color_selection:
setColor(col, QPalette::Highlight); return QPalette::Highlight;
break; break;
case Color_selectiontext: case Color_selectiontext:
setColor(col, QPalette::HighlightedText); return QPalette::HighlightedText;
break; break;
default: default:
return NoRole;
}
}
}
void ColorCache::init()
{
for (int col = 0; col <= Color_ignore; ++col) {
lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str()); 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());
}
initialized_ = true; initialized_ = true;
} }
/// get the given color /// 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_) if (!initialized_)
const_cast<ColorCache *>(this)->init(); const_cast<ColorCache *>(this)->init();
if (color <= Color_ignore && color.mergeColor == Color_ignore) 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]; return lcolors_[color.baseColor];
}
if (color.mergeColor != Color_ignore) { if (color.mergeColor != Color_ignore) {
// FIXME: This would ideally be done in the Color class, but // 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. // that means that we'd have to use the Qt code in the core.
QColor base_color = get(color.baseColor).toRgb(); QColor base_color = get(color.baseColor, syscolors).toRgb();
QColor merge_color = get(color.mergeColor).toRgb(); QColor merge_color = get(color.mergeColor, syscolors).toRgb();
return QColor( return QColor(
(base_color.red() + merge_color.red()) / 2, (base_color.red() + merge_color.red()) / 2,
(base_color.green() + merge_color.green()) / 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) QColor const rgb2qcolor(RGBColor const & rgb)
{ {
return QColor(rgb.r, rgb.g, rgb.b); return QColor(rgb.r, rgb.g, rgb.b);

View File

@ -30,11 +30,17 @@ public:
/// ///
ColorCache() : initialized_(false) {} ColorCache() : initialized_(false) {}
/// get the given color (depends on LyXRC::use_system_color)
QColor get(Color const & color) const;
/// get the given color /// 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 /// change the undelying palette
void setPalette(QPalette const pal) { pal_ = pal; initialized_ = false; } void setPalette(QPalette const pal) { pal_ = pal; clear(); }
/// clear all colors /// clear all colors
void clear() { initialized_ = false; } void clear() { initialized_ = false; }
@ -43,8 +49,6 @@ private:
/// ///
void init(); void init();
/// ///
void setColor(int col, QPalette::ColorRole cr);
///
QColor lcolors_[Color_ignore + 1]; QColor lcolors_[Color_ignore + 1];
/// ///
bool initialized_; bool initialized_;

View File

@ -1090,6 +1090,8 @@ PrefColors::PrefColors(GuiPreferences * form)
this, SLOT(changeColor())); this, SLOT(changeColor()));
connect(syscolorsCB, SIGNAL(toggled(bool)), connect(syscolorsCB, SIGNAL(toggled(bool)),
this, SIGNAL(changed())); 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) void PrefColors::update(LyXRC const & rc)
{ {
for (unsigned int i = 0; i < lcolors_.size(); ++i) { 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); QPixmap coloritem(32, 32);
coloritem.fill(color); coloritem.fill(color);
lyxObjectsLW->item(i)->setIcon(QIcon(coloritem)); 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() void PrefColors::changeLyxObjectsSelection()
{ {
colorChangePB->setDisabled(lyxObjectsLW->currentRow() < 0); colorChangePB->setDisabled(lyxObjectsLW->currentRow() < 0);

View File

@ -254,6 +254,7 @@ public:
private Q_SLOTS: private Q_SLOTS:
void changeColor(); void changeColor();
void changeSysColor();
void changeLyxObjectsSelection(); void changeLyxObjectsSelection();
private: private: