Qt 4.8
qitemeditorfactory.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <qplatformdefs.h>
43 #include "qitemeditorfactory.h"
44 #include "qitemeditorfactory_p.h"
45 
46 #ifndef QT_NO_ITEMVIEWS
47 
48 #include <qcombobox.h>
49 #include <qdatetimeedit.h>
50 #include <qlabel.h>
51 #include <qlineedit.h>
52 #include <qspinbox.h>
53 #include <limits.h>
54 #include <float.h>
55 #include <qapplication.h>
56 #include <qdebug.h>
57 
59 
60 
61 #ifndef QT_NO_COMBOBOX
62 
64 {
65  Q_OBJECT
66  bool value;
67 
68 public:
70  void setValue(bool);
71  bool value() const;
72 };
73 
74 #endif // QT_NO_COMBOBOX
75 
140 {
141  QItemEditorCreatorBase *creator = creatorMap.value(type, 0);
142  if (!creator) {
143  const QItemEditorFactory *dfactory = defaultFactory();
144  return dfactory == this ? 0 : dfactory->createEditor(type, parent);
145  }
146  return creator->createWidget(parent);
147 }
148 
153 {
154  QItemEditorCreatorBase *creator = creatorMap.value(type, 0);
155  if (!creator) {
156  const QItemEditorFactory *dfactory = defaultFactory();
157  return dfactory == this ? QByteArray() : dfactory->valuePropertyName(type);
158  }
159  return creator->valuePropertyName();
160 }
161 
166 {
167  //we make sure we delete all the QItemEditorCreatorBase
168  //this has to be done only once, hence the QSet
169  QSet<QItemEditorCreatorBase*> set = creatorMap.values().toSet();
170  qDeleteAll(set);
171 }
172 
182 {
184  if (it != creatorMap.end()) {
185  QItemEditorCreatorBase *oldCreator = it.value();
186  Q_ASSERT(oldCreator);
187  creatorMap.erase(it);
188  if (!creatorMap.values().contains(oldCreator))
189  delete oldCreator; // if it is no more in use we can delete it
190  }
191 
192  creatorMap[type] = creator;
193 }
194 
196 {
197 public:
199  QWidget *createEditor(QVariant::Type type, QWidget *parent) const;
200  QByteArray valuePropertyName(QVariant::Type) const;
201 };
202 
204 {
205  switch (type) {
206 #ifndef QT_NO_COMBOBOX
207  case QVariant::Bool: {
208  QBooleanComboBox *cb = new QBooleanComboBox(parent);
209  cb->setFrame(false);
210  return cb; }
211 #endif
212 #ifndef QT_NO_SPINBOX
213  case QVariant::UInt: {
214  QSpinBox *sb = new QSpinBox(parent);
215  sb->setFrame(false);
216  sb->setMaximum(INT_MAX);
217  return sb; }
218  case QVariant::Int: {
219  QSpinBox *sb = new QSpinBox(parent);
220  sb->setFrame(false);
221  sb->setMinimum(INT_MIN);
222  sb->setMaximum(INT_MAX);
223  return sb; }
224 #endif
225 #ifndef QT_NO_DATETIMEEDIT
226  case QVariant::Date: {
227  QDateTimeEdit *ed = new QDateEdit(parent);
228  ed->setFrame(false);
229  return ed; }
230  case QVariant::Time: {
231  QDateTimeEdit *ed = new QTimeEdit(parent);
232  ed->setFrame(false);
233  return ed; }
234  case QVariant::DateTime: {
235  QDateTimeEdit *ed = new QDateTimeEdit(parent);
236  ed->setFrame(false);
237  return ed; }
238 #endif
239  case QVariant::Pixmap:
240  return new QLabel(parent);
241 #ifndef QT_NO_SPINBOX
242  case QVariant::Double: {
243  QDoubleSpinBox *sb = new QDoubleSpinBox(parent);
244  sb->setFrame(false);
245  sb->setMinimum(-DBL_MAX);
246  sb->setMaximum(DBL_MAX);
247  return sb; }
248 #endif
249 #ifndef QT_NO_LINEEDIT
250  case QVariant::String:
251  default: {
252  // the default editor is a lineedit
253  QExpandingLineEdit *le = new QExpandingLineEdit(parent);
256  le->setWidgetOwnsGeometry(true);
257  return le; }
258 #else
259  default:
260  break;
261 #endif
262  }
263  return 0;
264 }
265 
267 {
268  switch (type) {
269 #ifndef QT_NO_COMBOBOX
270  case QVariant::Bool:
271  return "currentIndex";
272 #endif
273 #ifndef QT_NO_SPINBOX
274  case QVariant::UInt:
275  case QVariant::Int:
276  case QVariant::Double:
277  return "value";
278 #endif
279 #ifndef QT_NO_DATETIMEEDIT
280  case QVariant::Date:
281  return "date";
282  case QVariant::Time:
283  return "time";
284  case QVariant::DateTime:
285  return "dateTime";
286 #endif
287  case QVariant::String:
288  default:
289  // the default editor is a lineedit
290  return "text";
291  }
292 }
293 
296 {
298  ~QDefaultFactoryCleaner() { delete q_default_factory; q_default_factory = 0; }
299 };
300 
307 {
308  static const QDefaultItemEditorFactory factory;
309  if (q_default_factory)
310  return q_default_factory;
311  return &factory;
312 }
313 
321 {
322  static const QDefaultFactoryCleaner cleaner;
323  delete q_default_factory;
324  q_default_factory = factory;
325 }
326 
504 #ifndef QT_NO_LINEEDIT
505 
507  : QLineEdit(parent), originalWidth(-1), widgetOwnsGeometry(false)
508 {
511 }
512 
514 {
515  switch (e->type())
516  {
517  case QEvent::FontChange:
518  case QEvent::StyleChange:
521  break;
522  default:
523  break;
524  }
525 
527 }
528 
530 {
531  int left, right;
532  getTextMargins(&left, 0, &right, 0);
533  int width = left + right + 4 /*horizontalMargin in qlineedit.cpp*/;
534  getContentsMargins(&left, 0, &right, 0);
535  width += left + right;
536 
538  initStyleOption(&opt);
539 
540  int minWidth = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(width, 0).
541  expandedTo(QApplication::globalStrut()), this).width();
542  setMinimumWidth(minWidth);
543 }
544 
546 {
547  int oldWidth = width();
548  if (originalWidth == -1)
549  originalWidth = oldWidth;
550  if (QWidget *parent = parentWidget()) {
551  QPoint position = pos();
552  int hintWidth = minimumWidth() + fontMetrics().width(displayText());
553  int parentWidth = parent->width();
554  int maxWidth = isRightToLeft() ? position.x() + oldWidth : parentWidth - position.x();
555  int newWidth = qBound(originalWidth, hintWidth, maxWidth);
556  if (widgetOwnsGeometry)
557  setMaximumWidth(newWidth);
558  if (isRightToLeft())
559  move(position.x() - newWidth + oldWidth, position.y());
560  resize(newWidth, height());
561  }
562 }
563 
564 #endif // QT_NO_LINEEDIT
565 
566 #ifndef QT_NO_COMBOBOX
567 
569  : QComboBox(parent)
570 {
571  addItem(QComboBox::tr("False"));
572  addItem(QComboBox::tr("True"));
573 }
574 
576 {
577  setCurrentIndex(value ? 1 : 0);
578 }
579 
580 bool QBooleanComboBox::value() const
581 {
582  return (currentIndex() == 1);
583 }
584 
585 #endif // QT_NO_COMBOBOX
586 
588 
589 #if !defined(QT_NO_LINEEDIT) || !defined(QT_NO_COMBOBOX)
590 #include "qitemeditorfactory.moc"
591 #endif
592 
593 #endif // QT_NO_ITEMVIEWS
QPoint pos() const
void setWidgetOwnsGeometry(bool value)
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
int width(const QString &, int len=-1) const
Returns the width in pixels of the first len characters of text.
void changeEvent(QEvent *)
Reimplemented Function
Definition: qlineedit.cpp:2299
int type
Definition: qmetatype.cpp:239
The QItemEditorFactory class provides widgets for editing item data in views and delegates.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QItemEditorCreatorBase class provides an abstract base class that must be subclassed when impleme...
QWidget * createEditor(QVariant::Type type, QWidget *parent) const
Creates an editor widget with the given parent for the specified type of data, and returns it as a QW...
#define it(className, varName)
void changeEvent(QEvent *e)
This event handler can be reimplemented to handle state changes.
The QTimeEdit class provides a widget for editing times based on the QDateTimeEdit widget...
static QSize globalStrut()
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QDateEdit class provides a widget for editing dates based on the QDateTimeEdit widget...
#define SLOT(a)
Definition: qobjectdefs.h:226
virtual QByteArray valuePropertyName(QVariant::Type type) const
Returns the property name used to access data for the given type of data.
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt=0, const QWidget *widget=0, QStyleHintReturn *returnData=0) const =0
Returns an integer representing the specified style hint for the given widget described by the provid...
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
void setCurrentIndex(int index)
Definition: qcombobox.cpp:2102
T & value() const
Returns a modifiable reference to the current item&#39;s value.
Definition: qhash.h:348
bool value() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void addItem(const QString &text, const QVariant &userData=QVariant())
Adds an item to the combobox with the given text, and containing the specified userData (stored in th...
Definition: qcombobox.h:323
int height() const
void setFrame(bool)
Definition: qcombobox.cpp:3465
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
QStyle * style() const
Definition: qwidget.cpp:2742
The QDateTimeEdit class provides a widget for editing dates and times.
Definition: qdatetimeedit.h:61
#define SIGNAL(a)
Definition: qobjectdefs.h:227
int width() const
Returns the width.
Definition: qsize.h:126
static QItemEditorFactory * q_default_factory
QString displayText() const
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setMaximumWidth(int maxw)
Definition: qwidget.cpp:4343
void getContentsMargins(int *left, int *top, int *right, int *bottom) const
Returns the widget&#39;s contents margins for left, top, right, and bottom.
Definition: qwidget.cpp:7509
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
int width() const
The QComboBox widget is a combined button and popup list.
Definition: qcombobox.h:62
QFontMetrics fontMetrics() const
Returns the font metrics for the widget&#39;s current font.
Definition: qwidget.h:984
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
QByteArray valuePropertyName(QVariant::Type) const
Returns the property name used to access data for the given type of data.
void setMinimum(double min)
Definition: qspinbox.cpp:830
#define Q_OBJECT
Definition: qobjectdefs.h:157
virtual QWidget * createWidget(QWidget *parent) const =0
Returns an editor widget with the given parent.
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=0) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
virtual QByteArray valuePropertyName() const =0
Returns the name of the property used to get and set values in the creator&#39;s editor widgets...
QBooleanComboBox(QWidget *parent)
QExpandingLineEdit(QWidget *parent)
void resize(int w, int h)
This corresponds to resize(QSize(w, h)).
Definition: qwidget.h:1014
The QDoubleSpinBox class provides a spin box widget that takes doubles.
Definition: qspinbox.h:126
void registerEditor(QVariant::Type type, QItemEditorCreatorBase *creator)
Registers an item editor creator specified by creator for the given type of data. ...
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
int currentIndex() const
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
virtual ~QItemEditorFactory()
Destroys the item editor factory.
Q_DECL_CONSTEXPR const T & qBound(const T &min, const T &val, const T &max)
Definition: qglobal.h:1219
static void setDefaultFactory(QItemEditorFactory *factory)
Sets the default item editor factory to the given factory.
The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
Definition: qhash.h:330
void setMinimumWidth(int minw)
Definition: qwidget.cpp:4325
The QSpinBox class provides a spin box widget.
Definition: qspinbox.h:56
The QLabel widget provides a text or image display.
Definition: qlabel.h:55
void initStyleOption(QStyleOptionFrame *option) const
Initialize option with the values from this QLineEdit.
Definition: qlineedit.cpp:104
The QStyleOptionFrameV2 class is used to describe the parameters necessary for drawing a frame in Qt ...
Definition: qstyleoption.h:134
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:66
void setFrame(bool)
Definition: qlineedit.cpp:514
static const QItemEditorFactory * defaultFactory()
Returns the default item editor factory.
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
bool isRightToLeft() const
Definition: qwidget.h:428
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
QList< T > values() const
Definition: qset.h:232
void setMaximum(double max)
Definition: qspinbox.cpp:864
void setMinimum(int min)
Definition: qspinbox.cpp:425
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
int minimumWidth() const
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
#define INT_MAX
void setMaximum(int max)
Definition: qspinbox.cpp:456
void getTextMargins(int *left, int *top, int *right, int *bottom) const
Returns the widget&#39;s text margins for left, top, right, and bottom.
Definition: qlineedit.cpp:1290
void move(int x, int y)
This corresponds to move(QPoint(x, y)).
Definition: qwidget.h:1011
virtual QWidget * createEditor(QVariant::Type type, QWidget *parent) const
Creates an editor widget with the given parent for the specified type of data, and returns it as a QW...
void textChanged(const QString &)
This signal is emitted whenever the text changes.