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