]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/ColorCache.cpp
Make the InsetInfo dialog a bit less esoteric.
[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
22 QPalette::ColorRole role(ColorCode col)
23 {
24         switch (ColorCode(col)) {
25         case Color_background:
26         case Color_commentbg:
27         case Color_greyedoutbg:
28         case Color_mathbg:
29         case Color_graphicsbg:
30         case Color_mathmacrobg:
31         case Color_mathcorners:
32                 return QPalette::Base;
33                 break;
34
35         case Color_foreground:
36         case Color_cursor:
37         case Color_preview:
38         case Color_tabularline:
39         case Color_previewframe:
40                 return QPalette::Text;
41                 break;
42
43         case Color_selection:
44                 return QPalette::Highlight;
45                 break;
46         case Color_selectiontext:
47                 return QPalette::HighlightedText;
48                 break;
49         case Color_urllabel:
50         case Color_urltext:
51                 return QPalette::Link;
52         default:
53                 return QPalette::NoRole;
54         }
55 }
56
57 } // namespace
58
59
60 void ColorCache::init()
61 {
62         for (int col = 0; col <= Color_ignore; ++col) {
63                 lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str());
64         }
65
66         initialized_ = true;
67 }
68
69
70 /// get the given color
71 QColor ColorCache::get(Color const & color) const
72 {
73         return get(color, lyxrc.use_system_colors);
74 }
75
76
77 /// get the given color
78 QColor ColorCache::get(Color const & color, bool syscolors) const
79 {
80         if (!initialized_)
81                 const_cast<ColorCache *>(this)->init();
82         if (color <= Color_ignore && color.mergeColor == Color_ignore) {
83                 QPalette::ColorRole const cr = role(color.baseColor);
84                 if (syscolors && cr != QPalette::NoRole) {
85                         static QColor const white = Qt::white;
86                         QColor const c = pal_.brush(QPalette::Active, cr).color();
87                         if (cr == QPalette::Base && c == white)
88                                 return lcolors_[color.baseColor];
89                         else
90                                 return c;
91                 } else
92                         return lcolors_[color.baseColor];
93         }
94         if (color.mergeColor != Color_ignore) {
95                 // FIXME: This would ideally be done in the Color class, but
96                 // that means that we'd have to use the Qt code in the core.
97                 QColor base_color = get(color.baseColor, syscolors).toRgb();
98                 QColor merge_color = get(color.mergeColor, syscolors).toRgb();
99                 return QColor(
100                         (base_color.red() + merge_color.red()) / 2,
101                         (base_color.green() + merge_color.green()) / 2,
102                         (base_color.blue() + merge_color.blue()) / 2);
103         }
104         // used by branches
105         return QColor(lcolor.getX11Name(color.baseColor).c_str());
106 }
107
108
109 bool ColorCache::isSystem(ColorCode const color) const
110 {
111         QPalette::ColorRole const cr = role(color);
112         if (cr == QPalette::Base) {
113                 static QColor const white = Qt::white;
114                 return pal_.brush(QPalette::Active, cr).color() != white;
115         } else
116                 return cr != QPalette::NoRole;
117 }
118
119
120 QColor const rgb2qcolor(RGBColor const & rgb)
121 {
122         return QColor(rgb.r, rgb.g, rgb.b);
123 }
124
125
126 } // namespace lyx