5#include <qplatformdefs.h>
12#if QT_CONFIG(datetimeedit)
13#include <qdatetimeedit.h>
18#if QT_CONFIG(lineedit)
25#include <qstyleoption.h>
28#include <qapplication.h>
36#if QT_CONFIG(combobox)
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
127
128
129
130
133
134
135
136
137
138QWidget *QItemEditorFactory::createEditor(
int userType, QWidget *parent)
const
140 QItemEditorCreatorBase *creator = creatorMap.value(userType, 0);
142 const QItemEditorFactory *dfactory = defaultFactory();
143 return dfactory ==
this ?
nullptr : dfactory->createEditor(userType, parent);
145 return creator->createWidget(parent);
149
150
151QByteArray QItemEditorFactory::valuePropertyName(
int userType)
const
153 QItemEditorCreatorBase *creator = creatorMap.value(userType, 0);
155 const QItemEditorFactory *dfactory = defaultFactory();
156 return dfactory ==
this ? QByteArray() : dfactory->valuePropertyName(userType);
158 return creator->valuePropertyName();
162
163
164QItemEditorFactory::~QItemEditorFactory()
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);
175
176
177
178
179
180
181
182void QItemEditorFactory::registerEditor(
int userType, QItemEditorCreatorBase *creator)
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())
193 creatorMap[userType] = creator;
207#if QT_CONFIG(combobox)
208 case QMetaType::Bool: {
209 QBooleanComboBox *cb =
new QBooleanComboBox(parent);
211 cb->setSizePolicy(QSizePolicy::Ignored, cb->sizePolicy().verticalPolicy());
214#if QT_CONFIG(spinbox)
215 case QMetaType::UInt: {
216 QSpinBox *sb =
new QUIntSpinBox(parent);
219 sb->setMaximum(INT_MAX);
220 sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy());
222 case QMetaType::Int: {
223 QSpinBox *sb =
new QSpinBox(parent);
225 sb->setMinimum(INT_MIN);
226 sb->setMaximum(INT_MAX);
227 sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy());
230#if QT_CONFIG(datetimeedit)
231 case QMetaType::QDate: {
232 QDateTimeEdit *ed =
new QDateEdit(parent);
235 case QMetaType::QTime: {
236 QDateTimeEdit *ed =
new QTimeEdit(parent);
239 case QMetaType::QDateTime: {
240 QDateTimeEdit *ed =
new QItemEditorDateTimeEdit(parent);
245 case QMetaType::QPixmap:
246 return new QLabel(parent);
248#if QT_CONFIG(spinbox)
249 case QMetaType::Double: {
250 QDoubleSpinBox *sb =
new QDoubleSpinBox(parent);
252 sb->setMinimum(-DBL_MAX);
253 sb->setMaximum(DBL_MAX);
254 sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy());
257#if QT_CONFIG(lineedit)
258 case QMetaType::QString:
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);
277#if QT_CONFIG(combobox)
278 case QMetaType::Bool:
279 return "currentIndex";
281#if QT_CONFIG(spinbox)
282 case QMetaType::UInt:
284 case QMetaType::Double:
287#if QT_CONFIG(datetimeedit)
288 case QMetaType::QDate:
290 case QMetaType::QTime:
292 case QMetaType::QDateTime:
295 case QMetaType::QString:
310
311
312
313
314const QItemEditorFactory *QItemEditorFactory::defaultFactory()
316 static const QDefaultItemEditorFactory factory;
317 if (q_default_factory)
318 return q_default_factory;
323
324
325
326
327
328void QItemEditorFactory::setDefaultFactory(QItemEditorFactory *factory)
330 static const QDefaultFactoryCleaner cleaner;
331 delete q_default_factory;
332 q_default_factory = factory;
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
369
370
371
372
373QItemEditorCreatorBase::~QItemEditorCreatorBase()
379
380
381
382
383
384
385
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
437
438
439
440
441
442
443
444
445
446
449
450
451
454
455
456
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
493
494
495
496
499
500
501
504
505
506
508#if QT_CONFIG(lineedit)
510QExpandingLineEdit::QExpandingLineEdit(QWidget *parent)
511 : QLineEdit(parent), originalWidth(-1), widgetOwnsGeometry(
false)
513 connect(
this, SIGNAL(textChanged(QString)),
this, SLOT(resizeToContents()));
514 updateMinimumWidth();
517void QExpandingLineEdit::changeEvent(QEvent *e)
521 case QEvent::FontChange:
522 case QEvent::StyleChange:
523 case QEvent::ContentsRectChange:
524 updateMinimumWidth();
530 QLineEdit::changeEvent(e);
533void QExpandingLineEdit::updateMinimumWidth()
535 const QMargins tm = textMargins();
536 const QMargins cm = contentsMargins();
537 const int width = tm.left() + tm.right() + cm.left() + cm.right() + 4 ;
539 QStyleOptionFrame opt;
540 initStyleOption(&opt);
542 int minWidth = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(width, 0),
this).width();
543 setMinimumWidth(minWidth);
546void QExpandingLineEdit::resizeToContents()
548 int oldWidth = width();
549 if (originalWidth == -1)
550 originalWidth = oldWidth;
551 if (QWidget *parent = parentWidget()) {
552 QPoint position = pos();
553 int hintWidth = minimumWidth() + fontMetrics().horizontalAdvance(displayText());
554 int parentWidth = parent->width();
555 int maxWidth = isRightToLeft() ? position.x() + oldWidth : parentWidth - position.x();
556 int newWidth = qBound(qMin(originalWidth, maxWidth), hintWidth, maxWidth);
557 if (widgetOwnsGeometry)
558 setMaximumWidth(newWidth);
560 move(position.x() - newWidth + oldWidth, position.y());
561 resize(newWidth, height());
567#if QT_CONFIG(datetimeedit)
568void QItemEditorDateTimeEdit::setDateTimeItemEditor(
const QDateTime &datetime)
570 setTimeZone(datetime.timeRepresentation());
571 setDateTime(datetime);
575#if QT_CONFIG(combobox)
577QBooleanComboBox::QBooleanComboBox(QWidget *parent)
580 addItem(QComboBox::tr(
"False"));
581 addItem(QComboBox::tr(
"True"));
584void QBooleanComboBox::setValue(
bool value)
586 setCurrentIndex(value ? 1 : 0);
589bool QBooleanComboBox::value()
const
591 return (currentIndex() == 1);
598#if QT_CONFIG(lineedit) || QT_CONFIG(combobox)
599#include "qitemeditorfactory.moc"
602#include "moc_qitemeditorfactory_p.cpp"
QWidget * createEditor(int userType, QWidget *parent) const override
Creates an editor widget with the given parent for the specified userType of data,...
QDefaultItemEditorFactory()
QByteArray valuePropertyName(int) const override
Returns the property name used to access data for the given userType of data.
static QItemEditorFactory * q_default_factory
~QDefaultFactoryCleaner()