Bitcoin Core  21.99.0
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, 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  singleColor(0,0,0),
76  textColor(0,0,0)
77 {
78  // Determine icon highlighting color
79  if (colorizeIcons) {
80  const QColor colorHighlightBg(QApplication::palette().color(QPalette::Highlight));
81  const QColor colorHighlightFg(QApplication::palette().color(QPalette::HighlightedText));
82  const QColor colorText(QApplication::palette().color(QPalette::WindowText));
83  const int colorTextLightness = colorText.lightness();
84  QColor colorbase;
85  if (abs(colorHighlightBg.lightness() - colorTextLightness) < abs(colorHighlightFg.lightness() - colorTextLightness))
86  colorbase = colorHighlightBg;
87  else
88  colorbase = colorHighlightFg;
89  singleColor = colorbase;
90  }
91  // Determine text color
92  textColor = QColor(QApplication::palette().color(QPalette::WindowText));
93 }
94 
95 QImage PlatformStyle::SingleColorImage(const QString& filename) const
96 {
97  if (!colorizeIcons)
98  return QImage(filename);
99  return ColorizeImage(filename, SingleColor());
100 }
101 
102 QIcon PlatformStyle::SingleColorIcon(const QString& filename) const
103 {
104  if (!colorizeIcons)
105  return QIcon(filename);
106  return ColorizeIcon(filename, SingleColor());
107 }
108 
109 QIcon PlatformStyle::SingleColorIcon(const QIcon& icon) const
110 {
111  if (!colorizeIcons)
112  return icon;
113  return ColorizeIcon(icon, SingleColor());
114 }
115 
116 QIcon PlatformStyle::TextColorIcon(const QIcon& icon) const
117 {
118  return ColorizeIcon(icon, TextColor());
119 }
120 
122 {
123  for (const auto& platform_style : platform_styles) {
124  if (platformId == platform_style.platformId) {
125  return new PlatformStyle(
126  platform_style.platformId,
127  platform_style.imagesOnButtons,
128  platform_style.colorizeIcons,
129  platform_style.useExtraSpacing);
130  }
131  }
132  return nullptr;
133 }
134 
PlatformStyle::SingleColorIcon
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
Definition: platformstyle.cpp:102
PlatformStyle::PlatformStyle
PlatformStyle(const QString &name, bool imagesOnButtons, bool colorizeIcons, bool useExtraSpacing)
Definition: platformstyle.cpp:70
PlatformStyle::SingleColor
QColor SingleColor() const
Definition: platformstyle.h:25
PlatformStyle::colorizeIcons
bool colorizeIcons
Definition: platformstyle.h:44
PlatformStyle::singleColor
QColor singleColor
Definition: platformstyle.h:46
useExtraSpacing
const bool useExtraSpacing
Extra padding/spacing in transactionview.
Definition: platformstyle.cpp:19
PlatformStyle::TextColorIcon
QIcon TextColorIcon(const QIcon &icon) const
Colorize an icon (given object) with the text color.
Definition: platformstyle.cpp:116
PlatformStyle::SingleColorImage
QImage SingleColorImage(const QString &filename) const
Colorize an image (given filename) with the icon color.
Definition: platformstyle.cpp:95
imagesOnButtons
const bool imagesOnButtons
Show images on push buttons.
Definition: platformstyle.cpp:15
platform_styles
static const struct @7 platform_styles[]
PlatformStyle::instantiate
static const PlatformStyle * instantiate(const QString &platformId)
Get style associated with provided platform name, or 0 if not known.
Definition: platformstyle.cpp:121
name
const char * name
Definition: rest.cpp:43
platformId
const char * platformId
Definition: platformstyle.cpp:13
platformstyle.h
PlatformStyle::TextColor
QColor TextColor() const
Definition: platformstyle.h:24
PlatformStyle
Definition: platformstyle.h:13
colorizeIcons
const bool colorizeIcons
Colorize single-color icons.
Definition: platformstyle.cpp:17
PlatformStyle::textColor
QColor textColor
Definition: platformstyle.h:47