Files
lcd-image-converter/classes/cmdline/cmdline.cpp
2017-03-12 15:24:00 +05:00

146 lines
3.9 KiB
C++

/*
* LCD Image Converter. Converts images and fonts for embedded applications.
* Copyright (C) 2014 riuson
* mailto: riuson@gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#include "qt-version-check.h"
#if QT_VERSION_COMBINED >= VERSION_COMBINE(5, 2, 0)
#include "cmdline.h"
#include "preset.h"
#include "parser.h"
#include "imagedocument.h"
#include "datacontainer.h"
#include "modeconvertimage.h"
#include "modeconvertfont.h"
#include "modehex2bin.h"
#include "appsettings.h"
#include <QDebug>
#include <QFile>
#include <QString>
#include <QStringList>
#include <QImage>
#include <QCommandLineParser>
namespace CommandLine
{
CmdLine::CmdLine(const QStringList &arguments, QObject *parent) :
QObject(parent)
{
this->mArguments = new QStringList(arguments);
this->mParser = new QCommandLineParser();
// information block
this->mParser->setApplicationDescription("Tool to create image and font source files for embedded applications.");
this->mParser->addHelpOption();
this->mParser->addVersionOption();
// required option:
// --mode=convert-image
QCommandLineOption optionMode(QStringList() << "m" << "mode",
QCoreApplication::translate("CmdLineParser", "Conversion mode for application, \"convert-image\", \"convert-font\" or \"hex2bin\"."),
QCoreApplication::translate("CmdLineParser", "mode"));
this->mParser->addOption(optionMode);
// optional option
QCommandLineOption optionConfig(QStringList() << "c" << "config",
QCoreApplication::translate("CmdLineParser", "Optional path to configuration file. If not specified, default is used."),
QCoreApplication::translate("CmdLineParser", "configuration"));
this->mParser->addOption(optionConfig);
}
CmdLine::~CmdLine()
{
delete this->mParser;
delete this->mArguments;
}
bool CmdLine::needProcess() const
{
if (this->mArguments->length() > 1) {
return true;
}
return false;
}
CmdLine::ProcessResult CmdLine::process()
{
ProcessResult result = ProcessResult::None;
this->mParser->parse(*this->mArguments);
QString modeName = this->mParser->value("mode");
ModeParserBase *mode = this->createMode(modeName, this->mParser);
if (mode != NULL) {
mode->fillParser();
}
this->mParser->process(*this->mArguments);
// Set path to configuration file
if (this->mParser->isSet("config")) {
QString configFile = this->mParser->value("config");
AppSettings::configure(configFile);
}
if (mode != NULL) {
if (mode->collectArguments()) {
if (mode->process() == 0) {
result = ProcessResult::Success;
} else {
result = ProcessResult::Failed;
}
} else {
qDebug() << "Arguments missing";
result = ProcessResult::Failed;
}
delete mode;
if (result == ProcessResult::Failed) {
this->mParser->showHelp(1);
}
}
return result;
}
ModeParserBase *CmdLine::createMode(const QString &name, QCommandLineParser *parser)
{
if (name == ModeConvertImage::modeName()) {
return new ModeConvertImage(parser, this);
}
if (name == ModeConvertFont::modeName()) {
return new ModeConvertFont(parser, this);
}
if (name == ModeHex2Bin::modeName()) {
return new ModeHex2Bin(parser, this);
}
return NULL;
}
}
#endif // QT_VERSION