Seamly2D
Code documentation
vabstractapplication.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file vabstractapplication.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 18 6, 2015
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentina project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2015 Valentina project
35  ** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
36  **
37  ** Valentina is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Valentina is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #include "vabstractapplication.h"
53 
54 #include <QDir>
55 #include <QLibraryInfo>
56 #include <QMessageLogger>
57 #include <QStaticStringData>
58 #include <QStringData>
59 #include <QStringDataPtr>
60 #include <QTranslator>
61 #include <Qt>
62 #include <QtDebug>
63 
64 #include "../vmisc/def.h"
65 #include "../vmisc/logging.h"
66 
67 //---------------------------------------------------------------------------------------------------------------------
69  :QApplication(argc, argv),
70  undoStack(nullptr),
71  mainWindow(nullptr),
72  settings(nullptr),
73  qtTranslator(nullptr),
74  qtxmlTranslator(nullptr),
75  qtBaseTranslator(nullptr),
76  appTranslator(nullptr),
77  pmsTranslator(nullptr),
78  _patternUnit(Unit::Cm),
79  _patternType(MeasurementsType::Unknown),
80  patternFilePath(),
81  currentScene(nullptr),
82  sceneView(nullptr),
83  doc(nullptr),
84  data(nullptr),
85  openingPattern(false)
86 {
87  QString rules;
88 
89 #if defined(V_NO_ASSERT)
90  // Ignore SSL-related warnings
91  // See issue #528: Error: QSslSocket: cannot resolve SSLv2_client_method.
92  rules += QLatin1String("qt.network.ssl.warning=false\n");
93  // See issue #568: Certificate checking on Mac OS X.
94  rules += QLatin1String("qt.network.ssl.critical=false\n"
95  "qt.network.ssl.fatal=false\n");
96 #endif //defined(V_NO_ASSERT)
97 
98  // cppcheck-suppress reademptycontainer
99  if (!rules.isEmpty())
100  {
101  QLoggingCategory::setFilterRules(rules);
102  }
103 
104  setAttribute(Qt::AA_UseHighDpiPixmaps);
105 
106  connect(this, &QApplication::aboutToQuit, this, [this]()
107  {
108  // If try to use the method QApplication::exit program can't sync settings and show warning about QApplication
109  // instance. Solution is to call sync() before quit.
110  // Connect this slot with VApplication::aboutToQuit.
111  Settings()->sync();
112  });
113 }
114 
115 //---------------------------------------------------------------------------------------------------------------------
117 {}
118 
119 //---------------------------------------------------------------------------------------------------------------------
120 /**
121  * @brief translationsPath return path to the root directory that contains QM files.
122  * @param locale historic, not used
123  * @return path to a directory that contains QM files, default from CONFIG+=embed_translations as set in translations.pri
124  */
125 QString VAbstractApplication::translationsPath(const QString &locale) const
126 {
127  Q_UNUSED(locale)
128  return QStringLiteral(":/i18n/");
129 }
130 
131 //---------------------------------------------------------------------------------------------------------------------
133 {
134  return _patternType;
135 }
136 
137 //---------------------------------------------------------------------------------------------------------------------
139 {
141 }
142 
143 //---------------------------------------------------------------------------------------------------------------------
145 {
146  this->doc = doc;
147 }
148 
149 //---------------------------------------------------------------------------------------------------------------------
151 {
152  SCASSERT(doc != nullptr)
153  return doc;
154 }
155 
156 //---------------------------------------------------------------------------------------------------------------------
158 {
159  this->data = data;
160 }
161 
162 //---------------------------------------------------------------------------------------------------------------------
164 {
165  SCASSERT(data != nullptr)
166  return data;
167 }
168 
169 //---------------------------------------------------------------------------------------------------------------------
171 {
172  return openingPattern;
173 }
174 
175 //---------------------------------------------------------------------------------------------------------------------
177 {
179 }
180 
181 //---------------------------------------------------------------------------------------------------------------------
183 {
184  return mainWindow;
185 }
186 
187 //---------------------------------------------------------------------------------------------------------------------
189 {
190  SCASSERT(value != nullptr)
191  mainWindow = value;
192 }
193 
194 //---------------------------------------------------------------------------------------------------------------------
196 {
197  return undoStack;
198 }
199 
200 //---------------------------------------------------------------------------------------------------------------------
202 {
203  return _patternUnit;
204 }
205 
206 //---------------------------------------------------------------------------------------------------------------------
208 {
209  return &_patternUnit;
210 }
211 
212 //---------------------------------------------------------------------------------------------------------------------
214 {
216 }
217 
218 //---------------------------------------------------------------------------------------------------------------------
219 /**
220  * @brief getSettings hide settings constructor.
221  * @return pointer to class for acssesing to settings in ini file.
222  */
224 {
225  SCASSERT(settings != nullptr)
226  return settings;
227 }
228 
229 //---------------------------------------------------------------------------------------------------------------------
231 {
232  SCASSERT(*currentScene != nullptr)
233  return *currentScene;
234 }
235 
236 //---------------------------------------------------------------------------------------------------------------------
237 void VAbstractApplication::setCurrentScene(QGraphicsScene **value)
238 {
239  currentScene = value;
240 }
241 
242 //---------------------------------------------------------------------------------------------------------------------
244 {
245  return sceneView;
246 }
247 
248 //---------------------------------------------------------------------------------------------------------------------
250 {
251  sceneView = value;
252 }
253 
254 //---------------------------------------------------------------------------------------------------------------------
255 double VAbstractApplication::toPixel(double val) const
256 {
257  return ToPixel(val, _patternUnit);
258 }
259 
260 //---------------------------------------------------------------------------------------------------------------------
261 double VAbstractApplication::fromPixel(double pix) const
262 {
263  return FromPixel(pix, _patternUnit);
264 }
265 
266 //---------------------------------------------------------------------------------------------------------------------
267 void VAbstractApplication::loadTranslations(const QString &locale)
268 {
269  if (locale.isEmpty())
270  {
271  qDebug()<<"Locale is empty.";
272  return;
273  }
274  qDebug()<<"Checked locale:"<<locale;
275 
277 
278  qtTranslator = new QTranslator(this);
279  qtxmlTranslator = new QTranslator(this);
280  qtBaseTranslator = new QTranslator(this);
281  appTranslator = new QTranslator(this);
282  pmsTranslator = new QTranslator(this);
283 
284 #if defined(Q_OS_WIN) || defined(Q_OS_MAC)
285  qtTranslator->load("qt_" + locale, translationsPath(locale));
286  qtxmlTranslator->load("qtxmlpatterns_" + locale, translationsPath(locale));
287  qtBaseTranslator->load("qtbase_" + locale, translationsPath(locale));
288 #else
289  qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
290  qtxmlTranslator->load("qtxmlpatterns_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
291  qtBaseTranslator->load("qtbase_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
292 #endif
293 
294  appTranslator->load("seamly2d_" + locale, translationsPath(locale));
295  pmsTranslator->load("measurements_" + locale, translationsPath(locale));
296 
297  installTranslator(qtTranslator);
298  installTranslator(qtxmlTranslator);
299  installTranslator(qtBaseTranslator);
300  installTranslator(appTranslator);
301  installTranslator(pmsTranslator);
302 
303  InitTrVars();//Very important do it after load QM files.
304 }
305 
306 //---------------------------------------------------------------------------------------------------------------------
308 {
309  if (!qtTranslator.isNull())
310  {
311  removeTranslator(qtTranslator);
312  delete qtTranslator;
313  }
314 
315  if (!qtxmlTranslator.isNull())
316  {
317  removeTranslator(qtxmlTranslator);
318  delete qtxmlTranslator;
319  }
320 
321  if (!qtBaseTranslator.isNull())
322  {
323  removeTranslator(qtBaseTranslator);
324  delete qtBaseTranslator;
325  }
326 
327  if (!appTranslator.isNull())
328  {
329  removeTranslator(appTranslator);
330  delete appTranslator;
331  }
332 
333  if (!pmsTranslator.isNull())
334  {
335  removeTranslator(pmsTranslator);
336  delete pmsTranslator;
337  }
338 }
QWidget * mainWindow
mainWindow pointer to main window. Usefull if need create modal dialog. Without pointer to main windo...
bool openingPattern
openingPattern true when we opening pattern. If something will be wrong in formula this help understa...
void setCurrentDocument(VAbstractPattern *doc)
QPointer< QTranslator > qtBaseTranslator
void setMainWindow(QWidget *value)
const Unit * patternUnitP() const
VMainGraphicsView * sceneView
void setPatternUnit(const Unit &patternUnit)
MeasurementsType _patternType
void setCurrentScene(QGraphicsScene **value)
virtual ~VAbstractApplication() Q_DECL_OVERRIDE
QWidget * getMainWindow() const
double fromPixel(double pix) const
VCommonSettings * settings
settings pointer to settings. Help hide constructor creation settings. Make make code more readable.
QGraphicsScene ** currentScene
QPointer< QTranslator > pmsTranslator
QUndoStack * getUndoStack() const
VContainer * getCurrentData() const
QPointer< QTranslator > appTranslator
virtual void InitTrVars()=0
QPointer< QTranslator > qtTranslator
QString translationsPath(const QString &locale=QString()) const
translationsPath return path to the root directory that contains QM files.
QPointer< QTranslator > qtxmlTranslator
double toPixel(double val) const
VAbstractPattern * doc
void loadTranslations(const QString &locale)
VCommonSettings * Settings()
getSettings hide settings constructor.
MeasurementsType patternType() const
void setPatternType(const MeasurementsType &patternType)
VMainGraphicsView * getSceneView() const
VAbstractPattern * getCurrentDocument() const
void setSceneView(VMainGraphicsView *value)
void setCurrentData(VContainer *data)
QGraphicsScene * getCurrentScene() const
VAbstractApplication(int &argc, char **argv)
The VContainer class container of all variables.
Definition: vcontainer.h:141
The VMainGraphicsView class main scene view.
double ToPixel(double val, const Unit &unit)
Definition: def.cpp:231
double FromPixel(double pix, const Unit &unit)
Definition: def.cpp:250
#define SCASSERT(cond)
Definition: def.h:317
MeasurementsType
Definition: def.h:104
Unit
Definition: def.h:105