Seamly2D
Code documentation
vobjectproperty.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file vobjectproperty.h
3  ** @author DS Caskey
4  ** @date Feb 21, 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 #include "vobjectproperty.h"
29 
30 #include <QComboBox>
31 #include <QCoreApplication>
32 #include <QLocale>
33 #include <QWidget>
34 
35 #include "../vproperty_p.h"
36 
38  : VProperty(name, QVariant::Int)
39  , m_objects()
40  , m_indexList()
41 {
43  VProperty::d_ptr->VariantValue.convert(QVariant::UInt);
44 }
45 
46 //! Get the data how it should be displayed
47 QVariant VPE::VObjectProperty::data(int column, int role) const
48 {
49  if (m_objects.empty())
50  {
51  return QVariant();
52  }
53 
54  int tmpIndex = VProperty::d_ptr->VariantValue.toInt();
55 
56  if (tmpIndex < 0 || tmpIndex >= m_indexList.count())
57  {
58  tmpIndex = 0;
59  }
60 
61  if (column == DPC_Data && Qt::DisplayRole == role)
62  {
63  return m_objects.value(m_indexList.at(tmpIndex));
64  }
65  else if (column == DPC_Data && Qt::EditRole == role)
66  {
67  return tmpIndex;
68  }
69  else
70  {
71  return VProperty::data(column, role);
72  }
73 }
74 
75 //! Returns an editor widget, or NULL if it doesn't supply one
76 QWidget *VPE::VObjectProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options,
77  const QAbstractItemDelegate *delegate)
78 {
79  Q_UNUSED(options)
80  Q_UNUSED(delegate)
81  QComboBox *objEditor = new QComboBox(parent);
82  objEditor->clear();
83  objEditor->setMinimumWidth(140);
84  objEditor->setLocale(parent->locale());
85  fillListItems(objEditor, m_objects);
86  objEditor->setCurrentIndex(VProperty::d_ptr->VariantValue.toInt());
87  connect(objEditor, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
89 
90  VProperty::d_ptr->editor = objEditor;
91  return VProperty::d_ptr->editor;
92 }
93 
94 //! Gets the data from the widget
95 QVariant VPE::VObjectProperty::getEditorData(const QWidget *editor) const
96 {
97  const QComboBox *objEditor = qobject_cast<const QComboBox*>(editor);
98  if (objEditor)
99  {
100  return objEditor->currentIndex();
101  }
102 
103  return QVariant(0);
104 }
105 
106 //! Sets the objects list
107 // cppcheck-suppress unusedFunction
109 {
110  this->m_objects = objects;
111 
112  m_indexList.clear();
113  QMap<QString, quint32>::const_iterator i = m_objects.constBegin();
114  while (i != objects.constEnd())
115  {
116  m_indexList.append(i.key());
117  ++i;
118  }
119 }
120 
121 //! Get the settings. This function has to be implemented in a subclass in order to have an effect
122 // cppcheck-suppress unusedFunction
124 {
125  return m_objects;
126 }
127 
128 //! Sets the value of the property
129 void VPE::VObjectProperty::setValue(const QVariant &value)
130 {
131  int tmpIndex = value.toInt();
132 
133  if (tmpIndex < 0 || tmpIndex >= m_indexList.count())
134  {
135  tmpIndex = 0;
136  }
137 
139  VProperty::d_ptr->VariantValue.convert(QVariant::UInt);
140 
141  if (VProperty::d_ptr->editor != nullptr)
142  {
143  setEditorData(VProperty::d_ptr->editor);
144  }
145 }
146 
148 {
149  return "objectList";
150 }
151 
152 VPE::VProperty *VPE::VObjectProperty::clone(bool include_children, VProperty *container) const
153 {
154  return VProperty::clone(include_children, container ? container : new VObjectProperty(getName()));
155 }
156 
157 int VPE::VObjectProperty::indexOfObject(const QMap<QString, quint32> &objects, const QString &object)
158 {
159  QVector<QString> m_indexList;
160  QMap<QString, quint32>::const_iterator i = objects.constBegin();
161  while (i != objects.constEnd())
162  {
163  m_indexList.append(i.key());
164  ++i;
165  }
166  return m_indexList.indexOf(object);
167 }
168 
170 {
171  Q_UNUSED(index)
172  UserChangeEvent *event = new UserChangeEvent();
173  QCoreApplication::postEvent (VProperty::d_ptr->editor, event );
174 }
175 
176 void VPE::VObjectProperty::fillListItems(QComboBox *box, const QMap<QString, quint32> &list) const
177 {
178  box->clear();
179 
181  for (i = list.constBegin(); i != list.constEnd(); ++i)
182  {
183  box->addItem(i.key(), i.value());
184  }
185 }
VObjectProperty(const QString &name)
Constructor.
static int indexOfObject(const QMap< QString, quint32 > &objects, const QString &object)
virtual QString type() const Q_DECL_OVERRIDE
Returns a string containing the type of the property.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
Clones this 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 void setValue(const QVariant &value) Q_DECL_OVERRIDE
Sets the value of the property.
void currentIndexChanged(int index)
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
Gets the data from the widget.
void setObjectsList(const QMap< QString, quint32 > &objects)
Sets the objects list.
void fillListItems(QComboBox *box, const QMap< QString, quint32 > &list) const
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 QMap< QString, quint32 > getObjects() const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
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