X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FColorCache.cpp;h=6036099e473e051e6c52e4edf0f4eba5ade98135;hb=b6eacd8d4f86734e8abef3335b190ce12a6a11b5;hp=16c4497eab66ea07afa86bce84cb34be57b26724;hpb=bbb9cb65e779ac3d1c563df1d44dab2236a7c1fd;p=lyx.git diff --git a/src/frontends/qt4/ColorCache.cpp b/src/frontends/qt4/ColorCache.cpp index 16c4497eab..6036099e47 100644 --- a/src/frontends/qt4/ColorCache.cpp +++ b/src/frontends/qt4/ColorCache.cpp @@ -10,18 +10,110 @@ #include +#include "LyXRC.h" + #include "ColorCache.h" +#include "ColorSet.h" -#include "Color.h" +namespace lyx { -#include +namespace{ + +QPalette::ColorRole role(ColorCode 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: + 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; + case Color_urllabel: + case Color_urltext: + return QPalette::Link; + default: + return QPalette::NoRole; + } +} + +} // namespace -namespace lyx { void ColorCache::init() { - for (int col = 0; col <= Color_ignore; ++col) + 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 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) { + QPalette::ColorRole const cr = role(color.baseColor); + if (syscolors && cr != QPalette::NoRole) { + static QColor const white = Qt::white; + QColor const c = pal_.brush(QPalette::Active, cr).color(); + if (cr == QPalette::Base && c == white) + return lcolors_[color.baseColor]; + else + return c; + } 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, 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, + (base_color.blue() + merge_color.blue()) / 2); + } + // used by branches + return QColor(lcolor.getX11Name(color.baseColor).c_str()); +} + + +bool ColorCache::isSystem(ColorCode const color) const +{ + QPalette::ColorRole const cr = role(color); + if (cr == QPalette::Base) { + static QColor const white = Qt::white; + return pal_.brush(QPalette::Active, cr).color() != white; + } else + return cr != QPalette::NoRole; }