Refactoring VPuzzleCommandLine

This commit is contained in:
Ronan Le Tiec 2020-05-23 14:43:57 +02:00
parent fb78189d29
commit 9e532edc1a
6 changed files with 54 additions and 54 deletions

View file

@ -9,9 +9,9 @@ SOURCES += \
$$PWD/vpcarrouselpiece.cpp \ $$PWD/vpcarrouselpiece.cpp \
$$PWD/vpcarrouselpiecelist.cpp \ $$PWD/vpcarrouselpiecelist.cpp \
$$PWD/vpcarrouselpiecepreview.cpp \ $$PWD/vpcarrouselpiecepreview.cpp \
$$PWD/vpcommandline.cpp \
$$PWD/vpcommands.cpp \ $$PWD/vpcommands.cpp \
$$PWD/vpmainwindow.cpp \ $$PWD/vpmainwindow.cpp \
$$PWD/vpuzzlecommandline.cpp \
$$PWD/vpuzzlegraphicslayout.cpp \ $$PWD/vpuzzlegraphicslayout.cpp \
$$PWD/vpuzzlegraphicspiece.cpp \ $$PWD/vpuzzlegraphicspiece.cpp \
$$PWD/vpuzzlelayout.cpp \ $$PWD/vpuzzlelayout.cpp \
@ -33,10 +33,10 @@ HEADERS += \
$$PWD/vpcarrouselpiece.h \ $$PWD/vpcarrouselpiece.h \
$$PWD/vpcarrouselpiecelist.h \ $$PWD/vpcarrouselpiecelist.h \
$$PWD/vpcarrouselpiecepreview.h \ $$PWD/vpcarrouselpiecepreview.h \
$$PWD/vpcommandline.h \
$$PWD/vpcommands.h \ $$PWD/vpcommands.h \
$$PWD/vpmainwindow.h \ $$PWD/vpmainwindow.h \
$$PWD/vpstable.h \ $$PWD/vpstable.h \
$$PWD/vpuzzlecommandline.h \
$$PWD/vpuzzlegraphicslayout.h \ $$PWD/vpuzzlegraphicslayout.h \
$$PWD/vpuzzlegraphicspiece.h \ $$PWD/vpuzzlegraphicspiece.h \
$$PWD/vpuzzlelayout.h \ $$PWD/vpuzzlelayout.h \

View file

@ -335,9 +335,9 @@ VPMainWindow *VPApplication::MainWindow()
Clean(); Clean();
if (mainWindows.isEmpty()) if (mainWindows.isEmpty())
{ {
VPuzzleCommandLinePtr cmd; VPCommandLinePtr cmd;
VPuzzleCommandLine::ProcessInstance(cmd, QStringList()); VPCommandLine::ProcessInstance(cmd, QStringList());
NewMainWindow(VPuzzleCommandLinePtr()); NewMainWindow(VPCommandLinePtr());
} }
return mainWindows[0]; return mainWindows[0];
} }
@ -355,7 +355,7 @@ QList<VPMainWindow *> VPApplication::MainWindows()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPMainWindow *VPApplication::NewMainWindow(const VPuzzleCommandLinePtr &cmd) VPMainWindow *VPApplication::NewMainWindow(const VPCommandLinePtr &cmd)
{ {
VPMainWindow *puzzle = new VPMainWindow(cmd); VPMainWindow *puzzle = new VPMainWindow(cmd);
mainWindows.prepend(puzzle); mainWindows.prepend(puzzle);
@ -382,7 +382,7 @@ void VPApplication::InitOptions()
LoadTranslation(QLocale().name());// By default the console version uses system locale LoadTranslation(QLocale().name());// By default the console version uses system locale
VPuzzleCommandLine::Instance(*this); VPCommandLine::Instance(*this);
static const char * GENERIC_ICON_TO_CHECK = "document-open"; static const char * GENERIC_ICON_TO_CHECK = "document-open";
if (QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK) == false) if (QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK) == false)
@ -439,8 +439,8 @@ void VPApplication::ActivateDarkMode()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments) void VPApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments)
{ {
VPuzzleCommandLinePtr cmd; VPCommandLinePtr cmd;
VPuzzleCommandLine::ProcessInstance(cmd, arguments); VPCommandLine::ProcessInstance(cmd, arguments);
if (cmd->IsGuiEnabled() && connection == SocketConnection::Client) if (cmd->IsGuiEnabled() && connection == SocketConnection::Client)
{ {
@ -484,7 +484,7 @@ void VPApplication::ParseCommandLine(const SocketConnection &connection, const Q
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPApplication::ProcessArguments(const VPuzzleCommandLinePtr &cmd) void VPApplication::ProcessArguments(const VPCommandLinePtr &cmd)
{ {
const QStringList rawLayouts = cmd->OptionRawLayouts(); const QStringList rawLayouts = cmd->OptionRawLayouts();
const QStringList args = cmd->OptionFileNames(); const QStringList args = cmd->OptionFileNames();
@ -638,7 +638,7 @@ void VPApplication::Clean()
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
VPuzzleCommandLinePtr VPApplication::CommandLine() const VPCommandLinePtr VPApplication::CommandLine() const
{ {
return VPuzzleCommandLine::instance; return VPCommandLine::instance;
} }

View file

@ -31,7 +31,7 @@
#include "../vmisc/def.h" #include "../vmisc/def.h"
#include "vpuzzlesettings.h" #include "vpuzzlesettings.h"
#include "../vmisc/vabstractapplication.h" #include "../vmisc/vabstractapplication.h"
#include "vpuzzlecommandline.h" #include "vpcommandline.h"
#include <memory> #include <memory>
@ -58,7 +58,7 @@ public:
virtual bool IsAppInGUIMode() const override; virtual bool IsAppInGUIMode() const override;
VPMainWindow *MainWindow(); VPMainWindow *MainWindow();
QList<VPMainWindow*> MainWindows(); QList<VPMainWindow*> MainWindows();
VPMainWindow *NewMainWindow(const VPuzzleCommandLinePtr &cmd); VPMainWindow *NewMainWindow(const VPCommandLinePtr &cmd);
void InitOptions(); void InitOptions();
@ -69,8 +69,8 @@ public:
void ActivateDarkMode(); void ActivateDarkMode();
void ParseCommandLine(const SocketConnection &connection, const QStringList &arguments); void ParseCommandLine(const SocketConnection &connection, const QStringList &arguments);
void ProcessArguments(const VPuzzleCommandLinePtr &cmd); void ProcessArguments(const VPCommandLinePtr &cmd);
VPuzzleCommandLinePtr CommandLine() const; VPCommandLinePtr CommandLine() const;
public slots: public slots:
void ProcessCMD(); void ProcessCMD();

View file

@ -72,10 +72,10 @@ protected:
private slots: private slots:
/** /**
* @brief on_ActionPieceMovedToLayer Slot called when the piece is moved via the * @brief on_ActionPieceMovedToPieceList Slot called when the piece is moved via the
* context menu to anoter layer * context menu to anoter piece list
*/ */
void on_ActionPieceMovedToLayer(); void on_ActionPieceMovedToPieceList();
private: private:
Q_DISABLE_COPY(VPCarrouselPiece) Q_DISABLE_COPY(VPCarrouselPiece)

View file

@ -1,6 +1,6 @@
/************************************************************************ /************************************************************************
** **
** @file vpuzzlecommandline.cpp ** @file vpcommandline.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com> ** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 12 4, 2020 ** @date 12 4, 2020
** **
@ -25,31 +25,31 @@
** along with Valentina. If not, see <http://www.gnu.org/licenses/>. ** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
** **
*************************************************************************/ *************************************************************************/
#include "vpuzzlecommandline.h" #include "vpcommandline.h"
#include "vpcommands.h" #include "vpcommands.h"
#include "../vmisc/vsysexits.h" #include "../vmisc/vsysexits.h"
#include "../vmisc/literals.h" #include "../vmisc/literals.h"
#include <QDebug> #include <QDebug>
std::shared_ptr<VPuzzleCommandLine> VPuzzleCommandLine::instance = nullptr; std::shared_ptr<VPCommandLine> VPCommandLine::instance = nullptr;
#define translate(context, source) QCoreApplication::translate((context), source) #define translate(context, source) QCoreApplication::translate((context), source)
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------
bool VPuzzleCommandLine::IsExportEnabled() const bool VPCommandLine::IsExportEnabled() const
{ {
const bool result = IsOptionSet(LONG_OPTION_EXPORT_FILE); const bool result = IsOptionSet(LONG_OPTION_EXPORT_FILE);
int argSize = parser.positionalArguments().size(); int argSize = parser.positionalArguments().size();
if (result && argSize != 1) if (result && argSize != 1)
{ {
qCritical() << translate("Puzzle", "Export options can be used with single input file only.") << "/n"; qCritical() << translate("Puzzle", "Export options can be used with single input file only.") << "/n";
const_cast<VPuzzleCommandLine*>(this)->parser.showHelp(V_EX_USAGE); const_cast<VPCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
} }
return result; return result;
} }
//---------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------
QString VPuzzleCommandLine::OptionExportFile() const QString VPCommandLine::OptionExportFile() const
{ {
QString path; QString path;
if (IsExportEnabled()) if (IsExportEnabled())
@ -61,49 +61,49 @@ QString VPuzzleCommandLine::OptionExportFile() const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QStringList VPuzzleCommandLine::OptionRawLayouts() const QStringList VPCommandLine::OptionRawLayouts() const
{ {
return OptionValues(LONG_OPTION_RAW_LAYOUT); return OptionValues(LONG_OPTION_RAW_LAYOUT);
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
bool VPuzzleCommandLine::IsTestModeEnabled() const bool VPCommandLine::IsTestModeEnabled() const
{ {
const bool r = IsOptionSet(LONG_OPTION_TEST); const bool r = IsOptionSet(LONG_OPTION_TEST);
if (r && parser.positionalArguments().size() != 1) if (r && parser.positionalArguments().size() != 1)
{ {
qCritical() << translate("VCommandLine", "Test option can be used with single input file only.") << "/n"; qCritical() << translate("VCommandLine", "Test option can be used with single input file only.") << "/n";
const_cast<VPuzzleCommandLine*>(this)->parser.showHelp(V_EX_USAGE); const_cast<VPCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
} }
return r; return r;
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
bool VPuzzleCommandLine::IsGuiEnabled() const bool VPCommandLine::IsGuiEnabled() const
{ {
return isGuiEnabled; return isGuiEnabled;
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
QStringList VPuzzleCommandLine::OptionFileNames() const QStringList VPCommandLine::OptionFileNames() const
{ {
return parser.positionalArguments(); return parser.positionalArguments();
} }
//------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------
bool VPuzzleCommandLine::IsNoScalingEnabled() const bool VPCommandLine::IsNoScalingEnabled() const
{ {
return IsOptionSet(LONG_OPTION_NO_HDPI_SCALING); return IsOptionSet(LONG_OPTION_NO_HDPI_SCALING);
} }
//---------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------
void VPuzzleCommandLine::ShowHelp(int exitCode) void VPCommandLine::ShowHelp(int exitCode)
{ {
parser.showHelp(exitCode); parser.showHelp(exitCode);
} }
//---------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------
VPuzzleCommandLine::VPuzzleCommandLine(): VPCommandLine::VPCommandLine():
parser(), parser(),
isGuiEnabled(false) isGuiEnabled(false)
{ {
@ -116,18 +116,18 @@ VPuzzleCommandLine::VPuzzleCommandLine():
} }
//------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------
VPuzzleCommandLinePtr VPuzzleCommandLine::Instance(const QCoreApplication &app) VPCommandLinePtr VPCommandLine::Instance(const QCoreApplication &app)
{ {
VPuzzleCommandLine::ProcessInstance(instance, app.arguments()); VPCommandLine::ProcessInstance(instance, app.arguments());
return instance; return instance;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPuzzleCommandLine::ProcessInstance(VPuzzleCommandLinePtr &instance, const QStringList &arguments) void VPCommandLine::ProcessInstance(VPCommandLinePtr &instance, const QStringList &arguments)
{ {
if (instance == nullptr) if (instance == nullptr)
{ {
instance.reset(new VPuzzleCommandLine); instance.reset(new VPCommandLine);
} }
instance->parser.process(arguments); instance->parser.process(arguments);
@ -135,7 +135,7 @@ void VPuzzleCommandLine::ProcessInstance(VPuzzleCommandLinePtr &instance, const
} }
//------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------
void VPuzzleCommandLine::InitCommandLineOptions() void VPCommandLine::InitCommandLineOptions()
{ {
//keep in mind order here - that is how user will see it, so group-up for usability //keep in mind order here - that is how user will see it, so group-up for usability
//================================================================================================================= //=================================================================================================================
@ -193,19 +193,19 @@ void VPuzzleCommandLine::InitCommandLineOptions()
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
bool VPuzzleCommandLine::IsOptionSet(const QString &option) const bool VPCommandLine::IsOptionSet(const QString &option) const
{ {
return parser.isSet(option); return parser.isSet(option);
} }
//------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------
QString VPuzzleCommandLine::OptionValue(const QString &option) const QString VPCommandLine::OptionValue(const QString &option) const
{ {
return parser.value(option); return parser.value(option);
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
QStringList VPuzzleCommandLine::OptionValues(const QString &option) const QStringList VPCommandLine::OptionValues(const QString &option) const
{ {
return parser.values(option); return parser.values(option);
} }

View file

@ -1,6 +1,6 @@
/************************************************************************ /************************************************************************
** **
** @file vpuzzlecommandline.h ** @file vpcommandline.h
** @author Dmytro Hladkykh <vorzelmir@gmail.com> ** @author Dmytro Hladkykh <vorzelmir@gmail.com>
** @date 12 4, 2020 ** @date 12 4, 2020
** **
@ -25,21 +25,21 @@
** along with Valentina. If not, see <http://www.gnu.org/licenses/>. ** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
** **
*************************************************************************/ *************************************************************************/
#ifndef VPUZZLECOMMANDLINE_H #ifndef VPCOMMANDLINE_H
#define VPUZZLECOMMANDLINE_H #define VPCOMMANDLINE_H
#include <memory> #include <memory>
#include <QCoreApplication> #include <QCoreApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
class VPuzzleCommandLine; class VPCommandLine;
using VPuzzleCommandLinePtr = std::shared_ptr<VPuzzleCommandLine>; using VPCommandLinePtr = std::shared_ptr<VPCommandLine>;
class VPuzzleCommandLine: public QObject class VPCommandLine: public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
virtual ~VPuzzleCommandLine() = default; virtual ~VPCommandLine() = default;
/** @brief if user enabled export from cmd */ /** @brief if user enabled export from cmd */
bool IsExportEnabled() const; bool IsExportEnabled() const;
@ -64,14 +64,14 @@ public:
Q_NORETURN void ShowHelp(int exitCode = 0); Q_NORETURN void ShowHelp(int exitCode = 0);
protected: protected:
VPuzzleCommandLine(); VPCommandLine();
/** @brief create the single instance of the class inside vpapplication */ /** @brief create the single instance of the class inside vpapplication */
static VPuzzleCommandLinePtr Instance(const QCoreApplication &app); static VPCommandLinePtr Instance(const QCoreApplication &app);
static void ProcessInstance(VPuzzleCommandLinePtr &instance, const QStringList &arguments); static void ProcessInstance(VPCommandLinePtr &instance, const QStringList &arguments);
private: private:
Q_DISABLE_COPY(VPuzzleCommandLine) Q_DISABLE_COPY(VPCommandLine)
static VPuzzleCommandLinePtr instance; static VPCommandLinePtr instance;
QCommandLineParser parser; QCommandLineParser parser;
bool isGuiEnabled; bool isGuiEnabled;
friend class VPApplication; friend class VPApplication;
@ -84,4 +84,4 @@ private:
QStringList OptionValues(const QString &option) const; QStringList OptionValues(const QString &option) const;
}; };
#endif // VPUZZLECOMMANDLINE_H #endif // VPCOMMANDLINE_H