1 /************************************************************************
2  **
3  **  @file   vcommonsettings.cpp
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   15 7, 2015
6  **
7  **  @brief
8  **  @copyright
9  **  This source code is part of the Valentina project, a pattern making
10  **  program, whose allow create and modeling patterns of clothing.
11  **  Copyright (C) 2015 Valentina project
12  **  <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
13  **
14  **  Valentina is free software: you can redistribute it and/or modify
15  **  it under the terms of the GNU General Public License as published by
16  **  the Free Software Foundation, either version 3 of the License, or
17  **  (at your option) any later version.
18  **
19  **  Valentina is distributed in the hope that it will be useful,
20  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  **  GNU General Public License for more details.
23  **
24  **  You should have received a copy of the GNU General Public License
25  **  along with Valentina.  If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #include "vcommonsettings.h"
30 
31 #include <QApplication>
32 #include <QDate>
33 #include <QDir>
34 #include <QLocale>
35 #include <QMessageLogger>
36 #include <QStaticStringData>
37 #include <QStringData>
38 #include <QStringDataPtr>
39 #include <QVariant>
40 #include <QtDebug>
41 #include <QTextCodec>
42 #include <QFont>
43 #include <QGlobalStatic>
44 
45 #include "../vmisc/def.h"
46 #include "../vmisc/vmath.h"
47 #include "../vmisc/compatibility.h"
48 #include "../vpatterndb/pmsystems.h"
49 
50 Q_DECLARE_METATYPE(QMarginsF)
51 
52 namespace
53 {
54 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPathsIndividualMeasurements, (QLatin1String("paths/individual_measurements")))
55 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPathsMultisizeMeasurements, (QLatin1String("paths/standard_measurements")))
56 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPathsPattern, (QLatin1String("paths/pattern")))
57 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPathsTemplates, (QLatin1String("paths/templates")))
58 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPathsLabelTemplate, (QLatin1String("paths/labels")))
59 
60 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationOsSeparator, (QLatin1String("configuration/osSeparator")))
61 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationAutosaveState, (QLatin1String("configuration/autosave/state")))
62 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationAutosaveTime , (QLatin1String("configuration/autosave/time")))
63 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationLocale, (QLatin1String("configuration/locale")))
64 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPMSystemCode, (QLatin1String("configuration/pmscode")))
65 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationUnit, (QLatin1String("configuration/unit")))
66 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationConfirmItemDeletion, (QLatin1String("configuration/confirm_item_deletion")))
67 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationConfirmFormatRewriting, (QLatin1String("configuration/confirm_format_rewriting")))
68 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationToolBarStyle, (QLatin1String("configuration/tool_bar_style")))
69 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationFreeCurveMode, (QLatin1String("configuration/freeCurveMode")))
70 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationDoubleClickZoomFitBestCurrentPP, (QLatin1String("configuration/doubleClickZoomFitBestCurrentPP")))
71 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationDontUseNativeDialog, (QLatin1String("configuration/dontUseNativeDialog")))
72 
73 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternUndo, (QLatin1String("pattern/undo")))
74 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternForbidFlipping, (QLatin1String("pattern/forbidFlipping")))
75 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternForceFlipping, (QLatin1String("pattern/forceFlipping")))
76 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternHideMainPath, (QLatin1String("pattern/hideMainPath")))
77 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingDoublePassmark, (QLatin1String("pattern/doublePassmark")))
78 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternDefaultSeamAllowance, (QLatin1String("pattern/defaultSeamAllowance")))
79 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternLabelFont, (QLatin1String("pattern/labelFont")))
80 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternLineWidth, (QLatin1String("pattern/lineWidth")))
81 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternCurveApproximationScale, (QLatin1String("pattern/curveApproximationScale")))
82 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternShowCurveDetails, (QLatin1String("pattern/showCurveDetails")))
83 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternPieceShowMainPath, (QLatin1String("pattern/pieceShowMainPath")))
84 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternLabelFontSize, (QLatin1String("pattern/labelFontSize")))
85 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPatternHideLabels, (QLatin1String("pattern/hideLabels")))
86 
87 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingGeneralRecentFileList, (QLatin1String("recentFileList")))
88 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingGeneralRestoreFileList, (QLatin1String("restoreFileList")))
89 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingGeneralGeometry, (QLatin1String("geometry")))
90 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingGeneralWindowState, (QLatin1String("windowState")))
91 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingGeneralToolbarsState, (QLatin1String("toolbarsState")))
92 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingConfigurationDarkMode, (QLatin1String("configuration/dark_mode")))
93 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPreferenceDialogSize, (QLatin1String("preferenceDialogSize")))
94 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingToolSeamAllowanceDialogSize, (QLatin1String("toolSeamAllowanceDialogSize")))
95 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingIncrementsDialogSize, (QLatin1String("toolIncrementsDialogSize")))
96 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingFormulaWizardDialogSize, (QLatin1String("formulaWizardDialogSize")))
97 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingFinalMeasurementsDialogSize, (QLatin1String("finalMeasurementsDialogSize")))
98 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLatestSkippedVersion, (QLatin1String("lastestSkippedVersion")))
99 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingDateOfLastRemind, (QLatin1String("dateOfLastRemind")))
100 
101 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingCSVWithHeader, (QLatin1String("csv/withHeader")))
102 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingCSVCodec, (QLatin1String("csv/withCodec")))
103 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingCSVSeparator, (QLatin1String("csv/withSeparator")))
104 
105 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLabelDateFormat, (QLatin1String("label/dateFormat")))
106 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLabelUserDateFormats, (QLatin1String("label/userDateFormats")))
107 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLabelTimeFormat, (QLatin1String("label/timeFormat")))
108 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLabelUserTimeFormats, (QLatin1String("label/userTimeFormats")))
109 
110 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingTiledPDFMargins, (QLatin1String("tiledPDF/margins")))
111 Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingTiledPDFOrientation, (QLatin1String("tiledPDF/orientation")))
112 
113 // Reading settings file is very expensive, cache curve approximation to speed up getting value
114 qreal curveApproximationCached = -1;
115 Q_GLOBAL_STATIC(QString, localeCached)
116 qreal lineWidthCached = 0;
117 int labelFontSizeCached = 0;
118 int pieceShowMainPath = -1;
119 
120 //---------------------------------------------------------------------------------------------------------------------
ClearFormats(const QStringList & predefinedFormats,QStringList formats)121 QStringList ClearFormats(const QStringList &predefinedFormats, QStringList formats)
122 {
123     for (auto &f : predefinedFormats)
124     {
125         formats.removeAll(f);
126     }
127     return formats;
128 }
129 }
130 
131 Q_GLOBAL_STATIC_WITH_ARGS(const QString, commonIniFilename, (QLatin1String("common")))
132 
133 #if !defined(Q_OS_WIN)
134 const QString VCommonSettings::unixStandardSharePath = PKGDATADIR;
135 #endif
136 
137 namespace
138 {
139 //---------------------------------------------------------------------------------------------------------------------
SymlinkCopyDirRecursive(const QString & fromDir,const QString & toDir,bool replaceOnConflit)140 void SymlinkCopyDirRecursive(const QString &fromDir, const QString &toDir, bool replaceOnConflit)
141 {
142     QDir dir;
143     dir.setPath(fromDir);
144 
145     const QStringList list = dir.entryList(QDir::Files);
146     for (const QString &copyFile : list)
147     {
148         const QString from = fromDir + QDir::separator() + copyFile;
149         QString to = toDir + QDir::separator() + copyFile;
150 
151 #ifdef Q_OS_WIN
152         {
153             // To fix issue #702 check each not symlink if it is actually broken symlink.
154             // Also trying to mimic Unix symlink. If a file eaxists do not create a symlink and remove it if exists.
155             QFile fileTo(to);
156             if (fileTo.exists())
157             {
158                 if (not fileTo.rename(to + QLatin1String(".lnk")))
159                 {
160                     QFile::remove(to + QLatin1String(".lnk"));
161                     fileTo.rename(to + QLatin1String(".lnk"));
162                 }
163 
164                 QFileInfo info(to + QLatin1String(".lnk"));
165                 if (info.symLinkTarget().isEmpty())
166                 {
167                     fileTo.copy(to);
168                     fileTo.remove();
169                     continue; // The file already exists, skip creating shortcut
170                 }
171             }
172         }
173 
174         to = to + QLatin1String(".lnk");
175 #endif
176 
177         QFileInfo fileTo(to);
178         if (not fileTo.isSymLink() && fileTo.exists())
179         {
180             if (replaceOnConflit)
181             {
182                 QFile::remove(to);
183             }
184             else
185             {
186                 continue;
187             }
188         }
189         else if (fileTo.isSymLink())
190         {
191             if (not fileTo.exists())
192             { // automatically fix broken symlink
193                 QFile::remove(to);
194             }
195             else if (replaceOnConflit)
196             {
197                 QFile::remove(to);
198             }
199             else
200             {
201                 continue;
202             }
203         }
204 
205         QFile::link(from, to);
206     }
207 
208     const QStringList dirList = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
209     for (const QString &copyDir : dirList)
210     {
211         const QString from = fromDir + QDir::separator() + copyDir;
212         const QString to = toDir + QDir::separator() + copyDir;
213 
214         if (dir.mkpath(to) == false)
215         {
216             return;
217         }
218 
219         SymlinkCopyDirRecursive(from, to, replaceOnConflit);
220     }
221 }
222 
223 //---------------------------------------------------------------------------------------------------------------------
PrepareStandardFiles(const QString & currentPath,const QString & standardPath,const QString & defPath)224 QString PrepareStandardFiles(const QString &currentPath, const QString &standardPath, const QString &defPath)
225 {
226     QDir standardPathDir(standardPath);
227     QDir currentPathDir(currentPath);
228     if ((currentPath == defPath || not currentPathDir.exists()) && standardPathDir.exists())
229     {
230         const QDir localdata (defPath);
231         if (localdata.mkpath(QChar('.')))
232         {
233             SymlinkCopyDirRecursive(standardPath, defPath, false);
234         }
235         return defPath;
236     }
237     return currentPath;
238 }
239 }
240 
241 //---------------------------------------------------------------------------------------------------------------------
VCommonSettings(Format format,Scope scope,const QString & organization,const QString & application,QObject * parent)242 VCommonSettings::VCommonSettings(Format format, Scope scope, const QString &organization,
243                             const QString &application, QObject *parent)
244     :QSettings(format, scope, organization, application, parent)
245 {}
246 
247 //---------------------------------------------------------------------------------------------------------------------
VCommonSettings(const QString & fileName,QSettings::Format format,QObject * parent)248 VCommonSettings::VCommonSettings(const QString &fileName, QSettings::Format format, QObject *parent)
249     :QSettings(fileName, format, parent)
250 {}
251 
252 //---------------------------------------------------------------------------------------------------------------------
SharePath(const QString & shareItem)253 QString VCommonSettings::SharePath(const QString &shareItem)
254 {
255 #ifdef Q_OS_WIN
256     return QCoreApplication::applicationDirPath() + shareItem;
257 #elif defined(Q_OS_MAC)
258     QDir dirBundle(QCoreApplication::applicationDirPath() + QStringLiteral("/../Resources") + shareItem);
259     if (dirBundle.exists())
260     {
261         return dirBundle.absolutePath();
262     }
263     else
264     {
265         QDir appDir = QDir(qApp->applicationDirPath());
266         appDir.cdUp();
267         appDir.cdUp();
268         appDir.cdUp();
269         QDir dir(appDir.absolutePath() + shareItem);
270         if (dir.exists())
271         {
272             return dir.absolutePath();
273         }
274         else
275         {
276             return VCommonSettings::unixStandardSharePath + shareItem;
277         }
278     }
279 #else // Unix
280 #ifdef QT_DEBUG
281     return QCoreApplication::applicationDirPath() + shareItem;
282 #else
283     QDir dir(QCoreApplication::applicationDirPath() + shareItem);
284     if (dir.exists())
285     {
286         return dir.absolutePath();
287     }
288     else
289     {
290         return VCommonSettings::unixStandardSharePath + shareItem;
291     }
292 #endif
293 #endif
294 }
295 
296 //---------------------------------------------------------------------------------------------------------------------
MultisizeTablesPath()297 QString VCommonSettings::MultisizeTablesPath()
298 {
299     return SharePath(QStringLiteral("/tables/multisize"));
300 }
301 
302 //---------------------------------------------------------------------------------------------------------------------
StandardTemplatesPath()303 QString VCommonSettings::StandardTemplatesPath()
304 {
305     return SharePath(QStringLiteral("/tables/templates"));
306 }
307 
308 //---------------------------------------------------------------------------------------------------------------------
LabelTemplatesPath()309 QString VCommonSettings::LabelTemplatesPath()
310 {
311     return SharePath(QStringLiteral("/labels"));
312 }
313 
314 //---------------------------------------------------------------------------------------------------------------------
PrepareStandardTemplates(const QString & currentPath)315 QString VCommonSettings::PrepareStandardTemplates(const QString & currentPath)
316 {
317     return PrepareStandardFiles(currentPath, StandardTemplatesPath(), GetDefPathTemplate());
318 }
319 
320 //---------------------------------------------------------------------------------------------------------------------
PrepareMultisizeTables(const QString & currentPath)321 QString VCommonSettings::PrepareMultisizeTables(const QString &currentPath)
322 {
323     return PrepareStandardFiles(currentPath, MultisizeTablesPath(), GetDefPathMultisizeMeasurements());
324 }
325 
326 //---------------------------------------------------------------------------------------------------------------------
PrepareLabelTemplates(const QString & currentPath)327 QString VCommonSettings::PrepareLabelTemplates(const QString &currentPath)
328 {
329     return PrepareStandardFiles(currentPath, LabelTemplatesPath(), GetDefPathLabelTemplate());
330 }
331 
332 //---------------------------------------------------------------------------------------------------------------------
GetDefPathIndividualMeasurements()333 QString VCommonSettings::GetDefPathIndividualMeasurements()
334 {
335     return QDir::homePath() + QLatin1String("/valentina/") + tr("measurements") + QLatin1String("/") + tr("individual");
336 }
337 
338 //---------------------------------------------------------------------------------------------------------------------
GetPathIndividualMeasurements() const339 QString VCommonSettings::GetPathIndividualMeasurements() const
340 {
341     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
342     return settings.value(*settingPathsIndividualMeasurements, GetDefPathIndividualMeasurements()).toString();
343 }
344 
345 //---------------------------------------------------------------------------------------------------------------------
SetPathIndividualMeasurements(const QString & value)346 void VCommonSettings::SetPathIndividualMeasurements(const QString &value)
347 {
348     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
349     settings.setValue(*settingPathsIndividualMeasurements, value);
350     settings.sync();
351 }
352 
353 //---------------------------------------------------------------------------------------------------------------------
GetDefPathMultisizeMeasurements()354 QString VCommonSettings::GetDefPathMultisizeMeasurements()
355 {
356     return QDir::homePath() + QLatin1String("/valentina/") + tr("measurements") + QLatin1String("/") + tr("multisize");
357 }
358 
359 //---------------------------------------------------------------------------------------------------------------------
GetPathMultisizeMeasurements() const360 QString VCommonSettings::GetPathMultisizeMeasurements() const
361 {
362     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
363     return settings.value(*settingPathsMultisizeMeasurements, GetDefPathMultisizeMeasurements()).toString();
364 }
365 
366 //---------------------------------------------------------------------------------------------------------------------
SetPathMultisizeMeasurements(const QString & value)367 void VCommonSettings::SetPathMultisizeMeasurements(const QString &value)
368 {
369     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
370     settings.setValue(*settingPathsMultisizeMeasurements, value);
371     settings.sync();
372 }
373 
374 //---------------------------------------------------------------------------------------------------------------------
GetDefPathPattern()375 QString VCommonSettings::GetDefPathPattern()
376 {
377     return QDir::homePath() + QLatin1String("/valentina/") + tr("patterns");
378 }
379 
380 //---------------------------------------------------------------------------------------------------------------------
GetPathPattern() const381 QString VCommonSettings::GetPathPattern() const
382 {
383     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
384     return settings.value(*settingPathsPattern, GetDefPathPattern()).toString();
385 }
386 
387 //---------------------------------------------------------------------------------------------------------------------
SetPathPattern(const QString & value)388 void VCommonSettings::SetPathPattern(const QString &value)
389 {
390     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
391     settings.setValue(*settingPathsPattern, value);
392     settings.sync();
393 }
394 
395 //---------------------------------------------------------------------------------------------------------------------
GetDefPathTemplate()396 QString VCommonSettings::GetDefPathTemplate()
397 {
398     return QDir::homePath() + QLatin1String("/valentina/") + tr("templates");
399 }
400 
401 //---------------------------------------------------------------------------------------------------------------------
GetPathTemplate() const402 QString VCommonSettings::GetPathTemplate() const
403 {
404     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
405     return settings.value(*settingPathsTemplates, GetDefPathTemplate()).toString();
406 }
407 
408 //---------------------------------------------------------------------------------------------------------------------
SetPathTemplate(const QString & value)409 void VCommonSettings::SetPathTemplate(const QString &value)
410 {
411     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
412     settings.setValue(*settingPathsTemplates, value);
413     settings.sync();
414 }
415 
416 //---------------------------------------------------------------------------------------------------------------------
GetDefPathLabelTemplate()417 QString VCommonSettings::GetDefPathLabelTemplate()
418 {
419     return QDir::homePath() + QLatin1String("/valentina/") + tr("label templates");
420 }
421 
422 //---------------------------------------------------------------------------------------------------------------------
GetPathLabelTemplate() const423 QString VCommonSettings::GetPathLabelTemplate() const
424 {
425     return value(*settingPathsLabelTemplate, GetDefPathLabelTemplate()).toString();
426 }
427 
428 //---------------------------------------------------------------------------------------------------------------------
SetPathLabelTemplate(const QString & value)429 void VCommonSettings::SetPathLabelTemplate(const QString &value)
430 {
431     setValue(*settingPathsLabelTemplate, value);
432 }
433 
434 //---------------------------------------------------------------------------------------------------------------------
GetOsSeparator() const435 bool VCommonSettings::GetOsSeparator() const
436 {
437     return value(*settingConfigurationOsSeparator, 1).toBool();
438 }
439 
440 //---------------------------------------------------------------------------------------------------------------------
SetOsSeparator(const bool & value)441 void VCommonSettings::SetOsSeparator(const bool &value)
442 {
443     setValue(*settingConfigurationOsSeparator, value);
444 }
445 
446 //---------------------------------------------------------------------------------------------------------------------
GetAutosaveState() const447 bool VCommonSettings::GetAutosaveState() const
448 {
449     return value(*settingConfigurationAutosaveState, 1).toBool();
450 }
451 
452 //---------------------------------------------------------------------------------------------------------------------
SetAutosaveState(const bool & value)453 void VCommonSettings::SetAutosaveState(const bool &value)
454 {
455     setValue(*settingConfigurationAutosaveState, value);
456 }
457 
458 //---------------------------------------------------------------------------------------------------------------------
GetAutosaveTime() const459 int VCommonSettings::GetAutosaveTime() const
460 {
461     bool ok = false;
462     int val = value(*settingConfigurationAutosaveTime, 1).toInt(&ok);
463     if (ok == false)
464     {
465         qDebug()<<"Could not convert value"<<value(*settingConfigurationAutosaveTime, 1)
466                <<"to int. Return default value for autosave time"<<1<<"minutes.";
467         val = 1;
468     }
469     return val;
470 }
471 
472 //---------------------------------------------------------------------------------------------------------------------
SetAutosaveTime(const int & value)473 void VCommonSettings::SetAutosaveTime(const int &value)
474 {
475     setValue(*settingConfigurationAutosaveTime, value);
476 }
477 
478 //---------------------------------------------------------------------------------------------------------------------
GetLocale() const479 QString VCommonSettings::GetLocale() const
480 {
481     if (localeCached->isEmpty())
482     {
483         *localeCached = value(*settingConfigurationLocale, QLocale().name()).toString();
484     }
485     return *localeCached;
486 }
487 
488 //---------------------------------------------------------------------------------------------------------------------
SetLocale(const QString & value)489 void VCommonSettings::SetLocale(const QString &value)
490 {
491     setValue(*settingConfigurationLocale, value);
492     *localeCached = value;
493 }
494 
495 //---------------------------------------------------------------------------------------------------------------------
GetPMSystemCode() const496 QString VCommonSettings::GetPMSystemCode() const
497 {
498     return value(*settingPMSystemCode, "p998").toString();
499 }
500 
501 //---------------------------------------------------------------------------------------------------------------------
SetPMSystemCode(const QString & value)502 void VCommonSettings::SetPMSystemCode(const QString &value)
503 {
504     setValue(*settingPMSystemCode, value);
505 }
506 
507 //---------------------------------------------------------------------------------------------------------------------
GetUnit() const508 QString VCommonSettings::GetUnit() const
509 {
510     return value(*settingConfigurationUnit,
511                  QLocale().measurementSystem() == QLocale::MetricSystem ? unitCM : unitINCH).toString();
512 }
513 
514 //---------------------------------------------------------------------------------------------------------------------
SetUnit(const QString & value)515 void VCommonSettings::SetUnit(const QString &value)
516 {
517     setValue(*settingConfigurationUnit, value);
518 }
519 
520 //---------------------------------------------------------------------------------------------------------------------
GetConfirmItemDelete() const521 bool VCommonSettings::GetConfirmItemDelete() const
522 {
523     return value(*settingConfigurationConfirmItemDeletion, 1).toBool();
524 }
525 
526 //---------------------------------------------------------------------------------------------------------------------
SetConfirmItemDelete(const bool & value)527 void VCommonSettings::SetConfirmItemDelete(const bool &value)
528 {
529     setValue(*settingConfigurationConfirmItemDeletion, value);
530 }
531 
532 //---------------------------------------------------------------------------------------------------------------------
GetConfirmFormatRewriting() const533 bool VCommonSettings::GetConfirmFormatRewriting() const
534 {
535     return value(*settingConfigurationConfirmFormatRewriting, 1).toBool();
536 }
537 
538 //---------------------------------------------------------------------------------------------------------------------
SetConfirmFormatRewriting(const bool & value)539 void VCommonSettings::SetConfirmFormatRewriting(const bool &value)
540 {
541     setValue(*settingConfigurationConfirmFormatRewriting, value);
542 }
543 
544 //---------------------------------------------------------------------------------------------------------------------
GetToolBarStyle() const545 bool VCommonSettings::GetToolBarStyle() const
546 {
547     return value(*settingConfigurationToolBarStyle, 1).toBool();
548 }
549 
550 //---------------------------------------------------------------------------------------------------------------------
SetToolBarStyle(const bool & value)551 void VCommonSettings::SetToolBarStyle(const bool &value)
552 {
553     setValue(*settingConfigurationToolBarStyle, value);
554 }
555 
556 //---------------------------------------------------------------------------------------------------------------------
GetDarkMode() const557 bool VCommonSettings::GetDarkMode() const
558 {
559     return value(*settingConfigurationDarkMode, 0).toBool();
560 }
561 
562 //---------------------------------------------------------------------------------------------------------------------
SetDarkMode(const bool & value)563 void VCommonSettings::SetDarkMode(const bool &value)
564 {
565     setValue(*settingConfigurationDarkMode, value);
566 }
567 
568 
569 //---------------------------------------------------------------------------------------------------------------------
IsFreeCurveMode() const570 bool VCommonSettings::IsFreeCurveMode() const
571 {
572     return value(*settingConfigurationFreeCurveMode, 1).toBool();
573 }
574 
575 //---------------------------------------------------------------------------------------------------------------------
SetFreeCurveMode(bool value)576 void VCommonSettings::SetFreeCurveMode(bool value)
577 {
578     setValue(*settingConfigurationFreeCurveMode, value);
579 }
580 
581 //---------------------------------------------------------------------------------------------------------------------
IsDoubleClickZoomFitBestCurrentPP() const582 bool VCommonSettings::IsDoubleClickZoomFitBestCurrentPP() const
583 {
584     return value(*settingConfigurationDoubleClickZoomFitBestCurrentPP, 1).toBool();
585 }
586 
587 //---------------------------------------------------------------------------------------------------------------------
SetDoubleClickZoomFitBestCurrentPP(bool value)588 void VCommonSettings::SetDoubleClickZoomFitBestCurrentPP(bool value)
589 {
590     setValue(*settingConfigurationDoubleClickZoomFitBestCurrentPP, value);
591 }
592 
593 //---------------------------------------------------------------------------------------------------------------------
GetUndoCount() const594 int VCommonSettings::GetUndoCount() const
595 {
596     bool ok = false;
597     int val = value(*settingPatternUndo, 0).toInt(&ok);
598     if (ok == false)
599     {
600         qDebug()<<"Could not convert value"<<value(*settingPatternUndo, 0)
601                <<"to int. Return default value for undo counts 0 (no limit).";
602         val = 0;
603     }
604     return val;
605 }
606 
607 //---------------------------------------------------------------------------------------------------------------------
SetUndoCount(const int & value)608 void VCommonSettings::SetUndoCount(const int &value)
609 {
610     setValue(*settingPatternUndo, value);
611 }
612 
613 //---------------------------------------------------------------------------------------------------------------------
GetRecentFileList() const614 QStringList VCommonSettings::GetRecentFileList() const
615 {
616     const QStringList files = value(*settingGeneralRecentFileList).toStringList();
617     QStringList cleared;
618 
619     for (auto &f : files)
620     {
621         if (QFileInfo::exists(f))
622         {
623             cleared.append(f);
624         }
625     }
626 
627     return cleared;
628 }
629 
630 //---------------------------------------------------------------------------------------------------------------------
SetRecentFileList(const QStringList & value)631 void VCommonSettings::SetRecentFileList(const QStringList &value)
632 {
633     setValue(*settingGeneralRecentFileList, value);
634 }
635 
636 //---------------------------------------------------------------------------------------------------------------------
GetRestoreFileList() const637 QStringList VCommonSettings::GetRestoreFileList() const
638 {
639     return value(*settingGeneralRestoreFileList).toStringList();
640 }
641 
642 //---------------------------------------------------------------------------------------------------------------------
SetRestoreFileList(const QStringList & value)643 void VCommonSettings::SetRestoreFileList(const QStringList &value)
644 {
645     setValue(*settingGeneralRestoreFileList, value);
646 }
647 
648 //---------------------------------------------------------------------------------------------------------------------
GetGeometry() const649 QByteArray VCommonSettings::GetGeometry() const
650 {
651     return value(*settingGeneralGeometry).toByteArray();
652 }
653 
654 //---------------------------------------------------------------------------------------------------------------------
SetGeometry(const QByteArray & value)655 void VCommonSettings::SetGeometry(const QByteArray &value)
656 {
657     setValue(*settingGeneralGeometry, value);
658 }
659 
660 //---------------------------------------------------------------------------------------------------------------------
GetWindowState() const661 QByteArray VCommonSettings::GetWindowState() const
662 {
663     return value(*settingGeneralWindowState).toByteArray();
664 }
665 
666 //---------------------------------------------------------------------------------------------------------------------
SetWindowState(const QByteArray & value)667 void VCommonSettings::SetWindowState(const QByteArray &value)
668 {
669     setValue(*settingGeneralWindowState, value);
670 }
671 
672 //---------------------------------------------------------------------------------------------------------------------
GetToolbarsState() const673 QByteArray VCommonSettings::GetToolbarsState() const
674 {
675     return value(*settingGeneralToolbarsState).toByteArray();
676 }
677 
678 //---------------------------------------------------------------------------------------------------------------------
SetToolbarsState(const QByteArray & value)679 void VCommonSettings::SetToolbarsState(const QByteArray &value)
680 {
681     setValue(*settingGeneralToolbarsState, value);
682 }
683 
684 //---------------------------------------------------------------------------------------------------------------------
GetPreferenceDialogSize() const685 QSize VCommonSettings::GetPreferenceDialogSize() const
686 {
687     return value(*settingPreferenceDialogSize, QSize(0, 0)).toSize();
688 }
689 
690 //---------------------------------------------------------------------------------------------------------------------
SetPreferenceDialogSize(const QSize & sz)691 void VCommonSettings::SetPreferenceDialogSize(const QSize& sz)
692 {
693     setValue(*settingPreferenceDialogSize, sz);
694 }
695 
696 //---------------------------------------------------------------------------------------------------------------------
GetToolSeamAllowanceDialogSize() const697 QSize VCommonSettings::GetToolSeamAllowanceDialogSize() const
698 {
699     return value(*settingToolSeamAllowanceDialogSize, QSize(0, 0)).toSize();
700 }
701 
702 //---------------------------------------------------------------------------------------------------------------------
SetToolSeamAllowanceDialogSize(const QSize & sz)703 void VCommonSettings::SetToolSeamAllowanceDialogSize(const QSize &sz)
704 {
705     setValue(*settingToolSeamAllowanceDialogSize, sz);
706 }
707 
708 //---------------------------------------------------------------------------------------------------------------------
GetFormulaWizardDialogSize() const709 QSize VCommonSettings::GetFormulaWizardDialogSize() const
710 {
711     return value(*settingFormulaWizardDialogSize, QSize(0, 0)).toSize();
712 }
713 
714 //---------------------------------------------------------------------------------------------------------------------
SetFormulaWizardDialogSize(const QSize & sz)715 void VCommonSettings::SetFormulaWizardDialogSize(const QSize &sz)
716 {
717     setValue(*settingFormulaWizardDialogSize, sz);
718 }
719 
720 //---------------------------------------------------------------------------------------------------------------------
GetIncrementsDialogSize() const721 QSize VCommonSettings::GetIncrementsDialogSize() const
722 {
723     return value(*settingIncrementsDialogSize, QSize(0, 0)).toSize();
724 }
725 
726 //---------------------------------------------------------------------------------------------------------------------
SetIncrementsDialogSize(const QSize & sz)727 void VCommonSettings::SetIncrementsDialogSize(const QSize &sz)
728 {
729     setValue(*settingIncrementsDialogSize, sz);
730 }
731 
732 //---------------------------------------------------------------------------------------------------------------------
GetFinalMeasurementsDialogSize() const733 QSize VCommonSettings::GetFinalMeasurementsDialogSize() const
734 {
735     return value(*settingFinalMeasurementsDialogSize, QSize(0, 0)).toSize();
736 }
737 
738 //---------------------------------------------------------------------------------------------------------------------
SetFinalMeasurementsDialogSize(const QSize & sz)739 void VCommonSettings::SetFinalMeasurementsDialogSize(const QSize &sz)
740 {
741     setValue(*settingFinalMeasurementsDialogSize, sz);
742 }
743 
744 //---------------------------------------------------------------------------------------------------------------------
GetLatestSkippedVersion() const745 int VCommonSettings::GetLatestSkippedVersion() const
746 {
747     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
748     return settings.value(*settingLatestSkippedVersion, 0x0).toInt();
749 }
750 
751 //---------------------------------------------------------------------------------------------------------------------
SetLatestSkippedVersion(int value)752 void VCommonSettings::SetLatestSkippedVersion(int value)
753 {
754     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
755     settings.setValue(*settingLatestSkippedVersion, value);
756     settings.sync();
757 }
758 
759 //---------------------------------------------------------------------------------------------------------------------
GetDateOfLastRemind() const760 QDate VCommonSettings::GetDateOfLastRemind() const
761 {
762     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
763     return settings.value(*settingDateOfLastRemind, QDate(1900, 1, 1)).toDate();
764 }
765 
766 //---------------------------------------------------------------------------------------------------------------------
SetDateOfLastRemind(const QDate & date)767 void VCommonSettings::SetDateOfLastRemind(const QDate &date)
768 {
769     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
770     settings.setValue(*settingDateOfLastRemind, date);
771     settings.sync();
772 }
773 
774 //---------------------------------------------------------------------------------------------------------------------
GetForbidWorkpieceFlipping() const775 bool VCommonSettings::GetForbidWorkpieceFlipping() const
776 {
777     return value(*settingPatternForbidFlipping, false).toBool();
778 }
779 
780 //---------------------------------------------------------------------------------------------------------------------
SetForbidWorkpieceFlipping(bool value)781 void VCommonSettings::SetForbidWorkpieceFlipping(bool value)
782 {
783     setValue(*settingPatternForbidFlipping, value);
784 
785     if (value)
786     {
787         SetForceWorkpieceFlipping(not value);
788     }
789 }
790 
791 //---------------------------------------------------------------------------------------------------------------------
GetForceWorkpieceFlipping() const792 bool VCommonSettings::GetForceWorkpieceFlipping() const
793 {
794     return value(*settingPatternForceFlipping, false).toBool();
795 }
796 
797 //---------------------------------------------------------------------------------------------------------------------
SetForceWorkpieceFlipping(bool value)798 void VCommonSettings::SetForceWorkpieceFlipping(bool value)
799 {
800     setValue(*settingPatternForceFlipping, value);
801 
802     if (value)
803     {
804         SetForbidWorkpieceFlipping(not value);
805     }
806 }
807 
808 //---------------------------------------------------------------------------------------------------------------------
IsHideMainPath() const809 bool VCommonSettings::IsHideMainPath() const
810 {
811     return value(*settingPatternHideMainPath, false).toBool();
812 }
813 
814 //---------------------------------------------------------------------------------------------------------------------
SetHideMainPath(bool value)815 void VCommonSettings::SetHideMainPath(bool value)
816 {
817     setValue(*settingPatternHideMainPath, value);
818 }
819 
820 //---------------------------------------------------------------------------------------------------------------------
IsDoublePassmark() const821 bool VCommonSettings::IsDoublePassmark() const
822 {
823     return value(*settingDoublePassmark, false).toBool();
824 }
825 
826 //---------------------------------------------------------------------------------------------------------------------
SetDoublePassmark(bool value)827 void VCommonSettings::SetDoublePassmark(bool value)
828 {
829     setValue(*settingDoublePassmark, value);
830 }
831 
832 //---------------------------------------------------------------------------------------------------------------------
SetCSVWithHeader(bool withHeader)833 void VCommonSettings::SetCSVWithHeader(bool withHeader)
834 {
835     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
836     settings.setValue(*settingCSVWithHeader, withHeader);
837 }
838 
839 //---------------------------------------------------------------------------------------------------------------------
GetCSVWithHeader() const840 bool VCommonSettings::GetCSVWithHeader() const
841 {
842     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
843     return settings.value(*settingCSVWithHeader, GetDefCSVWithHeader()).toBool();
844 }
845 
846 //---------------------------------------------------------------------------------------------------------------------
GetDefCSVWithHeader()847 bool VCommonSettings::GetDefCSVWithHeader()
848 {
849     return false;
850 }
851 
852 //---------------------------------------------------------------------------------------------------------------------
SetCSVCodec(int mib)853 void VCommonSettings::SetCSVCodec(int mib)
854 {
855     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
856     settings.setValue(*settingCSVCodec, mib);
857 }
858 
859 //---------------------------------------------------------------------------------------------------------------------
GetCSVCodec() const860 int VCommonSettings::GetCSVCodec() const
861 {
862     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
863     return settings.value(*settingCSVCodec, GetDefCSVCodec()).toInt();
864 }
865 
866 //---------------------------------------------------------------------------------------------------------------------
GetDefCSVCodec()867 int VCommonSettings::GetDefCSVCodec()
868 {
869     return QTextCodec::codecForLocale()->mibEnum();
870 }
871 
872 //---------------------------------------------------------------------------------------------------------------------
SetCSVSeparator(const QChar & separator)873 void VCommonSettings::SetCSVSeparator(const QChar &separator)
874 {
875     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
876     switch(separator.toLatin1())
877     {
878         case '\t':
879             settings.setValue(*settingCSVSeparator, 0);
880             break;
881         case ';':
882             settings.setValue(*settingCSVSeparator, 1);
883             break;
884         case ' ':
885             settings.setValue(*settingCSVSeparator, 2);
886             break;
887         default:
888             settings.setValue(*settingCSVSeparator, 3);
889             break;
890     }
891 }
892 
893 //---------------------------------------------------------------------------------------------------------------------
GetCSVSeparator() const894 QChar VCommonSettings::GetCSVSeparator() const
895 {
896     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
897     const quint8 separator = static_cast<quint8>(settings.value(*settingCSVSeparator, 3).toUInt());
898     switch(separator)
899     {
900         case 0:
901             return QChar('\t');
902         case 1:
903             return QChar(';');
904         case 2:
905             return QChar(' ');
906         default:
907             return QChar(',');
908     }
909 }
910 
911 //---------------------------------------------------------------------------------------------------------------------
GetDefCSVSeparator()912 QChar VCommonSettings::GetDefCSVSeparator()
913 {
914     return QChar(',');
915 }
916 
917 //---------------------------------------------------------------------------------------------------------------------
SetDefaultSeamAllowance(double value)918 void VCommonSettings::SetDefaultSeamAllowance(double value)
919 {
920     setValue(*settingPatternDefaultSeamAllowance, UnitConvertor(value, StrToUnits(GetUnit()), Unit::Cm));
921 }
922 
923 //---------------------------------------------------------------------------------------------------------------------
924 /**
925  * @brief returns the default seam allowance. The corresponding unit is the default unit.
926  * @return the default seam allowance
927  */
GetDefaultSeamAllowance()928 double VCommonSettings::GetDefaultSeamAllowance()
929 {
930     double defaultValue;
931 
932     const Unit globalUnit = StrToUnits(GetUnit());
933 
934     switch (globalUnit)
935     {
936         case Unit::Mm:
937             defaultValue = 10;
938             break;
939         case Unit::Inch:
940             defaultValue = 0.25;
941             break;
942         case Unit::Cm:
943         default:
944             defaultValue = 1;
945             break;
946     }
947 
948     bool ok = false;
949     double val = value(*settingPatternDefaultSeamAllowance, -1).toDouble(&ok);
950     if (ok == false)
951     {
952         qDebug()<< "Could not convert value"<<value(*settingPatternDefaultSeamAllowance, 0)
953                 << "to real. Return default value for default seam allowance is "
954                 << defaultValue << QChar('.');
955         val = defaultValue;
956     }
957 
958     if (val < 0)
959     {
960         val = defaultValue;
961     }
962     else
963     {
964         val = UnitConvertor(val, Unit::Cm, globalUnit);
965     }
966 
967     return val;
968 }
969 
970 //---------------------------------------------------------------------------------------------------------------------
GetLabelFont() const971 QFont VCommonSettings::GetLabelFont() const
972 {
973     return qvariant_cast<QFont>(value(*settingPatternLabelFont, QApplication::font()));
974 }
975 
976 //---------------------------------------------------------------------------------------------------------------------
SetLabelFont(const QFont & f)977 void VCommonSettings::SetLabelFont(const QFont &f)
978 {
979     setValue(*settingPatternLabelFont, f);
980 }
981 
982 //---------------------------------------------------------------------------------------------------------------------
GetLabelFontSize() const983 int VCommonSettings::GetLabelFontSize() const
984 {
985     if (labelFontSizeCached <= 0)
986     {
987         bool ok = false;
988         labelFontSizeCached = value(*settingPatternLabelFontSize, GetDefLabelFontSize()).toInt(&ok);
989         if (not ok)
990         {
991             labelFontSizeCached = GetDefLabelFontSize();
992         }
993         labelFontSizeCached = qBound(minLabelFontSize, labelFontSizeCached, maxLabelFontSize);
994     }
995     return labelFontSizeCached;
996 }
997 
998 //---------------------------------------------------------------------------------------------------------------------
SetLabelFontSize(int size)999 void VCommonSettings::SetLabelFontSize(int size)
1000 {
1001     size = qBound(minLabelFontSize, size, maxLabelFontSize);
1002     setValue(*settingPatternLabelFontSize, size);
1003     labelFontSizeCached = size;
1004 }
1005 
1006 //---------------------------------------------------------------------------------------------------------------------
GetDefLabelFontSize()1007 int VCommonSettings::GetDefLabelFontSize()
1008 {
1009     return 32;
1010 }
1011 
1012 //---------------------------------------------------------------------------------------------------------------------
GetHideLabels() const1013 bool VCommonSettings::GetHideLabels() const
1014 {
1015     return value(*settingPatternHideLabels, false).toBool();
1016 }
1017 
1018 //---------------------------------------------------------------------------------------------------------------------
SetHideLabels(bool value)1019 void VCommonSettings::SetHideLabels(bool value)
1020 {
1021     setValue(*settingPatternHideLabels, value);
1022 }
1023 
1024 //---------------------------------------------------------------------------------------------------------------------
GetLabelDateFormat() const1025 QString VCommonSettings::GetLabelDateFormat() const
1026 {
1027     const QString format = value(*settingLabelDateFormat,
1028                                  ConstFirst<QString> (VCommonSettings::PredefinedDateFormats())).toString();
1029     const QStringList allFormats = VCommonSettings::PredefinedDateFormats() + GetUserDefinedDateFormats();
1030 
1031     return allFormats.contains(format) ? format : ConstFirst<QString> (VCommonSettings::PredefinedDateFormats());
1032 }
1033 
1034 //---------------------------------------------------------------------------------------------------------------------
SetLabelDateFormat(const QString & format)1035 void VCommonSettings::SetLabelDateFormat(const QString &format)
1036 {
1037     setValue(*settingLabelDateFormat, format);
1038 }
1039 
1040 //---------------------------------------------------------------------------------------------------------------------
PredefinedDateFormats()1041 QStringList VCommonSettings::PredefinedDateFormats()
1042 {
1043     return QStringList
1044     {
1045         "MM-dd-yyyy",
1046         "d/M/yy",
1047         "ddddMMMM dd, yyyy",
1048         "dd/MM/yy",
1049         "dd/MM/yyyy",
1050         "MMM d, yy",
1051         "MMM d, yyyy",
1052         "d. MMM. yyyy",
1053         "MMMM d, yyyy",
1054         "d. MMMM yyyy",
1055         "ddd, MMM d, yy",
1056         "ddd dd/MMM yy",
1057         "ddd, MMMM d, yyyy",
1058         "ddddMMMM d, yyyy",
1059         "MM-dd",
1060         "yy-MM-dd",
1061         "yyyy-MM-dd",
1062         "MM/yy",
1063         "MMM dd",
1064         "MMMM"
1065     };
1066 }
1067 
1068 //---------------------------------------------------------------------------------------------------------------------
GetUserDefinedDateFormats() const1069 QStringList VCommonSettings::GetUserDefinedDateFormats() const
1070 {
1071     return value(*settingLabelUserDateFormats, QStringList()).toStringList();
1072 }
1073 
1074 //---------------------------------------------------------------------------------------------------------------------
SetUserDefinedDateFormats(const QStringList & formats)1075 void VCommonSettings::SetUserDefinedDateFormats(const QStringList &formats)
1076 {
1077     setValue(*settingLabelUserDateFormats, ClearFormats(VCommonSettings::PredefinedDateFormats(), formats));
1078 }
1079 
1080 //---------------------------------------------------------------------------------------------------------------------
GetLabelTimeFormat() const1081 QString VCommonSettings::GetLabelTimeFormat() const
1082 {
1083     const QString format = value(*settingLabelTimeFormat,
1084                                  ConstFirst<QString> (VCommonSettings::PredefinedTimeFormats())).toString();
1085     const QStringList allFormats = VCommonSettings::PredefinedTimeFormats() + GetUserDefinedTimeFormats();
1086 
1087     return allFormats.contains(format) ? format : ConstFirst<QString> (VCommonSettings::PredefinedTimeFormats());
1088 }
1089 
1090 //---------------------------------------------------------------------------------------------------------------------
SetLabelTimeFormat(const QString & format)1091 void VCommonSettings::SetLabelTimeFormat(const QString &format)
1092 {
1093     setValue(*settingLabelTimeFormat, format);
1094 }
1095 
1096 //---------------------------------------------------------------------------------------------------------------------
PredefinedTimeFormats()1097 QStringList VCommonSettings::PredefinedTimeFormats()
1098 {
1099     return QStringList
1100     {
1101         "hh:mm:ss",
1102         "hh:mm:ss AP",
1103         "hh:mm",
1104         "hh:mm AP"
1105     };
1106 }
1107 
1108 //---------------------------------------------------------------------------------------------------------------------
GetUserDefinedTimeFormats() const1109 QStringList VCommonSettings::GetUserDefinedTimeFormats() const
1110 {
1111     return value(*settingLabelUserTimeFormats, QStringList()).toStringList();
1112 }
1113 
1114 //---------------------------------------------------------------------------------------------------------------------
SetUserDefinedTimeFormats(const QStringList & formats)1115 void VCommonSettings::SetUserDefinedTimeFormats(const QStringList &formats)
1116 {
1117     setValue(*settingLabelUserTimeFormats, ClearFormats(VCommonSettings::PredefinedTimeFormats(), formats));
1118 }
1119 
1120 //---------------------------------------------------------------------------------------------------------------------
GetCurveApproximationScale() const1121 qreal VCommonSettings::GetCurveApproximationScale() const
1122 {
1123     if (curveApproximationCached < 0)
1124     {
1125         bool ok = false;
1126         const qreal scale = value(*settingPatternCurveApproximationScale, defCurveApproximationScale).toDouble(&ok);
1127         if (ok && scale >= minCurveApproximationScale && scale <= maxCurveApproximationScale)
1128         {
1129             curveApproximationCached = scale;
1130         }
1131         else
1132         {
1133             curveApproximationCached = defCurveApproximationScale;
1134         }
1135     }
1136 
1137     return curveApproximationCached;
1138 }
1139 
1140 //---------------------------------------------------------------------------------------------------------------------
SetCurveApproximationScale(qreal value)1141 void VCommonSettings::SetCurveApproximationScale(qreal value)
1142 {
1143     if (value >= minCurveApproximationScale && value <= maxCurveApproximationScale)
1144     {
1145         setValue(*settingPatternCurveApproximationScale, value);
1146         curveApproximationCached = value;
1147     }
1148 }
1149 
1150 //---------------------------------------------------------------------------------------------------------------------
IsShowCurveDetails() const1151 bool VCommonSettings::IsShowCurveDetails() const
1152 {
1153     return value(*settingPatternShowCurveDetails, false).toBool();
1154 }
1155 
1156 //---------------------------------------------------------------------------------------------------------------------
SetShowCurveDetails(bool value)1157 void VCommonSettings::SetShowCurveDetails(bool value)
1158 {
1159     setValue(*settingPatternShowCurveDetails, value);
1160 }
1161 
1162 //---------------------------------------------------------------------------------------------------------------------
IsPieceShowMainPath() const1163 bool VCommonSettings::IsPieceShowMainPath() const
1164 {
1165     if (pieceShowMainPath < 0)
1166     {
1167         pieceShowMainPath = value(*settingPatternPieceShowMainPath, 0).toInt();
1168     }
1169     return pieceShowMainPath;
1170 }
1171 
1172 //---------------------------------------------------------------------------------------------------------------------
SetPieceShowMainPath(bool value)1173 void VCommonSettings::SetPieceShowMainPath(bool value)
1174 {
1175     pieceShowMainPath = value;
1176     setValue(*settingPatternPieceShowMainPath, pieceShowMainPath);
1177 }
1178 
1179 //---------------------------------------------------------------------------------------------------------------------
IsDontUseNativeDialog() const1180 bool VCommonSettings::IsDontUseNativeDialog() const
1181 {
1182     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
1183     return settings.value(*settingConfigurationDontUseNativeDialog, false).toBool();
1184 }
1185 
1186 //---------------------------------------------------------------------------------------------------------------------
SetDontUseNativeDialog(bool value)1187 void VCommonSettings::SetDontUseNativeDialog(bool value)
1188 {
1189     QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename);
1190     settings.setValue(*settingConfigurationDontUseNativeDialog, value);
1191     settings.sync();
1192 }
1193 
1194 //---------------------------------------------------------------------------------------------------------------------
GetLineWidth() const1195 qreal VCommonSettings::GetLineWidth() const
1196 {
1197     if (lineWidthCached <= 0)
1198     {
1199         lineWidthCached = qBound(VCommonSettings::MinimalLineWidth(), value(*settingPatternLineWidth, 1.2).toDouble(),
1200                                  VCommonSettings::MaximalLineWidth());
1201     }
1202 
1203     return lineWidthCached;
1204 }
1205 
1206 //---------------------------------------------------------------------------------------------------------------------
SetLineWidth(qreal width)1207 void VCommonSettings::SetLineWidth(qreal width)
1208 {
1209     lineWidthCached = qBound(VCommonSettings::MinimalLineWidth(), width, VCommonSettings::MaximalLineWidth());
1210     setValue(*settingPatternLineWidth, lineWidthCached);
1211 }
1212 
1213 //---------------------------------------------------------------------------------------------------------------------
WidthMainLine() const1214 qreal VCommonSettings::WidthMainLine() const
1215 {
1216     return GetLineWidth() / 25.4 * PrintDPI;
1217 }
1218 
1219 //---------------------------------------------------------------------------------------------------------------------
WidthHairLine() const1220 qreal VCommonSettings::WidthHairLine() const
1221 {
1222     return WidthMainLine()/3.0;
1223 }
1224 
1225 //---------------------------------------------------------------------------------------------------------------------
1226 /**
1227  * @brief GetTiledPDFMargins returns the tiled pdf margins in the given unit. When the setting is
1228  * called for the first time, the 4 default margins are 10mm.
1229  * @param unit the unit in which are the value. Necessary because we save the values
1230  * internaly as mm so there is conversion beeing made.
1231  * @return tiled pdf margins
1232  */
GetTiledPDFMargins(const Unit & unit) const1233 auto VCommonSettings::GetTiledPDFMargins(const Unit &unit) const -> QMarginsF
1234 {
1235     // default value is 10mm. We save the margins in mm in the setting.
1236     return UnitConvertor(ValueOrDef<QMarginsF>(*settingTiledPDFMargins, QMarginsF(10, 10, 10, 10)), Unit::Mm, unit);
1237 }
1238 
1239 //---------------------------------------------------------------------------------------------------------------------
1240 /**
1241  * @brief SetTiledPDFMargins sets the setting tiled pdf margins to the given value.
1242  * @param value the margins to save
1243  * @param unit the unit in which are the value. Necessary because we save the values
1244  * internaly as mm so there is conversion beeing made.
1245  */
SetTiledPDFMargins(const QMarginsF & value,const Unit & unit)1246 void VCommonSettings::SetTiledPDFMargins(const QMarginsF &value, const Unit &unit)
1247 {
1248     setValue(*settingTiledPDFMargins, QVariant::fromValue(UnitConvertor(value, unit, Unit::Mm)));
1249 }
1250 
1251 //---------------------------------------------------------------------------------------------------------------------
GetTiledPDFOrientation() const1252 auto VCommonSettings::GetTiledPDFOrientation() const -> PageOrientation
1253 {
1254     bool defaultValue = static_cast<bool>(PageOrientation::Portrait);
1255     bool result = value(*settingTiledPDFOrientation, defaultValue).toBool();
1256     return static_cast<PageOrientation>(result);
1257 }
1258 
1259 //---------------------------------------------------------------------------------------------------------------------
SetTiledPDFOrientation(PageOrientation value)1260 void VCommonSettings::SetTiledPDFOrientation(PageOrientation value)
1261 {
1262     setValue(*settingTiledPDFOrientation, static_cast<bool> (value));
1263 }
1264