Fix constness of ArgsManager methods
[bitcoinplatinum.git] / src / qt / platformstyle.cpp
blob1f4e1a442f069ee873405b7adcc5ef02c0afdbf1
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.
5 #include "platformstyle.h"
7 #include "guiconstants.h"
9 #include <QApplication>
10 #include <QColor>
11 #include <QIcon>
12 #include <QImage>
13 #include <QPalette>
14 #include <QPixmap>
16 static const struct {
17 const char *platformId;
18 /** Show images on push buttons */
19 const bool imagesOnButtons;
20 /** Colorize single-color icons */
21 const bool colorizeIcons;
22 /** Extra padding/spacing in transactionview */
23 const bool useExtraSpacing;
24 } platform_styles[] = {
25 {"macosx", false, false, true},
26 {"windows", true, false, false},
27 /* Other: linux, unix, ... */
28 {"other", true, true, false}
30 static const unsigned platform_styles_count = sizeof(platform_styles)/sizeof(*platform_styles);
32 namespace {
33 /* Local functions for colorizing single-color images */
35 void MakeSingleColorImage(QImage& img, const QColor& colorbase)
37 img = img.convertToFormat(QImage::Format_ARGB32);
38 for (int x = img.width(); x--; )
40 for (int y = img.height(); y--; )
42 const QRgb rgb = img.pixel(x, y);
43 img.setPixel(x, y, qRgba(colorbase.red(), colorbase.green(), colorbase.blue(), qAlpha(rgb)));
48 QIcon ColorizeIcon(const QIcon& ico, const QColor& colorbase)
50 QIcon new_ico;
51 for (const QSize sz : ico.availableSizes())
53 QImage img(ico.pixmap(sz).toImage());
54 MakeSingleColorImage(img, colorbase);
55 new_ico.addPixmap(QPixmap::fromImage(img));
57 return new_ico;
60 QImage ColorizeImage(const QString& filename, const QColor& colorbase)
62 QImage img(filename);
63 MakeSingleColorImage(img, colorbase);
64 return img;
67 QIcon ColorizeIcon(const QString& filename, const QColor& colorbase)
69 return QIcon(QPixmap::fromImage(ColorizeImage(filename, colorbase)));
75 PlatformStyle::PlatformStyle(const QString &_name, bool _imagesOnButtons, bool _colorizeIcons, bool _useExtraSpacing):
76 name(_name),
77 imagesOnButtons(_imagesOnButtons),
78 colorizeIcons(_colorizeIcons),
79 useExtraSpacing(_useExtraSpacing),
80 singleColor(0,0,0),
81 textColor(0,0,0)
83 // Determine icon highlighting color
84 if (colorizeIcons) {
85 const QColor colorHighlightBg(QApplication::palette().color(QPalette::Highlight));
86 const QColor colorHighlightFg(QApplication::palette().color(QPalette::HighlightedText));
87 const QColor colorText(QApplication::palette().color(QPalette::WindowText));
88 const int colorTextLightness = colorText.lightness();
89 QColor colorbase;
90 if (abs(colorHighlightBg.lightness() - colorTextLightness) < abs(colorHighlightFg.lightness() - colorTextLightness))
91 colorbase = colorHighlightBg;
92 else
93 colorbase = colorHighlightFg;
94 singleColor = colorbase;
96 // Determine text color
97 textColor = QColor(QApplication::palette().color(QPalette::WindowText));
100 QImage PlatformStyle::SingleColorImage(const QString& filename) const
102 if (!colorizeIcons)
103 return QImage(filename);
104 return ColorizeImage(filename, SingleColor());
107 QIcon PlatformStyle::SingleColorIcon(const QString& filename) const
109 if (!colorizeIcons)
110 return QIcon(filename);
111 return ColorizeIcon(filename, SingleColor());
114 QIcon PlatformStyle::SingleColorIcon(const QIcon& icon) const
116 if (!colorizeIcons)
117 return icon;
118 return ColorizeIcon(icon, SingleColor());
121 QIcon PlatformStyle::TextColorIcon(const QString& filename) const
123 return ColorizeIcon(filename, TextColor());
126 QIcon PlatformStyle::TextColorIcon(const QIcon& icon) const
128 return ColorizeIcon(icon, TextColor());
131 const PlatformStyle *PlatformStyle::instantiate(const QString &platformId)
133 for (unsigned x=0; x<platform_styles_count; ++x)
135 if (platformId == platform_styles[x].platformId)
137 return new PlatformStyle(
138 platform_styles[x].platformId,
139 platform_styles[x].imagesOnButtons,
140 platform_styles[x].colorizeIcons,
141 platform_styles[x].useExtraSpacing);
144 return 0;