📄 qitemdelegate.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qitemdelegate.h"#ifndef QT_NO_ITEMVIEWS#include <qabstractitemmodel.h>#include <qapplication.h>#include <qbrush.h>#include <qlineedit.h>#include <qpainter.h>#include <qpalette.h>#include <qpoint.h>#include <qrect.h>#include <qsize.h>#include <qstyle.h>#include <qdatetime.h>#include <qstyleoption.h>#include <qevent.h>#include <qpixmap.h>#include <qbitmap.h>#include <qpixmapcache.h>#include <qitemeditorfactory.h>#include <qmetaobject.h>#include <qtextlayout.h>#include <private/qobject_p.h>#include <private/qdnd_p.h>#include <private/qtextengine_p.h>#include <qdebug.h>#include <qlocale.h>#include <limits.h>class QItemDelegatePrivate : public QObjectPrivate{ Q_DECLARE_PUBLIC(QItemDelegate)public: QItemDelegatePrivate() : f(0), clipPainting(true) {} inline const QItemEditorFactory *editorFactory() const { return f ? f : QItemEditorFactory::defaultFactory(); } inline QIcon::Mode iconMode(QStyle::State state) const { if (!(state & QStyle::State_Enabled)) return QIcon::Disabled; if (state & QStyle::State_Selected) return QIcon::Selected; return QIcon::Normal; } inline QIcon::State iconState(QStyle::State state) const { return state & QStyle::State_Open ? QIcon::On : QIcon::Off; } inline static QString replaceNewLine(QString text) { const QChar nl = QLatin1Char('\n'); for (int i = 0; i < text.count(); ++i) if (text.at(i) == nl) text[i] = QChar::LineSeparator; return text; } static QString valueToText(const QVariant &value, const QStyleOptionViewItemV3 &option); void _q_commitDataAndCloseEditor(QWidget *editor); QItemEditorFactory *f; bool clipPainting; QRect textLayoutBounds(const QStyleOptionViewItemV2 &options) const; QSizeF doTextLayout(int lineWidth) const; mutable QTextLayout textLayout; mutable QTextOption textOption; const QWidget *widget(const QStyleOptionViewItem &option) const { if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option)) return v3->widget; return 0; } // ### temporary hack until we have QStandardItemDelegate mutable struct Icon { QIcon icon; QIcon::Mode mode; QIcon::State state; } tmp;};void QItemDelegatePrivate::_q_commitDataAndCloseEditor(QWidget *editor){ Q_Q(QItemDelegate); emit q->commitData(editor); emit q->closeEditor(editor, QAbstractItemDelegate::SubmitModelCache);}QRect QItemDelegatePrivate::textLayoutBounds(const QStyleOptionViewItemV2 &option) const{ QRect rect = option.rect; const bool wrapText = option.features & QStyleOptionViewItemV2::WrapText; switch (option.decorationPosition) { case QStyleOptionViewItem::Left: case QStyleOptionViewItem::Right: rect.setWidth(wrapText && rect.isValid() ? rect.width() : (QFIXED_MAX)); break; case QStyleOptionViewItem::Top: case QStyleOptionViewItem::Bottom: rect.setWidth(wrapText ? option.decorationSize.width() : (QFIXED_MAX)); break; } return rect;}QSizeF QItemDelegatePrivate::doTextLayout(int lineWidth) const{ qreal height = 0; qreal widthUsed = 0; textLayout.beginLayout(); while (true) { QTextLine line = textLayout.createLine(); if (!line.isValid()) break; line.setLineWidth(lineWidth); line.setPosition(QPointF(0, height)); height += line.height(); widthUsed = qMax(widthUsed, line.naturalTextWidth()); } textLayout.endLayout(); return QSizeF(widthUsed, height);}/*! \class QItemDelegate \brief The QItemDelegate class provides display and editing facilities for data items from a model. \ingroup model-view \mainclass QItemDelegate can be used to provide custom display features and editor widgets for item views based on QAbstractItemView subclasses. Using a delegate for this purpose allows the display and editing mechanisms to be customized and developed independently from the model and view. The QItemDelegate class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. When displaying items from a custom model in a standard view, it is often sufficient to simply ensure that the model returns appropriate data for each of the \l{Qt::ItemDataRole}{roles} that determine the appearance of items in views. The default delegate used by Qt's standard views uses this role information to display items in most of the common forms expected by users. However, it is sometimes necessary to have even more control over the appearance of items than the default delegate can provide. This class provides default implementations of the functions for painting item data in a view and editing data from item models. Default implementations of the paint() and sizeHint() virtual functions, defined in QAbstractItemDelegate, are provided to ensure that the delegate implements the correct basic behavior expected by views. You can reimplement these functions in subclasses to customize the appearance of items. When editing data in an item view, QItemDelegate provides an editor widget, which is a widget that is placed on top of the view while editing takes place. Editors are created with a QItemEditorFactory; a default static instance provided by QItemEditorFactory is installed on all item delagates. You can set a custom factory using setItemEditorFactory() or set a new default factory with QItemEditorFactory::setDefaultFactory(). It is the data stored in the item model with the Qt::EditRole that is edited. Only the standard editing functions for widget-based delegates are reimplemented here: \list \o createEditor() returns the widget used to change data from the model and can be reimplemented to customize editing behavior. \o setEditorData() provides the widget with data to manipulate. \o updateEditorGeometry() ensures that the editor is displayed correctly with respect to the item view. \o setModelData() returns updated data to the model. \endlist The closeEditor() signal indicates that the user has completed editing the data, and that the editor widget can be destroyed. \section1 Standard Roles and Data Types The default delegate used by the standard views supplied with Qt associates each standard role (defined by Qt::ItemDataRole) with certain data types. Models that return data in these types can influence the appearance of the delegate as described in the following table. \table \header \o Role \o Accepted Types \omit \row \o \l Qt::AccessibleDescriptionRole \o QString \row \o \l Qt::AccessibleTextRole \o QString \endomit \row \o \l Qt::BackgroundRole \o QBrush \row \o \l Qt::BackgroundColorRole \o QColor (obsolete; use Qt::BackgroundRole instead) \row \o \l Qt::CheckStateRole \o Qt::CheckState \row \o \l Qt::DecorationRole \o QIcon and QColor \row \o \l Qt::DisplayRole \o QString and types with a string representation \row \o \l Qt::EditRole \o See QItemEditorFactory for details \row \o \l Qt::FontRole \o QFont \row \o \l Qt::SizeHintRole \o QSize \omit \row \o \l Qt::StatusTipRole \o \endomit \row \o \l Qt::TextAlignmentRole \o Qt::Alignment \row \o \l Qt::ForegroundRole \o QBrush \row \o \l Qt::TextColorRole \o QColor (obsolete; use Qt::ForegroundRole instead) \omit \row \o \l Qt::ToolTipRole \row \o \l Qt::WhatsThisRole \endomit \endtable If the default delegate does not allow the level of customization that you need, either for display purposes or for editing data, it is possible to subclass QItemDelegate to implement the desired behavior. \section1 Subclassing When subclassing QItemDelegate to create a delegate that displays items using a custom renderer, it is important to ensure that the delegate can render items suitably for all the required states; e.g. selected, disabled, checked. The documentation for the paint() function contains some hints to show how this can be achieved. You can provide custom editors by using a QItemEditorFactory. The \l{Color Editor Factory Example} shows how a custom editor can be made available to delegates with the default item editor factory. This way, there is no need to subclass QItemDelegate. An alternative is to reimplement createEditor(), setEditorData(), setModelData(), and updateEditorGeometry(). This process is described in the \l{Spin Box Delegate example}. \sa {Delegate Classes}, QAbstractItemDelegate, {Spin Box Delegate Example}, {Settings Editor Example}, {Icons Example}*//*! Constructs an item delegate with the given \a parent.*/QItemDelegate::QItemDelegate(QObject *parent) : QAbstractItemDelegate(*new QItemDelegatePrivate(), parent){}/*! Destroys the item delegate.*/QItemDelegate::~QItemDelegate(){}/*! \property QItemDelegate::clipping \brief if the delegate should clip the paint events \since 4.2 This property will set the paint clip to the size of the item. The default value is on. It is useful for cases such as when images are larger then the size of the item.*/bool QItemDelegate::hasClipping() const{ Q_D(const QItemDelegate); return d->clipPainting;}void QItemDelegate::setClipping(bool clip){ Q_D(QItemDelegate); d->clipPainting = clip;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -