Seamly2D
Code documentation
plaintext_property.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file plaintext_property.cpp
4  ** @author DS Caskey
5  ** @date Feb 18, 2023
6  **
7  ** @brief
8  ** @copyright
9  ** This source code is part of the Seamly2D project, a pattern making
10  ** program, whose allow create and modeling patterns of clothing.
11  ** Copyright (C) 2017-2023 Seamly2D project
12  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
13  **
14  ** Seamly2D is free software: you can redistribute it and/or modify
15  ** it under the terms of the GNU General Public License as published by
16  ** the Free Software Foundation, either version 3 of the License, or
17  ** (at your option) any later version.
18  **
19  ** Seamly2D is distributed in the hope that it will be useful,
20  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
21  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  ** GNU General Public License for more details.
23  **
24  ** You should have received a copy of the GNU General Public License
25  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #include "plaintext_property.h"
30 
31 #include <QKeyEvent>
32 #include <QLatin1String>
33 #include <QLocale>
34 #include <QSizePolicy>
35 #include <QStaticStringData>
36 #include <QStringData>
37 #include <QStringDataPtr>
38 #include <QWidget>
39 
40 #include "expandingtextedit.h"
41 #include "../vproperty_p.h"
42 
43 
45  : VProperty(name, QVariant::String)
46  , m_readOnly(false)
47  , m_typeForParent(0)
48  , m_osSeparator(false)
49 {
50  VProperty::setSettings(settings);
51  d_ptr->VariantValue.setValue(QString());
52  d_ptr->VariantValue.convert(QVariant::String);
53 }
54 
56  : VProperty(name)
57  , m_readOnly(false)
58  , m_typeForParent(0)
59  , m_osSeparator(false)
60 {
61  d_ptr->VariantValue.setValue(QString());
62  d_ptr->VariantValue.convert(QVariant::String);
63 }
64 
65 /**
66  * @brief createEditor Returns an editor widget, or NULL if it doesn't supply one
67  * @param parent The widget to which the editor will be added as a child
68  * @param options Render options
69  * @param delegate A pointer to the QAbstractItemDelegate requesting the editor.
70  * This can be used to connect signals and slots.
71  */
72 QWidget *VPE::PlainTextProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options,
73  const QAbstractItemDelegate *delegate)
74 {
75  Q_UNUSED(options)
76  Q_UNUSED(delegate)
77 
78  ExpandingTextEdit *textEditor = new ExpandingTextEdit(parent);
79  textEditor->setMinimumWidth(140);
80  textEditor->setFixedHeight(28);
81  textEditor->setLocale(parent->locale());
82  textEditor->setReadOnly(m_readOnly);
83  textEditor->installEventFilter(this);
84  textEditor->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
85  textEditor->setPlainText(d_ptr->VariantValue.toString());
86 
87  d_ptr->editor = textEditor;
88  return d_ptr->editor;
89 }
90 
91 /**
92  * @brief getEditorData Gets the data from the widget
93  * @oaram editor
94  */
95 QVariant VPE::PlainTextProperty::getEditorData(const QWidget *editor) const
96 {
97  const QPlainTextEdit* textEditor = qobject_cast<const QPlainTextEdit*>(editor);
98  if (textEditor)
99  {
100  return textEditor->toPlainText();
101  }
102 
103  return QVariant(QString());
104 }
105 
107 {
108  this->m_readOnly = m_readOnly;
109 }
110 
112 {
113  m_osSeparator = separator;
114 }
115 
116 /**
117  * @brief setSetting Sets the settings.
118  */
119 void VPE::PlainTextProperty::setSetting(const QString &key, const QVariant &value)
120 {
121  if (key == QLatin1String("ReadOnly"))
122  {
123  setReadOnly(value.toBool());
124  }
125  if (key == QLatin1String("TypeForParent"))
126  {
127  setTypeForParent(value.toInt());
128  }
129 }
130 
131 /**
132  * @brief getSetting Gets the settings.his function has to be implemented in a subclass in order to have an effect.
133  */
134 QVariant VPE::PlainTextProperty::getSetting(const QString &key) const
135 {
136  if (key == QLatin1String("ReadOnly"))
137  {
138  return m_readOnly;
139  }
140  else if (key == QLatin1String("TypeForParent"))
141  {
142  return m_typeForParent;
143  }
144  else
145  return VProperty::getSetting(key);
146 }
147 
148 /**
149  * @brief getSettingKeys Returns the list of keys of the property's settings.
150  */
152 {
153  QStringList settings;
154  settings << QStringLiteral("ReadOnly") << QStringLiteral("TypeForParent");
155  return settings;
156 }
157 
158 /**
159  * @brief type Returns a string containing the type of the property.
160  */
162 {
163  return QStringLiteral("string");
164 }
165 
166 /**
167  * @brief clone Clones this property
168  * @param include_children Indicates whether to also clone the children
169  * @param container If a property is being passed here, no new VProperty is being created but instead it is tried
170  * to fill all the data into container. This can also be used when subclassing this function.
171  * @return Returns the newly created property (or container, if it was not NULL)
172  */
173 VPE::VProperty *VPE::PlainTextProperty::clone(bool include_children, VPE::VProperty *container) const
174 {
175  return VProperty::clone(include_children, container ? container : new PlainTextProperty(getName(), getSettings()));
176 }
177 
178 void VPE::PlainTextProperty::updateParent(const QVariant &value)
179 {
180  emit childChanged(value, m_typeForParent);
181 }
182 
183 // cppcheck-suppress unusedFunction
185 {
186  return m_typeForParent;
187 }
188 
190 {
191  m_typeForParent = value;
192 }
193 
194 bool VPE::PlainTextProperty::eventFilter(QObject *object, QEvent *event)
195 {
196  if (QPlainTextEdit *textEdit = qobject_cast<QPlainTextEdit *>(object))
197  {
198  if (event->type() == QEvent::KeyPress)
199  {
200  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
201  if ((keyEvent->key() == Qt::Key_Period) && (keyEvent->modifiers() & Qt::KeypadModifier))
202  {
203  if (m_osSeparator)
204  {
205  textEdit->insertPlainText(QLocale().decimalPoint());
206  }
207  else
208  {
209  textEdit->insertPlainText(QLocale::c().decimalPoint());
210  }
211  return true;
212  }
213  }
214  }
215  else
216  {
217  // pass the event on to the parent class
218  return VProperty::eventFilter(object, event);
219  }
220  return false;// pass the event to the widget
221 }
Class for holding a plain text property.
void setReadOnly(bool readOnly)
virtual void updateParent(const QVariant &value) Q_DECL_OVERRIDE
virtual void setSetting(const QString &key, const QVariant &value) Q_DECL_OVERRIDE
setSetting Sets the settings.
virtual bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE
void setOsSeparator(bool separator)
virtual QVariant getEditorData(const QWidget *editor) const Q_DECL_OVERRIDE
getEditorData Gets the data from the widget @oaram editor
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const Q_DECL_OVERRIDE
clone Clones this property
PlainTextProperty(const QString &name, const QMap< QString, QVariant > &settings)
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &options, const QAbstractItemDelegate *delegate) Q_DECL_OVERRIDE
createEditor Returns an editor widget, or NULL if it doesn't supply one
void setTypeForParent(int value)
virtual QString type() const Q_DECL_OVERRIDE
type Returns a string containing the type of the property.
virtual QStringList getSettingKeys() const Q_DECL_OVERRIDE
getSettingKeys Returns the list of keys of the property's settings.
virtual QVariant getSetting(const QString &key) const Q_DECL_OVERRIDE
getSetting Gets the settings.his function has to be implemented in a subclass in order to have an eff...
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