Skip to content

Commit

Permalink
Merge pull request #3 from presstab/themes
Browse files Browse the repository at this point in the history
Add hot swapping themes
  • Loading branch information
saluscoin authored Jun 5, 2018
2 parents fc509a7 + 56b4448 commit 7636623
Show file tree
Hide file tree
Showing 67 changed files with 2,470 additions and 3 deletions.
190 changes: 189 additions & 1 deletion src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
#include <QUrl>
#include <QMimeData>
#include <QStyle>
#include <QFile>
#include <QTextStream>
#include <QSignalMapper>
#include <QSettings>

#include <iostream>

Expand Down Expand Up @@ -92,6 +96,11 @@ BitcoinGUI::BitcoinGUI(QWidget *parent):
// Accept D&D of URIs
setAcceptDrops(true);

/* zeewolf: Hot swappable wallet themes */
// Discover themes
listThemes(themesList);
/* zeewolf: Hot swappable wallet themes */

// Create actions for the toolbar, menu bar and tray/dock icon
createActions();

Expand Down Expand Up @@ -314,6 +323,25 @@ void BitcoinGUI::createActions()
connect(lockWalletAction, SIGNAL(triggered()), this, SLOT(lockWallet()));
connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));

/* zeewolf: Hot swappable wallet themes */
if (themesList.count()>0)
{
QSignalMapper* signalMapper = new QSignalMapper (this) ;

// Add custom themes (themes directory)
for( int i=0; i < themesList.count(); i++ )
{
QString theme=themesList[i];
customActions[i] = new QAction(QIcon(":/icons/options"), theme, this);
customActions[i]->setToolTip(QString("Switch to " + theme + " theme"));
customActions[i]->setStatusTip(QString("Switch to " + theme + " theme"));
signalMapper->setMapping(customActions[i], theme);
connect(customActions[i], SIGNAL(triggered()), signalMapper, SLOT (map()));
}
connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(changeTheme(QString)));
}
/* zeewolf: Hot swappable wallet themes */
}

void BitcoinGUI::createMenuBar()
Expand All @@ -333,6 +361,16 @@ void BitcoinGUI::createMenuBar()
file->addSeparator();
file->addAction(quitAction);

/* zeewolf: Hot swappable wallet themes */
if (themesList.count()>0)
{
QMenu *themes = appMenuBar->addMenu(tr("&Themes"));
for (int i = 0; i < themesList.count(); i++) {
themes->addAction(customActions[i]);
}
}
/* zeewolf: Hot swappable wallet themes */

QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
settings->addAction(encryptWalletAction);
settings->addAction(changePassphraseAction);
Expand All @@ -352,7 +390,7 @@ static QWidget* makeToolBarSpacer()
{
QWidget* spacer = new QWidget();
spacer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
spacer->setStyleSheet(fUseRedTheme ? "QWidget { background: rgb(37,0,0); }" : "QWidget { background: none; }");
//spacer->setStyleSheet(fUseRedTheme ? "QWidget { background: rgb(37,0,0); }" : "QWidget { background: none; }");
return spacer;
}

Expand Down Expand Up @@ -1042,3 +1080,153 @@ void BitcoinGUI::detectShutdown()
if (ShutdownRequested())
QMetaObject::invokeMethod(QCoreApplication::instance(), "quit", Qt::QueuedConnection);
}

/* zeewolf: Hot swappable wallet themes */
void BitcoinGUI::changeTheme(QString theme)
{
// load Default theme first (if present) to apply default styles
loadTheme("Default");

if (theme != "Default") {
loadTheme(theme);
}
}

void BitcoinGUI::loadTheme(QString theme)
{
// template variables : key => value
QMap<QString, QString> variables;

// path to selected theme dir - for simpler use, just use $theme-dir in qss : url($theme-dir/image.png)
QString themeDir = themesDir + "/" + theme;

// if theme selected
if (theme != "") {
QFile qss(themeDir + "/styles.qss");
// open qss
if (qss.open(QFile::ReadOnly))
{
// read stylesheet
QString styleSheet = QString(qss.readAll());
QTextStream in(&qss);
// rewind
in.seek(0);
bool readingVariables = false;

// seek for variables
while(!in.atEnd()) {
QString line = in.readLine();
// variables starts here
if (line == "/** [VARS]") {
readingVariables = true;
}
// variables end here
if (line == "[/VARS] */") {
break;
}
// if we're reading variables - store them in a map
if (readingVariables == true) {
// skip empty lines
if (line.length()>3 && line.contains('=')) {
QStringList fields = line.split("=");
QString var = fields.at(0).trimmed();
QString value = fields.at(1).trimmed();
variables[var] = value;
}
}
}

// replace path to themes dir
styleSheet.replace("$theme-dir", themeDir);
styleSheet.replace("$themes-dir", themesDir);

QMapIterator<QString, QString> variable(variables);
variable.toBack();
// iterate backwards to prevent overwriting variables
while (variable.hasPrevious()) {
variable.previous();
// replace variables
styleSheet.replace(variable.key(), variable.value());
}

qss.close();

// Apply the result qss file to Qt

/*if (styleSheet.contains("$", Qt::CaseInsensitive)) {
QRegExp rx("(\\$[-\\w]+)");
rx.indexIn(styleSheet);
QString captured = rx.cap(1);
QMessageBox::warning(this, "Theme syntax error", "You have used variable that is not declared " + captured + ". Theme will not be applied.");
} else {*/
qApp->setStyleSheet(styleSheet);
/*}*/
}
} else {
// If not theme name given - clear styles
qApp->setStyleSheet(QString(""));
}

// set selected theme and store it in registry
selectedTheme = theme;
QSettings settings;
settings.setValue("Template", selectedTheme);
}

void BitcoinGUI::listThemes(QStringList& themes)
{
QDir currentDir(qApp->applicationDirPath());
printf("directory %s \n", currentDir.absolutePath().toStdString().c_str());
// try app dir
if (currentDir.cd("themes")) {
printf("directory found 1\n");
} else if (currentDir.cd("src/qt/res/themes")) {
printf("directory found 2\n");
} else if (currentDir.cd("../src/qt/res/themes")) {
printf("directory found 3\n");
} else {
printf("directory not found \n");
return;
}
themesDir = currentDir.path();
currentDir.setFilter(QDir::Dirs);
QStringList entries = currentDir.entryList();
printf("entry count = %d\n", entries.size());
for( QStringList::ConstIterator entry=entries.begin(); entry!=entries.end(); ++entry )
{
QString themeName=*entry;
if(themeName != tr(".") && themeName != tr(".."))
{
themes.append(themeName);
}
}

// get selected theme from registry (if any)
QSettings settings;
selectedTheme = settings.value("Template").toString();
// or use default theme
if (selectedTheme=="") {
selectedTheme = "Default";
}
// load it!
loadTheme(selectedTheme);
}

void BitcoinGUI::keyPressEvent(QKeyEvent * e)
{
switch (e->type())
{
case QEvent::KeyPress:
// $ key
if (e->key() == 36) {
// dev feature: key reloads selected theme
loadTheme(selectedTheme);
}
break;
default:
break;
}

}

/* /zeewolf: Hot swappable wallet themes */
14 changes: 14 additions & 0 deletions src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ class BitcoinGUI : public QMainWindow
/** Create system tray (notification) icon */
void createTrayIcon();

/* Themes support */
QString selectedTheme;
QStringList themesList;
// Path to directory where all themes are (usable for some common images?...)
QString themesDir;
QAction *customActions[100];
/* Themes support */

public slots:
/** Set number of connections shown in the UI */
void setNumConnections(int count);
Expand Down Expand Up @@ -196,6 +204,12 @@ private slots:

/** called by a timer to check if fRequestShutdown has been set **/
void detectShutdown();

/** Load external QSS stylesheet */
void changeTheme(QString theme);
void loadTheme(QString theme);
void listThemes(QStringList& themes);
void keyPressEvent(QKeyEvent * e);
};

#endif // BITCOINGUI_H
25 changes: 23 additions & 2 deletions src/qt/forms/overviewpage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,1">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
Expand Down Expand Up @@ -265,6 +265,25 @@
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="label_overview">
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="../bitcoin.qrc">:/icons/bitcoin</pixmap>
</property>
<property name="scaledContents">
<bool>false</bool>
</property>
<property name="margin">
<number>0</number>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
Expand Down Expand Up @@ -360,6 +379,8 @@
</item>
</layout>
</widget>
<resources/>
<resources>
<include location="../bitcoin.qrc"/>
</resources>
<connections/>
</ui>
Binary file added src/qt/res/themes/Blackwood/arrow-down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/black.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/calendar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/cb-checked-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/cb-checked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/cb-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/cb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/down-arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/r-checked-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/r-checked.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/r-hover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/qt/res/themes/Blackwood/sizegrip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7636623

Please sign in to comment.