Seamly2D
Code documentation
dialogendline.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 dialogendline.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 "dialogendline.h"
53 
54 #include <QDialog>
55 #include <QLineEdit>
56 #include <QLineF>
57 #include <QPlainTextEdit>
58 #include <QPointF>
59 #include <QPointer>
60 #include <QPushButton>
61 #include <QSharedPointer>
62 #include <QTimer>
63 #include <QToolButton>
64 #include <new>
65 
66 #include "../vgeometry/vpointf.h"
67 #include "../vpatterndb/vcontainer.h"
68 #include "../vpatterndb/vtranslatevars.h"
69 #include "../vwidgets/vmaingraphicsscene.h"
70 #include "../vwidgets/vabstractmainwindow.h"
71 #include "../../tools/vabstracttool.h"
72 #include "../../visualization/line/vistoolendline.h"
73 #include "../../visualization/visualization.h"
74 #include "../ifc/xml/vabstractpattern.h"
75 #include "../ifc/xml/vdomdocument.h"
76 #include "../support/edit_formula_dialog.h"
77 #include "../vmisc/vabstractapplication.h"
78 #include "../vmisc/vcommonsettings.h"
79 #include "ui_dialogendline.h"
80 
81 //---------------------------------------------------------------------------------------------------------------------
82 /**
83  * @brief DialogEndLine create dialog
84  * @param data container with data
85  * @param parent parent widget
86  */
87 DialogEndLine::DialogEndLine(const VContainer *data, const quint32 &toolId, QWidget *parent)
88  : DialogTool(data, toolId, parent),
89  ui(new Ui::DialogEndLine),
90  formulaLength(),
91  formulaAngle(),
92  formulaBaseHeight(0),
93  formulaBaseHeightAngle(0),
94  m_firstRelease(false)
95 {
96  ui->setupUi(this);
97  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
98  setWindowIcon(QIcon(":/toolicon/32x32/segment.png"));
99 
100  ui->lineEditNamePoint->setClearButtonEnabled(true);
101 
103  ui->lineEditNamePoint->setText(qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
104  labelEditNamePoint = ui->labelEditNamePoint;
105  this->formulaBaseHeight = ui->plainTextEditFormula->height();
106  this->formulaBaseHeightAngle = ui->plainTextEditAngle->height();
107 
108  ui->plainTextEditFormula->installEventFilter(this);
109  ui->plainTextEditAngle->installEventFilter(this);
110 
112  flagFormula = false;
114 
115  FillComboBoxPoints(ui->comboBoxBasePoint);
116 
117  int index = ui->lineColor_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineColor());
118  if (index != -1)
119  {
120  ui->lineColor_ComboBox->setCurrentIndex(index);
121  }
122 
123  index = ui->lineWeight_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineWeight());
124  if (index != -1)
125  {
126  ui->lineWeight_ComboBox->setCurrentIndex(index);
127  }
128 
129  index = ui->lineType_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineType());
130  if (index != -1)
131  {
132  ui->lineType_ComboBox->setCurrentIndex(index);
133  }
134 
135  connect(ui->toolButtonExprLength, &QPushButton::clicked, this, &DialogEndLine::FXLength);
136  connect(ui->toolButtonExprAngle, &QPushButton::clicked, this, &DialogEndLine::FXAngle);
137 
138  connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, &DialogEndLine::NamePointChanged);
139 
140  connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogEndLine::FormulaTextChanged);
141  connect(ui->plainTextEditAngle, &QPlainTextEdit::textChanged, this, &DialogEndLine::AngleTextChanged);
142 
143  connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogEndLine::DeployFormulaTextEdit);
144  connect(ui->pushButtonGrowLengthAngle, &QPushButton::clicked, this, &DialogEndLine::DeployAngleTextEdit);
145 
146  connect(timerFormula, &QTimer::timeout, this, &DialogEndLine::EvalAngle);
147 
148  vis = new VisToolEndLine(data);
149 }
150 
151 //---------------------------------------------------------------------------------------------------------------------
152 /**
153  * @brief EvalAngle calculate value of angle
154  */
156 {
157  labelEditFormula = ui->labelEditAngle;
158  Eval(ui->plainTextEditAngle->toPlainText(), flagError, ui->labelResultCalculationAngle, degreeSymbol, false);
159  labelEditFormula = ui->labelEditFormula;
160 }
161 
162 //---------------------------------------------------------------------------------------------------------------------
164 {
165  this->FormulaChangedPlainText();
166 }
167 
168 //---------------------------------------------------------------------------------------------------------------------
170 {
171  labelEditFormula = ui->labelEditAngle;
172  ValFormulaChanged(flagError, ui->plainTextEditAngle, timerFormula, degreeSymbol);
173  labelEditFormula = ui->labelEditFormula;
174 }
175 
176 //---------------------------------------------------------------------------------------------------------------------
178 {
179  DeployFormula(ui->plainTextEditFormula, ui->pushButtonGrowLength, formulaBaseHeight);
180 }
181 
182 //---------------------------------------------------------------------------------------------------------------------
184 {
185  DeployFormula(ui->plainTextEditAngle, ui->pushButtonGrowLengthAngle, formulaBaseHeightAngle);
186 }
187 
188 //---------------------------------------------------------------------------------------------------------------------
190 {
191  EditFormulaDialog *dialog = new EditFormulaDialog(data, toolId, this);
192  dialog->setWindowTitle(tr("Edit angle"));
193  dialog->SetFormula(GetAngle());
194  dialog->setPostfix(degreeSymbol);
195  if (dialog->exec() == QDialog::Accepted)
196  {
197  SetAngle(dialog->GetFormula());
198  }
199  delete dialog;
200 }
201 
202 //---------------------------------------------------------------------------------------------------------------------
204 {
205  EditFormulaDialog *dialog = new EditFormulaDialog(data, toolId, this);
206  dialog->setWindowTitle(tr("Edit length"));
207  dialog->SetFormula(GetFormula());
208  dialog->setPostfix(UnitsToStr(qApp->patternUnit(), true));
209  if (dialog->exec() == QDialog::Accepted)
210  {
211  SetFormula(dialog->GetFormula());
212  }
213  delete dialog;
214 }
215 
216 //---------------------------------------------------------------------------------------------------------------------
217 /**
218  * @brief ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
219  * @param id id of point or detail
220  * @param type type of object
221  */
222 void DialogEndLine::ChosenObject(quint32 id, const SceneObject &type)
223 {
224  if (prepare == false)// After first choose we ignore all objects
225  {
226  if (type == SceneObject::Point)
227  {
228  if (SetObject(id, ui->comboBoxBasePoint, ""))
229  {
230  vis->VisualMode(id);
231  VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow());
232  SCASSERT(window != nullptr)
233  connect(vis.data(), &Visualization::ToolTip, window, &VAbstractMainWindow::ShowToolTip);
234  prepare = true;
235  }
236  }
237  }
238 }
239 
240 //---------------------------------------------------------------------------------------------------------------------
241 /**
242  * @brief SetPointName set name of point
243  * @param value name
244  */
245 void DialogEndLine::SetPointName(const QString &value)
246 {
247  pointName = value;
248  ui->lineEditNamePoint->setText(pointName);
249 }
250 
251 //---------------------------------------------------------------------------------------------------------------------
252 /**
253  * @brief setLineType set type of line
254  * @param value type
255  */
256 void DialogEndLine::setLineType(const QString &value)
257 {
258  ChangeCurrentData(ui->lineType_ComboBox, value);
259  vis->setLineStyle(lineTypeToPenStyle(value));
260 }
261 
262 //---------------------------------------------------------------------------------------------------------------------
263 /**
264  * @brief getLineWeight return weight of the lines
265  * @return type
266  */
268 {
269  return GetComboBoxCurrentData(ui->lineWeight_ComboBox, "0.35");
270 }
271 
272 //---------------------------------------------------------------------------------------------------------------------
273 /**
274  * @brief setLineWeight set weight of the lines
275  * @param value type
276  */
277 void DialogEndLine::setLineWeight(const QString &value)
278 {
279  ChangeCurrentData(ui->lineWeight_ComboBox, value);
280  vis->setLineWeight(value);
281 }
282 
283 //---------------------------------------------------------------------------------------------------------------------
284 /**
285  * @brief SetFormula set string of formula
286  * @param value formula
287  */
288 void DialogEndLine::SetFormula(const QString &value)
289 {
290  formulaLength = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
291  // increase height if needed. TODO : see if I can get the max number of caracters in one line
292  // of this PlainTextEdit to change 80 to this value
293  if (formulaLength.length() > 80)
294  {
295  this->DeployFormulaTextEdit();
296  }
297  ui->plainTextEditFormula->setPlainText(formulaLength);
298 
299  VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
300  SCASSERT(line != nullptr)
301  line->setLength(formulaLength);
302 
303  MoveCursorToEnd(ui->plainTextEditFormula);
304 }
305 
306 //---------------------------------------------------------------------------------------------------------------------
307 /**
308  * @brief SetAngle set angle of line
309  * @param value angle in degree
310  */
311 void DialogEndLine::SetAngle(const QString &value)
312 {
313  formulaAngle = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
314  // increase height if needed. TODO : see if I can get the max number of caracters in one line
315  // of this PlainTextEdit to change 80 to this value
316  if (formulaAngle.length() > 80)
317  {
318  this->DeployAngleTextEdit();
319  }
320  ui->plainTextEditAngle->setPlainText(formulaAngle);
321 
322  VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
323  SCASSERT(line != nullptr)
324  line->SetAngle(formulaAngle);
325 
326  MoveCursorToEnd(ui->plainTextEditAngle);
327 }
328 
329 //---------------------------------------------------------------------------------------------------------------------
330 /**
331  * @brief SetBasePointId set id base point of line
332  * @param value id
333  */
334 void DialogEndLine::SetBasePointId(const quint32 &value)
335 {
336  setCurrentPointId(ui->comboBoxBasePoint, value);
337 
338  VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
339  SCASSERT(line != nullptr)
340  line->setObject1Id(value);
341 }
342 
343 //---------------------------------------------------------------------------------------------------------------------
344 /**
345  * @brief getLineColor get the color of line
346  * @param value type
347  */
348 //---------------------------------------------------------------------------------------------------------------------
350 {
351  return GetComboBoxCurrentData(ui->lineColor_ComboBox, ColorBlack);
352 }
353 
354 /**
355  * @brief setLineColor set color of the line
356  * @param value type
357  */
358 //---------------------------------------------------------------------------------------------------------------------
359 void DialogEndLine::setLineColor(const QString &value)
360 {
361  ChangeCurrentData(ui->lineColor_ComboBox, value);
362 }
363 
364 //---------------------------------------------------------------------------------------------------------------------
365 /**
366  * @brief DialogEndLine::ShowDialog show dialog after finish working with visual part
367  * @param click true if need show dialog after click mouse
368  */
370 {
371  if (prepare)
372  {
373  if (click)
374  {
375  // The check need to ignore first release of mouse button.
376  // User can select point by clicking on a label.
377  if (not m_firstRelease)
378  {
379  m_firstRelease = true;
380  return;
381  }
382 
383  /*We will ignore click if pointer is in point circle*/
384  VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(qApp->getCurrentScene());
385  SCASSERT(scene != nullptr)
387  QLineF line = QLineF(static_cast<QPointF>(*point), scene->getScenePos());
388 
389  //Radius of point circle, but little bigger. Need handle with hover sizes.
390  if (line.length() <= defPointRadiusPixel*1.5)
391  {
392  return;
393  }
394  }
395  this->setModal(true);
396 
397  VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
398  SCASSERT(line != nullptr)
399 
400  this->SetAngle(line->Angle());//Show in dialog angle what user choose
401  this->SetFormula(line->Length());
402  emit ToolTip("");
403  timerFormula->start();
404  this->show();
405  }
406 }
407 
408 //---------------------------------------------------------------------------------------------------------------------
410 {
411  AddVisualization<VisToolEndLine>();
412 }
413 
414 //---------------------------------------------------------------------------------------------------------------------
416 {
417  pointName = ui->lineEditNamePoint->text();
418 
419  formulaLength = ui->plainTextEditFormula->toPlainText();
420  formulaLength.replace("\n", " ");
421 
422  formulaAngle = ui->plainTextEditAngle->toPlainText();
423  formulaAngle.replace("\n", " ");
424 
425  VisToolEndLine *line = qobject_cast<VisToolEndLine *>(vis);
426  SCASSERT(line != nullptr)
427 
428  line->setObject1Id(GetBasePointId());
429  line->setLength(formulaLength);
430  line->SetAngle(formulaAngle);
432  line->setLineWeight(getLineWeight());
433  line->RefreshGeometry();
434 }
435 
436 //---------------------------------------------------------------------------------------------------------------------
437 void DialogEndLine::closeEvent(QCloseEvent *event)
438 {
439  ui->plainTextEditFormula->blockSignals(true);
440  ui->plainTextEditAngle->blockSignals(true);
441  DialogTool::closeEvent(event);
442 }
443 
444 //---------------------------------------------------------------------------------------------------------------------
446 {
447  delete ui;
448 }
449 
450 //---------------------------------------------------------------------------------------------------------------------
451 /**
452  * @brief getLineType return type of line
453  * @return type
454  */
456 {
457  return GetComboBoxCurrentData(ui->lineType_ComboBox, LineTypeSolidLine);
458 }
459 
460 //---------------------------------------------------------------------------------------------------------------------
461 /**
462  * @brief GetFormula return string of formula
463  * @return formula
464  */
466 {
467  return qApp->TrVars()->TryFormulaFromUser(formulaLength, qApp->Settings()->GetOsSeparator());
468 }
469 
470 //---------------------------------------------------------------------------------------------------------------------
471 /**
472  * @brief GetAngle return formula angle of line
473  * @return angle formula
474  */
475 QString DialogEndLine::GetAngle() const
476 {
477  return qApp->TrVars()->TryFormulaFromUser(formulaAngle, qApp->Settings()->GetOsSeparator());
478 }
479 
480 //---------------------------------------------------------------------------------------------------------------------
481 /**
482  * @brief GetBasePointId return id base point of line
483  * @return id
484  */
486 {
487  return getCurrentObjectId(ui->comboBoxBasePoint);
488 }
The DialogEndLine class dialog for ToolEndLine. Help create point and edit option.
Definition: dialogendline.h:73
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
void FormulaTextChanged()
FormulaTextChanged when formula text changes for validation and calc.
void EvalAngle()
EvalAngle calculate value of angle.
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 AngleTextChanged()
void DeployFormulaTextEdit()
DeployFormulaTextEdit grow or shrink formula input.
void SetPointName(const QString &value)
SetPointName set name of point.
void SetAngle(const QString &value)
SetAngle set angle of line.
QString formulaAngle
angle angle of line
virtual ~DialogEndLine() Q_DECL_OVERRIDE
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
void setLineType(const QString &value)
setLineType set type of line
DialogEndLine(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
DialogEndLine create dialog.
void SetFormula(const QString &value)
SetFormula set string of formula.
void SetBasePointId(const quint32 &value)
SetBasePointId set id base point of line.
void DeployAngleTextEdit()
int formulaBaseHeight
formulaBaseHeight base height defined by dialogui
quint32 GetBasePointId() const
GetBasePointId return id base point of line.
void setLineColor(const QString &value)
setLineColor set color of the line
QString getLineWeight() const
getLineWeight return weight of the lines
QString getLineColor() const
getLineColor get the color of line
void setLineWeight(const QString &value)
setLineWeight set weight of the lines
virtual void ShowVisualization() Q_DECL_OVERRIDE
QString formulaLength
formula formula
QString GetFormula() const
GetFormula return string of formula.
Ui::DialogEndLine * ui
ui keeps information about user interface
int formulaBaseHeightAngle
virtual void ShowDialog(bool click) Q_DECL_OVERRIDE
DialogEndLine::ShowDialog show dialog after finish working with visual part.
QString GetAngle() const
GetAngle return formula angle of line.
QString getLineType() const
getLineType return type of line
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
void ToolTip(const QString &toolTip)
ToolTip emit tooltipe for tool.
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
qreal Eval(const QString &text, bool &flag, QLabel *label, const QString &postfix, bool checkZero=true, bool checkLessThanZero=false)
Eval evaluate formula and show result.
Definition: dialogtool.cpp:805
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
Definition: dialogtool.cpp:192
void ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer *timer, const QString &postfix=QString())
ValFormulaChanged handle change formula.
Definition: dialogtool.cpp:744
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
QLabel * labelEditFormula
labelEditFormula label used when need show wrong formula
Definition: dialogtool.h:217
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
QTimer * timerFormula
timerFormula timer for check formula
Definition: dialogtool.h:196
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 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)
virtual void ShowToolTip(const QString &toolTip)=0
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
The VMainGraphicsScene class main scene.
QPointF getScenePos() const
The VPointF class keep data of point.
Definition: vpointf.h:75
void SetAngle(const QString &expression)
QString Angle() const
QString Length() const
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLength(const QString &expression)
void ToolTip(const QString &toolTip)
void setLineWeight(const QString &value)
void setObject1Id(const quint32 &value)
void setLineStyle(const Qt::PenStyle &value)
const QString degreeSymbol
Definition: def.cpp:196
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 qreal defPointRadiusPixel
Definition: global.cpp:59
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
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