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>
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
);
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
)
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
));
60 QImage
ColorizeImage(const QString
& filename
, const QColor
& colorbase
)
63 MakeSingleColorImage(img
, colorbase
);
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
):
77 imagesOnButtons(_imagesOnButtons
),
78 colorizeIcons(_colorizeIcons
),
79 useExtraSpacing(_useExtraSpacing
),
83 // Determine icon highlighting color
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();
90 if (abs(colorHighlightBg
.lightness() - colorTextLightness
) < abs(colorHighlightFg
.lightness() - colorTextLightness
))
91 colorbase
= colorHighlightBg
;
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
103 return QImage(filename
);
104 return ColorizeImage(filename
, SingleColor());
107 QIcon
PlatformStyle::SingleColorIcon(const QString
& filename
) const
110 return QIcon(filename
);
111 return ColorizeIcon(filename
, SingleColor());
114 QIcon
PlatformStyle::SingleColorIcon(const QIcon
& icon
) const
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
);