utilitydialog.cpp raw
1 // Copyright (c) 2011-2022 The Limenka 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 <limenka-build-config.h> // IWYU pragma: keep
6
7 #include <qt/utilitydialog.h>
8
9 #include <qt/forms/ui_helpmessagedialog.h>
10
11 #include <qt/guiutil.h>
12
13 #include <clientversion.h>
14 #include <common/args.h>
15 #include <init.h>
16 #include <util/strencodings.h>
17
18 #include <cstdio>
19
20 #include <QCloseEvent>
21 #include <QLabel>
22 #include <QMainWindow>
23 #include <QRegularExpression>
24 #include <QString>
25 #include <QTextCursor>
26 #include <QTextTable>
27 #include <QVBoxLayout>
28
29 /** "Help message" or "About" dialog box */
30 HelpMessageDialog::HelpMessageDialog(QWidget *parent, bool about) :
31 QDialog(parent, GUIUtil::dialog_flags),
32 ui(new Ui::HelpMessageDialog)
33 {
34 ui->setupUi(this);
35
36 QString version = QString{CLIENT_NAME} + " " + tr("version") + " " + QString::fromStdString(FormatFullVersion());
37
38 if (about)
39 {
40 setWindowTitle(tr("About %1").arg(CLIENT_NAME));
41
42 std::string licenseInfo = LicenseInfo();
43 /// HTML-format the license message from the core
44 QString licenseInfoHTML = QString::fromStdString(LicenseInfo());
45 // Make URLs clickable
46 QRegularExpression uri(QStringLiteral("<(.*)>"), QRegularExpression::InvertedGreedinessOption);
47 licenseInfoHTML.replace(uri, QStringLiteral("<a href=\"\\1\">\\1</a>"));
48 // Replace newlines with HTML breaks
49 licenseInfoHTML.replace("\n", "<br>");
50
51 ui->aboutMessage->setTextFormat(Qt::RichText);
52 ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
53 text = version + "\n" + QString::fromStdString(FormatParagraph(licenseInfo));
54 ui->aboutMessage->setText(version + "<br><br>" + licenseInfoHTML);
55 ui->aboutMessage->setWordWrap(true);
56 ui->helpMessage->setVisible(false);
57 } else {
58 setWindowTitle(tr("Command-line options"));
59 QString header = "The limenka-qt application provides a graphical interface for interacting with " CLIENT_NAME ".\n\n"
60 "It combines the core functionalities of limenkad with a user-friendly interface for wallet management, transaction history, and network statistics.\n\n"
61 "It is suitable for users who prefer a graphical over a command-line interface.\n\n"
62 "You can optionally specify a payment [URI], in e.g. the BIP20 or BIP21 URI formats.\n\n"
63 "Usage: limenka-qt [options] [URI]\n\n";
64 QTextCursor cursor(ui->helpMessage->document());
65 cursor.insertText(version);
66 cursor.insertBlock();
67 cursor.insertText(header);
68 cursor.insertBlock();
69
70 std::string strUsage = gArgs.GetHelpMessage();
71 QString coreOptions = QString::fromStdString(strUsage);
72 text = version + "\n\n" + header + "\n" + coreOptions;
73
74 QTextTableFormat tf;
75 tf.setBorderStyle(QTextFrameFormat::BorderStyle_None);
76 tf.setCellPadding(2);
77 QVector<QTextLength> widths;
78 widths << QTextLength(QTextLength::PercentageLength, 35);
79 widths << QTextLength(QTextLength::PercentageLength, 65);
80 tf.setColumnWidthConstraints(widths);
81
82 QTextCharFormat bold;
83 bold.setFontWeight(QFont::Bold);
84
85 for (const QString &line : coreOptions.split("\n")) {
86 if (line.startsWith(" -"))
87 {
88 cursor.currentTable()->appendRows(1);
89 cursor.movePosition(QTextCursor::PreviousCell);
90 cursor.movePosition(QTextCursor::NextRow);
91 cursor.insertText(line.trimmed());
92 cursor.movePosition(QTextCursor::NextCell);
93 } else if (line.startsWith(" ")) {
94 cursor.insertText(line.trimmed()+' ');
95 } else if (line.size() > 0) {
96 //Title of a group
97 if (cursor.currentTable())
98 cursor.currentTable()->appendRows(1);
99 cursor.movePosition(QTextCursor::Down);
100 cursor.insertText(line.trimmed(), bold);
101 cursor.insertTable(1, 2, tf);
102 }
103 }
104
105 ui->helpMessage->moveCursor(QTextCursor::Start);
106 ui->scrollArea->setVisible(false);
107 ui->aboutLogo->setVisible(false);
108 }
109
110 GUIUtil::handleCloseWindowShortcut(this);
111 }
112
113 HelpMessageDialog::~HelpMessageDialog()
114 {
115 delete ui;
116 }
117
118 void HelpMessageDialog::printToConsole()
119 {
120 // On other operating systems, the expected action is to print the message to the console.
121 tfm::format(std::cout, "%s", qPrintable(text));
122 }
123
124 void HelpMessageDialog::showOrPrint()
125 {
126 #if defined(WIN32)
127 // On Windows, show a message box, as there is no stderr/stdout in windowed applications
128 exec();
129 #else
130 // On other operating systems, print help text to console
131 printToConsole();
132 #endif
133 }
134
135 void HelpMessageDialog::on_okButton_accepted()
136 {
137 close();
138 }
139
140
141 /** "Shutdown" window */
142 ShutdownWindow::ShutdownWindow(QWidget *parent, Qt::WindowFlags f):
143 QWidget(parent, f)
144 {
145 QVBoxLayout *layout = new QVBoxLayout();
146 layout->addWidget(new QLabel(
147 tr("%1 is shutting down…").arg(CLIENT_NAME) + "<br /><br />" +
148 tr("Do not shut down the computer until this window disappears.")));
149 setLayout(layout);
150
151 GUIUtil::handleCloseWindowShortcut(this);
152 }
153
154 QWidget* ShutdownWindow::showShutdownWindow(QMainWindow* window)
155 {
156 assert(window != nullptr);
157
158 // Show a simple window indicating shutdown status
159 QWidget *shutdownWindow = new ShutdownWindow();
160 shutdownWindow->setWindowTitle(window->windowTitle());
161
162 // Center shutdown window at where main window was
163 const QPoint global = window->mapToGlobal(window->rect().center());
164 shutdownWindow->move(global.x() - shutdownWindow->width() / 2, global.y() - shutdownWindow->height() / 2);
165 shutdownWindow->show();
166 return shutdownWindow;
167 }
168
169 void ShutdownWindow::closeEvent(QCloseEvent *event)
170 {
171 event->ignore();
172 }
173