Seamly2D
Code documentation
dialoglineintersect.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 dialoglineintersect.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date November 15, 2013
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) 2013-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 "dialoglineintersect.h"
53 
54 #include <QColor>
55 #include <QComboBox>
56 #include <QLabel>
57 #include <QLine>
58 #include <QLineEdit>
59 #include <QLineF>
60 #include <QPointF>
61 #include <QPointer>
62 #include <QPushButton>
63 #include <QSet>
64 #include <QSharedPointer>
65 #include <new>
66 
67 #include "../../visualization/visualization.h"
68 #include "../../visualization/line/vistoollineintersect.h"
69 #include "../ifc/xml/vabstractpattern.h"
70 #include "../vgeometry/vpointf.h"
71 #include "../vmisc/vabstractapplication.h"
72 #include "../vpatterndb/vcontainer.h"
73 #include "dialogtool.h"
74 #include "ui_dialoglineintersect.h"
75 
76 //---------------------------------------------------------------------------------------------------------------------
77 /**
78  * @brief DialogLineIntersect create dialog
79  * @param data container with data
80  * @param parent parent widget
81  */
82 DialogLineIntersect::DialogLineIntersect(const VContainer *data, const quint32 &toolId, QWidget *parent)
83  : DialogTool(data, toolId, parent)
84  , ui(new Ui::DialogLineIntersect)
85  , flagPoint(true)
86 {
87  ui->setupUi(this);
88  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
89  setWindowIcon(QIcon(":/toolicon/32x32/intersect.png"));
90 
91  ui->lineEditNamePoint->setClearButtonEnabled(true);
92 
93  number = 0;
95  ui->lineEditNamePoint->setText(qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
96  labelEditNamePoint = ui->labelEditNamePoint;
97 
98  FillComboBoxPoints(ui->comboBoxP1Line1);
99  FillComboBoxPoints(ui->comboBoxP2Line1);
100  FillComboBoxPoints(ui->comboBoxP1Line2);
101  FillComboBoxPoints(ui->comboBoxP2Line2);
102 
103  connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, &DialogLineIntersect::NamePointChanged);
104  connect(ui->comboBoxP1Line1, &QComboBox::currentTextChanged, this, &DialogLineIntersect::PointNameChanged);
105  connect(ui->comboBoxP2Line1, &QComboBox::currentTextChanged, this, &DialogLineIntersect::PointNameChanged);
106  connect(ui->comboBoxP1Line2, &QComboBox::currentTextChanged, this, &DialogLineIntersect::PointNameChanged);
107  connect(ui->comboBoxP2Line2, &QComboBox::currentTextChanged, this, &DialogLineIntersect::PointNameChanged);
108 
110 }
111 
112 //---------------------------------------------------------------------------------------------------------------------
114 {
115  delete ui;
116 }
117 
118 //---------------------------------------------------------------------------------------------------------------------
119 /**
120  * @brief ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
121  * @param id id of point or detail
122  * @param type type of object
123  */
124 void DialogLineIntersect::ChosenObject(quint32 id, const SceneObject &type)
125 {
126  if (prepare == false)// After first choose we ignore all objects
127  {
128  if (type == SceneObject::Point)
129  {
130  VisToolLineIntersect *line = qobject_cast<VisToolLineIntersect *>(vis);
131  SCASSERT(line != nullptr)
132 
133  switch (number)
134  {
135  case 0:
136  if (SetObject(id, ui->comboBoxP1Line1, tr("Select second point of first line")))
137  {
138  number++;
139  line->VisualMode(id);
140  }
141  break;
142  case 1:
143  if (getCurrentObjectId(ui->comboBoxP1Line1) != id)
144  {
145  if (SetObject(id, ui->comboBoxP2Line1, tr("Select first point of second line")))
146  {
147  number++;
148  line->setLine1P2Id(id);
149  line->RefreshGeometry();
150  }
151  }
152  break;
153  case 2:
154  if (SetObject(id, ui->comboBoxP1Line2, tr("Select second point of second line")))
155  {
156  number++;
157  line->setLine2P1Id(id);
158  line->RefreshGeometry();
159  }
160  break;
161  case 3:
162  {
163  QSet<quint32> set;
164  set.insert(getCurrentObjectId(ui->comboBoxP1Line1));
165  set.insert(getCurrentObjectId(ui->comboBoxP2Line1));
166  set.insert(getCurrentObjectId(ui->comboBoxP1Line2));
167  set.insert(id);
168 
169  if (set.size() >= 3)
170  {
171  if (SetObject(id, ui->comboBoxP2Line2, ""))
172  {
173  line->setLine2P2Id(id);
174  line->RefreshGeometry();
175  prepare = true;
177  CheckState();
178  if (flagPoint)
179  {
180  DialogAccepted();
181  }
182  else
183  {
184  this->setModal(true);
185  this->show();
186 
187  connect(ui->comboBoxP1Line1, &QComboBox::currentTextChanged, this,
189  connect(ui->comboBoxP2Line1, &QComboBox::currentTextChanged, this,
191  connect(ui->comboBoxP1Line2, &QComboBox::currentTextChanged, this,
193  connect(ui->comboBoxP2Line2, &QComboBox::currentTextChanged, this,
195  }
196  }
197  }
198  }
199  break;
200  default:
201  break;
202  }
203  }
204  }
205 }
206 
207 //---------------------------------------------------------------------------------------------------------------------
209 {
210  pointName = ui->lineEditNamePoint->text();
211 
212  VisToolLineIntersect *line = qobject_cast<VisToolLineIntersect *>(vis);
213  SCASSERT(line != nullptr)
214 
215  line->setObject1Id(GetP1Line1());
216  line->setLine1P2Id(GetP2Line1());
217  line->setLine2P1Id(GetP1Line2());
218  line->setLine2P2Id(GetP2Line2());
219  line->RefreshGeometry();
220 }
221 
222 //---------------------------------------------------------------------------------------------------------------------
223 /**
224  * @brief P1Line1Changed changed first point of first line
225  */
227 {
229  CheckState();
230 }
231 
232 //---------------------------------------------------------------------------------------------------------------------
234 {
235  QSet<quint32> set;
236  const quint32 p1Line1Id = getCurrentObjectId(ui->comboBoxP1Line1);
237  const quint32 p2Line1Id = getCurrentObjectId(ui->comboBoxP2Line1);
238  const quint32 p1Line2Id = getCurrentObjectId(ui->comboBoxP1Line2);
239  const quint32 p2Line2Id = getCurrentObjectId(ui->comboBoxP2Line2);
240 
241  set.insert(p1Line1Id);
242  set.insert(p2Line1Id);
243  set.insert(p1Line2Id);
244  set.insert(p2Line2Id);
245 
246  const QSharedPointer<VPointF> p1Line1 = data->GeometricObject<VPointF>(p1Line1Id);
247  const QSharedPointer<VPointF> p2Line1 = data->GeometricObject<VPointF>(p2Line1Id);
248  const QSharedPointer<VPointF> p1Line2 = data->GeometricObject<VPointF>(p1Line2Id);
249  const QSharedPointer<VPointF> p2Line2 = data->GeometricObject<VPointF>(p2Line2Id);
250 
251  QLineF line1(static_cast<QPointF>(*p1Line1), static_cast<QPointF>(*p2Line1));
252  QLineF line2(static_cast<QPointF>(*p1Line2), static_cast<QPointF>(*p2Line2));
253  QPointF fPoint;
254  QLineF::IntersectType intersect = line1.intersects(line2, &fPoint);
255 
256  QColor color = okColor;
257  if (set.size() < 3 || intersect == QLineF::NoIntersection)
258  {
259  flagError = false;
260  color = errorColor;
261  }
262  else
263  {
264  flagError = true;
265  color = okColor;
266  }
267  ChangeColor(ui->labelP1Line1, color);
268  ChangeColor(ui->labelP2Line1, color);
269  ChangeColor(ui->labelP1Line2, color);
270  ChangeColor(ui->labelP2Line2, color);
271  CheckState();
272 }
273 
274 //---------------------------------------------------------------------------------------------------------------------
276 {
277  AddVisualization<VisToolLineIntersect>();
278 }
279 
280 //---------------------------------------------------------------------------------------------------------------------
281 /**
282  * @brief CheckState check state of dialog. Enable or disable button ok.
283  */
285 {
286  SCASSERT(ok_Button != nullptr)
287  ok_Button->setEnabled(flagName && flagPoint);
288 }
289 
290 //---------------------------------------------------------------------------------------------------------------------
291 /**
292  * @brief CheckIntersecion check intersection of points
293  * @return true - line have intersection, false = don't have
294  */
296 {
301 
302  QLineF line1(static_cast<QPointF>(*p1L1), static_cast<QPointF>(*p2L1));
303  QLineF line2(static_cast<QPointF>(*p1L2), static_cast<QPointF>(*p2L2));
304  QPointF fPoint;
305  QLineF::IntersectType intersect = line1.intersects(line2, &fPoint);
306  if (intersect == QLineF::UnboundedIntersection || intersect == QLineF::BoundedIntersection)
307  {
308  return true;
309  }
310  else
311  {
312  return false;
313  }
314 }
315 
316 //---------------------------------------------------------------------------------------------------------------------
317 /**
318  * @brief SetP2Line2 set id second point of second line
319  * @param value id
320  */
321 void DialogLineIntersect::SetP2Line2(const quint32 &value)
322 {
323  setCurrentPointId(ui->comboBoxP2Line2, value);
324 
325  VisToolLineIntersect *line = qobject_cast<VisToolLineIntersect *>(vis);
326  SCASSERT(line != nullptr)
327  line->setLine2P2Id(value);
328 }
329 
330 //---------------------------------------------------------------------------------------------------------------------
331 /**
332  * @brief SetP1Line2 set id first point of second line
333  * @param value id
334  */
335 void DialogLineIntersect::SetP1Line2(const quint32 &value)
336 {
337  setCurrentPointId(ui->comboBoxP1Line2, value);
338 
339  VisToolLineIntersect *line = qobject_cast<VisToolLineIntersect *>(vis);
340  SCASSERT(line != nullptr)
341  line->setLine2P1Id(value);
342 }
343 
344 //---------------------------------------------------------------------------------------------------------------------
345 /**
346  * @brief SetP2Line1 set id second point of first line
347  * @param value id
348  */
349 void DialogLineIntersect::SetP2Line1(const quint32 &value)
350 {
351  setCurrentPointId(ui->comboBoxP2Line1, value);
352 
353  VisToolLineIntersect *line = qobject_cast<VisToolLineIntersect *>(vis);
354  SCASSERT(line != nullptr)
355  line->setLine1P2Id(value);
356 }
357 
358 //---------------------------------------------------------------------------------------------------------------------
359 /**
360  * @brief SetP1Line1 set id first point of first line
361  * @param value id
362  */
363 void DialogLineIntersect::SetP1Line1(const quint32 &value)
364 {
365  setCurrentPointId(ui->comboBoxP1Line1, value);
366 
367  VisToolLineIntersect *line = qobject_cast<VisToolLineIntersect *>(vis);
368  SCASSERT(line != nullptr)
369  line->setObject1Id(value);
370 }
371 
372 //---------------------------------------------------------------------------------------------------------------------
373 /**
374  * @brief SetPointName set name of point
375  * @param value name of point
376  */
377 void DialogLineIntersect::SetPointName(const QString &value)
378 {
379  pointName = value;
380  ui->lineEditNamePoint->setText(pointName);
381 }
382 
383 //---------------------------------------------------------------------------------------------------------------------
384 /**
385  * @brief GetP1Line1 return id first point of first line
386  * @return id
387  */
389 {
390  return getCurrentObjectId(ui->comboBoxP1Line1);
391 }
392 
393 //---------------------------------------------------------------------------------------------------------------------
394 /**
395  * @brief GetP2Line1 return id second point of first line
396  * @return id
397  */
399 {
400  return getCurrentObjectId(ui->comboBoxP2Line1);
401 }
402 
403 //---------------------------------------------------------------------------------------------------------------------
404 /**
405  * @brief GetP1Line2 return id first point of second line
406  * @return id
407  */
409 {
410  return getCurrentObjectId(ui->comboBoxP1Line2);
411 }
412 
413 //---------------------------------------------------------------------------------------------------------------------
414 /**
415  * @brief GetP2Line2 return id second point of second line
416  * @return id
417  */
419 {
420  return getCurrentObjectId(ui->comboBoxP2Line2);
421 }
The DialogLineIntersect class dialog for ToolLineIntersect. Help create point and edit option.
void SetP2Line2(const quint32 &value)
SetP2Line2 set id second point of second line.
quint32 GetP2Line1() const
GetP2Line1 return id second point of first line.
virtual void ShowVisualization() Q_DECL_OVERRIDE
quint32 GetP1Line2() const
GetP1Line2 return id first point of second line.
virtual void PointNameChanged() Q_DECL_OVERRIDE
void SetP1Line1(const quint32 &value)
SetP1Line1 set id first point of first line.
Ui::DialogLineIntersect * ui
ui keeps information about user interface
DialogLineIntersect(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
DialogLineIntersect create dialog.
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
virtual void CheckState() Q_DECL_FINAL
CheckState check state of dialog. Enable or disable button ok.
void SetP1Line2(const quint32 &value)
SetP1Line2 set id first point of second line.
quint32 GetP2Line2() const
GetP2Line2 return id second point of second line.
virtual ~DialogLineIntersect() Q_DECL_OVERRIDE
void SetPointName(const QString &value)
SetPointName set name of point.
quint32 GetP1Line1() const
GetP1Line1 return id first point of first line.
void SetP2Line1(const quint32 &value)
SetP2Line1 set id second point of first line.
virtual void ChosenObject(quint32 id, const SceneObject &type) Q_DECL_OVERRIDE
ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
bool CheckIntersecion()
CheckIntersecion check intersection of points.
void PointChanged()
P1Line1Changed changed first point of first line.
bool flagPoint
flagPoint keep state of point
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
void NamePointChanged()
NamePointChanged check name of point.
bool flagName
flagName true if name is correct
Definition: dialogtool.h:183
QPushButton * ok_Button
ok_Button button ok
Definition: dialogtool.h:199
QString pointName
pointName name of point
Definition: dialogtool.h:231
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.
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
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
The VPointF class keep data of point.
Definition: vpointf.h:75
void setLine2P2Id(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLine2P1Id(const quint32 &value)
void setLine1P2Id(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 qApp
Definition: vapplication.h:67