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 #include <config/bitcoin-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 
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{PACKAGE_NAME} + " " + tr("version") + " " + QString::fromStdString(FormatFullVersion());
37 
38  if (about)
39  {
40  setWindowTitle(tr("About %1").arg(PACKAGE_NAME));
41 
42  std::string licenseInfo = LicenseInfo();
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 = "Usage: bitcoin-qt [command-line options] [URI]\n\n"
60  "Optional URI is a Bitcoin address in BIP21 URI format.\n";
61  QTextCursor cursor(ui->helpMessage->document());
62  cursor.insertText(version);
63  cursor.insertBlock();
64  cursor.insertText(header);
65  cursor.insertBlock();
66 
67  std::string strUsage = gArgs.GetHelpMessage();
68  QString coreOptions = QString::fromStdString(strUsage);
69  text = version + "\n\n" + header + "\n" + coreOptions;
70 
71  QTextTableFormat tf;
72  tf.setBorderStyle(QTextFrameFormat::BorderStyle_None);
73  tf.setCellPadding(2);
74  QVector<QTextLength> widths;
75  widths << QTextLength(QTextLength::PercentageLength, 35);
76  widths << QTextLength(QTextLength::PercentageLength, 65);
77  tf.setColumnWidthConstraints(widths);
78 
79  QTextCharFormat bold;
80  bold.setFontWeight(QFont::Bold);
81 
82  for (const QString &line : coreOptions.split("\n")) {
83  if (line.startsWith(" -"))
84  {
85  cursor.currentTable()->appendRows(1);
86  cursor.movePosition(QTextCursor::PreviousCell);
87  cursor.movePosition(QTextCursor::NextRow);
88  cursor.insertText(line.trimmed());
89  cursor.movePosition(QTextCursor::NextCell);
90  } else if (line.startsWith(" ")) {
91  cursor.insertText(line.trimmed()+' ');
92  } else if (line.size() > 0) {
93  //Title of a group
94  if (cursor.currentTable())
95  cursor.currentTable()->appendRows(1);
96  cursor.movePosition(QTextCursor::Down);
97  cursor.insertText(line.trimmed(), bold);
98  cursor.insertTable(1, 2, tf);
99  }
100  }
101 
102  ui->helpMessage->moveCursor(QTextCursor::Start);
103  ui->scrollArea->setVisible(false);
104  ui->aboutLogo->setVisible(false);
105  }
106 
108 }
109 
111 {
112  delete ui;
113 }
114 
116 {
117  // On other operating systems, the expected action is to print the message to the console.
118  tfm::format(std::cout, "%s", qPrintable(text));
119 }
120 
122 {
123 #if defined(WIN32)
124  // On Windows, show a message box, as there is no stderr/stdout in windowed applications
125  exec();
126 #else
127  // On other operating systems, print help text to console
128  printToConsole();
129 #endif
130 }
131 
133 {
134  close();
135 }
136 
137 
139 ShutdownWindow::ShutdownWindow(QWidget *parent, Qt::WindowFlags f):
140  QWidget(parent, f)
141 {
142  QVBoxLayout *layout = new QVBoxLayout();
143  layout->addWidget(new QLabel(
144  tr("%1 is shutting down…").arg(PACKAGE_NAME) + "<br /><br />" +
145  tr("Do not shut down the computer until this window disappears.")));
146  setLayout(layout);
147 
149 }
150 
151 QWidget* ShutdownWindow::showShutdownWindow(QMainWindow* window)
152 {
153  assert(window != nullptr);
154 
155  // Show a simple window indicating shutdown status
156  QWidget *shutdownWindow = new ShutdownWindow();
157  shutdownWindow->setWindowTitle(window->windowTitle());
158 
159  // Center shutdown window at where main window was
160  const QPoint global = window->mapToGlobal(window->rect().center());
161  shutdownWindow->move(global.x() - shutdownWindow->width() / 2, global.y() - shutdownWindow->height() / 2);
162  shutdownWindow->show();
163  return shutdownWindow;
164 }
165 
166 void ShutdownWindow::closeEvent(QCloseEvent *event)
167 {
168  event->ignore();
169 }
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:58
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:425
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())