Seamly2D
Code documentation
dialognormal.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 dialognormal.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 "dialognormal.h"
53 
54 #include <QColor>
55 #include <QComboBox>
56 #include <QDialog>
57 #include <QDoubleSpinBox>
58 #include <QLabel>
59 #include <QLineEdit>
60 #include <QPlainTextEdit>
61 #include <QPointer>
62 #include <QPushButton>
63 #include <QToolButton>
64 
65 #include "../vpatterndb/vtranslatevars.h"
66 #include "../../tools/vabstracttool.h"
67 #include "../../visualization/line/vistoolnormal.h"
68 #include "../../visualization/visualization.h"
69 #include "../ifc/xml/vabstractpattern.h"
70 #include "../ifc/xml/vdomdocument.h"
71 #include "../support/edit_formula_dialog.h"
72 #include "../vmisc/vabstractapplication.h"
73 #include "../vmisc/vcommonsettings.h"
74 #include "ui_dialognormal.h"
75 
76 //---------------------------------------------------------------------------------------------------------------------
77 /**
78  * @brief DialogNormal create dialog
79  * @param data container with data
80  * @param parent parent widget
81  */
82 DialogNormal::DialogNormal(const VContainer *data, const quint32 &toolId, QWidget *parent)
83  : DialogTool(data, toolId, parent), ui(new Ui::DialogNormal)
84  , formula(QString())
85  , angle(0)
86  , formulaBaseHeight(0)
87 {
88  ui->setupUi(this);
89  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
90  setWindowIcon(QIcon(":/toolicon/32x32/normal.png"));
91 
92  ui->lineEditNamePoint->setClearButtonEnabled(true);
93 
95  ui->lineEditNamePoint->setText(qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
96  labelEditNamePoint = ui->labelEditNamePoint;
97  this->formulaBaseHeight = ui->plainTextEditFormula->height();
98  ui->plainTextEditFormula->installEventFilter(this);
99 
101  flagFormula = false;
103 
104  FillComboBoxPoints(ui->comboBoxFirstPoint);
105  FillComboBoxPoints(ui->comboBoxSecondPoint);
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, &DialogNormal::FXLength);
126  connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, &DialogNormal::NamePointChanged);
127  connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogNormal::FormulaTextChanged);
128  connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogNormal::DeployFormulaTextEdit);
129  connect(ui->comboBoxFirstPoint, &QComboBox::currentTextChanged, this, &DialogNormal::PointNameChanged);
130  connect(ui->comboBoxSecondPoint, &QComboBox::currentTextChanged, this, &DialogNormal::PointNameChanged);
131 
132  vis = new VisToolNormal(data);
133 
134  // Call after initialization vis!!!!
136  setLineWeight("0.35");
137 }
138 
139 //---------------------------------------------------------------------------------------------------------------------
141 {
142  this->FormulaChangedPlainText();
143 }
144 
145 //---------------------------------------------------------------------------------------------------------------------
147 {
148  QColor color = okColor;
149  if (getCurrentObjectId(ui->comboBoxFirstPoint) == getCurrentObjectId(ui->comboBoxSecondPoint))
150  {
151  flagError = false;
152  color = errorColor;
153  }
154  else
155  {
156  flagError = true;
157  color = okColor;
158  }
159  ChangeColor(ui->labelFirstPoint, color);
160  ChangeColor(ui->labelSecondPoint, color);
161  CheckState();
162 }
163 
164 //---------------------------------------------------------------------------------------------------------------------
166 {
167  EditFormulaDialog *dialog = new EditFormulaDialog(data, toolId, this);
168  dialog->setWindowTitle(tr("Edit length"));
169  dialog->SetFormula(GetFormula());
170  dialog->setPostfix(UnitsToStr(qApp->patternUnit(), true));
171  if (dialog->exec() == QDialog::Accepted)
172  {
173  SetFormula(dialog->GetFormula());
174  }
175  delete dialog;
176 }
177 
178 //---------------------------------------------------------------------------------------------------------------------
180 {
181  AddVisualization<VisToolNormal>();
182 }
183 
184 //---------------------------------------------------------------------------------------------------------------------
186 {
187  DeployFormula(ui->plainTextEditFormula, ui->pushButtonGrowLength, formulaBaseHeight);
188 }
189 
190 //---------------------------------------------------------------------------------------------------------------------
192 {
193  delete ui;
194 }
195 
196 //---------------------------------------------------------------------------------------------------------------------
197 /**
198  * @brief ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
199  * @param id id of point or detail
200  * @param type type of object
201  */
202 void DialogNormal::ChosenObject(quint32 id, const SceneObject &type)
203 {
204  if (prepare == false)// After first choose we ignore all objects
205  {
206  if (type == SceneObject::Point)
207  {
208  VisToolNormal *line = qobject_cast<VisToolNormal *>(vis);
209  SCASSERT(line != nullptr)
210 
211  switch (number)
212  {
213  case 0:
214  if (SetObject(id, ui->comboBoxFirstPoint, tr("Select second point of line")))
215  {
216  number++;
217  line->VisualMode(id);
218  }
219  break;
220  case 1:
221  if (getCurrentObjectId(ui->comboBoxFirstPoint) != id)
222  {
223  if (SetObject(id, ui->comboBoxSecondPoint, ""))
224  {
225  line->setObject2Id(id);
226  line->RefreshGeometry();
227  prepare = true;
228  this->setModal(true);
229  this->show();
230  }
231  }
232  break;
233  default:
234  break;
235  }
236  }
237  }
238 }
239 
240 //---------------------------------------------------------------------------------------------------------------------
242 {
243  pointName = ui->lineEditNamePoint->text();
244  formula = ui->plainTextEditFormula->toPlainText();
245  formula.replace("\n", " ");
246  angle = ui->doubleSpinBoxAngle->value();
247 
248  VisToolNormal *line = qobject_cast<VisToolNormal *>(vis);
249  SCASSERT(line != nullptr)
250 
251  line->setObject1Id(GetFirstPointId());
253  line->setLength(formula);
254  line->SetAngle(angle);
256  line->setLineWeight(getLineWeight());
257  line->RefreshGeometry();
258 }
259 
260 //---------------------------------------------------------------------------------------------------------------------
261 void DialogNormal::closeEvent(QCloseEvent *event)
262 {
263  ui->plainTextEditFormula->blockSignals(true);
264  DialogTool::closeEvent(event);
265 }
266 
267 //---------------------------------------------------------------------------------------------------------------------
268 /**
269  * @brief SetSecondPointId set id of second point
270  * @param value id
271  */
272 void DialogNormal::SetSecondPointId(const quint32 &value)
273 {
274  setCurrentPointId(ui->comboBoxSecondPoint, value);
275 
276  VisToolNormal *line = qobject_cast<VisToolNormal *>(vis);
277  SCASSERT(line != nullptr)
278  line->setObject2Id(value);
279 }
280 
281 //---------------------------------------------------------------------------------------------------------------------
282 /**
283  * @brief SetFirstPointId set id of first point
284  * @param value id
285  */
286 void DialogNormal::SetFirstPointId(const quint32 &value)
287 {
288  setCurrentPointId(ui->comboBoxFirstPoint, value);
289 
290  VisToolNormal *line = qobject_cast<VisToolNormal *>(vis);
291  SCASSERT(line != nullptr)
292  line->setObject1Id(value);
293 }
294 
295 //---------------------------------------------------------------------------------------------------------------------
296 /**
297  * @brief SetAngle set aditional angle of normal
298  * @param value angle in degree
299  */
300 void DialogNormal::SetAngle(const qreal &value)
301 {
302  angle = value;
303  ui->doubleSpinBoxAngle->setValue(angle);
304 
305  VisToolNormal *line = qobject_cast<VisToolNormal *>(vis);
306  SCASSERT(line != nullptr)
307  line->SetAngle(angle);
308 }
309 
310 //---------------------------------------------------------------------------------------------------------------------
311 /**
312  * @brief SetFormula set string of formula
313  * @param value formula
314  */
315 void DialogNormal::SetFormula(const QString &value)
316 {
317  formula = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
318  // increase height if needed.
319  if (formula.length() > 80)
320  {
321  this->DeployFormulaTextEdit();
322  }
323  ui->plainTextEditFormula->setPlainText(formula);
324 
325  VisToolNormal *line = qobject_cast<VisToolNormal *>(vis);
326  SCASSERT(line != nullptr)
327  line->setLength(formula);
328 
329  MoveCursorToEnd(ui->plainTextEditFormula);
330 }
331 
332 //---------------------------------------------------------------------------------------------------------------------
333 /**
334  * @brief getLineType return type of line
335  * @return type
336  */
338 {
339  return GetComboBoxCurrentData(ui->lineType_ComboBox, LineTypeSolidLine);
340 }
341 
342 //---------------------------------------------------------------------------------------------------------------------
343 /**
344  * @brief setLineType set type of line
345  * @param value type
346  */
347 void DialogNormal::setLineType(const QString &value)
348 {
349  ChangeCurrentData(ui->lineType_ComboBox, value);
350  vis->setLineStyle(lineTypeToPenStyle(value));
351 }
352 
353 //---------------------------------------------------------------------------------------------------------------------
354 /**
355  * @brief getLineWeight return weight of the lines
356  * @return type
357  */
359 {
360  return GetComboBoxCurrentData(ui->lineWeight_ComboBox, "0.35");
361 }
362 
363 //---------------------------------------------------------------------------------------------------------------------
364 /**
365  * @brief setLineWeight set weight of the lines
366  * @param value type
367  */
368 void DialogNormal::setLineWeight(const QString &value)
369 {
370  ChangeCurrentData(ui->lineWeight_ComboBox, value);
371  vis->setLineWeight(value);
372 }
373 
374 //---------------------------------------------------------------------------------------------------------------------
376 {
377  return GetComboBoxCurrentData(ui->lineColor_ComboBox, ColorBlack);
378 }
379 
380 //---------------------------------------------------------------------------------------------------------------------
381 void DialogNormal::setLineColor(const QString &value)
382 {
383  ChangeCurrentData(ui->lineColor_ComboBox, value);
384 }
385 
386 //---------------------------------------------------------------------------------------------------------------------
387 /**
388  * @brief SetPointName set name of point
389  * @param value name
390  */
391 void DialogNormal::SetPointName(const QString &value)
392 {
393  pointName = value;
394  ui->lineEditNamePoint->setText(pointName);
395 }
396 
397 //---------------------------------------------------------------------------------------------------------------------
398 /**
399  * @brief GetFormula return string of formula
400  * @return formula
401  */
403 {
404  return qApp->TrVars()->TryFormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
405 }
406 
407 //---------------------------------------------------------------------------------------------------------------------
408 /**
409  * @brief GetAngle return aditional angle of normal
410  * @return angle in degree
411  */
413 {
414  return angle;
415 }
416 
417 //---------------------------------------------------------------------------------------------------------------------
418 /**
419  * @brief GetFirstPointId return id of first point
420  * @return id
421  */
423 {
424  return getCurrentObjectId(ui->comboBoxFirstPoint);
425 }
426 
427 //---------------------------------------------------------------------------------------------------------------------
428 /**
429  * @brief GetSecondPointId return id of second point
430  * @return id
431  */
433 {
434  return getCurrentObjectId(ui->comboBoxSecondPoint);
435 }
The DialogNormal class dialog for ToolNormal. Help create point and edit option.
Definition: dialognormal.h:73
int formulaBaseHeight
formulaBaseHeight base height defined by dialogui
Definition: dialognormal.h:136
void setLineType(const QString &value)
setLineType set type of line
quint32 GetFirstPointId() const
GetFirstPointId return id of first point.
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
void SetFirstPointId(const quint32 &value)
SetFirstPointId set id of first point.
void SetPointName(const QString &value)
SetPointName set name of point.
DialogNormal(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
DialogNormal create dialog.
void FormulaTextChanged()
FormulaTextChanged when formula text changes for validation and calc.
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.
void SetAngle(const qreal &value)
SetAngle set aditional angle of normal.
QString getLineType() const
getLineType return type of line
virtual ~DialogNormal() Q_DECL_OVERRIDE
QString formula
formula formula
Definition: dialognormal.h:130
QString getLineColor() const
virtual void ShowVisualization() Q_DECL_OVERRIDE
void DeployFormulaTextEdit()
DeployFormulaTextEdit grow or shrink formula input.
qreal angle
angle aditional angle of normal
Definition: dialognormal.h:133
qreal GetAngle() const
GetAngle return aditional angle of normal.
void setLineWeight(const QString &value)
setLineWeight set weight of the lines
void SetSecondPointId(const quint32 &value)
SetSecondPointId set id of second point.
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
virtual void PointNameChanged() Q_DECL_OVERRIDE
Ui::DialogNormal * ui
ui keeps information about user interface
Definition: dialognormal.h:127
void SetFormula(const QString &value)
SetFormula set string of formula.
QString getLineWeight() const
getLineWeight return weight of the lines
void setLineColor(const QString &value)
QString GetFormula() const
GetFormula return string of formula.
quint32 GetSecondPointId() const
GetSecondPointId return id of second 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
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
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void SetAngle(const qreal &value)
void setObject2Id(const quint32 &value)
void setLength(const QString &expression)
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