Seamly2D
Code documentation
preferencespatternpage.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 preferencespatternpage.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 12 4, 2017
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2017 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D 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  ** Seamly2D 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 Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  **************************************************************************/
51 
52 #include "preferencespatternpage.h"
53 #include "ui_preferencespatternpage.h"
54 
55 #include "../dialogdatetimeformats.h"
56 #include "../ifc/xml/vabstractpattern.h"
57 #include "../../core/vapplication.h"
58 
59 #include <QComboBox>
60 #include <QDate>
61 #include <QFileDialog>
62 #include <QMessageBox>
63 #include <QTime>
64 
65 namespace
66 {
67 QStringList initAllStringsComboBox(QComboBox *combo)
68 {
69  SCASSERT(combo != nullptr)
70 
71  QStringList itemsInComboBox;
72  for (int index = 0; index < combo->count(); ++index)
73  {
74  itemsInComboBox << combo->itemText(index);
75  }
76 
77  return itemsInComboBox;
78 }
79 }
80 
81 //---------------------------------------------------------------------------------------------------------------------
83  : QWidget(parent),
84  ui(new Ui::PreferencesPatternPage)
85 {
86  ui->setupUi(this);
87  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
88 
91  initNotches();
93 }
94 
95 //---------------------------------------------------------------------------------------------------------------------
97 {
98  delete ui;
99 }
100 
101 //---------------------------------------------------------------------------------------------------------------------
103 {
104  VSettings *settings = qApp->Seamly2DSettings();
105 
106  settings->SetDefaultSeamAllowance(ui->defaultSeamAllowance_DoubleSpinBox->value());
107  settings->setDefaultSeamColor(ui->defaultSeamColor_ComboBox->currentData().toString());
108  settings->setDefaultSeamLinetype(ui->defaultSeamLinetype_ComboBox->currentData().toString());
109  settings->setDefaultSeamLineweight(ui->defaultSeamLineweight_ComboBox->currentData().toReal());
110  settings->setDefaultCutColor(ui->defaultCutColor_ComboBox->currentData().toString());
111  settings->setDefaultCutLinetype(ui->defaultCutLinetype_ComboBox->currentData().toString());
112  settings->setDefaultCutLineweight(ui->defaultCutLineweight_ComboBox->currentData().toReal());
113 
114  settings->setDefaultInternalColor(ui->defaultInternalColor_ComboBox->currentData().toString());
115  settings->setDefaultInternalLinetype(ui->defaultInternalLinetype_ComboBox->currentData().toString());
116  settings->setDefaultInternalLineweight(ui->defaultInternalLineweight_ComboBox->currentData().toReal());
117 
118  settings->setDefaultCutoutColor(ui->defaultCutoutColor_ComboBox->currentData().toString());
119  settings->setDefaultCutoutLinetype(ui->defaultCutoutLinetype_ComboBox->currentData().toString());
120  settings->setDefaultCutoutLineweight(ui->defaultCutoutLineweight_ComboBox->currentData().toReal());
121 
122  settings->setForbidPieceFlipping(ui->forbidFlipping_CheckBox->isChecked());
123  settings->setHideSeamLine(ui->hideSeamLine_CheckBox->isChecked());
124 
125  settings->setDefaultNotchType(ui->defaultNotchType_ComboBox->currentData().toString());
126  settings->setDefaultNotchColor(ui->defaultNotchColor_ComboBox->currentData().toString());
127  settings->setDefaultNotchLength(ui->defaultNotchLength_DoubleSpinBox->value());
128  settings->setDefaultNotchWidth(ui->defaultNotchWidth_DoubleSpinBox->value());
129  settings->setShowSeamAllowanceNotch(ui->showSeamAllowanceNotch_CheckBox->isChecked());
130  settings->setShowSeamlineNotch(ui->showSeamlineNotch_CheckBox->isChecked());
131 
132  settings->setDefaultSeamAllowanceVisibilty(ui->showSeamAllowances_CheckBox->isChecked());
133  settings->setDefaultGrainlineVisibilty(ui->showGrainlines_CheckBox->isChecked());
134  settings->setDefaultGrainlineLength(ui->defaultGrainlineLength_DoubleSpinBox->value());
135  settings->setDefaultGrainlineColor(ui->defaultGrainlineColor_ComboBox->currentData().toString());
136  settings->setDefaultGrainlineLineweight(ui->defaultGrainlineLineweight_ComboBox->currentData().toReal());
137 
138 
139  settings->setShowPatternLabels(ui->showPatternLabels_CheckBox->isChecked());
140  settings->setShowPieceLabels(ui->showPieceLabels_CheckBox->isChecked());
141  settings->setDefaultLabelWidth(ui->defaultLabelWidth_DoubleSpinBox->value());
142  settings->setDefaultLabelHeight(ui->defaultLabelHeight_DoubleSpinBox->value());
143  settings->setDefaultLabelColor(ui->defaultLabelColor_ComboBox->currentData().toString());
144  settings->SetLabelDateFormat(ui->dateFormats_ComboBox->currentText());
145  settings->SetLabelTimeFormat(ui->timeFormats_ComboBox->currentText());
146 
147  settings->SetUserDefinedDateFormats(initAllStringsComboBox(ui->dateFormats_ComboBox));
148  settings->SetUserDefinedTimeFormats(initAllStringsComboBox(ui->timeFormats_ComboBox));
149 }
150 
151 //---------------------------------------------------------------------------------------------------------------------
153 {
154  ui->forbidFlipping_CheckBox->setChecked(qApp->Seamly2DSettings()->getForbidPieceFlipping());
155  ui->hideSeamLine_CheckBox->setChecked(qApp->Seamly2DSettings()->isHideSeamLine());
156  ui->showSeamAllowances_CheckBox->setChecked(qApp->Seamly2DSettings()->getDefaultSeamAllowanceVisibilty());
157  ui->defaultSeamAllowance_DoubleSpinBox->setValue(qApp->Seamly2DSettings()->GetDefaultSeamAllowance());
158  ui->defaultSeamAllowance_DoubleSpinBox->setSuffix(" " + UnitsToStr(StrToUnits(qApp->Seamly2DSettings()->GetUnit()), true));
159 
160  int index = ui->defaultSeamColor_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultSeamColor());
161  if (index != -1)
162  {
163  ui->defaultSeamColor_ComboBox->setCurrentIndex(index);
164  }
165  index = ui->defaultSeamLinetype_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultSeamLinetype());
166  if (index != -1)
167  {
168  ui->defaultSeamLinetype_ComboBox->setCurrentIndex(index);
169  }
170  index = ui->defaultSeamLineweight_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultSeamLineweight());
171  if (index != -1)
172  {
173  ui->defaultSeamLineweight_ComboBox->setCurrentIndex(index);
174  }
175 
176 
177  index = ui->defaultCutColor_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultCutColor());
178  if (index != -1)
179  {
180  ui->defaultCutColor_ComboBox->setCurrentIndex(index);
181  }
182  index = ui->defaultCutLinetype_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultCutLinetype());
183  if (index != -1)
184  {
185  ui->defaultCutLinetype_ComboBox->setCurrentIndex(index);
186  }
187  index = ui->defaultCutLineweight_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultCutLineweight());
188  if (index != -1)
189  {
190  ui->defaultCutLineweight_ComboBox->setCurrentIndex(index);
191  }
192 
193  index = ui->defaultInternalColor_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultInternalColor());
194  if (index != -1)
195  {
196  ui->defaultInternalColor_ComboBox->setCurrentIndex(index);
197  }
198  index = ui->defaultInternalLinetype_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultInternalLinetype());
199  if (index != -1)
200  {
201  ui->defaultInternalLinetype_ComboBox->setCurrentIndex(index);
202  }
203  index = ui->defaultInternalLineweight_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultInternalLineweight());
204  if (index != -1)
205  {
206  ui->defaultInternalLineweight_ComboBox->setCurrentIndex(index);
207  }
208 
209  index = ui->defaultCutoutColor_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultCutoutColor());
210  if (index != -1)
211  {
212  ui->defaultCutoutColor_ComboBox->setCurrentIndex(index);
213  }
214  index = ui->defaultCutoutLinetype_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultCutoutLinetype());
215  if (index != -1)
216  {
217  ui->defaultCutoutLinetype_ComboBox->setCurrentIndex(index);
218  }
219  index = ui->defaultCutoutLineweight_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultCutoutLineweight());
220  if (index != -1)
221  {
222  ui->defaultCutoutLineweight_ComboBox->setCurrentIndex(index);
223  }
224 }
225 
226 //---------------------------------------------------------------------------------------------------------------------
228 {
229  VSettings *settings = qApp->Seamly2DSettings();
230 
231  QPushButton *button = qobject_cast<QPushButton *>(sender());
232  if (button == ui->editDateFormats_PushButton)
233  {
234  callDateTimeFormatEditor(QDate::currentDate(), settings->PredefinedDateFormats(),
235  settings->GetUserDefinedDateFormats(), ui->dateFormats_ComboBox);
236  }
237  else if (button == ui->editTimeFormats_PushButton)
238  {
239  callDateTimeFormatEditor(QTime::currentTime(), settings->PredefinedTimeFormats(),
240  settings->GetUserDefinedTimeFormats(), ui->timeFormats_ComboBox);
241  }
242 }
243 
244 //---------------------------------------------------------------------------------------------------------------------
246 {
247  QToolButton *button = qobject_cast<QToolButton *>(sender());
248  VSettings *settings = qApp->Seamly2DSettings();
249 
250  QString filter(tr("Label template") + QLatin1String("(*.xml)"));
251  const QString fileName = QFileDialog::getOpenFileName(this, tr("Import template"),
252  settings->GetPathLabelTemplate(), filter, nullptr,
253  QFileDialog::DontUseNativeDialog);
254 
255 
256  if (button == ui->patternTemplate_ToolButton)
257  {
258  ui->patternTemplate_LineEdit->setText(fileName);
259  settings->setDefaultPatternTemplate(fileName);
260  }
261  else if (button == ui->pieceTemplate_ToolButton)
262  {
263  ui->pieceTemplate_LineEdit->setText(fileName);
264  settings->setDefaultPieceTemplate(fileName);
265  }
266 }
267 
268 //---------------------------------------------------------------------------------------------------------------------
270 {
271  VSettings *settings = qApp->Seamly2DSettings();
272 
273  ui->showPatternLabels_CheckBox->setChecked(qApp->Seamly2DSettings()->showPatternLabels());
274  ui->showPieceLabels_CheckBox->setChecked(qApp->Seamly2DSettings()->showPieceLabels());
275 
276  ui->defaultLabelWidth_DoubleSpinBox->setValue(qApp->Seamly2DSettings()->getDefaultLabelWidth());
277  ui->defaultLabelWidth_DoubleSpinBox->setSuffix(" " + UnitsToStr(StrToUnits(qApp->Seamly2DSettings()->GetUnit()), true));
278  ui->defaultLabelHeight_DoubleSpinBox->setValue(qApp->Seamly2DSettings()->getDefaultLabelHeight());
279  ui->defaultLabelHeight_DoubleSpinBox->setSuffix(" " + UnitsToStr(StrToUnits(qApp->Seamly2DSettings()->GetUnit()), true));
280 
281  int index = ui->defaultLabelColor_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultLabelColor());
282  if (index != -1)
283  {
284  ui->defaultLabelColor_ComboBox->setCurrentIndex(index);
285  }
286 
287  initComboBoxFormats(ui->dateFormats_ComboBox,
289  settings->GetLabelDateFormat());
290  initComboBoxFormats(ui->timeFormats_ComboBox,
292  settings->GetLabelTimeFormat());
293 
294  connect(ui->editDateFormats_PushButton, &QPushButton::clicked, this, &PreferencesPatternPage::editDateTimeFormats);
295  connect(ui->editTimeFormats_PushButton, &QPushButton::clicked, this, &PreferencesPatternPage::editDateTimeFormats);
296 
297  ui->patternTemplate_LineEdit->setText(settings->getDefaultPatternTemplate());
298  ui->pieceTemplate_LineEdit->setText(settings->getDefaultPieceTemplate());
299  connect(ui->patternTemplate_ToolButton, &QToolButton::clicked, this, &PreferencesPatternPage::setDefaultTemplate);
300  connect(ui->pieceTemplate_ToolButton, &QToolButton::clicked, this, &PreferencesPatternPage::setDefaultTemplate);
301 }
302 
303 //---------------------------------------------------------------------------------------------------------------------
305 {
306  ui->defaultNotchType_ComboBox->addItem(QIcon(), tr("Slit"), "slit");
307  ui->defaultNotchType_ComboBox->addItem(QIcon(), tr("T Notch"), "tNotch");
308  ui->defaultNotchType_ComboBox->addItem(QIcon(), tr("U Notch"), "uNotch");
309  ui->defaultNotchType_ComboBox->addItem(QIcon(), tr("V Internal"), "vInternal");
310  ui->defaultNotchType_ComboBox->addItem(QIcon(), tr("V External"), "vExternal");
311  ui->defaultNotchType_ComboBox->addItem(QIcon(), tr("Castle"), "castle");
312  ui->defaultNotchType_ComboBox->addItem(QIcon(), tr("Diamond"), "diamond");
313 
314  //----------------------- Get Default Notch
315  int index = ui->defaultNotchType_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultNotchType());
316  if (index != -1)
317  {
318  ui->defaultNotchType_ComboBox->setCurrentIndex(index);
319  }
320 
321  index = ui->defaultNotchColor_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultNotchColor());
322  if (index != -1)
323  {
324  ui->defaultNotchColor_ComboBox->setCurrentIndex(index);
325  }
326  ui->showSeamlineNotch_CheckBox->setChecked(qApp->Seamly2DSettings()->showSeamlineNotch());
327  ui->showSeamAllowanceNotch_CheckBox->setChecked(qApp->Seamly2DSettings()->showSeamAllowanceNotch());
328  ui->defaultNotchLength_DoubleSpinBox->setValue(qApp->Seamly2DSettings()->getDefaultNotchLength());
329  ui->defaultNotchLength_DoubleSpinBox->setSuffix(" " + UnitsToStr(StrToUnits(qApp->Seamly2DSettings()->GetUnit()), true));
330  ui->defaultNotchWidth_DoubleSpinBox->setValue(qApp->Seamly2DSettings()->getDefaultNotchWidth());
331  ui->defaultNotchWidth_DoubleSpinBox->setSuffix(" " + UnitsToStr(StrToUnits(qApp->Seamly2DSettings()->GetUnit()), true));
332 }
333 
334 //---------------------------------------------------------------------------------------------------------------------
336 {
337  ui->showGrainlines_CheckBox->setChecked(qApp->Seamly2DSettings()->getDefaultGrainlineVisibilty());
338 
339  ui->defaultGrainlineLength_DoubleSpinBox->setValue(qApp->Seamly2DSettings()->getDefaultGrainlineLength());
340  ui->defaultGrainlineLength_DoubleSpinBox->setSuffix(" " + UnitsToStr(StrToUnits(qApp->Seamly2DSettings()->GetUnit()), true));
341 
342  int index = ui->defaultGrainlineColor_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultGrainlineColor());
343  if (index != -1)
344  {
345  ui->defaultGrainlineColor_ComboBox->setCurrentIndex(index);
346  }
347  index = ui->defaultGrainlineLineweight_ComboBox->findData(qApp->Seamly2DSettings()->getDefaultGrainlineLineweight());
348  if (index != -1)
349  {
350  ui->defaultGrainlineLineweight_ComboBox->setCurrentIndex(index);
351  }
352 }
353 //---------------------------------------------------------------------------------------------------------------------
354 void PreferencesPatternPage::initComboBoxFormats(QComboBox *box, const QStringList &items, const QString &currentFormat)
355 {
356  SCASSERT(box != nullptr)
357 
358  box->addItems(items);
359  int index = box->findText(currentFormat);
360  if (index != -1)
361  {
362  box->setCurrentIndex(index);
363  }
364  else
365  {
366  box->setCurrentIndex(0);
367  }
368 }
369 
370 //---------------------------------------------------------------------------------------------------------------------
371 template <typename T>
372 void PreferencesPatternPage::callDateTimeFormatEditor(const T &type, const QStringList &predefinedFormats,
373  const QStringList &userDefinedFormats, QComboBox *box)
374 {
375  SCASSERT(box != nullptr)
376 
377  DialogDateTimeFormats dialog(type, predefinedFormats, userDefinedFormats);
378 
379  if (QDialog::Accepted == dialog.exec())
380  {
381  const QString currentFormat = box->currentText();
382  box->clear();
383  box->addItems(dialog.GetFormats());
384 
385  int index = box->findText(currentFormat);
386  if (index != -1)
387  {
388  box->setCurrentIndex(index);
389  }
390  else
391  {
392  box->setCurrentIndex(0);
393  }
394  }
395 }
QStringList GetFormats() const
void callDateTimeFormatEditor(const T &type, const QStringList &predefinedFormats, const QStringList &userDefinedFormats, QComboBox *box)
PreferencesPatternPage(QWidget *parent=nullptr)
void initComboBoxFormats(QComboBox *box, const QStringList &items, const QString &currentFormat)
Ui::PreferencesPatternPage * ui
void SetDefaultSeamAllowance(double value)
void setDefaultGrainlineLength(const qreal &value)
void setDefaultSeamLinetype(const QString &value)
void setDefaultGrainlineColor(const QString &value)
void setDefaultSeamLineweight(const qreal &value)
void setDefaultCutLinetype(const QString &value)
void setDefaultCutoutLinetype(const QString &value)
void setForbidPieceFlipping(bool value)
QString getDefaultPatternTemplate() const
void setDefaultCutColor(const QString &value)
void setDefaultInternalLinetype(const QString &value)
void setDefaultLabelHeight(const qreal &value)
QString GetLabelDateFormat() const
void SetLabelTimeFormat(const QString &format)
void setShowPieceLabels(const bool &value)
void setDefaultSeamColor(const QString &value)
void setDefaultInternalColor(const QString &value)
void setDefaultPieceTemplate(const QString &text)
void setDefaultCutoutColor(const QString &value)
void setDefaultPatternTemplate(const QString &text)
void setDefaultInternalLineweight(const qreal &value)
void setDefaultNotchColor(const QString &value)
void setDefaultCutoutLineweight(const qreal &value)
QStringList GetUserDefinedTimeFormats() const
void setShowSeamlineNotch(bool value)
QString GetPathLabelTemplate() const
void SetUserDefinedDateFormats(const QStringList &formats)
void setHideSeamLine(bool value)
void setDefaultSeamAllowanceVisibilty(const bool &value)
QString GetLabelTimeFormat() const
void setDefaultGrainlineLineweight(const qreal &value)
void setShowSeamAllowanceNotch(bool value)
void setDefaultLabelColor(const QString &value)
void setShowPatternLabels(const bool &value)
void setDefaultNotchWidth(const qreal &value)
static QStringList PredefinedTimeFormats()
QString getDefaultPieceTemplate() const
void SetLabelDateFormat(const QString &format)
void setDefaultLabelWidth(const qreal &value)
static QStringList PredefinedDateFormats()
QStringList GetUserDefinedDateFormats() const
void setDefaultGrainlineVisibilty(const bool &value)
void setDefaultNotchType(const QString &value)
void setDefaultNotchLength(const qreal &value)
void SetUserDefinedTimeFormats(const QStringList &formats)
void setDefaultCutLineweight(const qreal &value)
Unit StrToUnits(const QString &unit)
Definition: def.cpp:670
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
Definition: def.cpp:702
#define SCASSERT(cond)
Definition: def.h:317
#define qApp
Definition: vapplication.h:67