Seamly2D
Code documentation
dialogalongline.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 dialogalongline.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 "dialogalongline.h"
53 
54 #include <QColor>
55 #include <QComboBox>
56 #include <QDialog>
57 #include <QLabel>
58 #include <QLatin1String>
59 #include <QLineEdit>
60 #include <QPlainTextEdit>
61 #include <QPointer>
62 #include <QPushButton>
63 #include <QSharedPointer>
64 #include <QToolButton>
65 #include <new>
66 
67 #include "../../visualization/line/vistoolalongline.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 "../vgeometry/../ifc/ifcdef.h"
73 #include "../vgeometry/vpointf.h"
74 #include "../vmisc/vabstractapplication.h"
75 #include "../vmisc/vcommonsettings.h"
76 #include "../vpatterndb/vcontainer.h"
77 #include "../vpatterndb/variables/vlinelength.h"
78 #include "../vpatterndb/vtranslatevars.h"
79 #include "ui_dialogalongline.h"
80 
81 //---------------------------------------------------------------------------------------------------------------------
82 /**
83  * @brief DialogAlongLine create dialog
84  * @param data container with data
85  * @param parent parent widget
86  */
87 DialogAlongLine::DialogAlongLine(const VContainer *data, const quint32 &toolId, QWidget *parent)
88  : DialogTool(data, toolId, parent), ui(new Ui::DialogAlongLine),
89  formula(QString()), formulaBaseHeight(0), buildMidpoint(false)
90 {
91  ui->setupUi(this);
92  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
93  setWindowIcon(QIcon(":/toolicon/32x32/along_line.png"));
94 
95  ui->lineEditNamePoint->setClearButtonEnabled(true);
96 
98  ui->lineEditNamePoint->setText(qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
99  labelEditNamePoint = ui->labelEditNamePoint;
100 
101  this->formulaBaseHeight = ui->plainTextEditFormula->height();
102  ui->plainTextEditFormula->installEventFilter(this);
103 
105  flagFormula = false;
107 
108  FillComboBoxPoints(ui->comboBoxFirstPoint);
109  FillComboBoxPoints(ui->comboBoxSecondPoint);
110 
111  int index = ui->lineColor_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineColor());
112  if (index != -1)
113  {
114  ui->lineColor_ComboBox->setCurrentIndex(index);
115  }
116 
117  index = ui->lineWeight_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineWeight());
118  if (index != -1)
119  {
120  ui->lineWeight_ComboBox->setCurrentIndex(index);
121  }
122 
123  index = ui->lineType_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineType());
124  if (index != -1)
125  {
126  ui->lineType_ComboBox->setCurrentIndex(index);
127  }
128 
129  connect(ui->toolButtonExprLength, &QPushButton::clicked, this, &DialogAlongLine::FXLength);
130  connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, &DialogAlongLine::NamePointChanged);
131  connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogAlongLine::FormulaTextChanged);
132  connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogAlongLine::DeployFormulaTextEdit);
133  connect(ui->comboBoxFirstPoint, &QComboBox::currentTextChanged, this, &DialogAlongLine::PointChanged);
134  connect(ui->comboBoxSecondPoint, &QComboBox::currentTextChanged, this, &DialogAlongLine::PointChanged);
135 
136  vis = new VisToolAlongLine(data);
137 
138  // Call after initialization vis!!!!
139  setLineType(LineTypeNone);//By default don't show line
140  setLineWeight("0.35");
141 }
142 
143 //---------------------------------------------------------------------------------------------------------------------
145 {
146  this->FormulaChangedPlainText();
147 }
148 
149 //---------------------------------------------------------------------------------------------------------------------
151 {
152  QColor color = okColor;
154  {
155  flagError = false;
156  color = errorColor;
157  }
158  else
159  {
160  flagError = true;
161  color = okColor;
162  }
164  ChangeColor(ui->labelFirstPoint, color);
165  ChangeColor(ui->labelSecondPoint, color);
166  CheckState();
167 }
168 
169 //---------------------------------------------------------------------------------------------------------------------
171 {
172  EditFormulaDialog *dialog = new EditFormulaDialog(data, toolId, this);
173  dialog->setWindowTitle(tr("Edit length"));
174  dialog->SetFormula(GetFormula());
175  dialog->setPostfix(UnitsToStr(qApp->patternUnit(), true));
176  if (dialog->exec() == QDialog::Accepted)
177  {
178  SetFormula(dialog->GetFormula());
179  }
180  delete dialog;
181 }
182 
183 //---------------------------------------------------------------------------------------------------------------------
185 {
186  AddVisualization<VisToolAlongLine>();
187 }
188 
189 //---------------------------------------------------------------------------------------------------------------------
191 {
192  DeployFormula(ui->plainTextEditFormula, ui->pushButtonGrowLength, formulaBaseHeight);
193 }
194 
195 //---------------------------------------------------------------------------------------------------------------------
197 {
198  delete ui;
199 }
200 
201 //---------------------------------------------------------------------------------------------------------------------
202 /**
203  * @brief ChoosedObject gets id and type of selected object. Save right data and ignore wrong.
204  * @param id id of point or detail
205  * @param type type of object
206  */
207 void DialogAlongLine::ChosenObject(quint32 id, const SceneObject &type)
208 {
209  if (prepare == false)// After first choose we ignore all objects
210  {
211  if (type == SceneObject::Point)
212  {
213  VisToolAlongLine *line = qobject_cast<VisToolAlongLine *>(vis);
214  SCASSERT(line != nullptr)
215 
216  const QString toolTip = tr("Select second point of line");
217  switch (number)
218  {
219  case 0:
220  if (SetObject(id, ui->comboBoxFirstPoint, toolTip))
221  {
222  number++;
223  line->VisualMode(id);
224  }
225  break;
226  case 1:
227  if (SetObject(id, ui->comboBoxSecondPoint, ""))
228  {
229  if (flagError)
230  {
231  line->setObject2Id(id);
232  line->RefreshGeometry();
233  if (buildMidpoint)
234  {
235  SetFormula(currentLength + QLatin1String("/2"));
236  }
237  prepare = true;
238  this->setModal(true);
239  this->show();
240  }
241  else
242  {
243  emit ToolTip(toolTip);
244  }
245  }
246  break;
247  default:
248  break;
249  }
250  }
251  }
252 }
253 
254 //---------------------------------------------------------------------------------------------------------------------
256 {
257  pointName = ui->lineEditNamePoint->text();
258 
259  formula = ui->plainTextEditFormula->toPlainText();
260  formula.replace("\n", " ");
261 
262  VisToolAlongLine *line = qobject_cast<VisToolAlongLine *>(vis);
263  SCASSERT(line != nullptr)
264 
265  line->setObject1Id(GetFirstPointId());
267  line->setLength(formula);
269  line->setLineWeight(getLineWeight());
270  line->RefreshGeometry();
271 }
272 
273 //---------------------------------------------------------------------------------------------------------------------
274 void DialogAlongLine::closeEvent(QCloseEvent *event)
275 {
276  ui->plainTextEditFormula->blockSignals(true);
277  DialogTool::closeEvent(event);
278 }
279 
280 //---------------------------------------------------------------------------------------------------------------------
282 {
285 
286  VLengthLine *length = new VLengthLine(p1.data(), GetFirstPointId(), p2.data(),
288  length->SetName(currentLength);
289 
290  VContainer *locData = const_cast<VContainer *> (data);
291  locData->AddVariable(currentLength, length);
292 }
293 
294 //---------------------------------------------------------------------------------------------------------------------
295 /**
296  * @brief SetSecondPointId set id second point of line
297  * @param value id
298  */
299 void DialogAlongLine::SetSecondPointId(const quint32 &value)
300 {
301  setCurrentPointId(ui->comboBoxSecondPoint, value);
302 
303  VisToolAlongLine *line = qobject_cast<VisToolAlongLine *>(vis);
304  SCASSERT(line != nullptr)
305  line->setObject2Id(value);
306 }
307 
308 //---------------------------------------------------------------------------------------------------------------------
309 void DialogAlongLine::Build(const Tool &type)
310 {
311  if (type == Tool::Midpoint)
312  {
313  buildMidpoint = true;
314  }
315 }
316 
317 //---------------------------------------------------------------------------------------------------------------------
318 /**
319  * @brief SetFirstPointId set id first point of line
320  * @param value id
321  */
322 void DialogAlongLine::SetFirstPointId(const quint32 &value)
323 {
324  setCurrentPointId(ui->comboBoxFirstPoint, value);
325 
326  VisToolAlongLine *line = qobject_cast<VisToolAlongLine *>(vis);
327  SCASSERT(line != nullptr)
328  line->setObject1Id(value);
329 }
330 
331 //---------------------------------------------------------------------------------------------------------------------
332 /**
333  * @brief SetFormula set string of formula
334  * @param value formula
335  */
336 void DialogAlongLine::SetFormula(const QString &value)
337 {
338  formula = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
339  // increase height if needed.
340  if (formula.length() > 80)
341  {
342  this->DeployFormulaTextEdit();
343  }
344  ui->plainTextEditFormula->setPlainText(formula);
345 
346  VisToolAlongLine *line = qobject_cast<VisToolAlongLine *>(vis);
347  SCASSERT(line != nullptr)
348  line->setLength(formula);
349 
350  MoveCursorToEnd(ui->plainTextEditFormula);
351 }
352 
353 //---------------------------------------------------------------------------------------------------------------------
354 /**
355  * @brief getLineType return type of line
356  * @return type
357  */
359 {
360  return GetComboBoxCurrentData(ui->lineType_ComboBox, LineTypeSolidLine);
361 }
362 
363 //---------------------------------------------------------------------------------------------------------------------
364 /**
365  * @brief setLineType set type of line
366  * @param value type
367  */
368 void DialogAlongLine::setLineType(const QString &value)
369 {
370  ChangeCurrentData(ui->lineType_ComboBox, value);
371  vis->setLineStyle(lineTypeToPenStyle(value));
372 }
373 
374 //---------------------------------------------------------------------------------------------------------------------
376 {
377  return GetComboBoxCurrentData(ui->lineColor_ComboBox, ColorBlack);
378 }
379 
380 //---------------------------------------------------------------------------------------------------------------------
381 void DialogAlongLine::setLineColor(const QString &value)
382 {
383  ChangeCurrentData(ui->lineColor_ComboBox, 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 DialogAlongLine::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 DialogAlongLine::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 GetFirstPointId return id first point of line
431  * @return id
432  */
434 {
435  return getCurrentObjectId(ui->comboBoxFirstPoint);
436 }
437 
438 //---------------------------------------------------------------------------------------------------------------------
439 /**
440  * @brief GetSecondPointId return id second point of line
441  * @return id
442  */
444 {
445  return getCurrentObjectId(ui->comboBoxSecondPoint);
446 }
The DialogAlongLine class dialog for ToolAlongLine. Help create point and edit option.
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.
virtual ~DialogAlongLine() Q_DECL_OVERRIDE
QString GetFormula() const
GetFormula return string of formula.
virtual void ShowVisualization() Q_DECL_OVERRIDE
quint32 GetSecondPointId() const
GetSecondPointId return id second point of line.
virtual void Build(const Tool &type) Q_DECL_OVERRIDE
quint32 GetFirstPointId() const
GetFirstPointId return id first point of line.
Ui::DialogAlongLine * ui
ui keeps information about user interface
void SetFormula(const QString &value)
SetFormula set string of formula.
QString formula
formula formula
QString getLineColor() const
DialogAlongLine(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
DialogAlongLine create dialog.
QString getLineWeight() const
getLineWeight return weight of the lines
void SetFirstPointId(const quint32 &value)
SetFirstPointId set id first point of line.
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
void DeployFormulaTextEdit()
DeployFormulaTextEdit grow or shrink formula input.
void setLineType(const QString &value)
setLineType set type of line
void FormulaTextChanged()
FormulaTextChanged when formula text changes for validation and calc.
int formulaBaseHeight
formulaBaseHeight base height defined by dialogui
void setLineColor(const QString &value)
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
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 SetSecondPointId(const quint32 &value)
SetSecondPointId set id second point 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
const QColor okColor
Definition: dialogtool.h:219
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
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 AddVariable(const QString &name, T *var)
Definition: vcontainer.h:327
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
const Unit * GetPatternUnit() const
Definition: vcontainer.cpp:599
The VPointF class keep data of point.
Definition: vpointf.h:75
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLength(const QString &expression)
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
Tool
Definition: def.h:161
@ Midpoint
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
const QString ColorBlack
Definition: ifcdef.cpp:373
const QString LineTypeNone
Definition: ifcdef.cpp:158
const QString currentLength
Definition: ifcdef.cpp:441
Qt::PenStyle lineTypeToPenStyle(const QString &lineType)
LineStyle return pen style for current line style.
Definition: ifcdef.cpp:183
#define qApp
Definition: vapplication.h:67