Seamly2D
Code documentation
dialogcurveintersectaxis.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 dialogcurveintersectaxis.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 21 10, 2014
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 
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 "../../visualization/line/vistoolcurveintersectaxis.h"
72 #include "../../visualization/visualization.h"
73 #include "../ifc/xml/vabstractpattern.h"
74 #include "../support/edit_formula_dialog.h"
75 #include "../vmisc/vabstractapplication.h"
76 #include "../vmisc/vcommonsettings.h"
77 #include "ui_dialogcurveintersectaxis.h"
78 
79 //---------------------------------------------------------------------------------------------------------------------
80 DialogCurveIntersectAxis::DialogCurveIntersectAxis(const VContainer *data, const quint32 &toolId, QWidget *parent)
81  : DialogTool(data, toolId, parent)
82  , ui(new Ui::DialogCurveIntersectAxis)
83  , formulaAngle()
84  , formulaBaseHeightAngle(0)
85  , m_firstRelease(false)
86 {
87  ui->setupUi(this);
88  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
89  setWindowIcon(QIcon(":/toolicon/32x32/curve_intersect_axis.png"));
90 
91  ui->pointName_LineEdit->setClearButtonEnabled(true);
92 
94  ui->pointName_LineEdit->setText(qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
95  labelEditNamePoint = ui->pointName_Label;
97  plainTextEditFormula->installEventFilter(this);
98 
100  flagFormula = false;
102 
103  FillComboBoxPoints(ui->axisPoint_ComboBox);
104  FillComboBoxCurves(ui->curve_ComboBox);
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->exprAngle_ToolButton, &QPushButton::clicked, this, &DialogCurveIntersectAxis::FXAngle);
125  connect(ui->pointName_LineEdit, &QLineEdit::textChanged, this, &DialogCurveIntersectAxis::NamePointChanged);
126  connect(plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogCurveIntersectAxis::AngleTextChanged);
127  connect(ui->growLengthAngle_PushButton, &QPushButton::clicked, this, &DialogCurveIntersectAxis::DeployAngleTextEdit);
128  connect(timerFormula, &QTimer::timeout, this, &DialogCurveIntersectAxis::EvalAngle);
129 
131 }
132 
133 //---------------------------------------------------------------------------------------------------------------------
135 {
136  delete ui;
137 }
138 
139 //---------------------------------------------------------------------------------------------------------------------
140 void DialogCurveIntersectAxis::SetPointName(const QString &value)
141 {
142  pointName = value;
143  ui->pointName_LineEdit->setText(pointName);
144 }
145 
146 //---------------------------------------------------------------------------------------------------------------------
148 {
149  return GetComboBoxCurrentData(ui->lineType_ComboBox, LineTypeSolidLine);
150 }
151 
152 //---------------------------------------------------------------------------------------------------------------------
153 void DialogCurveIntersectAxis::setLineType(const QString &value)
154 {
155  ChangeCurrentData(ui->lineType_ComboBox, value);
156  vis->setLineStyle(lineTypeToPenStyle(value));
157 }
158 
159 //---------------------------------------------------------------------------------------------------------------------
160 /**
161  * @brief getLineWeight return weight of the lines
162  * @return type
163  */
165 {
166  return GetComboBoxCurrentData(ui->lineWeight_ComboBox, "0.35");
167 }
168 
169 //---------------------------------------------------------------------------------------------------------------------
170 /**
171  * @brief setLineWeight set weight of the lines
172  * @param value type
173  */
174 void DialogCurveIntersectAxis::setLineWeight(const QString &value)
175 {
176  ChangeCurrentData(ui->lineWeight_ComboBox, value);
177  vis->setLineWeight(value);
178 }
179 
180 //---------------------------------------------------------------------------------------------------------------------
182 {
183  return qApp->TrVars()->TryFormulaFromUser(formulaAngle, qApp->Settings()->GetOsSeparator());
184 }
185 
186 //---------------------------------------------------------------------------------------------------------------------
187 void DialogCurveIntersectAxis::SetAngle(const QString &value)
188 {
189  formulaAngle = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
190  // increase height if needed. TODO : see if I can get the max number of caracters in one line
191  // of this PlainTextEdit to change 80 to this value
192  if (formulaAngle.length() > 80)
193  {
194  this->DeployAngleTextEdit();
195  }
196  plainTextEditFormula->setPlainText(formulaAngle);
197 
198  VisToolCurveIntersectAxis *line = qobject_cast<VisToolCurveIntersectAxis *>(vis);
199  SCASSERT(line != nullptr)
200  line->SetAngle(formulaAngle);
201 
203 }
204 
205 //---------------------------------------------------------------------------------------------------------------------
207 {
208  return getCurrentObjectId(ui->axisPoint_ComboBox);
209 }
210 
211 //---------------------------------------------------------------------------------------------------------------------
213 {
214  setCurrentPointId(ui->axisPoint_ComboBox, value);
215 
216  VisToolCurveIntersectAxis *line = qobject_cast<VisToolCurveIntersectAxis *>(vis);
217  SCASSERT(line != nullptr)
218  line->setAxisPointId(value);
219 }
220 
221 //---------------------------------------------------------------------------------------------------------------------
223 {
224  return getCurrentObjectId(ui->curve_ComboBox);
225 }
226 
227 //---------------------------------------------------------------------------------------------------------------------
228 void DialogCurveIntersectAxis::setCurveId(const quint32 &value)
229 {
230  setCurrentCurveId(ui->curve_ComboBox, value);
231 
232  VisToolCurveIntersectAxis *line = qobject_cast<VisToolCurveIntersectAxis *>(vis);
233  SCASSERT(line != nullptr)
234  line->setObject1Id(value);
235 }
236 
237 //---------------------------------------------------------------------------------------------------------------------
238 /**
239  * @brief getLineColor get the color of line
240  * @param value type
241  */
243 {
244  return GetComboBoxCurrentData(ui->lineColor_ComboBox, ColorBlack);
245 }
246 
247 //---------------------------------------------------------------------------------------------------------------------
248 /* @brief setLineColor set color of the line
249  * @param value type
250  */
251 void DialogCurveIntersectAxis::setLineColor(const QString &value)
252 {
253  ChangeCurrentData(ui->lineColor_ComboBox, value);
254 }
255 
256 //---------------------------------------------------------------------------------------------------------------------
258 {
259  if (prepare)
260  {
261  if (click)
262  {
263  // The check need to ignore first release of mouse button.
264  // User can select point by clicking on a label.
265  if (not m_firstRelease)
266  {
267  m_firstRelease = true;
268  return;
269  }
270 
271  /*We will ignore click if poinet is in point circle*/
272  VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(qApp->getCurrentScene());
273  SCASSERT(scene != nullptr)
275  QLineF line = QLineF(static_cast<QPointF>(*point), scene->getScenePos());
276 
277  //Radius of point circle, but little bigger. Need handle with hover sizes.
278  if (line.length() <= defPointRadiusPixel*1.5)
279  {
280  return;
281  }
282  }
283 
284  VisToolCurveIntersectAxis *line = qobject_cast<VisToolCurveIntersectAxis *>(vis);
285  SCASSERT(line != nullptr)
286 
287  this->SetAngle(line->Angle());//Show in dialog angle what user choose
288  emit ToolTip("");
289 
290  DialogAccepted();// Just set default values and don't show dialog
291  }
292 }
293 
294 //---------------------------------------------------------------------------------------------------------------------
296 {
297  if (prepare == false)// After first choose we ignore all objects
298  {
299  VisToolCurveIntersectAxis *line = qobject_cast<VisToolCurveIntersectAxis *>(vis);
300  SCASSERT(line != nullptr)
301 
302  switch (number)
303  {
304  case (0):
305  if (type == SceneObject::Spline
306  || type == SceneObject::Arc
307  || type == SceneObject::ElArc
308  || type == SceneObject::SplinePath)
309  {
310  if (SetObject(id, ui->curve_ComboBox, tr("Select axis point")))
311  {
312  number++;
313  line->VisualMode(id);
314  VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow());
315  SCASSERT(window != nullptr)
317 
318  }
319  }
320  break;
321  case (1):
322  if (type == SceneObject::Point)
323  {
324  if (SetObject(id, ui->axisPoint_ComboBox, ""))
325  {
326  line->setAxisPointId(id);
327  line->RefreshGeometry();
328  prepare = true;
329  }
330  }
331  break;
332  default:
333  break;
334  }
335  }
336 }
337 
338 //---------------------------------------------------------------------------------------------------------------------
340 {
341  Eval(ui->plainTextEditFormula->toPlainText(), flagError, ui->labelResultCalculation, degreeSymbol, false);
342 }
343 
344 //---------------------------------------------------------------------------------------------------------------------
346 {
348 }
349 
350 //---------------------------------------------------------------------------------------------------------------------
352 {
353  DeployFormula(plainTextEditFormula, ui->growLengthAngle_PushButton, formulaBaseHeightAngle);
354 }
355 
356 //---------------------------------------------------------------------------------------------------------------------
358 {
359  EditFormulaDialog *dialog = new EditFormulaDialog(data, toolId, this);
360  dialog->setWindowTitle(tr("Edit angle"));
361  dialog->SetFormula(GetAngle());
362  dialog->setPostfix(degreeSymbol);
363  if (dialog->exec() == QDialog::Accepted)
364  {
365  SetAngle(dialog->GetFormula());
366  }
367  delete dialog;
368 }
369 
370 //---------------------------------------------------------------------------------------------------------------------
372 {
373  AddVisualization<VisToolCurveIntersectAxis>();
374 }
375 
376 //---------------------------------------------------------------------------------------------------------------------
378 {
379  pointName = ui->pointName_LineEdit->text();
380 
381  formulaAngle = plainTextEditFormula->toPlainText();
382  formulaAngle.replace("\n", " ");
383 
384  VisToolCurveIntersectAxis *line = qobject_cast<VisToolCurveIntersectAxis *>(vis);
385  SCASSERT(line != nullptr)
386 
387  line->setObject1Id(getCurveId());
389  line->SetAngle(formulaAngle);
391  line->setLineWeight(getLineWeight());
392  line->RefreshGeometry();
393 }
394 
395 //---------------------------------------------------------------------------------------------------------------------
396 void DialogCurveIntersectAxis::closeEvent(QCloseEvent *event)
397 {
398  plainTextEditFormula->blockSignals(true);
399  DialogTool::closeEvent(event);
400 }
void SetAngle(const QString &value)
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
virtual void ShowDialog(bool click) Q_DECL_OVERRIDE
Ui::DialogCurveIntersectAxis * ui
void setLineColor(const QString &value)
void SetPointName(const QString &value)
QString getLineColor() const
getLineColor get the color of line
virtual void ShowVisualization() Q_DECL_OVERRIDE
virtual ~DialogCurveIntersectAxis() Q_DECL_OVERRIDE
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
DialogCurveIntersectAxis(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
void SetBasePointId(const quint32 &value)
virtual void ChosenObject(quint32 id, const SceneObject &type) Q_DECL_OVERRIDE
void setLineWeight(const QString &value)
setLineWeight set weight of the lines
void setLineType(const QString &value)
QString getLineWeight() const
getLineWeight return weight of the lines
void setCurveId(const quint32 &value)
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
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
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
virtual void DialogAccepted()
DialogAccepted save data and emit signal about closed dialog.
QLabel * labelEditNamePoint
labelEditNamePoint label used when need show wrong name of point
Definition: dialogtool.h:214
quint32 toolId
Definition: dialogtool.h:225
void FillComboBoxCurves(QComboBox *box) const
Definition: dialogtool.cpp:302
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
QPlainTextEdit * plainTextEditFormula
plainTextEditFormula formula
Definition: dialogtool.h:208
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
void setCurrentCurveId(QComboBox *box, const quint32 &value) const
Definition: dialogtool.cpp:946
QPointer< Visualization > vis
Definition: dialogtool.h:236
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
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void SetAngle(const QString &expression)
void setAxisPointId(const quint32 &value)
void ToolTip(const QString &toolTip)
void setLineWeight(const QString &value)
virtual void VisualMode(const quint32 &pointId)
void setObject1Id(const quint32 &value)
void setLineStyle(const Qt::PenStyle &value)
const QString degreeSymbol
Definition: def.cpp:196
#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