From e1c945d02c8b24bf30f080e7829ad0480adea7d5 Mon Sep 17 00:00:00 2001 From: vorzelmir Date: Sun, 12 Apr 2020 23:28:36 +0300 Subject: [PATCH] add VPuzzleCommandLine to Puzzleapplication --- src/app/puzzle/puzzle.pro | 2 +- src/app/puzzle/puzzleapplication.cpp | 51 ++++++++++++++++----------- src/app/puzzle/puzzleapplication.h | 5 ++- src/app/puzzle/vpuzzlecommandline.cpp | 43 ++++++++++++++-------- src/app/puzzle/vpuzzlecommandline.h | 11 ++++++ 5 files changed, 75 insertions(+), 37 deletions(-) diff --git a/src/app/puzzle/puzzle.pro b/src/app/puzzle/puzzle.pro index 818e299dd..7463ba832 100644 --- a/src/app/puzzle/puzzle.pro +++ b/src/app/puzzle/puzzle.pro @@ -7,7 +7,7 @@ # File with common stuff for whole project include(../../../common.pri) -QT += core gui widgets network xml xmlpatterns printsupport testlib +QT += core gui widgets network xml xmlpatterns printsupport # Name of binary file TARGET = puzzle diff --git a/src/app/puzzle/puzzleapplication.cpp b/src/app/puzzle/puzzleapplication.cpp index 9cb624b52..4c41359df 100644 --- a/src/app/puzzle/puzzleapplication.cpp +++ b/src/app/puzzle/puzzleapplication.cpp @@ -387,6 +387,8 @@ void PuzzleApplication::InitOptions() LoadTranslation(QLocale().name());// By default the console version uses system locale + VPuzzleCommandLine::Instance(*this); + static const char * GENERIC_ICON_TO_CHECK = "document-open"; if (QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK) == false) { @@ -442,24 +444,26 @@ void PuzzleApplication::ActivateDarkMode() //--------------------------------------------------------------------------------------------------------------------- void PuzzleApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments) { - QCommandLineParser parser; - parser.setApplicationDescription(tr("Valentina's manual layout editor.")); - parser.addHelpOption(); - parser.addVersionOption(); - parser.addPositionalArgument("filename", tr("The raw layout file.")); - //----- - QCommandLineOption testOption(QStringList() << "test", - tr("Use for unit testing. Run the program and open a file without showing the main window.")); - parser.addOption(testOption); - //----- - QCommandLineOption scalingOption(QStringList() << LONG_OPTION_NO_HDPI_SCALING, - tr("Disable high dpi scaling. Call this option if has problem with scaling (by default scaling enabled). " - "Alternatively you can use the %1 environment variable.").arg("QT_AUTO_SCREEN_SCALE_FACTOR=0")); - parser.addOption(scalingOption); - //----- - parser.process(arguments); + std::shared_ptrcmd = CommandLine(); +// QCommandLineParser parser; +// parser.setApplicationDescription(tr("Valentina's manual layout editor.")); +// parser.addHelpOption(); +// parser.addVersionOption(); +// parser.addPositionalArgument("filename", tr("The raw layout file.")); +// //----- +// QCommandLineOption testOption(QStringList() << "test", +// tr("Use for unit testing. Run the program and open a file without showing the main window.")); +// parser.addOption(testOption); +// //----- +// QCommandLineOption scalingOption(QStringList() << LONG_OPTION_NO_HDPI_SCALING, +// tr("Disable high dpi scaling. Call this option if has problem with scaling (by default scaling enabled). " +// "Alternatively you can use the %1 environment variable.").arg("QT_AUTO_SCREEN_SCALE_FACTOR=0")); +// parser.addOption(scalingOption); +// //----- +// parser.process(arguments); - testMode = parser.isSet(testOption); +// testMode = parser.isSet(testOption); + testMode = cmd->IsTestModeEnabled(); if (not testMode && connection == SocketConnection::Client) { @@ -499,13 +503,14 @@ void PuzzleApplication::ParseCommandLine(const SocketConnection &connection, con LoadTranslation(PuzzleSettings()->GetLocale()); } - const QStringList args = parser.positionalArguments(); +// const QStringList args = parser.positionalArguments(); + const QStringList args = cmd->OptionFileNames(); if (args.count() > 0) { if (testMode && args.count() > 1) { qCCritical(mApp, "%s\n", qPrintable(tr("Test mode doesn't support openning several files."))); - parser.showHelp(V_EX_USAGE); + cmd.get()->parser.showHelp(V_EX_USAGE); } for (auto &arg : args) @@ -531,7 +536,7 @@ void PuzzleApplication::ParseCommandLine(const SocketConnection &connection, con else { qCCritical(mApp, "%s\n", qPrintable(tr("Please, provide one input file."))); - parser.showHelp(V_EX_USAGE); + cmd.get()->parser.showHelp(V_EX_USAGE); } } @@ -634,3 +639,9 @@ void PuzzleApplication::Clean() } } } + +//-------------------------------------------------------------------------------------------- +const std::shared_ptr PuzzleApplication::CommandLine() +{ + return VPuzzleCommandLine::instance; +} diff --git a/src/app/puzzle/puzzleapplication.h b/src/app/puzzle/puzzleapplication.h index 9f2b15760..780ea0617 100644 --- a/src/app/puzzle/puzzleapplication.h +++ b/src/app/puzzle/puzzleapplication.h @@ -31,6 +31,9 @@ #include "../vmisc/def.h" #include "../vmisc/vpuzzlesettings.h" #include "../vmisc/vabstractapplication.h" +#include "vpuzzlecommandline.h" + +#include class PuzzleApplication;// use in define class PuzzleMainWindow; @@ -68,7 +71,7 @@ public: void ActivateDarkMode(); void ParseCommandLine(const SocketConnection &connection, const QStringList &arguments); - + const std::shared_ptr CommandLine(); public slots: void ProcessCMD(); diff --git a/src/app/puzzle/vpuzzlecommandline.cpp b/src/app/puzzle/vpuzzlecommandline.cpp index 1c826e341..4b54e9ad9 100644 --- a/src/app/puzzle/vpuzzlecommandline.cpp +++ b/src/app/puzzle/vpuzzlecommandline.cpp @@ -1,8 +1,8 @@ #include "vpuzzlecommandline.h" #include "../vmisc/commandoptions.h" #include "../vmisc/vsysexits.h" +#include "../vmisc/literals.h" #include -#include std::shared_ptr VPuzzleCommandLine::instance = nullptr; @@ -57,6 +57,12 @@ QStringList VPuzzleCommandLine::OptionFileNames() const return parser.positionalArguments(); } +//------------------------------------------------------------------------------------------- +bool VPuzzleCommandLine::IsNoScalingEnabled() const +{ + return IsOptionSet(LONG_OPTION_NO_HDPI_SCALING); +} + //---------------------------------------------------------------------------------------------- VPuzzleCommandLine::VPuzzleCommandLine(): parser(), @@ -85,23 +91,30 @@ std::shared_ptr VPuzzleCommandLine::Instance(const QCoreAppl //------------------------------------------------------------------------------------------- void VPuzzleCommandLine::InitCommandLineOptions() { - if (IsExportEnabled()) - { - QStringList args = parser.positionalArguments(); - parser.setSingleDashWordOptionMode( - QCommandLineParser::SingleDashWordOptionMode(args.takeFirst().toInt())); - QString source = args.isEmpty() ? QString() : args.at(0); - QString destination = args.isEmpty() ? QString() : args.at(1); - parser.clearPositionalArguments(); - parser.addPositionalArgument(source, - translate("Puzzle", "The raw layout input file.")); - parser.addPositionalArgument(destination, - translate("Puzzle", "The destination folder")); - } + QStringList args = parser.positionalArguments(); + parser.setSingleDashWordOptionMode( + QCommandLineParser::SingleDashWordOptionMode(args.takeFirst().toInt())); + QString source = args.isEmpty() ? QString() : args.at(0); + QString destination = args.isEmpty() ? QString() : args.at(1); + parser.clearPositionalArguments(); + parser.addPositionalArgument(source, + translate("Puzzle", "The raw layout input file.")); + parser.addPositionalArgument(destination, + translate("Puzzle", "The destination folder")); QCommandLineOption forceOption(QStringList() << "f" << "force", translate("Puzzle", "Overwrite existing files.")); - parser.addOption(forceOption); + parser.addOption(forceOption); + + QCommandLineOption testOption(QStringList() << "test", + tr("Use for unit testing. Run the program and open a file without showing the main window.")); + parser.addOption(testOption); + + QCommandLineOption scalingOption(QStringList() << LONG_OPTION_NO_HDPI_SCALING, + tr("Disable high dpi scaling. Call this option if has problem with scaling (by default scaling enabled). " + "Alternatively you can use the %1 environment variable.").arg("QT_AUTO_SCREEN_SCALE_FACTOR=0")); + parser.addOption(scalingOption); + } //-------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/vpuzzlecommandline.h b/src/app/puzzle/vpuzzlecommandline.h index 2735e6487..d802e3c73 100644 --- a/src/app/puzzle/vpuzzlecommandline.h +++ b/src/app/puzzle/vpuzzlecommandline.h @@ -7,6 +7,7 @@ class VPuzzleCommandLine: public QObject { + friend class PuzzleApplication; Q_OBJECT public: virtual ~VPuzzleCommandLine() = default; @@ -15,24 +16,34 @@ public: * @brief if user enabled export from cmd */ bool IsExportEnabled() const; + /** * @brief the base name of layout file or empty string if not */ QString OptionBaseName() const; + /** * @brief if user enabled test mode from cmd */ bool IsTestModeEnabled() const; + /** * @brief if gui enabled or not */ bool IsGuiEnabled() const; + /** * @brief the file name which should be loaded */ QStringList OptionFileNames() const; + + /** + * @brief if high dpi scaling is enabled + */ + bool IsNoScalingEnabled() const; protected: VPuzzleCommandLine(); + /** * @brief create the single instance of the class inside puzzleapplication */