]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/ColorCache.cpp
66b10c1df97cc2bf625acc0565946e116152a866
[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         default:
52                 return NoRole;
53         }
54 }
55
56 }
57
58
59 void ColorCache::init()
60 {
61         for (int col = 0; col <= Color_ignore; ++col) {
62                 lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str());
63         }
64
65         initialized_ = true;
66 }
67
68
69 /// get the given color
70 QColor ColorCache::get(Color const & color) const
71 {
72         return get(color, lyxrc.use_system_colors);
73 }
74
75
76 /// get the given color
77 QColor ColorCache::get(Color const & color, bool syscolors) const
78 {
79         if (!initialized_)
80                 const_cast<ColorCache *>(this)->init();
81         if (color <= Color_ignore && color.mergeColor == Color_ignore) {
82                 QPalette::ColorRole cr = role(color.baseColor);
83                 if (syscolors && cr != NoRole) {
84                         static QColor white = Qt::white;
85                         QColor c = pal_.brush(QPalette::Active, cr).color();
86                         if (cr == QPalette::Base && c == white)
87                                 return lcolors_[color.baseColor];
88                         else
89                                 return c;
90                 } else
91                         return lcolors_[color.baseColor];
92         }
93         if (color.mergeColor != Color_ignore) {
94                 // FIXME: This would ideally be done in the Color class, but
95                 // that means that we'd have to use the Qt code in the core.
96                 QColor base_color = get(color.baseColor, syscolors).toRgb();
97                 QColor merge_color = get(color.mergeColor, syscolors).toRgb();
98                 return QColor(
99                         (base_color.red() + merge_color.red()) / 2,
100                         (base_color.green() + merge_color.green()) / 2,
101                         (base_color.blue() + merge_color.blue()) / 2);
102         }
103         // used by branches
104         return QColor(lcolor.getX11Name(color.baseColor).c_str()); 
105 }
106
107
108 bool ColorCache::isSystem(ColorCode const color) const
109 {
110         return role(color) != NoRole;
111 }
112
113
114 QColor const rgb2qcolor(RGBColor const & rgb)
115 {
116         return QColor(rgb.r, rgb.g, rgb.b);
117 }
118
119
120 } // namespace lyx