Review class VPApplication.

This commit is contained in:
Roman Telezhynskyi 2021-05-20 17:10:43 +03:00
parent e15b370ed4
commit a0dba0ea25
4 changed files with 40 additions and 40 deletions

View File

@ -61,7 +61,7 @@ inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &con
// writing a multithreaded application and the error happens on // writing a multithreaded application and the error happens on
// a non-GUI thread, you'll have to queue the message to the GUI // a non-GUI thread, you'll have to queue the message to the GUI
QCoreApplication *instance = QCoreApplication::instance(); QCoreApplication *instance = QCoreApplication::instance();
const bool isGuiThread = instance && (QThread::currentThread() == instance->thread()); const bool isGuiThread = (instance != nullptr) && (QThread::currentThread() == instance->thread());
if (not isGuiThread) if (not isGuiThread)
{ {
@ -199,7 +199,7 @@ inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &con
if (type == QtWarningMsg || type == QtCriticalMsg || type == QtFatalMsg) if (type == QtWarningMsg || type == QtCriticalMsg || type == QtFatalMsg)
{ {
if (qApp->IsAppInGUIMode()) if (VPApplication::VApp()->IsAppInGUIMode())
{ {
if (topWinAllowsPop) if (topWinAllowsPop)
{ {
@ -234,9 +234,7 @@ inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &con
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPApplication::VPApplication(int &argc, char **argv) VPApplication::VPApplication(int &argc, char **argv)
:VAbstractApplication(argc, argv), :VAbstractApplication(argc, argv)
mainWindows(),
localServer(nullptr)
{ {
setApplicationDisplayName(VER_PRODUCTNAME_STR); setApplicationDisplayName(VER_PRODUCTNAME_STR);
setApplicationName(VER_INTERNALNAME_STR); setApplicationName(VER_INTERNALNAME_STR);
@ -264,7 +262,7 @@ VPApplication::~VPApplication()
* @return value that is returned from the receiver's event handler. * @return value that is returned from the receiver's event handler.
*/ */
// reimplemented from QApplication so we can throw exceptions in slots // reimplemented from QApplication so we can throw exceptions in slots
bool VPApplication::notify(QObject *receiver, QEvent *event) auto VPApplication::notify(QObject *receiver, QEvent *event) -> bool
{ {
try try
{ {
@ -325,13 +323,13 @@ bool VPApplication::notify(QObject *receiver, QEvent *event)
/** /**
* @brief IsAppInGUIMode little hack that allow to have access to application state from VAbstractApplication class. * @brief IsAppInGUIMode little hack that allow to have access to application state from VAbstractApplication class.
*/ */
bool VPApplication::IsAppInGUIMode() const auto VPApplication::IsAppInGUIMode() const -> bool
{ {
return CommandLine()->IsGuiEnabled(); return CommandLine()->IsGuiEnabled();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPMainWindow *VPApplication::MainWindow() auto VPApplication::MainWindow()-> VPMainWindow *
{ {
Clean(); Clean();
if (mainWindows.isEmpty()) if (mainWindows.isEmpty())
@ -344,7 +342,7 @@ VPMainWindow *VPApplication::MainWindow()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QList<VPMainWindow *> VPApplication::MainWindows() auto VPApplication::MainWindows() -> QList<VPMainWindow *>
{ {
Clean(); Clean();
QList<VPMainWindow*> list; QList<VPMainWindow*> list;
@ -356,7 +354,7 @@ QList<VPMainWindow *> VPApplication::MainWindows()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPMainWindow *VPApplication::NewMainWindow(const VPCommandLinePtr &cmd) auto VPApplication::NewMainWindow(const VPCommandLinePtr &cmd) -> VPMainWindow *
{ {
VPMainWindow *puzzle = new VPMainWindow(cmd); VPMainWindow *puzzle = new VPMainWindow(cmd);
mainWindows.prepend(puzzle); mainWindows.prepend(puzzle);
@ -399,7 +397,7 @@ void VPApplication::InitOptions()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
const VTranslateVars *VPApplication::TrVars() auto VPApplication::TrVars() -> const VTranslateVars *
{ {
return nullptr; return nullptr;
} }
@ -412,7 +410,7 @@ void VPApplication::OpenSettings()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPSettings *VPApplication::PuzzleSettings() auto VPApplication::PuzzleSettings() -> VPSettings *
{ {
SCASSERT(settings != nullptr) SCASSERT(settings != nullptr)
return qobject_cast<VPSettings *>(settings); return qobject_cast<VPSettings *>(settings);
@ -421,7 +419,7 @@ VPSettings *VPApplication::PuzzleSettings()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPApplication::ActivateDarkMode() void VPApplication::ActivateDarkMode()
{ {
VPSettings *settings = qApp->PuzzleSettings(); VPSettings *settings = VPApplication::VApp()->PuzzleSettings();
if (settings->GetDarkMode()) if (settings->GetDarkMode())
{ {
QFile f(":qdarkstyle/style.qss"); QFile f(":qdarkstyle/style.qss");
@ -498,14 +496,14 @@ void VPApplication::ProcessArguments(const VPCommandLinePtr &cmd)
cmd.get()->parser.showHelp(V_EX_USAGE); cmd.get()->parser.showHelp(V_EX_USAGE);
} }
if (args.count() > 1 && rawLayouts.size() > 0) if (args.count() > 1 && not rawLayouts.isEmpty())
{ {
qCCritical(pApp, "%s\n", qCCritical(pApp, "%s\n",
qPrintable(tr("Import raw layout data does not support penning several layout files."))); qPrintable(tr("Import raw layout data does not support openning several layout files.")));
cmd.get()->parser.showHelp(V_EX_USAGE); cmd.get()->parser.showHelp(V_EX_USAGE);
} }
for (auto &arg : args) for (const auto &arg : args)
{ {
NewMainWindow(cmd); NewMainWindow(cmd);
if (not MainWindow()->LoadFile(arg)) if (not MainWindow()->LoadFile(arg))
@ -518,7 +516,7 @@ void VPApplication::ProcessArguments(const VPCommandLinePtr &cmd)
continue; continue;
} }
if (rawLayouts.size() > 0) if (not rawLayouts.isEmpty())
{ {
MainWindow()->ImportRawLayouts(rawLayouts); MainWindow()->ImportRawLayouts(rawLayouts);
} }
@ -533,7 +531,7 @@ void VPApplication::ProcessArguments(const VPCommandLinePtr &cmd)
} }
NewMainWindow(cmd); NewMainWindow(cmd);
if (rawLayouts.size() > 0) if (not rawLayouts.isEmpty())
{ {
// MainWindow()->New(); // prepare layout settings // MainWindow()->New(); // prepare layout settings
MainWindow()->ImportRawLayouts(rawLayouts); MainWindow()->ImportRawLayouts(rawLayouts);
@ -553,7 +551,7 @@ void VPApplication::ProcessCMD()
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
bool VPApplication::event(QEvent *e) auto VPApplication::event(QEvent *e) -> bool
{ {
switch(e->type()) switch(e->type())
{ {
@ -561,12 +559,12 @@ bool VPApplication::event(QEvent *e)
// Mac specific). // Mac specific).
case QEvent::FileOpen: case QEvent::FileOpen:
{ {
QFileOpenEvent *fileOpenEvent = static_cast<QFileOpenEvent *>(e); auto *fileOpenEvent = static_cast<QFileOpenEvent *>(e);
const QString macFileOpen = fileOpenEvent->file(); const QString macFileOpen = fileOpenEvent->file();
if(not macFileOpen.isEmpty()) if(not macFileOpen.isEmpty())
{ {
VPMainWindow *mw = MainWindow(); VPMainWindow *mw = MainWindow();
if (mw) if (mw != nullptr)
{ {
mw->LoadFile(macFileOpen); // open file in existing window mw->LoadFile(macFileOpen); // open file in existing window
} }
@ -639,8 +637,14 @@ void VPApplication::Clean()
} }
} }
//-------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VPCommandLinePtr VPApplication::CommandLine() const auto VPApplication::CommandLine() -> VPCommandLinePtr
{ {
return VPCommandLine::instance; return VPCommandLine::instance;
} }
//---------------------------------------------------------------------------------------------------------------------
VPApplication *VPApplication::VApp()
{
return qobject_cast<VPApplication*>(QCoreApplication::instance());
}

View File

@ -35,15 +35,9 @@
#include <memory> #include <memory>
class VPApplication;// use in define
class VPMainWindow; class VPMainWindow;
class QLocalServer; class QLocalServer;
#if defined(qApp)
#undef qApp
#endif
#define qApp (static_cast<VPApplication*>(VAbstractApplication::instance()))
enum class SocketConnection : bool {Client = false, Server = true}; enum class SocketConnection : bool {Client = false, Server = true};
class VPApplication : public VAbstractApplication class VPApplication : public VAbstractApplication
@ -70,7 +64,9 @@ public:
void ParseCommandLine(const SocketConnection &connection, const QStringList &arguments); void ParseCommandLine(const SocketConnection &connection, const QStringList &arguments);
void ProcessArguments(const VPCommandLinePtr &cmd); void ProcessArguments(const VPCommandLinePtr &cmd);
VPCommandLinePtr CommandLine() const;
static VPCommandLinePtr CommandLine();
static VPApplication *VApp();
public slots: public slots:
void ProcessCMD(); void ProcessCMD();
@ -86,8 +82,8 @@ private slots:
private: private:
Q_DISABLE_COPY(VPApplication) Q_DISABLE_COPY(VPApplication)
QList<QPointer<VPMainWindow> > mainWindows; QList<QPointer<VPMainWindow> > mainWindows{};
QLocalServer *localServer; QLocalServer *localServer{nullptr};
void Clean(); void Clean();
}; };

View File

@ -89,7 +89,7 @@ VPMainWindow::VPMainWindow(const VPCommandLinePtr &cmd, QWidget *parent) :
ui->setupUi(this); ui->setupUi(this);
// init the tile factory // init the tile factory
m_tileFactory = new VPTileFactory(m_layout, qApp->Settings()); m_tileFactory = new VPTileFactory(m_layout, VPApplication::VApp()->Settings());
m_tileFactory->refreshTileInfos(); m_tileFactory->refreshTileInfos();
InitMenuBar(); InitMenuBar();
@ -665,7 +665,7 @@ void VPMainWindow::SetCheckBoxValue(QCheckBox *checkbox, bool value)
void VPMainWindow::ReadSettings() void VPMainWindow::ReadSettings()
{ {
qCDebug(pWindow, "Reading settings."); qCDebug(pWindow, "Reading settings.");
const VPSettings *settings = qApp->PuzzleSettings(); const VPSettings *settings = VPApplication::VApp()->PuzzleSettings();
if (settings->status() == QSettings::NoError) if (settings->status() == QSettings::NoError)
{ {
@ -691,7 +691,7 @@ void VPMainWindow::ReadSettings()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPMainWindow::WriteSettings() void VPMainWindow::WriteSettings()
{ {
VPSettings *settings = qApp->PuzzleSettings(); VPSettings *settings = VPApplication::VApp()->PuzzleSettings();
settings->SetGeometry(saveGeometry()); settings->SetGeometry(saveGeometry());
settings->SetWindowState(saveState()); settings->SetWindowState(saveState());
settings->SetToolbarsState(saveState(APP_VERSION)); settings->SetToolbarsState(saveState(APP_VERSION));
@ -876,7 +876,7 @@ void VPMainWindow::on_actionOpen_triggered()
const QString filter(tr("Layout files") + QLatin1String(" (*.vlt)")); const QString filter(tr("Layout files") + QLatin1String(" (*.vlt)"));
//Get list last open files //Get list last open files
QStringList recentFiles = qApp->PuzzleSettings()->GetRecentFileList(); QStringList recentFiles = VPApplication::VApp()->PuzzleSettings()->GetRecentFileList();
QString dir; QString dir;
if (recentFiles.isEmpty()) if (recentFiles.isEmpty())
{ {
@ -914,7 +914,7 @@ void VPMainWindow::on_actionOpen_triggered()
{ {
recentFiles.removeLast(); recentFiles.removeLast();
} }
qApp->PuzzleSettings()->SetRecentFileList(recentFiles); VPApplication::VApp()->PuzzleSettings()->SetRecentFileList(recentFiles);
// updates the properties with the loaded data // updates the properties with the loaded data
SetPropertiesData(); SetPropertiesData();
@ -942,7 +942,7 @@ void VPMainWindow::on_actionSaveAs_triggered()
// extension .vlt, check for empty file names etc. // extension .vlt, check for empty file names etc.
//Get list last open files //Get list last open files
QStringList recentFiles = qApp->PuzzleSettings()->GetRecentFileList(); QStringList recentFiles = VPApplication::VApp()->PuzzleSettings()->GetRecentFileList();
QString dir; QString dir;
if (recentFiles.isEmpty()) if (recentFiles.isEmpty())
{ {
@ -1374,7 +1374,8 @@ void VPMainWindow::on_pushButtonSheetExport_clicked()
QPainter painter; QPainter painter;
painter.begin(&generator); painter.begin(&generator);
painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, qApp->Settings()->WidthHairLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter.setPen(QPen(Qt::black, VPApplication::VApp()->Settings()->WidthHairLine(), Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) ); painter.setBrush ( QBrush ( Qt::NoBrush ) );
m_graphicsView->GetScene()->render(&painter, r, r, Qt::IgnoreAspectRatio); m_graphicsView->GetScene()->render(&painter, r, r, Qt::IgnoreAspectRatio);
painter.end(); painter.end();

View File

@ -35,7 +35,6 @@
#include "../vmisc/vabstractapplication.h" #include "../vmisc/vabstractapplication.h"
#include "dialogs/dialogmdatabase.h" #include "dialogs/dialogmdatabase.h"
class MApplication;// use in define
class TMainWindow; class TMainWindow;
class QLocalServer; class QLocalServer;