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
}
}
#define DefWidth 1.2//mm
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief VApplication constructor.
@ -316,16 +312,12 @@ inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &con
* @param argv command line.
*/
VApplication::VApplication(int &argc, char **argv)
: VAbstractValApplication(argc, argv),
trVars(nullptr),
autoSaveTimer(nullptr),
lockLog(),
out(nullptr)
: VAbstractValApplication(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);
// making sure will create new instance...just in case we will ever do 2 objects of VApplication
@ -337,7 +329,7 @@ VApplication::~VApplication()
{
qCDebug(vApp, "Application closing.");
qInstallMessageHandler(nullptr); // Resore the message handler
delete trVars;
delete m_trVars;
VCommandLine::Reset();
}
@ -386,7 +378,7 @@ void VApplication::NewValentina(const QString &fileName)
* @return value that is returned from the receiver's event handler.
*/
// reimplemented from QApplication so we can throw exceptions in slots
bool VApplication::notify(QObject *receiver, QEvent *event)
auto VApplication::notify(QObject *receiver, QEvent *event) -> bool
{
try
{
@ -465,7 +457,7 @@ bool VApplication::notify(QObject *receiver, QEvent *event)
//---------------------------------------------------------------------------------------------------------------------
void VApplication::ActivateDarkMode()
{
VValentinaSettings *settings = VAbstractValApplication::VApp()->ValentinaSettings();
VValentinaSettings *settings = ValentinaSettings();
if (settings->GetDarkMode())
{
QFile f(QStringLiteral(":qdarkstyle/style.qss"));
@ -479,24 +471,23 @@ void VApplication::ActivateDarkMode()
QTextStream ts(&f);
qApp->setStyleSheet(ts.readAll());
}
}
}
//---------------------------------------------------------------------------------------------------------------------
QString VApplication::TapeFilePath() const
auto VApplication::TapeFilePath() -> QString
{
return AppFilePath(QStringLiteral("tape"));
}
//---------------------------------------------------------------------------------------------------------------------
QString VApplication::PuzzleFilePath() const
auto VApplication::PuzzleFilePath() -> QString
{
return AppFilePath(QStringLiteral("puzzle"));
}
//---------------------------------------------------------------------------------------------------------------------
QString VApplication::LogDirPath() const
auto VApplication::LogDirPath() -> QString
{
#if defined(Q_OS_WIN) || defined(Q_OS_OSX)
const QString logDirPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QString(),
@ -510,16 +501,16 @@ QString VApplication::LogDirPath() const
}
//---------------------------------------------------------------------------------------------------------------------
QString VApplication::LogPath() const
auto VApplication::LogPath() -> QString
{
return QStringLiteral("%1/valentina-pid%2.log").arg(LogDirPath()).arg(applicationPid());
}
//---------------------------------------------------------------------------------------------------------------------
bool VApplication::CreateLogDir() const
auto VApplication::CreateLogDir() -> bool
{
QDir logDir(LogDirPath());
if (logDir.exists() == false)
if (not logDir.exists())
{
return logDir.mkpath(QChar('.')); // Create directory for log if need
}
@ -529,13 +520,13 @@ bool VApplication::CreateLogDir() const
//---------------------------------------------------------------------------------------------------------------------
void VApplication::BeginLogging()
{
VlpCreateLock(lockLog, LogPath(), [this](){return new QFile(LogPath());});
VlpCreateLock(m_lockLog, LogPath(), [](){return new QFile(LogPath());});
if (lockLog->IsLocked())
if (m_lockLog->IsLocked())
{
if (lockLog->GetProtected()->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
if (m_lockLog->GetProtected()->open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
{
out.reset(new QTextStream(lockLog->GetProtected().data()));
m_out.reset(new QTextStream(m_lockLog->GetProtected().data()));
qInstallMessageHandler(noisyFailureMsgHandler);
qCDebug(vApp, "Log file %s was locked.", qUtf8Printable(LogPath()));
}
@ -552,18 +543,18 @@ void VApplication::BeginLogging()
}
//---------------------------------------------------------------------------------------------------------------------
void VApplication::ClearOldLogs() const
void VApplication::ClearOldLogs()
{
const QString workingDirectory = QDir::currentPath();// Save the app working directory
QDir logsDir(LogDirPath());
logsDir.setNameFilters(QStringList("*.log"));
logsDir.setCurrent(LogDirPath());
logsDir.setNameFilters(QStringList(QStringLiteral("*.log")));
QDir::setCurrent(LogDirPath());
const QStringList allFiles = logsDir.entryList(QDir::NoDotAndDotDot | QDir::Files);
if (allFiles.isEmpty() == false)
if (not allFiles.isEmpty())
{
qCDebug(vApp, "Clearing old logs");
for (auto &fn : allFiles)
for (const auto &fn : allFiles)
{
QFileInfo info(fn);
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
@ -640,7 +631,7 @@ void VApplication::InitOptions()
}
//---------------------------------------------------------------------------------------------------------------------
QStringList VApplication::LabelLanguages()
auto VApplication::LabelLanguages() -> QStringList
{
QStringList list {"de", // German
"en", // English
@ -666,32 +657,32 @@ void VApplication::StartLogging()
}
//---------------------------------------------------------------------------------------------------------------------
QTextStream *VApplication::LogFile()
auto VApplication::LogFile() -> QTextStream *
{
return out.get();
return m_out.get();
}
//---------------------------------------------------------------------------------------------------------------------
const VTranslateVars *VApplication::TrVars()
auto VApplication::TrVars() -> const VTranslateVars *
{
return trVars;
return m_trVars;
}
//---------------------------------------------------------------------------------------------------------------------
void VApplication::InitTrVars()
{
if (trVars != nullptr)
if (m_trVars != nullptr)
{
trVars->Retranslate();
m_trVars->Retranslate();
}
else
{
trVars = new VTranslateVars();
m_trVars = new VTranslateVars();
}
}
//---------------------------------------------------------------------------------------------------------------------
bool VApplication::event(QEvent *e)
auto VApplication::event(QEvent *e) -> bool
{
switch(e->type())
{
@ -699,11 +690,11 @@ bool VApplication::event(QEvent *e)
// Mac specific).
case QEvent::FileOpen:
{
QFileOpenEvent *fileOpenEvent = static_cast<QFileOpenEvent *>(e);
auto *fileOpenEvent = dynamic_cast<QFileOpenEvent *>(e);
const QString macFileOpen = fileOpenEvent->file();
if(not macFileOpen.isEmpty())
{
MainWindow *window = qobject_cast<MainWindow*>(mainWindow);
auto *window = qobject_cast<MainWindow*>(mainWindow);
if (window)
{
window->LoadPattern(macFileOpen); // open file in existing window
@ -738,7 +729,7 @@ void VApplication::AboutToQuit()
}
//---------------------------------------------------------------------------------------------------------------------
bool VApplication::IsGUIMode()
auto VApplication::IsGUIMode() -> bool
{
return (VCommandLine::instance != nullptr) && VCommandLine::instance->IsGuiEnabled();
}
@ -747,13 +738,13 @@ bool VApplication::IsGUIMode()
/**
* @brief IsAppInGUIMode little hack that allow to have access to application state from VAbstractApplication class.
*/
bool VApplication::IsAppInGUIMode() const
auto VApplication::IsAppInGUIMode() const -> bool
{
return IsGUIMode();
}
//---------------------------------------------------------------------------------------------------------------------
bool VApplication::IsPedantic() const
auto VApplication::IsPedantic() const -> bool
{
return (VCommandLine::instance != nullptr) && VCommandLine::instance->IsPedantic();
}
@ -765,7 +756,7 @@ auto VApplication::VApp() -> VApplication *
}
//---------------------------------------------------------------------------------------------------------------------
const VCommandLinePtr VApplication::CommandLine() const
auto VApplication::CommandLine() -> VCommandLinePtr
{
return VCommandLine::instance;
}

View File

@ -30,7 +30,6 @@
#define VAPPLICATION_H
#include "../vmisc/vabstractvalapplication.h"
#include "../vwidgets/vmaingraphicsview.h"
#include "../vpatterndb/vtranslatevars.h"
#include "vcmdexport.h"
#include "vlockguard.h"
@ -42,72 +41,73 @@ class VApplication;// use in define
*/
class VApplication : public VAbstractValApplication
{
Q_OBJECT
Q_OBJECT // NOLINT
public:
VApplication(int &argc, char ** argv);
virtual ~VApplication() override;
static void NewValentina(const QString &fileName = QString());
virtual bool notify(QObject * receiver, QEvent * event) override;
~VApplication() override;
void InitOptions();
static void NewValentina(const QString &fileName = QString());
auto notify(QObject * receiver, QEvent * event) -> bool override;
QString TapeFilePath() const;
QString PuzzleFilePath() const;
void InitOptions();
QTimer *getAutoSaveTimer() const;
void setAutoSaveTimer(QTimer *value);
static auto TapeFilePath() -> QString;
static auto PuzzleFilePath() -> QString;
static QStringList LabelLanguages();
auto getAutoSaveTimer() const -> QTimer *;
void setAutoSaveTimer(QTimer *value);
void StartLogging();
void ActivateDarkMode();
QTextStream *LogFile();
static auto LabelLanguages() -> QStringList;
virtual const VTranslateVars *TrVars() override;
void StartLogging();
void ActivateDarkMode();
auto LogFile() -> QTextStream *;
bool static IsGUIMode();
virtual bool IsAppInGUIMode() const override;
virtual bool IsPedantic() const override;
auto TrVars() -> const VTranslateVars * override;
static VApplication *VApp();
auto static IsGUIMode() -> bool;
auto IsAppInGUIMode() const -> bool override;
auto IsPedantic() const -> bool override;
static auto VApp() -> VApplication *;
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:
Q_DISABLE_COPY(VApplication)
VTranslateVars *trVars;
QTimer *autoSaveTimer;
Q_DISABLE_COPY_MOVE(VApplication) // NOLINT
VTranslateVars *m_trVars{nullptr};
QTimer *m_autoSaveTimer{nullptr};
QSharedPointer<VLockGuard<QFile>> lockLog;
std::shared_ptr<QTextStream> out;
QSharedPointer<VLockGuard<QFile>> m_lockLog{};
std::shared_ptr<QTextStream> m_out{nullptr};
QString LogDirPath()const;
QString LogPath()const;
bool CreateLogDir()const;
void BeginLogging();
void ClearOldLogs()const;
static auto LogDirPath() -> QString;
static auto LogPath() -> QString;
static auto CreateLogDir() -> bool;
void BeginLogging();
static void ClearOldLogs();
public:
//moved to the end of class so merge should go
const VCommandLinePtr CommandLine() const;
static auto CommandLine() -> VCommandLinePtr;
};
//---------------------------------------------------------------------------------------------------------------------
inline QTimer *VApplication::getAutoSaveTimer() const
inline auto VApplication::getAutoSaveTimer() const -> QTimer *
{
return autoSaveTimer;
return m_autoSaveTimer;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VApplication::setAutoSaveTimer(QTimer *value)
{
autoSaveTimer = value;
m_autoSaveTimer = value;
}
#endif // VAPPLICATION_H

View File

@ -28,11 +28,12 @@
#include "preferencespatternpage.h"
#include "ui_preferencespatternpage.h"
#include "../../core/vapplication.h"
#include "../ifc/xml/vabstractpattern.h"
#include "../dialogdatetimeformats.h"
#include "../dialogknownmaterials.h"
#include "../vmisc/vvalentinasettings.h"
#include "../vmisc/vabstractvalapplication.h"
#include "../vwidgets/vmaingraphicsview.h"
#include "../ifc/xml/vabstractpattern.h"
#include <QMessageBox>
#include <QDate>