Skip to content

Commit

Permalink
Merge pull request #2378 from jamescowens/fix_gui_version_arg
Browse files Browse the repository at this point in the history
gui: Add text output and dialog boxes for -help and -version in GUI client
  • Loading branch information
jamescowens authored Nov 10, 2021
2 parents 4988716 + 70a0e81 commit 5d13429
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
46 changes: 32 additions & 14 deletions src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
#include "policy/fees.h"
#include "upgradeqt.h"
#include "validation.h"
#include "decoration.h"

#include <QMessageBox>
#include <QGridLayout>
#include <QDebug>
#include <QTextCodec>
#include <QLocale>
Expand Down Expand Up @@ -301,13 +303,38 @@ int main(int argc, char *argv[])
// a dialog can be raised, and is the latest that is safe if the command line is not parseable.
if (command_line_parse_failure) {
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
ThreadSafeMessageBox(strprintf("Error reading configuration file.\n"),
"", CClientUIInterface::ICON_ERROR | CClientUIInterface::BTN_OK | CClientUIInterface::MODAL);
QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error: Cannot parse command line arguments. Please check "
"the arguments and ensure they are valid and formatted "
"correctly."));
QMessageBox::critical(nullptr, PACKAGE_NAME, QObject::tr("Error: Cannot parse command line arguments. Please "
"check the arguments and ensure they are valid and "
"formatted correctly: \n\n")
+ QString::fromStdString(error));
return EXIT_FAILURE;
}


// Show help message immediately after parsing command-line options (for "-lang") and setting locale,
// but before showing splash screen.
if (HelpRequested(gArgs))
{
GUIUtil::HelpMessageBox help;

QSize size = GRC::ScaleSize((QPaintDevice *) &help, 550);

QSpacerItem* horizontalSpacer = new QSpacerItem(size.width(), 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*) help.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());

help.showAndPrint();
return EXIT_SUCCESS;
}

if (gArgs.IsArgSet("-version"))
{
tfm::format(std::cout, "%s", "Version: " + VersionMessage());
QMessageBox::information(nullptr, PACKAGE_NAME, QString::fromStdString("Version: " + VersionMessage()));

return EXIT_SUCCESS;
}

// Application identification (must be set before OptionsModel is initialized,
// as it is used to locate QSettings)
app.setOrganizationName("Gridcoin");
Expand Down Expand Up @@ -586,15 +613,6 @@ int StartGridcoinQt(int argc, char *argv[], QApplication& app, OptionsModel& opt

uiInterface.UpdateMessageBox_connect(UpdateMessageBox);

// Show help message immediately after parsing command-line options (for "-lang") and setting locale,
// but before showing splash screen.
if (HelpRequested(gArgs))
{
GUIUtil::HelpMessageBox help;
help.showOrPrint();
return EXIT_FAILURE;
}

QSplashScreen splash(QPixmap(":/images/splash"));
if (gArgs.GetBoolArg("-splash", true) && !gArgs.GetBoolArg("-min"))
{
Expand Down
23 changes: 10 additions & 13 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,17 +685,15 @@ bool SetStartOnSystemStartup(bool fAutoStart, bool fStartMin) { return false; }
HelpMessageBox::HelpMessageBox(QWidget *parent) :
QMessageBox(parent)
{
header = "gridcoinresearch " + tr("version") + " " +
header = "gridcoinresearch " + tr("version") + ": " +
QString::fromStdString(FormatFullVersion()) + "\n\n" +
tr("Usage:") + "\n" +
" gridcoinresearch [" + tr("command-line options") + "] " + "\n";
tr("Usage:") + " gridcoinresearch [" + tr("command-line options") + "]\n";

options = QString::fromStdString(gArgs.GetHelpMessage());

setWindowTitle(tr("Gridcoin"));
setTextFormat(Qt::PlainText);
// setMinimumWidth is ignored for QMessageBox so put in non-breaking spaces to make it wider.
setText(header + QString(QChar(0x2003)).repeated(50));
setText(header);
setDetailedText(options);

setStandardButtons(QMessageBox::Ok);
Expand All @@ -710,15 +708,14 @@ void HelpMessageBox::printToConsole()
tfm::format(std::cout, "%s", strUsage.toStdString().c_str());
}

void HelpMessageBox::showOrPrint()
void HelpMessageBox::showAndPrint()
{
#if defined(WIN32)
// On Windows, show a message box, as there is no stderr/stdout in windowed applications
exec();
#else
// On other operating systems, print help text to console
printToConsole();
#endif
// The proper behavior here is for all environments to dump the help to the console and
// present a model dialog box with the same information. The GUI program could have been
// started from a command line, in which case the console output is visible, or from an
// icon, which means it would not be.
printToConsole();
exec();
}

QDateTime StartOfDay(const QDate& date)
Expand Down
2 changes: 1 addition & 1 deletion src/qt/guiutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ namespace GUIUtil
HelpMessageBox(QWidget* parent = nullptr);

/** Show message box or print help message to standard output, based on operating system. */
void showOrPrint();
void showAndPrint();

/** Print help message to console */
void printToConsole();
Expand Down

0 comments on commit 5d13429

Please sign in to comment.