Seamly2D
Code documentation
dialoglineintersectaxis.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 dialoglineintersectaxis.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 19 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 <QColor>
55 #include <QComboBox>
56 #include <QDialog>
57 #include <QLabel>
58 #include <QLineEdit>
59 #include <QLineF>
60 #include <QPlainTextEdit>
61 #include <QPointF>
62 #include <QPointer>
63 #include <QPushButton>
64 #include <QSet>
65 #include <QSharedPointer>
66 #include <QTimer>
67 #include <QToolButton>
68 #include <new>
69 
70 #include "../vgeometry/vpointf.h"
71 #include "../vpatterndb/vcontainer.h"
72 #include "../vpatterndb/vtranslatevars.h"
73 #include "../vwidgets/vmaingraphicsscene.h"
74 #include "../vwidgets/vabstractmainwindow.h"
75 #include "../../tools/vabstracttool.h"
76 #include "../../visualization/line/vistoollineintersectaxis.h"
77 #include "../../visualization/visualization.h"
78 #include "../ifc/xml/vabstractpattern.h"
79 #include "../support/edit_formula_dialog.h"
80 #include "../vmisc/vabstractapplication.h"
81 #include "../vmisc/vcommonsettings.h"
82 #include "ui_dialoglineintersectaxis.h"
83 
84 //---------------------------------------------------------------------------------------------------------------------
85 DialogLineIntersectAxis::DialogLineIntersectAxis(const VContainer *data, const quint32 &toolId, QWidget *parent)
86  : DialogTool(data, toolId, parent),
87  ui(new Ui::DialogLineIntersectAxis),
88  formulaAngle(),
89  formulaBaseHeightAngle(0),
90  m_firstRelease(false)
91 {
92  ui->setupUi(this);
93  setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
94  setWindowIcon(QIcon(":/toolicon/32x32/line_intersect_axis.png"));
95 
96  ui->lineEditNamePoint->setClearButtonEnabled(true);
97 
99  ui->lineEditNamePoint->setText(qApp->getCurrentDocument()->GenerateLabel(LabelType::NewLabel));
100  labelEditNamePoint = ui->labelEditNamePoint;
101  this->formulaBaseHeightAngle = ui->plainTextEditFormula->height();
102  ui->plainTextEditFormula->installEventFilter(this);
103 
105  flagFormula = false;
107 
108  FillComboBoxPoints(ui->comboBoxAxisPoint);
109  FillComboBoxPoints(ui->comboBoxFirstLinePoint);
110  FillComboBoxPoints(ui->comboBoxSecondLinePoint);
111 
112  int index = ui->lineColor_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineColor());
113  if (index != -1)
114  {
115  ui->lineColor_ComboBox->setCurrentIndex(index);
116  }
117 
118  index = ui->lineWeight_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineWeight());
119  if (index != -1)
120  {
121  ui->lineWeight_ComboBox->setCurrentIndex(index);
122  }
123 
124  index = ui->lineType_ComboBox->findData(qApp->getCurrentDocument()->getDefaultLineType());
125  if (index != -1)
126  {
127  ui->lineType_ComboBox->setCurrentIndex(index);
128  }
129 
130  connect(ui->toolButtonExprAngle, &QPushButton::clicked, this, &DialogLineIntersectAxis::FXAngle);
131  connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, &DialogLineIntersectAxis::NamePointChanged);
132  connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogLineIntersectAxis::AngleTextChanged);
133  connect(ui->pushButtonGrowLengthAngle, &QPushButton::clicked, this, &DialogLineIntersectAxis::DeployAngleTextEdit);
134  connect(timerFormula, &QTimer::timeout, this, &DialogLineIntersectAxis::EvalAngle);
135  connect(ui->comboBoxFirstLinePoint, &QComboBox::currentTextChanged,
137  connect(ui->comboBoxSecondLinePoint, &QComboBox::currentTextChanged,
139  connect(ui->comboBoxAxisPoint, &QComboBox::currentTextChanged,
141 
143 }
144 
145 //---------------------------------------------------------------------------------------------------------------------
147 {
148  delete ui;
149 }
150 
151 //---------------------------------------------------------------------------------------------------------------------
152 void DialogLineIntersectAxis::SetPointName(const QString &value)
153 {
154  pointName = value;
155  ui->lineEditNamePoint->setText(pointName);
156 }
157 
158 //---------------------------------------------------------------------------------------------------------------------
160 {
161  return GetComboBoxCurrentData(ui->lineType_ComboBox, LineTypeSolidLine);
162 }
163 
164 //---------------------------------------------------------------------------------------------------------------------
165 void DialogLineIntersectAxis::setLineType(const QString &value)
166 {
167  ChangeCurrentData(ui->lineType_ComboBox, value);
168  vis->setLineStyle(lineTypeToPenStyle(value));
169 }
170 
171 //---------------------------------------------------------------------------------------------------------------------
172 /**
173  * @brief getLineWeight return weight of the lines
174  * @return type
175  */
177 {
178  return GetComboBoxCurrentData(ui->lineWeight_ComboBox, "0.35");
179 }
180 
181 //---------------------------------------------------------------------------------------------------------------------
182 /**
183  * @brief setLineWeight set weight of the lines
184  * @param value type
185  */
186 void DialogLineIntersectAxis::setLineWeight(const QString &value)
187 {
188  ChangeCurrentData(ui->lineWeight_ComboBox, value);
189  vis->setLineWeight(value);
190 }
191 
192 //---------------------------------------------------------------------------------------------------------------------
194 {
195  return GetComboBoxCurrentData(ui->lineColor_ComboBox, ColorBlack);
196 }
197 
198 //---------------------------------------------------------------------------------------------------------------------
199 void DialogLineIntersectAxis::setLineColor(const QString &value)
200 {
201  ChangeCurrentData(ui->lineColor_ComboBox, value);
202 }
203 
204 //---------------------------------------------------------------------------------------------------------------------
206 {
207  return qApp->TrVars()->TryFormulaFromUser(formulaAngle, qApp->Settings()->GetOsSeparator());
208 }
209 
210 //---------------------------------------------------------------------------------------------------------------------
211 void DialogLineIntersectAxis::SetAngle(const QString &value)
212 {
213  formulaAngle = qApp->TrVars()->FormulaToUser(value, qApp->Settings()->GetOsSeparator());
214  // increase height if needed. TODO : see if I can get the max number of caracters in one line
215  // of this PlainTextEdit to change 80 to this value
216  if (formulaAngle.length() > 80)
217  {
218  this->DeployAngleTextEdit();
219  }
220  ui->plainTextEditFormula->setPlainText(formulaAngle);
221 
222  VisToolLineIntersectAxis *line = qobject_cast<VisToolLineIntersectAxis *>(vis);
223  SCASSERT(line != nullptr)
224  line->SetAngle(formulaAngle);
225 
226  MoveCursorToEnd(ui->plainTextEditFormula);
227 }
228 
229 //---------------------------------------------------------------------------------------------------------------------
231 {
232  return getCurrentObjectId(ui->comboBoxAxisPoint);
233 }
234 
235 //---------------------------------------------------------------------------------------------------------------------
236 void DialogLineIntersectAxis::SetBasePointId(const quint32 &value)
237 {
238  setCurrentPointId(ui->comboBoxAxisPoint, value);
239 
240  VisToolLineIntersectAxis *line = qobject_cast<VisToolLineIntersectAxis *>(vis);
241  SCASSERT(line != nullptr)
242  line->setAxisPointId(value);
243 }
244 
245 //---------------------------------------------------------------------------------------------------------------------
247 {
248  return getCurrentObjectId(ui->comboBoxFirstLinePoint);
249 }
250 
251 //---------------------------------------------------------------------------------------------------------------------
253 {
254  setCurrentPointId(ui->comboBoxFirstLinePoint, value);
255 
256  VisToolLineIntersectAxis *line = qobject_cast<VisToolLineIntersectAxis *>(vis);
257  SCASSERT(line != nullptr)
258  line->setObject1Id(value);
259 }
260 
261 //---------------------------------------------------------------------------------------------------------------------
263 {
264  return getCurrentObjectId(ui->comboBoxSecondLinePoint);
265 }
266 
267 //---------------------------------------------------------------------------------------------------------------------
269 {
270  setCurrentPointId(ui->comboBoxSecondLinePoint, value);
271 
272  VisToolLineIntersectAxis *line = qobject_cast<VisToolLineIntersectAxis *>(vis);
273  SCASSERT(line != nullptr)
274  line->setPoint2Id(value);
275 }
276 
277 //---------------------------------------------------------------------------------------------------------------------
279 {
280  if (prepare)
281  {
282  if (click)
283  {
284  // The check need to ignore first release of mouse button.
285  // User can select point by clicking on a label.
286  if (not m_firstRelease)
287  {
288  m_firstRelease = true;
289  return;
290  }
291 
292  /*We will ignore click if poinet is in point circle*/
293  VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(qApp->getCurrentScene());
294  SCASSERT(scene != nullptr)
296  QLineF line = QLineF(static_cast<QPointF>(*point), scene->getScenePos());
297 
298  //Radius of point circle, but little bigger. Need handle with hover sizes.
299  if (line.length() <= defPointRadiusPixel*1.5)
300  {
301  return;
302  }
303  }
304 
305  VisToolLineIntersectAxis *line = qobject_cast<VisToolLineIntersectAxis *>(vis);
306  SCASSERT(line != nullptr)
307 
308  this->SetAngle(line->Angle());//Show in dialog angle what user choose
309  emit ToolTip("");
310 
311  DialogAccepted();// Just set default values and don't show dialog
312  }
313 }
314 
315 //---------------------------------------------------------------------------------------------------------------------
317 {
318  if (prepare == false)// After first choose we ignore all objects
319  {
320  if (type == SceneObject::Point)
321  {
322  VisToolLineIntersectAxis *line = qobject_cast<VisToolLineIntersectAxis *>(vis);
323  SCASSERT(line != nullptr)
324 
325  switch (number)
326  {
327  case (0):
328  if (SetObject(id, ui->comboBoxFirstLinePoint, tr("Select second point of line")))
329  {
330  number++;
331  line->VisualMode(id);
332  VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow());
333  SCASSERT(window != nullptr)
335  }
336  break;
337  case (1):
338  if (getCurrentObjectId(ui->comboBoxFirstLinePoint) != id)
339  {
340  if (SetObject(id, ui->comboBoxSecondLinePoint, tr("Select axis point")))
341  {
342  number++;
343  line->setPoint2Id(id);
344  line->RefreshGeometry();
345  }
346  }
347  break;
348  case (2):
349  {
350  QSet<quint32> set;
351  set.insert(getCurrentObjectId(ui->comboBoxFirstLinePoint));
352  set.insert(getCurrentObjectId(ui->comboBoxSecondLinePoint));
353  set.insert(id);
354 
355  if (set.size() == 3)
356  {
357  if (SetObject(id, ui->comboBoxAxisPoint, ""))
358  {
359  line->setAxisPointId(id);
360  line->RefreshGeometry();
361  prepare = true;
362  }
363  }
364  }
365  break;
366  default:
367  break;
368  }
369  }
370  }
371 }
372 
373 //---------------------------------------------------------------------------------------------------------------------
375 {
376  Eval(ui->plainTextEditFormula->toPlainText(), flagError, ui->labelResultCalculation, degreeSymbol, false);
377 }
378 
379 //---------------------------------------------------------------------------------------------------------------------
381 {
382  ValFormulaChanged(flagError, ui->plainTextEditFormula, timerFormula, degreeSymbol);
383 }
384 
385 //---------------------------------------------------------------------------------------------------------------------
387 {
388  DeployFormula(ui->plainTextEditFormula, ui->pushButtonGrowLengthAngle, formulaBaseHeightAngle);
389 }
390 
391 //---------------------------------------------------------------------------------------------------------------------
393 {
394  QSet<quint32> set;
395  set.insert(getCurrentObjectId(ui->comboBoxFirstLinePoint));
396  set.insert(getCurrentObjectId(ui->comboBoxSecondLinePoint));
397  set.insert(getCurrentObjectId(ui->comboBoxAxisPoint));
398 
399  QColor color = okColor;
400  if (set.size() != 3)
401  {
402  flagError = false;
403  color = errorColor;
404  }
405  else
406  {
407  flagError = true;
408  color = okColor;
409  }
410  ChangeColor(ui->labelFirstLinePoint, color);
411  ChangeColor(ui->labelSecondLinePoint, color);
412  ChangeColor(ui->labelAxisPoint, color);
413  CheckState();
414 }
415 
416 //---------------------------------------------------------------------------------------------------------------------
418 {
419  EditFormulaDialog *dialog = new EditFormulaDialog(data, toolId, this);
420  dialog->setWindowTitle(tr("Edit angle"));
421  dialog->SetFormula(GetAngle());
422  dialog->setPostfix(degreeSymbol);
423  if (dialog->exec() == QDialog::Accepted)
424  {
425  SetAngle(dialog->GetFormula());
426  }
427  delete dialog;
428 }
429 
430 //---------------------------------------------------------------------------------------------------------------------
432 {
433  AddVisualization<VisToolLineIntersectAxis>();
434 }
435 
436 //---------------------------------------------------------------------------------------------------------------------
438 {
439  pointName = ui->lineEditNamePoint->text();
440 
441  formulaAngle = ui->plainTextEditFormula->toPlainText();
442  formulaAngle.replace("\n", " ");
443 
444  VisToolLineIntersectAxis *line = qobject_cast<VisToolLineIntersectAxis *>(vis);
445  SCASSERT(line != nullptr)
446 
447  line->setObject1Id(GetFirstPointId());
448  line->setPoint2Id(GetSecondPointId());
450  line->SetAngle(formulaAngle);
452  line->setLineWeight(getLineWeight());
453  line->RefreshGeometry();
454 }
455 
456 //---------------------------------------------------------------------------------------------------------------------
457 void DialogLineIntersectAxis::closeEvent(QCloseEvent *event)
458 {
459  ui->plainTextEditFormula->blockSignals(true);
460  DialogTool::closeEvent(event);
461 }
virtual void PointNameChanged() Q_DECL_OVERRIDE
virtual void SaveData() Q_DECL_OVERRIDE
SaveData Put dialog data in local variables.
QString getLineWeight() const
getLineWeight return weight of the lines
virtual void ShowDialog(bool click) Q_DECL_OVERRIDE
virtual void ChosenObject(quint32 id, const SceneObject &type) Q_DECL_OVERRIDE
void SetSecondPointId(const quint32 &value)
virtual void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE
closeEvent handle when dialog cloded
void SetAngle(const QString &value)
void setLineColor(const QString &value)
void SetFirstPointId(const quint32 &value)
void setLineWeight(const QString &value)
setLineWeight set weight of the lines
void SetBasePointId(const quint32 &value)
DialogLineIntersectAxis(const VContainer *data, const quint32 &toolId, QWidget *parent=nullptr)
void SetPointName(const QString &value)
virtual void ShowVisualization() Q_DECL_OVERRIDE
Ui::DialogLineIntersectAxis * ui
void setLineType(const QString &value)
virtual ~DialogLineIntersectAxis() Q_DECL_OVERRIDE
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
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
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
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 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 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)
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 setPoint2Id(const quint32 &value)
void setAxisPointId(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void SetAngle(const QString &expression)
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