Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qitemeditorfactory.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qplatformdefs.h>
7
8#if QT_CONFIG(combobox)
9#include <qcombobox.h>
10#endif
11#if QT_CONFIG(datetimeedit)
12#include <qdatetimeedit.h>
13#endif
14#if QT_CONFIG(label)
15#include <qlabel.h>
16#endif
17#if QT_CONFIG(lineedit)
18#include <qlineedit.h>
19#endif
20#if QT_CONFIG(spinbox)
21#include <qspinbox.h>
22#endif
23#include <qstyle.h>
24#include <qstyleoption.h>
25#include <limits.h>
26#include <float.h>
27#include <qapplication.h>
28#include <qdebug.h>
29
30#include <vector>
31#include <algorithm>
33
34
35#if QT_CONFIG(combobox)
36
37class QBooleanComboBox : public QComboBox
38{
40 Q_PROPERTY(bool value READ value WRITE setValue USER true)
41
42public:
43 QBooleanComboBox(QWidget *parent);
44 void setValue(bool);
45 bool value() const;
46};
47
48#endif // QT_CONFIG(combobox)
49
50
51#if QT_CONFIG(spinbox)
52
53class QUIntSpinBox : public QSpinBox
54{
56 Q_PROPERTY(uint value READ uintValue WRITE setUIntValue NOTIFY uintValueChanged USER true)
57public:
58 explicit QUIntSpinBox(QWidget *parent = nullptr)
59 : QSpinBox(parent)
60 {
61 connect(this, SIGNAL(valueChanged(int)), SIGNAL(uintValueChanged()));
62 }
63
64 uint uintValue()
65 {
66 return value();
67 }
68
69 void setUIntValue(uint value_)
70 {
71 return setValue(value_);
72 }
73
75 void uintValueChanged();
76};
77
78#endif // QT_CONFIG(spinbox)
79
139{
140 QItemEditorCreatorBase *creator = creatorMap.value(userType, 0);
141 if (!creator) {
142 const QItemEditorFactory *dfactory = defaultFactory();
143 return dfactory == this ? nullptr : dfactory->createEditor(userType, parent);
144 }
145 return creator->createWidget(parent);
146}
147
152{
153 QItemEditorCreatorBase *creator = creatorMap.value(userType, 0);
154 if (!creator) {
155 const QItemEditorFactory *dfactory = defaultFactory();
156 return dfactory == this ? QByteArray() : dfactory->valuePropertyName(userType);
157 }
158 return creator->valuePropertyName();
159}
160
165{
166 //we make sure we delete all the QItemEditorCreatorBase
167 //this has to be done only once, hence the sort-unique idiom
168 std::vector<QItemEditorCreatorBase*> creators(creatorMap.cbegin(), creatorMap.cend());
169 std::sort(creators.begin(), creators.end());
170 const auto it = std::unique(creators.begin(), creators.end());
171 qDeleteAll(creators.begin(), it);
172}
173
183{
184 const auto it = creatorMap.constFind(userType);
185 if (it != creatorMap.cend()) {
186 QItemEditorCreatorBase *oldCreator = it.value();
187 Q_ASSERT(oldCreator);
188 creatorMap.erase(it);
189 if (std::find(creatorMap.cbegin(), creatorMap.cend(), oldCreator) == creatorMap.cend())
190 delete oldCreator; // if it is no more in use we can delete it
191 }
192
193 creatorMap[userType] = creator;
194}
195
197{
198public:
200 QWidget *createEditor(int userType, QWidget *parent) const override;
201 QByteArray valuePropertyName(int) const override;
202};
203
205{
206 switch (userType) {
207#if QT_CONFIG(combobox)
208 case QMetaType::Bool: {
209 QBooleanComboBox *cb = new QBooleanComboBox(parent);
210 cb->setFrame(false);
211 cb->setSizePolicy(QSizePolicy::Ignored, cb->sizePolicy().verticalPolicy());
212 return cb; }
213#endif
214#if QT_CONFIG(spinbox)
215 case QMetaType::UInt: {
216 QSpinBox *sb = new QUIntSpinBox(parent);
217 sb->setFrame(false);
218 sb->setMinimum(0);
219 sb->setMaximum(INT_MAX);
220 sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy());
221 return sb; }
222 case QMetaType::Int: {
223 QSpinBox *sb = new QSpinBox(parent);
224 sb->setFrame(false);
225 sb->setMinimum(INT_MIN);
226 sb->setMaximum(INT_MAX);
227 sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy());
228 return sb; }
229#endif
230#if QT_CONFIG(datetimeedit)
231 case QMetaType::QDate: {
232 QDateTimeEdit *ed = new QDateEdit(parent);
233 ed->setFrame(false);
234 return ed; }
235 case QMetaType::QTime: {
236 QDateTimeEdit *ed = new QTimeEdit(parent);
237 ed->setFrame(false);
238 return ed; }
239 case QMetaType::QDateTime: {
240 QDateTimeEdit *ed = new QDateTimeEdit(parent);
241 ed->setFrame(false);
242 return ed; }
243#endif
244#if QT_CONFIG(label)
245 case QMetaType::QPixmap:
246 return new QLabel(parent);
247#endif
248#if QT_CONFIG(spinbox)
249 case QMetaType::Double: {
250 QDoubleSpinBox *sb = new QDoubleSpinBox(parent);
251 sb->setFrame(false);
252 sb->setMinimum(-DBL_MAX);
253 sb->setMaximum(DBL_MAX);
254 sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy());
255 return sb; }
256#endif
257#if QT_CONFIG(lineedit)
258 case QMetaType::QString:
259 default: {
260 // the default editor is a lineedit
261 QExpandingLineEdit *le = new QExpandingLineEdit(parent);
262 le->setFrame(le->style()->styleHint(QStyle::SH_ItemView_DrawDelegateFrame, nullptr, le));
263 if (!le->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, nullptr, le))
264 le->setWidgetOwnsGeometry(true);
265 return le; }
266#else
267 default:
268 break;
269#endif
270 }
271 return nullptr;
272}
273
275{
276 switch (userType) {
277#if QT_CONFIG(combobox)
278 case QMetaType::Bool:
279 return "currentIndex";
280#endif
281#if QT_CONFIG(spinbox)
282 case QMetaType::UInt:
283 case QMetaType::Int:
284 case QMetaType::Double:
285 return "value";
286#endif
287#if QT_CONFIG(datetimeedit)
288 case QMetaType::QDate:
289 return "date";
290 case QMetaType::QTime:
291 return "time";
292 case QMetaType::QDateTime:
293 return "dateTime";
294#endif
295 case QMetaType::QString:
296 default:
297 // the default editor is a lineedit
298 return "text";
299 }
300}
301
308
321
334
378
510#if QT_CONFIG(lineedit)
511
512QExpandingLineEdit::QExpandingLineEdit(QWidget *parent)
513 : QLineEdit(parent), originalWidth(-1), widgetOwnsGeometry(false)
514{
515 connect(this, SIGNAL(textChanged(QString)), this, SLOT(resizeToContents()));
516 updateMinimumWidth();
517}
518
519void QExpandingLineEdit::changeEvent(QEvent *e)
520{
521 switch (e->type())
522 {
526 updateMinimumWidth();
527 break;
528 default:
529 break;
530 }
531
533}
534
535void QExpandingLineEdit::updateMinimumWidth()
536{
537 const QMargins tm = textMargins();
538 const QMargins cm = contentsMargins();
539 const int width = tm.left() + tm.right() + cm.left() + cm.right() + 4 /*horizontalMargin in qlineedit.cpp*/;
540
542 initStyleOption(&opt);
543
544 int minWidth = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(width, 0), this).width();
545 setMinimumWidth(minWidth);
546}
547
548void QExpandingLineEdit::resizeToContents()
549{
550 int oldWidth = width();
551 if (originalWidth == -1)
552 originalWidth = oldWidth;
553 if (QWidget *parent = parentWidget()) {
554 QPoint position = pos();
555 int hintWidth = minimumWidth() + fontMetrics().horizontalAdvance(displayText());
556 int parentWidth = parent->width();
557 int maxWidth = isRightToLeft() ? position.x() + oldWidth : parentWidth - position.x();
558 int newWidth = qBound(qMin(originalWidth, maxWidth), hintWidth, maxWidth);
559 if (widgetOwnsGeometry)
560 setMaximumWidth(newWidth);
561 if (isRightToLeft())
562 move(position.x() - newWidth + oldWidth, position.y());
563 resize(newWidth, height());
564 }
565}
566
567#endif // QT_CONFIG(lineedit)
568
569#if QT_CONFIG(combobox)
570
571QBooleanComboBox::QBooleanComboBox(QWidget *parent)
572 : QComboBox(parent)
573{
574 addItem(QComboBox::tr("False"));
575 addItem(QComboBox::tr("True"));
576}
577
578void QBooleanComboBox::setValue(bool value)
579{
580 setCurrentIndex(value ? 1 : 0);
581}
582
583bool QBooleanComboBox::value() const
584{
585 return (currentIndex() == 1);
586}
587
588#endif // QT_CONFIG(combobox)
589
591
592#if QT_CONFIG(lineedit) || QT_CONFIG(combobox)
593#include "qitemeditorfactory.moc"
594#endif
595
596#include "moc_qitemeditorfactory_p.cpp"
\inmodule QtCore
Definition qbytearray.h:57
The QComboBox widget combines a button with a dropdown list.
Definition qcombobox.h:24
The QDateEdit class provides a widget for editing dates based on the QDateTimeEdit widget.
The QDateTimeEdit class provides a widget for editing dates and times.
QWidget * createEditor(int userType, QWidget *parent) const override
Creates an editor widget with the given parent for the specified userType of data,...
QByteArray valuePropertyName(int) const override
Returns the property name used to access data for the given userType of data.
The QDoubleSpinBox class provides a spin box widget that takes doubles.
Definition qspinbox.h:82
\inmodule QtCore
Definition qcoreevent.h:45
@ ContentsRectChange
Definition qcoreevent.h:219
@ StyleChange
Definition qcoreevent.h:136
@ FontChange
Definition qcoreevent.h:133
Type type() const
Returns the event type.
Definition qcoreevent.h:304
const_iterator cbegin() const noexcept
Definition qhash.h:1214
const_iterator constFind(const Key &key) const noexcept
Definition qhash.h:1299
iterator erase(const_iterator it)
Definition qhash.h:1233
T value(const Key &key) const noexcept
Definition qhash.h:1054
const_iterator cend() const noexcept
Definition qhash.h:1218
The QItemEditorCreatorBase class provides an abstract base class that must be subclassed when impleme...
virtual QByteArray valuePropertyName() const =0
Returns the name of the property used to get and set values in the creator's editor widgets.
virtual QWidget * createWidget(QWidget *parent) const =0
Returns an editor widget with the given parent.
virtual ~QItemEditorCreatorBase()
Destroys the editor creator object.
The QItemEditorFactory class provides widgets for editing item data in views and delegates.
virtual QByteArray valuePropertyName(int userType) const
Returns the property name used to access data for the given userType of data.
virtual ~QItemEditorFactory()
Destroys the item editor factory.
void registerEditor(int userType, QItemEditorCreatorBase *creator)
Registers an item editor creator specified by creator for the given userType of data.
static void setDefaultFactory(QItemEditorFactory *factory)
[2]
static const QItemEditorFactory * defaultFactory()
Returns the default item editor factory.
virtual QWidget * createEditor(int userType, QWidget *parent) const
Creates an editor widget with the given parent for the specified userType of data,...
The QLabel widget provides a text or image display.
Definition qlabel.h:20
The QLineEdit widget is a one-line text editor.
Definition qlineedit.h:28
void changeEvent(QEvent *) override
\reimp
\inmodule QtCore
Definition qmargins.h:24
constexpr int left() const noexcept
Returns the left margin.
Definition qmargins.h:106
constexpr int right() const noexcept
Returns the right margin.
Definition qmargins.h:112
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore
Definition qsize.h:25
The QSpinBox class provides a spin box widget.
Definition qspinbox.h:16
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\variable QStyleOptionFocusRect::backgroundColor
@ CT_LineEdit
Definition qstyle.h:561
@ SH_ItemView_DrawDelegateFrame
Definition qstyle.h:676
@ SH_ItemView_ShowDecorationSelected
Definition qstyle.h:645
The QTimeEdit class provides a widget for editing times based on the QDateTimeEdit widget.
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
int width
the width of the widget excluding any window frame
Definition qwidget.h:114
a resize(100000)
void textChanged(const QString &newText)
qDeleteAll(list.begin(), list.end())
QSet< QString >::iterator it
QStyleOptionButton opt
fontMetrics
Combined button and popup list for selecting options.
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isRightToLeft(QStringView string) noexcept
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
static QItemEditorFactory * q_default_factory
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLint GLsizei GLsizei height
GLint GLsizei width
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
#define Q_PROPERTY(...)
#define Q_OBJECT
#define Q_SIGNALS
unsigned int uint
Definition qtypes.h:34
settings setValue("DataPump/bgcolor", color)
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QObject::connect nullptr
scene addItem(form)
QItemEditorCreatorBase * creator
QItemEditorFactory * factory
QPointer< QLineEdit > le