]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/ColorCache.cpp
Set url color from system theme.
[lyx.git] / src / frontends / qt4 / ColorCache.cpp
1 /**
2  * \file ColorCache.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "LyXRC.h"
14
15 #include "ColorCache.h"
16 #include "ColorSet.h"
17
18 namespace lyx {
19
20 namespace{
21 // FIXME (later): Qt >= 4.4 has a proper QPalette::NoRole value.
22 QPalette::ColorRole const NoRole = static_cast<QPalette::ColorRole>(-1);
23
24 QPalette::ColorRole role(ColorCode col)
25 {
26         switch (ColorCode(col)) {
27         case Color_background:
28         case Color_commentbg:
29         case Color_greyedoutbg:
30         case Color_mathbg:
31         case Color_graphicsbg:
32         case Color_mathmacrobg:
33         case Color_mathcorners:
34                 return QPalette::Base;
35                 break;
36                                 
37         case Color_foreground:
38         case Color_cursor:
39         case Color_preview:
40         case Color_tabularline:
41         case Color_previewframe:
42                 return QPalette::Text;
43                 break;
44                                 
45         case Color_selection:
46                 return QPalette::Highlight;
47                 break;
48         case Color_selectiontext:
49                 return QPalette::HighlightedText;
50                 break;
51         case Color_urllabel:
52         case Color_urltext:
53                 return QPalette::Link;
54         default:
55                 return NoRole;
56         }
57 }
58
59 }
60
61
62 void ColorCache::init()
63 {
64         for (int col = 0; col <= Color_ignore; ++col) {
65                 lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str());
66         }
67
68         initialized_ = true;
69 }
70
71
72 /// get the given color
73 QColor ColorCache::get(Color const & color) const
74 {
75         return get(color, lyxrc.use_system_colors);
76 }
77
78
79 /// get the given color
80 QColor ColorCache::get(Color const & color, bool syscolors) const
81 {
82         if (!initialized_)
83                 const_cast<ColorCache *>(this)->init();
84         if (color <= Color_ignore && color.mergeColor == Color_ignore) {
85                 QPalette::ColorRole const cr = role(color.baseColor);
86                 if (syscolors && cr != NoRole) {
87                         static QColor const white = Qt::white;
88                         QColor const c = pal_.brush(QPalette::Active, cr).color();
89                         if (cr == QPalette::Base && c == white)
90                                 return lcolors_[color.baseColor];
91                         else
92                                 return c;
93                 } else
94                         return lcolors_[color.baseColor];
95         }
96         if (color.mergeColor != Color_ignore) {
97                 // FIXME: This would ideally be done in the Color class, but
98                 // that means that we'd have to use the Qt code in the core.
99                 QColor base_color = get(color.baseColor, syscolors).toRgb();
100                 QColor merge_color = get(color.mergeColor, syscolors).toRgb();
101                 return QColor(
102                         (base_color.red() + merge_color.red()) / 2,
103                         (base_color.green() + merge_color.green()) / 2,
104                         (base_color.blue() + merge_color.blue()) / 2);
105         }
106         // used by branches
107         return QColor(lcolor.getX11Name(color.baseColor).c_str()); 
108 }
109
110
111 bool ColorCache::isSystem(ColorCode const color) const
112 {
113         QPalette::ColorRole const cr = role(color);
114         if (cr == QPalette::Base) {
115                 static QColor const white = Qt::white;
116                 return pal_.brush(QPalette::Active, cr).color() != white;
117         } else
118                 return cr != NoRole;
119 }
120
121
122 QColor const rgb2qcolor(RGBColor const & rgb)
123 {
124         return QColor(rgb.r, rgb.g, rgb.b);
125 }
126
127
128 } // namespace lyx