add VPuzzleCommandLine to Puzzleapplication

This commit is contained in:
vorzelmir 2020-04-12 23:28:36 +03:00
parent b88e23697e
commit e1c945d02c
5 changed files with 75 additions and 37 deletions

View file

@ -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

View file

@ -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_ptr<VPuzzleCommandLine>cmd = 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<VPuzzleCommandLine> PuzzleApplication::CommandLine()
{
return VPuzzleCommandLine::instance;
}

View file

@ -31,6 +31,9 @@
#include "../vmisc/def.h"
#include "../vmisc/vpuzzlesettings.h"
#include "../vmisc/vabstractapplication.h"
#include "vpuzzlecommandline.h"
#include <memory>
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<VPuzzleCommandLine> CommandLine();
public slots:
void ProcessCMD();

View file

@ -1,8 +1,8 @@
#include "vpuzzlecommandline.h"
#include "../vmisc/commandoptions.h"
#include "../vmisc/vsysexits.h"
#include "../vmisc/literals.h"
#include <QDebug>
#include <QTest>
std::shared_ptr<VPuzzleCommandLine> 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> 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);
}
//--------------------------------------------------------------------------------------------

View file

@ -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
*/