on xenial help sign in
tests and try targets for the organicgrid

still need to generalize the code with the abstractjournal

commit295355ba7a3aedd45a9fbc5b0169ef8343af0472 authorAlbert Astals <albert.astals@canonical.com> dateTue 14 Jan 2014, 13:25 (13 years ago) parent023665a
7 files changed+855−30
plugins/DashViews/organicgrid.cpp+78−19 plugins/DashViews/organicgrid.h+3−3 tests/plugins/DashViews/CMakeLists.txt+8−8 tests/plugins/DashViews/organicgridtest.cpp+531 tests/plugins/DashViews/organicgridtest.qml+43 tests/plugins/DashViews/organicgridtry.cpp+98 tests/plugins/DashViews/organicgridtry.qml+94
plugins/DashViews/organicgrid.cpp+78−19
@@ -428,7 +428,7 @@ void OrganicGrid::componentComplete()
428428 polish();
429429}
430430
431−QPointF OrganicGrid::positionForIndex(int modelIndex)
431+QPointF OrganicGrid::positionForIndex(int modelIndex) const
432432{
433433 const qreal moduleHeight = m_smallDelegateSize.height() + rowSpacing() + m_bigDelegateSize.height();
434434 const qreal moduleWidth = m_smallDelegateSize.width() * 2 + columnSpacing() * 2 + m_bigDelegateSize.width();
@@ -457,6 +457,16 @@ QPointF OrganicGrid::positionForIndex(int modelIndex)
457457 return QPointF(xPos, yPos);
458458}
459459
460+QSizeF OrganicGrid::sizeForIndex(int modelIndex) const
461+{
462+ const int moduleIndex = modelIndex % 6;
463+ if (moduleIndex == 0 || moduleIndex == 1 || moduleIndex == 3 || moduleIndex == 5) {
464+ return m_smallDelegateSize;
465+ } else {
466+ return m_bigDelegateSize;
467+ }
468+}
469+
460470void OrganicGrid::findBottomModelIndexToAdd(int *modelIndex, qreal *yPos)
461471{
462472 if (m_visibleItems.isEmpty()) {
@@ -464,7 +474,10 @@ void OrganicGrid::findBottomModelIndexToAdd(int *modelIndex, qreal *yPos)
464474 *yPos = 0;
465475 } else {
466476 *modelIndex = m_firstVisibleIndex + m_visibleItems.count();
467− *yPos = positionForIndex(*modelIndex).y();
477+ // We create stuff in a 6-module basis, so always return back
478+ // the y position of the first item
479+ const int firstModuleIndex = ((*modelIndex) / 6) * 6;
480+ *yPos = positionForIndex(firstModuleIndex).y();
468481 }
469482}
470483
@@ -475,7 +488,11 @@ void OrganicGrid::findTopModelIndexToAdd(int *modelIndex, qreal *yPos)
475488 *yPos = 0;
476489 } else {
477490 *modelIndex = m_firstVisibleIndex - 1;
478− *yPos = positionForIndex(*modelIndex).y();
491+ // We create stuff in a 6-module basis, so always return back
492+ // the y position of the last item bottom
493+ const int lastModuleIndex = ((*modelIndex) / 6) * 6 + 5;
494+ *yPos = positionForIndex(lastModuleIndex).y();
495+ *yPos += sizeForIndex(lastModuleIndex).height();
479496 }
480497}
481498
@@ -500,18 +517,40 @@ void OrganicGrid::positionItem(int modelIndex, QQuickItem *item)
500517 item->setPosition(pos);
501518
502519 // TODO Do we want warnings here in case the sizes are not the one we want like we have in the journals?
503− const int moduleIndex = modelIndex % 6;
504− if (moduleIndex == 0 || moduleIndex == 1 || moduleIndex == 3 || moduleIndex == 5) {
505− item->setSize(m_smallDelegateSize);
506− } else {
507− item->setSize(m_bigDelegateSize);
508− }
520+ item->setSize(sizeForIndex(modelIndex));
509521}
510522
511−bool OrganicGrid::removeNonVisibleItems(qreal /*bufferFromY*/, qreal /*bufferToY*/)
523+bool OrganicGrid::removeNonVisibleItems(qreal bufferFromY, qreal bufferToY)
512524{
513− // TODO
514− return false;
525+ bool changed = false;
526+
527+ // As adding, we also remove in a 6-module basis
528+ int lastModuleIndex = (m_firstVisibleIndex / 6) * 6 + 5;
529+ bool removeIndex = positionForIndex(lastModuleIndex).y() + sizeForIndex(lastModuleIndex).height() < bufferFromY;
530+ while (removeIndex && !m_visibleItems.isEmpty()) {
531+ releaseItem(m_visibleItems.takeFirst());
532+ changed = true;
533+ m_firstVisibleIndex++;
534+
535+ lastModuleIndex = (m_firstVisibleIndex / 6) * 6 + 5;
536+ removeIndex = positionForIndex(lastModuleIndex).y() + sizeForIndex(lastModuleIndex).height() < bufferFromY;
537+ }
538+
539+ int firstModuleIndex = ((m_firstVisibleIndex + m_visibleItems.count() - 1) / 6) * 6;
540+ removeIndex = positionForIndex(firstModuleIndex).y() > bufferToY;
541+ while (removeIndex && !m_visibleItems.isEmpty()) {
542+ releaseItem(m_visibleItems.takeLast());
543+ changed = true;
544+
545+ firstModuleIndex = ((m_firstVisibleIndex + m_visibleItems.count() - 1) / 6) * 6;
546+ removeIndex = positionForIndex(firstModuleIndex).y() > bufferToY;
547+ }
548+
549+ if (m_visibleItems.isEmpty()) {
550+ m_firstVisibleIndex = -1;
551+ }
552+
553+ return changed;
515554}
516555
517556void OrganicGrid::cleanupExistingItems()
@@ -520,6 +559,7 @@ void OrganicGrid::cleanupExistingItems()
520559 releaseItem(item);
521560 m_visibleItems.clear();
522561 m_firstVisibleIndex = -1;
562+ m_implicitHeightDirty = true;
523563}
524564
525565void OrganicGrid::doRelayout()
@@ -539,10 +579,8 @@ void OrganicGrid::doRelayout()
539579
540580void OrganicGrid::updateItemCulling(qreal visibleFromY, qreal visibleToY)
541581{
542− int i = m_firstVisibleIndex;
543582 Q_FOREACH(QQuickItem *item, m_visibleItems) {
544583 QQuickItemPrivate::get(item)->setCulled(item->y() + item->height() <= visibleFromY || item->y() >= visibleToY);
545− ++i;
546584 }
547585}
548586
@@ -557,18 +595,39 @@ void OrganicGrid::calculateImplicitHeight()
557595 if (remainingItems == 0) {
558596 setImplicitHeight(fullRowsHeight);
559597 } else if (remainingItems <= 2) {
560− setImplicitHeight(m_smallDelegateSize.height() + rowSpacing());
598+ setImplicitHeight(fullRowsHeight + m_smallDelegateSize.height() + rowSpacing());
561599 } else {
562− setImplicitHeight(m_bigDelegateSize.height() + rowSpacing());
600+ setImplicitHeight(fullRowsHeight + rowSpacing() + moduleHeight);
563601 }
564602}
565603
566604#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
567−void OrganicGrid::processModelRemoves(const QVector<QQuickChangeSet::Remove> & /*removes*/)
605+void OrganicGrid::processModelRemoves(const QVector<QQuickChangeSet::Remove> &removes)
568606#else
569−void OrganicGrid::processModelRemoves(const QVector<QQmlChangeSet::Remove> & /*removes*/)
607+void OrganicGrid::processModelRemoves(const QVector<QQmlChangeSet::Remove> &removes)
570608#endif
571609{
572− // TODO
610+#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
611+ Q_FOREACH(const QQuickChangeSet::Remove &remove, removes) {
612+#else
613+ Q_FOREACH(const QQmlChangeSet::Remove &remove, removes) {
614+#endif
615+ for (int i = remove.count - 1; i >= 0; --i) {
616+ const int indexToRemove = remove.index + i;
617+ // We only support removing from the end
618+ const int lastIndex = m_firstVisibleIndex + m_visibleItems.count() - 1;
619+ if (indexToRemove == lastIndex) {
620+ releaseItem(m_visibleItems.takeLast());
621+ } else {
622+ if (indexToRemove < lastIndex) {
623+ qFatal("OrganicGrid only supports removal from the end of the model");
624+ }
625+ }
626+ }
627+ }
628+ if (m_visibleItems.isEmpty()) {
629+ m_firstVisibleIndex = -1;
630+ }
631+ setImplicitHeightDirty();
573632}
574633
plugins/DashViews/organicgrid.h+3−3
@@ -50,8 +50,7 @@ class OrganicGrid : public QQuickItem
5050 NOTIFY delegateCreationEndChanged
5151 RESET resetDelegateCreationEnd)
5252
53−friend class VerticalJournalTest;
54−friend class HorizontalJournalTest;
53+friend class OrganicGridTest;
5554
5655public:
5756 OrganicGrid();
@@ -137,7 +136,8 @@ private:
137136// virtual void processModelRemoves(const QVector<QQmlChangeSet::Remove> &removes) = 0;
138137// #endif
139138
140− QPointF positionForIndex(int modelIndex);
139+ QPointF positionForIndex(int modelIndex) const;
140+ QSizeF sizeForIndex(int modelIndex) const;
141141
142142 void findBottomModelIndexToAdd(int *modelIndex, qreal *yPos);
143143 void findTopModelIndexToAdd(int *modelIndex, qreal *yPos);
tests/plugins/DashViews/CMakeLists.txt+8−8
@@ -40,7 +40,7 @@ qt5_use_modules(listviewwithpageheadertestsectionexternalmodelExec Test Core Qml
4040target_link_libraries(listviewwithpageheadertestsectionexternalmodelExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})
4141add_custom_target(testlistviewwithpageheadersectionexternalmodel ${CMAKE_CURRENT_BINARY_DIR}/listviewwithpageheadertestsectionexternalmodelExec -o ${CMAKE_BINARY_DIR}/testlistviewwithpageheadersectionexternalmodel.xml,xunitxml -o -,txt)
4242
43−macro(add_journal_try_test FILENAME)
43+macro(add_dashview_try_test FILENAME)
4444 # Capitalite the first letter for the test/try name targets
4545 string(SUBSTRING ${FILENAME} 0 1 FIRST_LETTER)
4646 string(TOUPPER ${FIRST_LETTER} FIRST_LETTER)
@@ -55,7 +55,7 @@ macro(add_journal_try_test FILENAME)
5555 target_link_libraries(${FILENAME}testExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})
5656 add_custom_target(test${CUSTOM_TARGET_NAME}
5757 ${CMAKE_CURRENT_BINARY_DIR}/${FILENAME}testExec
58− -o ${CMAKE_BINARY_DIR}/testVerticalJournal.xml,xunitxml
58+ -o ${CMAKE_BINARY_DIR}/test${CUSTOM_TARGET_NAME}.xml,xunitxml
5959 -o -,txt
6060 DEPENDS ${FILENAME}testExec DashViews-qml
6161 )
@@ -69,14 +69,13 @@ macro(add_journal_try_test FILENAME)
6969 target_link_libraries(${FILENAME}tryExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})
7070 add_custom_target(try${CUSTOM_TARGET_NAME}
7171 ${CMAKE_CURRENT_BINARY_DIR}/${FILENAME}tryExec
72− -o ${CMAKE_BINARY_DIR}/testVerticalJournal.xml,xunitxml
73− -o -,txt
7472 DEPENDS ${FILENAME}tryExec DashViews-qml
7573 )
7674endmacro()
7775
78−add_journal_try_test(verticaljournal)
79−add_journal_try_test(horizontaljournal)
76+add_dashview_try_test(verticaljournal)
77+add_dashview_try_test(horizontaljournal)
78+add_dashview_try_test(organicgrid)
8079
8180set(qmltest_DEFAULT_NO_ADD_TEST TRUE)
8281add_qml_test(. listviewwithpageheaderqml IMPORT_PATHS ${CMAKE_BINARY_DIR}/plugins)
@@ -85,5 +84,6 @@ add_dependencies(qmluitests testlistviewwithpageheader)
8584add_dependencies(qmluitests testlistviewwithpageheadersection)
8685add_dependencies(qmluitests testlistviewwithpageheadersectionexternalmodel)
8786add_dependencies(qmluitests testlistviewwithpageheaderqml)
88−add_dependencies(qmluitests testVerticalJournal)
89−add_dependencies(qmluitests testHorizontalJournal)
87+add_dependencies(qmluitests testVerticaljournal)
88+add_dependencies(qmluitests testHorizontaljournal)
89+add_dependencies(qmluitests testOrganicgrid)
tests/plugins/DashViews/organicgridtest.cppnew file+531
@@ -0,0 +1,531 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include <QQuickView>
18+#include <QtTestGui>
19+#include <QDebug>
20+#include <QGuiApplication>
21+#include <QQuickView>
22+#include <QtQml/qqml.h>
23+#include <QStringListModel>
24+#include <QQmlContext>
25+#include <QQmlEngine>
26+#pragma GCC diagnostic push
27+#pragma GCC diagnostic ignored "-pedantic"
28+#include <private/qquickitem_p.h>
29+#pragma GCC diagnostic pop
30+
31+#include "organicgrid.h"
32+
33+class DummyModel : public QAbstractListModel {
34+public:
35+ DummyModel() : m_count(0) {}
36+
37+ int rowCount(const QModelIndex & /*parent*/) const
38+ {
39+ return m_count;
40+ }
41+
42+ QVariant data(const QModelIndex & /*index*/, int /*role*/) const
43+ {
44+ return QVariant();
45+ }
46+
47+ void setCount(int count)
48+ {
49+ beginResetModel();
50+ m_count = count;
51+ endResetModel();
52+ }
53+
54+ void addItem()
55+ {
56+ beginInsertRows(QModelIndex(), m_count, m_count);
57+ m_count++;
58+ endInsertRows();
59+ }
60+
61+ void removeLast()
62+ {
63+ beginRemoveRows(QModelIndex(), m_count - 1, m_count - 1);
64+ m_count--;
65+ endRemoveRows();
66+ }
67+
68+private:
69+ int m_count;
70+};
71+
72+class OrganicGridTest : public QObject
73+{
74+ Q_OBJECT
75+
76+private:
77+ void verifyItem(const QQuickItem *item, int modelIndex, qreal x, qreal y, bool visible)
78+ {
79+ QTRY_COMPARE(item->x(), x);
80+ QTRY_COMPARE(item->y(), y);
81+ QTRY_COMPARE(QQuickItemPrivate::get(item)->culled, !visible);
82+ const int inModuleIndex = modelIndex % 6;
83+ if (inModuleIndex == 0 || inModuleIndex == 1 || inModuleIndex == 3 || inModuleIndex == 5) {
84+ QTRY_COMPARE(item->height(), grid->smallDelegateSize().height());
85+ QTRY_COMPARE(item->width(), grid->smallDelegateSize().width());
86+ } else {
87+ QTRY_COMPARE(item->height(), grid->bigDelegateSize().height());
88+ QTRY_COMPARE(item->width(), grid->bigDelegateSize().width());
89+ }
90+ }
91+
92+ void checkInitialPositions()
93+ {
94+ QTRY_COMPARE(grid->m_visibleItems.count(), 18);
95+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
96+ QTRY_COMPARE(grid->m_numberOfModulesPerRow, 1);
97+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
98+ verifyItem(grid->m_visibleItems[ 1], 1, 100, 0, true);
99+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 100, true);
100+ verifyItem(grid->m_visibleItems[ 3], 3, 190, 190, true);
101+ verifyItem(grid->m_visibleItems[ 4], 4, 200, 0, true);
102+ verifyItem(grid->m_visibleItems[ 5], 5, 290, 190, true);
103+ verifyItem(grid->m_visibleItems[ 6], 6, 0, 290, true);
104+ verifyItem(grid->m_visibleItems[ 7], 7, 100, 290, true);
105+ verifyItem(grid->m_visibleItems[ 8], 8, 0, 390, true);
106+ verifyItem(grid->m_visibleItems[ 9], 9, 190, 480, false);
107+ verifyItem(grid->m_visibleItems[10], 10, 200, 290, true);
108+ verifyItem(grid->m_visibleItems[11], 11, 290, 480, false);
109+ verifyItem(grid->m_visibleItems[12], 12, 0, 580, false);
110+ verifyItem(grid->m_visibleItems[13], 13, 100, 580, false);
111+ verifyItem(grid->m_visibleItems[14], 14, 0, 680, false);
112+ verifyItem(grid->m_visibleItems[15], 15, 190, 770, false);
113+ verifyItem(grid->m_visibleItems[16], 16, 200, 580, false);
114+ verifyItem(grid->m_visibleItems[17], 17, 290, 770, false);
115+ checkImplicitHeight(1150);
116+ }
117+
118+ void checkImplicitHeight(qreal implicitHeight)
119+ {
120+ QTRY_COMPARE(grid->m_implicitHeightDirty, false);
121+ QCOMPARE(grid->implicitHeight(), implicitHeight);
122+ }
123+
124+private Q_SLOTS:
125+ void init()
126+ {
127+ view = new QQuickView();
128+ view->setResizeMode(QQuickView::SizeRootObjectToView);
129+ view->engine()->addImportPath(BUILT_PLUGINS_DIR);
130+
131+ model = new DummyModel();
132+ model->setCount(21);
133+
134+ view->setSource(QUrl::fromLocalFile(DASHVIEWSTEST_FOLDER "/organicgridtest.qml"));
135+
136+ view->show();
137+ view->resize(470, 400);
138+ QTest::qWaitForWindowExposed(view);
139+
140+ grid = dynamic_cast<OrganicGrid*>(view->rootObject()->findChild<QObject*>("grid"));
141+ grid->setModel(model);
142+
143+ checkInitialPositions();
144+ }
145+
146+ void cleanup()
147+ {
148+ delete view;
149+ delete model;
150+ }
151+
152+ void testInitialPosition()
153+ {
154+ }
155+
156+ void testWidthResize()
157+ {
158+ view->resize(770, 400);
159+ QTRY_COMPARE(grid->width(), 770.);
160+
161+ QTRY_COMPARE(grid->m_visibleItems.count(), 21);
162+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
163+ QTRY_COMPARE(grid->m_numberOfModulesPerRow, 2);
164+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
165+ verifyItem(grid->m_visibleItems[ 1], 1, 100, 0, true);
166+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 100, true);
167+ verifyItem(grid->m_visibleItems[ 3], 3, 190, 190, true);
168+ verifyItem(grid->m_visibleItems[ 4], 4, 200, 0, true);
169+ verifyItem(grid->m_visibleItems[ 5], 5, 290, 190, true);
170+
171+ verifyItem(grid->m_visibleItems[ 6], 6, 390, 0, true);
172+ verifyItem(grid->m_visibleItems[ 7], 7, 490, 0, true);
173+ verifyItem(grid->m_visibleItems[ 8], 8, 390, 100, true);
174+ verifyItem(grid->m_visibleItems[ 9], 9, 580, 190, true);
175+ verifyItem(grid->m_visibleItems[10], 10, 590, 0, true);
176+ verifyItem(grid->m_visibleItems[11], 11, 680, 190, true);
177+
178+ verifyItem(grid->m_visibleItems[12], 12, 0, 290, true);
179+ verifyItem(grid->m_visibleItems[13], 13, 100, 290, true);
180+ verifyItem(grid->m_visibleItems[14], 14, 0, 390, true);
181+ verifyItem(grid->m_visibleItems[15], 15, 190, 480, false);
182+ verifyItem(grid->m_visibleItems[16], 16, 200, 290, true);
183+ verifyItem(grid->m_visibleItems[17], 17, 290, 480, false);
184+
185+ verifyItem(grid->m_visibleItems[18], 18, 390, 290, true);
186+ verifyItem(grid->m_visibleItems[19], 19, 490, 290, true);
187+ verifyItem(grid->m_visibleItems[20], 20, 390, 390, true);
188+
189+ checkImplicitHeight(570);
190+
191+ view->resize(769, 400);
192+
193+ // This is exactly the same block as the first again
194+ checkInitialPositions();
195+ }
196+
197+ void testColumnSpacing()
198+ {
199+ grid->setColumnSpacing(11);
200+
201+ QTRY_COMPARE(grid->m_needsRelayout, false);
202+ QTRY_COMPARE(grid->m_visibleItems.count(), 18);
203+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
204+ QTRY_COMPARE(grid->m_numberOfModulesPerRow, 1);
205+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
206+ verifyItem(grid->m_visibleItems[ 1], 1, 101, 0, true);
207+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 100, true);
208+ verifyItem(grid->m_visibleItems[ 3], 3, 191, 190, true);
209+ verifyItem(grid->m_visibleItems[ 4], 4, 202, 0, true);
210+ verifyItem(grid->m_visibleItems[ 5], 5, 292, 190, true);
211+ verifyItem(grid->m_visibleItems[ 6], 6, 0, 290, true);
212+ verifyItem(grid->m_visibleItems[ 7], 7, 101, 290, true);
213+ verifyItem(grid->m_visibleItems[ 8], 8, 0, 390, true);
214+ verifyItem(grid->m_visibleItems[ 9], 9, 191, 480, false);
215+ verifyItem(grid->m_visibleItems[10], 10, 202, 290, true);
216+ verifyItem(grid->m_visibleItems[11], 11, 292, 480, false);
217+ verifyItem(grid->m_visibleItems[12], 12, 0, 580, false);
218+ verifyItem(grid->m_visibleItems[13], 13, 101, 580, false);
219+ verifyItem(grid->m_visibleItems[14], 14, 0, 680, false);
220+ verifyItem(grid->m_visibleItems[15], 15, 191, 770, false);
221+ verifyItem(grid->m_visibleItems[16], 16, 202, 580, false);
222+ verifyItem(grid->m_visibleItems[17], 17, 292, 770, false);
223+ checkImplicitHeight(1150);
224+
225+ grid->setColumnSpacing(10);
226+ QTRY_COMPARE(grid->m_needsRelayout, false);
227+
228+ // This is exactly the same block as the first again
229+ checkInitialPositions();
230+ }
231+
232+ void testRowSpacing()
233+ {
234+ grid->setRowSpacing(11);
235+
236+ QTRY_COMPARE(grid->m_needsRelayout, false);
237+ QTRY_COMPARE(grid->m_visibleItems.count(), 18);
238+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
239+ QTRY_COMPARE(grid->m_numberOfModulesPerRow, 1);
240+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
241+ verifyItem(grid->m_visibleItems[ 1], 1, 100, 0, true);
242+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 101, true);
243+ verifyItem(grid->m_visibleItems[ 3], 3, 190, 191, true);
244+ verifyItem(grid->m_visibleItems[ 4], 4, 200, 0, true);
245+ verifyItem(grid->m_visibleItems[ 5], 5, 290, 191, true);
246+ verifyItem(grid->m_visibleItems[ 6], 6, 0, 292, true);
247+ verifyItem(grid->m_visibleItems[ 7], 7, 100, 292, true);
248+ verifyItem(grid->m_visibleItems[ 8], 8, 0, 393, true);
249+ verifyItem(grid->m_visibleItems[ 9], 9, 190, 483, false);
250+ verifyItem(grid->m_visibleItems[10], 10, 200, 292, true);
251+ verifyItem(grid->m_visibleItems[11], 11, 290, 483, false);
252+ verifyItem(grid->m_visibleItems[12], 12, 0, 584, false);
253+ verifyItem(grid->m_visibleItems[13], 13, 100, 584, false);
254+ verifyItem(grid->m_visibleItems[14], 14, 0, 685, false);
255+ verifyItem(grid->m_visibleItems[15], 15, 190, 775, false);
256+ verifyItem(grid->m_visibleItems[16], 16, 200, 584, false);
257+ verifyItem(grid->m_visibleItems[17], 17, 290, 775, false);
258+ checkImplicitHeight(1157);
259+
260+ grid->setRowSpacing(10);
261+ QTRY_COMPARE(grid->m_needsRelayout, false);
262+
263+ // This is exactly the same block as the first again
264+ checkInitialPositions();
265+ }
266+
267+ void testDelegateSizeChange()
268+ {
269+ grid->setSmallDelegateSize(QSizeF(30, 30));
270+ grid->setBigDelegateSize(QSizeF(50, 50));
271+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
272+ QTRY_COMPARE(grid->m_visibleItems.count(), 21);
273+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
274+ verifyItem(grid->m_visibleItems[ 1], 1, 40, 0, true);
275+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 40, true);
276+ verifyItem(grid->m_visibleItems[ 3], 3, 60, 60, true);
277+ verifyItem(grid->m_visibleItems[ 4], 4, 80, 0, true);
278+ verifyItem(grid->m_visibleItems[ 5], 5, 100, 60, true);
279+
280+ verifyItem(grid->m_visibleItems[ 6], 6, 140, 0, true);
281+ verifyItem(grid->m_visibleItems[ 7], 7, 180, 0, true);
282+ verifyItem(grid->m_visibleItems[ 8], 8, 140, 40, true);
283+ verifyItem(grid->m_visibleItems[ 9], 9, 200, 60, true);
284+ verifyItem(grid->m_visibleItems[10], 10, 220, 0, true);
285+ verifyItem(grid->m_visibleItems[11], 11, 240, 60, true);
286+
287+ verifyItem(grid->m_visibleItems[12], 12, 280, 0, true);
288+ verifyItem(grid->m_visibleItems[13], 13, 320, 0, true);
289+ verifyItem(grid->m_visibleItems[14], 14, 280, 40, true);
290+ verifyItem(grid->m_visibleItems[15], 15, 340, 60, true);
291+ verifyItem(grid->m_visibleItems[16], 16, 360, 0, true);
292+ verifyItem(grid->m_visibleItems[17], 17, 380, 60, true);
293+
294+ verifyItem(grid->m_visibleItems[18], 18, 0, 100, true);
295+ verifyItem(grid->m_visibleItems[19], 19, 40, 100, true);
296+ verifyItem(grid->m_visibleItems[20], 20, 0, 140, true);
297+ checkImplicitHeight(190);
298+
299+ grid->setSmallDelegateSize(QSizeF(90, 90));
300+ grid->setBigDelegateSize(QSizeF(180, 180));
301+
302+ // This is exactly the same block as the first again
303+ checkInitialPositions();
304+ }
305+
306+ void testChangeModel()
307+ {
308+ DummyModel *model2 = new DummyModel();
309+ model2->setCount(6);
310+ grid->setModel(model2);
311+ delete model;
312+ model = model2;
313+
314+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
315+ QTRY_COMPARE(grid->m_visibleItems.count(), 6);
316+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
317+ verifyItem(grid->m_visibleItems[ 1], 1, 100, 0, true);
318+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 100, true);
319+ verifyItem(grid->m_visibleItems[ 3], 3, 190, 190, true);
320+ verifyItem(grid->m_visibleItems[ 4], 4, 200, 0, true);
321+ verifyItem(grid->m_visibleItems[ 5], 5, 290, 190, true);
322+ checkImplicitHeight(280);
323+ }
324+
325+ void testModelReset()
326+ {
327+ model->setCount(6);
328+
329+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
330+ QTRY_COMPARE(grid->m_visibleItems.count(), 6);
331+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
332+ verifyItem(grid->m_visibleItems[ 1], 1, 100, 0, true);
333+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 100, true);
334+ verifyItem(grid->m_visibleItems[ 3], 3, 190, 190, true);
335+ verifyItem(grid->m_visibleItems[ 4], 4, 200, 0, true);
336+ verifyItem(grid->m_visibleItems[ 5], 5, 290, 190, true);
337+ checkImplicitHeight(280);
338+ }
339+
340+ void testModelRemoveLastNonVisible()
341+ {
342+ model->removeLast();
343+
344+ // This is the same than checkInitialPositions but
345+ // with a different implicitHeight since there's an item less
346+ // in the model
347+ QTRY_COMPARE(grid->m_visibleItems.count(), 18);
348+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
349+ QTRY_COMPARE(grid->m_numberOfModulesPerRow, 1);
350+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
351+ verifyItem(grid->m_visibleItems[ 1], 1, 100, 0, true);
352+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 100, true);
353+ verifyItem(grid->m_visibleItems[ 3], 3, 190, 190, true);
354+ verifyItem(grid->m_visibleItems[ 4], 4, 200, 0, true);
355+ verifyItem(grid->m_visibleItems[ 5], 5, 290, 190, true);
356+ verifyItem(grid->m_visibleItems[ 6], 6, 0, 290, true);
357+ verifyItem(grid->m_visibleItems[ 7], 7, 100, 290, true);
358+ verifyItem(grid->m_visibleItems[ 8], 8, 0, 390, true);
359+ verifyItem(grid->m_visibleItems[ 9], 9, 190, 480, false);
360+ verifyItem(grid->m_visibleItems[10], 10, 200, 290, true);
361+ verifyItem(grid->m_visibleItems[11], 11, 290, 480, false);
362+ verifyItem(grid->m_visibleItems[12], 12, 0, 580, false);
363+ verifyItem(grid->m_visibleItems[13], 13, 100, 580, false);
364+ verifyItem(grid->m_visibleItems[14], 14, 0, 680, false);
365+ verifyItem(grid->m_visibleItems[15], 15, 190, 770, false);
366+ verifyItem(grid->m_visibleItems[16], 16, 200, 580, false);
367+ verifyItem(grid->m_visibleItems[17], 17, 290, 770, false);
368+ checkImplicitHeight(960);
369+
370+ model->removeLast();
371+
372+ // Removing this one didn't cause any change since it only
373+ // was the second item of a non visible 6-module
374+ QTRY_COMPARE(grid->m_visibleItems.count(), 18);
375+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
376+ QTRY_COMPARE(grid->m_numberOfModulesPerRow, 1);
377+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
378+ verifyItem(grid->m_visibleItems[ 1], 1, 100, 0, true);
379+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 100, true);
380+ verifyItem(grid->m_visibleItems[ 3], 3, 190, 190, true);
381+ verifyItem(grid->m_visibleItems[ 4], 4, 200, 0, true);
382+ verifyItem(grid->m_visibleItems[ 5], 5, 290, 190, true);
383+ verifyItem(grid->m_visibleItems[ 6], 6, 0, 290, true);
384+ verifyItem(grid->m_visibleItems[ 7], 7, 100, 290, true);
385+ verifyItem(grid->m_visibleItems[ 8], 8, 0, 390, true);
386+ verifyItem(grid->m_visibleItems[ 9], 9, 190, 480, false);
387+ verifyItem(grid->m_visibleItems[10], 10, 200, 290, true);
388+ verifyItem(grid->m_visibleItems[11], 11, 290, 480, false);
389+ verifyItem(grid->m_visibleItems[12], 12, 0, 580, false);
390+ verifyItem(grid->m_visibleItems[13], 13, 100, 580, false);
391+ verifyItem(grid->m_visibleItems[14], 14, 0, 680, false);
392+ verifyItem(grid->m_visibleItems[15], 15, 190, 770, false);
393+ verifyItem(grid->m_visibleItems[16], 16, 200, 580, false);
394+ verifyItem(grid->m_visibleItems[17], 17, 290, 770, false);
395+ checkImplicitHeight(960);
396+
397+ model->removeLast();
398+
399+ // Removing this one removes the first of a 6-module so
400+ // it does change height again
401+ QTRY_COMPARE(grid->m_visibleItems.count(), 18);
402+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
403+ QTRY_COMPARE(grid->m_numberOfModulesPerRow, 1);
404+ verifyItem(grid->m_visibleItems[ 0], 0, 0, 0, true);
405+ verifyItem(grid->m_visibleItems[ 1], 1, 100, 0, true);
406+ verifyItem(grid->m_visibleItems[ 2], 2, 0, 100, true);
407+ verifyItem(grid->m_visibleItems[ 3], 3, 190, 190, true);
408+ verifyItem(grid->m_visibleItems[ 4], 4, 200, 0, true);
409+ verifyItem(grid->m_visibleItems[ 5], 5, 290, 190, true);
410+ verifyItem(grid->m_visibleItems[ 6], 6, 0, 290, true);
411+ verifyItem(grid->m_visibleItems[ 7], 7, 100, 290, true);
412+ verifyItem(grid->m_visibleItems[ 8], 8, 0, 390, true);
413+ verifyItem(grid->m_visibleItems[ 9], 9, 190, 480, false);
414+ verifyItem(grid->m_visibleItems[10], 10, 200, 290, true);
415+ verifyItem(grid->m_visibleItems[11], 11, 290, 480, false);
416+ verifyItem(grid->m_visibleItems[12], 12, 0, 580, false);
417+ verifyItem(grid->m_visibleItems[13], 13, 100, 580, false);
418+ verifyItem(grid->m_visibleItems[14], 14, 0, 680, false);
419+ verifyItem(grid->m_visibleItems[15], 15, 190, 770, false);
420+ verifyItem(grid->m_visibleItems[16], 16, 200, 580, false);
421+ verifyItem(grid->m_visibleItems[17], 17, 290, 770, false);
422+ checkImplicitHeight(860);
423+
424+ }
425+
426+ void testModelAppendRemoveLast()
427+ {
428+ DummyModel *model2 = new DummyModel();
429+ model2->setCount(6);
430+ grid->setModel(model2);
431+ delete model;
432+ model = model2;
433+
434+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
435+ QTRY_COMPARE(grid->m_visibleItems.count(), 6);
436+ verifyItem(grid->m_visibleItems[0], 0, 0, 0, true);
437+ verifyItem(grid->m_visibleItems[1], 1, 100, 0, true);
438+ verifyItem(grid->m_visibleItems[2], 2, 0, 100, true);
439+ verifyItem(grid->m_visibleItems[3], 3, 190, 190, true);
440+ verifyItem(grid->m_visibleItems[4], 4, 200, 0, true);
441+ verifyItem(grid->m_visibleItems[5], 5, 290, 190, true);
442+ checkImplicitHeight(280);
443+
444+ model2->addItem();
445+
446+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
447+ QTRY_COMPARE(grid->m_visibleItems.count(), 7);
448+ verifyItem(grid->m_visibleItems[0], 0, 0, 0, true);
449+ verifyItem(grid->m_visibleItems[1], 1, 100, 0, true);
450+ verifyItem(grid->m_visibleItems[2], 2, 0, 100, true);
451+ verifyItem(grid->m_visibleItems[3], 3, 190, 190, true);
452+ verifyItem(grid->m_visibleItems[4], 4, 200, 0, true);
453+ verifyItem(grid->m_visibleItems[5], 5, 290, 190, true);
454+ verifyItem(grid->m_visibleItems[6], 6, 0, 290, true);
455+ checkImplicitHeight(380);
456+
457+ model2->addItem();
458+ model2->addItem();
459+
460+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
461+ QTRY_COMPARE(grid->m_visibleItems.count(), 9);
462+ verifyItem(grid->m_visibleItems[0], 0, 0, 0, true);
463+ verifyItem(grid->m_visibleItems[1], 1, 100, 0, true);
464+ verifyItem(grid->m_visibleItems[2], 2, 0, 100, true);
465+ verifyItem(grid->m_visibleItems[3], 3, 190, 190, true);
466+ verifyItem(grid->m_visibleItems[4], 4, 200, 0, true);
467+ verifyItem(grid->m_visibleItems[5], 5, 290, 190, true);
468+ verifyItem(grid->m_visibleItems[6], 6, 0, 290, true);
469+ verifyItem(grid->m_visibleItems[7], 7, 100, 290, true);
470+ verifyItem(grid->m_visibleItems[8], 8, 0, 390, true);
471+ checkImplicitHeight(570);
472+
473+ model2->removeLast();
474+
475+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
476+ QTRY_COMPARE(grid->m_visibleItems.count(), 8);
477+ verifyItem(grid->m_visibleItems[0], 0, 0, 0, true);
478+ verifyItem(grid->m_visibleItems[1], 1, 100, 0, true);
479+ verifyItem(grid->m_visibleItems[2], 2, 0, 100, true);
480+ verifyItem(grid->m_visibleItems[3], 3, 190, 190, true);
481+ verifyItem(grid->m_visibleItems[4], 4, 200, 0, true);
482+ verifyItem(grid->m_visibleItems[5], 5, 290, 190, true);
483+ verifyItem(grid->m_visibleItems[6], 6, 0, 290, true);
484+ verifyItem(grid->m_visibleItems[7], 7, 100, 290, true);
485+ checkImplicitHeight(380);
486+ }
487+
488+ void testDelegateCreationRanges()
489+ {
490+ grid->setSmallDelegateSize(QSizeF(30, 30));
491+ grid->setBigDelegateSize(QSizeF(50, 50));
492+ model->setCount(400);
493+ checkImplicitHeight(2290);
494+
495+ view->resize(470, 700);
496+ QTRY_COMPARE(grid->height(), 700.);
497+ checkImplicitHeight(2290);
498+ QTRY_COMPARE(grid->m_firstVisibleIndex, 0);
499+ QTRY_COMPARE(grid->m_visibleItems.count(), 198);
500+
501+ grid->setDelegateCreationBegin(300);
502+ grid->setDelegateCreationEnd(400);
503+ checkImplicitHeight(2290);
504+
505+ QTRY_COMPARE(grid->m_firstVisibleIndex, 36);
506+ QTRY_COMPARE(grid->m_visibleItems.count(), 54);
507+ checkImplicitHeight(2290);
508+
509+ grid->resetDelegateCreationBegin();
510+ grid->resetDelegateCreationEnd();
511+
512+ view->resize(470, 470);
513+
514+ grid->setSmallDelegateSize(QSizeF(90, 90));
515+ grid->setBigDelegateSize(QSizeF(180, 180));
516+
517+ model->setCount(21);
518+
519+ // This is exactly the same block as the first again
520+ checkInitialPositions();
521+ }
522+
523+private:
524+ QQuickView *view;
525+ OrganicGrid *grid;
526+ DummyModel *model;
527+};
528+
529+QTEST_MAIN(OrganicGridTest)
530+
531+#include "organicgridtest.moc"
tests/plugins/DashViews/organicgridtest.qmlnew file+43
@@ -0,0 +1,43 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.1
18+import DashViews 0.1
19+
20+Item {
21+
22+ OrganicGrid {
23+ id: vj
24+ objectName: "grid"
25+ anchors.fill: parent
26+ columnSpacing: 10
27+ rowSpacing: 10
28+ smallDelegateSize: Qt.size(90, 90)
29+ bigDelegateSize: Qt.size(180, 180)
30+
31+ delegate: Rectangle {
32+ property real randomValue: Math.random()
33+ color: randomValue < 0.3 ? "green" : randomValue < 0.6 ? "blue" : "red";
34+ border.width: 3
35+
36+ Text {
37+ text: index
38+ x: 10
39+ y: 10
40+ }
41+ }
42+ }
43+}
tests/plugins/DashViews/organicgridtry.cppnew file+98
@@ -0,0 +1,98 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include <QQuickView>
18+#include <QDebug>
19+#include <QGuiApplication>
20+#include <QQuickView>
21+#include <QtQml/qqml.h>
22+#include <QStringListModel>
23+#include <QQmlContext>
24+#include <QQmlEngine>
25+#include <QGuiApplication>
26+
27+#include "verticaljournal.h"
28+
29+class DummyModel : public QAbstractListModel {
30+ Q_OBJECT
31+public:
32+ DummyModel() : m_count(0) {}
33+
34+ int rowCount(const QModelIndex & /*parent*/) const
35+ {
36+ return m_count;
37+ }
38+
39+ QVariant data(const QModelIndex & /*index*/, int /*role*/) const
40+ {
41+ return QVariant();
42+ }
43+
44+ void setCount(int count)
45+ {
46+ beginResetModel();
47+ m_count = count;
48+ endResetModel();
49+ }
50+
51+public slots:
52+ void addItem()
53+ {
54+ beginInsertRows(QModelIndex(), m_count, m_count);
55+ m_count++;
56+ endInsertRows();
57+ }
58+
59+ void removeLast()
60+ {
61+ beginRemoveRows(QModelIndex(), m_count - 1, m_count - 1);
62+ m_count--;
63+ endRemoveRows();
64+ }
65+
66+private:
67+ int m_count;
68+};
69+
70+int main(int argc, char *argv[])
71+{
72+ QGuiApplication a(argc, argv);
73+
74+ QQuickView *view = new QQuickView();
75+ view->setResizeMode(QQuickView::SizeRootObjectToView);
76+ view->engine()->addImportPath(BUILT_PLUGINS_DIR);
77+
78+ DummyModel model;
79+ model.setCount(3);
80+
81+ QStringListModel listModel;
82+ listModel.setStringList(QStringList() << QString());
83+
84+ view->rootContext()->setContextProperty("listModel", &listModel);
85+ view->rootContext()->setContextProperty("gridModel", &model);
86+
87+ view->setSource(QUrl::fromLocalFile(DASHVIEWSTEST_FOLDER "/organicgridtry.qml"));
88+
89+ view->show();
90+ view->resize(530, 400);
91+
92+ QObject::connect(view->rootObject(), SIGNAL(add()), &model, SLOT(addItem()));
93+ QObject::connect(view->rootObject(), SIGNAL(remove()), &model, SLOT(removeLast()));
94+
95+ return a.exec();
96+}
97+
98+#include "organicgridtry.moc"
tests/plugins/DashViews/organicgridtry.qmlnew file+94
@@ -0,0 +1,94 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+import QtQuick 2.1
18+import DashViews 0.1
19+
20+Item {
21+ id: root
22+ signal add()
23+ signal remove()
24+
25+ ListViewWithPageHeader {
26+ id: lvwph
27+ model: listModel
28+ height: parent.height
29+ width: parent.width - controls.width
30+
31+ delegate: OrganicGrid {
32+ id: grid
33+ model: gridModel
34+ smallDelegateSize: Qt.size(90, 90)
35+ bigDelegateSize: Qt.size(180, 180)
36+ columnSpacing: 10
37+ rowSpacing: 10
38+ width: parent.width
39+ height: implicitHeight > 100 ? implicitHeight : 100
40+ delegateCreationBegin: lvwph.contentY
41+ delegateCreationEnd: lvwph.contentY + lvwph.height
42+
43+ delegate: Rectangle {
44+ width: 100
45+ color: "red";
46+ border.width: 3
47+
48+ Text {
49+ text: index
50+ x: 10
51+ y: 10
52+ }
53+ }
54+ }
55+ }
56+
57+ Rectangle {
58+ id: controls
59+ height: parent.height
60+ width: 100
61+ anchors.right: parent.right
62+ color: "gray"
63+ opacity: 0.4
64+
65+ Rectangle {
66+ id: addButton
67+ height: 50
68+ width: parent.width
69+ color: "blue"
70+ Text {
71+ text: "Add"
72+ anchors.centerIn: parent
73+ }
74+ MouseArea {
75+ anchors.fill: parent
76+ onClicked: root.add()
77+ }
78+ }
79+ Rectangle {
80+ anchors.top: addButton.bottom
81+ height: 50
82+ width: parent.width
83+ color: "red"
84+ Text {
85+ text: "Remove"
86+ anchors.centerIn: parent
87+ }
88+ MouseArea {
89+ anchors.fill: parent
90+ onClicked: root.remove()
91+ }
92+ }
93+ }
94+}