2020-02-16 18:18:39 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
2020-05-23 14:10:05 +02:00
|
|
|
** @file vpapplication.cpp
|
2020-02-16 18:18:39 +01:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 16 2, 2020
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentina project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2020 Valentina project
|
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
2020-05-23 14:10:05 +02:00
|
|
|
#include "vpapplication.h"
|
2020-02-16 18:18:39 +01:00
|
|
|
#include "version.h"
|
2020-05-23 14:17:20 +02:00
|
|
|
#include "vpmainwindow.h"
|
2020-02-16 18:18:39 +01:00
|
|
|
#include "../ifc/exception/vexceptionobjecterror.h"
|
|
|
|
#include "../ifc/exception/vexceptionbadid.h"
|
|
|
|
#include "../ifc/exception/vexceptionconversionerror.h"
|
|
|
|
#include "../ifc/exception/vexceptionemptyparameter.h"
|
|
|
|
#include "../ifc/exception/vexceptionwrongid.h"
|
|
|
|
#include "../vmisc/vsysexits.h"
|
|
|
|
#include "../vmisc/diagnostic.h"
|
|
|
|
#include "../vmisc/qt_dispatch/qt_dispatch.h"
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
2020-12-23 17:40:33 +01:00
|
|
|
#include <QLoggingCategory>
|
2020-02-16 18:18:39 +01:00
|
|
|
|
|
|
|
QT_WARNING_PUSH
|
|
|
|
QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
|
|
|
|
QT_WARNING_DISABLE_INTEL(1418)
|
|
|
|
|
2020-12-23 17:40:33 +01:00
|
|
|
Q_LOGGING_CATEGORY(pApp, "p.application")
|
2020-02-16 18:18:39 +01:00
|
|
|
|
|
|
|
QT_WARNING_POP
|
|
|
|
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QLocalSocket>
|
|
|
|
#include <QLocalServer>
|
2020-02-16 22:07:44 +01:00
|
|
|
#include <QFileOpenEvent>
|
2020-02-16 18:18:39 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void noisyFailureMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
|
|
|
{
|
|
|
|
// only the GUI thread should display message boxes. If you are
|
|
|
|
// writing a multithreaded application and the error happens on
|
|
|
|
// a non-GUI thread, you'll have to queue the message to the GUI
|
|
|
|
QCoreApplication *instance = QCoreApplication::instance();
|
2021-05-20 16:10:43 +02:00
|
|
|
const bool isGuiThread = (instance != nullptr) && (QThread::currentThread() == instance->thread());
|
2020-02-16 18:18:39 +01:00
|
|
|
|
|
|
|
if (not isGuiThread)
|
|
|
|
{
|
|
|
|
auto Handler = [](QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
|
|
|
{
|
|
|
|
noisyFailureMsgHandler(type, context, msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
q_dispatch_async_main(Handler, type, context, msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Why on earth didn't Qt want to make failed signal/slot connections qWarning?
|
|
|
|
if ((type == QtDebugMsg) && msg.contains(QStringLiteral("::connect")))
|
|
|
|
{
|
|
|
|
type = QtWarningMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(V_NO_ASSERT)
|
|
|
|
// I have decided to hide this annoing message for release builds.
|
|
|
|
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("QSslSocket: cannot resolve")))
|
|
|
|
{
|
|
|
|
type = QtDebugMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("setGeometry: Unable to set geometry")))
|
|
|
|
{
|
|
|
|
type = QtDebugMsg;
|
|
|
|
}
|
|
|
|
#endif //defined(V_NO_ASSERT)
|
|
|
|
|
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
# if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) && QT_VERSION < QT_VERSION_CHECK(5, 7, 0)
|
|
|
|
// Try hide very annoying, Qt related, warnings in Mac OS X
|
|
|
|
// QNSView mouseDragged: Internal mouse button tracking invalid (missing Qt::LeftButton)
|
|
|
|
// https://bugreports.qt.io/browse/QTBUG-42846
|
|
|
|
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("QNSView")))
|
|
|
|
{
|
|
|
|
type = QtDebugMsg;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2021-06-04 07:50:37 +02:00
|
|
|
# if QT_VERSION < QT_VERSION_CHECK(5, 9, 0)
|
2020-02-16 18:18:39 +01:00
|
|
|
// Hide Qt bug 'Assertion when reading an icns file'
|
|
|
|
// https://bugreports.qt.io/browse/QTBUG-45537
|
|
|
|
// Remove after Qt fix will be released
|
|
|
|
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("QICNSHandler::read()")))
|
|
|
|
{
|
|
|
|
type = QtDebugMsg;
|
|
|
|
}
|
2021-06-04 07:50:37 +02:00
|
|
|
# endif
|
|
|
|
|
|
|
|
// Hide anything that starts with QMacCGContext
|
|
|
|
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("QMacCGContext::")))
|
|
|
|
{
|
|
|
|
type = QtDebugMsg;
|
|
|
|
}
|
2020-02-16 18:18:39 +01:00
|
|
|
|
|
|
|
// See issue #568
|
|
|
|
if (msg.contains(QStringLiteral("Error receiving trust for a CA certificate")))
|
|
|
|
{
|
|
|
|
type = QtDebugMsg;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// this is another one that doesn't make sense as just a debug message. pretty serious
|
|
|
|
// sign of a problem
|
|
|
|
// http://www.developer.nokia.com/Community/Wiki/QPainter::begin:Paint_device_returned_engine_%3D%3D_0_(Known_Issue)
|
|
|
|
if ((type == QtDebugMsg) && msg.contains(QStringLiteral("QPainter::begin"))
|
|
|
|
&& msg.contains(QStringLiteral("Paint device returned engine")))
|
|
|
|
{
|
|
|
|
type = QtWarningMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This qWarning about "Cowardly refusing to send clipboard message to hung application..."
|
|
|
|
// is something that can easily happen if you are debugging and the application is paused.
|
|
|
|
// As it is so common, not worth popping up a dialog.
|
|
|
|
if ((type == QtWarningMsg) && msg.contains(QStringLiteral("QClipboard::event"))
|
|
|
|
&& msg.contains(QStringLiteral("Cowardly refusing")))
|
|
|
|
{
|
|
|
|
type = QtDebugMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case QtDebugMsg:
|
|
|
|
vStdOut() << QApplication::translate("mNoisyHandler", "DEBUG:") << msg << "\n";
|
|
|
|
return;
|
|
|
|
case QtWarningMsg:
|
|
|
|
vStdErr() << QApplication::translate("mNoisyHandler", "WARNING:") << msg << "\n";
|
|
|
|
break;
|
|
|
|
case QtCriticalMsg:
|
|
|
|
vStdErr() << QApplication::translate("mNoisyHandler", "CRITICAL:") << msg << "\n";
|
|
|
|
break;
|
|
|
|
case QtFatalMsg:
|
|
|
|
vStdErr() << QApplication::translate("mNoisyHandler", "FATAL:") << msg << "\n";
|
|
|
|
break;
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
|
|
|
|
case QtInfoMsg:
|
|
|
|
vStdOut() << QApplication::translate("mNoisyHandler", "INFO:") << msg << "\n";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
vStdOut().flush();
|
|
|
|
vStdErr().flush();
|
|
|
|
|
|
|
|
if (isGuiThread)
|
|
|
|
{
|
|
|
|
//fixme: trying to make sure there are no save/load dialogs are opened, because error message during them will
|
|
|
|
//lead to crash
|
|
|
|
const bool topWinAllowsPop = (QApplication::activeModalWidget() == nullptr) ||
|
|
|
|
!QApplication::activeModalWidget()->inherits("QFileDialog");
|
|
|
|
QMessageBox messageBox;
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case QtWarningMsg:
|
|
|
|
messageBox.setWindowTitle(QApplication::translate("mNoisyHandler", "Warning"));
|
|
|
|
messageBox.setIcon(QMessageBox::Warning);
|
|
|
|
break;
|
|
|
|
case QtCriticalMsg:
|
|
|
|
messageBox.setWindowTitle(QApplication::translate("mNoisyHandler", "Critical error"));
|
|
|
|
messageBox.setIcon(QMessageBox::Critical);
|
|
|
|
break;
|
|
|
|
case QtFatalMsg:
|
|
|
|
messageBox.setWindowTitle(QApplication::translate("mNoisyHandler", "Fatal error"));
|
|
|
|
messageBox.setIcon(QMessageBox::Critical);
|
|
|
|
break;
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
|
|
|
|
case QtInfoMsg:
|
|
|
|
messageBox.setWindowTitle(QApplication::translate("mNoisyHandler", "Information"));
|
|
|
|
messageBox.setIcon(QMessageBox::Information);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case QtDebugMsg:
|
|
|
|
Q_UNREACHABLE(); //-V501
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == QtWarningMsg || type == QtCriticalMsg || type == QtFatalMsg)
|
|
|
|
{
|
2021-05-20 16:10:43 +02:00
|
|
|
if (VPApplication::VApp()->IsAppInGUIMode())
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
if (topWinAllowsPop)
|
|
|
|
{
|
2020-11-10 15:00:01 +01:00
|
|
|
messageBox.setText(msg);
|
2020-02-16 18:18:39 +01:00
|
|
|
messageBox.setStandardButtons(QMessageBox::Ok);
|
|
|
|
messageBox.setWindowModality(Qt::ApplicationModal);
|
|
|
|
messageBox.setModal(true);
|
|
|
|
#ifndef QT_NO_CURSOR
|
|
|
|
QGuiApplication::setOverrideCursor(Qt::ArrowCursor);
|
|
|
|
#endif
|
|
|
|
messageBox.exec();
|
|
|
|
#ifndef QT_NO_CURSOR
|
|
|
|
QGuiApplication::restoreOverrideCursor();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (QtFatalMsg == type)
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (type != QtDebugMsg)
|
|
|
|
{
|
|
|
|
abort(); // be NOISY unless overridden!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
VPApplication::VPApplication(int &argc, char **argv)
|
2021-05-20 16:10:43 +02:00
|
|
|
:VAbstractApplication(argc, argv)
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
setApplicationDisplayName(VER_PRODUCTNAME_STR);
|
|
|
|
setApplicationName(VER_INTERNALNAME_STR);
|
|
|
|
setOrganizationName(VER_COMPANYNAME_STR);
|
|
|
|
setOrganizationDomain(VER_COMPANYDOMAIN_STR);
|
|
|
|
// Setting the Application version
|
|
|
|
setApplicationVersion(APP_VERSION_STR);
|
|
|
|
// We have been running Puzzle in two different cases.
|
|
|
|
// The first inside own bundle where info.plist is works fine, but the second,
|
|
|
|
// when we run inside Valentina's bundle, require direct setting the icon.
|
|
|
|
setWindowIcon(QIcon(":/puzzleicon/64x64/logo.png"));
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
VPApplication::~VPApplication()
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
qDeleteAll(mainWindows);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief notify Reimplemented from QApplication::notify().
|
|
|
|
* @param receiver receiver.
|
|
|
|
* @param event event.
|
|
|
|
* @return value that is returned from the receiver's event handler.
|
|
|
|
*/
|
|
|
|
// reimplemented from QApplication so we can throw exceptions in slots
|
2021-05-20 16:10:43 +02:00
|
|
|
auto VPApplication::notify(QObject *receiver, QEvent *event) -> bool
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return QApplication::notify(receiver, event);
|
|
|
|
}
|
|
|
|
catch (const VExceptionObjectError &e)
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Error parsing file. Program will be terminated.")), //-V807
|
2020-02-16 18:18:39 +01:00
|
|
|
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
|
|
|
|
exit(V_EX_DATAERR);
|
|
|
|
}
|
|
|
|
catch (const VExceptionBadId &e)
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Error bad id. Program will be terminated.")),
|
2020-02-16 18:18:39 +01:00
|
|
|
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
|
|
|
|
exit(V_EX_DATAERR);
|
|
|
|
}
|
|
|
|
catch (const VExceptionConversionError &e)
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Error can't convert value. Program will be terminated.")),
|
2020-02-16 18:18:39 +01:00
|
|
|
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
|
|
|
|
exit(V_EX_DATAERR);
|
|
|
|
}
|
|
|
|
catch (const VExceptionEmptyParameter &e)
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Error empty parameter. Program will be terminated.")),
|
2020-02-16 18:18:39 +01:00
|
|
|
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
|
|
|
|
exit(V_EX_DATAERR);
|
|
|
|
}
|
|
|
|
catch (const VExceptionWrongId &e)
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Error wrong id. Program will be terminated.")),
|
2020-02-16 18:18:39 +01:00
|
|
|
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
|
|
|
|
exit(V_EX_DATAERR);
|
|
|
|
}
|
|
|
|
catch (const VExceptionToolWasDeleted &e)
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n\n%s\n\n%s",
|
2020-02-16 18:18:39 +01:00
|
|
|
qUtf8Printable("Unhadled deleting tool. Continue use object after deleting!"),
|
|
|
|
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
|
|
|
|
exit(V_EX_DATAERR);
|
|
|
|
}
|
|
|
|
catch (const VException &e)
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n\n%s\n\n%s", qUtf8Printable(tr("Something's wrong!!")),
|
2020-02-16 18:18:39 +01:00
|
|
|
qUtf8Printable(e.ErrorMessage()), qUtf8Printable(e.DetailedInformation()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s", qUtf8Printable(tr("Exception thrown: %1. Program will be terminated.").arg(e.what())));
|
2020-02-16 18:18:39 +01:00
|
|
|
exit(V_EX_SOFTWARE);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief IsAppInGUIMode little hack that allow to have access to application state from VAbstractApplication class.
|
|
|
|
*/
|
2021-05-20 16:10:43 +02:00
|
|
|
auto VPApplication::IsAppInGUIMode() const -> bool
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2020-04-14 10:36:21 +02:00
|
|
|
return CommandLine()->IsGuiEnabled();
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-05-20 16:10:43 +02:00
|
|
|
auto VPApplication::MainWindow()-> VPMainWindow *
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
Clean();
|
|
|
|
if (mainWindows.isEmpty())
|
|
|
|
{
|
2021-05-21 10:25:43 +02:00
|
|
|
NewMainWindow();
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
|
|
|
return mainWindows[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-05-21 19:51:46 +02:00
|
|
|
auto VPApplication::MainWindows() -> QList<VPMainWindow *>
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
Clean();
|
2020-05-23 14:17:20 +02:00
|
|
|
QList<VPMainWindow*> list;
|
2020-02-16 18:18:39 +01:00
|
|
|
for (auto &w : mainWindows)
|
|
|
|
{
|
|
|
|
list.append(w);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-05-20 17:33:53 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-05-21 10:25:43 +02:00
|
|
|
auto VPApplication::NewMainWindow() -> VPMainWindow *
|
2021-05-20 17:33:53 +02:00
|
|
|
{
|
2021-05-21 10:25:43 +02:00
|
|
|
VPCommandLinePtr cmd;
|
|
|
|
VPCommandLine::ProcessInstance(cmd, {ConstFirst<QString>(VPApplication::arguments())});
|
|
|
|
return NewMainWindow(cmd);
|
2021-05-20 17:33:53 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 18:18:39 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-05-20 16:10:43 +02:00
|
|
|
auto VPApplication::NewMainWindow(const VPCommandLinePtr &cmd) -> VPMainWindow *
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2021-05-21 10:25:43 +02:00
|
|
|
auto *puzzle = new VPMainWindow(cmd);
|
2020-02-16 18:18:39 +01:00
|
|
|
mainWindows.prepend(puzzle);
|
2021-05-20 17:33:53 +02:00
|
|
|
if (not cmd->IsTestModeEnabled())
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
puzzle->show();
|
2021-05-21 19:51:46 +02:00
|
|
|
puzzle->UpdateWindowTitle();
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
2020-11-13 23:31:22 +01:00
|
|
|
puzzle->InitZoom();
|
2020-02-16 18:18:39 +01:00
|
|
|
return puzzle;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
void VPApplication::InitOptions()
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
qInstallMessageHandler(noisyFailureMsgHandler);
|
|
|
|
|
|
|
|
OpenSettings();
|
|
|
|
|
2020-12-23 17:40:33 +01:00
|
|
|
qCDebug(pApp, "Version: %s", qUtf8Printable(APP_VERSION_STR));
|
|
|
|
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, "Process ID: %s", qUtf8Printable(QString().setNum(applicationPid())));
|
2020-02-16 18:18:39 +01:00
|
|
|
|
|
|
|
LoadTranslation(QLocale().name());// By default the console version uses system locale
|
|
|
|
|
2020-05-23 14:43:57 +02:00
|
|
|
VPCommandLine::Instance(*this);
|
2020-04-12 22:28:36 +02:00
|
|
|
|
2020-02-16 18:18:39 +01:00
|
|
|
static const char * GENERIC_ICON_TO_CHECK = "document-open";
|
|
|
|
if (QIcon::hasThemeIcon(GENERIC_ICON_TO_CHECK) == false)
|
|
|
|
{
|
|
|
|
//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");
|
|
|
|
}
|
|
|
|
ActivateDarkMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-05-20 16:10:43 +02:00
|
|
|
auto VPApplication::TrVars() -> const VTranslateVars *
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
void VPApplication::OpenSettings()
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2020-05-23 15:44:44 +02:00
|
|
|
settings = new VPSettings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(),
|
2020-02-16 18:18:39 +01:00
|
|
|
QCoreApplication::applicationName(), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-05-20 16:10:43 +02:00
|
|
|
auto VPApplication::PuzzleSettings() -> VPSettings *
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
SCASSERT(settings != nullptr)
|
2020-05-23 15:44:44 +02:00
|
|
|
return qobject_cast<VPSettings *>(settings);
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
void VPApplication::ActivateDarkMode()
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2021-05-20 16:10:43 +02:00
|
|
|
VPSettings *settings = VPApplication::VApp()->PuzzleSettings();
|
2020-02-16 18:18:39 +01:00
|
|
|
if (settings->GetDarkMode())
|
|
|
|
{
|
|
|
|
QFile f(":qdarkstyle/style.qss");
|
|
|
|
if (!f.exists())
|
|
|
|
{
|
|
|
|
qDebug()<<"Unable to set stylesheet, file not found\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
f.open(QFile::ReadOnly | QFile::Text);
|
|
|
|
QTextStream ts(&f);
|
2021-07-29 16:11:18 +02:00
|
|
|
VPApplication::VApp()->setStyleSheet(ts.readAll());
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
void VPApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments)
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2020-05-23 14:43:57 +02:00
|
|
|
VPCommandLinePtr cmd;
|
|
|
|
VPCommandLine::ProcessInstance(cmd, arguments);
|
2020-02-16 18:18:39 +01:00
|
|
|
|
2020-04-14 10:36:21 +02:00
|
|
|
if (cmd->IsGuiEnabled() && connection == SocketConnection::Client)
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
const QString serverName = QCoreApplication::applicationName();
|
|
|
|
QLocalSocket socket;
|
|
|
|
socket.connectToServer(serverName);
|
|
|
|
if (socket.waitForConnected(1000))
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCDebug(pApp, "Connected to the server '%s'", qUtf8Printable(serverName));
|
2020-02-16 18:18:39 +01:00
|
|
|
QTextStream stream(&socket);
|
2020-04-14 10:36:21 +02:00
|
|
|
stream << arguments.join(";;");
|
2020-02-16 18:18:39 +01:00
|
|
|
stream.flush();
|
|
|
|
socket.waitForBytesWritten();
|
2021-07-29 16:11:18 +02:00
|
|
|
QCoreApplication::exit(V_EX_OK);
|
2020-02-16 18:18:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-23 17:40:33 +01:00
|
|
|
qCDebug(pApp, "Can't establish connection to the server '%s'", qUtf8Printable(serverName));
|
2020-02-16 18:18:39 +01:00
|
|
|
|
|
|
|
localServer = new QLocalServer(this);
|
2020-05-23 14:10:05 +02:00
|
|
|
connect(localServer, &QLocalServer::newConnection, this, &VPApplication::NewLocalSocketConnection);
|
2020-02-16 18:18:39 +01:00
|
|
|
if (not localServer->listen(serverName))
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCDebug(pApp, "Can't begin to listen for incoming connections on name '%s'",
|
2020-02-16 18:18:39 +01:00
|
|
|
qUtf8Printable(serverName));
|
|
|
|
if (localServer->serverError() == QAbstractSocket::AddressInUseError)
|
|
|
|
{
|
|
|
|
QLocalServer::removeServer(serverName);
|
|
|
|
if (not localServer->listen(serverName))
|
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCWarning(pApp, "%s",
|
2020-02-16 18:18:39 +01:00
|
|
|
qUtf8Printable(tr("Can't begin to listen for incoming connections on name '%1'").arg(serverName)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadTranslation(PuzzleSettings()->GetLocale());
|
|
|
|
}
|
|
|
|
|
2020-04-14 10:36:21 +02:00
|
|
|
ProcessArguments(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:43:57 +02:00
|
|
|
void VPApplication::ProcessArguments(const VPCommandLinePtr &cmd)
|
2020-04-14 10:36:21 +02:00
|
|
|
{
|
|
|
|
const QStringList rawLayouts = cmd->OptionRawLayouts();
|
2020-04-12 22:28:36 +02:00
|
|
|
const QStringList args = cmd->OptionFileNames();
|
2020-02-16 18:18:39 +01:00
|
|
|
if (args.count() > 0)
|
|
|
|
{
|
2020-04-14 10:36:21 +02:00
|
|
|
if (not cmd->IsGuiEnabled() && args.count() > 1)
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n", qPrintable(tr("Export mode doesn't support openning several files.")));
|
2020-04-14 10:36:21 +02:00
|
|
|
cmd.get()->parser.showHelp(V_EX_USAGE);
|
|
|
|
}
|
|
|
|
|
2021-05-20 16:10:43 +02:00
|
|
|
if (args.count() > 1 && not rawLayouts.isEmpty())
|
2020-04-14 10:36:21 +02:00
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n",
|
2021-05-20 16:10:43 +02:00
|
|
|
qPrintable(tr("Import raw layout data does not support openning several layout files.")));
|
2020-04-12 22:28:36 +02:00
|
|
|
cmd.get()->parser.showHelp(V_EX_USAGE);
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 16:10:43 +02:00
|
|
|
for (const auto &arg : args)
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2020-04-21 18:03:36 +02:00
|
|
|
NewMainWindow(cmd);
|
2020-02-16 18:18:39 +01:00
|
|
|
if (not MainWindow()->LoadFile(arg))
|
|
|
|
{
|
2020-04-14 10:36:21 +02:00
|
|
|
if (not cmd->IsGuiEnabled())
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
return; // process only one input file
|
|
|
|
}
|
|
|
|
delete MainWindow();
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-14 10:36:21 +02:00
|
|
|
|
2021-05-20 16:10:43 +02:00
|
|
|
if (not rawLayouts.isEmpty())
|
2020-04-21 18:03:36 +02:00
|
|
|
{
|
|
|
|
MainWindow()->ImportRawLayouts(rawLayouts);
|
|
|
|
}
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-14 10:36:21 +02:00
|
|
|
if (cmd->IsTestModeEnabled())
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2020-12-23 17:40:33 +01:00
|
|
|
qCCritical(pApp, "%s\n", qPrintable(tr("Please, provide one input file.")));
|
2020-04-12 22:28:36 +02:00
|
|
|
cmd.get()->parser.showHelp(V_EX_USAGE);
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
2020-04-14 10:36:21 +02:00
|
|
|
|
2020-04-21 18:03:36 +02:00
|
|
|
NewMainWindow(cmd);
|
2021-05-20 16:10:43 +02:00
|
|
|
if (not rawLayouts.isEmpty())
|
2020-04-21 18:03:36 +02:00
|
|
|
{
|
|
|
|
MainWindow()->ImportRawLayouts(rawLayouts);
|
|
|
|
}
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
|
|
|
|
2020-04-18 09:53:32 +02:00
|
|
|
if (not cmd->IsGuiEnabled())
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2021-07-29 16:11:18 +02:00
|
|
|
QCoreApplication::exit(V_EX_OK); // close program after processing in console mode
|
2020-02-16 18:18:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
void VPApplication::ProcessCMD()
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
ParseCommandLine(SocketConnection::Client, arguments());
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2021-05-20 16:10:43 +02:00
|
|
|
auto VPApplication::event(QEvent *e) -> bool
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
switch(e->type())
|
|
|
|
{
|
|
|
|
// In Mac OS X the QFileOpenEvent event is generated when user perform "Open With" from Finder (this event is
|
|
|
|
// Mac specific).
|
|
|
|
case QEvent::FileOpen:
|
|
|
|
{
|
2021-05-20 16:10:43 +02:00
|
|
|
auto *fileOpenEvent = static_cast<QFileOpenEvent *>(e);
|
2020-02-16 18:18:39 +01:00
|
|
|
const QString macFileOpen = fileOpenEvent->file();
|
|
|
|
if(not macFileOpen.isEmpty())
|
|
|
|
{
|
2020-05-23 14:17:20 +02:00
|
|
|
VPMainWindow *mw = MainWindow();
|
2021-05-20 16:10:43 +02:00
|
|
|
if (mw != nullptr)
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
mw->LoadFile(macFileOpen); // open file in existing window
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if defined(Q_OS_MAC)
|
|
|
|
case QEvent::ApplicationActivate:
|
|
|
|
{
|
|
|
|
Clean();
|
2020-05-23 14:17:20 +02:00
|
|
|
VPMainWindow *mw = MainWindow();
|
2020-02-16 18:18:39 +01:00
|
|
|
if (mw && not mw->isMinimized())
|
|
|
|
{
|
|
|
|
mw->show();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif //defined(Q_OS_MAC)
|
|
|
|
default:
|
|
|
|
return VAbstractApplication::event(e);
|
|
|
|
}
|
|
|
|
return VAbstractApplication::event(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
void VPApplication::InitTrVars()
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
void VPApplication::AboutToQuit()
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
// If try to use the method QApplication::exit program can't sync settings and show warning about QApplication
|
|
|
|
// instance. Solution is to call sync() before quit.
|
|
|
|
// Connect this slot with VApplication::aboutToQuit.
|
|
|
|
Settings()->sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
void VPApplication::NewLocalSocketConnection()
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
2020-04-14 10:36:21 +02:00
|
|
|
QScopedPointer<QLocalSocket>socket(localServer->nextPendingConnection());
|
|
|
|
if (socket.isNull())
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
socket->waitForReadyRead(1000);
|
2020-04-14 10:36:21 +02:00
|
|
|
QTextStream stream(socket.data());
|
2020-02-16 18:18:39 +01:00
|
|
|
const QString arg = stream.readAll();
|
|
|
|
if (not arg.isEmpty())
|
|
|
|
{
|
|
|
|
ParseCommandLine(SocketConnection::Server, arg.split(";;"));
|
|
|
|
}
|
|
|
|
MainWindow()->raise();
|
|
|
|
MainWindow()->activateWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2020-05-23 14:10:05 +02:00
|
|
|
void VPApplication::Clean()
|
2020-02-16 18:18:39 +01:00
|
|
|
{
|
|
|
|
// cleanup any deleted main windows first
|
|
|
|
for (int i = mainWindows.count() - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
if (mainWindows.at(i).isNull())
|
|
|
|
{
|
|
|
|
mainWindows.removeAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 22:28:36 +02:00
|
|
|
|
2021-05-20 16:10:43 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
auto VPApplication::CommandLine() -> VPCommandLinePtr
|
2020-04-12 22:28:36 +02:00
|
|
|
{
|
2020-05-23 14:43:57 +02:00
|
|
|
return VPCommandLine::instance;
|
2020-04-12 22:28:36 +02:00
|
|
|
}
|
2021-05-20 16:10:43 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VPApplication *VPApplication::VApp()
|
|
|
|
{
|
|
|
|
return qobject_cast<VPApplication*>(QCoreApplication::instance());
|
|
|
|
}
|