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