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