Test mode for Valentina.exe.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-10-03 20:27:27 +03:00
parent 693dd7a06e
commit f310c6a479
4 changed files with 84 additions and 56 deletions

View File

@ -499,31 +499,27 @@ void MApplication::RetranslateTables()
void MApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments)
{
QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "Valentina's measurements editor."));
parser.setApplicationDescription(tr("Valentina's measurements editor."));
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("filename", QCoreApplication::translate("main", "The measurement file."));
parser.addPositionalArgument("filename", tr("The measurement file."));
//-----
QCommandLineOption heightOption(QStringList() << "e" << "height",
QCoreApplication::translate("main", "Open with the base height: 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, "
"152, 158, 164, 170, 176, 182 or 188 cm."),
QCoreApplication::translate("main", "The base height"));
tr("Open with the base height: 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 158, 164, 170, 176, 182 or "
"188 cm."), tr("The base height"));
parser.addOption(heightOption);
//-----
QCommandLineOption sizeOption(QStringList() << "s" << "size",
QCoreApplication::translate("main", "Open with the base size: 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, "
"48, 50, 52, 54 or 56 cm."),
QCoreApplication::translate("main", "The base size"));
tr("Open with the base size: 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54 or 56 cm."),
tr("The base size"));
parser.addOption(sizeOption);
//-----
QCommandLineOption unitOption(QStringList() << "u" << "unit",
QCoreApplication::translate("main", "Set pattern file unit: cm, mm, inch."),
QCoreApplication::translate("main", "The pattern unit"));
tr("Set pattern file unit: cm, mm, inch."), tr("The pattern unit"));
parser.addOption(unitOption);
//-----
QCommandLineOption testOption(QStringList() << "test",
QCoreApplication::translate("main", "Use for unit testing. Run the program and open a file without showing a "
"window."));
tr("Use for unit testing. Run the program and open a file without showing a window."));
parser.addOption(testOption);
//-----
parser.process(arguments);
@ -548,9 +544,9 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
}
else
{
fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main",
"Error: Invalid base height argument. Must be 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 158, "
"164, 170, 176, 182 or 188 cm.")));
qCCritical(mApp, "%s\n",
qPrintable(tr("Invalid base height argument. Must be 92, 98, 104, 110, 116, 122, 128, 134, "
"140, 146, 152, 158, 164, 170, 176, 182 or 188 cm.")));
parser.showHelp(V_EX_USAGE);
}
}
@ -568,9 +564,9 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
}
else
{
fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main",
"Error: Invalid base size argument. Must be 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, "
"52, 54 or 56 cm.")));
qCCritical(mApp, "%s\n",
qPrintable(tr("Invalid base size argument. Must be 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, "
"42, 44, 46, 48, 50, 52, 54 or 56 cm.")));
parser.showHelp(V_EX_USAGE);
}
}
@ -591,8 +587,7 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
}
else
{
fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main",
"Error: Invalid base size argument. Must be cm, mm or inch.")));
qCCritical(mApp, "%s\n", qPrintable(tr("Invalid base size argument. Must be cm, mm or inch.")));
parser.showHelp(V_EX_USAGE);
}
}
@ -628,8 +623,8 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
QLocalServer::removeServer(serverName);
if (not localServer->listen(serverName))
{
qCWarning(mApp, "Can't begin to listen for incoming connections on name '%s'",
qUtf8Printable(serverName));
qCWarning(mApp, "%s",
qUtf8Printable(tr("Can't begin to listen for incoming connections on name '%1'").arg(serverName)));
}
}
}
@ -642,8 +637,7 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
{
if (testMode && args.count() > 1)
{
fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main",
"Error: Test mode doesn't support openning several files.")));
qCCritical(mApp, "%s\n", qPrintable(tr("Test mode doesn't support openning several files.")));
parser.showHelp(V_EX_USAGE);
}
@ -674,6 +668,11 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
{
NewMainWindow();
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Please, provide one input file.")));
parser.showHelp(V_EX_USAGE);
}
}
}

View File

@ -50,8 +50,8 @@ const static auto SINGLE_OPTION_GAPWIDTH = QStringLiteral("G");
const static auto LONG_OPTION_GROUPPING = QStringLiteral("groups");
const static auto SINGLE_OPTION_GROUPPING = QStringLiteral("g");
#define tr(A) QCoreApplication::translate("VCommandLine", (A))
//---------------------------------------------------------------------------------------------------------------------
const static auto LONG_OPTION_TEST = QStringLiteral("test");
const static auto SINGLE_OPTION_TEST = QStringLiteral("t");
//such a tricky initialization is used, because it uses static functions which relay on static variables and order of
//initialization is not defined between compiled units. i.e. - segv is possible (I hit it when
@ -139,14 +139,20 @@ VCommandLine::VCommandLine() : parser()
{LONG_OPTION_GROUPPING,
new QCommandLineOption(QStringList() << SINGLE_OPTION_GROUPPING << LONG_OPTION_GROUPPING,
tr("Sets layout groupping (export mode): ")
+ DialogLayoutSettings::MakeGroupsHelp(), tr("Grouping type"), "2")}
+ DialogLayoutSettings::MakeGroupsHelp(), tr("Grouping type"), "2")},
{LONG_OPTION_TEST,
new QCommandLineOption(QStringList() << SINGLE_OPTION_TEST << LONG_OPTION_TEST,
tr("Run the program in a test mode. The program this mode load a single pattern "
"file and silently quit without showing the main window. The key have priority "
"before key '%1'.").arg(LONG_OPTION_OUTFILE))}
}),
isGuiEnabled(false)
{
parser.setApplicationDescription(QCoreApplication::translate("main", "Pattern making program."));
parser.setApplicationDescription(tr("Pattern making program."));
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("filename", QCoreApplication::translate("main", "Pattern file."));
parser.addPositionalArgument("filename", tr("Pattern file."));
QMap<QString, QCommandLineOption *>::const_iterator i = optionsUsed.constBegin();
while (i != optionsUsed.constEnd())
@ -188,12 +194,14 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
if ((a || b || c) && x)
{
Error(tr("Cannot use pageformat and page explicit size/units together."));
qCritical() << tr("Cannot use pageformat and page explicit size/units together.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
if ((a || b || c) && !(a && b && c))
{
Error(tr("Page height, width, units must be used all 3 at once."));
qCritical() << tr("Page height, width, units must be used all 3 at once.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
}
@ -207,7 +215,8 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
// if ((a || b) && !(a && b))
// {
// Error(tr("Shift length must be used together with shift units."));
// qCritical() << tr("Shift length must be used together with shift units.") << "\n";
// const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
// }
// }
@ -218,14 +227,16 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
{
if (!diag.SetIncrease(rotateDegree))
{
Error(tr("Invalid rotation value. That must be one of predefined values."));
qCritical() << tr("Invalid rotation value. That must be one of predefined values.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
}
// if present units MUST be set before any other to keep conversions correct
if (!diag.SelectTemplate(OptPaperSize()))
{
Error(tr("Unknown page templated selected."));
qCritical() << tr("Unknown page templated selected.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
if (parser.isSet(*optionsUsed.value(LONG_OPTION_PAGEH))) //at this point we already sure 3 are set or none
@ -233,7 +244,8 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
if (!diag.SelectPaperUnit(parser.value(*optionsUsed.value(LONG_OPTION_PAGEUNITS))))
{
Error(tr("Unsupported paper units."));
qCritical() << tr("Unsupported paper units.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
diag.SetPaperHeight (Pg2Px(parser.value(*optionsUsed.value(LONG_OPTION_PAGEH)), diag));
@ -244,7 +256,8 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
{
if (!diag.SelectLayoutUnit(parser.value(*optionsUsed.value(LONG_OPTION_SHIFTUNITS))))
{
Error(tr("Unsupported layout units."));
qCritical() << tr("Unsupported layout units.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
}
@ -281,7 +294,7 @@ VCommandLinePtr VCommandLine::Get(const QCoreApplication& app)
instance->parser.process(app);
//fixme: in case of additional options/modes which will need to disable GUI - add it here too
instance->isGuiEnabled = !instance->IsExportEnabled();
instance->isGuiEnabled = not (instance->IsExportEnabled() || instance->IsTestModeEnabled());
return instance;
}
@ -293,26 +306,32 @@ VCommandLine::~VCommandLine()
optionsUsed.clear();
}
//------------------------------------------------------------------------------------------------------
Q_NORETURN void VCommandLine::Error(const QString &text) const
{
vStdErr() << text << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
//------------------------------------------------------------------------------------------------------
void VCommandLine::Reset()
{
instance.reset();
}
//---------------------------------------------------------------------------------------------------------------------
bool VCommandLine::IsTestModeEnabled() const
{
const bool r = parser.isSet(*optionsUsed.value(LONG_OPTION_TEST));
if (r && parser.positionalArguments().size() != 1)
{
qCritical() << tr("Test option can be used with single input file only.") << "/n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
return r;
}
//------------------------------------------------------------------------------------------------------
bool VCommandLine::IsExportEnabled() const
{
bool r = parser.isSet(*optionsUsed.value(LONG_OPTION_OUTFILE));
const bool r = parser.isSet(*optionsUsed.value(LONG_OPTION_OUTFILE));
if (r && parser.positionalArguments().size() != 1)
{
Error(tr("Export options can be used with single input file only."));
qCritical() << tr("Export options can be used with single input file only.") << "/n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
return r;
}
@ -399,5 +418,3 @@ bool VCommandLine::IsGuiEnabled() const
{
return isGuiEnabled;
}
#undef tr

View File

@ -20,6 +20,7 @@ typedef std::shared_ptr<VCommandLine> VCommandLinePtr;
//QCommandLineParser* object must exists until this object alive
class VCommandLine
{
Q_DECLARE_TR_FUNCTIONS(VCommandLine)
private:
static VCommandLinePtr instance;
QCommandLineParser parser;
@ -41,9 +42,6 @@ protected:
Cases OptGroup() const;
//@brief convinient shortcut to show error and help and exit
Q_NORETURN void Error(const QString& text) const;
//@brief: called in destructor of application, so instance destroyed and new maybe created (never happen scenario though)
static void Reset();
@ -56,6 +54,9 @@ public:
//@brief creates object and applies export related options to parser
//@brief tests if user enabled test mode from cmd, throws exception if not exactly 1 input VAL file supplied in case test mode enabled
bool IsTestModeEnabled() const;
//@brief tests if user enabled export from cmd, throws exception if not exactly 1 input VAL file supplied in case export enabled
bool IsExportEnabled() const;

View File

@ -66,17 +66,28 @@ int main(int argc, char *argv[])
w.show();
w.ReopenFilesAfterCrash(args);
}
else
{
if (args.size() != 1)
{
qCritical() << QCoreApplication::translate("vmain", "Please, provide one input file.");
std::exit(V_EX_NOINPUT);
}
}
for (size_t i=0, sz = args.size(); i < sz;++i)
for (int i=0, sz = args.size(); i < sz; ++i)
{
const bool loaded = w.LoadPattern(args.at(static_cast<int>(i)), app.CommandLine()->OptMeasurePath());
if (app.CommandLine()->IsExportEnabled())
if (not app.CommandLine()->IsTestModeEnabled())
{
if (loaded)
if (app.CommandLine()->IsExportEnabled())
{
w.DoExport(app.CommandLine());
if (loaded)
{
w.DoExport(app.CommandLine());
}
break;
}
break;
}
}