]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/ColorCache.cpp
Fix faulty undo recording
[lyx.git] / src / frontends / qt / 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 "ColorCache.h"
14
15 #include "LyXRC.h"
16
17 #include "Color.h"
18 #include "ColorSet.h"
19
20 namespace lyx {
21
22 namespace{
23
24 QPalette::ColorRole role(ColorCode col)
25 {
26         switch (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_selectionmath:
49         case Color_selectiontext:
50                 return QPalette::HighlightedText;
51                 break;
52         case Color_urllabel:
53         case Color_urltext:
54                 return QPalette::Link;
55         default:
56                 return QPalette::NoRole;
57         }
58 }
59
60 } // namespace
61
62
63 void ColorCache::init()
64 {
65         for (int col = 0; col <= Color_ignore; ++col) {
66                 lcolors_[col] = QColor(lcolor.getX11HexName(ColorCode(col), isDarkMode()).c_str());
67         }
68
69         initialized_ = true;
70 }
71
72
73 /// get the given color
74 QColor ColorCache::get(Color const & color) const
75 {
76         return get(color, lyxrc.use_system_colors);
77 }
78
79
80 /// get the given color
81 QColor ColorCache::get(Color const & color, bool syscolors) const
82 {
83         if (!initialized_)
84                 const_cast<ColorCache *>(this)->init();
85         if (color <= Color_ignore && color.mergeColor == Color_ignore) {
86                 QPalette::ColorRole const cr = role(color.baseColor);
87                 if (syscolors && cr != QPalette::NoRole) {
88                         static QColor const white = Qt::white;
89                         QColor const c = pal_.brush(QPalette::Active, cr).color();
90                         if (cr == QPalette::Base && c == white)
91                                 return lcolors_[color.baseColor];
92                         else
93                                 return c;
94                 } else
95                         return lcolors_[color.baseColor];
96         }
97         if (color.mergeColor != Color_ignore) {
98                 // FIXME: This would ideally be done in the Color class, but
99                 // that means that we'd have to use the Qt code in the core.
100                 QColor base_color = get(color.baseColor, syscolors).toRgb();
101                 QColor merge_color = get(color.mergeColor, syscolors).toRgb();
102                 return QColor(
103                         (base_color.red() + merge_color.red()) / 2,
104                         (base_color.green() + merge_color.green()) / 2,
105                         (base_color.blue() + merge_color.blue()) / 2);
106         }
107         // used by branches
108         return QColor(lcolor.getX11HexName(color.baseColor, isDarkMode()).c_str());
109 }
110
111
112 bool ColorCache::isSystem(ColorCode const color) const
113 {
114         QPalette::ColorRole const cr = role(color);
115         if (cr == QPalette::Base) {
116                 static QColor const white = Qt::white;
117                 return pal_.brush(QPalette::Active, cr).color() != white;
118         } else
119                 return cr != QPalette::NoRole;
120 }
121
122
123 bool ColorCache::isDarkMode() const
124 {
125         QPalette palette = QPalette();
126         QColor text_color = palette.color(QPalette::Active, QPalette::WindowText);
127         QColor bg_color = palette.color(QPalette::Active, QPalette::Window);
128         
129         return (text_color.black() < bg_color.black());
130 }
131
132
133 QColor const rgb2qcolor(RGBColor const & rgb)
134 {
135         return QColor(rgb.r, rgb.g, rgb.b);
136 }
137
138
139 } // namespace lyx