]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/ColorCache.cpp
5b0e56d32a4ad414b512317e6a62ac0b854cc633
[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                         return pal_.brush(QPalette::Active, cr).color();
85                 else
86                         return lcolors_[color.baseColor];
87         }
88         if (color.mergeColor != Color_ignore) {
89                 // FIXME: This would ideally be done in the Color class, but
90                 // that means that we'd have to use the Qt code in the core.
91                 QColor base_color = get(color.baseColor, syscolors).toRgb();
92                 QColor merge_color = get(color.mergeColor, syscolors).toRgb();
93                 return QColor(
94                         (base_color.red() + merge_color.red()) / 2,
95                         (base_color.green() + merge_color.green()) / 2,
96                         (base_color.blue() + merge_color.blue()) / 2);
97         }
98         // used by branches
99         return QColor(lcolor.getX11Name(color.baseColor).c_str()); 
100 }
101
102
103 bool ColorCache::isSystem(ColorCode const color) const
104 {
105         return role(color) != NoRole;
106 }
107
108
109 QColor const rgb2qcolor(RGBColor const & rgb)
110 {
111         return QColor(rgb.r, rgb.g, rgb.b);
112 }
113
114
115 } // namespace lyx