Seamly2D
Code documentation
dialogtruedarts.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 dialogtruedarts.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 12 6, 2015
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) 2015 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 "dialogtruedarts.h"
53 
54 #include <QColor>
55 #include <QComboBox>
56 #include <QLabel>
57 #include <QLineEdit>
58 #include <QPointer>
59 #include <QPushButton>
60 #include <QRegularExpression>
61 #include <QRegularExpressionMatch>
62 #include <QSet>
63 #include <Qt>
64 
65 #include "../../visualization/visualization.h"
66 #include "../../visualization/line/vistooltruedarts.h"
67 #include "../ifc/xml/vabstractpattern.h"
68 #include "../qmuparser/qmudef.h"
69 #include "../vgeometry/../ifc/ifcdef.h"
70 #include "../vmisc/vabstractapplication.h"
71 #include "../vpatterndb/vcontainer.h"
72 #include "dialogtool.h"
73 #include "ui_dialogtruedarts.h"
74 
75 //---------------------------------------------------------------------------------------------------------------------
76 DialogTrueDarts::DialogTrueDarts(const VContainer *data, const quint32 &toolId, QWidget *parent)
77  : DialogTool(data, toolId, parent)
78  , ui(new Ui::DialogTrueDarts)
79  , d1PointName()
80  , d2PointName()
81  , ch1(NULL_ID)
82  , ch2(NULL_ID)
83  , flagName1(true)
84  , flagName2(true)
85 {
86  ui->setupUi(this);
87  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
88  setWindowIcon(QIcon(":/toolicon/32x32/true_darts.png"));
89 
90  ui->lineEditFirstNewDartPoint->setClearButtonEnabled(true);
91  ui->lineEditSecondNewDartPoint->setClearButtonEnabled(true);
92 
93  const QString name1 = qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel);
94  const QString name2 = qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel, name1);
95  ui->lineEditFirstNewDartPoint->setText(name1);
96  ui->lineEditSecondNewDartPoint->setText(name2);
97 
99  CheckState();
100 
102 
103  connect(ui->lineEditFirstNewDartPoint, &QLineEdit::textChanged, this, &DialogTrueDarts::NameDartPoint1Changed);
104  connect(ui->lineEditSecondNewDartPoint, &QLineEdit::textChanged, this, &DialogTrueDarts::NameDartPoint2Changed);
105  connect(ui->comboBoxFirstBasePoint, &QComboBox::currentTextChanged, this, &DialogTrueDarts::PointNameChanged);
106  connect(ui->comboBoxSecondBasePoint, &QComboBox::currentTextChanged, this, &DialogTrueDarts::PointNameChanged);
107  connect(ui->comboBoxFirstDartPoint, &QComboBox::currentTextChanged, this, &DialogTrueDarts::PointNameChanged);
108  connect(ui->comboBoxSecondDartPoint, &QComboBox::currentTextChanged, this, &DialogTrueDarts::PointNameChanged);
109  connect(ui->comboBoxThirdDartPoint, &QComboBox::currentTextChanged, this, &DialogTrueDarts::PointNameChanged);
110 
111  vis = new VisToolTrueDarts(data);
112 }
113 
114 //---------------------------------------------------------------------------------------------------------------------
116 {
117  delete ui;
118 }
119 
120 //---------------------------------------------------------------------------------------------------------------------
122 {
123  return d1PointName;
124 }
125 
126 //---------------------------------------------------------------------------------------------------------------------
128 {
129  return d2PointName;
130 }
131 
132 //---------------------------------------------------------------------------------------------------------------------
133 void DialogTrueDarts::SetNewDartPointNames(const QString &firstPoint, const QString &secondPoint)
134 {
135  ui->lineEditFirstNewDartPoint->blockSignals(true);
136  ui->lineEditSecondNewDartPoint->blockSignals(true);
137 
138  d1PointName = firstPoint;
139  ui->lineEditFirstNewDartPoint->setText(d1PointName);
140 
141  d2PointName = secondPoint;
142  ui->lineEditSecondNewDartPoint->setText(d2PointName);
143 
144  ui->lineEditSecondNewDartPoint->blockSignals(false);
145  ui->lineEditFirstNewDartPoint->blockSignals(false);
146 
147  CheckName(ui->lineEditFirstNewDartPoint, ui->labelFirstNewDartPoint, d1PointName, d2PointName,
148  ui->lineEditSecondNewDartPoint, flagName1);
149  CheckName(ui->lineEditSecondNewDartPoint, ui->labelSecondNewDartPoint, d1PointName, d2PointName,
150  ui->lineEditFirstNewDartPoint, flagName2);
151 }
152 
153 //---------------------------------------------------------------------------------------------------------------------
155 {
156  return getCurrentObjectId(ui->comboBoxFirstBasePoint);
157 }
158 
159 //---------------------------------------------------------------------------------------------------------------------
160 void DialogTrueDarts::SetFirstBasePointId(const quint32 &value)
161 {
162  setCurrentPointId(ui->comboBoxFirstBasePoint, value, FillComboBox::NoChildren, ch1, ch2);
163 
164  VisToolTrueDarts *points = qobject_cast<VisToolTrueDarts *>(vis);
165  SCASSERT(points != nullptr)
166  points->setObject1Id(value);
167 }
168 
169 //---------------------------------------------------------------------------------------------------------------------
171 {
172  return getCurrentObjectId(ui->comboBoxSecondBasePoint);
173 }
174 
175 //---------------------------------------------------------------------------------------------------------------------
176 void DialogTrueDarts::SetSecondBasePointId(const quint32 &value)
177 {
178  setCurrentPointId(ui->comboBoxSecondBasePoint, value, FillComboBox::NoChildren, ch1, ch2);
179 
180  VisToolTrueDarts *points = qobject_cast<VisToolTrueDarts *>(vis);
181  SCASSERT(points != nullptr)
182  points->setObject2Id(value);
183 }
184 
185 //---------------------------------------------------------------------------------------------------------------------
187 {
188  return getCurrentObjectId(ui->comboBoxFirstDartPoint);
189 }
190 
191 //---------------------------------------------------------------------------------------------------------------------
192 void DialogTrueDarts::SetFirstDartPointId(const quint32 &value)
193 {
194  setCurrentPointId(ui->comboBoxFirstDartPoint, value, FillComboBox::NoChildren, ch1, ch2);
195 
196  VisToolTrueDarts *points = qobject_cast<VisToolTrueDarts *>(vis);
197  SCASSERT(points != nullptr)
198  points->setD1PointId(value);
199 }
200 
201 //---------------------------------------------------------------------------------------------------------------------
203 {
204  return getCurrentObjectId(ui->comboBoxSecondDartPoint);
205 }
206 
207 //---------------------------------------------------------------------------------------------------------------------
208 void DialogTrueDarts::SetSecondDartPointId(const quint32 &value)
209 {
210  setCurrentPointId(ui->comboBoxSecondDartPoint, value, FillComboBox::NoChildren, ch1, ch2);
211 
212  VisToolTrueDarts *points = qobject_cast<VisToolTrueDarts *>(vis);
213  SCASSERT(points != nullptr)
214  points->setD2PointId(value);
215 }
216 
217 //---------------------------------------------------------------------------------------------------------------------
219 {
220  return getCurrentObjectId(ui->comboBoxThirdDartPoint);
221 }
222 
223 //---------------------------------------------------------------------------------------------------------------------
224 void DialogTrueDarts::SetThirdDartPointId(const quint32 &value)
225 {
226  setCurrentPointId(ui->comboBoxThirdDartPoint, value, FillComboBox::NoChildren, ch1, ch2);
227 
228  VisToolTrueDarts *points = qobject_cast<VisToolTrueDarts *>(vis);
229  SCASSERT(points != nullptr)
230  points->setD3PointId(value);
231 }
232 
233 //---------------------------------------------------------------------------------------------------------------------
234 void DialogTrueDarts::SetChildrenId(const quint32 &ch1, const quint32 &ch2)
235 {
236  this->ch1 = ch1;
237  this->ch2 = ch2;
239 }
240 
241 //---------------------------------------------------------------------------------------------------------------------
242 void DialogTrueDarts::ChosenObject(quint32 id, const SceneObject &type)
243 {
244  if (prepare == false)// After first choose we ignore all objects
245  {
246  if (type == SceneObject::Point)
247  {
248  VisToolTrueDarts *points = qobject_cast<VisToolTrueDarts *>(vis);
249  SCASSERT(points != nullptr)
250 
251  switch (number)
252  {
253  case 0:
254  if (SetObject(id, ui->comboBoxFirstBasePoint, tr("Select the second base point")))
255  {
256  number++;
257  points->VisualMode(id);
258  }
259  break;
260  case 1:
261  if (getCurrentObjectId(ui->comboBoxFirstBasePoint) != id)
262  {
263  if (SetObject(id, ui->comboBoxSecondBasePoint, tr("Select the first dart point")))
264  {
265  number++;
266  points->setObject2Id(id);
267  points->RefreshGeometry();
268  }
269  }
270  break;
271  case 2:
272  {
273  QSet<quint32> set;
274  set.insert(getCurrentObjectId(ui->comboBoxFirstBasePoint));
275  set.insert(getCurrentObjectId(ui->comboBoxSecondBasePoint));
276  set.insert(id);
277 
278  if (set.size() == 3)
279  {
280  if (SetObject(id, ui->comboBoxFirstDartPoint, tr("Select the second dart point")))
281  {
282  number++;
283  points->setD1PointId(id);
284  points->RefreshGeometry();
285  }
286  }
287  break;
288  }
289  case 3:
290  {
291  QSet<quint32> set;
292  set.insert(getCurrentObjectId(ui->comboBoxFirstBasePoint));
293  set.insert(getCurrentObjectId(ui->comboBoxSecondBasePoint));
294  set.insert(getCurrentObjectId(ui->comboBoxFirstDartPoint));
295  set.insert(id);
296 
297  if (set.size() == 4)
298  {
299  if (SetObject(id, ui->comboBoxSecondDartPoint, tr("Select the third dart point")))
300  {
301  number++;
302  points->setD2PointId(id);
303  points->RefreshGeometry();
304  }
305  }
306  break;
307  }
308  case 4:
309  {
310  QSet<quint32> set;
311  set.insert(getCurrentObjectId(ui->comboBoxFirstBasePoint));
312  set.insert(getCurrentObjectId(ui->comboBoxSecondBasePoint));
313  set.insert(getCurrentObjectId(ui->comboBoxFirstDartPoint));
314  set.insert(getCurrentObjectId(ui->comboBoxSecondDartPoint));
315  set.insert(id);
316 
317  if (set.size() == 5)
318  {
319  if (SetObject(id, ui->comboBoxThirdDartPoint, ""))
320  {
321  points->setD3PointId(id);
322  points->RefreshGeometry();
323  prepare = true;
324  DialogAccepted();
325  }
326  }
327  break;
328  }
329  default:
330  break;
331  }
332  }
333  }
334 }
335 
336 //---------------------------------------------------------------------------------------------------------------------
338 {
339  QSet<quint32> set;
340  set.insert(getCurrentObjectId(ui->comboBoxFirstBasePoint));
341  set.insert(getCurrentObjectId(ui->comboBoxSecondBasePoint));
342  set.insert(getCurrentObjectId(ui->comboBoxFirstDartPoint));
343  set.insert(getCurrentObjectId(ui->comboBoxSecondDartPoint));
344  set.insert(getCurrentObjectId(ui->comboBoxThirdDartPoint));
345 
346  QColor color = okColor;
347  if (set.size() != 5)
348  {
349  flagError = false;
350  color = errorColor;
351  }
352  else
353  {
354  flagError = true;
355  color = okColor;
356  }
357  ChangeColor(ui->labelFirstBasePoint, color);
358  ChangeColor(ui->labelSecondBasePoint, color);
359  ChangeColor(ui->labelFirstDartPoint, color);
360  ChangeColor(ui->labelSecondDartPoint, color);
361  ChangeColor(ui->labelThirdDartPoint, color);
362  CheckState();
363 }
364 
365 //---------------------------------------------------------------------------------------------------------------------
367 {
368  NameChanged(ui->labelFirstNewDartPoint, d1PointName, d2PointName, ui->lineEditSecondNewDartPoint, flagName1);
369 }
370 
371 //---------------------------------------------------------------------------------------------------------------------
373 {
374  NameChanged(ui->labelSecondNewDartPoint, d1PointName, d2PointName, ui->lineEditFirstNewDartPoint, flagName2);
375 }
376 
377 //---------------------------------------------------------------------------------------------------------------------
379 {
380  AddVisualization<VisToolTrueDarts>();
381 }
382 
383 //---------------------------------------------------------------------------------------------------------------------
385 {
386  d1PointName = ui->lineEditFirstNewDartPoint->text();
387  d2PointName = ui->lineEditSecondNewDartPoint->text();
388 
389  VisToolTrueDarts *points = qobject_cast<VisToolTrueDarts *>(vis);
390  SCASSERT(points != nullptr)
391 
397  points->RefreshGeometry();
398 }
399 
400 //---------------------------------------------------------------------------------------------------------------------
402 {
403  SCASSERT(ok_Button != nullptr)
404  ok_Button->setEnabled(flagName1 && flagName2 && flagError);
405  // In case dialog hasn't apply button
406  if (apply_Button != nullptr)
407  {
408  apply_Button->setEnabled(ok_Button->isEnabled());
409  }
410 }
411 
412 //---------------------------------------------------------------------------------------------------------------------
413 void DialogTrueDarts::NameChanged(QLabel *labelEditNamePoint, const QString &pointD1Name, const QString &pointD2Name,
414  QLineEdit* secondPointName, bool &flagName)
415 {
416  SCASSERT(labelEditNamePoint != nullptr)
417  SCASSERT(secondPointName != nullptr)
418  QLineEdit* edit = qobject_cast<QLineEdit*>(sender());
419  if (edit)
420  {
421  CheckName(edit, labelEditNamePoint, pointD1Name, pointD2Name, secondPointName, flagName);
422  }
423  CheckState();
424 }
425 
426 //---------------------------------------------------------------------------------------------------------------------
427 void DialogTrueDarts::FillComboBoxs(const quint32 &ch1, const quint32 &ch2)
428 {
429  FillComboBoxPoints(ui->comboBoxFirstBasePoint, FillComboBox::NoChildren, ch1, ch2);
430  FillComboBoxPoints(ui->comboBoxSecondBasePoint, FillComboBox::NoChildren, ch1, ch2);
431  FillComboBoxPoints(ui->comboBoxFirstDartPoint, FillComboBox::NoChildren, ch1, ch2);
432  FillComboBoxPoints(ui->comboBoxSecondDartPoint, FillComboBox::NoChildren, ch1, ch2);
433  FillComboBoxPoints(ui->comboBoxThirdDartPoint, FillComboBox::NoChildren, ch1, ch2);
434 }
435 
436 //---------------------------------------------------------------------------------------------------------------------
437 void DialogTrueDarts::CheckName(QLineEdit *edit, QLabel *labelEditNamePoint, const QString &pointD1Name,
438  const QString &pointD2Name, QLineEdit *secondPointName, bool &flagName)
439 {
440  SCASSERT(labelEditNamePoint != nullptr)
441  SCASSERT(secondPointName != nullptr)
442  SCASSERT(edit != nullptr)
443 
444  const QString name = edit->text();
445  const QString secondName = secondPointName->text();
446  QRegularExpression rx(NameRegExp());
447  if (name.isEmpty()
448  || secondName == name
449  || (pointD1Name != name && pointD2Name != name && data->IsUnique(name) == false)
450  || rx.match(name).hasMatch() == false)
451  {
452  flagName = false;
454  }
455  else
456  {
457  flagName = true;
459  }
460 }
The DialogTool class parent for all dialog of tools.
Definition: dialogtool.h:107
void setCurrentPointId(QComboBox *box, const quint32 &value, FillComboBox rule=FillComboBox::NoChildren, const quint32 &ch1=NULL_ID, const quint32 &ch2=NULL_ID) const
Definition: dialogtool.cpp:896
const QColor okColor
Definition: dialogtool.h:219
void FillComboBoxPoints(QComboBox *box, FillComboBox rule=FillComboBox::Whole, const quint32 &ch1=NULL_ID, const quint32 &ch2=NULL_ID) const
FillComboBoxPoints fill comboBox list of points.
Definition: dialogtool.cpp:242
bool flagName
flagName true if name is correct
Definition: dialogtool.h:183
QPushButton * ok_Button
ok_Button button ok
Definition: dialogtool.h:199
const QColor errorColor
Definition: dialogtool.h:220
qint32 number
number number of handled objects
Definition: dialogtool.h:234
bool SetObject(const quint32 &id, QComboBox *box, const QString &toolTip)
Definition: dialogtool.cpp:974
virtual void DialogAccepted()
DialogAccepted save data and emit signal about closed dialog.
QPushButton * apply_Button
apply_Button button apply
Definition: dialogtool.h:202
QLabel * labelEditNamePoint
labelEditNamePoint label used when need show wrong name of point
Definition: dialogtool.h:214
void initializeOkCancelApply(T *ui)
initializeOkCancelApply initialize OK / Cancel and Apply buttons
Definition: dialogtool.h:365
bool flagError
flagError use this flag if for you do not enought
Definition: dialogtool.h:193
const VContainer * data
data container with data
Definition: dialogtool.h:177
bool prepare
prepare show if we prepare. Show dialog after finish working with visual part of tool
Definition: dialogtool.h:228
QPointer< Visualization > vis
Definition: dialogtool.h:236
void ChangeColor(QWidget *widget, const QColor &color)
quint32 getCurrentObjectId(QComboBox *box) const
getCurrentPointId return current point id stored in combobox
Definition: dialogtool.cpp:959
QString GetSecondNewDartPointName()
void SetSecondDartPointId(const quint32 &value)
void FillComboBoxs(const quint32 &ch1, const quint32 &ch2)
DialogTrueDarts(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
quint32 GetSecondDartPointId() const
virtual void ShowVisualization() Q_DECL_OVERRIDE
QString GetFirstNewDartPointName()
void CheckName(QLineEdit *edit, QLabel *labelEditNamePoint, const QString &pointD1Name, const QString &pointD2Name, QLineEdit *secondPointName, bool &flagName)
void SetNewDartPointNames(const QString &firstPoint, const QString &secondPoint)
void SetFirstDartPointId(const quint32 &value)
void SetSecondBasePointId(const quint32 &value)
void SetFirstBasePointId(const quint32 &value)
void SetThirdDartPointId(const quint32 &value)
quint32 GetFirstBasePointId() const
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
void NameChanged(QLabel *labelEditNamePoint, const QString &pointD1Name, const QString &pointD2Name, QLineEdit *secondPointName, bool &flagName)
quint32 GetThirdDartPointId() const
Ui::DialogTrueDarts * ui
virtual void PointNameChanged() Q_DECL_OVERRIDE
void SetChildrenId(const quint32 &ch1, const quint32 &ch2)
virtual void ChosenObject(quint32 id, const SceneObject &type) Q_DECL_OVERRIDE
quint32 GetFirstDartPointId() const
quint32 GetSecondBasePointId() const
virtual void CheckState() Q_DECL_FINAL
CheckState enable, when all is correct, or disable, when something wrong, button ok.
The VContainer class container of all variables.
Definition: vcontainer.h:141
static bool IsUnique(const QString &name)
Definition: vcontainer.cpp:585
void setD3PointId(const quint32 &value)
void setD1PointId(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setObject2Id(const quint32 &value)
void setD2PointId(const quint32 &value)
virtual void VisualMode(const quint32 &pointId)
void setObject1Id(const quint32 &value)
#define SCASSERT(cond)
Definition: def.h:317
SceneObject
Definition: def.h:103
#define NULL_ID
Definition: ifcdef.h:76
QString NameRegExp()
Definition: qmudef.cpp:281
#define qApp
Definition: vapplication.h:67