2020-04-03 22:05:03 +02:00
|
|
|
#include "vpuzzlecommandline.h"
|
2020-04-06 23:57:01 +02:00
|
|
|
#include "../vmisc/commandoptions.h"
|
|
|
|
#include "../vmisc/vsysexits.h"
|
2020-04-12 22:28:36 +02:00
|
|
|
#include "../vmisc/literals.h"
|
2020-04-06 23:57:01 +02:00
|
|
|
#include <QDebug>
|
2020-04-03 22:05:03 +02:00
|
|
|
|
2020-04-06 23:57:01 +02:00
|
|
|
std::shared_ptr<VPuzzleCommandLine> VPuzzleCommandLine::instance = nullptr;
|
|
|
|
|
|
|
|
#define translate(context, source) QCoreApplication::translate((context), source)
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------------
|
|
|
|
bool VPuzzleCommandLine::IsExportEnabled() const
|
|
|
|
{
|
|
|
|
const bool result = IsOptionSet(QStringLiteral("destination"));
|
|
|
|
int argSize = parser.positionalArguments().size();
|
|
|
|
if (result && argSize != 1)
|
|
|
|
{
|
|
|
|
qCritical() << translate("Puzzle", "Export options can be used with single input file only.") << "/n";
|
|
|
|
const_cast<VPuzzleCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------------
|
|
|
|
QString VPuzzleCommandLine::OptionBaseName() const
|
|
|
|
{
|
|
|
|
QString path;
|
|
|
|
if (IsExportEnabled())
|
|
|
|
{
|
|
|
|
path = OptionValue(QStringLiteral("destination"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------
|
|
|
|
bool VPuzzleCommandLine::IsTestModeEnabled() const
|
|
|
|
{
|
|
|
|
const bool r = IsOptionSet(QStringLiteral("test"));
|
|
|
|
if (r && parser.positionalArguments().size() != 1)
|
|
|
|
{
|
|
|
|
qCritical() << translate("VCommandLine", "Test option can be used with single input file only.") << "/n";
|
|
|
|
const_cast<VPuzzleCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------
|
|
|
|
bool VPuzzleCommandLine::IsGuiEnabled() const
|
|
|
|
{
|
|
|
|
return isGuiEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------
|
|
|
|
QStringList VPuzzleCommandLine::OptionFileNames() const
|
|
|
|
{
|
|
|
|
return parser.positionalArguments();
|
|
|
|
}
|
|
|
|
|
2020-04-12 22:28:36 +02:00
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
bool VPuzzleCommandLine::IsNoScalingEnabled() const
|
|
|
|
{
|
|
|
|
return IsOptionSet(LONG_OPTION_NO_HDPI_SCALING);
|
|
|
|
}
|
|
|
|
|
2020-04-06 23:57:01 +02:00
|
|
|
//----------------------------------------------------------------------------------------------
|
2020-04-03 22:05:03 +02:00
|
|
|
VPuzzleCommandLine::VPuzzleCommandLine():
|
|
|
|
parser(),
|
|
|
|
isGuiEnabled(false)
|
|
|
|
{
|
2020-04-06 23:57:01 +02:00
|
|
|
parser.setApplicationDescription(translate("Puzzle", "Valentina's manual layout editor."));
|
2020-04-03 22:05:03 +02:00
|
|
|
parser.addHelpOption();
|
|
|
|
parser.addVersionOption();
|
2020-04-06 23:57:01 +02:00
|
|
|
|
|
|
|
InitCommandLineOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
std::shared_ptr<VPuzzleCommandLine> VPuzzleCommandLine::Instance(const QCoreApplication &app)
|
|
|
|
{
|
|
|
|
if (instance == nullptr)
|
|
|
|
{
|
|
|
|
instance.reset(new VPuzzleCommandLine);
|
|
|
|
}
|
|
|
|
instance->parser.process(app);
|
|
|
|
|
|
|
|
instance->isGuiEnabled = not (instance->IsGuiEnabled() || instance->IsExportEnabled());
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
void VPuzzleCommandLine::InitCommandLineOptions()
|
|
|
|
{
|
2020-04-12 22:28:36 +02:00
|
|
|
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"));
|
2020-04-06 23:57:01 +02:00
|
|
|
|
|
|
|
QCommandLineOption forceOption(QStringList() << "f" << "force",
|
|
|
|
translate("Puzzle", "Overwrite existing files."));
|
2020-04-12 22:28:36 +02:00
|
|
|
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);
|
|
|
|
|
2020-04-06 23:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------
|
|
|
|
bool VPuzzleCommandLine::IsOptionSet(const QString &option) const
|
|
|
|
{
|
|
|
|
return parser.isSet(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
QString VPuzzleCommandLine::OptionValue(const QString &option) const
|
|
|
|
{
|
|
|
|
return parser.value(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------
|
|
|
|
QStringList VPuzzleCommandLine::OptionValues(const QString &option) const
|
|
|
|
{
|
|
|
|
return parser.values(option);
|
2020-04-03 22:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|