Bitcoin Core  27.99.0
P2P Digital Currency
utilitydialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 The Bitcoin Core 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 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <qt/utilitydialog.h>
10 
11 #include <qt/forms/ui_helpmessagedialog.h>
12 
13 #include <qt/guiutil.h>
14 
15 #include <clientversion.h>
16 #include <common/args.h>
17 #include <init.h>
18 #include <util/strencodings.h>
19 
20 #include <cstdio>
21 
22 #include <QCloseEvent>
23 #include <QLabel>
24 #include <QMainWindow>
25 #include <QRegularExpression>
26 #include <QString>
27 #include <QTextCursor>
28 #include <QTextTable>
29 #include <QVBoxLayout>
30 
32 HelpMessageDialog::HelpMessageDialog(QWidget *parent, bool about) :
33  QDialog(parent, GUIUtil::dialog_flags),
34  ui(new Ui::HelpMessageDialog)
35 {
36  ui->setupUi(this);
37 
38  QString version = QString{PACKAGE_NAME} + " " + tr("version") + " " + QString::fromStdString(FormatFullVersion());
39 
40  if (about)
41  {
42  setWindowTitle(tr("About %1").arg(PACKAGE_NAME));
43 
44  std::string licenseInfo = LicenseInfo();
46  QString licenseInfoHTML = QString::fromStdString(LicenseInfo());
47  // Make URLs clickable
48  QRegularExpression uri(QStringLiteral("<(.*)>"), QRegularExpression::InvertedGreedinessOption);
49  licenseInfoHTML.replace(uri, QStringLiteral("<a href=\"\\1\">\\1</a>"));
50  // Replace newlines with HTML breaks
51  licenseInfoHTML.replace("\n", "<br>");
52 
53  ui->aboutMessage->setTextFormat(Qt::RichText);
54  ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
55  text = version + "\n" + QString::fromStdString(FormatParagraph(licenseInfo));
56  ui->aboutMessage->setText(version + "<br><br>" + licenseInfoHTML);
57  ui->aboutMessage->setWordWrap(true);
58  ui->helpMessage->setVisible(false);
59  } else {
60  setWindowTitle(tr("Command-line options"));
61  QString header = "Usage: bitcoin-qt [command-line options] [URI]\n\n"
62  "Optional URI is a Bitcoin address in BIP21 URI format.\n";
63  QTextCursor cursor(ui->helpMessage->document());
64  cursor.insertText(version);
65  cursor.insertBlock();
66  cursor.insertText(header);
67  cursor.insertBlock();
68 
69  std::string strUsage = gArgs.GetHelpMessage();
70  QString coreOptions = QString::fromStdString(strUsage);
71  text = version + "\n\n" + header + "\n" + coreOptions;
72 
73  QTextTableFormat tf;
74  tf.setBorderStyle(QTextFrameFormat::BorderStyle_None);
75  tf.setCellPadding(2);
76  QVector<QTextLength> widths;
77  widths << QTextLength(QTextLength::PercentageLength, 35);
78  widths << QTextLength(QTextLength::PercentageLength, 65);
79  tf.setColumnWidthConstraints(widths);
80 
81  QTextCharFormat bold;
82  bold.setFontWeight(QFont::Bold);
83 
84  for (const QString &line : coreOptions.split("\n")) {
85  if (line.startsWith(" -"))
86  {
87  cursor.currentTable()->appendRows(1);
88  cursor.movePosition(QTextCursor::PreviousCell);
89  cursor.movePosition(QTextCursor::NextRow);
90  cursor.insertText(line.trimmed());
91  cursor.movePosition(QTextCursor::NextCell);
92  } else if (line.startsWith(" ")) {
93  cursor.insertText(line.trimmed()+' ');
94  } else if (line.size() > 0) {
95  //Title of a group
96  if (cursor.currentTable())
97  cursor.currentTable()->appendRows(1);
98  cursor.movePosition(QTextCursor::Down);
99  cursor.insertText(line.trimmed(), bold);
100  cursor.insertTable(1, 2, tf);
101  }
102  }
103 
104  ui->helpMessage->moveCursor(QTextCursor::Start);
105  ui->scrollArea->setVisible(false);
106  ui->aboutLogo->setVisible(false);
107  }
108 
110 }
111 
113 {
114  delete ui;
115 }
116 
118 {
119  // On other operating systems, the expected action is to print the message to the console.
120  tfm::format(std::cout, "%s\n", qPrintable(text));
121 }
122 
124 {
125 #if defined(WIN32)
126  // On Windows, show a message box, as there is no stderr/stdout in windowed applications
127  exec();
128 #else
129  // On other operating systems, print help text to console
130  printToConsole();
131 #endif
132 }
133 
135 {
136  close();
137 }
138 
139 
141 ShutdownWindow::ShutdownWindow(QWidget *parent, Qt::WindowFlags f):
142  QWidget(parent, f)
143 {
144  QVBoxLayout *layout = new QVBoxLayout();
145  layout->addWidget(new QLabel(
146  tr("%1 is shutting down…").arg(PACKAGE_NAME) + "<br /><br />" +
147  tr("Do not shut down the computer until this window disappears.")));
148  setLayout(layout);
149 
151 }
152 
153 QWidget* ShutdownWindow::showShutdownWindow(QMainWindow* window)
154 {
155  assert(window != nullptr);
156 
157  // Show a simple window indicating shutdown status
158  QWidget *shutdownWindow = new ShutdownWindow();
159  shutdownWindow->setWindowTitle(window->windowTitle());
160 
161  // Center shutdown window at where main window was
162  const QPoint global = window->mapToGlobal(window->rect().center());
163  shutdownWindow->move(global.x() - shutdownWindow->width() / 2, global.y() - shutdownWindow->height() / 2);
164  shutdownWindow->show();
165  return shutdownWindow;
166 }
167 
168 void ShutdownWindow::closeEvent(QCloseEvent *event)
169 {
170  event->ignore();
171 }
ArgsManager gArgs
Definition: args.cpp:41
#define PACKAGE_NAME
std::string GetHelpMessage() const
Get the help string.
Definition: args.cpp:590
"Help message" dialog box
Definition: utilitydialog.h:21
HelpMessageDialog(QWidget *parent, bool about)
"Help message" or "About" dialog box
Ui::HelpMessageDialog * ui
Definition: utilitydialog.h:32
ShutdownWindow(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::Widget)
"Shutdown" window
void closeEvent(QCloseEvent *event) override
static QWidget * showShutdownWindow(QMainWindow *window)
std::string FormatFullVersion()
std::string LicenseInfo()
Returns licensing information (for -version)
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:60
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:427
constexpr auto dialog_flags
Definition: guiutil.h:60
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1060
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
assert(!tx.IsCoinBase())