Fabcoin Core  0.16.2
P2P Digital Currency
platformstyle.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <platformstyle.h>
6 
7 #include <guiconstants.h>
8 
9 #include <QApplication>
10 #include <QColor>
11 #include <QIcon>
12 #include <QImage>
13 #include <QPalette>
14 #include <QPixmap>
15 
16 static const struct {
17  const char *platformId;
19  const bool imagesOnButtons;
21  const bool colorizeIcons;
23  const bool useExtraSpacing;
24 } platform_styles[] = {
25  {"macosx", true, true, false},
26  {"windows", true, true, false},
27  /* Other: linux, unix, ... */
28  {"other", true, true, false}
29 };
30 static const unsigned platform_styles_count = sizeof(platform_styles)/sizeof(*platform_styles);
31 
32 namespace {
33 /* Local functions for colorizing single-color images */
34 
35 void MakeSingleColorImage(QImage& img, const QColor& colorbase, double opacity = 1)
36 {
37  //Opacity representation in percentage (0, 1) i.e. (0%, 100%)
38  if(opacity > 1 && opacity < 0) opacity = 1;
39 
40  img = img.convertToFormat(QImage::Format_ARGB32);
41  for (int x = img.width(); x--; )
42  {
43  for (int y = img.height(); y--; )
44  {
45  const QRgb rgb = img.pixel(x, y);
46  img.setPixel(x, y, qRgba(colorbase.red(), colorbase.green(), colorbase.blue(), opacity * qAlpha(rgb)));
47  }
48  }
49 }
50 
51 QPixmap MakeSingleColorPixmap(QImage& img, const QColor& colorbase, double opacity = 1)
52 {
53  MakeSingleColorImage(img, colorbase, opacity);
54  return QPixmap::fromImage(img);
55 }
56 
57 QIcon ColorizeIcon(const QIcon& ico, const QColor& colorbase, double opacity = 1)
58 {
59  QIcon new_ico;
60  QSize sz;
61  Q_FOREACH(sz, ico.availableSizes())
62  {
63  QImage img(ico.pixmap(sz).toImage());
64  MakeSingleColorImage(img, colorbase, opacity);
65  new_ico.addPixmap(QPixmap::fromImage(img));
66  }
67  return new_ico;
68 }
69 
70 QImage ColorizeImage(const QString& filename, const QColor& colorbase, double opacity = 1)
71 {
72  QImage img(filename);
73  MakeSingleColorImage(img, colorbase, opacity);
74  return img;
75 }
76 
77 QIcon ColorizeIcon(const QString& filename, const QColor& colorbase, double opacity = 1)
78 {
79  return QIcon(QPixmap::fromImage(ColorizeImage(filename, colorbase, opacity)));
80 }
81 
82 }
83 
84 
85 PlatformStyle::PlatformStyle(const QString &_name, bool _imagesOnButtons, bool _colorizeIcons, bool _useExtraSpacing):
86  name(_name),
87  imagesOnButtons(_imagesOnButtons),
88  colorizeIcons(_colorizeIcons),
89  useExtraSpacing(_useExtraSpacing),
90  singleColor(0,0,0),
91  textColor(0,0,0),
92  menuColor(0,0,0)
93 {
94  // Determine icon highlighting color
95  if (colorizeIcons) {
96  singleColor = 0x008ac8;
97  }
98  // Determine text color
99  textColor = 0xe6f0f0;
100  menuColor = QColor(QApplication::palette().color(QPalette::WindowText));
101 }
102 
103 QImage PlatformStyle::SingleColorImage(const QString& filename) const
104 {
105  if (!colorizeIcons)
106  return QImage(filename);
107  return ColorizeImage(filename, SingleColor(), 0.8);
108 }
109 
110 QIcon PlatformStyle::SingleColorIcon(const QString& filename) const
111 {
112  if (!colorizeIcons)
113  return QIcon(filename);
114  return ColorizeIcon(filename, SingleColor());
115 }
116 
117 QIcon PlatformStyle::SingleColorIcon(const QIcon& icon) const
118 {
119  if (!colorizeIcons)
120  return icon;
121  return ColorizeIcon(icon, SingleColor());
122 }
123 
124 QIcon PlatformStyle::TextColorIcon(const QString& filename) const
125 {
126  return ColorizeIcon(filename, TextColor(), 0.6);
127 }
128 
129 QIcon PlatformStyle::TextColorIcon(const QIcon& icon) const
130 {
131  return ColorizeIcon(icon, TextColor(), 0.6);
132 }
133 
134 QIcon PlatformStyle::MenuColorIcon(const QString &filename) const
135 {
136  return ColorizeIcon(filename, MenuColor(), 0.8);
137 }
138 
139 QIcon PlatformStyle::MenuColorIcon(const QIcon &icon) const
140 {
141  return ColorizeIcon(icon, MenuColor(), 0.8);
142 }
143 
144 QIcon PlatformStyle::MultiStatesIcon(const QString &resourcename, StateType type, QColor color, QColor colorAlt) const
145 {
146  QIcon icon;
147  switch (type) {
148  case NavBar:
149  {
150  QImage img1(resourcename);
151  QImage img2(img1);
152  QImage img3(img1);
153  QPixmap pix1 = MakeSingleColorPixmap(img1, color, 1);
154  QPixmap pix2 = MakeSingleColorPixmap(img2, color, 0.8);
155  QPixmap pix3 = MakeSingleColorPixmap(img3, colorAlt, 0.8);
156  icon.addPixmap(pix1, QIcon::Normal, QIcon::On);
157  icon.addPixmap(pix2, QIcon::Normal, QIcon::Off);
158  icon.addPixmap(pix3, QIcon::Selected, QIcon::On);
159  break;
160  }
161  case PushButton:
162  {
163  QImage img1(resourcename);
164  QImage img2(img1);
165  QPixmap pix1 = MakeSingleColorPixmap(img1, color, 1);
166  QPixmap pix2 = MakeSingleColorPixmap(img2, color, 0.2);
167  icon.addPixmap(pix1, QIcon::Normal, QIcon::Off);
168  icon.addPixmap(pix2, QIcon::Disabled, QIcon::On);
169  icon.addPixmap(pix2, QIcon::Disabled, QIcon::Off);
170  break;
171  }
172  default:
173  break;
174  }
175  return icon;
176 }
177 
178 QIcon PlatformStyle::TableColorIcon(const QString &resourcename, TableColorType type) const
179 {
180  // Initialize variables
181  QIcon icon;
182  QImage img1(resourcename);
183  QImage img2(img1);
184  double opacity = 1;
185  double opacitySelected = 0.8;
186  int color = 0xffffff;
187  int colorSelected = 0xffffff;
188 
189  // Choose color
190  TableColor(type, color, opacity);
191 
192  // Create pixmaps
193  QPixmap pix1 = MakeSingleColorPixmap(img1, color, opacity);
194  QPixmap pix2 = MakeSingleColorPixmap(img2, colorSelected, opacitySelected);
195 
196  // Create icon
197  icon.addPixmap(pix1, QIcon::Normal, QIcon::On);
198  icon.addPixmap(pix1, QIcon::Normal, QIcon::Off);
199  icon.addPixmap(pix2, QIcon::Selected, QIcon::On);
200  icon.addPixmap(pix2, QIcon::Selected, QIcon::Off);
201 
202  return icon;
203 }
204 
205 QImage PlatformStyle::TableColorImage(const QString &resourcename, PlatformStyle::TableColorType type) const
206 {
207  // Initialize variables
208  QImage img(resourcename);
209  double opacity = 1;
210  int color = 0xffffff;
211 
212  // Choose color
213  TableColor(type, color, opacity);
214 
215  // Create imtge
216  MakeSingleColorImage(img, color, opacity);
217 
218  return img;
219 }
220 
221 void PlatformStyle::TableColor(PlatformStyle::TableColorType type, int &color, double &opacity) const
222 {
223  // Initialize variables
224  opacity = 1;
225  color = 0xffffff;
226 
227  // Choose color
228  switch (type) {
229  case Normal:
230  opacity = 0.3;
231  color = 0xffffff;
232  break;
233  case Input:
234  opacity = 0.8;
235  color = 0x2fa5df;
236  break;
237  case Inout:
238  opacity = 0.8;
239  color = 0x40bb00;
240  break;
241  case Output:
242  opacity = 0.8;
243  color = 0x40bb00;
244  break;
245  case Error:
246  opacity = 0.8;
247  color = 0xd02e49;
248  break;
249  default:
250  break;
251  }
252 }
253 
255 {
256  for (unsigned x=0; x<platform_styles_count; ++x)
257  {
258  if (platformId == platform_styles[x].platformId)
259  {
260  return new PlatformStyle(
261  platform_styles[x].platformId,
262  platform_styles[x].imagesOnButtons,
263  platform_styles[x].colorizeIcons,
264  platform_styles[x].useExtraSpacing);
265  }
266  }
267  return 0;
268 }
269 
const bool colorizeIcons
Colorize single-color icons.
void TableColor(TableColorType type, int &color, double &opacity) const
QIcon TableColorIcon(const QString &resourcename, TableColorType type) const
QColor TextColor() const
Definition: platformstyle.h:24
PlatformStyle(const QString &name, bool imagesOnButtons, bool colorizeIcons, bool useExtraSpacing)
QColor singleColor
Definition: platformstyle.h:74
#define x(i)
const bool imagesOnButtons
Show images on push buttons.
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
const char * name
Definition: rest.cpp:36
QColor menuColor
Definition: platformstyle.h:76
const bool useExtraSpacing
Extra padding/spacing in transactionview.
QColor SingleColor() const
Definition: platformstyle.h:25
QColor textColor
Definition: platformstyle.h:75
QColor MenuColor() const
Definition: platformstyle.h:26
static const PlatformStyle * instantiate(const QString &platformId)
Get style associated with provided platform name, or 0 if not known.
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
QIcon MultiStatesIcon(const QString &resourcename, StateType type=NavBar, QColor color=Qt::white, QColor colorAlt=0x2d2d2d) const
Get multi-states icon.
QIcon MenuColorIcon(const QString &filename) const
Colorize an icon (given filename) with the menu text color.
QImage TableColorImage(const QString &resourcename, TableColorType type) const
bool imagesOnButtons
Definition: platformstyle.h:71
bool useExtraSpacing
Definition: platformstyle.h:73
QImage SingleColorImage(const QString &filename) const
Colorize an image (given filename) with the icon color.
const char * platformId
QIcon TextColorIcon(const QString &filename) const
Colorize an icon (given filename) with the text color.