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