2015-06-18 10:45:38 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vabstractapplication.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 18 6, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2015-06-18 10:45:38 +02:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2015 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2015-06-18 10:45:38 +02:00
|
|
|
**
|
|
|
|
** 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/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vabstractapplication.h"
|
|
|
|
|
2015-11-18 13:42:25 +01:00
|
|
|
#include <QDir>
|
2015-09-19 21:58:32 +02:00
|
|
|
#include <QLibraryInfo>
|
2019-04-05 14:42:22 +02:00
|
|
|
#include <QLoggingCategory>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QMessageLogger>
|
|
|
|
#include <QStaticStringData>
|
|
|
|
#include <QStringData>
|
|
|
|
#include <QStringDataPtr>
|
2015-10-18 21:30:51 +02:00
|
|
|
#include <QTranslator>
|
2018-07-09 09:45:23 +02:00
|
|
|
#include <QUndoStack>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <Qt>
|
2015-10-18 21:30:51 +02:00
|
|
|
#include <QtDebug>
|
2019-10-28 16:44:31 +01:00
|
|
|
#include <QWidget>
|
2019-11-26 18:56:40 +01:00
|
|
|
#include <QStandardPaths>
|
2015-09-19 21:58:32 +02:00
|
|
|
|
2019-11-26 18:56:40 +01:00
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2020-07-13 15:11:57 +02:00
|
|
|
#if defined(APPIMAGE) && defined(Q_OS_LINUX)
|
|
|
|
# include "appimage.h"
|
|
|
|
#endif // defined(APPIMAGE) && defined(Q_OS_LINUX)
|
|
|
|
|
2020-11-20 12:18:38 +01:00
|
|
|
const QString VAbstractApplication::warningMessageSignature = QStringLiteral("[PATTERN MESSAGE]");
|
|
|
|
|
2015-06-18 10:45:38 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VAbstractApplication::VAbstractApplication(int &argc, char **argv)
|
2015-06-18 19:23:24 +02:00
|
|
|
:QApplication(argc, argv),
|
2020-10-15 17:05:21 +02:00
|
|
|
undoStack(new QUndoStack(this))
|
2015-10-03 16:03:25 +02:00
|
|
|
{
|
2016-09-20 13:10:42 +02:00
|
|
|
QString rules;
|
2016-09-20 13:27:21 +02:00
|
|
|
|
2016-08-02 12:46:46 +02:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 1)
|
2016-08-02 12:43:41 +02:00
|
|
|
#if defined(V_NO_ASSERT)
|
2016-07-20 10:48:09 +02:00
|
|
|
// Ignore SSL-related warnings
|
|
|
|
// See issue #528: Error: QSslSocket: cannot resolve SSLv2_client_method.
|
2016-09-20 13:10:42 +02:00
|
|
|
rules += QLatin1String("qt.network.ssl.warning=false\n");
|
|
|
|
// See issue #568: Certificate checking on Mac OS X.
|
2016-09-20 16:28:39 +02:00
|
|
|
rules += QLatin1String("qt.network.ssl.critical=false\n"
|
|
|
|
"qt.network.ssl.fatal=false\n");
|
2016-08-02 12:43:41 +02:00
|
|
|
#endif //defined(V_NO_ASSERT)
|
2016-08-02 12:46:46 +02:00
|
|
|
#endif // QT_VERSION >= QT_VERSION_CHECK(5, 4, 1)
|
2016-09-20 13:10:42 +02:00
|
|
|
|
2020-01-20 09:14:44 +01:00
|
|
|
#if defined(V_NO_ASSERT)
|
|
|
|
// See issue #992: QXcbConnection: XCB Error.
|
|
|
|
rules += QLatin1String("qt.qpa.*=false\n");
|
|
|
|
#endif
|
|
|
|
|
2017-07-06 11:58:26 +02:00
|
|
|
// cppcheck-suppress reademptycontainer
|
2016-09-20 13:10:42 +02:00
|
|
|
if (not rules.isEmpty())
|
|
|
|
{
|
|
|
|
QLoggingCategory::setFilterRules(rules);
|
|
|
|
}
|
2016-09-20 13:27:21 +02:00
|
|
|
|
2016-11-04 11:44:57 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
|
2016-09-20 13:27:21 +02:00
|
|
|
// Enable support for HiDPI bitmap resources
|
2016-11-04 11:44:57 +01:00
|
|
|
// The attribute is available since Qt 5.1, but by default disabled.
|
|
|
|
// Because on Windows and Mac OS X we always use last version
|
|
|
|
// and Linux users send bug reports probably they related to this attribute
|
|
|
|
// better not enable it before Qt 5.6.
|
|
|
|
//
|
|
|
|
// Related issues:
|
|
|
|
// Issue #584. frequent xcb errors and hangs
|
|
|
|
// https://bitbucket.org/dismine/valentina/issues/584/frequent-xcb-errors-and-hangs
|
|
|
|
// Issue #527. Error: Pasting a wrong formula : every dialog box is "glued" to the screen and can't close file
|
|
|
|
// or Valentina.
|
|
|
|
// https://bitbucket.org/dismine/valentina/issues/527/error-pasting-a-wrong-formula-every-dialog
|
|
|
|
|
2016-09-20 13:27:21 +02:00
|
|
|
setAttribute(Qt::AA_UseHighDpiPixmaps);
|
|
|
|
#endif
|
|
|
|
|
2019-07-01 11:47:06 +02:00
|
|
|
connect(this, &QApplication::aboutToQuit, this, &VAbstractApplication::AboutToQuit);
|
2015-10-03 16:03:25 +02:00
|
|
|
}
|
2015-06-18 10:45:38 +02:00
|
|
|
|
2015-10-22 11:28:17 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-11-18 13:42:25 +01:00
|
|
|
/**
|
|
|
|
* @brief translationsPath return path to the root directory that contain QM files.
|
|
|
|
* @param locale used only in Mac OS. If empty return path to the root directory. If not - return path to locale
|
|
|
|
* subdirectory inside an app bundle.
|
|
|
|
* @return path to a directory that contain QM files.
|
|
|
|
*/
|
|
|
|
QString VAbstractApplication::translationsPath(const QString &locale) const
|
|
|
|
{
|
|
|
|
const QString trPath = QStringLiteral("/translations");
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
Q_UNUSED(locale)
|
2017-07-09 08:02:26 +02:00
|
|
|
return QCoreApplication::applicationDirPath() + trPath;
|
2015-11-18 13:42:25 +01:00
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
QString mainPath;
|
|
|
|
if (locale.isEmpty())
|
|
|
|
{
|
2017-07-09 08:02:26 +02:00
|
|
|
mainPath = QCoreApplication::applicationDirPath() + QLatin1String("/../Resources") + trPath;
|
2015-11-18 13:42:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-09 08:02:26 +02:00
|
|
|
mainPath = QCoreApplication::applicationDirPath() + QLatin1String("/../Resources") + trPath + QLatin1String("/")
|
2016-07-20 10:39:15 +02:00
|
|
|
+ locale + QLatin1String(".lproj");
|
2015-11-18 13:42:25 +01:00
|
|
|
}
|
|
|
|
QDir dirBundle(mainPath);
|
|
|
|
if (dirBundle.exists())
|
|
|
|
{
|
|
|
|
return dirBundle.absolutePath();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-25 16:54:51 +02:00
|
|
|
QDir appDir = QDir(qApp->applicationDirPath());
|
|
|
|
appDir.cdUp();
|
|
|
|
appDir.cdUp();
|
|
|
|
appDir.cdUp();
|
|
|
|
QDir dir(appDir.absolutePath() + trPath);
|
2015-11-18 13:42:25 +01:00
|
|
|
if (dir.exists())
|
|
|
|
{
|
|
|
|
return dir.absolutePath();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-11 09:52:53 +02:00
|
|
|
return PKGDATADIR + trPath;
|
2015-11-18 13:42:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else // Unix
|
|
|
|
Q_UNUSED(locale)
|
2017-07-09 08:02:26 +02:00
|
|
|
QDir dir(QCoreApplication::applicationDirPath() + trPath);
|
2015-11-18 13:42:25 +01:00
|
|
|
if (dir.exists())
|
|
|
|
{
|
|
|
|
return dir.absolutePath();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-13 15:11:57 +02:00
|
|
|
#if defined(APPIMAGE) && defined(Q_OS_LINUX)
|
|
|
|
/* Fix path to trasnaltions when run inside AppImage. */
|
|
|
|
return AppImageRoot() + PKGDATADIR + trPath;
|
|
|
|
#else
|
2020-07-11 09:52:53 +02:00
|
|
|
return PKGDATADIR + trPath;
|
2020-07-13 15:11:57 +02:00
|
|
|
#endif // defined(APPIMAGE) && defined(Q_OS_LINUX)
|
2015-11-18 13:42:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-22 11:28:17 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QUndoStack *VAbstractApplication::getUndoStack() const
|
|
|
|
{
|
|
|
|
return undoStack;
|
|
|
|
}
|
2018-07-13 15:18:04 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VAbstractApplication::IsPedantic() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-22 11:28:17 +02:00
|
|
|
|
2017-12-10 12:17:04 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
void VAbstractApplication::WinAttachConsole()
|
|
|
|
{
|
|
|
|
/* Windows does not really support dual mode applications.
|
|
|
|
* To see console output we need to attach console.
|
|
|
|
* For case of using pipeline we check std output handler.
|
|
|
|
* Original idea: https://stackoverflow.com/a/41701133/3045403
|
|
|
|
*/
|
|
|
|
auto stdout_type = GetFileType(GetStdHandle(STD_OUTPUT_HANDLE));
|
|
|
|
if (stdout_type == FILE_TYPE_UNKNOWN && AttachConsole(ATTACH_PARENT_PROCESS))
|
|
|
|
{
|
2017-12-10 12:29:17 +01:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2017-12-10 12:17:04 +01:00
|
|
|
freopen("CONOUT$", "w", stdout);
|
2017-12-10 12:29:17 +01:00
|
|
|
// cppcheck-suppress ignoredReturnValue
|
2017-12-10 12:17:04 +01:00
|
|
|
freopen("CONOUT$", "w", stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-18 10:45:38 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief getSettings hide settings constructor.
|
|
|
|
* @return pointer to class for acssesing to settings in ini file.
|
|
|
|
*/
|
2015-07-24 14:06:53 +02:00
|
|
|
VCommonSettings *VAbstractApplication::Settings()
|
2015-06-18 10:45:38 +02:00
|
|
|
{
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(settings != nullptr)
|
2015-06-18 10:45:38 +02:00
|
|
|
return settings;
|
|
|
|
}
|
2015-06-18 19:23:24 +02:00
|
|
|
|
2015-09-19 21:58:32 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractApplication::LoadTranslation(const QString &locale)
|
|
|
|
{
|
|
|
|
if (locale.isEmpty())
|
|
|
|
{
|
|
|
|
qDebug()<<"Locale is empty.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qDebug()<<"Checked locale:"<<locale;
|
|
|
|
|
|
|
|
ClearTranslation();
|
|
|
|
|
|
|
|
qtTranslator = new QTranslator(this);
|
2015-11-17 15:20:36 +01:00
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
|
|
|
qtTranslator->load("qt_" + locale, translationsPath(locale));
|
2015-09-19 21:58:32 +02:00
|
|
|
#else
|
|
|
|
qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
|
|
#endif
|
|
|
|
installTranslator(qtTranslator);
|
|
|
|
|
|
|
|
qtxmlTranslator = new QTranslator(this);
|
2015-11-17 15:20:36 +01:00
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
|
|
|
qtxmlTranslator->load("qtxmlpatterns_" + locale, translationsPath(locale));
|
2015-09-19 21:58:32 +02:00
|
|
|
#else
|
|
|
|
qtxmlTranslator->load("qtxmlpatterns_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
|
|
#endif
|
|
|
|
installTranslator(qtxmlTranslator);
|
|
|
|
|
2015-11-17 15:27:51 +01:00
|
|
|
qtBaseTranslator = new QTranslator(this);
|
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
|
|
|
qtBaseTranslator->load("qtbase_" + locale, translationsPath(locale));
|
|
|
|
#else
|
|
|
|
qtBaseTranslator->load("qtbase_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
|
|
#endif
|
|
|
|
installTranslator(qtBaseTranslator);
|
|
|
|
|
2015-09-19 21:58:32 +02:00
|
|
|
appTranslator = new QTranslator(this);
|
2015-11-17 15:20:36 +01:00
|
|
|
appTranslator->load("valentina_" + locale, translationsPath(locale));
|
2015-09-19 21:58:32 +02:00
|
|
|
installTranslator(appTranslator);
|
|
|
|
|
|
|
|
const QString system = Settings()->GetPMSystemCode();
|
|
|
|
|
|
|
|
pmsTranslator = new QTranslator(this);
|
2015-11-17 15:20:36 +01:00
|
|
|
pmsTranslator->load("measurements_" + system + "_" + locale, translationsPath(locale));
|
2015-09-19 21:58:32 +02:00
|
|
|
installTranslator(pmsTranslator);
|
|
|
|
|
|
|
|
InitTrVars();//Very important do it after load QM files.
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VAbstractApplication::ClearTranslation()
|
|
|
|
{
|
|
|
|
if (not qtTranslator.isNull())
|
|
|
|
{
|
|
|
|
removeTranslator(qtTranslator);
|
|
|
|
delete qtTranslator;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not qtxmlTranslator.isNull())
|
|
|
|
{
|
|
|
|
removeTranslator(qtxmlTranslator);
|
|
|
|
delete qtxmlTranslator;
|
|
|
|
}
|
|
|
|
|
2015-11-17 15:27:51 +01:00
|
|
|
if (not qtBaseTranslator.isNull())
|
|
|
|
{
|
|
|
|
removeTranslator(qtBaseTranslator);
|
|
|
|
delete qtBaseTranslator;
|
|
|
|
}
|
|
|
|
|
2015-09-19 21:58:32 +02:00
|
|
|
if (not appTranslator.isNull())
|
|
|
|
{
|
|
|
|
removeTranslator(appTranslator);
|
|
|
|
delete appTranslator;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not pmsTranslator.isNull())
|
|
|
|
{
|
|
|
|
removeTranslator(pmsTranslator);
|
|
|
|
delete pmsTranslator;
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 12:18:38 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief ClearMessage helps to clear a message string from standard Qt function.
|
|
|
|
* @param msg the message that contains '"' at the start and at the end
|
|
|
|
* @return cleared string
|
|
|
|
*/
|
|
|
|
QString VAbstractApplication::ClearMessage(QString msg)
|
|
|
|
{
|
|
|
|
if (msg.startsWith('"') && msg.endsWith('"'))
|
|
|
|
{
|
|
|
|
msg.remove(0, 1);
|
|
|
|
msg.chop(1);
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:07:55 +01:00
|
|
|
msg.replace("\\\"", "\"");
|
|
|
|
|
2020-11-20 12:18:38 +01:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool VAbstractApplication::IsWarningMessage(const QString &message) const
|
|
|
|
{
|
|
|
|
return VAbstractApplication::ClearMessage(message).startsWith(warningMessageSignature);
|
|
|
|
}
|
2020-12-17 17:43:15 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QFileDialog::Options VAbstractApplication::NativeFileDialog(QFileDialog::Options options) const
|
|
|
|
{
|
|
|
|
if (settings->IsDontUseNativeDialog())
|
|
|
|
{
|
|
|
|
options |= QFileDialog::DontUseNativeDialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
2021-02-06 14:52:21 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
auto VAbstractApplication::VApp() -> VAbstractApplication *
|
|
|
|
{
|
|
|
|
return qobject_cast<VAbstractApplication*>(QCoreApplication::instance());
|
|
|
|
}
|