Qt 4.8
qitemdelegate.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 "qitemdelegate.h"
43 
44 #ifndef QT_NO_ITEMVIEWS
45 #include <qabstractitemmodel.h>
46 #include <qapplication.h>
47 #include <qbrush.h>
48 #include <qlineedit.h>
49 #include <qtextedit.h>
50 #include <qplaintextedit.h>
51 #include <qpainter.h>
52 #include <qpalette.h>
53 #include <qpoint.h>
54 #include <qrect.h>
55 #include <qsize.h>
56 #include <qstyle.h>
57 #include <qdatetime.h>
58 #include <qstyleoption.h>
59 #include <qevent.h>
60 #include <qpixmap.h>
61 #include <qbitmap.h>
62 #include <qpixmapcache.h>
63 #include <qitemeditorfactory.h>
64 #include <qmetaobject.h>
65 #include <qtextlayout.h>
66 #include <private/qobject_p.h>
67 #include <private/qdnd_p.h>
68 #include <private/qtextengine_p.h>
69 #include <qdebug.h>
70 #include <qlocale.h>
71 #include <qdialog.h>
72 #include <qmath.h>
73 
74 #include <limits.h>
75 
76 #ifndef DBL_DIG
77 # define DBL_DIG 10
78 #endif
79 
81 
83 {
85 
86 public:
88 
89  inline const QItemEditorFactory *editorFactory() const
90  { return f ? f : QItemEditorFactory::defaultFactory(); }
91 
92  inline QIcon::Mode iconMode(QStyle::State state) const
93  {
94  if (!(state & QStyle::State_Enabled)) return QIcon::Disabled;
95  if (state & QStyle::State_Selected) return QIcon::Selected;
96  return QIcon::Normal;
97  }
98 
99  inline QIcon::State iconState(QStyle::State state) const
100  { return state & QStyle::State_Open ? QIcon::On : QIcon::Off; }
101 
103  {
104  const QChar nl = QLatin1Char('\n');
105  for (int i = 0; i < text.count(); ++i)
106  if (text.at(i) == nl)
107  text[i] = QChar::LineSeparator;
108  return text;
109  }
110 
111  static QString valueToText(const QVariant &value, const QStyleOptionViewItemV4 &option);
112 
113  void _q_commitDataAndCloseEditor(QWidget *editor);
114 
117 
118  QRect textLayoutBounds(const QStyleOptionViewItemV2 &options) const;
119  QSizeF doTextLayout(int lineWidth) const;
122 
123  const QWidget *widget(const QStyleOptionViewItem &option) const
124  {
125  if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option))
126  return v3->widget;
127 
128  return 0;
129  }
130 
131  // ### temporary hack until we have QStandardItemDelegate
132  mutable struct Icon {
136  } tmp;
137 };
138 
140 {
142  emit q->commitData(editor);
143  emit q->closeEditor(editor, QAbstractItemDelegate::SubmitModelCache);
144 }
145 
147 {
148  QRect rect = option.rect;
149  const bool wrapText = option.features & QStyleOptionViewItemV2::WrapText;
150  switch (option.decorationPosition) {
153  rect.setWidth(wrapText && rect.isValid() ? rect.width() : (QFIXED_MAX));
154  break;
157  rect.setWidth(wrapText ? option.decorationSize.width() : (QFIXED_MAX));
158  break;
159  }
160 
161  return rect;
162 }
163 
165 {
166  qreal height = 0;
167  qreal widthUsed = 0;
169  while (true) {
171  if (!line.isValid())
172  break;
173  line.setLineWidth(lineWidth);
174  line.setPosition(QPointF(0, height));
175  height += line.height();
176  widthUsed = qMax(widthUsed, line.naturalTextWidth());
177  }
179  return QSizeF(widthUsed, height);
180 }
181 
321 {
322 
323 }
324 
330 {
331 }
332 
347 {
348  Q_D(const QItemDelegate);
349  return d->clipPainting;
350 }
351 
353 {
355  d->clipPainting = clip;
356 }
357 
359 {
360  QString text;
361  switch (value.userType()) {
362  case QMetaType::Float:
363  text = option.locale.toString(value.toFloat(), 'g');
364  break;
365  case QVariant::Double:
366  text = option.locale.toString(value.toDouble(), 'g', DBL_DIG);
367  break;
368  case QVariant::Int:
369  case QVariant::LongLong:
370  text = option.locale.toString(value.toLongLong());
371  break;
372  case QVariant::UInt:
373  case QVariant::ULongLong:
374  text = option.locale.toString(value.toULongLong());
375  break;
376  case QVariant::Date:
377  text = option.locale.toString(value.toDate(), QLocale::ShortFormat);
378  break;
379  case QVariant::Time:
380  text = option.locale.toString(value.toTime(), QLocale::ShortFormat);
381  break;
382  case QVariant::DateTime:
383  text = option.locale.toString(value.toDateTime().date(), QLocale::ShortFormat);
384  text += QLatin1Char(' ');
385  text += option.locale.toString(value.toDateTime().time(), QLocale::ShortFormat);
386  break;
387  default:
388  text = replaceNewLine(value.toString());
389  break;
390  }
391  return text;
392 }
393 
417  const QStyleOptionViewItem &option,
418  const QModelIndex &index) const
419 {
420  Q_D(const QItemDelegate);
421  Q_ASSERT(index.isValid());
422 
423  QStyleOptionViewItemV4 opt = setOptions(index, option);
424 
426  opt.features = v2 ? v2->features
427  : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None);
429  opt.locale = v3 ? v3->locale : QLocale();
430  opt.widget = v3 ? v3->widget : 0;
431 
432  // prepare
433  painter->save();
434  if (d->clipPainting)
435  painter->setClipRect(opt.rect);
436 
437  // get the data and the rectangles
438 
439  QVariant value;
440 
441  QPixmap pixmap;
442  QRect decorationRect;
443  value = index.data(Qt::DecorationRole);
444  if (value.isValid()) {
445  // ### we need the pixmap to call the virtual function
446  pixmap = decoration(opt, value);
447  if (value.type() == QVariant::Icon) {
448  d->tmp.icon = qvariant_cast<QIcon>(value);
449  d->tmp.mode = d->iconMode(option.state);
450  d->tmp.state = d->iconState(option.state);
451  const QSize size = d->tmp.icon.actualSize(option.decorationSize,
452  d->tmp.mode, d->tmp.state);
453  decorationRect = QRect(QPoint(0, 0), size);
454  } else {
455  d->tmp.icon = QIcon();
456  decorationRect = QRect(QPoint(0, 0), pixmap.size());
457  }
458  } else {
459  d->tmp.icon = QIcon();
460  decorationRect = QRect();
461  }
462 
463  QString text;
464  QRect displayRect;
465  value = index.data(Qt::DisplayRole);
466  if (value.isValid() && !value.isNull()) {
467  text = QItemDelegatePrivate::valueToText(value, opt);
468  displayRect = textRectangle(painter, d->textLayoutBounds(opt), opt.font, text);
469  }
470 
471  QRect checkRect;
472  Qt::CheckState checkState = Qt::Unchecked;
473  value = index.data(Qt::CheckStateRole);
474  if (value.isValid()) {
475  checkState = static_cast<Qt::CheckState>(value.toInt());
476  checkRect = check(opt, opt.rect, value);
477  }
478 
479  // do the layout
480 
481  doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
482 
483  // draw the item
484 
485  drawBackground(painter, opt, index);
486  drawCheck(painter, opt, checkRect, checkState);
487  drawDecoration(painter, opt, decorationRect, pixmap);
488  drawDisplay(painter, opt, displayRect, text);
489  drawFocus(painter, opt, displayRect);
490 
491  // done
492  painter->restore();
493 }
494 
506  const QModelIndex &index) const
507 {
508  QVariant value = index.data(Qt::SizeHintRole);
509  if (value.isValid())
510  return qvariant_cast<QSize>(value);
511  QRect decorationRect = rect(option, index, Qt::DecorationRole);
512  QRect displayRect = rect(option, index, Qt::DisplayRole);
513  QRect checkRect = rect(option, index, Qt::CheckStateRole);
514 
515  doLayout(option, &checkRect, &decorationRect, &displayRect, true);
516 
517  return (decorationRect|displayRect|checkRect).size();
518 }
519 
529  const QStyleOptionViewItem &,
530  const QModelIndex &index) const
531 {
532  Q_D(const QItemDelegate);
533  if (!index.isValid())
534  return 0;
535  QVariant::Type t = static_cast<QVariant::Type>(index.data(Qt::EditRole).userType());
536  const QItemEditorFactory *factory = d->f;
537  if (factory == 0)
539  return factory->createEditor(t, parent);
540 }
541 
553 {
554 #ifdef QT_NO_PROPERTIES
555  Q_UNUSED(editor);
556  Q_UNUSED(index);
557 #else
558  Q_D(const QItemDelegate);
559  QVariant v = index.data(Qt::EditRole);
560  QByteArray n = editor->metaObject()->userProperty().name();
561 
562  // ### Qt 5: remove
563  // A work-around for missing "USER true" in qdatetimeedit.h for
564  // QTimeEdit's time property and QDateEdit's date property.
565  // It only triggers if the default user property "dateTime" is
566  // reported for QTimeEdit and QDateEdit.
567  if (n == "dateTime") {
568  if (editor->inherits("QTimeEdit"))
569  n = "time";
570  else if (editor->inherits("QDateEdit"))
571  n = "date";
572  }
573 
574  // ### Qt 5: give QComboBox a USER property
575  if (n.isEmpty() && editor->inherits("QComboBox"))
576  n = d->editorFactory()->valuePropertyName(static_cast<QVariant::Type>(v.userType()));
577  if (!n.isEmpty()) {
578  if (!v.isValid())
579  v = QVariant(editor->property(n).userType(), (const void *)0);
580  editor->setProperty(n, v);
581  }
582 #endif
583 }
584 
596  QAbstractItemModel *model,
597  const QModelIndex &index) const
598 {
599 #ifdef QT_NO_PROPERTIES
600  Q_UNUSED(model);
601  Q_UNUSED(editor);
602  Q_UNUSED(index);
603 #else
604  Q_D(const QItemDelegate);
605  Q_ASSERT(model);
606  Q_ASSERT(editor);
607  QByteArray n = editor->metaObject()->userProperty().name();
608  if (n.isEmpty())
609  n = d->editorFactory()->valuePropertyName(
610  static_cast<QVariant::Type>(model->data(index, Qt::EditRole).userType()));
611  if (!n.isEmpty())
612  model->setData(index, editor->property(n), Qt::EditRole);
613 #endif
614 }
615 
622  const QStyleOptionViewItem &option,
623  const QModelIndex &index) const
624 {
625  if (!editor)
626  return;
627  Q_ASSERT(index.isValid());
628  QPixmap pixmap = decoration(option, index.data(Qt::DecorationRole));
630  QRect pixmapRect = QRect(QPoint(0, 0), option.decorationSize).intersected(pixmap.rect());
631  QRect textRect = textRectangle(0, option.rect, option.font, text);
632  QRect checkRect = check(option, textRect, index.data(Qt::CheckStateRole));
633  QStyleOptionViewItem opt = option;
634  opt.showDecorationSelected = true; // let the editor take up all available space
635  doLayout(opt, &checkRect, &pixmapRect, &textRect, false);
636  editor->setGeometry(textRect);
637 }
638 
646 {
647  Q_D(const QItemDelegate);
648  return d->f;
649 }
650 
659 {
661  d->f = factory;
662 }
663 
670  const QRect &rect, const QString &text) const
671 {
672  Q_D(const QItemDelegate);
673 
676  if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
677  cg = QPalette::Inactive;
678  if (option.state & QStyle::State_Selected) {
679  painter->fillRect(rect, option.palette.brush(cg, QPalette::Highlight));
680  painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
681  } else {
682  painter->setPen(option.palette.color(cg, QPalette::Text));
683  }
684 
685  if (text.isEmpty())
686  return;
687 
688  if (option.state & QStyle::State_Editing) {
689  painter->save();
690  painter->setPen(option.palette.color(cg, QPalette::Text));
691  painter->drawRect(rect.adjusted(0, 0, -1, -1));
692  painter->restore();
693  }
694 
695  const QStyleOptionViewItemV4 opt = option;
696 
697  const QWidget *widget = d->widget(option);
698  QStyle *style = widget ? widget->style() : QApplication::style();
699  const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1;
700  QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding
701  const bool wrapText = opt.features & QStyleOptionViewItemV2::WrapText;
702  d->textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap);
703  d->textOption.setTextDirection(option.direction);
704  d->textOption.setAlignment(QStyle::visualAlignment(option.direction, option.displayAlignment));
705  d->textLayout.setTextOption(d->textOption);
706  d->textLayout.setFont(option.font);
707  d->textLayout.setText(QItemDelegatePrivate::replaceNewLine(text));
708 
709  QSizeF textLayoutSize = d->doTextLayout(textRect.width());
710 
711  if (textRect.width() < textLayoutSize.width()
712  || textRect.height() < textLayoutSize.height()) {
713  QString elided;
714  int start = 0;
715  int end = text.indexOf(QChar::LineSeparator, start);
716  if (end == -1) {
717  elided += option.fontMetrics.elidedText(text, option.textElideMode, textRect.width());
718  } else {
719  while (end != -1) {
720  elided += option.fontMetrics.elidedText(text.mid(start, end - start),
721  option.textElideMode, textRect.width());
722  elided += QChar::LineSeparator;
723  start = end + 1;
724  end = text.indexOf(QChar::LineSeparator, start);
725  }
726  //let's add the last line (after the last QChar::LineSeparator)
727  elided += option.fontMetrics.elidedText(text.mid(start),
728  option.textElideMode, textRect.width());
729  }
730  d->textLayout.setText(elided);
731  textLayoutSize = d->doTextLayout(textRect.width());
732  }
733 
734  const QSize layoutSize(textRect.width(), int(textLayoutSize.height()));
735  const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment,
736  layoutSize, textRect);
737  // if we still overflow even after eliding the text, enable clipping
738  if (!hasClipping() && (textRect.width() < textLayoutSize.width()
739  || textRect.height() < textLayoutSize.height())) {
740  painter->save();
741  painter->setClipRect(layoutRect);
742  d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect);
743  painter->restore();
744  } else {
745  d->textLayout.draw(painter, layoutRect.topLeft(), QVector<QTextLayout::FormatRange>(), layoutRect);
746  }
747 }
748 
754  const QRect &rect, const QPixmap &pixmap) const
755 {
756  Q_D(const QItemDelegate);
757  // if we have an icon, we ignore the pixmap
758  if (!d->tmp.icon.isNull()) {
759  d->tmp.icon.paint(painter, rect, option.decorationAlignment,
760  d->tmp.mode, d->tmp.state);
761  return;
762  }
763 
764  if (pixmap.isNull() || !rect.isValid())
765  return;
767  pixmap.size(), rect).topLeft();
768  if (option.state & QStyle::State_Selected) {
769  QPixmap *pm = selected(pixmap, option.palette, option.state & QStyle::State_Enabled);
770  painter->drawPixmap(p, *pm);
771  } else {
772  painter->drawPixmap(p, pixmap);
773  }
774 }
775 
782  const QStyleOptionViewItem &option,
783  const QRect &rect) const
784 {
785  Q_D(const QItemDelegate);
786  if ((option.state & QStyle::State_HasFocus) == 0 || !rect.isValid())
787  return;
789  o.QStyleOption::operator=(option);
790  o.rect = rect;
795  o.backgroundColor = option.palette.color(cg, (option.state & QStyle::State_Selected)
797  const QWidget *widget = d->widget(option);
798  QStyle *style = widget ? widget->style() : QApplication::style();
799  style->drawPrimitive(QStyle::PE_FrameFocusRect, &o, painter, widget);
800 }
801 
809  const QStyleOptionViewItem &option,
810  const QRect &rect, Qt::CheckState state) const
811 {
812  Q_D(const QItemDelegate);
813  if (!rect.isValid())
814  return;
815 
816  QStyleOptionViewItem opt(option);
817  opt.rect = rect;
818  opt.state = opt.state & ~QStyle::State_HasFocus;
819 
820  switch (state) {
821  case Qt::Unchecked:
822  opt.state |= QStyle::State_Off;
823  break;
826  break;
827  case Qt::Checked:
828  opt.state |= QStyle::State_On;
829  break;
830  }
831 
832  const QWidget *widget = d->widget(option);
833  QStyle *style = widget ? widget->style() : QApplication::style();
834  style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);
835 }
836 
848  const QStyleOptionViewItem &option,
849  const QModelIndex &index) const
850 {
851  if (option.showDecorationSelected && (option.state & QStyle::State_Selected)) {
854  if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
855  cg = QPalette::Inactive;
856 
857  painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
858  } else {
859  QVariant value = index.data(Qt::BackgroundRole);
860  if (value.canConvert<QBrush>()) {
861  QPointF oldBO = painter->brushOrigin();
862  painter->setBrushOrigin(option.rect.topLeft());
863  painter->fillRect(option.rect, qvariant_cast<QBrush>(value));
864  painter->setBrushOrigin(oldBO);
865  }
866  }
867 }
868 
869 
877  QRect *checkRect, QRect *pixmapRect, QRect *textRect,
878  bool hint) const
879 {
880  Q_ASSERT(checkRect && pixmapRect && textRect);
881  Q_D(const QItemDelegate);
882  const QWidget *widget = d->widget(option);
883  QStyle *style = widget ? widget->style() : QApplication::style();
884  const bool hasCheck = checkRect->isValid();
885  const bool hasPixmap = pixmapRect->isValid();
886  const bool hasText = textRect->isValid();
887  const int textMargin = hasText ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
888  const int pixmapMargin = hasPixmap ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
889  const int checkMargin = hasCheck ? style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget) + 1 : 0;
890  int x = option.rect.left();
891  int y = option.rect.top();
892  int w, h;
893 
894  textRect->adjust(-textMargin, 0, textMargin, 0); // add width padding
895  if (textRect->height() == 0 && (!hasPixmap || !hint)) {
896  //if there is no text, we still want to have a decent height for the item sizeHint and the editor size
897  textRect->setHeight(option.fontMetrics.height());
898  }
899 
900  QSize pm(0, 0);
901  if (hasPixmap) {
902  pm = pixmapRect->size();
903  pm.rwidth() += 2 * pixmapMargin;
904  }
905  if (hint) {
906  h = qMax(checkRect->height(), qMax(textRect->height(), pm.height()));
909  w = textRect->width() + pm.width();
910  } else {
911  w = qMax(textRect->width(), pm.width());
912  }
913  } else {
914  w = option.rect.width();
915  h = option.rect.height();
916  }
917 
918  int cw = 0;
919  QRect check;
920  if (hasCheck) {
921  cw = checkRect->width() + 2 * checkMargin;
922  if (hint) w += cw;
923  if (option.direction == Qt::RightToLeft) {
924  check.setRect(x + w - cw, y, cw, h);
925  } else {
926  check.setRect(x + checkMargin, y, cw, h);
927  }
928  }
929 
930  // at this point w should be the *total* width
931 
932  QRect display;
934  switch (option.decorationPosition) {
936  if (hasPixmap)
937  pm.setHeight(pm.height() + pixmapMargin); // add space
938  h = hint ? textRect->height() : h - pm.height();
939 
940  if (option.direction == Qt::RightToLeft) {
941  decoration.setRect(x, y, w - cw, pm.height());
942  display.setRect(x, y + pm.height(), w - cw, h);
943  } else {
944  decoration.setRect(x + cw, y, w - cw, pm.height());
945  display.setRect(x + cw, y + pm.height(), w - cw, h);
946  }
947  break; }
949  if (hasText)
950  textRect->setHeight(textRect->height() + textMargin); // add space
951  h = hint ? textRect->height() + pm.height() : h;
952 
953  if (option.direction == Qt::RightToLeft) {
954  display.setRect(x, y, w - cw, textRect->height());
955  decoration.setRect(x, y + textRect->height(), w - cw, h - textRect->height());
956  } else {
957  display.setRect(x + cw, y, w - cw, textRect->height());
958  decoration.setRect(x + cw, y + textRect->height(), w - cw, h - textRect->height());
959  }
960  break; }
962  if (option.direction == Qt::LeftToRight) {
963  decoration.setRect(x + cw, y, pm.width(), h);
964  display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h);
965  } else {
966  display.setRect(x, y, w - pm.width() - cw, h);
967  decoration.setRect(display.right() + 1, y, pm.width(), h);
968  }
969  break; }
971  if (option.direction == Qt::LeftToRight) {
972  display.setRect(x + cw, y, w - pm.width() - cw, h);
973  decoration.setRect(display.right() + 1, y, pm.width(), h);
974  } else {
975  decoration.setRect(x, y, pm.width(), h);
976  display.setRect(decoration.right() + 1, y, w - pm.width() - cw, h);
977  }
978  break; }
979  default:
980  qWarning("doLayout: decoration position is invalid");
981  decoration = *pixmapRect;
982  break;
983  }
984 
985  if (!hint) { // we only need to do the internal layout if we are going to paint
986  *checkRect = QStyle::alignedRect(option.direction, Qt::AlignCenter,
987  checkRect->size(), check);
988  *pixmapRect = QStyle::alignedRect(option.direction, option.decorationAlignment,
989  pixmapRect->size(), decoration);
990  // the text takes up all available space, unless the decoration is not shown as selected
991  if (option.showDecorationSelected)
992  *textRect = display;
993  else
994  *textRect = QStyle::alignedRect(option.direction, option.displayAlignment,
995  textRect->size().boundedTo(display.size()), display);
996  } else {
997  *checkRect = check;
998  *pixmapRect = decoration;
999  *textRect = display;
1000  }
1001 }
1002 
1015 {
1016  Q_D(const QItemDelegate);
1017  switch (variant.type()) {
1018  case QVariant::Icon: {
1019  QIcon::Mode mode = d->iconMode(option.state);
1020  QIcon::State state = d->iconState(option.state);
1021  return qvariant_cast<QIcon>(variant).pixmap(option.decorationSize, mode, state); }
1022  case QVariant::Color: {
1023  static QPixmap pixmap(option.decorationSize);
1024  pixmap.fill(qvariant_cast<QColor>(variant));
1025  return pixmap; }
1026  default:
1027  break;
1028  }
1029 
1030  return qvariant_cast<QPixmap>(variant);
1031 }
1032 
1033 // hacky but faster version of "QString::sprintf("%d-%d", i, enabled)"
1035 {
1036  ushort arr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', ushort('0' + enabled) };
1037  ushort *ptr = &arr[16];
1038 
1039  while (i > 0) {
1040  // hey - it's our internal representation, so use the ascii character after '9'
1041  // instead of 'a' for hex
1042  *(--ptr) = '0' + i % 16;
1043  i >>= 4;
1044  }
1045 
1046  return QString((const QChar *)ptr, int(&arr[sizeof(arr) / sizeof(ushort)] - ptr));
1047 }
1048 
1058 QPixmap *QItemDelegate::selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const
1059 {
1060  QString key = qPixmapSerial(qt_pixmap_id(pixmap), enabled);
1061  QPixmap *pm = QPixmapCache::find(key);
1062  if (!pm) {
1064 
1065  QColor color = palette.color(enabled ? QPalette::Normal : QPalette::Disabled,
1067  color.setAlphaF((qreal)0.3);
1068 
1069  QPainter painter(&img);
1071  painter.fillRect(0, 0, img.width(), img.height(), color);
1072  painter.end();
1073 
1075  int n = (img.byteCount() >> 10) + 1;
1076  if (QPixmapCache::cacheLimit() < n)
1078 
1079  QPixmapCache::insert(key, selected);
1080  pm = QPixmapCache::find(key);
1081  }
1082  return pm;
1083 }
1084 
1090  const QModelIndex &index, int role) const
1091 {
1092  Q_D(const QItemDelegate);
1093  QVariant value = index.data(role);
1094  if (role == Qt::CheckStateRole)
1095  return check(option, option.rect, value);
1096  if (value.isValid() && !value.isNull()) {
1097  switch (value.type()) {
1098  case QVariant::Invalid:
1099  break;
1100  case QVariant::Pixmap:
1101  return QRect(QPoint(0, 0), qvariant_cast<QPixmap>(value).size());
1102  case QVariant::Image:
1103  return QRect(QPoint(0, 0), qvariant_cast<QImage>(value).size());
1104  case QVariant::Icon: {
1105  QIcon::Mode mode = d->iconMode(option.state);
1106  QIcon::State state = d->iconState(option.state);
1107  QIcon icon = qvariant_cast<QIcon>(value);
1108  QSize size = icon.actualSize(option.decorationSize, mode, state);
1109  return QRect(QPoint(0, 0), size); }
1110  case QVariant::Color:
1111  return QRect(QPoint(0, 0), option.decorationSize);
1112  case QVariant::String:
1113  default: {
1115  value = index.data(Qt::FontRole);
1116  QFont fnt = qvariant_cast<QFont>(value).resolve(option.font);
1117  return textRectangle(0, d->textLayoutBounds(option), fnt, text); }
1118  }
1119  }
1120  return QRect();
1121 }
1122 
1142  const QRect &bounding, const QVariant &value) const
1143 {
1144  if (value.isValid()) {
1145  Q_D(const QItemDelegate);
1146  QStyleOptionButton opt;
1147  opt.QStyleOption::operator=(option);
1148  opt.rect = bounding;
1149  const QWidget *widget = d->widget(option); // cast
1150  QStyle *style = widget ? widget->style() : QApplication::style();
1151  return style->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt, widget);
1152  }
1153  return QRect();
1154 }
1155 
1160  const QFont &font, const QString &text) const
1161 {
1162  Q_D(const QItemDelegate);
1163  d->textOption.setWrapMode(QTextOption::WordWrap);
1164  d->textLayout.setTextOption(d->textOption);
1165  d->textLayout.setFont(font);
1166  d->textLayout.setText(QItemDelegatePrivate::replaceNewLine(text));
1167  QSizeF fpSize = d->doTextLayout(rect.width());
1168  const QSize size = QSize(qCeil(fpSize.width()), qCeil(fpSize.height()));
1169  // ###: textRectangle should take style option as argument
1170  const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
1171  return QRect(0, 0, size.width() + 2 * textMargin, size.height());
1172 }
1173 
1203 {
1204  QWidget *editor = qobject_cast<QWidget*>(object);
1205  if (!editor)
1206  return false;
1207  if (event->type() == QEvent::KeyPress) {
1208  switch (static_cast<QKeyEvent *>(event)->key()) {
1209  case Qt::Key_Tab:
1210  emit commitData(editor);
1212  return true;
1213  case Qt::Key_Backtab:
1214  emit commitData(editor);
1216  return true;
1217  case Qt::Key_Enter:
1218  case Qt::Key_Return:
1219 #ifndef QT_NO_TEXTEDIT
1220  if (qobject_cast<QTextEdit *>(editor) || qobject_cast<QPlainTextEdit *>(editor))
1221  return false; // don't filter enter key events for QTextEdit
1222  // We want the editor to be able to process the key press
1223  // before committing the data (e.g. so it can do
1224  // validation/fixup of the input).
1225 #endif // QT_NO_TEXTEDIT
1226 #ifndef QT_NO_LINEEDIT
1227  if (QLineEdit *e = qobject_cast<QLineEdit*>(editor))
1228  if (!e->hasAcceptableInput())
1229  return false;
1230 #endif // QT_NO_LINEEDIT
1231  QMetaObject::invokeMethod(this, "_q_commitDataAndCloseEditor",
1232  Qt::QueuedConnection, Q_ARG(QWidget*, editor));
1233  return false;
1234  case Qt::Key_Escape:
1235  // don't commit data
1237  break;
1238  default:
1239  return false;
1240  }
1241  if (editor->parentWidget())
1242  editor->parentWidget()->setFocus();
1243  return true;
1244  } else if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow())) {
1245  //the Hide event will take care of he editors that are in fact complete dialogs
1246  if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) {
1248  while (w) { // don't worry about focus changes internally in the editor
1249  if (w == editor)
1250  return false;
1251  w = w->parentWidget();
1252  }
1253 #ifndef QT_NO_DRAGANDDROP
1254  // The window may lose focus during an drag operation.
1255  // i.e when dragging involves the taskbar on Windows.
1256  if (QDragManager::self() && QDragManager::self()->object != 0)
1257  return false;
1258 #endif
1259 
1260  emit commitData(editor);
1261  emit closeEditor(editor, NoHint);
1262  }
1263  } else if (event->type() == QEvent::ShortcutOverride) {
1264  if (static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape) {
1265  event->accept();
1266  return true;
1267  }
1268  }
1269  return false;
1270 }
1271 
1277  QAbstractItemModel *model,
1278  const QStyleOptionViewItem &option,
1279  const QModelIndex &index)
1280 {
1281  Q_ASSERT(event);
1282  Q_ASSERT(model);
1283 
1284  // make sure that the item is checkable
1285  Qt::ItemFlags flags = model->flags(index);
1286  if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
1287  || !(flags & Qt::ItemIsEnabled))
1288  return false;
1289 
1290  // make sure that we have a check state
1291  QVariant value = index.data(Qt::CheckStateRole);
1292  if (!value.isValid())
1293  return false;
1294 
1295  // make sure that we have the right event type
1296  if ((event->type() == QEvent::MouseButtonRelease)
1297  || (event->type() == QEvent::MouseButtonDblClick)
1298  || (event->type() == QEvent::MouseButtonPress)) {
1299  QRect checkRect = check(option, option.rect, Qt::Checked);
1300  QRect emptyRect;
1301  doLayout(option, &checkRect, &emptyRect, &emptyRect, false);
1302  QMouseEvent *me = static_cast<QMouseEvent*>(event);
1303  if (me->button() != Qt::LeftButton || !checkRect.contains(me->pos()))
1304  return false;
1305 
1306  // eat the double click events inside the check rect
1307  if ((event->type() == QEvent::MouseButtonPress)
1308  || (event->type() == QEvent::MouseButtonDblClick))
1309  return true;
1310 
1311  } else if (event->type() == QEvent::KeyPress) {
1312  if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space
1313  && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select)
1314  return false;
1315  } else {
1316  return false;
1317  }
1318 
1319  Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
1321  return model->setData(index, state, Qt::CheckStateRole);
1322 }
1323 
1329  const QStyleOptionViewItem &option) const
1330 {
1331  QStyleOptionViewItem opt = option;
1332 
1333  // set font
1334  QVariant value = index.data(Qt::FontRole);
1335  if (value.isValid()){
1336  opt.font = qvariant_cast<QFont>(value).resolve(opt.font);
1337  opt.fontMetrics = QFontMetrics(opt.font);
1338  }
1339 
1340  // set text alignment
1341  value = index.data(Qt::TextAlignmentRole);
1342  if (value.isValid())
1343  opt.displayAlignment = Qt::Alignment(value.toInt());
1344 
1345  // set foreground brush
1346  value = index.data(Qt::ForegroundRole);
1347  if (value.canConvert<QBrush>())
1348  opt.palette.setBrush(QPalette::Text, qvariant_cast<QBrush>(value));
1349 
1350  return opt;
1351 }
1352 
1354 
1355 #include "moc_qitemdelegate.cpp"
1356 
1357 #endif // QT_NO_ITEMVIEWS
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
static int cacheLimit()
Returns the cache limit (in kilobytes).
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
static QRect alignedRect(Qt::LayoutDirection direction, Qt::Alignment alignment, const QSize &size, const QRect &rectangle)
Returns a new rectangle of the specified size that is aligned to the given rectangle according to the...
Definition: qstyle.cpp:2120
const QWidget * widget(const QStyleOptionViewItem &option) const
QImage toImage() const
Converts the pixmap to a QImage.
Definition: qpixmap.cpp:542
CheckState
Definition: qnamespace.h:1607
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
The QAbstractItemDelegate class is used to display and edit data items from a model.
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition: qpixmap.cpp:2197
The QKeyEvent class describes a key event.
Definition: qevent.h:224
QRect adjusted(int x1, int y1, int x2, int y2) const
Returns a new rectangle with dx1, dy1, dx2 and dy2 added respectively to the existing coordinates of ...
Definition: qrect.h:431
void setHeight(int h)
Sets the height of the rectangle to the given height.
Definition: qrect.h:445
QFont font
the font used for the item
Definition: qstyleoption.h:552
double qreal
Definition: qglobal.h:1193
Qt::Alignment decorationAlignment
the alignment of the decoration for the item
Definition: qstyleoption.h:548
The QFontMetrics class provides font metrics information.
Definition: qfontmetrics.h:65
Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap)
Definition: qpixmap.cpp:94
QRect textRectangle(QPainter *painter, const QRect &rect, const QFont &font, const QString &text) const
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
void setPosition(const QPointF &pos)
Moves the line to position pos.
#define QFIXED_MAX
Definition: qfixed_p.h:158
QPointer< QWidget > widget
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
void setItemEditorFactory(QItemEditorFactory *factory)
Sets the editor factory to be used by the item delegate to be the factory specified.
bool isActiveWindow
whether this widget&#39;s window is the active window
Definition: qwidget.h:186
QVariant data(int role=Qt::DisplayRole) const
Returns the data for the given role for the item referred to by the index.
int qCeil(qreal v)
Definition: qmath.h:63
QSizeF doTextLayout(int lineWidth) const
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
QStyle::State state
the style flags that are used when drawing the control
Definition: qstyleoption.h:88
bool isWindow() const
Returns true if the widget is an independent window, otherwise returns false.
Definition: qwidget.h:945
virtual void drawCheck(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, Qt::CheckState state) const
Renders a check indicator within the rectangle specified by rect, using the given painter and style o...
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option=0, const QWidget *widget=0) const =0
Returns the value of the given pixel metric.
qreal width() const
Returns the width.
Definition: qsize.h:284
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation...
Definition: qpainter.cpp:2801
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
qreal height() const
Returns the height.
Definition: qsize.h:287
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Gets data from the editor widget and stores it in the specified model at the item index...
The QTextLine class represents a line of text inside a QTextLayout.
Definition: qtextlayout.h:197
void restore()
Restores the current painter state (pops a saved state off the stack).
Definition: qpainter.cpp:1620
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
The QStyleOptionViewItemV4 class is used to describe the parameters necessary for drawing a frame in ...
Definition: qstyleoption.h:609
Position decorationPosition
the position of the decoration for the item
Definition: qstyleoption.h:550
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object&#39;s name property to value.
Definition: qobject.cpp:3755
State
This enum describes the state for which a pixmap is intended to be used.
Definition: qicon.h:64
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
QDateTime toDateTime() const
Returns the variant as a QDateTime if the variant has type() DateTime , Date , or String ; otherwise ...
Definition: qvariant.cpp:2349
static QString qPixmapSerial(quint64 i, bool enabled)
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
int byteCount() const
Returns the number of bytes occupied by the image data.
Definition: qimage.cpp:1800
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
Qt::TextElideMode textElideMode
where ellipsis should be added for text that is too long to fit into an item
Definition: qstyleoption.h:549
#define Q_ARG(type, data)
Definition: qobjectdefs.h:246
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
Reimplemented Function
QRect intersected(const QRect &other) const
Returns the intersection of this rectangle and the given rectangle.
Definition: qrect.h:481
static QStyle * style()
Returns the application&#39;s style object.
void setLineWidth(qreal width)
Lays out the line with the given width.
void setGeometry(int x, int y, int w, int h)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qwidget.h:1017
void doLayout(const QStyleOptionViewItem &option, QRect *checkRect, QRect *iconRect, QRect *textRect, bool hint) const
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
The QString class provides a Unicode character string.
Definition: qstring.h:83
QFontMetrics fontMetrics
the font metrics that should be used when drawing text in the control
Definition: qstyleoption.h:91
void setHeight(int h)
Sets the height to the given height.
Definition: qsize.h:135
T * qobject_cast(QObject *object)
Definition: qobject.h:375
QItemDelegate(QObject *parent=0)
Constructs an item delegate with the given parent.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QVector class is a template class that provides a dynamic array.
Definition: qdatastream.h:64
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
#define Q_D(Class)
Definition: qglobal.h:2482
const QColor & color(ColorGroup cg, ColorRole cr) const
Returns the color in the specified color group, used for the given color role.
Definition: qpalette.h:107
void setBrushOrigin(int x, int y)
Sets the brush&#39;s origin to point (x, y).
Definition: qpainter.h:825
bool isValid() const
Returns true if this text line is valid; otherwise returns false.
Definition: qtextlayout.h:201
QSize boundedTo(const QSize &) const
Returns a size holding the minimum width and height of this size and the given otherSize.
Definition: qsize.h:192
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QString toString(qlonglong i) const
Returns a localized string representation of i.
Definition: qlocale.cpp:1295
void save()
Saves the current painter state (pushes the state onto a stack).
Definition: qpainter.cpp:1590
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
QString elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags=0) const
If the string text is wider than width, returns an elided version of the string (i.
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
static QPixmap * find(const QString &key)
QStyle * style() const
Definition: qwidget.cpp:2742
virtual void drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const
Renders the decoration pixmap within the rectangle specified by rect using the given painter and styl...
#define Q_Q(Class)
Definition: qglobal.h:2483
The QStyleOptionViewItemV2 class is used to describe the parameters necessary for drawing a frame in ...
Definition: qstyleoption.h:562
ColorGroup
Definition: qpalette.h:92
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
QColor backgroundColor
the background color on which the focus rectangle is being drawn
Definition: qstyleoption.h:109
QIcon::State iconState(QStyle::State state) const
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Returns the item flags for the given index.
int width() const
Returns the width.
Definition: qsize.h:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qlonglong toLongLong(bool *ok=0) const
Returns the variant as a long long int if the variant has type() LongLong , Bool , ByteArray , Char , Double , Int , String , UInt , or ULongLong ; otherwise returns 0.
Definition: qvariant.cpp:2659
void setBrush(ColorRole cr, const QBrush &brush)
Sets the brush for the given color role to the specified brush for all groups in the palette...
Definition: qpalette.h:206
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
The QStyleOptionViewItemV3 class is used to describe the parameters necessary for drawing a frame in ...
Definition: qstyleoption.h:590
ViewItemFeatures features
a bitwise OR of the features that describe this view item
Definition: qstyleoption.h:577
QPixmap decoration(const QStyleOptionViewItem &option, const QVariant &variant) const
Returns the pixmap used to decorate the root of the item view.
QPoint brushOrigin() const
Returns the currently set brush origin.
Definition: qpainter.cpp:2168
bool canConvert(Type t) const
Returns true if the variant&#39;s type can be cast to the requested type, t.
Definition: qvariant.cpp:2886
unsigned __int64 quint64
Definition: qglobal.h:943
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
Mode
This enum type describes the mode for which a pixmap is intended to be used.
Definition: qicon.h:63
#define emit
Definition: qobjectdefs.h:76
void adjust(int x1, int y1, int x2, int y2)
Adds dx1, dy1, dx2 and dy2 respectively to the existing coordinates of the rectangle.
Definition: qrect.h:434
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
Updates the editor for the item specified by index according to the style option given.
virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w=0) const =0
Draws the given primitive element with the provided painter using the style options specified by opti...
The QStyleOptionFocusRect class is used to describe the parameters for drawing a focus rectangle with...
Definition: qstyleoption.h:103
qulonglong toULongLong(bool *ok=0) const
Returns the variant as as an unsigned long long int if the variant has type() ULongLong ...
Definition: qvariant.cpp:2675
Q_CORE_EXPORT void qWarning(const char *,...)
bool showDecorationSelected
whether the decoration should be highlighted on selected items
Definition: qstyleoption.h:553
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Returns the widget used to edit the item specified by index for editing.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const =0
Returns the data stored under the given role for the item referred to by the index.
const T * ptr(const T &t)
void setClipping(bool clip)
bool inherits(const char *classname) const
Returns true if this object is an instance of a class that inherits className or a QObject subclass t...
Definition: qobject.h:275
Qt::MouseButton button() const
Returns the button that caused the event.
Definition: qevent.h:101
void commitData(QWidget *editor)
This signal must be emitted when the editor widget has completed editing the data, and wants to write it back into the model.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
void _q_commitDataAndCloseEditor(QWidget *editor)
int count() const
Definition: qstring.h:103
bool contains(const QPoint &p, bool proper=false) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false...
Definition: qrect.cpp:1101
const QBrush & brush(ColorGroup cg, ColorRole cr) const
Returns the brush in the specified color group, used for the given color role.
Definition: qpalette.cpp:874
QStyleOptionViewItem setOptions(const QModelIndex &index, const QStyleOptionViewItem &option) const
The QAbstractItemModel class provides the abstract interface for item model classes.
bool hasClipping() const
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
static QDragManager * self()
Definition: qdnd.cpp:163
void fill(const QColor &fillColor=Qt::white)
Fills the pixmap with the given color.
Definition: qpixmap.cpp:1080
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
QPalette palette
the palette that should be used when painting the control
Definition: qstyleoption.h:92
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
virtual void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
Renders the item view text within the rectangle specified by rect using the given painter and style o...
static QString valueToText(const QVariant &value, const QStyleOptionViewItemV4 &option)
void setFocus()
Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its paren...
Definition: qwidget.h:432
QDate toDate() const
Returns the variant as a QDate if the variant has type() Date , DateTime , or String ; otherwise retu...
Definition: qvariant.cpp:2311
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
The QTextLayout class is used to lay out and render text.
Definition: qtextlayout.h:105
static Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
Transforms an alignment of Qt::AlignLeft or Qt::AlignRight without Qt::AlignAbsolute into Qt::AlignLe...
Definition: qstyle.cpp:2149
void setEditorData(QWidget *editor, const QModelIndex &index) const
Sets the data to be displayed and edited by the editor from the data model item specified by the mode...
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
QIcon::Mode iconMode(QStyle::State state) const
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
virtual QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget=0) const =0
Returns the sub-area for the given element as described in the provided style option.
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const Q_REQUIRED_RESULT
Returns a copy of the image in the given format.
Definition: qimage.cpp:3966
QRect rect(const QStyleOptionViewItem &option, const QModelIndex &index, int role) const
const QItemEditorFactory * editorFactory() const
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
void setCompositionMode(CompositionMode mode)
Sets the composition mode to the given mode.
Definition: qpainter.cpp:2422
void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint=NoHint)
This signal is emitted when the user has finished editing an item using the specified editor...
void setAlphaF(qreal alpha)
Sets the alpha of this color to alpha.
Definition: qcolor.cpp:1117
const QWidget * widget
Definition: qstyleoption.h:596
const char * name() const
Returns this property&#39;s name.
QItemEditorFactory * f
QRect check(const QStyleOptionViewItem &option, const QRect &bounding, const QVariant &variant) const
Note that on Mac, if /usr/include/AssertMacros.
State
Definition: qaudio.h:59
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
unsigned short ushort
Definition: qglobal.h:995
T qstyleoption_cast(const QStyleOption *opt)
Definition: qstyleoption.h:885
QTextLine createLine()
Returns a new text line to be laid out if there is text to be inserted into the layout; otherwise ret...
qreal naturalTextWidth() const
Returns the width of the line that is occupied by text.
QSize actualSize(const QSize &size, Mode mode=Normal, State state=Off) const
Returns the actual size of the icon for the requested size, mode, and state.
Definition: qicon.cpp:730
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
The QItemDelegate class provides display and editing facilities for data items from a model...
Definition: qitemdelegate.h:61
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void setRect(int x, int y, int w, int h)
Sets the coordinates of the rectangle&#39;s top-left corner to ({x}, {y}), and its size to the given widt...
Definition: qrect.h:400
Qt::Alignment displayAlignment
the alignment of the display value for the item
Definition: qstyleoption.h:547
static bool insert(const QString &key, const QPixmap &pixmap)
Inserts a copy of the pixmap pixmap associated with the key into the cache.
int key
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QPixmap * selected(const QPixmap &pixmap, const QPalette &palette, bool enabled) const
Returns the selected version of the given pixmap using the given palette.
The QModelIndex class is used to locate data in a data model.
QSize decorationSize
the size of the decoration for the item
Definition: qstyleoption.h:551
~QItemDelegate()
Destroys the item delegate.
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Returns the size needed by the delegate to display the item specified by index, taking into account t...
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI...
Definition: qstyle.h:68
float toFloat(bool *ok=0) const
Returns the variant as a float if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2725
void setWidth(int w)
Sets the width of the rectangle to the given width.
Definition: qrect.h:442
void setPen(const QColor &color)
Sets the painter&#39;s pen to have style Qt::SolidLine, width 0 and the specified color.
Definition: qpainter.cpp:4047
int height() const
Returns the height.
Definition: qsize.h:129
Qt::LayoutDirection direction
the text layout direction that should be used when drawing text in the control
Definition: qstyleoption.h:89
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
static void setCacheLimit(int)
Sets the cache limit to n kilobytes.
The QTextOption class provides a description of general rich text properties.
Definition: qtextoption.h:59
int height() const
Returns the height of the image.
Definition: qimage.cpp:1572
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
void drawRect(const QRectF &rect)
Draws the current rectangle with the current pen and brush.
Definition: qpainter.h:650
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:66
quint16 index
QVariant property(const char *name) const
Returns the value of the object&#39;s name property.
Definition: qobject.cpp:3807
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
QObject * parent
Definition: qobject.h:92
virtual void drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const
Renders the region within the rectangle specified by rect, indicating that it has the focus...
QMetaProperty userProperty() const
Returns the property that has the USER flag set to true.
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
Definition: qpainter.cpp:5619
bool eventFilter(QObject *object, QEvent *event)
Returns true if the given editor is a valid QWidget and the given event is handled; otherwise returns...
static const QItemEditorFactory * defaultFactory()
Returns the default item editor factory.
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
#define DBL_DIG
QTime time() const
Returns the time part of the datetime.
Definition: qdatetime.cpp:2368
QTime toTime() const
Returns the variant as a QTime if the variant has type() Time , DateTime , or String ; otherwise retu...
Definition: qvariant.cpp:2330
QItemEditorFactory * itemEditorFactory() const
Returns the editor factory used by the item delegate.
static QString replaceNewLine(QString text)
void endLayout()
Ends the layout process.
const char * variant
The QStyleOptionViewItem class is used to describe the parameters used to draw an item in a view widg...
Definition: qstyleoption.h:539
void drawBackground(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Renders the item background for the given index, using the given painter and style option...
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
int height() const
Returns the height of the font.
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
QRect textLayoutBounds(const QStyleOptionViewItemV2 &options) const
static const KeyPair *const end
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
static QWidget * focusWidget()
Returns the application widget that has the keyboard input focus, or 0 if no widget in this applicati...
The QStyleOptionButton class is used to describe the parameters for drawing buttons.
Definition: qstyleoption.h:279
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
struct QItemDelegatePrivate::Icon tmp
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Renders the delegate using the given painter and style option for the item specified by index...
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QRect rect
the area that should be used for various calculations and painting
Definition: qstyleoption.h:90
qreal height() const
Returns the line&#39;s height.
#define enabled
void beginLayout()
Begins the layout process.
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
bool end()
Ends painting.
Definition: qpainter.cpp:1929
int & rwidth()
Returns a reference to the width.
Definition: qsize.h:141
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
Definition: qpainter.cpp:7420
#define text
Definition: qobjectdefs.h:80
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...
QPoint topLeft() const
Returns the position of the rectangle&#39;s top-left corner.
Definition: qrect.h:288
The QPalette class contains color groups for each widget state.
Definition: qpalette.h:61
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60