Bitcoin Core  0.18.99
P2P Digital Currency
utilitydialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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/bitcoingui.h>
14 #ifdef ENABLE_BIP70
15 #include <qt/paymentrequestplus.h>
16 #endif
17 
18 #include <clientversion.h>
19 #include <init.h>
20 #include <util/system.h>
21 #include <util/strencodings.h>
22 
23 #include <stdio.h>
24 
25 #include <QCloseEvent>
26 #include <QLabel>
27 #include <QRegExp>
28 #include <QTextTable>
29 #include <QTextCursor>
30 #include <QVBoxLayout>
31 
33 HelpMessageDialog::HelpMessageDialog(interfaces::Node& node, QWidget *parent, bool about) :
34  QDialog(parent),
35  ui(new Ui::HelpMessageDialog)
36 {
37  ui->setupUi(this);
38 
39  QString version = tr(PACKAGE_NAME) + " " + tr("version") + " " + QString::fromStdString(FormatFullVersion());
40  /* On x86 add a bit specifier to the version so that users can distinguish between
41  * 32 and 64 bit builds. On other architectures, 32/64 bit may be more ambiguous.
42  */
43 #if defined(__x86_64__)
44  version += " " + tr("(%1-bit)").arg(64);
45 #elif defined(__i386__ )
46  version += " " + tr("(%1-bit)").arg(32);
47 #endif
48 
49  if (about)
50  {
51  setWindowTitle(tr("About %1").arg(tr(PACKAGE_NAME)));
52 
53  std::string licenseInfo = LicenseInfo();
55  QString licenseInfoHTML = QString::fromStdString(LicenseInfo());
56  // Make URLs clickable
57  QRegExp uri("<(.*)>", Qt::CaseSensitive, QRegExp::RegExp2);
58  uri.setMinimal(true); // use non-greedy matching
59  licenseInfoHTML.replace(uri, "<a href=\"\\1\">\\1</a>");
60  // Replace newlines with HTML breaks
61  licenseInfoHTML.replace("\n", "<br>");
62 
63  ui->aboutMessage->setTextFormat(Qt::RichText);
64  ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
65  text = version + "\n" + QString::fromStdString(FormatParagraph(licenseInfo));
66  ui->aboutMessage->setText(version + "<br><br>" + licenseInfoHTML);
67  ui->aboutMessage->setWordWrap(true);
68  ui->helpMessage->setVisible(false);
69  } else {
70  setWindowTitle(tr("Command-line options"));
71  QString header = "Usage: bitcoin-qt [command-line options] \n";
72  QTextCursor cursor(ui->helpMessage->document());
73  cursor.insertText(version);
74  cursor.insertBlock();
75  cursor.insertText(header);
76  cursor.insertBlock();
77 
78  std::string strUsage = gArgs.GetHelpMessage();
79  QString coreOptions = QString::fromStdString(strUsage);
80  text = version + "\n\n" + header + "\n" + coreOptions;
81 
82  QTextTableFormat tf;
83  tf.setBorderStyle(QTextFrameFormat::BorderStyle_None);
84  tf.setCellPadding(2);
85  QVector<QTextLength> widths;
86  widths << QTextLength(QTextLength::PercentageLength, 35);
87  widths << QTextLength(QTextLength::PercentageLength, 65);
88  tf.setColumnWidthConstraints(widths);
89 
90  QTextCharFormat bold;
91  bold.setFontWeight(QFont::Bold);
92 
93  for (const QString &line : coreOptions.split("\n")) {
94  if (line.startsWith(" -"))
95  {
96  cursor.currentTable()->appendRows(1);
97  cursor.movePosition(QTextCursor::PreviousCell);
98  cursor.movePosition(QTextCursor::NextRow);
99  cursor.insertText(line.trimmed());
100  cursor.movePosition(QTextCursor::NextCell);
101  } else if (line.startsWith(" ")) {
102  cursor.insertText(line.trimmed()+' ');
103  } else if (line.size() > 0) {
104  //Title of a group
105  if (cursor.currentTable())
106  cursor.currentTable()->appendRows(1);
107  cursor.movePosition(QTextCursor::Down);
108  cursor.insertText(line.trimmed(), bold);
109  cursor.insertTable(1, 2, tf);
110  }
111  }
112 
113  ui->helpMessage->moveCursor(QTextCursor::Start);
114  ui->scrollArea->setVisible(false);
115  ui->aboutLogo->setVisible(false);
116  }
117 }
118 
120 {
121  delete ui;
122 }
123 
125 {
126  // On other operating systems, the expected action is to print the message to the console.
127  tfm::format(std::cout, "%s\n", qPrintable(text));
128 }
129 
131 {
132 #if defined(WIN32)
133  // On Windows, show a message box, as there is no stderr/stdout in windowed applications
134  exec();
135 #else
136  // On other operating systems, print help text to console
137  printToConsole();
138 #endif
139 }
140 
142 {
143  close();
144 }
145 
146 
148 ShutdownWindow::ShutdownWindow(QWidget *parent, Qt::WindowFlags f):
149  QWidget(parent, f)
150 {
151  QVBoxLayout *layout = new QVBoxLayout();
152  layout->addWidget(new QLabel(
153  tr("%1 is shutting down...").arg(tr(PACKAGE_NAME)) + "<br /><br />" +
154  tr("Do not shut down the computer until this window disappears.")));
155  setLayout(layout);
156 }
157 
159 {
160  if (!window)
161  return nullptr;
162 
163  // Show a simple window indicating shutdown status
164  QWidget *shutdownWindow = new ShutdownWindow();
165  shutdownWindow->setWindowTitle(window->windowTitle());
166 
167  // Center shutdown window at where main window was
168  const QPoint global = window->mapToGlobal(window->rect().center());
169  shutdownWindow->move(global.x() - shutdownWindow->width() / 2, global.y() - shutdownWindow->height() / 2);
170  shutdownWindow->show();
171  return shutdownWindow;
172 }
173 
174 void ShutdownWindow::closeEvent(QCloseEvent *event)
175 {
176  event->ignore();
177 }
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
Bitcoin GUI main class.
Definition: bitcoingui.h:64
std::string LicenseInfo()
Returns licensing information (for -version)
Definition: init.cpp:561
void closeEvent(QCloseEvent *event)
std::string GetHelpMessage() const
Get the help string.
Definition: system.cpp:561
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:967
std::string FormatFullVersion()
Ui::HelpMessageDialog * ui
Definition: utilitydialog.h:34
ShutdownWindow(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::Widget)
"Shutdown" window
static QWidget * showShutdownWindow(BitcoinGUI *window)
ArgsManager gArgs
Definition: system.cpp:72
"Help message" dialog box
Definition: utilitydialog.h:22
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:36