Skip to content

Commit 00cb69b

Browse files
committed
[Qt] allow to execute a callback during splashscreen progress
1 parent 90a002e commit 00cb69b

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

src/qt/bitcoin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ int main(int argc, char *argv[])
578578
// Need to pass name here as CAmount is a typedef (see http://qt-project.org/doc/qt-5/qmetatype.html#qRegisterMetaType)
579579
// IMPORTANT if it is no longer a typedef use the normal variant above
580580
qRegisterMetaType< CAmount >("CAmount");
581+
qRegisterMetaType< std::function<void(void)> >("std::function<void(void)>");
581582

582583
/// 3. Application identification
583584
// must be set before OptionsModel is initialized or translations are loaded,

src/qt/splashscreen.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,24 @@ SplashScreen::SplashScreen(Qt::WindowFlags f, const NetworkStyle *networkStyle)
131131
move(QApplication::desktop()->screenGeometry().center() - r.center());
132132

133133
subscribeToCoreSignals();
134+
installEventFilter(this);
134135
}
135136

136137
SplashScreen::~SplashScreen()
137138
{
138139
unsubscribeFromCoreSignals();
139140
}
140141

142+
bool SplashScreen::eventFilter(QObject * obj, QEvent * ev) {
143+
if (ev->type() == QEvent::KeyPress) {
144+
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
145+
if(keyEvent->text()[0] == 'q' && breakAction != nullptr) {
146+
breakAction();
147+
}
148+
}
149+
return QObject::eventFilter(obj, ev);
150+
}
151+
141152
void SplashScreen::slotFinish(QWidget *mainWin)
142153
{
143154
Q_UNUSED(mainWin);
@@ -164,6 +175,18 @@ static void ShowProgress(SplashScreen *splash, const std::string &title, int nPr
164175
InitMessage(splash, title + strprintf("%d", nProgress) + "%");
165176
}
166177

178+
void SplashScreen::setBreakAction(const std::function<void(void)> &action)
179+
{
180+
breakAction = action;
181+
}
182+
183+
static void SetProgressBreakAction(SplashScreen *splash, const std::function<void(void)> &action)
184+
{
185+
QMetaObject::invokeMethod(splash, "setBreakAction",
186+
Qt::QueuedConnection,
187+
Q_ARG(std::function<void(void)>, action));
188+
}
189+
167190
#ifdef ENABLE_WALLET
168191
void SplashScreen::ConnectWallet(CWallet* wallet)
169192
{
@@ -177,6 +200,7 @@ void SplashScreen::subscribeToCoreSignals()
177200
// Connect signals to client
178201
uiInterface.InitMessage.connect(boost::bind(InitMessage, this, _1));
179202
uiInterface.ShowProgress.connect(boost::bind(ShowProgress, this, _1, _2));
203+
uiInterface.SetProgressBreakAction.connect(boost::bind(SetProgressBreakAction, this, _1));
180204
#ifdef ENABLE_WALLET
181205
uiInterface.LoadWallet.connect(boost::bind(&SplashScreen::ConnectWallet, this, _1));
182206
#endif

src/qt/splashscreen.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_QT_SPLASHSCREEN_H
66
#define BITCOIN_QT_SPLASHSCREEN_H
77

8+
#include <functional>
89
#include <QSplashScreen>
910

1011
class CWallet;
@@ -35,6 +36,11 @@ public Q_SLOTS:
3536
/** Show message and progress */
3637
void showMessage(const QString &message, int alignment, const QColor &color);
3738

39+
/** Sets the break action */
40+
void setBreakAction(const std::function<void(void)> &action);
41+
protected:
42+
bool eventFilter(QObject * obj, QEvent * ev);
43+
3844
private:
3945
/** Connect core signals to splash screen */
4046
void subscribeToCoreSignals();
@@ -49,6 +55,8 @@ public Q_SLOTS:
4955
int curAlignment;
5056

5157
QList<CWallet*> connectedWallets;
58+
59+
std::function<void(void)> breakAction;
5260
};
5361

5462
#endif // BITCOIN_QT_SPLASHSCREEN_H

src/ui_interface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class CClientUIInterface
9797
/** Show progress e.g. for verifychain */
9898
boost::signals2::signal<void (const std::string &title, int nProgress)> ShowProgress;
9999

100+
/** Set progress break action (possible "cancel button" triggers that action) */
101+
boost::signals2::signal<void (std::function<void(void)> action)> SetProgressBreakAction;
102+
100103
/** New block has been accepted */
101104
boost::signals2::signal<void (bool, const CBlockIndex *)> NotifyBlockTip;
102105

0 commit comments

Comments
 (0)