src/gui/itemviews/qtreewidget.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QTREEWIDGET_H
       
    43 #define QTREEWIDGET_H
       
    44 
       
    45 #include <QtGui/qtreeview.h>
       
    46 #include <QtGui/qtreewidgetitemiterator.h>
       
    47 #include <QtCore/qvariant.h>
       
    48 #include <QtCore/qvector.h>
       
    49 
       
    50 QT_BEGIN_HEADER
       
    51 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 QT_MODULE(Gui)
       
    55 
       
    56 #ifndef QT_NO_TREEWIDGET
       
    57 
       
    58 class QTreeWidget;
       
    59 class QTreeModel;
       
    60 class QWidgetItemData;
       
    61 class QTreeWidgetItemPrivate;
       
    62 
       
    63 class Q_GUI_EXPORT QTreeWidgetItem
       
    64 {
       
    65     friend class QTreeModel;
       
    66     friend class QTreeWidget;
       
    67     friend class QTreeWidgetPrivate;
       
    68     friend class QTreeWidgetItemIterator;
       
    69     friend class QTreeWidgetItemPrivate;
       
    70 public:
       
    71     enum ItemType { Type = 0, UserType = 1000 };
       
    72     explicit QTreeWidgetItem(int type = Type);
       
    73     QTreeWidgetItem(const QStringList &strings, int type = Type);
       
    74     explicit QTreeWidgetItem(QTreeWidget *view, int type = Type);
       
    75     QTreeWidgetItem(QTreeWidget *view, const QStringList &strings, int type = Type);
       
    76     QTreeWidgetItem(QTreeWidget *view, QTreeWidgetItem *after, int type = Type);
       
    77     explicit QTreeWidgetItem(QTreeWidgetItem *parent, int type = Type);
       
    78     QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type = Type);
       
    79     QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type = Type);
       
    80     QTreeWidgetItem(const QTreeWidgetItem &other);
       
    81     virtual ~QTreeWidgetItem();
       
    82 
       
    83     virtual QTreeWidgetItem *clone() const;
       
    84 
       
    85     inline QTreeWidget *treeWidget() const { return view; }
       
    86 
       
    87     inline void setSelected(bool select);
       
    88     inline bool isSelected() const;
       
    89 
       
    90     inline void setHidden(bool hide);
       
    91     inline bool isHidden() const;
       
    92 
       
    93     inline void setExpanded(bool expand);
       
    94     inline bool isExpanded() const;
       
    95 
       
    96     inline void setFirstColumnSpanned(bool span);
       
    97     inline bool isFirstColumnSpanned() const;
       
    98 
       
    99     inline void setDisabled(bool disabled);
       
   100     inline bool isDisabled() const;
       
   101 
       
   102     enum ChildIndicatorPolicy { ShowIndicator, DontShowIndicator, DontShowIndicatorWhenChildless };
       
   103     void setChildIndicatorPolicy(QTreeWidgetItem::ChildIndicatorPolicy policy);
       
   104     QTreeWidgetItem::ChildIndicatorPolicy childIndicatorPolicy() const;
       
   105 
       
   106     Qt::ItemFlags flags() const;
       
   107     void setFlags(Qt::ItemFlags flags);
       
   108 
       
   109     inline QString text(int column) const
       
   110         { return data(column, Qt::DisplayRole).toString(); }
       
   111     inline void setText(int column, const QString &text);
       
   112 
       
   113     inline QIcon icon(int column) const
       
   114         { return qvariant_cast<QIcon>(data(column, Qt::DecorationRole)); }
       
   115     inline void setIcon(int column, const QIcon &icon);
       
   116 
       
   117     inline QString statusTip(int column) const
       
   118         { return data(column, Qt::StatusTipRole).toString(); }
       
   119     inline void setStatusTip(int column, const QString &statusTip);
       
   120 
       
   121 #ifndef QT_NO_TOOLTIP
       
   122     inline QString toolTip(int column) const
       
   123         { return data(column, Qt::ToolTipRole).toString(); }
       
   124     inline void setToolTip(int column, const QString &toolTip);
       
   125 #endif
       
   126 
       
   127 #ifndef QT_NO_WHATSTHIS
       
   128     inline QString whatsThis(int column) const
       
   129         { return data(column, Qt::WhatsThisRole).toString(); }
       
   130     inline void setWhatsThis(int column, const QString &whatsThis);
       
   131 #endif
       
   132 
       
   133     inline QFont font(int column) const
       
   134         { return qvariant_cast<QFont>(data(column, Qt::FontRole)); }
       
   135     inline void setFont(int column, const QFont &font);
       
   136 
       
   137     inline int textAlignment(int column) const
       
   138         { return data(column, Qt::TextAlignmentRole).toInt(); }
       
   139     inline void setTextAlignment(int column, int alignment)
       
   140         { setData(column, Qt::TextAlignmentRole, alignment); }
       
   141 
       
   142     inline QColor backgroundColor(int column) const
       
   143         { return qvariant_cast<QColor>(data(column, Qt::BackgroundColorRole)); }
       
   144     inline void setBackgroundColor(int column, const QColor &color)
       
   145         { setData(column, Qt::BackgroundColorRole, color); }
       
   146 
       
   147     inline QBrush background(int column) const
       
   148         { return qvariant_cast<QBrush>(data(column, Qt::BackgroundRole)); }
       
   149     inline void setBackground(int column, const QBrush &brush)
       
   150         { setData(column, Qt::BackgroundRole, brush); }
       
   151 
       
   152     inline QColor textColor(int column) const
       
   153         { return qvariant_cast<QColor>(data(column, Qt::TextColorRole)); }
       
   154     inline void setTextColor(int column, const QColor &color)
       
   155         { setData(column, Qt::TextColorRole, color); }
       
   156 
       
   157     inline QBrush foreground(int column) const
       
   158         { return qvariant_cast<QBrush>(data(column, Qt::ForegroundRole)); }
       
   159     inline void setForeground(int column, const QBrush &brush)
       
   160         { setData(column, Qt::ForegroundRole, brush); }
       
   161 
       
   162     inline Qt::CheckState checkState(int column) const
       
   163         { return static_cast<Qt::CheckState>(data(column, Qt::CheckStateRole).toInt()); }
       
   164     inline void setCheckState(int column, Qt::CheckState state)
       
   165         { setData(column, Qt::CheckStateRole, state); }
       
   166 
       
   167     inline QSize sizeHint(int column) const
       
   168         { return qvariant_cast<QSize>(data(column, Qt::SizeHintRole)); }
       
   169     inline void setSizeHint(int column, const QSize &size)
       
   170         { setData(column, Qt::SizeHintRole, size); }
       
   171 
       
   172     virtual QVariant data(int column, int role) const;
       
   173     virtual void setData(int column, int role, const QVariant &value);
       
   174 
       
   175     virtual bool operator<(const QTreeWidgetItem &other) const;
       
   176 
       
   177 #ifndef QT_NO_DATASTREAM
       
   178     virtual void read(QDataStream &in);
       
   179     virtual void write(QDataStream &out) const;
       
   180 #endif
       
   181     QTreeWidgetItem &operator=(const QTreeWidgetItem &other);
       
   182 
       
   183     inline QTreeWidgetItem *parent() const { return par; }
       
   184     inline QTreeWidgetItem *child(int index) const {
       
   185         if (index < 0 || index >= children.size())
       
   186             return 0;
       
   187         executePendingSort();
       
   188         return children.at(index);
       
   189     }
       
   190     inline int childCount() const { return children.count(); }
       
   191     inline int columnCount() const { return values.count(); }
       
   192     inline int indexOfChild(QTreeWidgetItem *child) const;
       
   193 
       
   194     void addChild(QTreeWidgetItem *child);
       
   195     void insertChild(int index, QTreeWidgetItem *child);
       
   196     void removeChild(QTreeWidgetItem *child);
       
   197     QTreeWidgetItem *takeChild(int index);
       
   198 
       
   199     void addChildren(const QList<QTreeWidgetItem*> &children);
       
   200     void insertChildren(int index, const QList<QTreeWidgetItem*> &children);
       
   201     QList<QTreeWidgetItem*> takeChildren();
       
   202 
       
   203     inline int type() const { return rtti; }
       
   204     inline void sortChildren(int column, Qt::SortOrder order)
       
   205         { sortChildren(column, order, false); }
       
   206 
       
   207 protected:
       
   208     void emitDataChanged();
       
   209 
       
   210 private:
       
   211     void sortChildren(int column, Qt::SortOrder order, bool climb);
       
   212     QVariant childrenCheckState(int column) const;
       
   213     void itemChanged();
       
   214     void executePendingSort() const;
       
   215 
       
   216     int rtti;
       
   217     // One item has a vector of column entries. Each column has a vector of (role, value) pairs.
       
   218     QVector< QVector<QWidgetItemData> > values;
       
   219     QTreeWidget *view;
       
   220     QTreeWidgetItemPrivate *d;
       
   221     QTreeWidgetItem *par;
       
   222     QList<QTreeWidgetItem*> children;
       
   223     Qt::ItemFlags itemFlags;
       
   224 };
       
   225 
       
   226 inline void QTreeWidgetItem::setText(int column, const QString &atext)
       
   227 { setData(column, Qt::DisplayRole, atext); }
       
   228 
       
   229 inline void QTreeWidgetItem::setIcon(int column, const QIcon &aicon)
       
   230 { setData(column, Qt::DecorationRole, aicon); }
       
   231 
       
   232 #ifndef QT_NO_STATUSTIP
       
   233 inline void QTreeWidgetItem::setStatusTip(int column, const QString &astatusTip)
       
   234 { setData(column, Qt::StatusTipRole, astatusTip); }
       
   235 #endif
       
   236 
       
   237 #ifndef QT_NO_TOOLTIP
       
   238 inline void QTreeWidgetItem::setToolTip(int column, const QString &atoolTip)
       
   239 { setData(column, Qt::ToolTipRole, atoolTip); }
       
   240 #endif
       
   241 
       
   242 #ifndef QT_NO_WHATSTHIS
       
   243 inline void QTreeWidgetItem::setWhatsThis(int column, const QString &awhatsThis)
       
   244 { setData(column, Qt::WhatsThisRole, awhatsThis); }
       
   245 #endif
       
   246 
       
   247 inline void QTreeWidgetItem::setFont(int column, const QFont &afont)
       
   248 { setData(column, Qt::FontRole, afont); }
       
   249 
       
   250 inline int QTreeWidgetItem::indexOfChild(QTreeWidgetItem *achild) const
       
   251 { executePendingSort(); return children.indexOf(achild); }
       
   252 
       
   253 #ifndef QT_NO_DATASTREAM
       
   254 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &out, const QTreeWidgetItem &item);
       
   255 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &in, QTreeWidgetItem &item);
       
   256 #endif
       
   257 
       
   258 class QTreeWidgetPrivate;
       
   259 
       
   260 class Q_GUI_EXPORT QTreeWidget : public QTreeView
       
   261 {
       
   262     Q_OBJECT
       
   263     Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount)
       
   264     Q_PROPERTY(int topLevelItemCount READ topLevelItemCount)
       
   265 
       
   266     friend class QTreeModel;
       
   267     friend class QTreeWidgetItem;
       
   268 public:
       
   269     explicit QTreeWidget(QWidget *parent = 0);
       
   270     ~QTreeWidget();
       
   271 
       
   272     int columnCount() const;
       
   273     void setColumnCount(int columns);
       
   274 
       
   275     QTreeWidgetItem *invisibleRootItem() const;
       
   276     QTreeWidgetItem *topLevelItem(int index) const;
       
   277     int topLevelItemCount() const;
       
   278     void insertTopLevelItem(int index, QTreeWidgetItem *item);
       
   279     void addTopLevelItem(QTreeWidgetItem *item);
       
   280     QTreeWidgetItem *takeTopLevelItem(int index);
       
   281     int indexOfTopLevelItem(QTreeWidgetItem *item); // ### Qt 5: remove me
       
   282     int indexOfTopLevelItem(QTreeWidgetItem *item) const;
       
   283 
       
   284     void insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items);
       
   285     void addTopLevelItems(const QList<QTreeWidgetItem*> &items);
       
   286 
       
   287     QTreeWidgetItem *headerItem() const;
       
   288     void setHeaderItem(QTreeWidgetItem *item);
       
   289     void setHeaderLabels(const QStringList &labels);
       
   290     inline void setHeaderLabel(const QString &label);
       
   291 
       
   292     QTreeWidgetItem *currentItem() const;
       
   293     int currentColumn() const;
       
   294     void setCurrentItem(QTreeWidgetItem *item);
       
   295     void setCurrentItem(QTreeWidgetItem *item, int column);
       
   296     void setCurrentItem(QTreeWidgetItem *item, int column, QItemSelectionModel::SelectionFlags command);
       
   297 
       
   298     QTreeWidgetItem *itemAt(const QPoint &p) const;
       
   299     inline QTreeWidgetItem *itemAt(int x, int y) const;
       
   300     QRect visualItemRect(const QTreeWidgetItem *item) const;
       
   301 
       
   302     int sortColumn() const;
       
   303     void sortItems(int column, Qt::SortOrder order);
       
   304     void setSortingEnabled(bool enable);
       
   305     bool isSortingEnabled() const;
       
   306 
       
   307     void editItem(QTreeWidgetItem *item, int column = 0);
       
   308     void openPersistentEditor(QTreeWidgetItem *item, int column = 0);
       
   309     void closePersistentEditor(QTreeWidgetItem *item, int column = 0);
       
   310 
       
   311     QWidget *itemWidget(QTreeWidgetItem *item, int column) const;
       
   312     void setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget);
       
   313     inline void removeItemWidget(QTreeWidgetItem *item, int column);
       
   314 
       
   315     bool isItemSelected(const QTreeWidgetItem *item) const;
       
   316     void setItemSelected(const QTreeWidgetItem *item, bool select);
       
   317     QList<QTreeWidgetItem*> selectedItems() const;
       
   318     QList<QTreeWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags,
       
   319                                       int column = 0) const;
       
   320 
       
   321     bool isItemHidden(const QTreeWidgetItem *item) const;
       
   322     void setItemHidden(const QTreeWidgetItem *item, bool hide);
       
   323 
       
   324     bool isItemExpanded(const QTreeWidgetItem *item) const;
       
   325     void setItemExpanded(const QTreeWidgetItem *item, bool expand);
       
   326 
       
   327     bool isFirstItemColumnSpanned(const QTreeWidgetItem *item) const;
       
   328     void setFirstItemColumnSpanned(const QTreeWidgetItem *item, bool span);
       
   329 
       
   330     QTreeWidgetItem *itemAbove(const QTreeWidgetItem *item) const;
       
   331     QTreeWidgetItem *itemBelow(const QTreeWidgetItem *item) const;
       
   332 
       
   333     void setSelectionModel(QItemSelectionModel *selectionModel);
       
   334 
       
   335 public Q_SLOTS:
       
   336     void scrollToItem(const QTreeWidgetItem *item,
       
   337                       QAbstractItemView::ScrollHint hint = EnsureVisible);
       
   338     void expandItem(const QTreeWidgetItem *item);
       
   339     void collapseItem(const QTreeWidgetItem *item);
       
   340     void clear();
       
   341 
       
   342 Q_SIGNALS:
       
   343     void itemPressed(QTreeWidgetItem *item, int column);
       
   344     void itemClicked(QTreeWidgetItem *item, int column);
       
   345     void itemDoubleClicked(QTreeWidgetItem *item, int column);
       
   346     void itemActivated(QTreeWidgetItem *item, int column);
       
   347     void itemEntered(QTreeWidgetItem *item, int column);
       
   348     void itemChanged(QTreeWidgetItem *item, int column);
       
   349     void itemExpanded(QTreeWidgetItem *item);
       
   350     void itemCollapsed(QTreeWidgetItem *item);
       
   351     void currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
       
   352     void itemSelectionChanged();
       
   353 
       
   354 protected:
       
   355     bool event(QEvent *e);
       
   356     virtual QStringList mimeTypes() const;
       
   357     virtual QMimeData *mimeData(const QList<QTreeWidgetItem*> items) const;
       
   358     virtual bool dropMimeData(QTreeWidgetItem *parent, int index,
       
   359                               const QMimeData *data, Qt::DropAction action);
       
   360     virtual Qt::DropActions supportedDropActions() const;
       
   361     QList<QTreeWidgetItem*> items(const QMimeData *data) const;
       
   362 
       
   363     QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const;
       
   364     QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const;
       
   365     void dropEvent(QDropEvent *event);
       
   366 
       
   367 private:
       
   368     void setModel(QAbstractItemModel *model);
       
   369 
       
   370     Q_DECLARE_PRIVATE(QTreeWidget)
       
   371     Q_DISABLE_COPY(QTreeWidget)
       
   372 
       
   373     Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
       
   374     Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
       
   375     Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
       
   376     Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
       
   377     Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
       
   378     Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
       
   379     Q_PRIVATE_SLOT(d_func(), void _q_emitItemExpanded(const QModelIndex &index))
       
   380     Q_PRIVATE_SLOT(d_func(), void _q_emitItemCollapsed(const QModelIndex &index))
       
   381     Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
       
   382     Q_PRIVATE_SLOT(d_func(), void _q_sort())
       
   383     Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
       
   384     Q_PRIVATE_SLOT(d_func(), void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected))
       
   385 };
       
   386 
       
   387 inline void QTreeWidget::removeItemWidget(QTreeWidgetItem *item, int column)
       
   388 { setItemWidget(item, column, 0); }
       
   389 
       
   390 inline QTreeWidgetItem *QTreeWidget::itemAt(int ax, int ay) const
       
   391 { return itemAt(QPoint(ax, ay)); }
       
   392 
       
   393 inline void QTreeWidget::setHeaderLabel(const QString &alabel)
       
   394 { setHeaderLabels(QStringList(alabel)); }
       
   395 
       
   396 inline void QTreeWidgetItem::setSelected(bool aselect)
       
   397 { if (view) view->setItemSelected(this, aselect); }
       
   398 
       
   399 inline bool QTreeWidgetItem::isSelected() const
       
   400 { return (view ? view->isItemSelected(this) : false); }
       
   401 
       
   402 inline void QTreeWidgetItem::setHidden(bool ahide)
       
   403 { if (view) view->setItemHidden(this, ahide); }
       
   404 
       
   405 inline bool QTreeWidgetItem::isHidden() const
       
   406 { return (view ? view->isItemHidden(this) : false); }
       
   407 
       
   408 inline void QTreeWidgetItem::setExpanded(bool aexpand)
       
   409 { if (view) view->setItemExpanded(this, aexpand); }
       
   410 
       
   411 inline bool QTreeWidgetItem::isExpanded() const
       
   412 { return (view ? view->isItemExpanded(this) : false); }
       
   413 
       
   414 inline void QTreeWidgetItem::setFirstColumnSpanned(bool aspan)
       
   415 { if (view) view->setFirstItemColumnSpanned(this, aspan); }
       
   416 
       
   417 inline bool QTreeWidgetItem::isFirstColumnSpanned() const
       
   418 { return (view ? view->isFirstItemColumnSpanned(this) : false); }
       
   419 
       
   420 inline void QTreeWidgetItem::setDisabled(bool disabled)
       
   421 { setFlags(disabled ? (flags() & ~Qt::ItemIsEnabled) : flags() | Qt::ItemIsEnabled); }
       
   422 
       
   423 inline bool QTreeWidgetItem::isDisabled() const
       
   424 { return !(flags() & Qt::ItemIsEnabled); }
       
   425 
       
   426 #endif // QT_NO_TREEWIDGET
       
   427 
       
   428 QT_END_NAMESPACE
       
   429 
       
   430 QT_END_HEADER
       
   431 
       
   432 #endif // QTREEWIDGET_H