Skip to content

Commit 6de50c3

Browse files
laanwjjtimon
authored andcommitted
qt: add network-specific style object
Mainly cleanups: Gets rid of isTestNet everywhere, by keeping track of network-specific theming in a central place. Also makes GUI no longer dependent on the network ID enumeration, which alleviates concerns about #4802.
1 parent dec5892 commit 6de50c3

File tree

8 files changed

+125
-75
lines changed

8 files changed

+125
-75
lines changed

src/Makefile.qt.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ BITCOIN_QT_H = \
178178
qt/macdockiconhandler.h \
179179
qt/macnotificationhandler.h \
180180
qt/monitoreddatamapper.h \
181+
qt/networkstyle.h \
181182
qt/notificator.h \
182183
qt/openuridialog.h \
183184
qt/optionsdialog.h \
@@ -269,6 +270,7 @@ BITCOIN_QT_CPP = \
269270
qt/guiutil.cpp \
270271
qt/intro.cpp \
271272
qt/monitoreddatamapper.cpp \
273+
qt/networkstyle.cpp \
272274
qt/notificator.cpp \
273275
qt/optionsdialog.cpp \
274276
qt/optionsmodel.cpp \

src/qt/bitcoin.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "guiconstants.h"
1313
#include "guiutil.h"
1414
#include "intro.h"
15+
#include "networkstyle.h"
1516
#include "optionsmodel.h"
1617
#include "splashscreen.h"
1718
#include "utilitydialog.h"
@@ -190,9 +191,9 @@ class BitcoinApplication: public QApplication
190191
/// Create options model
191192
void createOptionsModel();
192193
/// Create main window
193-
void createWindow(bool isaTestNet);
194+
void createWindow(const NetworkStyle *networkStyle);
194195
/// Create splash screen
195-
void createSplashScreen(bool isaTestNet);
196+
void createSplashScreen(const NetworkStyle *networkStyle);
196197

197198
/// Request core initialization
198199
void requestInitialize();
@@ -331,18 +332,18 @@ void BitcoinApplication::createOptionsModel()
331332
optionsModel = new OptionsModel();
332333
}
333334

334-
void BitcoinApplication::createWindow(bool isaTestNet)
335+
void BitcoinApplication::createWindow(const NetworkStyle *networkStyle)
335336
{
336-
window = new BitcoinGUI(isaTestNet, 0);
337+
window = new BitcoinGUI(networkStyle, 0);
337338

338339
pollShutdownTimer = new QTimer(window);
339340
connect(pollShutdownTimer, SIGNAL(timeout()), window, SLOT(detectShutdown()));
340341
pollShutdownTimer->start(200);
341342
}
342343

343-
void BitcoinApplication::createSplashScreen(bool isaTestNet)
344+
void BitcoinApplication::createSplashScreen(const NetworkStyle *networkStyle)
344345
{
345-
SplashScreen *splash = new SplashScreen(0, isaTestNet);
346+
SplashScreen *splash = new SplashScreen(0, networkStyle);
346347
// We don't hold a direct pointer to the splash screen after creation, so use
347348
// Qt::WA_DeleteOnClose to make sure that the window will be deleted eventually.
348349
splash->setAttribute(Qt::WA_DeleteOnClose);
@@ -572,12 +573,10 @@ int main(int argc, char *argv[])
572573
if (!PaymentServer::ipcParseCommandLine(argc, argv))
573574
exit(0);
574575
#endif
575-
bool isaTestNet = Params().NetworkID() != CBaseChainParams::MAIN;
576+
QScopedPointer<const NetworkStyle> networkStyle(NetworkStyle::instantiate(QString::fromStdString(Params().NetworkIDString())));
577+
assert(!networkStyle.isNull());
576578
// Allow for separate UI settings for testnets
577-
if (isaTestNet)
578-
QApplication::setApplicationName(QAPP_APP_NAME_TESTNET);
579-
else
580-
QApplication::setApplicationName(QAPP_APP_NAME_DEFAULT);
579+
QApplication::setApplicationName(networkStyle->getAppName());
581580
// Re-initialize translations after changing application name (language in network-specific settings can be different)
582581
initTranslations(qtTranslatorBase, qtTranslator, translatorBase, translator);
583582

@@ -617,11 +616,11 @@ int main(int argc, char *argv[])
617616
uiInterface.InitMessage.connect(InitMessage);
618617

619618
if (GetBoolArg("-splash", true) && !GetBoolArg("-min", false))
620-
app.createSplashScreen(isaTestNet);
619+
app.createSplashScreen(networkStyle.data());
621620

622621
try
623622
{
624-
app.createWindow(isaTestNet);
623+
app.createWindow(networkStyle.data());
625624
app.requestInitialize();
626625
#if defined(Q_OS_WIN) && QT_VERSION >= 0x050000
627626
WinShutdownMonitor::registerShutdownBlockReason(QObject::tr("Bitcoin Core didn't yet exit safely..."), (HWND)app.getMainWinId());

src/qt/bitcoingui.cpp

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "clientmodel.h"
99
#include "guiconstants.h"
1010
#include "guiutil.h"
11+
#include "networkstyle.h"
1112
#include "notificator.h"
1213
#include "openuridialog.h"
1314
#include "optionsdialog.h"
@@ -59,7 +60,7 @@
5960

6061
const QString BitcoinGUI::DEFAULT_WALLET = "~Default";
6162

62-
BitcoinGUI::BitcoinGUI(bool fIsTestnet, QWidget *parent) :
63+
BitcoinGUI::BitcoinGUI(const NetworkStyle *networkStyle, QWidget *parent) :
6364
QMainWindow(parent),
6465
clientModel(0),
6566
walletFrame(0),
@@ -112,26 +113,13 @@ BitcoinGUI::BitcoinGUI(bool fIsTestnet, QWidget *parent) :
112113
} else {
113114
windowTitle += tr("Node");
114115
}
115-
116-
if (!fIsTestnet)
117-
{
116+
windowTitle += " " + networkStyle->getTitleAddText();
118117
#ifndef Q_OS_MAC
119-
QApplication::setWindowIcon(QIcon(":icons/bitcoin"));
120-
setWindowIcon(QIcon(":icons/bitcoin"));
118+
QApplication::setWindowIcon(networkStyle->getAppIcon());
119+
setWindowIcon(networkStyle->getAppIcon());
121120
#else
122-
MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin"));
121+
MacDockIconHandler::instance()->setIcon(networkStyle->getAppIcon());
123122
#endif
124-
}
125-
else
126-
{
127-
windowTitle += " " + tr("[testnet]");
128-
#ifndef Q_OS_MAC
129-
QApplication::setWindowIcon(QIcon(":icons/bitcoin_testnet"));
130-
setWindowIcon(QIcon(":icons/bitcoin_testnet"));
131-
#else
132-
MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
133-
#endif
134-
}
135123
setWindowTitle(windowTitle);
136124

137125
#if defined(Q_OS_MAC) && QT_VERSION < 0x050000
@@ -161,7 +149,7 @@ BitcoinGUI::BitcoinGUI(bool fIsTestnet, QWidget *parent) :
161149

162150
// Create actions for the toolbar, menu bar and tray/dock icon
163151
// Needs walletFrame to be initialized
164-
createActions(fIsTestnet);
152+
createActions(networkStyle);
165153

166154
// Create application menu bar
167155
createMenuBar();
@@ -170,7 +158,7 @@ BitcoinGUI::BitcoinGUI(bool fIsTestnet, QWidget *parent) :
170158
createToolBars();
171159

172160
// Create system tray icon and notification
173-
createTrayIcon(fIsTestnet);
161+
createTrayIcon(networkStyle);
174162

175163
// Create status bar
176164
statusBar();
@@ -248,7 +236,7 @@ BitcoinGUI::~BitcoinGUI()
248236
#endif
249237
}
250238

251-
void BitcoinGUI::createActions(bool fIsTestnet)
239+
void BitcoinGUI::createActions(const NetworkStyle *networkStyle)
252240
{
253241
QActionGroup *tabGroup = new QActionGroup(this);
254242

@@ -295,10 +283,7 @@ void BitcoinGUI::createActions(bool fIsTestnet)
295283
quitAction->setStatusTip(tr("Quit application"));
296284
quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
297285
quitAction->setMenuRole(QAction::QuitRole);
298-
if (!fIsTestnet)
299-
aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About Bitcoin Core"), this);
300-
else
301-
aboutAction = new QAction(QIcon(":/icons/bitcoin_testnet"), tr("&About Bitcoin Core"), this);
286+
aboutAction = new QAction(networkStyle->getAppIcon(), tr("&About Bitcoin Core"), this);
302287
aboutAction->setStatusTip(tr("Show information about Bitcoin Core"));
303288
aboutAction->setMenuRole(QAction::AboutRole);
304289
#if QT_VERSION < 0x050000
@@ -311,10 +296,7 @@ void BitcoinGUI::createActions(bool fIsTestnet)
311296
optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
312297
optionsAction->setStatusTip(tr("Modify configuration options for Bitcoin"));
313298
optionsAction->setMenuRole(QAction::PreferencesRole);
314-
if (!fIsTestnet)
315-
toggleHideAction = new QAction(QIcon(":/icons/bitcoin"), tr("&Show / Hide"), this);
316-
else
317-
toggleHideAction = new QAction(QIcon(":/icons/bitcoin_testnet"), tr("&Show / Hide"), this);
299+
toggleHideAction = new QAction(networkStyle->getAppIcon(), tr("&Show / Hide"), this);
318300
toggleHideAction->setStatusTip(tr("Show or hide the main Window"));
319301

320302
encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet..."), this);
@@ -505,22 +487,13 @@ void BitcoinGUI::setWalletActionsEnabled(bool enabled)
505487
openAction->setEnabled(enabled);
506488
}
507489

508-
void BitcoinGUI::createTrayIcon(bool fIsTestnet)
490+
void BitcoinGUI::createTrayIcon(const NetworkStyle *networkStyle)
509491
{
510492
#ifndef Q_OS_MAC
511493
trayIcon = new QSystemTrayIcon(this);
512-
513-
if (!fIsTestnet)
514-
{
515-
trayIcon->setToolTip(tr("Bitcoin Core client"));
516-
trayIcon->setIcon(QIcon(":/icons/bitcoin"));
517-
}
518-
else
519-
{
520-
trayIcon->setToolTip(tr("Bitcoin Core client") + " " + tr("[testnet]"));
521-
trayIcon->setIcon(QIcon(":/icons/bitcoin_testnet"));
522-
}
523-
494+
QString toolTip = tr("Bitcoin Core client") + " " + networkStyle->getTitleAddText();
495+
trayIcon->setToolTip(toolTip);
496+
trayIcon->setIcon(networkStyle->getAppIcon());
524497
trayIcon->show();
525498
#endif
526499

src/qt/bitcoingui.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <QSystemTrayIcon>
2020

2121
class ClientModel;
22+
class NetworkStyle;
2223
class Notificator;
2324
class OptionsModel;
2425
class RPCConsole;
@@ -46,7 +47,7 @@ class BitcoinGUI : public QMainWindow
4647
public:
4748
static const QString DEFAULT_WALLET;
4849

49-
explicit BitcoinGUI(bool fIsTestnet = false, QWidget *parent = 0);
50+
explicit BitcoinGUI(const NetworkStyle *networkStyle, QWidget *parent = 0);
5051
~BitcoinGUI();
5152

5253
/** Set the client model.
@@ -114,13 +115,13 @@ class BitcoinGUI : public QMainWindow
114115
int spinnerFrame;
115116

116117
/** Create the main UI actions. */
117-
void createActions(bool fIsTestnet);
118+
void createActions(const NetworkStyle *networkStyle);
118119
/** Create the menu bar and sub-menus. */
119120
void createMenuBar();
120121
/** Create the toolbars */
121122
void createToolBars();
122123
/** Create system tray icon and notification */
123-
void createTrayIcon(bool fIsTestnet);
124+
void createTrayIcon(const NetworkStyle *networkStyle);
124125
/** Create system tray menu (or setup the dock menu) */
125126
void createTrayIconMenu();
126127

src/qt/networkstyle.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2014 The Bitcoin 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 char *appIcon;
15+
const char *titleAddText;
16+
const char *splashImage;
17+
} network_styles[] = {
18+
{"main", QAPP_APP_NAME_DEFAULT, ":/icons/bitcoin", "", ":/images/splash"},
19+
{"test", QAPP_APP_NAME_TESTNET, ":/icons/bitcoin_testnet", QT_TRANSLATE_NOOP("SplashScreen", "[testnet]"), ":/images/splash_testnet"},
20+
{"regtest", QAPP_APP_NAME_TESTNET, ":/icons/bitcoin_testnet", "[regtest]", ":/images/splash_testnet"}
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+
NetworkStyle::NetworkStyle(const QString &appName, const QString &appIcon, const char *titleAddText, const QString &splashImage):
26+
appName(appName),
27+
appIcon(appIcon),
28+
titleAddText(qApp->translate("SplashScreen", titleAddText)),
29+
splashImage(splashImage)
30+
{
31+
}
32+
33+
const NetworkStyle *NetworkStyle::instantiate(const QString &networkId)
34+
{
35+
for (unsigned x=0; x<network_styles_count; ++x)
36+
{
37+
if (networkId == network_styles[x].networkId)
38+
{
39+
return new NetworkStyle(
40+
network_styles[x].appName,
41+
network_styles[x].appIcon,
42+
network_styles[x].titleAddText,
43+
network_styles[x].splashImage);
44+
}
45+
}
46+
return 0;
47+
}

src/qt/networkstyle.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2014 The Bitcoin 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+
#ifndef H_NETWORKSTYLE
6+
#define H_NETWORKSTYLE
7+
8+
#include <QIcon>
9+
#include <QPixmap>
10+
#include <QString>
11+
12+
/* Coin network-specific GUI style information */
13+
class NetworkStyle
14+
{
15+
public:
16+
/** Get style associated with provided BIP70 network id, or 0 if not known */
17+
static const NetworkStyle *instantiate(const QString &networkId);
18+
19+
const QString &getAppName() const { return appName; }
20+
const QIcon &getAppIcon() const { return appIcon; }
21+
const QString &getTitleAddText() const { return titleAddText; }
22+
const QPixmap &getSplashImage() const { return splashImage; }
23+
24+
private:
25+
NetworkStyle(const QString &appName, const QString &appIcon, const char *titleAddText, const QString &splashImage);
26+
27+
QString appName;
28+
QIcon appIcon;
29+
QString titleAddText;
30+
QPixmap splashImage;
31+
};
32+
33+
#endif // H_NETWORKSTYLE

src/qt/splashscreen.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "clientversion.h"
88
#include "init.h"
9+
#include "networkstyle.h"
910
#include "ui_interface.h"
1011
#include "util.h"
1112
#include "version.h"
@@ -19,7 +20,7 @@
1920
#include <QDesktopWidget>
2021
#include <QPainter>
2122

22-
SplashScreen::SplashScreen(Qt::WindowFlags f, bool isTestNet) :
23+
SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle) :
2324
QWidget(0, f), curAlignment(0)
2425
{
2526
// set reference point, paddings
@@ -34,17 +35,12 @@ SplashScreen::SplashScreen(Qt::WindowFlags f, bool isTestNet) :
3435
QString titleText = tr("Bitcoin Core");
3536
QString versionText = QString("Version %1").arg(QString::fromStdString(FormatFullVersion()));
3637
QString copyrightText = QChar(0xA9)+QString(" 2009-%1 ").arg(COPYRIGHT_YEAR) + QString(tr("The Bitcoin Core developers"));
37-
QString testnetAddText = QString(tr("[testnet]")); // define text to place as single text object
38+
QString titleAddText = networkStyle->getTitleAddText();
3839

3940
QString font = "Arial";
4041

4142
// load the bitmap for writing some text over it
42-
if(isTestNet) {
43-
pixmap = QPixmap(":/images/splash_testnet");
44-
}
45-
else {
46-
pixmap = QPixmap(":/images/splash");
47-
}
43+
pixmap = networkStyle->getSplashImage();
4844

4945
QPainter pixPaint(&pixmap);
5046
pixPaint.setPen(QColor(100,100,100));
@@ -78,23 +74,20 @@ SplashScreen::SplashScreen(Qt::WindowFlags f, bool isTestNet) :
7874
pixPaint.setFont(QFont(font, 10*fontFactor));
7975
pixPaint.drawText(pixmap.width()-titleTextWidth-paddingRight,paddingTop+titleCopyrightVSpace,copyrightText);
8076

81-
// draw testnet string if testnet is on
82-
if(isTestNet) {
77+
// draw additional text if special network
78+
if(!titleAddText.isEmpty()) {
8379
QFont boldFont = QFont(font, 10*fontFactor);
8480
boldFont.setWeight(QFont::Bold);
8581
pixPaint.setFont(boldFont);
8682
fm = pixPaint.fontMetrics();
87-
int testnetAddTextWidth = fm.width(testnetAddText);
88-
pixPaint.drawText(pixmap.width()-testnetAddTextWidth-10,15,testnetAddText);
83+
int titleAddTextWidth = fm.width(titleAddText);
84+
pixPaint.drawText(pixmap.width()-titleAddTextWidth-10,15,titleAddText);
8985
}
9086

9187
pixPaint.end();
9288

9389
// Set window title
94-
if(isTestNet)
95-
setWindowTitle(titleText + " " + testnetAddText);
96-
else
97-
setWindowTitle(titleText);
90+
setWindowTitle(titleText + " " + titleAddText);
9891

9992
// Resize window and move to center of desktop, disallow resizing
10093
QRect r(QPoint(), pixmap.size());

0 commit comments

Comments
 (0)