Skip to content

Commit

Permalink
refactor: Implement HelpMessageDialog using Modes enum
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonOdiwuor committed Nov 13, 2023
1 parent 29c2c90 commit 10b3cd1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ int GuiMain(int argc, char* argv[])
// Show help message immediately after parsing command-line options (for "-lang") and setting locale,
// but before showing splash screen.
if (HelpRequested(gArgs) || gArgs.IsArgSet("-version")) {
HelpMessageDialog help(nullptr, gArgs.IsArgSet("-version"));
bool about = gArgs.IsArgSet("-version");
HelpMessageDialog help(nullptr, about ? HelpMessageDialog::AboutMode : HelpMessageDialog::CommandLineOptionsMode);
help.showOrPrint();
return EXIT_SUCCESS;
}
Expand Down
4 changes: 2 additions & 2 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty
updateWindowTitle();

rpcConsole = new RPCConsole(node, _platformStyle, nullptr);
helpMessageDialog = new HelpMessageDialog(this, false);
helpMessageDialog = new HelpMessageDialog(this, HelpMessageDialog::CommandLineOptionsMode);
#ifdef ENABLE_WALLET
if(enableWallet)
{
Expand Down Expand Up @@ -911,7 +911,7 @@ void BitcoinGUI::aboutClicked()
if(!clientModel)
return;

auto dlg = new HelpMessageDialog(this, /*about=*/true);
auto dlg = new HelpMessageDialog(this, HelpMessageDialog::AboutMode);
GUIUtil::ShowModalDialogAsynchronously(dlg);
}

Expand Down
48 changes: 27 additions & 21 deletions src/qt/utilitydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,39 @@
#include <QVBoxLayout>

/** "Help message" or "About" dialog box */
HelpMessageDialog::HelpMessageDialog(QWidget *parent, bool about) :
HelpMessageDialog::HelpMessageDialog(QWidget *parent, Mode _mode) :
QDialog(parent, GUIUtil::dialog_flags),
ui(new Ui::HelpMessageDialog)
ui(new Ui::HelpMessageDialog),
mode(_mode)
{
ui->setupUi(this);

QString version = QString{PACKAGE_NAME} + " " + tr("version") + " " + QString::fromStdString(FormatFullVersion());

if (about)
switch(mode)
{
setWindowTitle(tr("About %1").arg(PACKAGE_NAME));

std::string licenseInfo = LicenseInfo();
/// HTML-format the license message from the core
QString licenseInfoHTML = QString::fromStdString(LicenseInfo());
// Make URLs clickable
QRegularExpression uri(QStringLiteral("<(.*)>"), QRegularExpression::InvertedGreedinessOption);
licenseInfoHTML.replace(uri, QStringLiteral("<a href=\"\\1\">\\1</a>"));
// Replace newlines with HTML breaks
licenseInfoHTML.replace("\n", "<br>");

ui->aboutMessage->setTextFormat(Qt::RichText);
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
text = version + "\n" + QString::fromStdString(FormatParagraph(licenseInfo));
ui->aboutMessage->setText(version + "<br><br>" + licenseInfoHTML);
ui->aboutMessage->setWordWrap(true);
ui->helpMessage->setVisible(false);
} else {
case AboutMode:
{
setWindowTitle(tr("About %1").arg(PACKAGE_NAME));

std::string licenseInfo = LicenseInfo();
/// HTML-format the license message from the core
QString licenseInfoHTML = QString::fromStdString(LicenseInfo());
// Make URLs clickable
QRegularExpression uri(QStringLiteral("<(.*)>"), QRegularExpression::InvertedGreedinessOption);
licenseInfoHTML.replace(uri, QStringLiteral("<a href=\"\\1\">\\1</a>"));
// Replace newlines with HTML breaks
licenseInfoHTML.replace("\n", "<br>");

ui->aboutMessage->setTextFormat(Qt::RichText);
ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
text = version + "\n" + QString::fromStdString(FormatParagraph(licenseInfo));
ui->aboutMessage->setText(version + "<br><br>" + licenseInfoHTML);
ui->aboutMessage->setWordWrap(true);
ui->helpMessage->setVisible(false);
break;
}
case CommandLineOptionsMode:
setWindowTitle(tr("Command-line options"));
QString header = "Usage: bitcoin-qt [command-line options] \n";
QTextCursor cursor(ui->helpMessage->document());
Expand Down Expand Up @@ -103,6 +108,7 @@ HelpMessageDialog::HelpMessageDialog(QWidget *parent, bool about) :
ui->helpMessage->moveCursor(QTextCursor::Start);
ui->scrollArea->setVisible(false);
ui->aboutLogo->setVisible(false);
break;
}

GUIUtil::handleCloseWindowShortcut(this);
Expand Down
8 changes: 7 additions & 1 deletion src/qt/utilitydialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class HelpMessageDialog : public QDialog
Q_OBJECT

public:
explicit HelpMessageDialog(QWidget *parent, bool about);
enum Mode {
AboutMode,
CommandLineOptionsMode
};

explicit HelpMessageDialog(QWidget *parent, Mode _mode);
~HelpMessageDialog();

void printToConsole();
Expand All @@ -31,6 +36,7 @@ class HelpMessageDialog : public QDialog
private:
Ui::HelpMessageDialog *ui;
QString text;
Mode mode;

private Q_SLOTS:
void on_okButton_accepted();
Expand Down

0 comments on commit 10b3cd1

Please sign in to comment.