Bitcoin Core  21.99.0
P2P Digital Currency
networkstyle.cpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2019 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/networkstyle.h>
6 
7 #include <qt/guiconstants.h>
8 
9 #include <chainparamsbase.h>
10 #include <tinyformat.h>
11 
12 #include <QApplication>
13 
14 static const struct {
15  const char *networkId;
16  const char *appName;
17  const int iconColorHueShift;
19 } network_styles[] = {
20  {"main", QAPP_APP_NAME_DEFAULT, 0, 0},
21  {"test", QAPP_APP_NAME_TESTNET, 70, 30},
22  {"signet", QAPP_APP_NAME_SIGNET, 35, 15},
23  {"regtest", QAPP_APP_NAME_REGTEST, 160, 30},
24 };
25 
26 // titleAddText needs to be const char* for tr()
27 NetworkStyle::NetworkStyle(const QString &_appName, const int iconColorHueShift, const int iconColorSaturationReduction, const char *_titleAddText):
28  appName(_appName),
29  titleAddText(qApp->translate("SplashScreen", _titleAddText))
30 {
31  // load pixmap
32  QPixmap pixmap(":/icons/bitcoin");
33 
35  {
36  // generate QImage from QPixmap
37  QImage img = pixmap.toImage();
38 
39  int h,s,l,a;
40 
41  // traverse though lines
42  for(int y=0;y<img.height();y++)
43  {
44  QRgb *scL = reinterpret_cast< QRgb *>( img.scanLine( y ) );
45 
46  // loop through pixels
47  for(int x=0;x<img.width();x++)
48  {
49  // preserve alpha because QColor::getHsl doesn't return the alpha value
50  a = qAlpha(scL[x]);
51  QColor col(scL[x]);
52 
53  // get hue value
54  col.getHsl(&h,&s,&l);
55 
56  // rotate color on RGB color circle
57  // 70° should end up with the typical "testnet" green
59 
60  // change saturation value
62  {
64  }
65  col.setHsl(h,s,l,a);
66 
67  // set the pixel
68  scL[x] = col.rgba();
69  }
70  }
71 
72  //convert back to QPixmap
73  pixmap.convertFromImage(img);
74  }
75 
76  appIcon = QIcon(pixmap);
77  trayAndWindowIcon = QIcon(pixmap.scaled(QSize(256,256)));
78 }
79 
81 {
82  std::string titleAddText = networkId == CBaseChainParams::MAIN ? "" : strprintf("[%s]", networkId);
83  for (const auto& network_style : network_styles) {
84  if (networkId == network_style.networkId) {
85  return new NetworkStyle(
86  network_style.appName,
87  network_style.iconColorHueShift,
88  network_style.iconColorSaturationReduction,
89  titleAddText.c_str());
90  }
91  }
92  return nullptr;
93 }
chainparamsbase.h
NetworkStyle
Definition: networkstyle.h:13
NetworkStyle::titleAddText
QString titleAddText
Definition: networkstyle.h:30
iconColorSaturationReduction
const int iconColorSaturationReduction
Definition: networkstyle.cpp:18
tinyformat.h
NetworkStyle::NetworkStyle
NetworkStyle(const QString &appName, const int iconColorHueShift, const int iconColorSaturationReduction, const char *titleAddText)
Definition: networkstyle.cpp:27
QAPP_APP_NAME_DEFAULT
#define QAPP_APP_NAME_DEFAULT
Definition: guiconstants.h:47
NetworkStyle::appIcon
QIcon appIcon
Definition: networkstyle.h:28
QAPP_APP_NAME_TESTNET
#define QAPP_APP_NAME_TESTNET
Definition: guiconstants.h:48
networkId
const char * networkId
Definition: networkstyle.cpp:15
QAPP_APP_NAME_SIGNET
#define QAPP_APP_NAME_SIGNET
Definition: guiconstants.h:49
network_styles
static const struct @5 network_styles[]
NetworkStyle::trayAndWindowIcon
QIcon trayAndWindowIcon
Definition: networkstyle.h:29
CBaseChainParams::MAIN
static const std::string MAIN
Chain name strings.
Definition: chainparamsbase.h:22
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
iconColorHueShift
const int iconColorHueShift
Definition: networkstyle.cpp:17
guiconstants.h
NetworkStyle::instantiate
static const NetworkStyle * instantiate(const std::string &networkId)
Get style associated with provided network id, or 0 if not known.
Definition: networkstyle.cpp:80
networkstyle.h
appName
const char * appName
Definition: networkstyle.cpp:16
QAPP_APP_NAME_REGTEST
#define QAPP_APP_NAME_REGTEST
Definition: guiconstants.h:50