Dogecoin Core  1.14.2
P2P Digital Currency
platformstyle.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2016 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", false, false, true},
26  {"windows", true, false, 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)
36 {
37  img = img.convertToFormat(QImage::Format_ARGB32);
38  for (int x = img.width(); x--; )
39  {
40  for (int y = img.height(); y--; )
41  {
42  const QRgb rgb = img.pixel(x, y);
43  img.setPixel(x, y, qRgba(colorbase.red(), colorbase.green(), colorbase.blue(), qAlpha(rgb)));
44  }
45  }
46 }
47 
48 QIcon ColorizeIcon(const QIcon& ico, const QColor& colorbase)
49 {
50  QIcon new_ico;
51  QSize sz;
52  Q_FOREACH(sz, ico.availableSizes())
53  {
54  QImage img(ico.pixmap(sz).toImage());
55  MakeSingleColorImage(img, colorbase);
56  new_ico.addPixmap(QPixmap::fromImage(img));
57  }
58  return new_ico;
59 }
60 
61 QImage ColorizeImage(const QString& filename, const QColor& colorbase)
62 {
63  QImage img(filename);
64  MakeSingleColorImage(img, colorbase);
65  return img;
66 }
67 
68 QIcon ColorizeIcon(const QString& filename, const QColor& colorbase)
69 {
70  return QIcon(QPixmap::fromImage(ColorizeImage(filename, colorbase)));
71 }
72 
73 }
74 
75 
76 PlatformStyle::PlatformStyle(const QString &_name, bool _imagesOnButtons, bool _colorizeIcons, bool _useExtraSpacing):
77  name(_name),
78  imagesOnButtons(_imagesOnButtons),
79  colorizeIcons(_colorizeIcons),
80  useExtraSpacing(_useExtraSpacing),
81  singleColor(0,0,0),
82  textColor(0,0,0)
83 {
84  // Determine icon highlighting color
85  if (colorizeIcons) {
86  const QColor colorHighlightBg(QApplication::palette().color(QPalette::Highlight));
87  const QColor colorHighlightFg(QApplication::palette().color(QPalette::HighlightedText));
88  const QColor colorText(QApplication::palette().color(QPalette::WindowText));
89  const int colorTextLightness = colorText.lightness();
90  QColor colorbase;
91  if (abs(colorHighlightBg.lightness() - colorTextLightness) < abs(colorHighlightFg.lightness() - colorTextLightness))
92  colorbase = colorHighlightBg;
93  else
94  colorbase = colorHighlightFg;
95  singleColor = colorbase;
96  }
97  // Determine text color
98  textColor = QColor(QApplication::palette().color(QPalette::WindowText));
99 }
100 
101 QImage PlatformStyle::SingleColorImage(const QString& filename) const
102 {
103  if (!colorizeIcons)
104  return QImage(filename);
105  return ColorizeImage(filename, SingleColor());
106 }
107 
108 QIcon PlatformStyle::SingleColorIcon(const QString& filename) const
109 {
110  if (!colorizeIcons)
111  return QIcon(filename);
112  return ColorizeIcon(filename, SingleColor());
113 }
114 
115 QIcon PlatformStyle::SingleColorIcon(const QIcon& icon) const
116 {
117  if (!colorizeIcons)
118  return icon;
119  return ColorizeIcon(icon, SingleColor());
120 }
121 
122 QIcon PlatformStyle::TextColorIcon(const QString& filename) const
123 {
124  return ColorizeIcon(filename, TextColor());
125 }
126 
127 QIcon PlatformStyle::TextColorIcon(const QIcon& icon) const
128 {
129  return ColorizeIcon(icon, TextColor());
130 }
131 
133 {
134  for (unsigned x=0; x<platform_styles_count; ++x)
135  {
136  if (platformId == platform_styles[x].platformId)
137  {
138  return new PlatformStyle(
139  platform_styles[x].platformId,
140  platform_styles[x].imagesOnButtons,
141  platform_styles[x].colorizeIcons,
142  platform_styles[x].useExtraSpacing);
143  }
144  }
145  return 0;
146 }
147 
bool imagesOnButtons
Definition: platformstyle.h:46
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
QColor SingleColor() const
Definition: platformstyle.h:25
PlatformStyle(const QString &name, bool imagesOnButtons, bool colorizeIcons, bool useExtraSpacing)
static const PlatformStyle * instantiate(const QString &platformId)
Get style associated with provided platform name, or 0 if not known.
QColor singleColor
Definition: platformstyle.h:49
QIcon TextColorIcon(const QString &filename) const
Colorize an icon (given filename) with the text color.
QColor textColor
Definition: platformstyle.h:50
bool useExtraSpacing
Definition: platformstyle.h:48
QColor TextColor() const
Definition: platformstyle.h:24
QImage SingleColorImage(const QString &filename) const
Colorize an image (given filename) with the icon color.
const bool useExtraSpacing
Extra padding/spacing in transactionview.
const bool imagesOnButtons
Show images on push buttons.
const char * platformId
const bool colorizeIcons
Colorize single-color icons.