Seamly2D
Code documentation
dialogbisector.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 dialogbisector.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 "dialogbisector.h"
53 
54 #include <QColor>
55 #include <QComboBox>
56 #include <QDialog>
57 #include <QLabel>
58 #include <QLineEdit>
59 #include <QPlainTextEdit>
60 #include <QPointer>
61 #include <QPushButton>
62 #include <QSet>
63 #include <QToolButton>
64 
65 #include "../../visualization/line/vistoolbisector.h"
66 #include "../../visualization/visualization.h"
67 #include "../ifc/xml/vabstractpattern.h"
68 #include "../ifc/xml/vdomdocument.h"
69 #include "../support/edit_formula_dialog.h"
70 #include "../vmisc/vabstractapplication.h"
71 #include "../vmisc/vcommonsettings.h"
72 #include "../vpatterndb/vtranslatevars.h"
73 #include "ui_dialogbisector.h"
74 
75 //---------------------------------------------------------------------------------------------------------------------
76 /**
77  * @brief DialogBisector create dialog
78  * @param data container with data
79  * @param parent parent widget
80  */
81 DialogBisector::DialogBisector(const VContainer *data, const quint32 &toolId, QWidget *parent)
82  : DialogTool(data, toolId, parent)
83  , ui(new Ui::DialogBisector)
84  , formula(QString())
85  , formulaBaseHeight(0)
86 {
87  ui->setupUi(this);
88  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
89  setWindowIcon(QIcon(":/toolicon/32x32/bisector.png"));
90 
91  ui->lineEditNamePoint->setClearButtonEnabled(true);
92 
94  ui->lineEditNamePoint->setText(qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
95  labelEditNamePoint = ui->labelEditNamePoint;
96  this->formulaBaseHeight = ui->plainTextEditFormula->height();
97  ui->plainTextEditFormula->installEventFilter(this);
98 
100  flagFormula = false;
102 
103  FillComboBoxPoints(ui->comboBoxFirstPoint);
104  FillComboBoxPoints(ui->comboBoxSecondPoint);
105  FillComboBoxPoints(ui->comboBoxThirdPoint);
106 
107  int index = ui->lineColor_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineColor());
108  if (index != -1)
109  {
110  ui->lineColor_ComboBox->setCurrentIndex(index);
111  }
112 
113  index = ui->lineWeight_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineWeight());
114  if (index != -1)
115  {
116  ui->lineWeight_ComboBox->setCurrentIndex(index);
117  }
118 
119  index = ui->lineType_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineType());
120  if (index != -1)
121  {
122  ui->lineType_ComboBox->setCurrentIndex(index);
123  }
124 
125  connect(ui->toolButtonExprLength, &QPushButton::clicked, this, &DialogBisector::FXLength);
126  connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, &DialogBisector::NamePointChanged);
127  connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogBisector::FormulaTextChanged);
128  connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogBisector::DeployFormulaTextEdit);
129  connect(ui->comboBoxFirstPoint, &QComboBox::currentTextChanged, this, &DialogBisector::PointNameChanged);
130  connect(ui->comboBoxSecondPoint, &QComboBox::currentTextChanged, this, &DialogBisector::PointNameChanged);
131  connect(ui->comboBoxThirdPoint, &QComboBox::currentTextChanged, this, &DialogBisector::PointNameChanged);
132 
133  vis = new VisToolBisector(data);
134 
135  // Call after initialization vis!!!!
137  setLineWeight("0.35");
138 }
139 
140 //---------------------------------------------------------------------------------------------------------------------
142 {
143  this->FormulaChangedPlainText();
144 }
145 
146 //---------------------------------------------------------------------------------------------------------------------
148 {
149  QSet<quint32> set;
150  set.insert(getCurrentObjectId(ui->comboBoxFirstPoint));
151  set.insert(getCurrentObjectId(ui->comboBoxSecondPoint));
152  set.insert(getCurrentObjectId(ui->comboBoxThirdPoint));
153 
154  QColor color = okColor;
155  if (set.size() != 3)
156  {
157  flagError = false;
158  color = errorColor;
159  }
160  else
161  {
162  flagError = true;
163  color = okColor;
164  }
165  ChangeColor(ui->labelFirstPoint, color);
166  ChangeColor(ui->labelSecondPoint, color);
167  ChangeColor(ui->labelThirdPoint, color);
168  CheckState();
169 }
170 
171 //---------------------------------------------------------------------------------------------------------------------
173 {
174  EditFormulaDialog *dialog = new EditFormulaDialog(data, toolId, this);
175  dialog->setWindowTitle(tr("Edit length"));
176  dialog->SetFormula(GetFormula());
177  dialog->setPostfix(UnitsToStr(qApp->patternUnit(), true));
178  if (dialog->exec() == QDialog::Accepted)
179  {
180  SetFormula(dialog->GetFormula());
181  }
182  delete dialog;
183 }
184 
185 //---------------------------------------------------------------------------------------------------------------------
187 {
188  AddVisualization<VisToolBisector>();
189 }
190 
191 //---------------------------------------------------------------------------------------------------------------------
193 {
194  DeployFormula(ui->plainTextEditFormula, ui->pushButtonGrowLength, formulaBaseHeight);
195 }
196 
197 //---------------------------------------------------------------------------------------------------------------------
199 {
200  delete ui;
201 }
202 
203 //---------------------------------------------------------------------------------------------------------------------
204 /**
205  * @brief ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
206  * @param id id of point or detail
207  * @param type type of object
208  */
209 void DialogBisector::ChosenObject(quint32 id, const SceneObject &type)
210 {
211  if (prepare == false)// After first choose we ignore all objects
212  {
213  if (type == SceneObject::Point)
214  {
215  VisToolBisector *line = qobject_cast<VisToolBisector *>(vis);
216  SCASSERT(line != nullptr)
217 
218  switch (number)
219  {
220  case 0:
221  if (SetObject(id, ui->comboBoxFirstPoint, tr("Select second point of angle")))
222  {
223  number++;
224  line->VisualMode(id);
225  }
226  break;
227  case 1:
228  if (getCurrentObjectId(ui->comboBoxFirstPoint) != id)
229  {
230  if (SetObject(id, ui->comboBoxSecondPoint, tr("Select third point of angle")))
231  {
232  number++;
233  line->setObject2Id(id);
234  line->RefreshGeometry();
235  }
236  }
237  break;
238  case 2:
239  {
240  QSet<quint32> set;
241  set.insert(getCurrentObjectId(ui->comboBoxFirstPoint));
242  set.insert(getCurrentObjectId(ui->comboBoxSecondPoint));
243  set.insert(id);
244 
245  if (set.size() == 3)
246  {
247  if (SetObject(id, ui->comboBoxThirdPoint, ""))
248  {
249  line->setObject3Id(id);
250  line->RefreshGeometry();
251  prepare = true;
252  this->setModal(true);
253  this->show();
254  }
255  }
256  }
257  break;
258  default:
259  break;
260  }
261  }
262  }
263 }
264 
265 //---------------------------------------------------------------------------------------------------------------------
266 /**
267  * @brief SetPointName set name of point
268  * @param value name
269  */
270 void DialogBisector::SetPointName(const QString &value)
271 {
272  pointName = value;
273  ui->lineEditNamePoint->setText(pointName);
274 }
275 
276 //---------------------------------------------------------------------------------------------------------------------
277 /**
278  * @brief getLineType return type of line
279  * @return type
280  */
282 {
283  return GetComboBoxCurrentData(ui->lineType_ComboBox, LineTypeSolidLine);
284 }
285 
286 //---------------------------------------------------------------------------------------------------------------------
287 /**
288  * @brief setLineType set type of line
289  * @param value type
290  */
291 void DialogBisector::setLineType(const QString &value)
292 {
293  ChangeCurrentData(ui->lineType_ComboBox, value);
294  vis->setLineStyle(lineTypeToPenStyle(value));
295 }
296 
297 //---------------------------------------------------------------------------------------------------------------------
298 /**
299  * @brief getLineWeight return weight of the lines
300  * @return type
301  */
303 {
304  return GetComboBoxCurrentData(ui->lineWeight_ComboBox, "0.35");
305 }
306 
307 //---------------------------------------------------------------------------------------------------------------------
308 /**
309  * @brief setLineWeight set weight of the lines
310  * @param value type
311  */
312 void DialogBisector::setLineWeight(const QString &value)
313 {
314  ChangeCurrentData(ui->lineWeight_ComboBox, value);
315  vis->setLineWeight(value);
316 }
317 
318 //---------------------------------------------------------------------------------------------------------------------
319 /**
320  * @brief getLineColor get the color of line
321  * @param value type
322  */
324 {
325  return GetComboBoxCurrentData(ui->lineColor_ComboBox, ColorBlack);
326 }
327 
328 //---------------------------------------------------------------------------------------------------------------------
329 /* @brief setLineColor set color of the line
330  * @param value type
331  */
332 void DialogBisector::setLineColor(const QString &value)
333 {
334  ChangeCurrentData(ui->lineColor_ComboBox, value);
335 }
336 
337 //---------------------------------------------------------------------------------------------------------------------
338 /**
339  * @brief SetFormula set string of formula
340  * @param value formula
341  */
342 void DialogBisector::SetFormula(const QString &value)
343 {
344  formula = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
345  // increase height if needed.
346  if (formula.length() > 80)
347  {
348  this->DeployFormulaTextEdit();
349  }
350  ui->plainTextEditFormula->setPlainText(formula);
351 
352  VisToolBisector *line = qobject_cast<VisToolBisector *>(vis);
353  SCASSERT(line != nullptr)
354  line->setLength(formula);
355 
356  MoveCursorToEnd(ui->plainTextEditFormula);
357 }
358 
359 //---------------------------------------------------------------------------------------------------------------------
360 /**
361  * @brief SetFirstPointId set id of first point
362  * @param value id
363  */
364 void DialogBisector::SetFirstPointId(const quint32 &value)
365 {
366  setCurrentPointId(ui->comboBoxFirstPoint, value);
367 
368  VisToolBisector *line = qobject_cast<VisToolBisector *>(vis);
369  SCASSERT(line != nullptr)
370  line->setObject1Id(value);
371 }
372 
373 //---------------------------------------------------------------------------------------------------------------------
374 /**
375  * @brief SetSecondPointId set id of second point
376  * @param value id
377  */
378 void DialogBisector::SetSecondPointId(const quint32 &value)
379 {
380  setCurrentPointId(ui->comboBoxSecondPoint, value);
381 
382  VisToolBisector *line = qobject_cast<VisToolBisector *>(vis);
383  SCASSERT(line != nullptr)
384  line->setObject2Id(value);
385 }
386 
387 //---------------------------------------------------------------------------------------------------------------------
388 /**
389  * @brief SetThirdPointId set id of third point
390  * @param value id
391  */
392 void DialogBisector::SetThirdPointId(const quint32 &value)
393 {
394  setCurrentPointId(ui->comboBoxThirdPoint, value);
395 
396  VisToolBisector *line = qobject_cast<VisToolBisector *>(vis);
397  SCASSERT(line != nullptr)
398  line->setObject3Id(value);
399 }
400 
401 //---------------------------------------------------------------------------------------------------------------------
403 {
404  pointName = ui->lineEditNamePoint->text();
405 
406  formula = ui->plainTextEditFormula->toPlainText();
407  formula.replace("\n", " ");
408 
409  VisToolBisector *line = qobject_cast<VisToolBisector *>(vis);
410  SCASSERT(line != nullptr)
411 
412  line->setObject1Id(GetFirstPointId());
414  line->setObject3Id(GetThirdPointId());
415  line->setLength(formula);
417  line->setLineWeight(getLineWeight());
418  line->RefreshGeometry();
419 }
420 
421 //---------------------------------------------------------------------------------------------------------------------
422 void DialogBisector::closeEvent(QCloseEvent *event)
423 {
424  ui->plainTextEditFormula->blockSignals(true);
425  DialogTool::closeEvent(event);
426 }
427 
428 //---------------------------------------------------------------------------------------------------------------------
429 /**
430  * @brief GetFormula return string of formula
431  * @return formula
432  */
434 {
435  return qApp->TrVars()->TryFormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
436 }
437 
438 //---------------------------------------------------------------------------------------------------------------------
439 /**
440  * @brief GetFirstPointId return id of first point
441  * @return id
442  */
444 {
445  return getCurrentObjectId(ui->comboBoxFirstPoint);
446 }
447 
448 //---------------------------------------------------------------------------------------------------------------------
449 /**
450  * @brief GetSecondPointId return id of second point
451  * @return id
452  */
454 {
455  return getCurrentObjectId(ui->comboBoxSecondPoint);
456 }
457 
458 //---------------------------------------------------------------------------------------------------------------------
459 /**
460  * @brief GetThirdPointId return id of third point
461  * @return id
462  */
464 {
465  return getCurrentObjectId(ui->comboBoxThirdPoint);
466 }
The DialogBisector class dialog for ToolBisector. Help create point and edit option.
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
void setLineType(const QString &value)
setLineType set type of line
quint32 GetFirstPointId() const
GetFirstPointId return id of first point.
virtual void ShowVisualization() Q_DECL_OVERRIDE
void DeployFormulaTextEdit()
DeployFormulaTextEdit grow or shrink formula input.
quint32 GetSecondPointId() const
GetSecondPointId return id of second point.
void setLineColor(const QString &value)
void SetSecondPointId(const quint32 &value)
SetSecondPointId set id of second point.
quint32 GetThirdPointId() const
GetThirdPointId return id of third point.
QString GetFormula() const
GetFormula return string of formula.
QString formula
formula formula
QString getLineColor() const
getLineColor get the color of line
void setLineWeight(const QString &value)
setLineWeight set weight of the lines
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.
DialogBisector(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
DialogBisector create dialog.
virtual void PointNameChanged() Q_DECL_OVERRIDE
void SetFirstPointId(const quint32 &value)
SetFirstPointId set id of first point.
QString getLineWeight() const
getLineWeight return weight of the lines
void SetThirdPointId(const quint32 &value)
SetThirdPointId set id of third point.
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
void FormulaTextChanged()
FormulaTextChanged when formula text changes for validation and calc.
int formulaBaseHeight
formulaBaseHeight base height defined by dialogui
void SetPointName(const QString &value)
SetPointName set name of point.
QString getLineType() const
getLineType return type of line
Ui::DialogBisector * ui
ui keeps information about user interface
void SetFormula(const QString &value)
SetFormula set string of formula.
virtual ~DialogBisector() Q_DECL_OVERRIDE
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
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
void NamePointChanged()
NamePointChanged check name of point.
void MoveCursorToEnd(QPlainTextEdit *plainTextEdit) const
Definition: dialogtool.cpp:432
bool flagFormula
flagFormula true if formula correct
Definition: dialogtool.h:186
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
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
Definition: dialogtool.cpp:192
bool SetObject(const quint32 &id, QComboBox *box, const QString &toolTip)
Definition: dialogtool.cpp:974
QLabel * labelEditNamePoint
labelEditNamePoint label used when need show wrong name of point
Definition: dialogtool.h:214
quint32 toolId
Definition: dialogtool.h:225
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
void initializeFormulaUi(T *ui)
initializeFormulaUi initialize ui object for the formula field
Definition: dialogtool.h:398
QPointer< Visualization > vis
Definition: dialogtool.h:236
void FormulaChangedPlainText()
FormulaChangedPlainText check formula (plain text editor editor)
void ChangeColor(QWidget *widget, const QColor &color)
void DeployFormula(QPlainTextEdit *formula, QPushButton *buttonGrowLength, int formulaBaseHeight)
Definition: dialogtool.cpp:992
quint32 getCurrentObjectId(QComboBox *box) const
getCurrentPointId return current point id stored in combobox
Definition: dialogtool.cpp:959
The EditFormulaDialog class dialog for editing wrong formula.
QString GetFormula() const
void SetFormula(const QString &value)
void setPostfix(const QString &value)
The VContainer class container of all variables.
Definition: vcontainer.h:141
void setLength(const QString &expression)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setObject3Id(const quint32 &value)
void setObject2Id(const quint32 &value)
void setLineWeight(const QString &value)
virtual void VisualMode(const quint32 &pointId)
void setObject1Id(const quint32 &value)
void setLineStyle(const Qt::PenStyle &value)
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
Definition: def.cpp:702
#define SCASSERT(cond)
Definition: def.h:317
SceneObject
Definition: def.h:103
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
const QString LineTypeDashLine
Definition: ifcdef.cpp:160
const QString ColorBlack
Definition: ifcdef.cpp:373
Qt::PenStyle lineTypeToPenStyle(const QString &lineType)
LineStyle return pen style for current line style.
Definition: ifcdef.cpp:183
#define qApp
Definition: vapplication.h:67