on xenial help sign in
Add a tryVerticalJournal were you can add/remove items
commit4abbad46b4578a6e89fa9e35f0dd60c190e4795b authorAlbert Astals <albert.astals@canonical.com> dateTue 17 Dec 2013, 17:35 (13 years ago) parentdda0506
5 files changed+225−1
plugins/DashViews/verticaljournal.cpp+26−1 plugins/DashViews/verticaljournal.h+4 tests/plugins/DashViews/CMakeLists.txt+5 tests/plugins/DashViews/verticaljournaltry.cpp+90 tests/plugins/DashViews/verticaljournaltry.qml+100
plugins/DashViews/verticaljournal.cpp+26−1
@@ -69,8 +69,18 @@ void VerticalJournal::setModel(QAbstractItemModel *model)
6969 if (model != this->model()) {
7070 if (!m_delegateModel) {
7171 createDelegateModel();
72+ } else {
73+#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
74+ disconnect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
75+ }
76+ m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
77+ connect(m_delegateModel, SIGNAL(modelUpdated(QQuickChangeSet,bool)), this, SLOT(onModelUpdated(QQuickChangeSet,bool)));
78+#else
79+ disconnect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
7280 }
7381 m_delegateModel->setModel(QVariant::fromValue<QAbstractItemModel *>(model));
82+ connect(m_delegateModel, SIGNAL(modelUpdated(QQmlChangeSet,bool)), this, SLOT(onModelUpdated(QQmlChangeSet,bool)));
83+#endif
7484
7585 cleanupExistingItems();
7686
@@ -477,7 +487,7 @@ void VerticalJournal::itemCreated(int modelIndex, QObject *object)
477487{
478488 QQuickItem *item = qmlobject_cast<QQuickItem*>(object);
479489 if (!item) {
480− qWarning() << "ListViewWithPageHeader::itemCreated got a non item for index" << modelIndex;
490+ qWarning() << "VerticalJournal::itemCreated got a non item for index" << modelIndex;
481491 return;
482492 }
483493#endif
@@ -489,6 +499,21 @@ void VerticalJournal::itemCreated(int modelIndex, QObject *object)
489499 }
490500}
491501
502+#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
503+void VerticalJournal::onModelUpdated(const QQuickChangeSet & /*changeSet*/, bool reset)
504+#else
505+void VerticalJournal::onModelUpdated(const QQmlChangeSet & /*changeSet*/, bool reset)
506+#endif
507+{
508+ // TODO Add support for additions/removals at end that are not reset calls
509+ // TODO Add a test for reset
510+ if (reset) {
511+ cleanupExistingItems();
512+ }
513+ polish();
514+}
515+
516+
492517void VerticalJournal::relayout()
493518{
494519 m_needsRelayout = true;
plugins/DashViews/verticaljournal.h+4
@@ -22,8 +22,10 @@
2222class QAbstractItemModel;
2323class QQmlComponent;
2424#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
25+class QQuickChangeSet;
2526class QQuickVisualDataModel;
2627#else
28+class QQmlChangeSet;
2729class QQmlDelegateModel;
2830#endif
2931
@@ -120,8 +122,10 @@ protected:
120122private Q_SLOTS:
121123#if (QT_VERSION < QT_VERSION_CHECK(5, 1, 0))
122124 void itemCreated(int modelIndex, QQuickItem *item);
125+ void onModelUpdated(const QQuickChangeSet &changeSet, bool reset);
123126#else
124127 void itemCreated(int modelIndex, QObject *object);
128+ void onModelUpdated(const QQmlChangeSet &changeSet, bool reset);
125129#endif
126130 void relayout();
127131 void onHeightChanged();
tests/plugins/DashViews/CMakeLists.txt+5
@@ -45,6 +45,11 @@ qt5_use_modules(verticaljournaltestExec Test Core Qml)
4545target_link_libraries(verticaljournaltestExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})
4646add_custom_target(testVerticalJournal ${CMAKE_CURRENT_BINARY_DIR}/verticaljournaltestExec -o ${CMAKE_BINARY_DIR}/testVerticalJournal.xml,xunitxml -o -,txt)
4747
48+add_executable(verticaljournaltryExec verticaljournaltry.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../plugins/DashViews/verticaljournal.cpp)
49+qt5_use_modules(verticaljournaltryExec Test Core Qml)
50+target_link_libraries(verticaljournaltryExec ${Qt5Gui_LIBRARIES} ${Qt5Quick_LIBRARIES})
51+add_custom_target(tryVerticalJournal ${CMAKE_CURRENT_BINARY_DIR}/verticaljournaltryExec -o ${CMAKE_BINARY_DIR}/testVerticalJournal.xml,xunitxml -o -,txt)
52+
4853set(qmltest_DEFAULT_NO_ADD_TEST TRUE)
4954add_qml_test(. listviewwithpageheaderqml IMPORT_PATHS ${CMAKE_BINARY_DIR}/plugins)
5055
tests/plugins/DashViews/verticaljournaltry.cppnew file+90
@@ -0,0 +1,90 @@
1+/*
2+ * Copyright (C) 2013 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 QHeightModel : public QStringListModel {
30+ Q_OBJECT
31+public:
32+ QHash<int, QByteArray> roleNames() const
33+ {
34+ QHash<int, QByteArray> roles;
35+ roles.insert(Qt::DisplayRole, "modelHeight");
36+ return roles;
37+ }
38+
39+public slots:
40+ void add(int height)
41+ {
42+ if (height <= 0) {
43+ height = 50 + 50. * qrand() / RAND_MAX ;
44+ }
45+ QStringList sl = stringList();
46+ sl << QString::number(height);
47+ setStringList(sl);
48+ }
49+
50+ void remove()
51+ {
52+ QStringList sl = stringList();
53+ if (!sl.isEmpty()) {
54+ sl.removeLast();
55+ setStringList(sl);
56+ }
57+ }
58+};
59+
60+int main(int argc, char *argv[])
61+{
62+ QGuiApplication a(argc, argv);
63+
64+ QQuickView *view = new QQuickView();
65+ view->setResizeMode(QQuickView::SizeRootObjectToView);
66+ view->engine()->addImportPath(BUILT_PLUGINS_DIR);
67+
68+ QHeightModel model;
69+ QStringList heightList;
70+ heightList << "100" << "50" << "125";
71+ model.setStringList(heightList);
72+
73+ QStringListModel listModel;
74+ listModel.setStringList(QStringList() << QString());
75+
76+ view->rootContext()->setContextProperty("listModel", &listModel);
77+ view->rootContext()->setContextProperty("vjModel", &model);
78+
79+ view->setSource(QUrl::fromLocalFile(DASHVIEWSTEST_FOLDER "/verticaljournaltry.qml"));
80+
81+ view->show();
82+ view->resize(530, 400);
83+
84+ QObject::connect(view->rootObject(), SIGNAL(add(int)), &model, SLOT(add(int)));
85+ QObject::connect(view->rootObject(), SIGNAL(remove()), &model, SLOT(remove()));
86+
87+ return a.exec();
88+}
89+
90+#include "verticaljournaltry.moc"
tests/plugins/DashViews/verticaljournaltry.qmlnew file+100
@@ -0,0 +1,100 @@
1+/*
2+ * Copyright (C) 2013 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(int height)
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: VerticalJournal {
32+ id: vj
33+ model: vjModel
34+ columnWidth: 100
35+ columnSpacing: 10
36+ rowSpacing: 10
37+ width: parent.width
38+ height: implicitHeight > 100 ? implicitHeight : 100
39+ delegateCreationBegin: lvwph.contentY
40+ delegateCreationEnd: lvwph.contentY + lvwph.height
41+
42+ delegate: Rectangle {
43+ width: 100
44+ color: "red";
45+ height: modelHeight
46+ border.width: 3
47+
48+ Text {
49+ text: index + "\ny: " + parent.y + "\nheight: " + parent.height
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(addField.text)
77+ }
78+ }
79+ TextInput {
80+ id: addField
81+ anchors.top: addButton.bottom
82+ height: 30
83+ width: parent.width
84+ }
85+ Rectangle {
86+ anchors.top: addField.bottom
87+ height: 50
88+ width: parent.width
89+ color: "red"
90+ Text {
91+ text: "Remove"
92+ anchors.centerIn: parent
93+ }
94+ MouseArea {
95+ anchors.fill: parent
96+ onClicked: root.remove()
97+ }
98+ }
99+ }
100+}