Line data Source code
1 : // Copyright (c) 2014 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 "networkstyle.h"
6 :
7 : #include "guiconstants.h"
8 :
9 : #include <QApplication>
10 :
11 : static const struct {
12 : const char *networkId;
13 : const char *appName;
14 : const int iconColorHueShift;
15 : const int iconColorSaturationReduction;
16 : const char *titleAddText;
17 : } network_styles[] = {
18 : {"main", QAPP_APP_NAME_DEFAULT, 0, 0, ""},
19 : {"test", QAPP_APP_NAME_TESTNET, 70, 30, QT_TRANSLATE_NOOP("SplashScreen", "[testnet]")},
20 : {"regtest", QAPP_APP_NAME_TESTNET, 160, 30, "[regtest]"}
21 : };
22 : static const unsigned network_styles_count = sizeof(network_styles)/sizeof(*network_styles);
23 :
24 : // titleAddText needs to be const char* for tr()
25 0 : NetworkStyle::NetworkStyle(const QString &appName, const int iconColorHueShift, const int iconColorSaturationReduction, const char *titleAddText):
26 : appName(appName),
27 0 : titleAddText(qApp->translate("SplashScreen", titleAddText))
28 : {
29 : // load pixmap
30 0 : QPixmap pixmap(":/icons/bitcoin");
31 :
32 0 : if(iconColorHueShift != 0 && iconColorSaturationReduction != 0)
33 : {
34 : // generate QImage from QPixmap
35 0 : QImage img = pixmap.toImage();
36 :
37 : int h,s,l,a;
38 :
39 : // traverse though lines
40 0 : for(int y=0;y<img.height();y++)
41 : {
42 0 : QRgb *scL = reinterpret_cast< QRgb *>( img.scanLine( y ) );
43 :
44 : // loop through pixels
45 0 : for(int x=0;x<img.width();x++)
46 : {
47 : // preserve alpha because QColor::getHsl doesen't return the alpha value
48 0 : a = qAlpha(scL[x]);
49 0 : QColor col(scL[x]);
50 :
51 : // get hue value
52 0 : col.getHsl(&h,&s,&l);
53 :
54 : // rotate color on RGB color circle
55 : // 70° should end up with the typical "testnet" green
56 0 : h+=iconColorHueShift;
57 :
58 : // change saturation value
59 0 : if(s>iconColorSaturationReduction)
60 : {
61 0 : s -= iconColorSaturationReduction;
62 : }
63 0 : col.setHsl(h,s,l,a);
64 :
65 : // set the pixel
66 0 : scL[x] = col.rgba();
67 : }
68 : }
69 :
70 : //convert back to QPixmap
71 : #if QT_VERSION >= 0x040700
72 0 : pixmap.convertFromImage(img);
73 : #else
74 : pixmap = QPixmap::fromImage(img);
75 : #endif
76 : }
77 :
78 0 : appIcon = QIcon(pixmap);
79 0 : trayAndWindowIcon = QIcon(pixmap.scaled(QSize(256,256)));
80 0 : }
81 :
82 0 : const NetworkStyle *NetworkStyle::instantiate(const QString &networkId)
83 : {
84 0 : for (unsigned x=0; x<network_styles_count; ++x)
85 : {
86 0 : if (networkId == network_styles[x].networkId)
87 : {
88 : return new NetworkStyle(
89 : network_styles[x].appName,
90 : network_styles[x].iconColorHueShift,
91 : network_styles[x].iconColorSaturationReduction,
92 0 : network_styles[x].titleAddText);
93 : }
94 : }
95 : return 0;
96 : }
|