Refactoring.

This commit is contained in:
Roman Telezhynskyi 2022-08-03 14:41:19 +03:00
parent 36180a7c38
commit f005b78ecc
7 changed files with 549 additions and 488 deletions

View file

@ -35,7 +35,11 @@
#include "../ifc/exception/vexceptionemptyparameter.h"
#include "../ifc/exception/vexceptionwrongid.h"
#include "../vmisc/vsysexits.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 5, 0)
#include "../vmisc/diagnostic.h"
#endif // QT_VERSION < QT_VERSION_CHECK(5, 5, 0)
#include "../vmisc/qt_dispatch/qt_dispatch.h"
#include "../fervor/fvupdater.h"
@ -46,7 +50,7 @@ QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
QT_WARNING_DISABLE_INTEL(1418)
Q_LOGGING_CATEGORY(pApp, "p.application")
Q_LOGGING_CATEGORY(pApp, "p.application") // NOLINT
QT_WARNING_POP
@ -246,10 +250,10 @@ inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &con
VPApplication::VPApplication(int &argc, char **argv)
:VAbstractApplication(argc, argv)
{
setApplicationDisplayName(VER_PRODUCTNAME_STR);
setApplicationName(VER_INTERNALNAME_STR);
setOrganizationName(VER_COMPANYNAME_STR);
setOrganizationDomain(VER_COMPANYDOMAIN_STR);
setApplicationDisplayName(QStringLiteral(VER_PRODUCTNAME_STR));
setApplicationName(QStringLiteral(VER_INTERNALNAME_STR));
setOrganizationName(QStringLiteral(VER_COMPANYNAME_STR));
setOrganizationDomain(QStringLiteral(VER_COMPANYDOMAIN_STR));
// Setting the Application version
setApplicationVersion(APP_VERSION_STR);
// We have been running Puzzle in two different cases.
@ -261,7 +265,7 @@ VPApplication::VPApplication(int &argc, char **argv)
//---------------------------------------------------------------------------------------------------------------------
VPApplication::~VPApplication()
{
qDeleteAll(mainWindows);
qDeleteAll(m_mainWindows);
}
//---------------------------------------------------------------------------------------------------------------------
@ -342,11 +346,11 @@ auto VPApplication::IsAppInGUIMode() const -> bool
auto VPApplication::MainWindow()-> VPMainWindow *
{
Clean();
if (mainWindows.isEmpty())
if (m_mainWindows.isEmpty())
{
NewMainWindow();
}
return mainWindows[0];
return m_mainWindows[0];
}
//---------------------------------------------------------------------------------------------------------------------
@ -354,7 +358,8 @@ auto VPApplication::MainWindows() -> QList<VPMainWindow *>
{
Clean();
QList<VPMainWindow*> list;
for (auto &w : mainWindows)
list.reserve(m_mainWindows.size());
for (auto &w : m_mainWindows)
{
list.append(w);
}
@ -373,7 +378,7 @@ auto VPApplication::NewMainWindow() -> VPMainWindow *
auto VPApplication::NewMainWindow(const VPCommandLinePtr &cmd) -> VPMainWindow *
{
auto *puzzle = new VPMainWindow(cmd);
mainWindows.prepend(puzzle);
m_mainWindows.prepend(puzzle);
if (not cmd->IsTestModeEnabled())
{
puzzle->show();
@ -394,7 +399,7 @@ void VPApplication::InitOptions()
qCDebug(pApp, "Build revision: %s", BUILD_REVISION);
qCDebug(pApp, "%s", qUtf8Printable(buildCompatibilityString()));
qCDebug(pApp, "Built on %s at %s", __DATE__, __TIME__);
qCDebug(pApp, "Command-line arguments: %s", qUtf8Printable(arguments().join(", ")));
qCDebug(pApp, "Command-line arguments: %s", qUtf8Printable(arguments().join(QStringLiteral(", "))));
qCDebug(pApp, "Process ID: %s", qUtf8Printable(QString().setNum(applicationPid())));
CheckSystemLocale();
@ -406,13 +411,13 @@ void VPApplication::InitOptions()
VPCommandLine::Instance();
static const char * GENERIC_ICON_TO_CHECK = "document-open";
if (QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK) == false)
if (not QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK))
{
//If there is no default working icon theme then we should
//use an icon theme that we provide via a .qrc file
//This case happens under Windows and Mac OS X
//This does not happen under GNOME or KDE
QIcon::setThemeName("win.icon.theme");
QIcon::setThemeName(QStringLiteral("win.icon.theme"));
}
ActivateDarkMode();
}
@ -440,10 +445,10 @@ auto VPApplication::PuzzleSettings() -> VPSettings *
//---------------------------------------------------------------------------------------------------------------------
void VPApplication::ActivateDarkMode()
{
VPSettings *settings = VPApplication::VApp()->PuzzleSettings();
VPSettings *settings = PuzzleSettings();
if (settings->GetDarkMode())
{
QFile f(":qdarkstyle/style.qss");
QFile f(QStringLiteral(":qdarkstyle/style.qss"));
if (!f.exists())
{
qDebug()<<"Unable to set stylesheet, file not found\n";
@ -472,7 +477,7 @@ void VPApplication::ParseCommandLine(const SocketConnection &connection, const Q
{
qCDebug(pApp, "Connected to the server '%s'", qUtf8Printable(serverName));
QTextStream stream(&socket);
stream << arguments.join(";;");
stream << arguments.join(QStringLiteral(";;"));
stream.flush();
socket.waitForBytesWritten();
QCoreApplication::exit(V_EX_OK);
@ -480,24 +485,7 @@ void VPApplication::ParseCommandLine(const SocketConnection &connection, const Q
}
qCDebug(pApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName));
localServer = new QLocalServer(this);
connect(localServer, &QLocalServer::newConnection, this, &VPApplication::NewLocalSocketConnection);
if (not localServer->listen(serverName))
{
qCDebug(pApp, "Can't begin to listen for incoming connections on name '%s'",
qUtf8Printable(serverName));
if (localServer->serverError() == QAbstractSocket::AddressInUseError)
{
QLocalServer::removeServer(serverName);
if (not localServer->listen(serverName))
{
qCWarning(pApp, "%s",
qUtf8Printable(tr("Can't begin to listen for incoming connections on name '%1'").arg(serverName)));
}
}
}
StartLocalServer(serverName);
LoadTranslation(PuzzleSettings()->GetLocale());
}
@ -509,54 +497,7 @@ void VPApplication::ProcessArguments(const VPCommandLinePtr &cmd)
{
const QStringList rawLayouts = cmd->OptionRawLayouts();
const QStringList args = cmd->OptionFileNames();
if (args.count() > 0)
{
if (not cmd->IsGuiEnabled() && args.count() > 1)
{
qCCritical(pApp, "%s\n", qPrintable(tr("Export mode doesn't support opening several files.")));
cmd.get()->parser.showHelp(V_EX_USAGE);
}
if (args.count() > 1 && not rawLayouts.isEmpty())
{
qCCritical(pApp, "%s\n",
qPrintable(tr("Import raw layout data does not support opening several layout files.")));
cmd.get()->parser.showHelp(V_EX_USAGE);
}
for (const auto &arg : args)
{
NewMainWindow(cmd);
if (not MainWindow()->LoadFile(arg))
{
if (not cmd->IsGuiEnabled())
{
return; // process only one input file
}
delete MainWindow();
continue;
}
if (not rawLayouts.isEmpty())
{
MainWindow()->ImportRawLayouts(rawLayouts);
}
}
}
else
{
if (cmd->IsTestModeEnabled())
{
qCCritical(pApp, "%s\n", qPrintable(tr("Please, provide one input file.")));
cmd.get()->parser.showHelp(V_EX_USAGE);
}
NewMainWindow(cmd);
if (not rawLayouts.isEmpty())
{
MainWindow()->ImportRawLayouts(rawLayouts);
}
}
args.count() > 0 ? StartWithFiles(cmd, rawLayouts) : SingleStart(cmd, rawLayouts);
if (not cmd->IsGuiEnabled())
{
@ -637,7 +578,7 @@ void VPApplication::AboutToQuit()
//---------------------------------------------------------------------------------------------------------------------
void VPApplication::NewLocalSocketConnection()
{
QScopedPointer<QLocalSocket>socket(localServer->nextPendingConnection());
QScopedPointer<QLocalSocket>socket(m_localServer->nextPendingConnection());
if (socket.isNull())
{
return;
@ -647,7 +588,7 @@ void VPApplication::NewLocalSocketConnection()
const QString arg = stream.readAll();
if (not arg.isEmpty())
{
ParseCommandLine(SocketConnection::Server, arg.split(";;"));
ParseCommandLine(SocketConnection::Server, arg.split(QStringLiteral(";;")));
}
MainWindow()->raise();
MainWindow()->activateWindow();
@ -669,15 +610,93 @@ void VPApplication::SetPreferencesDialog(const QSharedPointer<DialogPuzzlePrefer
void VPApplication::Clean()
{
// cleanup any deleted main windows first
for (int i = mainWindows.count() - 1; i >= 0; --i)
for (int i = m_mainWindows.count() - 1; i >= 0; --i)
{
if (mainWindows.at(i).isNull())
if (m_mainWindows.at(i).isNull())
{
mainWindows.removeAt(i);
m_mainWindows.removeAt(i);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPApplication::StartLocalServer(const QString &serverName)
{
m_localServer = new QLocalServer(this);
connect(m_localServer, &QLocalServer::newConnection, this, &VPApplication::NewLocalSocketConnection);
if (not m_localServer->listen(serverName))
{
qCDebug(pApp, "Can't begin to listen for incoming connections on name '%s'",
qUtf8Printable(serverName));
if (m_localServer->serverError() == QAbstractSocket::AddressInUseError)
{
QLocalServer::removeServer(serverName);
if (not m_localServer->listen(serverName))
{
qCWarning(pApp, "%s",
qUtf8Printable(tr("Can't begin to listen for incoming connections on name '%1'")
.arg(serverName)));
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPApplication::StartWithFiles(const VPCommandLinePtr &cmd, const QStringList &rawLayouts)
{
const QStringList args = cmd->OptionFileNames();
if (args.count() > 0)
{
if (not cmd->IsGuiEnabled() && args.count() > 1)
{
qCCritical(pApp, "%s\n", qPrintable(tr("Export mode doesn't support opening several files.")));
cmd.get()->parser.showHelp(V_EX_USAGE);
}
if (args.count() > 1 && not rawLayouts.isEmpty())
{
qCCritical(pApp, "%s\n",
qPrintable(tr("Import raw layout data does not support opening several layout files.")));
cmd.get()->parser.showHelp(V_EX_USAGE);
}
for (const auto &arg : args)
{
NewMainWindow(cmd);
if (not MainWindow()->LoadFile(arg))
{
if (not cmd->IsGuiEnabled())
{
return; // process only one input file
}
delete MainWindow();
continue;
}
if (not rawLayouts.isEmpty())
{
MainWindow()->ImportRawLayouts(rawLayouts);
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPApplication::SingleStart(const VPCommandLinePtr &cmd, const QStringList &rawLayouts)
{
if (cmd->IsTestModeEnabled())
{
qCCritical(pApp, "%s\n", qPrintable(tr("Please, provide one input file.")));
cmd.get()->parser.showHelp(V_EX_USAGE);
}
NewMainWindow(cmd);
if (not rawLayouts.isEmpty())
{
MainWindow()->ImportRawLayouts(rawLayouts);
}
}
//---------------------------------------------------------------------------------------------------------------------
auto VPApplication::CommandLine() -> VPCommandLinePtr
{
@ -685,7 +704,7 @@ auto VPApplication::CommandLine() -> VPCommandLinePtr
}
//---------------------------------------------------------------------------------------------------------------------
VPApplication *VPApplication::VApp()
auto VPApplication::VApp() -> VPApplication *
{
return qobject_cast<VPApplication*>(QCoreApplication::instance());
}

View file

@ -42,32 +42,32 @@ enum class SocketConnection : bool {Client = false, Server = true};
class VPApplication : public VAbstractApplication
{
Q_OBJECT
Q_OBJECT // NOLINT
public:
VPApplication(int &argc, char **argv);
virtual ~VPApplication() override;
~VPApplication() override;
virtual bool notify(QObject * receiver, QEvent * event) override;
auto notify(QObject * receiver, QEvent * event) -> bool override;
virtual bool IsAppInGUIMode() const override;
VPMainWindow *MainWindow();
QList<VPMainWindow*> MainWindows();
VPMainWindow *NewMainWindow();
VPMainWindow *NewMainWindow(const VPCommandLinePtr &cmd);
auto IsAppInGUIMode() const -> bool override;
auto MainWindow() -> VPMainWindow *;
auto MainWindows() -> QList<VPMainWindow*>;
auto NewMainWindow() -> VPMainWindow *;
auto NewMainWindow(const VPCommandLinePtr &cmd) -> VPMainWindow *;
void InitOptions();
virtual const VTranslateVars *TrVars() override;
auto TrVars() -> const VTranslateVars * override;
virtual void OpenSettings() override;
VPSettings *PuzzleSettings();
void OpenSettings() override;
auto PuzzleSettings() -> VPSettings *;
void ActivateDarkMode();
void ParseCommandLine(const SocketConnection &connection, const QStringList &arguments);
void ProcessArguments(const VPCommandLinePtr &cmd);
static VPCommandLinePtr CommandLine();
static VPApplication *VApp();
static auto CommandLine() -> VPCommandLinePtr;
static auto VApp() -> VPApplication *;
auto PreferencesDialog() const -> QSharedPointer<DialogPuzzlePreferences>;
void SetPreferencesDialog(const QSharedPointer<DialogPuzzlePreferences> &newPreferencesDialog);
@ -76,22 +76,27 @@ public slots:
void ProcessCMD();
protected:
virtual void InitTrVars() override;
virtual bool event(QEvent *e) override;
void InitTrVars() override;
auto event(QEvent *e) -> bool override;
protected slots:
virtual void AboutToQuit() override;
void AboutToQuit() override;
private slots:
void NewLocalSocketConnection();
private:
Q_DISABLE_COPY(VPApplication)
QList<QPointer<VPMainWindow> > mainWindows{};
QLocalServer *localServer{nullptr};
Q_DISABLE_COPY_MOVE(VPApplication) //NOLINT
QList<QPointer<VPMainWindow> > m_mainWindows{};
QLocalServer *m_localServer{nullptr};
QWeakPointer<DialogPuzzlePreferences> m_preferencesDialog{};
void Clean();
void StartLocalServer(const QString &serverName);
void StartWithFiles(const VPCommandLinePtr &cmd, const QStringList &rawLayouts);
void SingleStart(const VPCommandLinePtr &cmd, const QStringList &rawLayouts);
};
#endif // VPAPPLICATION_H

View file

@ -35,10 +35,13 @@
#include "../ifc/exception/vexceptionemptyparameter.h"
#include "../ifc/exception/vexceptionwrongid.h"
#include "../vmisc/vsysexits.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 5, 0)
#include "../vmisc/diagnostic.h"
#endif // QT_VERSION < QT_VERSION_CHECK(5, 5, 0)
#include "../vmisc/qt_dispatch/qt_dispatch.h"
#include "../qmuparser/qmuparsererror.h"
#include "../vpatterndb/variables/vmeasurement.h"
#include "../fervor/fvupdater.h"
#include <QDir>
@ -53,6 +56,7 @@
#include <QGridLayout>
#include <QSpacerItem>
#include <QThread>
#include <QGlobalStatic>
#if defined(APPIMAGE) && defined(Q_OS_LINUX)
# include "../vmisc/appimage.h"
@ -62,12 +66,29 @@ QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
QT_WARNING_DISABLE_INTEL(1418)
Q_LOGGING_CATEGORY(mApp, "m.application")
Q_LOGGING_CATEGORY(mApp, "m.application") // NOLINT
QT_WARNING_POP
#include <QCommandLineParser>
namespace
{
Q_GLOBAL_STATIC_WITH_ARGS(const QString, LONG_OPTION_DIMENSION_A, (QLatin1String("dimensionA"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, SINGLE_OPTION_DIMENSION_A, (QChar('a'))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, LONG_OPTION_DIMENSION_B, (QLatin1String("dimensionB"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, SINGLE_OPTION_DIMENSION_B, (QChar('b'))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, LONG_OPTION_DIMENSION_C, (QLatin1String("dimensionC"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, SINGLE_OPTION_DIMENSION_C, (QChar('c'))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, LONG_OPTION_UNITS, (QLatin1String("units"))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, SINGLE_OPTION_UNITS, (QChar('u'))) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QString, LONG_OPTION_TEST, (QLatin1String("test"))) // NOLINT
} // namespace
//---------------------------------------------------------------------------------------------------------------------
inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
@ -263,17 +284,12 @@ inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &con
//---------------------------------------------------------------------------------------------------------------------
MApplication::MApplication(int &argc, char **argv)
:VAbstractApplication(argc, argv),
mainWindows(),
localServer(nullptr),
trVars(nullptr),
dataBase(QPointer<DialogMDataBase>()),
testMode(false)
:VAbstractApplication(argc, argv)
{
setApplicationDisplayName(VER_PRODUCTNAME_STR);
setApplicationName(VER_INTERNALNAME_STR);
setOrganizationName(VER_COMPANYNAME_STR);
setOrganizationDomain(VER_COMPANYDOMAIN_STR);
setApplicationDisplayName(QStringLiteral(VER_PRODUCTNAME_STR));
setApplicationName(QStringLiteral(VER_INTERNALNAME_STR));
setOrganizationName(QStringLiteral(VER_COMPANYNAME_STR));
setOrganizationDomain(QStringLiteral(VER_COMPANYDOMAIN_STR));
// Setting the Application version
setApplicationVersion(APP_VERSION_STR);
// We have been running Tape in two different cases.
@ -285,12 +301,12 @@ MApplication::MApplication(int &argc, char **argv)
//---------------------------------------------------------------------------------------------------------------------
MApplication::~MApplication()
{
qDeleteAll(mainWindows);
qDeleteAll(m_mainWindows);
delete trVars;
if (not dataBase.isNull())
delete m_trVars;
if (not m_dataBase.isNull())
{
delete dataBase;
delete m_dataBase;
}
}
@ -302,7 +318,7 @@ MApplication::~MApplication()
* @return value that is returned from the receiver's event handler.
*/
// reimplemented from QApplication so we can throw exceptions in slots
bool MApplication::notify(QObject *receiver, QEvent *event)
auto MApplication::notify(QObject *receiver, QEvent *event) -> bool
{
try
{
@ -372,37 +388,38 @@ bool MApplication::notify(QObject *receiver, QEvent *event)
}
//---------------------------------------------------------------------------------------------------------------------
bool MApplication::IsTestMode() const
auto MApplication::IsTestMode() const -> bool
{
return testMode;
return m_testMode;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief IsAppInGUIMode little hack that allow to have access to application state from VAbstractApplication class.
*/
bool MApplication::IsAppInGUIMode() const
auto MApplication::IsAppInGUIMode() const -> bool
{
return not IsTestMode();
}
//---------------------------------------------------------------------------------------------------------------------
TMainWindow *MApplication::MainWindow()
auto MApplication::MainWindow() -> TMainWindow *
{
Clean();
if (mainWindows.isEmpty())
if (m_mainWindows.isEmpty())
{
NewMainWindow();
}
return mainWindows[0];
return m_mainWindows[0];
}
//---------------------------------------------------------------------------------------------------------------------
QList<TMainWindow *> MApplication::MainWindows()
auto MApplication::MainWindows() -> QList<TMainWindow *>
{
Clean();
QList<TMainWindow*> list;
for (auto &w : mainWindows)
list.reserve(m_mainWindows.size());
for (auto &w : m_mainWindows)
{
list.append(w);
}
@ -420,7 +437,7 @@ void MApplication::InitOptions()
qCDebug(mApp, "Build revision: %s", BUILD_REVISION);
qCDebug(mApp, "%s", qUtf8Printable(buildCompatibilityString()));
qCDebug(mApp, "Built on %s at %s", __DATE__, __TIME__);
qCDebug(mApp, "Command-line arguments: %s", qUtf8Printable(arguments().join(", ")));
qCDebug(mApp, "Command-line arguments: %s", qUtf8Printable(arguments().join(QStringLiteral(", "))));
qCDebug(mApp, "Process ID: %s", qUtf8Printable(QString().setNum(applicationPid())));
CheckSystemLocale();
@ -428,13 +445,13 @@ void MApplication::InitOptions()
LoadTranslation(QString());// By default the console version uses system locale
static const char * GENERIC_ICON_TO_CHECK = "document-open";
if (QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK) == false)
if (not QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK))
{
//If there is no default working icon theme then we should
//use an icon theme that we provide via a .qrc file
//This case happens under Windows and Mac OS X
//This does not happen under GNOME or KDE
QIcon::setThemeName("win.icon.theme");
QIcon::setThemeName(QStringLiteral("win.icon.theme"));
}
ActivateDarkMode();
QResource::registerResource(diagramsPath());
@ -444,10 +461,10 @@ void MApplication::InitOptions()
// Dark mode
void MApplication::ActivateDarkMode()
{
VTapeSettings *settings = MApplication::VApp()->TapeSettings();
VTapeSettings *settings = TapeSettings();
if (settings->GetDarkMode())
{
QFile f(":qdarkstyle/style.qss");
QFile f(QStringLiteral(":qdarkstyle/style.qss"));
if (!f.exists())
{
qDebug()<<"Unable to set stylesheet, file not found\n";
@ -465,18 +482,18 @@ void MApplication::ActivateDarkMode()
//---------------------------------------------------------------------------------------------------------------------
void MApplication::InitTrVars()
{
if (trVars != nullptr)
if (m_trVars != nullptr)
{
trVars->Retranslate();
m_trVars->Retranslate();
}
else
{
trVars = new VTranslateVars();
m_trVars = new VTranslateVars();
}
}
//---------------------------------------------------------------------------------------------------------------------
bool MApplication::event(QEvent *e)
auto MApplication::event(QEvent *e) -> bool
{
switch(e->type())
{
@ -484,7 +501,7 @@ bool MApplication::event(QEvent *e)
// Mac specific).
case QEvent::FileOpen:
{
QFileOpenEvent *fileOpenEvent = static_cast<QFileOpenEvent *>(e);
auto *fileOpenEvent = static_cast<QFileOpenEvent *>(e);
const QString macFileOpen = fileOpenEvent->file();
if(not macFileOpen.isEmpty())
{
@ -532,14 +549,14 @@ void MApplication::OpenSettings()
}
//---------------------------------------------------------------------------------------------------------------------
VTapeSettings *MApplication::TapeSettings()
auto MApplication::TapeSettings() -> VTapeSettings *
{
SCASSERT(settings != nullptr)
return qobject_cast<VTapeSettings *>(settings);
}
//---------------------------------------------------------------------------------------------------------------------
QString MApplication::diagramsPath() const
auto MApplication::diagramsPath() -> QString
{
const QString dPath = QStringLiteral("/diagrams.rcc");
#ifdef Q_OS_WIN
@ -568,40 +585,38 @@ QString MApplication::diagramsPath() const
{
return file.absoluteFilePath();
}
else
{
#if defined(APPIMAGE) && defined(Q_OS_LINUX)
/* Fix path to diagrams when run inside AppImage. */
return AppImageRoot() + PKGDATADIR + dPath;
/* Fix path to diagrams when run inside AppImage. */
return AppImageRoot() + PKGDATADIR + dPath;
#else
return PKGDATADIR + dPath;
return PKGDATADIR + dPath;
#endif // defined(APPIMAGE) && defined(Q_OS_LINUX)
}
#endif
#endif // Unix
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::ShowDataBase()
{
if (dataBase.isNull())
if (m_dataBase.isNull())
{
dataBase = new DialogMDataBase();
dataBase->setAttribute(Qt::WA_DeleteOnClose, true);
dataBase->setModal(false);
dataBase->show();
m_dataBase = new DialogMDataBase();
m_dataBase->setAttribute(Qt::WA_DeleteOnClose, true);
m_dataBase->setModal(false);
m_dataBase->show();
}
else
{
dataBase->activateWindow();
m_dataBase->activateWindow();
}
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::RetranslateGroups()
{
if (not dataBase.isNull())
if (not m_dataBase.isNull())
{
dataBase->RetranslateGroups();
m_dataBase->RetranslateGroups();
}
}
@ -609,7 +624,7 @@ void MApplication::RetranslateGroups()
void MApplication::RetranslateTables()
{
const QList<TMainWindow*> list = MainWindows();
for (auto w : list)
for (auto *w : list)
{
w->RetranslateTable();
}
@ -622,49 +637,14 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
parser.setApplicationDescription(tr("Valentina's measurements editor."));
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("filename", tr("The measurement file."));
const QString LONG_OPTION_DIMENSION_A = QStringLiteral("dimensionA");
const QString SINGLE_OPTION_DIMENSION_A = QChar('a');
const QString LONG_OPTION_DIMENSION_B = QStringLiteral("dimensionB");
const QString SINGLE_OPTION_DIMENSION_B = QChar('b');
const QString LONG_OPTION_DIMENSION_C = QStringLiteral("dimensionC");
const QString SINGLE_OPTION_DIMENSION_C = QChar('c');
const QString LONG_OPTION_UNITS = QStringLiteral("units");
const QString SINGLE_OPTION_UNITS = QChar('u');
const QString LONG_OPTION_TEST = QStringLiteral("test");
parser.addOptions(
{
{{SINGLE_OPTION_DIMENSION_A, LONG_OPTION_DIMENSION_A}, tr("Set base for dimension A in the table units."),
tr("The dimension A base")},
{{SINGLE_OPTION_DIMENSION_B, LONG_OPTION_DIMENSION_B}, tr("Set base for dimension B in the table units."),
tr("The dimension B base")},
{{SINGLE_OPTION_DIMENSION_C, LONG_OPTION_DIMENSION_C}, tr("Set base for dimension C in the table units."),
tr("The dimension C base")},
{{SINGLE_OPTION_UNITS, LONG_OPTION_UNITS}, tr("Set pattern file units: cm, mm, inch."),
tr("The pattern units")},
{LONG_OPTION_TEST,
tr("Use for unit testing. Run the program and open a file without showing the main window.")},
{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")},
});
InitParserOptions(parser);
parser.process(arguments);
testMode = parser.isSet(LONG_OPTION_TEST);
m_testMode = parser.isSet(*LONG_OPTION_TEST);
if (not testMode && connection == SocketConnection::Client)
if (not m_testMode && connection == SocketConnection::Client)
{
const QString serverName = QCoreApplication::applicationName();
QLocalSocket socket;
@ -673,181 +653,24 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
{
qCDebug(mApp, "Connected to the server '%s'", qUtf8Printable(serverName));
QTextStream stream(&socket);
stream << QCoreApplication::arguments().join(";;");
stream << QCoreApplication::arguments().join(QStringLiteral(";;"));
stream.flush();
socket.waitForBytesWritten();
qApp->exit(V_EX_OK);
QCoreApplication::exit(V_EX_OK);
return;
}
qCDebug(mApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName));
localServer = new QLocalServer(this);
connect(localServer, &QLocalServer::newConnection, this, &MApplication::NewLocalSocketConnection);
if (not localServer->listen(serverName))
{
qCDebug(mApp, "Can't begin to listen for incoming connections on name '%s'",
qUtf8Printable(serverName));
if (localServer->serverError() == QAbstractSocket::AddressInUseError)
{
QLocalServer::removeServer(serverName);
if (not localServer->listen(serverName))
{
qCWarning(mApp, "%s",
qUtf8Printable(tr("Can't begin to listen for incoming connections on name '%1'").arg(serverName)));
}
}
}
StartLocalServer(serverName);
LoadTranslation(TapeSettings()->GetLocale());
}
const QStringList args = parser.positionalArguments();
if (args.count() > 0)
args.count() > 0 ? StartWithFiles(parser) : SingleStart(parser);
if (m_testMode)
{
if (testMode && args.count() > 1)
{
qCCritical(mApp, "%s\n", qPrintable(tr("Test mode doesn't support opening several files.")));
parser.showHelp(V_EX_USAGE);
}
bool flagDimensionA = false;
bool flagDimensionB = false;
bool flagDimensionC = false;
bool flagUnits = false;
int dimensionAValue = 0;
int dimensionBValue = 0;
int dimensionCValue = 0;
Unit unit = Unit::Cm;
if (parser.isSet(LONG_OPTION_DIMENSION_A))
{
const QString value = parser.value(LONG_OPTION_DIMENSION_A);
bool ok = false;
dimensionAValue = value.toInt(&ok);
if(ok && dimensionAValue > 0)
{
flagDimensionA = true;
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Invalid dimension A base value.")));
parser.showHelp(V_EX_USAGE);
}
}
if (parser.isSet(LONG_OPTION_DIMENSION_B))
{
const QString value = parser.value(LONG_OPTION_DIMENSION_B);
bool ok = false;
dimensionBValue = value.toInt(&ok);
if(ok && dimensionBValue > 0)
{
flagDimensionB = true;
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Invalid dimension B base value.")));
parser.showHelp(V_EX_USAGE);
}
}
if (parser.isSet(LONG_OPTION_DIMENSION_C))
{
const QString value = parser.value(LONG_OPTION_DIMENSION_C);
bool ok = false;
dimensionCValue = value.toInt(&ok);
if(ok && dimensionCValue > 0)
{
flagDimensionC = true;
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Invalid dimension C base value.")));
parser.showHelp(V_EX_USAGE);
}
}
{
const QString unitValue = parser.value(LONG_OPTION_UNITS);
if (not unitValue.isEmpty())
{
if (QStringList{unitMM, unitCM, unitINCH}.contains(unitValue))
{
flagUnits = true;
unit = StrToUnits(unitValue);
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Invalid base size argument. Must be cm, mm or inch.")));
parser.showHelp(V_EX_USAGE);
}
}
}
for (const auto &arg : args)
{
NewMainWindow();
if (not MainWindow()->LoadFile(arg))
{
if (testMode)
{
return; // process only one input file
}
delete MainWindow();
continue;
}
if (flagDimensionA)
{
if (not MainWindow()->SetDimensionABase(dimensionAValue))
{
parser.showHelp(V_EX_USAGE);
}
}
if (flagDimensionB)
{
if (not MainWindow()->SetDimensionBBase(dimensionBValue))
{
parser.showHelp(V_EX_USAGE);
}
}
if (flagDimensionC)
{
if (not MainWindow()->SetDimensionCBase(dimensionCValue))
{
parser.showHelp(V_EX_USAGE);
}
}
if (flagUnits)
{
MainWindow()->SetPUnit(unit);
}
}
}
else
{
if (not testMode)
{
NewMainWindow();
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Please, provide one input file.")));
parser.showHelp(V_EX_USAGE);
}
}
if (testMode)
{
qApp->exit(V_EX_OK); // close program after processing in console mode
QCoreApplication::exit(V_EX_OK); // close program after processing in console mode
}
}
@ -858,10 +681,10 @@ auto MApplication::VApp() -> MApplication *
}
//---------------------------------------------------------------------------------------------------------------------
TMainWindow *MApplication::NewMainWindow()
auto MApplication::NewMainWindow() -> TMainWindow *
{
TMainWindow *tape = new TMainWindow();
mainWindows.prepend(tape);
auto *tape = new TMainWindow();
m_mainWindows.prepend(tape);
if (not MApplication::VApp()->IsTestMode())
{
tape->show();
@ -888,7 +711,7 @@ void MApplication::ProcessCMD()
//---------------------------------------------------------------------------------------------------------------------
void MApplication::NewLocalSocketConnection()
{
QLocalSocket *socket = localServer->nextPendingConnection();
QLocalSocket *socket = m_localServer->nextPendingConnection();
if (not socket)
{
return;
@ -898,7 +721,7 @@ void MApplication::NewLocalSocketConnection()
const QString arg = stream.readAll();
if (not arg.isEmpty())
{
ParseCommandLine(SocketConnection::Server, arg.split(";;"));
ParseCommandLine(SocketConnection::Server, arg.split(QStringLiteral(";;")));
}
delete socket;
MainWindow()->raise();
@ -909,11 +732,222 @@ void MApplication::NewLocalSocketConnection()
void MApplication::Clean()
{
// cleanup any deleted main windows first
for (int i = mainWindows.count() - 1; i >= 0; --i)
for (int i = m_mainWindows.count() - 1; i >= 0; --i)
{
if (mainWindows.at(i).isNull())
if (m_mainWindows.at(i).isNull())
{
mainWindows.removeAt(i);
m_mainWindows.removeAt(i);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::InitParserOptions(QCommandLineParser &parser)
{
parser.addPositionalArgument(QStringLiteral("filename"), tr("The measurement file."));
parser.addOptions(
{
{{*SINGLE_OPTION_DIMENSION_A, *LONG_OPTION_DIMENSION_A}, tr("Set base for dimension A in the table units."),
tr("The dimension A base")},
{{*SINGLE_OPTION_DIMENSION_B, *LONG_OPTION_DIMENSION_B}, tr("Set base for dimension B in the table units."),
tr("The dimension B base")},
{{*SINGLE_OPTION_DIMENSION_C, *LONG_OPTION_DIMENSION_C}, tr("Set base for dimension C in the table units."),
tr("The dimension C base")},
{{*SINGLE_OPTION_UNITS, *LONG_OPTION_UNITS}, tr("Set pattern file units: cm, mm, inch."),
tr("The pattern units")},
{*LONG_OPTION_TEST,
tr("Use for unit testing. Run the program and open a file without showing the main window.")},
{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")},
});
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::StartLocalServer(const QString &serverName)
{
m_localServer = new QLocalServer(this);
connect(m_localServer, &QLocalServer::newConnection, this, &MApplication::NewLocalSocketConnection);
if (not m_localServer->listen(serverName))
{
qCDebug(mApp, "Can't begin to listen for incoming connections on name '%s'",
qUtf8Printable(serverName));
if (m_localServer->serverError() == QAbstractSocket::AddressInUseError)
{
QLocalServer::removeServer(serverName);
if (not m_localServer->listen(serverName))
{
qCWarning(mApp, "%s",
qUtf8Printable(tr("Can't begin to listen for incoming connections on name '%1'")
.arg(serverName)));
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::StartWithFiles(QCommandLineParser &parser)
{
const QStringList args = parser.positionalArguments();
if (args.count() <= 0)
{
return;
}
if (m_testMode && args.count() > 1)
{
qCCritical(mApp, "%s\n", qPrintable(tr("Test mode doesn't support opening several files.")));
parser.showHelp(V_EX_USAGE);
}
bool flagDimensionA = false;
bool flagDimensionB = false;
bool flagDimensionC = false;
bool flagUnits = false;
int dimensionAValue = 0;
int dimensionBValue = 0;
int dimensionCValue = 0;
Unit unit = Unit::Cm;
ParseDimensionAOption(parser, dimensionAValue, flagDimensionA);
ParseDimensionBOption(parser, dimensionBValue, flagDimensionB);
ParseDimensionCOption(parser, dimensionCValue, flagDimensionC);
ParseUnitsOption(parser, unit, flagUnits);
for (const auto &arg : args)
{
NewMainWindow();
if (not MainWindow()->LoadFile(arg))
{
if (m_testMode)
{
return; // process only one input file
}
delete MainWindow();
continue;
}
if (flagDimensionA && not MainWindow()->SetDimensionABase(dimensionAValue))
{
parser.showHelp(V_EX_USAGE);
}
if (flagDimensionB && not MainWindow()->SetDimensionBBase(dimensionBValue))
{
parser.showHelp(V_EX_USAGE);
}
if (flagDimensionC && not MainWindow()->SetDimensionCBase(dimensionCValue))
{
parser.showHelp(V_EX_USAGE);
}
if (flagUnits)
{
MainWindow()->SetPUnit(unit);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::SingleStart(QCommandLineParser &parser)
{
if (not m_testMode)
{
NewMainWindow();
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Please, provide one input file.")));
parser.showHelp(V_EX_USAGE);
}
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::ParseDimensionAOption(QCommandLineParser &parser, int &dimensionAValue, bool &flagDimensionA)
{
if (parser.isSet(*LONG_OPTION_DIMENSION_A))
{
const QString value = parser.value(*LONG_OPTION_DIMENSION_A);
bool ok = false;
dimensionAValue = value.toInt(&ok);
if(ok && dimensionAValue > 0)
{
flagDimensionA = true;
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Invalid dimension A base value.")));
parser.showHelp(V_EX_USAGE);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::ParseDimensionBOption(QCommandLineParser &parser, int &dimensionBValue, bool &flagDimensionB)
{
if (parser.isSet(*LONG_OPTION_DIMENSION_B))
{
const QString value = parser.value(*LONG_OPTION_DIMENSION_B);
bool ok = false;
dimensionBValue = value.toInt(&ok);
if(ok && dimensionBValue > 0)
{
flagDimensionB = true;
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Invalid dimension B base value.")));
parser.showHelp(V_EX_USAGE);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::ParseDimensionCOption(QCommandLineParser &parser, int &dimensionCValue, bool &flagDimensionC)
{
if (parser.isSet(*LONG_OPTION_DIMENSION_C))
{
const QString value = parser.value(*LONG_OPTION_DIMENSION_C);
bool ok = false;
dimensionCValue = value.toInt(&ok);
if(ok && dimensionCValue > 0)
{
flagDimensionC = true;
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Invalid dimension C base value.")));
parser.showHelp(V_EX_USAGE);
}
}
}
//---------------------------------------------------------------------------------------------------------------------
void MApplication::ParseUnitsOption(QCommandLineParser &parser, Unit &unit, bool &flagUnits)
{
const QString unitValue = parser.value(*LONG_OPTION_UNITS);
if (not unitValue.isEmpty())
{
if (QStringList{unitMM, unitCM, unitINCH}.contains(unitValue))
{
flagUnits = true;
unit = StrToUnits(unitValue);
}
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Invalid base size argument. Must be cm, mm or inch.")));
parser.showHelp(V_EX_USAGE);
}
}
}

View file

@ -30,41 +30,41 @@
#define MAPPLICATION_H
#include "../vpatterndb/vtranslatevars.h"
#include "../vmisc/def.h"
#include "vtapesettings.h"
#include "../vmisc/vabstractapplication.h"
#include "dialogs/dialogmdatabase.h"
class TMainWindow;
class QLocalServer;
class QCommandLineParser;
enum class SocketConnection : bool {Client = false, Server = true};
class MApplication : public VAbstractApplication
{
Q_OBJECT
Q_OBJECT // NOLINT
public:
MApplication(int &argc, char **argv);
virtual ~MApplication() override;
~MApplication() override;
virtual bool notify(QObject * receiver, QEvent * event) override;
auto notify(QObject * receiver, QEvent * event) -> bool override;
bool IsTestMode() const;
virtual bool IsAppInGUIMode() const override;
TMainWindow *MainWindow();
QList<TMainWindow*> MainWindows();
TMainWindow *NewMainWindow();
auto IsTestMode() const -> bool;
auto IsAppInGUIMode() const -> bool override;
auto MainWindow() -> TMainWindow *;
auto MainWindows() -> QList<TMainWindow*>;
auto NewMainWindow() -> TMainWindow *;
void InitOptions();
virtual const VTranslateVars *TrVars() override;
auto TrVars() -> const VTranslateVars * override;
virtual void OpenSettings() override;
VTapeSettings *TapeSettings();
void OpenSettings() override;
auto TapeSettings() -> VTapeSettings *;
void ActivateDarkMode();
QString diagramsPath() const;
static auto diagramsPath() -> QString;
void ShowDataBase();
void RetranslateGroups();
@ -72,36 +72,47 @@ public:
void ParseCommandLine(const SocketConnection &connection, const QStringList &arguments);
static MApplication *VApp();
static auto VApp() -> MApplication *;
public slots:
void ProcessCMD();
protected:
virtual void InitTrVars() override;
virtual bool event(QEvent *e) override;
void InitTrVars() override;
auto event(QEvent *e) -> bool override;
protected slots:
virtual void AboutToQuit() override;
void AboutToQuit() override;
private slots:
void NewLocalSocketConnection();
private:
Q_DISABLE_COPY(MApplication)
QList<QPointer<TMainWindow> > mainWindows;
QLocalServer *localServer;
VTranslateVars *trVars;
QPointer<DialogMDataBase> dataBase;
bool testMode;
Q_DISABLE_COPY_MOVE(MApplication) // NOLINT
QList<QPointer<TMainWindow> > m_mainWindows{};
QLocalServer *m_localServer{nullptr};
VTranslateVars *m_trVars{nullptr};
QPointer<DialogMDataBase> m_dataBase{};
bool m_testMode{false};
void Clean();
static void InitParserOptions(QCommandLineParser &parser);
void StartLocalServer(const QString &serverName);
void StartWithFiles(QCommandLineParser &parser);
void SingleStart(QCommandLineParser &parser);
static void ParseDimensionAOption(QCommandLineParser &parser, int &dimensionAValue, bool &flagDimensionA);
static void ParseDimensionBOption(QCommandLineParser &parser, int &dimensionBValue, bool &flagDimensionB);
static void ParseDimensionCOption(QCommandLineParser &parser, int &dimensionCValue, bool &flagDimensionC);
static void ParseUnitsOption(QCommandLineParser &parser, Unit &unit , bool &flagUnits);
};
//---------------------------------------------------------------------------------------------------------------------
inline const VTranslateVars *MApplication::TrVars()
inline auto MApplication::TrVars() -> const VTranslateVars *
{
return trVars;
return m_trVars;
}
#endif // MAPPLICATION_H

View file

@ -56,7 +56,7 @@ QT_WARNING_PUSH
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
QT_WARNING_DISABLE_INTEL(1418)
Q_LOGGING_CATEGORY(vApp, "v.application")
Q_LOGGING_CATEGORY(vApp, "v.application") // NOLINT
QT_WARNING_POP
@ -64,25 +64,23 @@ Q_DECL_CONSTEXPR auto DAYS_TO_KEEP_LOGS = 3;
namespace
{
QString AppFilePath(const QString &appName)
auto AppFilePath(const QString &appName) -> QString
{
QString appNameExe = appName;
#ifdef Q_OS_WIN
appNameExe += ".exe";
#endif
QFileInfo canonicalFile(QString("%1/%2").arg(QCoreApplication::applicationDirPath(), appNameExe));
QFileInfo canonicalFile(QStringLiteral("%1/%2").arg(QCoreApplication::applicationDirPath(), appNameExe));
if (canonicalFile.exists())
{
return canonicalFile.absoluteFilePath();
}
else
{
QFileInfo debugFile(QString("%1/../../%2/bin/%3")
.arg(QCoreApplication::applicationDirPath(), appName, appNameExe));
return debugFile.exists() ? debugFile.absoluteFilePath() : appNameExe;
}
}
QFileInfo debugFile(QStringLiteral("%1/../../%2/bin/%3")
.arg(QCoreApplication::applicationDirPath(), appName, appNameExe));
return debugFile.exists() ? debugFile.absoluteFilePath() : appNameExe;
}
} // namespace
//---------------------------------------------------------------------------------------------------------------------
inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
@ -307,8 +305,6 @@ inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &con
}
}
#defin