Seamly2D
Code documentation
venumproperty.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file venumproperty.cpp
4  ** @author hedgeware <internal(at)hedgeware.net>
5  ** @date
6  **
7  ** @brief
8  ** @copyright
9  ** All rights reserved. This program and the accompanying materials
10  ** are made available under the terms of the GNU Lesser General Public License
11  ** (LGPL) version 2.1 which accompanies this distribution, and is available at
12  ** http://www.gnu.org/licenses/lgpl-2.1.html
13  **
14  ** This library 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 GNU
17  ** Lesser General Public License for more details.
18  **
19  *************************************************************************/
20 
21 #include "venumproperty.h"
22 
23 #include <QComboBox>
24 #include <QCoreApplication>
25 #include <QLocale>
26 #include <QWidget>
27 
28 #include "../vproperty_p.h"
29 
31  : VProperty(name, QVariant::Int), EnumerationLiterals()
32 {
34  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
35 }
36 
37 
38 //! Get the data how it should be displayed
39 QVariant VPE::VEnumProperty::data (int column, int role) const
40 {
41  if (EnumerationLiterals.empty())
42  {
43  return QVariant();
44  }
45 
46  int tmpIndex = VProperty::d_ptr->VariantValue.toInt();
47 
48  if (tmpIndex < 0 || tmpIndex >= EnumerationLiterals.count())
49  {
50  tmpIndex = 0;
51  }
52 
53  if (column == DPC_Data && Qt::DisplayRole == role)
54  {
55  return EnumerationLiterals.at(tmpIndex);
56  }
57  else if (column == DPC_Data && Qt::EditRole == role)
58  {
59  return tmpIndex;
60  }
61  else
62  return VProperty::data(column, role);
63 }
64 
65 
66 //! Returns an editor widget, or NULL if it doesn't supply one
67 QWidget* VPE::VEnumProperty::createEditor(QWidget * parent, const QStyleOptionViewItem& options,
68  const QAbstractItemDelegate* delegate)
69 {
70  Q_UNUSED(options)
71  Q_UNUSED(delegate)
72  QComboBox* tmpEditor = new QComboBox(parent);
73  tmpEditor->clear();
74  tmpEditor->setLocale(parent->locale());
75  tmpEditor->addItems(EnumerationLiterals);
76  tmpEditor->setCurrentIndex(VProperty::d_ptr->VariantValue.toInt());
77  connect(tmpEditor, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
79 
80  VProperty::d_ptr->editor = tmpEditor;
81  return VProperty::d_ptr->editor;
82 }
83 
84 //! Gets the data from the widget
85 QVariant VPE::VEnumProperty::getEditorData(const QWidget *editor) const
86 {
87  const QComboBox* tmpEditor = qobject_cast<const QComboBox*>(editor);
88  if (tmpEditor)
89  {
90  return tmpEditor->currentIndex();
91  }
92 
93  return QVariant(0);
94 }
95 
96 //! Sets the enumeration literals
97 void VPE::VEnumProperty::setLiterals(const QStringList& literals)
98 {
99  EnumerationLiterals = literals;
100 }
101 
102 //! Get the settings. This function has to be implemented in a subclass in order to have an effect
104 {
105  return EnumerationLiterals;
106 }
107 
108 //! Sets the value of the property
109 void VPE::VEnumProperty::setValue(const QVariant& value)
110 {
111  int tmpIndex = value.toInt();
112 
113  if (tmpIndex < 0 || tmpIndex >= EnumerationLiterals.count())
114  {
115  tmpIndex = 0;
116  }
117 
118  VProperty::d_ptr->VariantValue = tmpIndex;
119  VProperty::d_ptr->VariantValue.convert(QVariant::Int);
120 
121  if (VProperty::d_ptr->editor != nullptr)
122  {
123  setEditorData(VProperty::d_ptr->editor);
124  }
125 }
126 
128 {
129  return "enum";
130 }
131 
132 VPE::VProperty* VPE::VEnumProperty::clone(bool include_children, VProperty* container) const
133 {
134  return VProperty::clone(include_children, container ? container : new VEnumProperty(getName()));
135 }
136 
137 void VPE::VEnumProperty::setSetting(const QString& key, const QVariant& value)
138 {
139  if (key == "literals")
140  {
141  setLiterals(value.toString().split(";;"));
142  }
143 }
144 
145 QVariant VPE::VEnumProperty::getSetting(const QString& key) const
146 {
147  if (key == "literals")
148  {
149  return getLiterals().join(";;");
150  }
151  else
152  return VProperty::getSetting(key);
153 }
154 
156 {
157  return QStringList("literals");
158 }
159 
161 {
162  Q_UNUSED(index)
163  UserChangeEvent *event = new UserChangeEvent();
164  QCoreApplication::postEvent (VProperty::d_ptr->editor, event );
165 }
void currentIndexChanged(int index)
virtual QStringList getLiterals() const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
virtual void setValue(const QVariant &value) Q_DECL_OVERRIDE
Sets the value of the property.
virtual QStringList getSettingKeys() const Q_DECL_OVERRIDE
Returns the list of keys of the property's settings.
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const Q_DECL_OVERRIDE
Get the data how it should be displayed.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
Clones this property.
virtual void setSetting(const QString &key, const QVariant &value) Q_DECL_OVERRIDE
Sets the settings. Available settings:
virtual QString type() const Q_DECL_OVERRIDE
Returns a string containing the type of the property.
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
Gets the data from the widget.
virtual QVariant getSetting(const QString &key) const Q_DECL_OVERRIDE
Get the settings. This function has to be implemented in a subclass in order to have an effect.
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.
VEnumProperty(const QString &name)
Constructor.
virtual void setLiterals(const QStringList &literals)
Sets the enumeration literals.
QVariant VariantValue
The property's value. This does not have to be used by subclasses, but it makes sense in cases where ...
Definition: vproperty_p.h:40
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const
Clones this property.
Definition: vproperty.cpp:372
VPropertyPrivate * d_ptr
The protected structure holding the member variables (to assure binary compatibility)
Definition: vproperty.h:214
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const
Get the data how it should be displayed.
Definition: vproperty.cpp:68
virtual QVariant getSetting(const QString &key) const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
Definition: vproperty.cpp:360