Bitcoin Core  27.99.0
P2P Digital Currency
platformstyle.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2021 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, true, true},
22  {"windows", true, false, false},
23  /* Other: linux, unix, ... */
24  {"other", true, true, false}
25 };
26 
27 namespace {
28 /* Local functions for colorizing single-color images */
29 
30 void MakeSingleColorImage(QImage& img, const QColor& colorbase)
31 {
32  img = img.convertToFormat(QImage::Format_ARGB32);
33  for (int x = img.width(); x--; )
34  {
35  for (int y = img.height(); y--; )
36  {
37  const QRgb rgb = img.pixel(x, y);
38  img.setPixel(x, y, qRgba(colorbase.red(), colorbase.green(), colorbase.blue(), qAlpha(rgb)));
39  }
40  }
41 }
42 
43 QIcon ColorizeIcon(const QIcon& ico, const QColor& colorbase)
44 {
45  QIcon new_ico;
46  for (const QSize& sz : ico.availableSizes())
47  {
48  QImage img(ico.pixmap(sz).toImage());
49  MakeSingleColorImage(img, colorbase);
50  new_ico.addPixmap(QPixmap::fromImage(img));
51  }
52  return new_ico;
53 }
54 
55 QImage ColorizeImage(const QString& filename, const QColor& colorbase)
56 {
57  QImage img(filename);
58  MakeSingleColorImage(img, colorbase);
59  return img;
60 }
61 
62 QIcon ColorizeIcon(const QString& filename, const QColor& colorbase)
63 {
64  return QIcon(QPixmap::fromImage(ColorizeImage(filename, colorbase)));
65 }
66 
67 }
68 
69 
70 PlatformStyle::PlatformStyle(const QString &_name, bool _imagesOnButtons, bool _colorizeIcons, bool _useExtraSpacing):
71  name(_name),
72  imagesOnButtons(_imagesOnButtons),
73  colorizeIcons(_colorizeIcons),
74  useExtraSpacing(_useExtraSpacing)
75 {
76 }
77 
79 {
80  return QApplication::palette().color(QPalette::WindowText);
81 }
82 
84 {
85  if (colorizeIcons) {
86  QColor colorHighlightBg(QApplication::palette().color(QPalette::Highlight));
87  QColor colorHighlightFg(QApplication::palette().color(QPalette::HighlightedText));
88  const QColor colorText(QApplication::palette().color(QPalette::WindowText));
89  const int colorTextLightness = colorText.lightness();
90  if (abs(colorHighlightBg.lightness() - colorTextLightness) < abs(colorHighlightFg.lightness() - colorTextLightness)) {
91  return colorHighlightBg;
92  }
93  return colorHighlightFg;
94  }
95  return {0, 0, 0};
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 QIcon& icon) const
120 {
121  return ColorizeIcon(icon, TextColor());
122 }
123 
125 {
126  for (const auto& platform_style : platform_styles) {
127  if (platformId == platform_style.platformId) {
128  return new PlatformStyle(
129  platform_style.platformId,
130  platform_style.imagesOnButtons,
131  platform_style.colorizeIcons,
132  platform_style.useExtraSpacing);
133  }
134  }
135  return nullptr;
136 }
137 
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
QColor SingleColor() const
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 TextColor() const
QImage SingleColorImage(const QString &filename) const
Colorize an image (given filename) with the icon color.
QIcon TextColorIcon(const QIcon &icon) const
Colorize an icon (given object) with the text color.
static const struct @7 platform_styles[]
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.
const char * name
Definition: rest.cpp:50