Seamly2D
Code documentation
dialogcubicbezier.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 dialogcubicbezier.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 10 3, 2016
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) 2016 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 "dialogcubicbezier.h"
53 
54 #include <QColor>
55 #include <QComboBox>
56 #include <QLabel>
57 #include <QLineEdit>
58 #include <QPointer>
59 #include <new>
60 
61 #include "../../tools/vabstracttool.h"
62 #include "../../visualization/path/vistoolcubicbezier.h"
63 #include "../../visualization/visualization.h"
64 #include "../vgeometry/vpointf.h"
65 #include "../vpatterndb/vcontainer.h"
66 #include "dialogtool.h"
67 #include "ui_dialogcubicbezier.h"
68 
69 //---------------------------------------------------------------------------------------------------------------------
70 DialogCubicBezier::DialogCubicBezier(const VContainer *data, const quint32 &toolId, QWidget *parent)
71  : DialogTool(data, toolId, parent)
72  , ui(new Ui::DialogCubicBezier)
73  , spl()
74  , newDuplicate(-1)
75 {
76  ui->setupUi(this);
77  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
78  setWindowIcon(QIcon(":/toolicon/32x32/cubic_bezier.png"));
79 
81 
82  FillComboBoxPoints(ui->comboBoxP1);
83  FillComboBoxPoints(ui->comboBoxP2);
84  FillComboBoxPoints(ui->comboBoxP3);
85  FillComboBoxPoints(ui->comboBoxP4);
86 
87  int index = ui->lineType_ComboBox->findData(LineTypeNone);
88  if (index != -1)
89  {
90  ui->lineType_ComboBox->removeItem(index);
91  }
92 
93  index = ui->lineColor_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineColor());
94  if (index != -1)
95  {
96  ui->lineColor_ComboBox->setCurrentIndex(index);
97  }
98 
99  index = ui->lineWeight_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineWeight());
100  if (index != -1)
101  {
102  ui->lineWeight_ComboBox->setCurrentIndex(index);
103  }
104 
105  index = ui->lineType_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineType());
106  if (index != -1)
107  {
108  ui->lineType_ComboBox->setCurrentIndex(index);
109  }
110 
112 
113  connect(ui->comboBoxP1, &QComboBox::currentTextChanged, this, &DialogCubicBezier::PointNameChanged);
114  connect(ui->comboBoxP2, &QComboBox::currentTextChanged, this, &DialogCubicBezier::PointNameChanged);
115  connect(ui->comboBoxP3, &QComboBox::currentTextChanged, this, &DialogCubicBezier::PointNameChanged);
116  connect(ui->comboBoxP4, &QComboBox::currentTextChanged, this, &DialogCubicBezier::PointNameChanged);
117 
118  vis = new VisToolCubicBezier(data);
119 }
120 
121 //---------------------------------------------------------------------------------------------------------------------
123 {
124  delete ui;
125 }
126 
127 //---------------------------------------------------------------------------------------------------------------------
129 {
130  return spl;
131 }
132 
133 //---------------------------------------------------------------------------------------------------------------------
135 {
136  spl = spline;
137 
138  setCurrentPointId(ui->comboBoxP1, spl.GetP1().id());
139  setCurrentPointId(ui->comboBoxP2, spl.GetP2().id());
140  setCurrentPointId(ui->comboBoxP3, spl.GetP3().id());
141  setCurrentPointId(ui->comboBoxP4, spl.GetP4().id());
142 
143  ui->lineEditSplineName->setText(qApp->TrVars()->VarToUser(spl.name()));
144 
145  auto path = qobject_cast<VisToolCubicBezier *>(vis);
146  SCASSERT(path != nullptr)
147 
148  path->setObject1Id(spl.GetP1().id());
149  path->setObject2Id(spl.GetP2().id());
150  path->setObject3Id(spl.GetP3().id());
151  path->setObject4Id(spl.GetP4().id());
152 }
153 
154 //---------------------------------------------------------------------------------------------------------------------
156 {
157  return GetComboBoxCurrentData(ui->lineType_ComboBox, LineTypeSolidLine);
158 }
159 
160 //---------------------------------------------------------------------------------------------------------------------
161 void DialogCubicBezier::setPenStyle(const QString &value)
162 {
163  ChangeCurrentData(ui->lineType_ComboBox, value);
164 }
165 
166 //---------------------------------------------------------------------------------------------------------------------
167 /**
168  * @brief getLineWeight return weight of the lines
169  * @return type
170  */
172 {
173  return GetComboBoxCurrentData(ui->lineWeight_ComboBox, "0.35");
174 }
175 
176 //---------------------------------------------------------------------------------------------------------------------
177 /**
178  * @brief setLineWeight set weight of the lines
179  * @param value type
180  */
181 void DialogCubicBezier::setLineWeight(const QString &value)
182 {
183  ChangeCurrentData(ui->lineWeight_ComboBox, value);
184 }
185 
186 //---------------------------------------------------------------------------------------------------------------------
188 {
189  return GetComboBoxCurrentData(ui->lineColor_ComboBox, ColorBlack);
190 }
191 
192 //---------------------------------------------------------------------------------------------------------------------
193 void DialogCubicBezier::setLineColor(const QString &value)
194 {
195  ChangeCurrentData(ui->lineColor_ComboBox, value);
196 }
197 
198 //---------------------------------------------------------------------------------------------------------------------
199 void DialogCubicBezier::ChosenObject(quint32 id, const SceneObject &type)
200 {
201  if (prepare == false)// After first choose we ignore all objects
202  {
203  if (type == SceneObject::Point)
204  {
205  auto *path = qobject_cast<VisToolCubicBezier *>(vis);
206  SCASSERT(path != nullptr)
207 
208  switch (number)
209  {
210  case 0:
211  if (SetObject(id, ui->comboBoxP1, tr("Select the second point of curve")))
212  {
213  ++number;
214  path->VisualMode(id);
215  }
216  break;
217  case 1:
218  if (SetObject(id, ui->comboBoxP2, tr("Select the third point of curve")))
219  {
220  ++number;
221 
222  path->setObject2Id(id);
223  path->RefreshGeometry();
224  }
225  break;
226  case 2:
227  if (SetObject(id, ui->comboBoxP3, tr("Select the fourth point of curve")))
228  {
229  ++number;
230 
231  path->setObject3Id(id);
232  path->RefreshGeometry();
233  }
234  break;
235  case 3:
236  if (getCurrentObjectId(ui->comboBoxP1) != id)
237  {
238  if (SetObject(id, ui->comboBoxP4, ""))
239  {
240  ++number;
241 
242  path->setObject4Id(id);
243  path->RefreshGeometry();
244  prepare = true;
245  DialogAccepted();
246  }
247  }
248  break;
249  default:
250  break;
251  }
252  }
253  }
254 }
255 
256 //---------------------------------------------------------------------------------------------------------------------
258 {
259  QColor color = okColor;
260  if (getCurrentObjectId(ui->comboBoxP1) == getCurrentObjectId(ui->comboBoxP4))
261  {
262  flagError = false;
263  color = errorColor;
264 
265  ui->lineEditSplineName->setText(tr("Invalid spline"));
266  }
267  else
268  {
269  flagError = true;
270  color = okColor;
271 
272  if (getCurrentObjectId(ui->comboBoxP1) == spl.GetP1().id() &&
273  getCurrentObjectId(ui->comboBoxP4) == spl.GetP4().id())
274  {
275  newDuplicate = -1;
276  ui->lineEditSplineName->setText(qApp->TrVars()->VarToUser(spl.name()));
277  }
278  else
279  {
280  VCubicBezier spline(*GetP1(), *GetP2(), *GetP3(), *GetP4());
281 
282  if (not data->IsUnique(spline.name()))
283  {
284  newDuplicate = static_cast<qint32>(DNumber(spline.name()));
285  spline.SetDuplicate(static_cast<quint32>(newDuplicate));
286  }
287  ui->lineEditSplineName->setText(qApp->TrVars()->VarToUser(spline.name()));
288  }
289  }
290  ChangeColor(ui->labelName, color);
291  ChangeColor(ui->labelFirstPoint, color);
292  ChangeColor(ui->labelSecondPoint, color);
293  ChangeColor(ui->labelThirdPoint, color);
294  ChangeColor(ui->labelForthPoint, color);
295  CheckState();
296 }
297 
298 //---------------------------------------------------------------------------------------------------------------------
300 {
301  AddVisualization<VisToolCubicBezier>();
302 }
303 
304 //---------------------------------------------------------------------------------------------------------------------
306 {
307  const auto p1 = GetP1();
308  const auto p2 = GetP2();
309  const auto p3 = GetP3();
310  const auto p4 = GetP4();
311 
312  spl = VCubicBezier(*p1, *p2, *p3, *p4);
313 
314  const quint32 d = spl.GetDuplicate();//Save previous value
315  newDuplicate <= -1 ? spl.SetDuplicate(d) : spl.SetDuplicate(static_cast<quint32>(newDuplicate));
316 
317  auto path = qobject_cast<VisToolCubicBezier *>(vis);
318  SCASSERT(path != nullptr)
319 
320  path->setObject1Id(p1->id());
321  path->setObject2Id(p2->id());
322  path->setObject3Id(p3->id());
323  path->setObject4Id(p4->id());
324  path->SetMode(Mode::Show);
325  path->RefreshGeometry();
326 }
327 
328 //---------------------------------------------------------------------------------------------------------------------
330 {
331  return data->GeometricObject<VPointF>(getCurrentObjectId(ui->comboBoxP1));
332 }
333 
334 //---------------------------------------------------------------------------------------------------------------------
336 {
337  return data->GeometricObject<VPointF>(getCurrentObjectId(ui->comboBoxP2));
338 }
339 
340 //---------------------------------------------------------------------------------------------------------------------
342 {
343  return data->GeometricObject<VPointF>(getCurrentObjectId(ui->comboBoxP3));
344 }
345 
346 //---------------------------------------------------------------------------------------------------------------------
348 {
349  return data->GeometricObject<VPointF>(getCurrentObjectId(ui->comboBoxP4));
350 }
const QSharedPointer< VPointF > GetP3() const
virtual void ShowVisualization() Q_DECL_OVERRIDE
const QSharedPointer< VPointF > GetP4() const
void SetSpline(const VCubicBezier &spline)
const QSharedPointer< VPointF > GetP2() const
void setLineWeight(const QString &value)
setLineWeight set weight of the lines
void setPenStyle(const QString &value)
VCubicBezier GetSpline() const
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
QString getLineColor() const
DialogCubicBezier(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
QString getPenStyle() const
const QSharedPointer< VPointF > GetP1() const
VCubicBezier spl
spl spline
Ui::DialogCubicBezier * ui
void setLineColor(const QString &value)
virtual void PointNameChanged() Q_DECL_OVERRIDE
virtual void ChosenObject(quint32 id, const SceneObject &type) Q_DECL_OVERRIDE
QString getLineWeight() const
getLineWeight return weight of the lines
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
void ChangeCurrentData(QComboBox *box, const QVariant &value) const
ChangeCurrentData select item in combobox by id.
Definition: dialogtool.cpp:419
quint32 DNumber(const QString &baseName) const
Definition: dialogtool.cpp:476
const QColor okColor
Definition: dialogtool.h:219
virtual void CheckState()
CheckState enable, when all is correct, or disable, when something wrong, button ok.
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
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.
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
QString GetComboBoxCurrentData(const QComboBox *box, const QString &def) const
Definition: dialogtool.cpp:400
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
void SetDuplicate(quint32 number)
quint32 GetDuplicate() const
The VContainer class container of all variables.
Definition: vcontainer.h:141
static bool IsUnique(const QString &name)
Definition: vcontainer.cpp:585
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
virtual VPointF GetP1() const Q_DECL_OVERRIDE
virtual VPointF GetP3() const Q_DECL_OVERRIDE
virtual VPointF GetP2() const Q_DECL_OVERRIDE
virtual VPointF GetP4() const Q_DECL_OVERRIDE
virtual QString name() const
name return name graphical object.
Definition: vgobject.cpp:148
quint32 id() const
id return id object.
Definition: vgobject.cpp:205
The VPointF class keep data of point.
Definition: vpointf.h:75
#define SCASSERT(cond)
Definition: def.h:317
SceneObject
Definition: def.h:103
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
const QString ColorBlack
Definition: ifcdef.cpp:373
const QString LineTypeNone
Definition: ifcdef.cpp:158
#define qApp
Definition: vapplication.h:67