Seamly2D
Code documentation
vformulaproperty.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 vformulaproperty.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 28 8, 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 
52 #include "vformulaproperty.h"
53 #include "vformulapropertyeditor.h"
54 
55 #include "../vpropertyexplorer/vproperty_p.h"
56 #include "vformulapropertyeditor.h"
57 #include "../vpropertyexplorer/vproperties.h"
58 #include "../vpatterndb/vformula.h"
59 #include "../vmisc/vabstractapplication.h"
60 
61 //---------------------------------------------------------------------------------------------------------------------
63  : VProperty(name, static_cast<QVariant::Type>(VFormula::FormulaTypeId()))
64 {
66 
67  VPE::PlainTextProperty *tmpFormula = new VPE::PlainTextProperty(tr("Formula:"));
68  addChild(tmpFormula);
69  tmpFormula->setUpdateBehaviour(true, false);
70  tmpFormula->setOsSeparator(qApp->Settings()->GetOsSeparator());
71  setValue(0);
72 }
73 
74 //---------------------------------------------------------------------------------------------------------------------
75 //! Get the data how it should be displayed
76 QVariant VFormulaProperty::data (int column, int role) const
77 {
78  if (column == DPC_Data && (Qt::DisplayRole == role || Qt::EditRole == role))
79  {
80  return getValue();
81  }
82  else
83  {
84  return VProperty::data(column, role);
85  }
86 }
87 
88 //---------------------------------------------------------------------------------------------------------------------
89 Qt::ItemFlags VFormulaProperty::flags(int column) const
90 {
91  if (column == DPC_Name || column == DPC_Data)
92  {
93  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
94  }
95  else
96  {
97  return Qt::NoItemFlags;
98  }
99 }
100 
101 //---------------------------------------------------------------------------------------------------------------------
102 //! Returns an editor widget, or NULL if it doesn't supply one
103 QWidget* VFormulaProperty::createEditor(QWidget* parent, const QStyleOptionViewItem& options,
104  const QAbstractItemDelegate* delegate)
105 {
106  Q_UNUSED(options)
107  Q_UNUSED(delegate)
108 
109  VFormula formula = VProperty::d_ptr->VariantValue.value<VFormula>();
110  VFormulaPropertyEditor* formulaEditor = new VFormulaPropertyEditor(parent);
111  formulaEditor->setLocale(parent->locale());
112  formulaEditor->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
113  formulaEditor->SetFormula(formula);
114  VProperty::d_ptr->editor = formulaEditor;
115  return VProperty::d_ptr->editor;
116 }
117 
118 //---------------------------------------------------------------------------------------------------------------------
119 //! Sets the property's data to the editor (returns false, if the standard delegate should do that)
120 bool VFormulaProperty::setEditorData(QWidget* editor)
121 {
122  VFormulaPropertyEditor* formulaEditor = qobject_cast<VFormulaPropertyEditor*>(editor);
123  if (formulaEditor)
124  {
125  VFormula formula = VProperty::d_ptr->VariantValue.value<VFormula>();
126  formulaEditor->SetFormula(formula);
127  }
128  else
129  return false;
130 
131  return true;
132 }
133 
134 //---------------------------------------------------------------------------------------------------------------------
135 //! Gets the data from the widget
136 QVariant VFormulaProperty::getEditorData(const QWidget *editor) const
137 {
138  const VFormulaPropertyEditor* formulaEditor = qobject_cast<const VFormulaPropertyEditor*>(editor);
139  if (formulaEditor)
140  {
141  QVariant value;
142  value.setValue(formulaEditor->GetFormula());
143  return value;
144  }
145 
146  return QVariant();
147 }
148 
149 //---------------------------------------------------------------------------------------------------------------------
150 QString VFormulaProperty::type() const
151 {
152  return "formula";
153 }
154 
155 //---------------------------------------------------------------------------------------------------------------------
156 VPE::VProperty *VFormulaProperty::clone(bool include_children, VProperty *container) const
157 {
158  if (!container)
159  {
160  container = new VFormulaProperty(getName());
161 
162  if (!include_children)
163  {
164  QList<VProperty*> tmpChildren = container->getChildren();
165  foreach (VProperty* tmpChild, tmpChildren)
166  {
167  container->removeChild(tmpChild);
168  delete tmpChild;
169  }
170  }
171  }
172 
173  return VProperty::clone(false, container); // Child
174 
175 }
176 
177 //---------------------------------------------------------------------------------------------------------------------
178 void VFormulaProperty::setValue(const QVariant &value)
179 {
180  VFormula tmpFormula = value.value<VFormula>();
181  SetFormula(tmpFormula);
182 }
183 
184 //---------------------------------------------------------------------------------------------------------------------
186 {
187  VFormula tmpFormula = GetFormula();
188  QVariant value;
189  value.setValue(tmpFormula);
190  return value;
191 }
192 
193 //---------------------------------------------------------------------------------------------------------------------
195 {
196  return VProperty::d_ptr->VariantValue.value<VFormula>();
197 }
198 
199 //---------------------------------------------------------------------------------------------------------------------
201 {
202  if (d_ptr->Children.count() < 1)
203  {
204  return;
205  }
206 
207  QVariant value;
208  value.setValue(formula);
209  value.convert(VFormula::FormulaTypeId());
210  VProperty::d_ptr->VariantValue = value;
211 
212  QVariant tmpFormula(formula.GetFormula());
213  tmpFormula.convert(QVariant::String);
214 
215  VProperty::d_ptr->Children.at(0)->setValue(tmpFormula);
216 
217  if (VProperty::d_ptr->editor != nullptr)
218  {
219  setEditorData(VProperty::d_ptr->editor);
220  }
221 }
222 
223 void VFormulaProperty::childValueChanged(const QVariant &value, int typeForParent)
224 {
225  Q_UNUSED(typeForParent)
226  VFormula newFormula = GetFormula();
227  newFormula.SetFormula(value.toString(), FormulaType::FromUser);
228  SetFormula(newFormula);
229 }
void SetFormula(const VFormula &formula)
Sets the color of the widget.
VFormula GetFormula() const
Returns the formula currently set.
VFormulaProperty(const QString &name)
virtual void setValue(const QVariant &value) Q_DECL_OVERRIDE
Sets the value of the property.
virtual QString type() const Q_DECL_OVERRIDE
Returns a string containing the type of the property.
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const Q_DECL_OVERRIDE
Get the data how it should be displayed.
virtual Qt::ItemFlags flags(int column=DPC_Name) const Q_DECL_OVERRIDE
Returns item flags.
VFormula GetFormula() const
Returns the formula.
void SetFormula(const VFormula &formula)
Sets the formula.
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &options, const QAbstractItemDelegate *delegate) Q_DECL_OVERRIDE
Returns an editor widget, or NULL if it doesn't supply one.
virtual void childValueChanged(const QVariant &value, int typeForParent) Q_DECL_OVERRIDE
virtual bool setEditorData(QWidget *editor) Q_DECL_OVERRIDE
Sets the property's data to the editor (returns false, if the standard delegate should do that)
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
Clones this property.
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
Gets the data from the widget.
virtual QVariant getValue() const Q_DECL_OVERRIDE
Returns the value of the property as a QVariant.
QString value
Definition: vformula.h:100
QString GetFormula(FormulaType type=FormulaType::ToUser) const
Definition: vformula.cpp:135
static int FormulaTypeId()
Definition: vformula.cpp:244
void SetFormula(const QString &value, FormulaType type=FormulaType::ToUser)
Definition: vformula.cpp:148
Class for holding a plain text property.
void setOsSeparator(bool separator)
QList< VProperty * > Children
List of child properties.
Definition: vproperty_p.h:68
virtual void setUpdateBehaviour(bool update_parent, bool update_children)
Sets whether the views should update Parents or children after this property changes.
Definition: vproperty.cpp:326
VPropertyPrivate * d_ptr
The protected structure holding the member variables (to assure binary compatibility)
Definition: vproperty.h:214
virtual QString getName() const
Gets the name of the property.
Definition: vproperty.cpp:204
virtual int addChild(VProperty *child)
Adds a child to this property.
Definition: vproperty.cpp:278
VProperty(const QString &name, QVariant::Type type=QVariant::String)
Standard constructor, takes a name and a parent property as argument.
Definition: vproperty.cpp:36
#define qApp
Definition: vapplication.h:67