1 /************************************************************************
2 **
3 ** @file dialogtool.cpp
4 ** @author Roman Telezhynskyi <dismine(at)gmail.com>
5 ** @date November 15, 2013
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) 2013-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 "dialogtool.h"
30
31 #include <climits>
32 #include <qiterator.h>
33 #include <qnumeric.h>
34 #include <QCloseEvent>
35 #include <QComboBox>
36 #include <QDoubleSpinBox>
37 #include <QEvent>
38 #include <QHash>
39 #include <QIcon>
40 #include <QKeyEvent>
41 #include <QLabel>
42 #include <QLineEdit>
43 #include <QMapIterator>
44 #include <QMessageLogger>
45 #include <QPalette>
46 #include <QPixmap>
47 #include <QPlainTextEdit>
48 #include <QPushButton>
49 #include <QRegularExpression>
50 #include <QRegularExpressionMatch>
51 #include <QScopedPointer>
52 #include <QSharedPointer>
53 #include <QShowEvent>
54 #include <QSize>
55 #include <QTextCursor>
56 #include <QTimer>
57 #include <QWidget>
58 #include <Qt>
59 #include <QtDebug>
60 #include <new>
61 #include <QBuffer>
62
63 #include "../ifc/xml/vdomdocument.h"
64 #include "../qmuparser/qmudef.h"
65 #include "../qmuparser/qmuparsererror.h"
66 #include "../vgeometry/vpointf.h"
67 #include "../vpatterndb/calculator.h"
68 #include "../vpatterndb/vcontainer.h"
69 #include "../vpatterndb/vtranslatevars.h"
70 #include "../vpatterndb/vpiecenode.h"
71 #include "../../tools/vabstracttool.h"
72 #include "../ifc/xml/vabstractpattern.h"
73 #include "../vgeometry/vabstractcurve.h"
74 #include "../vgeometry/vgobject.h"
75
76 template <class T> class QSharedPointer;
77
78 Q_LOGGING_CATEGORY(vDialog, "v.dialog")
79
80 //---------------------------------------------------------------------------------------------------------------------
81 /**
82 * @brief DialogTool create dialog
83 * @param data container with data
84 * @param parent parent widget
85 */
DialogTool(const VContainer * data,quint32 toolId,QWidget * parent)86 DialogTool::DialogTool(const VContainer *data, quint32 toolId, QWidget *parent)
87 : QDialog(parent),
88 data(data),
89 isInitialized(false),
90 bOk(nullptr),
91 bApply(nullptr),
92 associatedTool(nullptr),
93 toolId(toolId),
94 prepare(false),
95 number(0),
96 vis(nullptr)
97 {
98 SCASSERT(data != nullptr)
99 }
100
101 //---------------------------------------------------------------------------------------------------------------------
~DialogTool()102 DialogTool::~DialogTool()
103 {
104 emit ToolTip(QString());
105
106 if (not vis.isNull())
107 {
108 delete vis;
109 }
110 }
111
112 //---------------------------------------------------------------------------------------------------------------------
113 /**
114 * @brief closeEvent handle when dialog cloded
115 * @param event event
116 */
closeEvent(QCloseEvent * event)117 void DialogTool::closeEvent(QCloseEvent *event)
118 {
119 DialogRejected();
120 event->accept();
121 }
122
123 //---------------------------------------------------------------------------------------------------------------------
124 /**
125 * @brief showEvent handle when window show
126 * @param event event
127 */
showEvent(QShowEvent * event)128 void DialogTool::showEvent(QShowEvent *event)
129 {
130 QDialog::showEvent( event );
131 if ( event->spontaneous() )
132 {
133 return;
134 }
135 if (isInitialized)
136 {
137 return;
138 }
139 // do your init stuff here
140
141 setMaximumSize(size());
142 setMinimumSize(size());
143
144 isInitialized = true;//first show windows are held
145 ShowVisualization();
146
147 CheckState();
148 }
149
150 //---------------------------------------------------------------------------------------------------------------------
keyPressEvent(QKeyEvent * event)151 void DialogTool::keyPressEvent(QKeyEvent *event)
152 {
153 switch (event->key())
154 {
155 case Qt::Key_Escape:
156 DialogRejected();
157 return; // After reject the dialog will be destroyed, exit imidiately
158 default:
159 break;
160 }
161 QDialog::keyPressEvent ( event );
162 }
163
164 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxPiecesList(QComboBox * box,const QVector<quint32> & list)165 void DialogTool::FillComboBoxPiecesList(QComboBox *box, const QVector<quint32> &list)
166 {
167 SCASSERT(box != nullptr)
168 box->blockSignals(true);
169 box->clear();
170 for (auto id : list)
171 {
172 box->addItem(data->GetPiece(id).GetName(), id);
173 }
174 box->blockSignals(false);
175 box->setCurrentIndex(-1); // Force a user to choose
176 }
177
178 //---------------------------------------------------------------------------------------------------------------------
179 /**
180 * @brief FillComboBoxPoints fill comboBox list of points
181 * @param box comboBox
182 */
FillComboBoxPoints(QComboBox * box,FillComboBox rule,quint32 ch1,quint32 ch2) const183 void DialogTool::FillComboBoxPoints(QComboBox *box, FillComboBox rule, quint32 ch1, quint32 ch2) const
184 {
185 FillCombo<VPointF>(box, GOType::Point, rule, ch1, ch2);
186 }
187
188 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxArcs(QComboBox * box,FillComboBox rule,quint32 ch1,quint32 ch2) const189 void DialogTool::FillComboBoxArcs(QComboBox *box, FillComboBox rule, quint32 ch1, quint32 ch2) const
190 {
191 FillCombo<VAbstractCurve>(box, GOType::Arc, rule, ch1, ch2);
192 }
193
194 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxSplines(QComboBox * box) const195 void DialogTool::FillComboBoxSplines(QComboBox *box) const
196 {
197 SCASSERT(box != nullptr)
198 box->blockSignals(true);
199
200 const auto objs = data->CalculationGObjects();
201 QHash<quint32, QSharedPointer<VGObject> >::const_iterator i;
202 QMap<QString, quint32> list;
203 for (i = objs->constBegin(); i != objs->constEnd(); ++i)
204 {
205 if (i.key() != toolId)
206 {
207 if (IsSpline(i.value()))
208 {
209 PrepareList<VAbstractCurve>(list, i.key());
210 }
211 }
212 }
213 FillList(box, list);
214 box->setCurrentIndex(-1); // force to select
215 box->blockSignals(false);
216 }
217
218 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxSplinesPath(QComboBox * box) const219 void DialogTool::FillComboBoxSplinesPath(QComboBox *box) const
220 {
221 SCASSERT(box != nullptr)
222 box->blockSignals(true);
223
224 const auto objs = data->CalculationGObjects();
225 QHash<quint32, QSharedPointer<VGObject> >::const_iterator i;
226 QMap<QString, quint32> list;
227 for (i = objs->constBegin(); i != objs->constEnd(); ++i)
228 {
229 if (i.key() != toolId)
230 {
231 if (IsSplinePath(i.value()))
232 {
233 PrepareList<VAbstractCurve>(list, i.key());
234 }
235 }
236 }
237 FillList(box, list);
238 box->setCurrentIndex(-1); // force to select
239 box->blockSignals(false);
240 }
241
242 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxCurves(QComboBox * box) const243 void DialogTool::FillComboBoxCurves(QComboBox *box) const
244 {
245 SCASSERT(box != nullptr)
246 const auto objs = data->CalculationGObjects();
247 QMap<QString, quint32> list;
248 QHash<quint32, QSharedPointer<VGObject> >::const_iterator i;
249 for (i = objs->constBegin(); i != objs->constEnd(); ++i)
250 {
251 if (i.key() != toolId)
252 {
253 QSharedPointer<VGObject> obj = i.value();
254 if (obj->getType() == GOType::Arc
255 || obj->getType() == GOType::EllipticalArc
256 || obj->getType() == GOType::Spline
257 || obj->getType() == GOType::SplinePath
258 || obj->getType() == GOType::CubicBezier
259 || obj->getType() == GOType::CubicBezierPath)
260 {
261 PrepareList<VAbstractCurve>(list, i.key());
262 }
263 }
264 }
265 FillList(box, list);
266 }
267
268 //---------------------------------------------------------------------------------------------------------------------
269 /**
270 * @brief FillComboBoxTypeLine fill comboBox list of type lines
271 * @param box comboBox
272 */
FillComboBoxTypeLine(QComboBox * box,const QMap<QString,QIcon> & stylesPics,const QString & def) const273 void DialogTool::FillComboBoxTypeLine(QComboBox *box, const QMap<QString, QIcon> &stylesPics, const QString &def) const
274 {
275 SCASSERT(box != nullptr)
276 QMap<QString, QIcon>::const_iterator i = stylesPics.constBegin();
277 while (i != stylesPics.constEnd())
278 {
279 box->addItem(i.value(), QString(), QVariant(i.key()));
280 ++i;
281 }
282
283 const int index = box->findData(QVariant(def));
284 if (index != -1)
285 {
286 box->setCurrentIndex(index);
287 }
288 }
289
290 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxLineColors(QComboBox * box) const291 void DialogTool::FillComboBoxLineColors(QComboBox *box) const
292 {
293 FillComboBoxLineColors(box, VAbstractTool::ColorsList());
294 }
295
296 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxLineColors(QComboBox * box,const QMap<QString,QString> & lineColors) const297 void DialogTool::FillComboBoxLineColors(QComboBox *box, const QMap<QString, QString> &lineColors) const
298 {
299 SCASSERT(box != nullptr)
300
301 box->clear();
302
303 QMap<QString, QString>::const_iterator i = lineColors.constBegin();
304 while (i != lineColors.constEnd())
305 {
306 box->addItem(LineColor(box->iconSize().height(), i.key()), i.value(), QVariant(i.key()));
307 ++i;
308 }
309 }
310
311 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxCrossCirclesPoints(QComboBox * box) const312 void DialogTool::FillComboBoxCrossCirclesPoints(QComboBox *box) const
313 {
314 SCASSERT(box != nullptr)
315
316 box->addItem(tr("First point"), QVariant(static_cast<int>(CrossCirclesPoint::FirstPoint)));
317 box->addItem(tr("Second point"), QVariant(static_cast<int>(CrossCirclesPoint::SecondPoint)));
318 }
319
320 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxVCrossCurvesPoint(QComboBox * box) const321 void DialogTool::FillComboBoxVCrossCurvesPoint(QComboBox *box) const
322 {
323 SCASSERT(box != nullptr)
324
325 box->addItem(tr("Highest point"), QVariant(static_cast<int>(VCrossCurvesPoint::HighestPoint)));
326 box->addItem(tr("Lowest point"), QVariant(static_cast<int>(VCrossCurvesPoint::LowestPoint)));
327 }
328
329 //---------------------------------------------------------------------------------------------------------------------
FillComboBoxHCrossCurvesPoint(QComboBox * box) const330 void DialogTool::FillComboBoxHCrossCurvesPoint(QComboBox *box) const
331 {
332 SCASSERT(box != nullptr)
333
334 box->addItem(tr("Leftmost point"), QVariant(static_cast<int>(HCrossCurvesPoint::LeftmostPoint)));
335 box->addItem(tr("Rightmost point"), QVariant(static_cast<int>(HCrossCurvesPoint::RightmostPoint)));
336 }
337
338 //---------------------------------------------------------------------------------------------------------------------
GetComboBoxCurrentData(const QComboBox * box,const QString & def) const339 QString DialogTool::GetComboBoxCurrentData(const QComboBox *box, const QString &def) const
340 {
341 SCASSERT(box != nullptr)
342 QString value;
343 value = box->currentData().toString();
344
345 if (value.isEmpty())
346 {
347 value = def;
348 }
349 return value;
350 }
351
352 //---------------------------------------------------------------------------------------------------------------------
353 /**
354 * @brief ChangeCurrentData select item in combobox by id
355 * @param box combobox
356 * @param value id of item
357 */
ChangeCurrentData(QComboBox * box,const QVariant & value) const358 void DialogTool::ChangeCurrentData(QComboBox *box, const QVariant &value) const
359 {
360 SCASSERT(box != nullptr)
361 const qint32 index = box->findData(value);
362 if (index != -1)
363 {
364 box->setCurrentIndex(index);
365 }
366 }
367
368 //---------------------------------------------------------------------------------------------------------------------
eventFilter(QObject * object,QEvent * event)369 bool DialogTool::eventFilter(QObject *object, QEvent *event)
370 {
371 const bool fitered = FilterObject(object, event);
372 if (fitered)
373 {
374 return fitered;
375 }
376
377 return QDialog::eventFilter(object, event);
378 }
379
380 //---------------------------------------------------------------------------------------------------------------------
DNumber(const QString & baseName) const381 quint32 DialogTool::DNumber(const QString &baseName) const
382 {
383 quint32 num = 0;
384 QString name;
385 do
386 {
387 ++num;
388 name = baseName + QString("_%1").arg(num);
389 } while (not data->IsUnique(name));
390
391 return num;
392 }
393
394 //---------------------------------------------------------------------------------------------------------------------
GetNodeName(const VPieceNode & node,bool showPassmarkDetails) const395 QString DialogTool::GetNodeName(const VPieceNode &node, bool showPassmarkDetails) const
396 {
397 const QSharedPointer<VGObject> obj = data->GetGObject(node.GetId());
398 QString name = obj->ObjectName();
399
400 if (node.GetTypeTool() != Tool::NodePoint)
401 {
402 if (node.GetReverse())
403 {
404 name = QStringLiteral("- ") + name;
405 }
406 }
407 else
408 {
409 if (showPassmarkDetails && node.IsPassmark())
410 {
411 switch(node.GetPassmarkLineType())
412 {
413 case PassmarkLineType::OneLine:
414 name += QLatin1Char('|');
415 break;
416 case PassmarkLineType::TwoLines:
417 name += QLatin1String("||");
418 break;
419 case PassmarkLineType::ThreeLines:
420 name += QLatin1String("|||");
421 break;
422 case PassmarkLineType::TMark:
423 name += QStringLiteral("┴");
424 break;
425 case PassmarkLineType::VMark:
426 name += QStringLiteral("⊼");
427 break;
428 case PassmarkLineType::VMark2:
429 name += QStringLiteral("⊽");
430 break;
431 case PassmarkLineType::UMark:
432 name += QStringLiteral("⋃");
433 break;
434 case PassmarkLineType::BoxMark:
435 name += QStringLiteral("⎕");
436 break;
437 default:
438 break;
439 }
440 }
441
442 if (not node.IsCheckUniqueness())
443 {
444 name = QLatin1Char('[') + name + QLatin1Char(']');
445 }
446 }
447
448 return name;
449 }
450
451 //---------------------------------------------------------------------------------------------------------------------
NewNodeItem(QListWidget * listWidget,const VPieceNode & node,bool showPassmark,bool showExclusion)452 void DialogTool::NewNodeItem(QListWidget *listWidget, const VPieceNode &node, bool showPassmark, bool showExclusion)
453 {
454 SCASSERT(listWidget != nullptr);
455 SCASSERT(node.GetId() > NULL_ID);
456 QString name;
457 switch (node.GetTypeTool())
458 {
459 case (Tool::NodePoint):
460 case (Tool::NodeArc):
461 case (Tool::NodeElArc):
462 case (Tool::NodeSpline):
463 case (Tool::NodeSplinePath):
464 name = GetNodeName(node, showPassmark);
465 break;
466 default:
467 qDebug()<<"Got wrong tools. Ignore.";
468 return;
469 }
470
471 bool canAddNewPoint = false;
472
473 if(listWidget->count() == 0)
474 {
475 canAddNewPoint = true;
476 }
477 else
478 {
479 if(RowNode(listWidget, listWidget->count()-1).GetId() != node.GetId())
480 {
481 canAddNewPoint = true;
482 }
483 }
484
485 if(canAddNewPoint)
486 {
487 QListWidgetItem *item = new QListWidgetItem(name);
488 item->setFont(NodeFont(item->font(), showExclusion ? node.IsExcluded() : false));
489 item->setData(Qt::UserRole, QVariant::fromValue(node));
490 listWidget->addItem(item);
491 listWidget->setCurrentRow(listWidget->count()-1);
492 }
493 }
494
495 //---------------------------------------------------------------------------------------------------------------------
InitNodeAngles(QComboBox * box)496 void DialogTool::InitNodeAngles(QComboBox *box)
497 {
498 SCASSERT(box != nullptr);
499 box->clear();
500
501 box->addItem(tr("by length"), static_cast<unsigned char>(PieceNodeAngle::ByLength));
502 box->addItem(tr("by points intersetions"), static_cast<unsigned char>(PieceNodeAngle::ByPointsIntersection));
503 box->addItem(tr("by first edge symmetry"), static_cast<unsigned char>(PieceNodeAngle::ByFirstEdgeSymmetry));
504 box->addItem(tr("by second edge symmetry"), static_cast<unsigned char>(PieceNodeAngle::BySecondEdgeSymmetry));
505 box->addItem(tr("by first edge right angle"), static_cast<unsigned char>(PieceNodeAngle::ByFirstEdgeRightAngle));
506 box->addItem(tr("by second edge right angle"), static_cast<unsigned char>(PieceNodeAngle::BySecondEdgeRightAngle));
507 }
508
509 //---------------------------------------------------------------------------------------------------------------------
MoveListRowTop(QListWidget * list)510 void DialogTool::MoveListRowTop(QListWidget *list)
511 {
512 SCASSERT(list != nullptr)
513 const int currentIndex = list->currentRow();
514 if (QListWidgetItem *currentItem = list->takeItem(currentIndex))
515 {
516 list->insertItem(0, currentItem);
517 list->setCurrentRow(0);
518 }
519 }
520
521 //---------------------------------------------------------------------------------------------------------------------
MoveListRowUp(QListWidget * list)522 void DialogTool::MoveListRowUp(QListWidget *list)
523 {
524 SCASSERT(list != nullptr)
525 int currentIndex = list->currentRow();
526 if (QListWidgetItem *currentItem = list->takeItem(currentIndex--))
527 {
528 if (currentIndex < 0)
529 {
530 currentIndex = 0;
531 }
532 list->insertItem(currentIndex, currentItem);
533 list->setCurrentRow(currentIndex);
534 }
535 }
536
537 //---------------------------------------------------------------------------------------------------------------------
MoveListRowDown(QListWidget * list)538 void DialogTool::MoveListRowDown(QListWidget *list)
539 {
540 SCASSERT(list != nullptr)
541 int currentIndex = list->currentRow();
542 if (QListWidgetItem *currentItem = list->takeItem(currentIndex++))
543 {
544 if (currentIndex > list->count())
545 {
546 currentIndex = list->count();
547 }
548 list->insertItem(currentIndex, currentItem);
549 list->setCurrentRow(currentIndex);
550 }
551 }
552
553 //---------------------------------------------------------------------------------------------------------------------
MoveListRowBottom(QListWidget * list)554 void DialogTool::MoveListRowBottom(QListWidget *list)
555 {
556 SCASSERT(list != nullptr)
557 const int currentIndex = list->currentRow();
558 if (QListWidgetItem *currentItem = list->takeItem(currentIndex))
559 {
560 list->insertItem(list->count(), currentItem);
561 list->setCurrentRow(list->count()-1);
562 }
563 }
564
565 //---------------------------------------------------------------------------------------------------------------------
IsSplinePath(const QSharedPointer<VGObject> & obj) const566 bool DialogTool::IsSplinePath(const QSharedPointer<VGObject> &obj) const
567 {
568 return obj->getType() == GOType::SplinePath || obj->getType() == GOType::CubicBezierPath;
569 }
570
571 //---------------------------------------------------------------------------------------------------------------------
572 /**
573 * @brief Eval evaluate formula and show result
574 * @param formulaData options to control parsing
575 */
Eval(const FormulaData & formulaData,bool & flag)576 qreal DialogTool::Eval(const FormulaData &formulaData, bool &flag)
577 {
578 const qreal result = EvalToolFormula(this, formulaData, flag);
579 CheckState(); // Disable Ok and Apply buttons if something wrong.
580 return result;
581 }
582
583 //---------------------------------------------------------------------------------------------------------------------
setCurrentPointId(QComboBox * box,const quint32 & value,FillComboBox rule,const quint32 & ch1,const quint32 & ch2) const584 void DialogTool::setCurrentPointId(QComboBox *box, const quint32 &value, FillComboBox rule,
585 const quint32 &ch1, const quint32 &ch2) const
586 {
587 SCASSERT(box != nullptr)
588
589 box->blockSignals(true);
590
591 FillComboBoxPoints(box, rule, ch1, ch2);
592 ChangeCurrentData(box, value);
593
594 box->blockSignals(false);
595 }
596
597 //---------------------------------------------------------------------------------------------------------------------
598 /**
599 * @brief setCurrentSplineId set current spline id in combobox
600 */
setCurrentSplineId(QComboBox * box,const quint32 & value) const601 void DialogTool::setCurrentSplineId(QComboBox *box, const quint32 &value) const
602 {
603 SCASSERT(box != nullptr)
604 FillComboBoxSplines(box);
605 ChangeCurrentData(box, value);
606 }
607
608 //---------------------------------------------------------------------------------------------------------------------
609 /**
610 * @brief setCurrentArcId
611 */
setCurrentArcId(QComboBox * box,const quint32 & value,FillComboBox rule,const quint32 & ch1,const quint32 & ch2) const612 void DialogTool::setCurrentArcId(QComboBox *box, const quint32 &value, FillComboBox rule,
613 const quint32 &ch1, const quint32 &ch2) const
614 {
615 SCASSERT(box != nullptr)
616 FillComboBoxArcs(box, rule, ch1, ch2);
617 ChangeCurrentData(box, value);
618 }
619
620 //---------------------------------------------------------------------------------------------------------------------
621 /**
622 * @brief setCurrentSplinePathId set current splinePath id in combobox
623 * @param box combobox
624 * @param value splinePath id
625 */
setCurrentSplinePathId(QComboBox * box,const quint32 & value) const626 void DialogTool::setCurrentSplinePathId(QComboBox *box, const quint32 &value) const
627 {
628 SCASSERT(box != nullptr)
629 FillComboBoxSplinesPath(box);
630 ChangeCurrentData(box, value);
631 }
632
633 //---------------------------------------------------------------------------------------------------------------------
setCurrentCurveId(QComboBox * box,const quint32 & value) const634 void DialogTool::setCurrentCurveId(QComboBox *box, const quint32 &value) const
635 {
636 SCASSERT(box != nullptr)
637 FillComboBoxCurves(box);
638 ChangeCurrentData(box, value);
639 }
640
641 //---------------------------------------------------------------------------------------------------------------------
642 /**
643 * @brief getCurrentPointId return current point id stored in combobox
644 * @param box combobox
645 * @return id or 0 if combobox is empty
646 */
getCurrentObjectId(QComboBox * box) const647 quint32 DialogTool::getCurrentObjectId(QComboBox *box) const
648 {
649 SCASSERT(box != nullptr)
650 qint32 index = box->currentIndex();
651 if (index != -1)
652 {
653 return qvariant_cast<quint32>(box->itemData(index));
654 }
655 else
656 {
657 return 0;
658 }
659 }
660
661 //---------------------------------------------------------------------------------------------------------------------
SetObject(const quint32 & id,QComboBox * box,const QString & toolTip)662 bool DialogTool::SetObject(const quint32 &id, QComboBox *box, const QString &toolTip)
663 {
664 SCASSERT(box != nullptr)
665 const qint32 index = box->findData(id);
666 if ( index != -1 )
667 { // -1 for not found
668 box->setCurrentIndex(index);
669 emit ToolTip(toolTip);
670 return true;
671 }
672 else
673 {
674 qWarning()<<"Can't find object by id"<<id;
675 }
676 return false;
677 }
678
679 //---------------------------------------------------------------------------------------------------------------------
680 /**
681 * @brief FillList fill combobox list
682 * @param box combobox
683 * @param list list with ids and names
684 */
FillList(QComboBox * box,const QMap<QString,quint32> & list) const685 void DialogTool::FillList(QComboBox *box, const QMap<QString, quint32> &list) const
686 {
687 SCASSERT(box != nullptr)
688 box->clear();
689
690 QMapIterator<QString, quint32> iter(list);
691 while (iter.hasNext())
692 {
693 iter.next();
694 box->addItem(iter.key(), iter.value());
695 }
696 }
697
698 //---------------------------------------------------------------------------------------------------------------------
699 template <typename T>
PrepareList(QMap<QString,quint32> & list,quint32 id) const700 void DialogTool::PrepareList(QMap<QString, quint32> &list, quint32 id) const
701 {
702 const auto obj = data->GeometricObject<T>(id);
703 SCASSERT(obj != nullptr)
704 list[obj->ObjectName()] = id;
705 }
706
707 //---------------------------------------------------------------------------------------------------------------------
IsSpline(const QSharedPointer<VGObject> & obj) const708 bool DialogTool::IsSpline(const QSharedPointer<VGObject> &obj) const
709 {
710 return obj->getType() == GOType::Spline || obj->getType() == GOType::CubicBezier;
711 }
712
713 //---------------------------------------------------------------------------------------------------------------------
714 /**
715 * @brief CheckState enable, when all is correct, or disable, when something wrong, button ok
716 */
CheckState()717 void DialogTool::CheckState()
718 {
719 SCASSERT(bOk != nullptr)
720 bOk->setEnabled(IsValid());
721 // In case dialog hasn't apply button
722 if ( bApply != nullptr)
723 {
724 bApply->setEnabled(bOk->isEnabled());
725 }
726 }
727
728 //---------------------------------------------------------------------------------------------------------------------
729 /**
730 * @brief ChosenObject gets id and type of selected object. Save right data and ignore wrong.
731 * @param id id of point or detail
732 * @param type type of object
733 */
ChosenObject(quint32 id,const SceneObject & type)734 void DialogTool::ChosenObject(quint32 id, const SceneObject &type)
735 {
736 Q_UNUSED(id)
737 Q_UNUSED(type)
738 }
739
740 //---------------------------------------------------------------------------------------------------------------------
SelectedObject(bool selected,quint32 object,quint32 tool)741 void DialogTool::SelectedObject(bool selected, quint32 object, quint32 tool)
742 {
743 Q_UNUSED(selected)
744 Q_UNUSED(object)
745 Q_UNUSED(tool)
746 }
747
748 //---------------------------------------------------------------------------------------------------------------------
749 /**
750 * @brief DialogAccepted save data and emit signal about closed dialog.
751 */
DialogAccepted()752 void DialogTool::DialogAccepted()
753 {
754 SaveData();
755 emit DialogClosed(QDialog::Accepted);
756 }
757
758 //---------------------------------------------------------------------------------------------------------------------
DialogApply()759 void DialogTool::DialogApply()
760 {
761 SaveData();
762 emit DialogApplied();
763 }
764
765 //---------------------------------------------------------------------------------------------------------------------
766 /**
767 * @brief DialogRejected emit signal dialog rejected
768 */
DialogRejected()769 void DialogTool::DialogRejected()
770 {
771 emit DialogClosed(QDialog::Rejected);
772 }
773
774 //---------------------------------------------------------------------------------------------------------------------
775 // cppcheck-suppress unusedFunction
GetToolId() const776 quint32 DialogTool::GetToolId() const
777 {
778 return toolId;
779 }
780
781 //---------------------------------------------------------------------------------------------------------------------
SetToolId(const quint32 & value)782 void DialogTool::SetToolId(const quint32 &value)
783 {
784 toolId = value;
785 }
786
787 //---------------------------------------------------------------------------------------------------------------------
SetGroupCategories(const QStringList & categories)788 void DialogTool::SetGroupCategories(const QStringList &categories)
789 {
790 Q_UNUSED(categories)
791 // do nothing
792 }
793
794 //---------------------------------------------------------------------------------------------------------------------
ShowDialog(bool click)795 void DialogTool::ShowDialog(bool click)
796 {
797 Q_UNUSED(click)
798 }
799
800 //---------------------------------------------------------------------------------------------------------------------
Build(const Tool & type)801 void DialogTool::Build(const Tool &type)
802 {
803 Q_UNUSED(type)
804 }
805
806 //---------------------------------------------------------------------------------------------------------------------
SetPiecesList(const QVector<quint32> & list)807 void DialogTool::SetPiecesList(const QVector<quint32> &list)
808 {
809 Q_UNUSED(list);
810 }
811
812 //---------------------------------------------------------------------------------------------------------------------
SetPatternDoc(VAbstractPattern * doc)813 void DialogTool::SetPatternDoc(VAbstractPattern *doc)
814 {
815 Q_UNUSED(doc);
816 }
817
818 //---------------------------------------------------------------------------------------------------------------------
SetAssociatedTool(VAbstractTool * tool)819 void DialogTool::SetAssociatedTool(VAbstractTool *tool)
820 {
821 if (tool != nullptr)
822 {
823 associatedTool = tool;
824 SetToolId(tool->getId());
825 data = tool->getData();
826 if (not vis.isNull())
827 {
828 vis->SetData(data);
829 }
830 }
831 else
832 {
833 associatedTool = nullptr;
834 SetToolId(NULL_ID);
835 }
836 }
837
838 //---------------------------------------------------------------------------------------------------------------------
839 template <typename GObject>
FillCombo(QComboBox * box,GOType gType,FillComboBox rule,const quint32 & ch1,const quint32 & ch2) const840 void DialogTool::FillCombo(QComboBox *box, GOType gType, FillComboBox rule, const quint32 &ch1,
841 const quint32 &ch2) const
842 {
843 SCASSERT(box != nullptr)
844 box->blockSignals(true);
845
846 const QHash<quint32, QSharedPointer<VGObject> > *objs = data->CalculationGObjects();
847 QHash<quint32, QSharedPointer<VGObject> >::const_iterator i;
848 QMap<QString, quint32> list;
849 for (i = objs->constBegin(); i != objs->constEnd(); ++i)
850 {
851 if (rule == FillComboBox::NoChildren)
852 {
853 if (i.key() != toolId && i.value()->getIdTool() != toolId && i.key() != ch1 && i.key() != ch2)
854 {
855 QSharedPointer<VGObject> obj = i.value();
856 if (obj->getType() == gType)
857 {
858 PrepareList<GObject>(list, i.key());
859 }
860 }
861 }
862 else
863 {
864 if (i.key() != toolId && i.value()->getIdTool() != toolId)
865 {
866 QSharedPointer<VGObject> obj = i.value();
867 if (obj->getType() == gType && obj->getMode() == Draw::Calculation)
868 {
869 PrepareList<GObject>(list, i.key());
870 }
871 }
872 }
873 }
874 FillList(box, list);
875 box->setCurrentIndex(-1); // force to select
876 box->blockSignals(false);
877 }
878