diff options
author | Kavindra Devi Palaraja <kavindra.palaraja@nokia.com> | 2009-05-11 11:21:21 +0200 |
---|---|---|
committer | Kavindra Devi Palaraja <kavindra.palaraja@nokia.com> | 2009-05-11 11:21:21 +0200 |
commit | 604ad797c5d60e4dc551eb6c2a22972b2de2a78e (patch) | |
tree | 32294ff1d1acaa6d5757b2500465d289df9599bb | |
parent | a5ce89747444169c9acd648d4a0d5f228f7321df (diff) | |
parent | 1f3b174b487f2fc294201e5756021f24906af8b8 (diff) |
Merge branch 'master' of git@scm.dev.nokia.troll.no:research/itemviews-ng
-rw-r--r-- | src/experimental/qgraphicsflowview.cpp | 4 | ||||
-rw-r--r-- | src/experimental/qgraphicspathview.cpp | 2 | ||||
-rw-r--r-- | src/qgraphicslistview.cpp | 26 | ||||
-rw-r--r-- | src/qgraphicslistview.h | 5 | ||||
-rw-r--r-- | src/qgraphicslistview_p.h | 2 | ||||
-rw-r--r-- | src/qlistcontroller.cpp | 52 | ||||
-rw-r--r-- | src/qlistcontroller.h | 1 | ||||
-rw-r--r-- | src/qlistmodelinterface.cpp | 73 | ||||
-rw-r--r-- | src/qlistmodelinterface.h | 6 | ||||
-rw-r--r-- | src/qlistselectionmanager.h | 2 | ||||
-rw-r--r-- | src/qtablecontroller.cpp | 18 | ||||
-rw-r--r-- | src/qtreecontroller.cpp | 18 | ||||
-rw-r--r-- | src/qtreecontroller.h | 1 | ||||
-rw-r--r-- | src/qtreedefaultmodel.h | 6 | ||||
-rw-r--r-- | tests/qgraphicslistview/tst_qgraphicslistview.cpp | 52 | ||||
-rw-r--r-- | tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp | 216 |
16 files changed, 318 insertions, 166 deletions
diff --git a/src/experimental/qgraphicsflowview.cpp b/src/experimental/qgraphicsflowview.cpp index 2300f67..23b7099 100644 --- a/src/experimental/qgraphicsflowview.cpp +++ b/src/experimental/qgraphicsflowview.cpp @@ -76,8 +76,8 @@ void QtGraphicsFlowViewItem::paint(QPainter *painter, const QStyleOptionGraphics d->view->initStyleOption(&d->option); d->view->initStyleOption(&d->option, d->index); - QHash<int, QVariant> itemData = d->view->model()->data(d->index, QList<int>() << QtListModelInterface::IconRole); - QVariant var = itemData.value(QtListModelInterface::IconRole); + QHash<int, QVariant> itemData = d->view->model()->data(d->index, QList<int>() << Qt::DecorationRole); + QVariant var = itemData.value(Qt::DecorationRole); if (var.isNull()) return; diff --git a/src/experimental/qgraphicspathview.cpp b/src/experimental/qgraphicspathview.cpp index 2a358b1..32bba08 100644 --- a/src/experimental/qgraphicspathview.cpp +++ b/src/experimental/qgraphicspathview.cpp @@ -154,7 +154,7 @@ void QtGraphicsPathView::doLayout() while (!path.isEmpty() && counter < count) { initStyleOption(&option, index); qreal c = qreal(counter) + progress; - QSizeF size = d->itemSize(&option, index); + QSizeF size = d->itemSizeHint(&option, index); //qreal scale = (qreal(count - qAbs(c - (count / 2))) / qreal(count)); qreal t = qBound(qreal(0), c / qreal(count), qreal(1)); QPointF pos = path.pointAtPercent(t); diff --git a/src/qgraphicslistview.cpp b/src/qgraphicslistview.cpp index 03321cb..09fcf55 100644 --- a/src/qgraphicslistview.cpp +++ b/src/qgraphicslistview.cpp @@ -432,7 +432,7 @@ QtGraphicsListViewItem *QtGraphicsListViewPrivate::viewItemAt(int index, int fir /*! \internal */ -QSizeF QtGraphicsListViewPrivate::itemSize(const QStyleOptionViewItemV4 *option, int index, const QSizeF &constraint) const +QSizeF QtGraphicsListViewPrivate::itemSizeHint(const QStyleOptionViewItemV4 *option, int index, const QSizeF &constraint) const { return viewItems.isEmpty() ? QSizeF() : viewItems.first()->sizeHint(index, option, Qt::PreferredSize, constraint); } @@ -628,7 +628,7 @@ int QtGraphicsListView::itemAt(const QPointF &position) const qreal y = -d->verticalOffset; while (y <= position.y() && index < count) { initStyleOption(&option, index); - QSizeF size = d->itemSize(&option, index, constraint); + QSizeF size = d->itemSizeHint(&option, index, constraint); y += size.height(); if (y >= position.y()) return index; @@ -638,7 +638,7 @@ int QtGraphicsListView::itemAt(const QPointF &position) const qreal x = -d->horizontalOffset; while (x <= position.x() && index < count) { initStyleOption(&option, index); - QSizeF size = d->itemSize(&option, index, constraint); + QSizeF size = d->itemSizeHint(&option, index, constraint); x += size.width(); if (x >= position.x()) return index; @@ -692,7 +692,7 @@ void QtGraphicsListView::doLayout() if (y < 0) { // the cached offset was above the visible area while (index < count) { initStyleOption(&option, index); - const qreal height = d->itemSize(&option, index, constraint).height(); + const qreal height = d->itemSizeHint(&option, index, constraint).height(); if (y + height > area.y()) break; y += height; @@ -701,7 +701,7 @@ void QtGraphicsListView::doLayout() } else if (y > 0) { // the cached offset was below while (index >= 0 && y > 0) { initStyleOption(&option, index); - const qreal height = d->itemSize(&option, index, constraint).height(); + const qreal height = d->itemSizeHint(&option, index, constraint).height(); y -= height; --index; } @@ -738,7 +738,7 @@ void QtGraphicsListView::doLayout() if (x < area.x()) { // the cached offset was left of the visible area while (index < count) { initStyleOption(&option, index); - const qreal width = d->itemSize(&option, index, constraint).width(); + const qreal width = d->itemSizeHint(&option, index, constraint).width(); if (x + width > area.x()) break; x += width; @@ -747,7 +747,7 @@ void QtGraphicsListView::doLayout() } else if (x > area.x()) { // the cached offset was to the right while (index >= 0 && x > area.x()) { initStyleOption(&option, index); - const qreal width = d->itemSize(&option, index, constraint).width(); + const qreal width = d->itemSizeHint(&option, index, constraint).width(); x -= width; --index; } @@ -798,7 +798,7 @@ int QtGraphicsListView::maximumFirstIndex() const qreal height = size().height() + d->verticalOffset; for (; index >= 0; --index) { initStyleOption(&option, index); - height -= d->itemSize(&option, index, constraint).height(); + height -= d->itemSizeHint(&option, index, constraint).height(); if (height < 0) break; } @@ -806,7 +806,7 @@ int QtGraphicsListView::maximumFirstIndex() const qreal width = size().width() + d->horizontalOffset; for (; index >= 0; --index) { initStyleOption(&option, index); - width -= d->itemSize(&option, index, constraint).width(); + width -= d->itemSizeHint(&option, index, constraint).width(); if (width < 0) break; } @@ -836,7 +836,7 @@ qreal QtGraphicsListView::maximumHorizontalOffset() const int count = (d->model ? d->model->count() : 0); for (int index = 0; index < count; ++index) { initStyleOption(&option, index); - QSizeF size = d->itemSize(&option, index, constraint); + QSizeF size = d->itemSizeHint(&option, index, constraint); max = qMax(max, size.width()); content += size.width(); } @@ -864,7 +864,7 @@ qreal QtGraphicsListView::maximumVerticalOffset() const int count = d->model ? d->model->count() : 0; for (int index = 0; index < count; ++index) { initStyleOption(&option, index); - QSizeF size = d->itemSize(&option, index, constraint); + QSizeF size = d->itemSizeHint(&option, index, constraint); content += size.height(); max = qMax(max, size.height()); } @@ -977,7 +977,7 @@ void QtGraphicsListView::setFirstIndexToEnsureIndexIsVisible(int index) qreal height = size().height() + d->verticalOffset; for (; index >= 0; --index) { initStyleOption(&option, index); - height -= d->itemSize(&option, index, constraint).height(); + height -= d->itemSizeHint(&option, index, constraint).height(); if (height < 0) break; } @@ -985,7 +985,7 @@ void QtGraphicsListView::setFirstIndexToEnsureIndexIsVisible(int index) qreal width = size().width() + d->horizontalOffset; for (; index >= 0; --index) { initStyleOption(&option, index); - width -= d->itemSize(&option, index, constraint).width(); + width -= d->itemSizeHint(&option, index, constraint).width(); if (width < 0) break; } diff --git a/src/qgraphicslistview.h b/src/qgraphicslistview.h index 23e26fd..31c80c8 100644 --- a/src/qgraphicslistview.h +++ b/src/qgraphicslistview.h @@ -53,7 +53,7 @@ public: int index() const; void setIndex(int index); - virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; + virtual QSizeF sizeHint(Qt::SizeHint which = Qt::PreferredSize, const QSizeF &constraint = QSizeF()) const; virtual QSizeF sizeHint(int index, const QStyleOptionViewItemV4 *option, Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const; virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); @@ -146,14 +146,13 @@ Q_SIGNALS: void layoutChanged(); // ### ??? protected: - friend class QtPrinterListView; + friend class QtPrinterListView; // ### FIXME: should not be needed QtGraphicsListView(QtGraphicsListViewPrivate &, Qt::Orientation orientation, QGraphicsWidget *parent, Qt::WindowFlags wFlags = 0); // ### making them virtual allows the user to hook up to any signals they need virtual void setController(QtListController *control); virtual void setModel(QtListModelInterface *model); virtual void setSelectionManager(QtListSelectionManager *selectionManager); - virtual bool event(QEvent *event); QtGraphicsListViewPrivate *d_ptr; diff --git a/src/qgraphicslistview_p.h b/src/qgraphicslistview_p.h index 148891e..28153b0 100644 --- a/src/qgraphicslistview_p.h +++ b/src/qgraphicslistview_p.h @@ -83,7 +83,7 @@ public: void recycleViewItems(int firstVisibleIndex); void removeViewItemsFrom(int i); QtGraphicsListViewItem *viewItemAt(int index, int firstVisibleIndex); - QSizeF itemSize(const QStyleOptionViewItemV4 *option, int index, const QSizeF &constraints = QSizeF()) const; + QSizeF itemSizeHint(const QStyleOptionViewItemV4 *option, int index, const QSizeF &constraints = QSizeF()) const; QtGraphicsListView *q_ptr; QtListController *controller; diff --git a/src/qlistcontroller.cpp b/src/qlistcontroller.cpp index c625d05..551fde4 100644 --- a/src/qlistcontroller.cpp +++ b/src/qlistcontroller.cpp @@ -107,17 +107,21 @@ void QtListControllerPrivate::_q_horizontalOffsetChanged(qreal offset) /*! \class QtListController - \brief provides a controller for model/view objects. + \brief provides a controller for list model and view objects. - QtListController represents a controller in model/view programming. It is - responsible for handling all user input and model changes. In other words, - QtListController accepts events and proceeds to make the requested changes - to the view, model, or selections. For example, when the user clicks on - the left mouse button, it is QtListController's responsibility to change - selection. + QtListController represents a controller in the model-view-controller (MFC) + design-pattern. - The actual widget to be used in an application is QtListWidgetNG. You can - install any QtListController object on a QtListWidgetNG. + It is used together with a sub-class of QtListModelInterface and QtGraphicsListView. + + QtListController is responsible for handling user input events and make the requested + changes to the view, model, or selections. + + For example, when the user clicks an item in the view, it is QtListController's + responsibility to change selection state of that item. + + QtListWidgetNG contains a QtListController, as well as a QtListDefaultModel + and a QtGraphicsListView. */ /*! @@ -273,6 +277,11 @@ void QtListController::setView(QtGraphicsListView *view) } /*! + Returns the scroll value of the view. + This value depends on the controller scroll mode and the + view orientation. + + \sa view() */ qreal QtListController::scrollValue() const { @@ -287,6 +296,11 @@ qreal QtListController::scrollValue() const } /*! + Sets the scroll value on the view. + The property this value is set on depends on the controller + scroll mode and the view orientation. + + \sa view() */ void QtListController::setScrollValue(qreal value) { @@ -302,6 +316,11 @@ void QtListController::setScrollValue(qreal value) } /*! + Returns the page step value of the view. + This value depends on the controller scroll mode and the + view orientation. + + \sa view() */ qreal QtListController::pageStepValue(qreal *maximumScrollValue) const { @@ -327,6 +346,11 @@ qreal QtListController::pageStepValue(qreal *maximumScrollValue) const /*! + Returns the maximum scroll value of the view. + This value depends on the controller scroll mode and the + view orientation. + + \sa view() */ qreal QtListController::maximumScrollValue() const { @@ -506,7 +530,7 @@ bool QtListController::hoverLeaveEvent(QGraphicsSceneHoverEvent *event, const QT } /*! -*/ + */ bool QtListController::wheelEvent(QGraphicsSceneWheelEvent *event, const QTransform &transform) { Q_UNUSED(transform); @@ -520,7 +544,7 @@ bool QtListController::wheelEvent(QGraphicsSceneWheelEvent *event, const QTransf } /*! -*/ + */ bool QtListController::resizeEvent(QGraphicsSceneResizeEvent *event, const QTransform &transform) { Q_UNUSED(event); @@ -533,8 +557,10 @@ bool QtListController::resizeEvent(QGraphicsSceneResizeEvent *event, const QTran } /*! - * This virtual function receives events to the list controller and should - * return true if the event \p event was recognized and processed. + Processes the given \a event, performing the appropriate changes to the, view + selections or model. The given \a transform is used to transform coordinates + and geometries if provided by the event. + Returns true if the event was recognized and processed, otherwise returns false. */ bool QtListController::processEvent(QEvent *event, const QTransform &transform) { diff --git a/src/qlistcontroller.h b/src/qlistcontroller.h index c74672e..784d89a 100644 --- a/src/qlistcontroller.h +++ b/src/qlistcontroller.h @@ -87,7 +87,6 @@ public: virtual bool hoverLeaveEvent(QGraphicsSceneHoverEvent *event, const QTransform &transform); virtual bool wheelEvent(QGraphicsSceneWheelEvent *event, const QTransform &transform); virtual bool resizeEvent(QGraphicsSceneResizeEvent *event, const QTransform &transform); - virtual bool processEvent(QEvent *event, const QTransform &transform); Q_SIGNALS: diff --git a/src/qlistmodelinterface.cpp b/src/qlistmodelinterface.cpp index 58cdfa9..12db683 100644 --- a/src/qlistmodelinterface.cpp +++ b/src/qlistmodelinterface.cpp @@ -25,42 +25,79 @@ /*! \class QtListModelInterface - \brief The QtListModelInterface is the interface class that data-models for - the QListViewNG implement. + \brief The QtListModelInterface is the interface class that list data-models + implement. - A QListViewNG is backed by two classes, a list control (QAbstractListControl) - and a model. Where the QAbstractListControl defines all the details on how to - show any sort of data, this class defines all the data that is shown in that list. - The default implementation of this interface is the QListItemModel. - - Each individual list item is addressed using an index in the form of an int. Each item - has multiple roles (QtListModelInterface::Roles) and those are requested by the control via - the data() method. + QtListWidgetNG uses QtListDefaultModel, a sub-class of QtListModelInterface, + together with QtListController and QtGraphicsListView to provide a widget that + shows a list of items. */ /*! - \enum QtListModelInterface::Roles - - Data roles for each of the items. -*/ - + */ QtListModelInterface::QtListModelInterface(QObject *parent) : QObject(parent) { } +/*! + \internal + */ QtListModelInterface::QtListModelInterface(QObjectPrivate &dd, QObject *parent) : QObject(dd, parent) { } - +/*! + */ QtListModelInterface::~QtListModelInterface() { } /*! - \fd QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const; + \fd count() const - return all the requested data roles for one specific index. + Returns the number of items in the model. + */ + +/*! + \fd QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const + + Returns the data for the given \a index and the given \a roles. */ + +/*! + \fn bool setData(int index, const QHash<int,QVariant> &values) + + Sets the data for the item refered to by the given \a index to the given \a values. + Returns true if the data was set on the item, otherwise false. + + The default implementation does not set the data, and will always return false. + */ + +/*! + \signal itemsInserted(int index, int count) + + Emitted when \a count number of items were inserted in the model starting with + the index \a index. + */ + +/*! + \signal itemsRemoved(int index, int count) + + Emitted when \a count number of items were removed in the model starting + with the index \a index. + */ + +/*! + \signal itemsMoved(int from, int to, int count) + + Emitted when \a count number of items were moved in the model from \a from to \to. + */ + +/*! + \signal itemsChanged(int index, int count, const QList<int> &roles) + + Emitted when the data for \a roles in \a count number of items were changed in the model + starting with the index \a index. + */ diff --git a/src/qlistmodelinterface.h b/src/qlistmodelinterface.h index 243a934..71d5630 100644 --- a/src/qlistmodelinterface.h +++ b/src/qlistmodelinterface.h @@ -40,12 +40,6 @@ public: QtListModelInterface(QObject *parent = 0); virtual ~QtListModelInterface(); - enum Roles { - // ### move these into the Qt namespace - unless we remove the roles - TextRole = Qt::DisplayRole, - IconRole = Qt::DecorationRole - }; - virtual int count() const = 0; virtual QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const = 0; virtual bool setData(int index, const QHash<int,QVariant> &values) { Q_UNUSED(index); Q_UNUSED(values); return false; } diff --git a/src/qlistselectionmanager.h b/src/qlistselectionmanager.h index 20fe985..5e6d450 100644 --- a/src/qlistselectionmanager.h +++ b/src/qlistselectionmanager.h @@ -52,6 +52,8 @@ private: QSharedDataPointer<QtListSelectionChangeData> d; }; +Q_DECLARE_TYPEINFO(QtListSelectionChange, Q_MOVABLE_TYPE); + class Q_ITEMVIEWSNG_EXPORT QtListSelectionManager : public QObject { Q_OBJECT diff --git a/src/qtablecontroller.cpp b/src/qtablecontroller.cpp index c177b65..e7f5746 100644 --- a/src/qtablecontroller.cpp +++ b/src/qtablecontroller.cpp @@ -480,8 +480,22 @@ bool QtTableControllerPrivate::sendMouseEditorEvent(QMouseEvent *event, const QT // QtTableController /*! - \class QtTableController - \brief A table control + \class QtTableController + \brief provides a controller for table model and view objects. + + QtTableController represents a controller in the model-view-controller (MFC) + design-pattern. + + It is used together with a sub-class of QtTableModelInterface and QtGraphicsTableView. + + QtTableController is responsible for handling user input events and make the requested + changes to the view, model, or selections. + + For example, when the user clicks a cell in the view, it is QtTableController's + responsibility to change selection state of that cell. + + QtTableWidgetNG contains a QtTableController, as well as a QtTableDefaultModel + and a QtGraphicsTableView. */ /*! diff --git a/src/qtreecontroller.cpp b/src/qtreecontroller.cpp index a3a4993..6dcbb11 100644 --- a/src/qtreecontroller.cpp +++ b/src/qtreecontroller.cpp @@ -160,8 +160,22 @@ bool QtTreeControllerPrivate::setCurrentItem(QtTreeModelBase::iterator_base &it) // QtTreeController /*! - \class QtTreeController - \brief A tree controller + \class QtTreeController + \brief provides a controller for tree model and view objects. + + QtTreeController represents a controller in the model-view-controller (MFC) + design-pattern. + + It is used together with a sub-class of QtTreeModelInterface and QtGraphicsTreeView. + + QtTreeController is responsible for handling user input events and make the requested + changes to the view, model, or selections. + + For example, when the user clicks an item in the view, it is QtTreeController's + responsibility to change selection state of that item. + + QtTreeWidgetNG contains a QtTreeController, as well as a QtTreeDefaultModel + and a QtGraphicsTreeView. */ /*! diff --git a/src/qtreecontroller.h b/src/qtreecontroller.h index fcf5e34..d1dc7fd 100644 --- a/src/qtreecontroller.h +++ b/src/qtreecontroller.h @@ -94,7 +94,6 @@ public: virtual bool hoverLeaveEvent(QGraphicsSceneHoverEvent *event, const QTransform &transform); virtual bool wheelEvent(QGraphicsSceneWheelEvent *event, const QTransform &transform); virtual bool resizeEvent(QGraphicsSceneResizeEvent *event, const QTransform &transform); - virtual bool processEvent(QEvent *event, const QTransform &transform); Q_SIGNALS: diff --git a/src/qtreedefaultmodel.h b/src/qtreedefaultmodel.h index 36c3b31..aae5b0d 100644 --- a/src/qtreedefaultmodel.h +++ b/src/qtreedefaultmodel.h @@ -21,8 +21,8 @@ ** ****************************************************************************/ -#ifndef QTTREEDefaultModel_H -#define QTTREEDefaultModel_H +#ifndef QTTREEDEFAULTMODEL_H +#define QTTREEDEFAULTMODEL_H #include "qtreemodelinterface.h" #include <QtGui/qicon.h> @@ -162,4 +162,4 @@ QT_END_NAMESPACE QT_END_HEADER -#endif//QTTREEDefaultModel_H +#endif//QTTREEDEFAULTMODEL_H diff --git a/tests/qgraphicslistview/tst_qgraphicslistview.cpp b/tests/qgraphicslistview/tst_qgraphicslistview.cpp index 1a21a40..6b54fc1 100644 --- a/tests/qgraphicslistview/tst_qgraphicslistview.cpp +++ b/tests/qgraphicslistview/tst_qgraphicslistview.cpp @@ -72,6 +72,7 @@ protected: GraphicsListView *view; }; +Q_DECLARE_METATYPE(QList<int>) Q_DECLARE_METATYPE(QList<QRectF>) tst_QtGraphicsListView::tst_QtGraphicsListView() @@ -387,6 +388,7 @@ void tst_QtGraphicsListView::layout_data() QTest::addColumn<qreal>("horizontalOffset"); QTest::addColumn<qreal>("verticalOffset"); QTest::addColumn<QList<QRectF> >("expectedGeometries"); + QTest::addColumn<QList<int> >("expectedIndexes"); QTest::newRow("no items, vertical") << 0 @@ -394,7 +396,8 @@ void tst_QtGraphicsListView::layout_data() << QSizeF(100, 100) << 0 << 0. << 0. - << QList<QRectF>(); + << QList<QRectF>() + << QList<int>(); QTest::newRow("one item, vertical, all visible") @@ -403,7 +406,8 @@ void tst_QtGraphicsListView::layout_data() << QSizeF(100, 100) << 0 << 0. << 0. - << (QList<QRectF>() << QRectF(0, 0, 20, 20)); + << (QList<QRectF>() << QRectF(0, 0, 20, 20)) + << (QList<int>() << 0); QTest::newRow("five items, vertical, all visible") @@ -417,7 +421,9 @@ void tst_QtGraphicsListView::layout_data() << QRectF(0, 20, 20, 20) << QRectF(0, 40, 20, 20) << QRectF(0, 60, 20, 20) - << QRectF(0, 80, 20, 20)); + << QRectF(0, 80, 20, 20)) + << (QList<int>() + << 0 << 1 << 2 << 3 << 4); QTest::newRow("ten items, vertical, five visible") << 10 @@ -430,7 +436,9 @@ void tst_QtGraphicsListView::layout_data() << QRectF(0, 20, 20, 20) << QRectF(0, 40, 20, 20) << QRectF(0, 60, 20, 20) - << QRectF(0, 80, 20, 20)); + << QRectF(0, 80, 20, 20)) + << (QList<int>() + << 0 << 1 << 2 << 3 << 4); QTest::newRow("ten items, vertical, five visible, first item five") << 10 @@ -443,7 +451,9 @@ void tst_QtGraphicsListView::layout_data() << QRectF(0, 20, 20, 20) << QRectF(0, 40, 20, 20) << QRectF(0, 60, 20, 20) - << QRectF(0, 80, 20, 20)); + << QRectF(0, 80, 20, 20)) + << (QList<int>() + << 5 << 6 << 7 << 8 << 9); QTest::newRow("ten items, vertical, five visible, vertical offset") << 10 @@ -457,7 +467,9 @@ void tst_QtGraphicsListView::layout_data() << QRectF(0, 30, 20, 20) << QRectF(0, 50, 20, 20) << QRectF(0, 70, 20, 20) - << QRectF(0, 90, 20, 20)); + << QRectF(0, 90, 20, 20)) + << (QList<int>() + << 0 << 1 << 2 << 3 << 4 << 5); QTest::newRow("no items, horizontal") << 0 @@ -465,7 +477,8 @@ void tst_QtGraphicsListView::layout_data() << QSizeF(100, 100) << 0 << 0. << 0. - << QList<QRectF>(); + << QList<QRectF>() + << QList<int>(); QTest::newRow("one item, horizontal, all visible") @@ -474,7 +487,8 @@ void tst_QtGraphicsListView::layout_data() << QSizeF(100, 100) << 0 << 0. << 0. - << (QList<QRectF>() << QRectF(0, 0, 20, 20)); + << (QList<QRectF>() << QRectF(0, 0, 20, 20)) + << (QList<int>() << 0); QTest::newRow("five items, horizontal, all visible") @@ -488,7 +502,9 @@ void tst_QtGraphicsListView::layout_data() << QRectF(20, 0, 20, 20) << QRectF(40, 0, 20, 20) << QRectF(60, 0, 20, 20) - << QRectF(80, 0, 20, 20)); + << QRectF(80, 0, 20, 20)) + << (QList<int>() + << 0 << 1 << 2 << 3 << 4); QTest::newRow("ten items, horizontal, five visible") << 10 @@ -501,7 +517,9 @@ void tst_QtGraphicsListView::layout_data() << QRectF(20, 0, 20, 20) << QRectF(40, 0, 20, 20) << QRectF(60, 0, 20, 20) - << QRectF(80, 0, 20, 20)); + << QRectF(80, 0, 20, 20)) + << (QList<int>() + << 0 << 1 << 2 << 3 << 4); QTest::newRow("ten items, horizontal, five visible, first item five") << 10 @@ -514,7 +532,9 @@ void tst_QtGraphicsListView::layout_data() << QRectF(20, 0, 20, 20) << QRectF(40, 0, 20, 20) << QRectF(60, 0, 20, 20) - << QRectF(80, 0, 20, 20)); + << QRectF(80, 0, 20, 20)) + << (QList<int>() + << 5 << 6 << 7 << 8 << 9); QTest::newRow("ten items, horizontal, five visible, horizontal offset") << 10 @@ -528,7 +548,9 @@ void tst_QtGraphicsListView::layout_data() << QRectF(30, 0, 20, 20) << QRectF(50, 0, 20, 20) << QRectF(70, 0, 20, 20) - << QRectF(90, 0, 20, 20)); + << QRectF(90, 0, 20, 20)) + << (QList<int>() + << 0 << 1 << 2 << 3 << 4 << 5); } void tst_QtGraphicsListView::layout() @@ -540,6 +562,7 @@ void tst_QtGraphicsListView::layout() QFETCH(qreal, horizontalOffset); QFETCH(qreal, verticalOffset); QFETCH(QList<QRectF>, expectedGeometries); + QFETCH(QList<int>, expectedIndexes); QtListDefaultModel model; for (int i = 0; i < count; ++i) @@ -555,8 +578,11 @@ void tst_QtGraphicsListView::layout() view->doLayout(); QCOMPARE(view->viewItems().count(), expectedGeometries.count()); - for (int j = 0; j < expectedGeometries.count(); ++j) + QCOMPARE(view->viewItems().count(), expectedIndexes.count()); + for (int j = 0; j < expectedGeometries.count(); ++j) { QCOMPARE(view->viewItems().at(j)->geometry(), expectedGeometries.at(j)); + QCOMPARE(view->viewItems().at(j)->index(), expectedIndexes.at(j)); + } } QTEST_MAIN(tst_QtGraphicsListView) diff --git a/tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp b/tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp index b7d53ca..5d491d0 100644 --- a/tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp +++ b/tests/qlistmodeladaptor/tst_qlistmodeladaptor.cpp @@ -21,58 +21,10 @@ ** ****************************************************************************/ #include <QtTest/QtTest> +#include <QtGui/qstandarditemmodel.h> #include <qlistmodeladaptor.h> -class MockOldListModel : public QAbstractListModel -{ -public: - int rowCount(const QModelIndex &parent=QModelIndex()) const - { - if (parent.isValid()) - return 0; - if (m_data.isEmpty()) - return 0; - QList<QVariant> data = m_data.value(m_data.keys().first()); - return data.count(); - } - - QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const - { - const_cast<MockOldListModel*>(this)->m_lastRequestedIndex = index; - if (!index.isValid() || index.parent().isValid() || index.model() != this) - return QVariant(); - if (index.column() != 0) - return QVariant(); - if (!m_data.contains(role)) - return QVariant(); - QList<QVariant> data = m_data.value(role); - if (index.row() < 0 || data.count() <= index.row()) - return QVariant(); - return QVariant(data.at(index.row())); - } - - void insertStuff(int role, QList<QVariant> data) { - if (! m_data.isEmpty()) { - QList<QVariant> existingData = m_data.value(m_data.keys().first()); - QCOMPARE(data.count(), existingData.count()); - } - QVERIFY(role >= 0 && role <= 13); // only allow valid roles - m_data.insert(role, data); - } - - void insertStrings(QStringList data) { - QList<QVariant> var; - foreach(const QString &string, data) - var << QVariant(string); - insertStuff(Qt::DisplayRole, var); - } - - QHash<int, QList<QVariant> > m_data; - - QModelIndex m_lastRequestedIndex; -}; - class tst_QtListModelAdaptor : public QObject { Q_OBJECT @@ -88,15 +40,23 @@ public slots: void cleanup(); private slots: - void checkGetData(); + void count_data(); + void count(); + void getData_data(); + void getData(); + void setData_data(); + void setData(); protected: - MockOldListModel *oldModel; - QtListModelAdaptor *model; + QStandardItemModel *source; + QtListModelAdaptor *adaptor; }; +Q_DECLARE_METATYPE(QList<int>) + tst_QtListModelAdaptor::tst_QtListModelAdaptor() { + qRegisterMetaType<QList<int> >(); } tst_QtListModelAdaptor::~tst_QtListModelAdaptor() @@ -105,16 +65,14 @@ tst_QtListModelAdaptor::~tst_QtListModelAdaptor() void tst_QtListModelAdaptor::initTestCase() { - oldModel = new MockOldListModel(); - model = new QtListModelAdaptor(oldModel); + source = new QStandardItemModel(); + adaptor = new QtListModelAdaptor(source); } void tst_QtListModelAdaptor::cleanupTestCase() { - delete oldModel; - oldModel = 0; - delete model; - model = 0; + delete source; + delete adaptor; } void tst_QtListModelAdaptor::init() @@ -123,38 +81,122 @@ void tst_QtListModelAdaptor::init() void tst_QtListModelAdaptor::cleanup() { + source->clear(); +} + +void tst_QtListModelAdaptor::count_data() +{ + QTest::addColumn<int>("itemCount"); + QTest::addColumn<int>("signalCount"); + + QTest::newRow("no items") << 0 << 0; + QTest::newRow("one items") << 1 << 1; + QTest::newRow("two items") << 2 << 1; + QTest::newRow("many items") << 10000 << 1; +} + +void tst_QtListModelAdaptor::count() +{ + QFETCH(int, itemCount); + QFETCH(int, signalCount); + + QSignalSpy itemsInserted(adaptor, SIGNAL(itemsInserted(int,int))); + source->setRowCount(itemCount); + QCOMPARE(adaptor->count(), itemCount); + QCOMPARE(itemsInserted.count(), signalCount); +} + +void tst_QtListModelAdaptor::getData_data() +{ + QTest::addColumn<QStringList>("items"); + QTest::addColumn<int>("role"); + QTest::addColumn<QStringList>("data"); + QTest::addColumn<int>("signalCount"); + + QTest::newRow("no items") + << QStringList() + << int(Qt::DisplayRole) + << QStringList() + << 0; + + QTest::newRow("three items") + << (QStringList() << "one" << "two" << "three") + << int(Qt::DisplayRole) + << (QStringList() << "four" << "five" << "six") + << 3; +} + +void tst_QtListModelAdaptor::getData() +{ + QFETCH(QStringList, items); + QFETCH(int, role); + QFETCH(QStringList, data); + QFETCH(int, signalCount); + + QSignalSpy itemsChanged(adaptor, SIGNAL(itemsChanged(int,int,const QList<int>&))); + + for (int i = 0; i < items.count(); ++i) + source->appendRow(new QStandardItem(items.at(i))); + + QCOMPARE(adaptor->count(), items.count()); + for (int j = 0; j < items.count(); ++j) + QCOMPARE(adaptor->data(j, QList<int>() << role).value(role).toString(), items.at(j)); + + for (int k = 0; k < data.count(); ++k) + source->setData(source->index(k, 0), data.at(k), role); + + for (int l = 0; l < data.count(); ++l) + QCOMPARE(adaptor->data(l, QList<int>() << role).value(role).toString(), data.at(l)); + + QCOMPARE(itemsChanged.count(), signalCount); +} + +void tst_QtListModelAdaptor::setData_data() +{ + QTest::addColumn<QStringList>("items"); + QTest::addColumn<int>("role"); + QTest::addColumn<QStringList>("data"); + QTest::addColumn<int>("signalCount"); + + QTest::newRow("no items") + << QStringList() + << int(Qt::DisplayRole) + << QStringList() + << 0; + + QTest::newRow("three items") + << (QStringList() << "one" << "two" << "three") + << int(Qt::DisplayRole) + << (QStringList() << "four" << "five" << "six") + << 3; } -void tst_QtListModelAdaptor::checkGetData() +void tst_QtListModelAdaptor::setData() { - QStringList data; - data << "One" << "two" << "Mad" << "Cow" << "Flies"; - oldModel->insertStrings(data); - QCOMPARE(model->model(), oldModel); - QVERIFY(!model->rootIndex().parent().isValid()); - QCOMPARE(model->column(), 0); - QCOMPARE(model->count(), 5); - - QHash<int, QVariant> answer; - QList<int> roles; - roles << QtListModelInterface::TextRole; - answer = model->data(4, roles); - QCOMPARE(answer.count(), 1); - QVERIFY(answer.contains(QtListModelInterface::TextRole)); - QCOMPARE(answer.value(QtListModelInterface::TextRole).toString(), data[4]); - answer = model->data(5, roles); - QCOMPARE(answer.count(), 1); - QVERIFY(answer.contains(QtListModelInterface::TextRole)); - QVERIFY(!answer.value(QtListModelInterface::TextRole).isValid()); - - roles << QtListModelInterface::IconRole; - - answer = model->data(2, roles); - QCOMPARE(answer.count(), 2); - QVERIFY(answer.contains(QtListModelInterface::TextRole)); - QVERIFY(answer.contains(QtListModelInterface::IconRole)); - QCOMPARE(answer.value(QtListModelInterface::TextRole).toString(), data[2]); - QVERIFY(!answer.value(QtListModelInterface::IconRole).isValid()); + QFETCH(QStringList, items); + QFETCH(int, role); + QFETCH(QStringList, data); + QFETCH(int, signalCount); + + QSignalSpy itemsChanged(adaptor, SIGNAL(itemsChanged(int,int,const QList<int>&))); + + for (int i = 0; i < items.count(); ++i) + source->appendRow(new QStandardItem(items.at(i))); + + QCOMPARE(adaptor->count(), items.count()); + for (int j = 0; j < items.count(); ++j) + QCOMPARE(adaptor->data(j, QList<int>() << role).value(role).toString(), items.at(j)); + + for (int k = 0; k < data.count(); ++k) { + QHash<int, QVariant> hash; + hash.insert(role, data.at(k)); + adaptor->setData(k, hash); + } + + for (int l = 0; l < data.count(); ++l) + QCOMPARE(source->data(source->index(l, 0), role).toString(), data.at(l)); + + QCOMPARE(itemsChanged.count(), signalCount); } QTEST_MAIN(tst_QtListModelAdaptor) |