Skip to content

Commit 3ad6d11

Browse files
committed
qml: Add start/shutdown functionality
1 parent 9bc8a6d commit 3ad6d11

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

src/qml/bitcoin.cpp

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <noui.h>
1212
#include <qml/nodemodel.h>
1313
#include <qt/guiconstants.h>
14+
#include <qt/initexecutor.h>
1415
#include <util/system.h>
1516
#include <util/translation.h>
1617

@@ -36,20 +37,50 @@ void SetupUIArgs(ArgsManager& argsman)
3637

3738
int QmlGuiMain(int argc, char* argv[])
3839
{
39-
NodeContext node_context;
40-
std::unique_ptr<interfaces::Node> node = interfaces::MakeNode(&node_context);
41-
42-
// Subscribe to global signals from core
43-
boost::signals2::scoped_connection handler_message_box = ::uiInterface.ThreadSafeMessageBox_connect(noui_ThreadSafeMessageBox);
44-
boost::signals2::scoped_connection handler_question = ::uiInterface.ThreadSafeQuestion_connect(noui_ThreadSafeQuestion);
45-
boost::signals2::scoped_connection handler_init_message = ::uiInterface.InitMessage_connect(noui_InitMessage);
46-
4740
Q_INIT_RESOURCE(bitcoin_qml);
4841

4942
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
5043
QGuiApplication app(argc, argv);
5144

45+
// Parse command-line options. We do this after qt in order to show an error if there are problems parsing these.
46+
SetupServerArgs(gArgs);
47+
SetupUIArgs(gArgs);
48+
std::string error;
49+
if (!gArgs.ParseParameters(argc, argv, error)) {
50+
InitError(strprintf(Untranslated("Error parsing command line arguments: %s\n"), error));
51+
return EXIT_FAILURE;
52+
}
53+
54+
CheckDataDirOption();
55+
56+
gArgs.ReadConfigFiles(error, true);
57+
58+
SelectParams(gArgs.GetChainName());
59+
60+
// Default printtoconsole to false for the GUI. GUI programs should not
61+
// print to the console unnecessarily.
62+
gArgs.SoftSetBoolArg("-printtoconsole", false);
63+
InitLogging(gArgs);
64+
InitParameterInteraction(gArgs);
65+
66+
NodeContext node_context;
67+
node_context.args = &gArgs;
68+
std::unique_ptr<interfaces::Node> node = interfaces::MakeNode(&node_context);
69+
node->baseInitialize();
70+
5271
NodeModel node_model;
72+
InitExecutor init_executor{*node};
73+
QObject::connect(&node_model, &NodeModel::requestedInitialize, &init_executor, &InitExecutor::initialize);
74+
QObject::connect(&node_model, &NodeModel::requestedShutdown, &init_executor, &InitExecutor::shutdown);
75+
// QObject::connect(&init_executor, &InitExecutor::initializeResult, &node_model, &NodeModel::initializeResult);
76+
QObject::connect(&init_executor, &InitExecutor::shutdownResult, qGuiApp, &QGuiApplication::quit, Qt::QueuedConnection);
77+
// QObject::connect(&init_executor, &InitExecutor::runawayException, &node_model, &NodeModel::handleRunawayException);
78+
79+
qGuiApp->setQuitOnLastWindowClosed(false);
80+
QObject::connect(qGuiApp, &QGuiApplication::lastWindowClosed, [&] {
81+
node->startShutdown();
82+
node_model.startNodeShutdown();
83+
});
5384

5485
QQmlApplicationEngine engine;
5586
engine.rootContext()->setContextProperty("nodeModel", &node_model);
@@ -59,14 +90,5 @@ int QmlGuiMain(int argc, char* argv[])
5990
return EXIT_FAILURE;
6091
}
6192

62-
// Parse command-line options. We do this after qt in order to show an error if there are problems parsing these.
63-
SetupServerArgs(gArgs);
64-
SetupUIArgs(gArgs);
65-
std::string error;
66-
if (!gArgs.ParseParameters(argc, argv, error)) {
67-
InitError(strprintf(Untranslated("Error parsing command line arguments: %s\n"), error));
68-
return EXIT_FAILURE;
69-
}
70-
71-
return app.exec();
93+
return qGuiApp->exec();
7294
}

src/qml/nodemodel.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <qml/nodemodel.h>
6+
7+
#include <QDebug>
8+
9+
void NodeModel::startNodeInitializionThread()
10+
{
11+
Q_EMIT requestedInitialize();
12+
}
13+
14+
void NodeModel::startNodeShutdown()
15+
{
16+
Q_EMIT requestedShutdown();
17+
}

src/qml/nodemodel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class NodeModel : public QObject
1212
{
1313
Q_OBJECT
1414

15+
public:
16+
Q_INVOKABLE void startNodeInitializionThread();
17+
void startNodeShutdown();
18+
19+
Q_SIGNALS:
20+
void requestedInitialize();
21+
void requestedShutdown();
1522
};
1623

1724
#endif // BITCOIN_QML_NODEMODEL_H

src/qml/pages/stub.qml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import QtQml 2.12
12
import QtQuick.Controls 2.12
23

34
ApplicationWindow {
@@ -6,4 +7,6 @@ ApplicationWindow {
67
minimumWidth: 750
78
minimumHeight: 450
89
visible: true
10+
11+
Component.onCompleted: nodeModel.startNodeInitializionThread();
912
}

0 commit comments

Comments
 (0)