Seamly2D
Code documentation
dialogshoulderpoint.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 dialogshoulderpoint.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 "dialogshoulderpoint.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 "../vpatterndb/vtranslatevars.h"
66 #include "../../tools/vabstracttool.h"
67 #include "../../visualization/visualization.h"
68 #include "../../visualization/line/vistoolshoulderpoint.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_dialogshoulderpoint.h"
75 
76 //---------------------------------------------------------------------------------------------------------------------
77 /**
78  * @brief DialogShoulderPoint create dialog
79  * @param data container with data
80  * @param parent parent widget
81  */
82 DialogShoulderPoint::DialogShoulderPoint(const VContainer *data, const quint32 &toolId, QWidget *parent)
83  : DialogTool(data, toolId, parent), ui(new Ui::DialogShoulderPoint), formula(QString()),
84  formulaBaseHeight(0)
85 {
86  ui->setupUi(this);
87  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
88  setWindowIcon(QIcon(":/toolicon/32x32/shoulder.png"));
89 
90  ui->lineEditNamePoint->setClearButtonEnabled(true);
91 
93  ui->lineEditNamePoint->setText(qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
94  labelEditNamePoint = ui->labelEditNamePoint;
95  this->formulaBaseHeight = ui->plainTextEditFormula->height();
96  ui->plainTextEditFormula->installEventFilter(this);
97 
99  flagFormula = false;
101 
102  FillComboBoxPoints(ui->comboBoxP1Line);
103  FillComboBoxPoints(ui->comboBoxP2Line);
104  FillComboBoxPoints(ui->comboBoxP3);
105 
106  int index = ui->lineColor_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineColor());
107  if (index != -1)
108  {
109  ui->lineColor_ComboBox->setCurrentIndex(index);
110  }
111 
112  index = ui->lineWeight_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineWeight());
113  if (index != -1)
114  {
115  ui->lineWeight_ComboBox->setCurrentIndex(index);
116  }
117 
118  index = ui->lineType_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineType());
119  if (index != -1)
120  {
121  ui->lineType_ComboBox->setCurrentIndex(index);
122  }
123 
124  connect(ui->toolButtonExprLength, &QPushButton::clicked, this, &DialogShoulderPoint::FXLength);
125  connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, &DialogShoulderPoint::NamePointChanged);
126  connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogShoulderPoint::FormulaTextChanged);
127  connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogShoulderPoint::DeployFormulaTextEdit);
128  connect(ui->comboBoxP1Line, &QComboBox::currentTextChanged, this, &DialogShoulderPoint::PointNameChanged);
129  connect(ui->comboBoxP2Line, &QComboBox::currentTextChanged, this, &DialogShoulderPoint::PointNameChanged);
130  connect(ui->comboBoxP3, &QComboBox::currentTextChanged, this, &DialogShoulderPoint::PointNameChanged);
131 
133 
134  // Call after initialization vis!!!!
136  setLineWeight("0.35");
137 }
138 
139 //---------------------------------------------------------------------------------------------------------------------
141 {
142  this->FormulaChangedPlainText();
143 }
144 
145 //---------------------------------------------------------------------------------------------------------------------
147 {
148  QSet<quint32> set;
149  set.insert(getCurrentObjectId(ui->comboBoxP1Line));
150  set.insert(getCurrentObjectId(ui->comboBoxP2Line));
151  set.insert(getCurrentObjectId(ui->comboBoxP3));
152 
153  QColor color = okColor;
154  if (set.size() != 3)
155  {
156  flagError = false;
157  color = errorColor;
158  }
159  else
160  {
161  flagError = true;
162  color = okColor;
163  }
164  ChangeColor(ui->labelFirstPoint, color);
165  ChangeColor(ui->labelSecondPoint, color);
166  ChangeColor(ui->labelThirdPoint, color);
167  CheckState();
168 }
169 
170 //---------------------------------------------------------------------------------------------------------------------
172 {
173  EditFormulaDialog *dialog = new EditFormulaDialog(data, toolId, this);
174  dialog->setWindowTitle(tr("Edit length"));
175  dialog->SetFormula(GetFormula());
176  dialog->setPostfix(UnitsToStr(qApp->patternUnit(), true));
177  if (dialog->exec() == QDialog::Accepted)
178  {
179  SetFormula(dialog->GetFormula());
180  }
181  delete dialog;
182 }
183 
184 //---------------------------------------------------------------------------------------------------------------------
186 {
187  AddVisualization<VisToolShoulderPoint>();
188 }
189 
190 //---------------------------------------------------------------------------------------------------------------------
192 {
193  DeployFormula(ui->plainTextEditFormula, ui->pushButtonGrowLength, formulaBaseHeight);
194 }
195 
196 //---------------------------------------------------------------------------------------------------------------------
198 {
199  delete ui;
200 }
201 
202 //---------------------------------------------------------------------------------------------------------------------
203 /**
204  * @brief ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
205  * @param id id of point or detail
206  * @param type type of object
207  */
208 void DialogShoulderPoint::ChosenObject(quint32 id, const SceneObject &type)
209 {
210  if (prepare == false)
211  {
212  if (type == SceneObject::Point)
213  {
214  VisToolShoulderPoint *line = qobject_cast<VisToolShoulderPoint *>(vis);
215  SCASSERT(line != nullptr)
216 
217  switch (number)
218  {
219  case 0:
220  if (SetObject(id, ui->comboBoxP3, tr("Select first point of line")))
221  {
222  number++;
223  line->VisualMode(id);
224  }
225  break;
226  case 1:
227  if (getCurrentObjectId(ui->comboBoxP3) != id)
228  {
229  if (SetObject(id, ui->comboBoxP1Line, tr("Select second point of line")))
230  {
231  number++;
232  line->setLineP1Id(id);
233  line->RefreshGeometry();
234  }
235  }
236  break;
237  case 2:
238  {
239  QSet<quint32> set;
240  set.insert(getCurrentObjectId(ui->comboBoxP3));
241  set.insert(getCurrentObjectId(ui->comboBoxP1Line));
242  set.insert(id);
243 
244  if (set.size() == 3)
245  {
246  if (SetObject(id, ui->comboBoxP2Line, ""))
247  {
248  line->setLineP2Id(id);
249  line->RefreshGeometry();
250  prepare = true;
251  this->setModal(true);
252  this->show();
253  }
254  }
255  }
256  break;
257  default:
258  break;
259  }
260  }
261  }
262 }
263 
264 //---------------------------------------------------------------------------------------------------------------------
266 {
267  pointName = ui->lineEditNamePoint->text();
268  formula = ui->plainTextEditFormula->toPlainText();
269  formula.replace("\n", " ");
270 
271  VisToolShoulderPoint *line = qobject_cast<VisToolShoulderPoint *>(vis);
272  SCASSERT(line != nullptr)
273 
274  line->setObject1Id(GetP3());
275  line->setLineP1Id(GetP1Line());
276  line->setLineP2Id(GetP2Line());
277  line->setLength(formula);
279  line->setLineWeight(getLineWeight());
280  line->RefreshGeometry();
281 }
282 
283 //---------------------------------------------------------------------------------------------------------------------
284 void DialogShoulderPoint::closeEvent(QCloseEvent *event)
285 {
286  ui->plainTextEditFormula->blockSignals(true);
287  DialogTool::closeEvent(event);
288 }
289 
290 //---------------------------------------------------------------------------------------------------------------------
291 /**
292  * @brief setPShoulder set id shoulder point
293  * @param value id
294  */
295 void DialogShoulderPoint::SetP3(const quint32 &value)
296 {
297  setCurrentPointId(ui->comboBoxP3, value);
298 
299  VisToolShoulderPoint *line = qobject_cast<VisToolShoulderPoint *>(vis);
300  SCASSERT(line != nullptr)
301  line->setObject1Id(value);
302 }
303 
304 //---------------------------------------------------------------------------------------------------------------------
306 {
307  return GetComboBoxCurrentData(ui->lineColor_ComboBox, ColorBlack);
308 }
309 
310 //---------------------------------------------------------------------------------------------------------------------
311 void DialogShoulderPoint::setLineColor(const QString &value)
312 {
313  ChangeCurrentData(ui->lineColor_ComboBox, value);
314 }
315 
316 //---------------------------------------------------------------------------------------------------------------------
317 /**
318  * @brief SetP2Line set id second point of line
319  * @param value id
320  */
321 void DialogShoulderPoint::SetP2Line(const quint32 &value)
322 {
323  setCurrentPointId(ui->comboBoxP2Line, value);
324 
325  VisToolShoulderPoint *line = qobject_cast<VisToolShoulderPoint *>(vis);
326  SCASSERT(line != nullptr)
327  line->setLineP2Id(value);
328 }
329 
330 //---------------------------------------------------------------------------------------------------------------------
331 /**
332  * @brief SetP1Line set id first point of line
333  * @param value id
334  */
335 void DialogShoulderPoint::SetP1Line(const quint32 &value)
336 {
337  setCurrentPointId(ui->comboBoxP1Line, value);
338 
339  VisToolShoulderPoint *line = qobject_cast<VisToolShoulderPoint *>(vis);
340  SCASSERT(line != nullptr)
341  line->setLineP1Id(value);
342 }
343 
344 //---------------------------------------------------------------------------------------------------------------------
345 /**
346  * @brief SetFormula set string of formula
347  * @param value formula
348  */
349 void DialogShoulderPoint::SetFormula(const QString &value)
350 {
351  formula = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
352  // increase height if needed.
353  if (formula.length() > 80)
354  {
355  this->DeployFormulaTextEdit();
356  }
357  ui->plainTextEditFormula->setPlainText(formula);
358 
359  VisToolShoulderPoint *line = qobject_cast<VisToolShoulderPoint *>(vis);
360  SCASSERT(line != nullptr)
361  line->setLength(formula);
362  MoveCursorToEnd(ui->plainTextEditFormula);
363 }
364 
365 //---------------------------------------------------------------------------------------------------------------------
366 /**
367  * @brief getLineType return type of line
368  * @return type
369  */
371 {
372  return GetComboBoxCurrentData(ui->lineType_ComboBox, LineTypeSolidLine);
373 }
374 
375 //---------------------------------------------------------------------------------------------------------------------
376 /**
377  * @brief setLineType set type of line
378  * @param value type
379  */
380 void DialogShoulderPoint::setLineType(const QString &value)
381 {
382  ChangeCurrentData(ui->lineType_ComboBox, value);
383  vis->setLineStyle(lineTypeToPenStyle(value));
384 }
385 
386 //---------------------------------------------------------------------------------------------------------------------
387 /**
388  * @brief getLineWeight return weight of the lines
389  * @return type
390  */
392 {
393  return GetComboBoxCurrentData(ui->lineWeight_ComboBox, "0.35");
394 }
395 
396 //---------------------------------------------------------------------------------------------------------------------
397 /**
398  * @brief setLineWeight set weight of the lines
399  * @param value type
400  */
401 void DialogShoulderPoint::setLineWeight(const QString &value)
402 {
403  ChangeCurrentData(ui->lineWeight_ComboBox, value);
404  vis->setLineWeight(value);
405 }
406 
407 //---------------------------------------------------------------------------------------------------------------------
408 /**
409  * @brief SetPointName set name of point
410  * @param value name
411  */
412 void DialogShoulderPoint::SetPointName(const QString &value)
413 {
414  pointName = value;
415  ui->lineEditNamePoint->setText(pointName);
416 }
417 
418 //---------------------------------------------------------------------------------------------------------------------
419 /**
420  * @brief GetFormula return string of formula
421  * @return formula
422  */
424 {
425  return qApp->TrVars()->TryFormulaFromUser(formula, qApp->Settings()->GetOsSeparator());
426 }
427 
428 //---------------------------------------------------------------------------------------------------------------------
429 /**
430  * @brief GetP1Line return id first point of line
431  * @return id
432  */
434 {
435  return getCurrentObjectId(ui->comboBoxP1Line);
436 }
437 
438 //---------------------------------------------------------------------------------------------------------------------
439 /**
440  * @brief GetP2Line return id second point of line
441  * @return id
442  */
444 {
445  return getCurrentObjectId(ui->comboBoxP2Line);
446 }
447 
448 //---------------------------------------------------------------------------------------------------------------------
449 /**
450  * @brief getPShoulder return id shoulder point
451  * @return id
452  */
454 {
455  return getCurrentObjectId(ui->comboBoxP3);
456 }
The DialogShoulderPoint class dialog for ToolShoulderPoint. Help create point and edit option.
quint32 GetP3() const
getPShoulder return id shoulder point
void FormulaTextChanged()
FormulaTextChanged when formula text changes for validation and calc.
virtual ~DialogShoulderPoint() Q_DECL_OVERRIDE
virtual void PointNameChanged() Q_DECL_OVERRIDE
QString GetFormula() const
GetFormula return string of formula.
void SetFormula(const QString &value)
SetFormula set string of formula.
int formulaBaseHeight
formulaBaseHeight base height defined by dialogui
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
QString getLineColor() const
quint32 GetP2Line() const
GetP2Line return id second point of line.
void SetP1Line(const quint32 &value)
SetP1Line set id first point of line.
quint32 GetP1Line() const
GetP1Line return id first point of line.
void setLineType(const QString &value)
setLineType set type of line
virtual void ChosenObject(quint32 id, const SceneObject &type) Q_DECL_OVERRIDE
ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
void SetP2Line(const quint32 &value)
SetP2Line set id second point of line.
QString getLineWeight() const
getLineWeight return weight of the lines
void SetP3(const quint32 &value)
setPShoulder set id shoulder point
void setLineWeight(const QString &value)
setLineWeight set weight of the lines
QString getLineType() const
getLineType return type of line
void SetPointName(const QString &value)
SetPointName set name of point.
void DeployFormulaTextEdit()
DeployFormulaTextEdit grow or shrink formula input.
void setLineColor(const QString &value)
Ui::DialogShoulderPoint * ui
ui keeps information about user interface
QString formula
formula formula
virtual void ShowVisualization() Q_DECL_OVERRIDE
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
DialogShoulderPoint(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
DialogShoulderPoint create dialog.
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)
void setLineP1Id(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLineP2Id(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