Bitcoin Core  0.19.99
P2P Digital Currency
utilitydialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2019 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 <clientversion.h>
14 #include <init.h>
15 #include <util/system.h>
16 #include <util/strencodings.h>
17 
18 #include <stdio.h>
19 
20 #include <QCloseEvent>
21 #include <QLabel>
22 #include <QMainWindow>
23 #include <QRegExp>
24 #include <QTextCursor>
25 #include <QTextTable>
26 #include <QVBoxLayout>
27 
29 HelpMessageDialog::HelpMessageDialog(interfaces::Node& node, QWidget *parent, bool about) :
30  QDialog(parent),
31  ui(new Ui::HelpMessageDialog)
32 {
33  ui->setupUi(this);
34 
35  QString version = QString{PACKAGE_NAME} + " " + tr("version") + " " + QString::fromStdString(FormatFullVersion());
36 
37  if (about)
38  {
39  setWindowTitle(tr("About %1").arg(PACKAGE_NAME));
40 
41  std::string licenseInfo = LicenseInfo();
43  QString licenseInfoHTML = QString::fromStdString(LicenseInfo());
44  // Make URLs clickable
45  QRegExp uri("<(.*)>", Qt::CaseSensitive, QRegExp::RegExp2);
46  uri.setMinimal(true); // use non-greedy matching
47  licenseInfoHTML.replace(uri, "<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] \n";
60  QTextCursor cursor(ui->helpMessage->document());
61  cursor.insertText(version);
62  cursor.insertBlock();
63  cursor.insertText(header);
64  cursor.insertBlock();
65 
66  std::string strUsage = gArgs.GetHelpMessage();
67  QString coreOptions = QString::fromStdString(strUsage);
68  text = version + "\n\n" + header + "\n" + coreOptions;
69 
70  QTextTableFormat tf;
71  tf.setBorderStyle(QTextFrameFormat::BorderStyle_None);
72  tf.setCellPadding(2);
73  QVector<QTextLength> widths;
74  widths << QTextLength(QTextLength::PercentageLength, 35);
75  widths << QTextLength(QTextLength::PercentageLength, 65);
76  tf.setColumnWidthConstraints(widths);
77 
78  QTextCharFormat bold;
79  bold.setFontWeight(QFont::Bold);
80 
81  for (const QString &line : coreOptions.split("\n")) {
82  if (line.startsWith(" -"))
83  {
84  cursor.currentTable()->appendRows(1);
85  cursor.movePosition(QTextCursor::PreviousCell);
86  cursor.movePosition(QTextCursor::NextRow);
87  cursor.insertText(line.trimmed());
88  cursor.movePosition(QTextCursor::NextCell);
89  } else if (line.startsWith(" ")) {
90  cursor.insertText(line.trimmed()+' ');
91  } else if (line.size() > 0) {
92  //Title of a group
93  if (cursor.currentTable())
94  cursor.currentTable()->appendRows(1);
95  cursor.movePosition(QTextCursor::Down);
96  cursor.insertText(line.trimmed(), bold);
97  cursor.insertTable(1, 2, tf);
98  }
99  }
100 
101  ui->helpMessage->moveCursor(QTextCursor::Start);
102  ui->scrollArea->setVisible(false);
103  ui->aboutLogo->setVisible(false);
104  }
105 }
106 
108 {
109  delete ui;
110 }
111 
113 {
114  // On other operating systems, the expected action is to print the message to the console.
115  tfm::format(std::cout, "%s\n", qPrintable(text));
116 }
117 
119 {
120 #if defined(WIN32)
121  // On Windows, show a message box, as there is no stderr/stdout in windowed applications
122  exec();
123 #else
124  // On other operating systems, print help text to console
125  printToConsole();
126 #endif
127 }
128 
130 {
131  close();
132 }
133 
134 
136 ShutdownWindow::ShutdownWindow(QWidget *parent, Qt::WindowFlags f):
137  QWidget(parent, f)
138 {
139  QVBoxLayout *layout = new QVBoxLayout();
140  layout->addWidget(new QLabel(
141  tr("%1 is shutting down...").arg(PACKAGE_NAME) + "<br /><br />" +
142  tr("Do not shut down the computer until this window disappears.")));
143  setLayout(layout);
144 }
145 
146 QWidget* ShutdownWindow::showShutdownWindow(QMainWindow* window)
147 {
148  assert(window != nullptr);
149 
150  // Show a simple window indicating shutdown status
151  QWidget *shutdownWindow = new ShutdownWindow();
152  shutdownWindow->setWindowTitle(window->windowTitle());
153 
154  // Center shutdown window at where main window was
155  const QPoint global = window->mapToGlobal(window->rect().center());
156  shutdownWindow->move(global.x() - shutdownWindow->width() / 2, global.y() - shutdownWindow->height() / 2);
157  shutdownWindow->show();
158  return shutdownWindow;
159 }
160 
161 void ShutdownWindow::closeEvent(QCloseEvent *event)
162 {
163  event->ignore();
164 }
std::string FormatParagraph(const std::string &in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line...
#define PACKAGE_NAME
std::string LicenseInfo()
Returns licensing information (for -version)
Definition: init.cpp:564
void closeEvent(QCloseEvent *event)
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:437
static QWidget * showShutdownWindow(QMainWindow *window)
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:1062
std::string FormatFullVersion()
Ui::HelpMessageDialog * ui
Definition: utilitydialog.h:36
ShutdownWindow(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::Widget)
"Shutdown" window
ArgsManager gArgs
Definition: system.cpp:75
"Help message" dialog box
Definition: utilitydialog.h:24
HelpMessageDialog(interfaces::Node &node, QWidget *parent, bool about)
"Help message" or "About" dialog box
Top-level interface for a bitcoin node (bitcoind process).
Definition: node.h:39