Seamly2D
Code documentation
vformulapropertyeditor.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file vformulapropertyeditor.cpp
3  ** @author DS Caskey
4  ** @date Feb 18, 2023
5  **
6  ** @brief
7  ** @copyright
8  ** This source code is part of the Seamly2D project, a pattern making
9  ** program, whose allow create and modeling patterns of clothing.
10  ** Copyright (C) 2017-2023 Seamly2D project
11  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
12  **
13  ** Seamly2D is free software: you can redistribute it and/or modify
14  ** it under the terms of the GNU General Public License as published by
15  ** the Free Software Foundation, either version 3 of the License, or
16  ** (at your option) any later version.
17  **
18  ** Seamly2D is distributed in the hope that it will be useful,
19  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  ** GNU General Public License for more details.
22  **
23  ** You should have received a copy of the GNU General Public License
24  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
25  **
26  *************************************************************************/
27 
28 /************************************************************************
29  **
30  ** @file vformulapropertyeditor.cpp
31  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
32  ** @date 28 8, 2014
33  **
34  ** @brief
35  ** @copyright
36  ** This source code is part of the Valentina project, a pattern making
37  ** program, whose allow create and modeling patterns of clothing.
38  ** Copyright (C) 2014 Valentina project
39  ** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
40  **
41  ** Valentina is free software: you can redistribute it and/or modify
42  ** it under the terms of the GNU General Public License as published by
43  ** the Free Software Foundation, either version 3 of the License, or
44  ** (at your option) any later version.
45  **
46  ** Valentina is distributed in the hope that it will be useful,
47  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
48  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49  ** GNU General Public License for more details.
50  **
51  ** You should have received a copy of the GNU General Public License
52  ** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
53  **
54  *************************************************************************/
55 
56 #include "vformulapropertyeditor.h"
57 
58 #include <QHBoxLayout>
59 #include <QFileDialog>
60 #include <QKeyEvent>
61 #include <QApplication>
62 #include <QColorDialog>
63 #include <QDebug>
64 #include <QRegularExpression>
65 
66 #include "../vpropertyexplorer/vproperty.h"
67 #include "../vtools/dialogs/support/edit_formula_dialog.h"
68 
69 // VFormulaPropertyEditor
70 //---------------------------------------------------------------------------------------------------------------------
72  : QWidget(parent)
73  , formula(VFormula())
74  , toolButton(nullptr)
75  , textLabel(nullptr)
76  , spacer(nullptr)
77 {
78  setAutoFillBackground(true);
79 
80  // Create the text label
81  textLabel = new QLabel(this);
82  textLabel->setText(formula.getStringValue());
83  textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
84 
85  // spacer (this is needed for proper display of the label and button)
86  spacer = new QSpacerItem(1, 0, QSizePolicy::Expanding, QSizePolicy::Fixed);
87 
88  // Create the tool button and make it the focus proxy
89  toolButton = new QToolButton(this);
90  toolButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
91  toolButton->setText("...");
92  toolButton->setIcon(QIcon("://icon/16x16/fx.png"));
93  toolButton->setFixedWidth(18);
94  toolButton->installEventFilter(this);
95  setFocusProxy(toolButton);
96  setFocusPolicy(toolButton->focusPolicy());
97  connect(toolButton, &QToolButton::clicked, this, &VFormulaPropertyEditor::onToolButtonClicked);
98 
99  // Add label, spacer & button to a horizontal layout)
100  QHBoxLayout *layout = new QHBoxLayout(this);
101  layout->setSpacing(3);
102  layout->setMargin(0);
103  layout->addWidget(textLabel);
104  layout->addItem(spacer);
105  layout->addWidget(toolButton);
106 }
107 
108 //---------------------------------------------------------------------------------------------------------------------
110 {
111  if (this->formula != formula)
112  {
113  this->formula = formula;
114  textLabel->setText(this->formula.getStringValue());
115  }
116 }
117 
118 //---------------------------------------------------------------------------------------------------------------------
120 {
122  qApp->getMainWindow());
123  dialog->setCheckZero(formula.getCheckZero());
124  dialog->setPostfix(formula.getPostfix());
126 
127  if (dialog->exec() == QDialog::Accepted)
128  {
130  textLabel->setText(formula.getStringValue());
131  delete dialog;
132  emit dataChangedByUser(formula, this);
134  QCoreApplication::postEvent ( this, event );
135  }
136 }
137 
138 //---------------------------------------------------------------------------------------------------------------------
139 bool VFormulaPropertyEditor::eventFilter(QObject *obj, QEvent *event)
140 {
141  if (obj == toolButton && event->type() == QEvent::KeyPress)
142  {
143  // Ignore the event, so that eventually the delegate gets the event.
144  event->ignore();
145  return true;
146  }
147 
148  return QWidget::eventFilter(obj, event);
149 }
150 
151 //---------------------------------------------------------------------------------------------------------------------
153 {
154  return formula;
155 }
The EditFormulaDialog class dialog for editing wrong formula.
QString GetFormula() const
void SetFormula(const QString &value)
void setCheckZero(bool value)
void setPostfix(const QString &value)
void SetFormula(const VFormula &formula)
Sets the color of the widget.
void dataChangedByUser(const VFormula &GetFormula, VFormulaPropertyEditor *editor)
This is emitted, when the user changes the color.
virtual bool eventFilter(QObject *obj, QEvent *ev) Q_DECL_OVERRIDE
Needed for proper event handling.
VFormulaPropertyEditor(QWidget *parent)
Constructor taking a widget as parent.
VFormula GetFormula() const
Returns the formula currently set.
QString getPostfix() const
Definition: vformula.cpp:222
const VContainer * getData() const
Definition: vformula.cpp:194
bool getCheckZero() const
Definition: vformula.cpp:178
QString GetFormula(FormulaType type=FormulaType::ToUser) const
Definition: vformula.cpp:135
QString getStringValue() const
Definition: vformula.cpp:166
quint32 getToolId() const
Definition: vformula.cpp:210
void SetFormula(const QString &value, FormulaType type=FormulaType::ToUser)
Definition: vformula.cpp:148
#define qApp
Definition: vapplication.h:67