Seamly2D
Code documentation
vstringproperty.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vstringproperty.cpp
4  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
5  ** @date 27 8, 2014
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 "vstringproperty.h"
22 
23 #include <QKeyEvent>
24 #include <QLatin1String>
25 #include <QLineEdit>
26 #include <QLocale>
27 #include <QSizePolicy>
28 #include <QStaticStringData>
29 #include <QStringData>
30 #include <QStringDataPtr>
31 #include <QWidget>
32 
33 #include "../vproperty_p.h"
34 
36  : VProperty(name, QVariant::String)
37  , readOnly(false)
38  , typeForParent(0)
39  , clearButton(false)
40  , m_osSeparator(false)
41 {
42  VProperty::setSettings(settings);
43  d_ptr->VariantValue.setValue(QString());
44  d_ptr->VariantValue.convert(QVariant::String);
45 }
46 
48  : VProperty(name)
49  , readOnly(false)
50  , typeForParent(0)
51  , clearButton(false)
52  , m_osSeparator(false)
53 {
54  d_ptr->VariantValue.setValue(QString());
55  d_ptr->VariantValue.convert(QVariant::String);
56 }
57 
58 QWidget *VPE::VStringProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options,
59  const QAbstractItemDelegate *delegate)
60 {
61  Q_UNUSED(options)
62  Q_UNUSED(delegate)
63 
64  QLineEdit *tmpEditor = new QLineEdit(parent);
65  tmpEditor->setMinimumWidth(140);
66  tmpEditor->setAlignment(Qt::AlignTop);
67  tmpEditor->setLocale(parent->locale());
68  tmpEditor->setReadOnly(readOnly);
69  tmpEditor->installEventFilter(this);
70  tmpEditor->setClearButtonEnabled(clearButton);
71  tmpEditor->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
72  tmpEditor->setText(d_ptr->VariantValue.toString());
73 
74  d_ptr->editor = tmpEditor;
75  return d_ptr->editor;
76 }
77 
78 QVariant VPE::VStringProperty::getEditorData(const QWidget *editor) const
79 {
80  const QLineEdit *tmpEditor = qobject_cast<const QLineEdit*>(editor);
81  if (tmpEditor)
82  {
83  return tmpEditor->text();
84  }
85 
86  return QVariant(QString());
87 }
88 
90 {
91  this->readOnly = readOnly;
92 }
93 
95 {
96  m_osSeparator = separator;
97 }
98 
100 {
101  this->clearButton = value;
102 }
103 
104 void VPE::VStringProperty::setSetting(const QString &key, const QVariant &value)
105 {
106  if (key == QLatin1String("ReadOnly"))
107  {
108  setReadOnly(value.toBool());
109  }
110  if (key == QLatin1String("TypeForParent"))
111  {
112  setTypeForParent(value.toInt());
113  }
114 }
115 
116 QVariant VPE::VStringProperty::getSetting(const QString &key) const
117 {
118  if (key == QLatin1String("ReadOnly"))
119  {
120  return readOnly;
121  }
122  else if (key == QLatin1String("TypeForParent"))
123  {
124  return typeForParent;
125  }
126  else
127  return VProperty::getSetting(key);
128 }
129 
131 {
132  QStringList settings;
133  settings << QStringLiteral("ReadOnly") << QStringLiteral("TypeForParent");
134  return settings;
135 }
136 
138 {
139  return QStringLiteral("string");
140 }
141 
142 VPE::VProperty *VPE::VStringProperty::clone(bool include_children, VPE::VProperty *container) const
143 {
144  return VProperty::clone(include_children, container ? container : new VStringProperty(getName(), getSettings()));
145 }
146 
147 void VPE::VStringProperty::updateParent(const QVariant &value)
148 {
149  emit childChanged(value, typeForParent);
150 }
151 
152 // cppcheck-suppress unusedFunction
154 {
155  return typeForParent;
156 }
157 
159 {
160  typeForParent = value;
161 }
162 
163 bool VPE::VStringProperty::eventFilter(QObject *object, QEvent *event)
164 {
165  if (QLineEdit *textEdit = qobject_cast<QLineEdit *>(object))
166  {
167  if (event->type() == QEvent::KeyPress)
168  {
169  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
170  if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier))
171  {
172  if (m_osSeparator)
173  {
174  textEdit->insert(QLocale().decimalPoint());
175  }
176  else
177  {
178  textEdit->insert(QLocale::c().decimalPoint());
179  }
180  return true;
181  }
182  }
183  }
184  else
185  {
186  // pass the event on to the parent class
187  return VProperty::eventFilter(object, event);
188  }
189  return false;// pass the event to the widget
190 }
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 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
virtual void setSettings(const QMap< QString, QVariant > &settings)
Sets the settings by calling the overloaded setSetting(const QString &key, const QVariant &value) for...
Definition: vproperty.cpp:333
Class for holding a string property.
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
Gets the data from the widget.
virtual QString type() const Q_DECL_OVERRIDE
Returns a string containing the type of the property.
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.
void setTypeForParent(int value)
void setReadOnly(bool readOnly)
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.
void setOsSeparator(bool separator)
virtual void updateParent(const QVariant &value) Q_DECL_OVERRIDE
virtual QStringList getSettingKeys() const Q_DECL_OVERRIDE
Returns the list of keys of the property's settings.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
Clones this property.
void setClearButtonEnable(bool value)
virtual void setSetting(const QString &key, const QVariant &value) Q_DECL_OVERRIDE
Sets the settings.
virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE
VStringProperty(const QString &name, const QMap< QString, QVariant > &settings)