[Ktutorial-commits] SF.net SVN: ktutorial:[221] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-03-30 19:50:30
|
Revision: 221
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=221&view=rev
Author: danxuliu
Date: 2010-03-30 19:50:23 +0000 (Tue, 30 Mar 2010)
Log Message:
-----------
Add AutoExpandableTreeView that expands the items when they are added or modified in the model
Modified Paths:
--------------
trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt
trunk/ktutorial/ktutorial-editor/src/view/WaitForWidget.ui
trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt
Added Paths:
-----------
trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.cpp
trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.h
trunk/ktutorial/ktutorial-editor/tests/unit/view/AutoExpandableTreeViewTest.cpp
Modified: trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-30 07:25:48 UTC (rev 220)
+++ trunk/ktutorial/ktutorial-editor/src/KTutorialEditor.cpp 2010-03-30 19:50:23 UTC (rev 221)
@@ -39,6 +39,7 @@
#include "data/Tutorial.h"
#include "serialization/Serialization.h"
#include "view/ActionListWidget.h"
+#include "view/AutoExpandableTreeView.h"
#include "view/EditionDialog.h"
#include "view/LicenseWidget.h"
#include "view/ReactionWidget.h"
@@ -57,7 +58,7 @@
mCurrentStep(0),
mCurrentReaction(0) {
- mTreeView = new QTreeView();
+ mTreeView = new AutoExpandableTreeView();
mTreeView->setObjectName("centralTreeView");
setCentralWidget(mTreeView);
Added: trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.cpp 2010-03-30 19:50:23 UTC (rev 221)
@@ -0,0 +1,64 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include "AutoExpandableTreeView.h"
+
+//public:
+
+AutoExpandableTreeView::AutoExpandableTreeView(QWidget* parent):
+ QTreeView(parent) {
+}
+
+//protected:
+
+void AutoExpandableTreeView::dataChanged(const QModelIndex& topLeft,
+ const QModelIndex& bottomRight) {
+ QTreeView::dataChanged(topLeft, bottomRight);
+
+ expandParents(topLeft);
+}
+
+void AutoExpandableTreeView::rowsInserted(const QModelIndex& parent, int start,
+ int end) {
+ QTreeView::rowsInserted(parent, start, end);
+
+ expand(parent);
+ expandParents(parent);
+
+ for (int i=start; i<=end; ++i) {
+ expandRecursive(model()->index(i, 0, parent));
+ }
+}
+
+//private:
+
+void AutoExpandableTreeView::expandParents(const QModelIndex& index) {
+ QModelIndex parent = index.parent();
+ while (parent.isValid()) {
+ expand(parent);
+ parent = parent.parent();
+ }
+}
+
+void AutoExpandableTreeView::expandRecursive(const QModelIndex& index) {
+ expand(index);
+
+ for (int i=0; i<model()->rowCount(index); ++i) {
+ expandRecursive(model()->index(i, 0, index));
+ }
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.h 2010-03-30 19:50:23 UTC (rev 221)
@@ -0,0 +1,92 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#ifndef AUTOEXPANDABLETREEVIEW_H
+#define AUTOEXPANDABLETREEVIEW_H
+
+#include <QTreeView>
+
+/**
+ * Special QTreeView that expands its item when are inserted or changed.
+ * When an item is inserted, that item, all its children and all its parents are
+ * expanded.
+ * When an item is changed, the parents to that item are expanded. Note that
+ * neither the item nor its children are expanded.
+ *
+ * Siblings of parent items are not expanded in any case. Only the parent
+ * leading to the item to be expanded are expanded.
+ */
+class AutoExpandableTreeView: public QTreeView {
+Q_OBJECT
+public:
+
+ /**
+ * Creates a new AutoExpandableTreeView with the given parent.
+ *
+ * @param parent The parent widget.
+ */
+ AutoExpandableTreeView(QWidget* parent = 0);
+
+protected:
+
+ /**
+ * This slot is called when items are changed in the model.
+ * After executing QTreeView default behavior, all the parents of the
+ * changed items are expanded. All the items should belong to the same
+ * parent.
+ *
+ * Reimplemented from QTreeView::dataChanged(QModelIndex, QModelIndex).
+ *
+ * @param topLeft The top left item changed, inclusive.
+ * @param bottomRight The bottom right item changed, inclusiev.
+ */
+ virtual void dataChanged(const QModelIndex& topLeft,
+ const QModelIndex& bottomRight);
+
+ /**
+ * This slot is called when rows are inserted.
+ * After executing QTreeView default behavior, all the items inserted, all
+ * their children and their parent are expanded.
+ *
+ * Reimplemented from QTreeView::rowsInserted(QModelIndex, int, int).
+ *
+ * @param parent The parent index where the children were added.
+ * @param start The first row inserted, inclusive.
+ * @param end The last row inserted, inclusive.
+ */
+ virtual void rowsInserted(const QModelIndex& parent, int start, int end);
+
+private:
+
+ /**
+ * Expands all the parents of the given index.
+ *
+ * @param index The index to expand its parents.
+ */
+ void expandParents(const QModelIndex& index);
+
+ /**
+ * Expands an index and all its children.
+ *
+ * @param index The index to expand.
+ */
+ void expandRecursive(const QModelIndex& index);
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-editor/src/view/AutoExpandableTreeView.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-30 07:25:48 UTC (rev 220)
+++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-30 19:50:23 UTC (rev 221)
@@ -1,5 +1,6 @@
set(ktutorial_editor_view_SRCS
ActionListWidget.cpp
+ AutoExpandableTreeView.cpp
CommandWidget.cpp
EditionDialog.cpp
EditionWidget.cpp
Modified: trunk/ktutorial/ktutorial-editor/src/view/WaitForWidget.ui
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/WaitForWidget.ui 2010-03-30 07:25:48 UTC (rev 220)
+++ trunk/ktutorial/ktutorial-editor/src/view/WaitForWidget.ui 2010-03-30 19:50:23 UTC (rev 221)
@@ -19,7 +19,7 @@
</property>
<layout class="QVBoxLayout" name="waitForWidgetVerticalLayout">
<item>
- <widget class="QTreeView" name="waitForTreeView"/>
+ <widget class="AutoExpandableTreeView" name="waitForTreeView"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
@@ -74,6 +74,11 @@
<extends>QPushButton</extends>
<header>kpushbutton.h</header>
</customwidget>
+ <customwidget>
+ <class>AutoExpandableTreeView</class>
+ <extends>QTreeView</extends>
+ <header>AutoExpandableTreeView.h</header>
+ </customwidget>
</customwidgets>
<resources/>
<connections/>
Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/AutoExpandableTreeViewTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/view/AutoExpandableTreeViewTest.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/view/AutoExpandableTreeViewTest.cpp 2010-03-30 19:50:23 UTC (rev 221)
@@ -0,0 +1,223 @@
+/***************************************************************************
+ * Copyright (C) 2010 by Daniel Calviño Sánchez *
+ * dan...@gm... *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 3 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; If not, see <http://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include <QtTest>
+
+#include "AutoExpandableTreeView.h"
+
+#include "TextTreeItem.h"
+#include "TreeModel.h"
+
+class AutoExpandableTreeViewTest: public QObject {
+Q_OBJECT
+
+private slots:
+
+ void testConstructor();
+
+ void testInsertRootItemChild();
+ void testInsertDeepNestedChildParentExpanded();
+ void testInsertDeepNestedChildParentNotExpanded();
+
+ void testChangeDeepNestedChild();
+
+};
+
+void AutoExpandableTreeViewTest::testConstructor() {
+ QWidget parent;
+ AutoExpandableTreeView* view = new AutoExpandableTreeView(&parent);
+
+ QCOMPARE(view->parentWidget(), &parent);
+}
+
+void AutoExpandableTreeViewTest::testInsertRootItemChild() {
+ AutoExpandableTreeView view;
+ TextTreeItem* rootItem = new TextTreeItem();
+ TreeModel model(rootItem);
+ view.setModel(&model);
+
+ TextTreeItem* item1 = new TextTreeItem(rootItem);
+ TextTreeItem* item1_1 = new TextTreeItem(item1);
+ TextTreeItem* item1_1_1 = new TextTreeItem(item1_1);
+ item1_1->appendChild(item1_1_1);
+ item1->appendChild(item1_1);
+ TextTreeItem* item1_2 = new TextTreeItem(item1);
+ TextTreeItem* item1_2_1 = new TextTreeItem(item1_2);
+ item1_2->appendChild(item1_2_1);
+ TextTreeItem* item1_2_2 = new TextTreeItem(item1_2);
+ TextTreeItem* item1_2_2_1 = new TextTreeItem(item1_2_2);
+ item1_2_2->appendChild(item1_2_2_1);
+ TextTreeItem* item1_2_2_2 = new TextTreeItem(item1_2_2);
+ item1_2_2->appendChild(item1_2_2_2);
+ item1_2->appendChild(item1_2_2);
+ item1->appendChild(item1_2);
+
+ rootItem->appendChild(item1);
+
+ QModelIndex index1 = model.index(0, 0);
+ QVERIFY(view.isExpanded(index1));
+ QModelIndex index1_1 = model.index(0, 0, index1);
+ QVERIFY(view.isExpanded(index1_1));
+ QModelIndex index1_2 = model.index(1, 0, index1);
+ QVERIFY(view.isExpanded(index1_2));
+ QModelIndex index1_2_2 = model.index(1, 0, index1_2);
+ QVERIFY(view.isExpanded(index1_2_2));
+}
+
+void AutoExpandableTreeViewTest::testInsertDeepNestedChildParentExpanded() {
+ AutoExpandableTreeView view;
+ TextTreeItem* rootItem = new TextTreeItem();
+ TreeModel model(rootItem);
+ view.setModel(&model);
+
+ TextTreeItem* item1 = new TextTreeItem(rootItem);
+ TextTreeItem* item1_1 = new TextTreeItem(item1);
+ TextTreeItem* item1_1_1 = new TextTreeItem(item1_1);
+ item1_1->appendChild(item1_1_1);
+ item1->appendChild(item1_1);
+ TextTreeItem* item1_2 = new TextTreeItem(item1);
+ TextTreeItem* item1_2_1 = new TextTreeItem(item1_2);
+ item1_2->appendChild(item1_2_1);
+ item1->appendChild(item1_2);
+ rootItem->appendChild(item1);
+
+ QModelIndex index1 = model.index(0, 0);
+ view.setExpanded(index1, true);
+ //Collapse sibling of parent to check that it is not expanded again
+ QModelIndex index1_1 = model.index(0, 0, index1);
+ view.setExpanded(index1_1, false);
+ QModelIndex index1_2 = model.index(1, 0, index1);
+ view.setExpanded(index1_2, true);
+
+ TextTreeItem* item1_2_2 = new TextTreeItem(item1_2);
+ TextTreeItem* item1_2_2_1 = new TextTreeItem(item1_2_2);
+ TextTreeItem* item1_2_2_1_1 = new TextTreeItem(item1_2_2_1);
+ item1_2_2_1->appendChild(item1_2_2_1_1);
+ TextTreeItem* item1_2_2_1_2 = new TextTreeItem(item1_2_2_1);
+ item1_2_2_1->appendChild(item1_2_2_1_2);
+ item1_2_2->appendChild(item1_2_2_1);
+ TextTreeItem* item1_2_2_2 = new TextTreeItem(item1_2_2);
+ item1_2_2->appendChild(item1_2_2_2);
+ item1_2->appendChild(item1_2_2);
+
+ QVERIFY(view.isExpanded(index1));
+ QVERIFY(!view.isExpanded(index1_1));
+ QVERIFY(view.isExpanded(index1_2));
+ QModelIndex index1_2_2 = model.index(1, 0, index1_2);
+ QVERIFY(view.isExpanded(index1_2_2));
+ QModelIndex index1_2_2_1 = model.index(0, 0, index1_2_2);
+ QVERIFY(view.isExpanded(index1_2_2_1));
+}
+
+void AutoExpandableTreeViewTest::testInsertDeepNestedChildParentNotExpanded() {
+ AutoExpandableTreeView view;
+ TextTreeItem* rootItem = new TextTreeItem();
+ TreeModel model(rootItem);
+ view.setModel(&model);
+
+ TextTreeItem* item1 = new TextTreeItem(rootItem);
+ TextTreeItem* item1_1 = new TextTreeItem(item1);
+ TextTreeItem* item1_1_1 = new TextTreeItem(item1_1);
+ item1_1->appendChild(item1_1_1);
+ item1->appendChild(item1_1);
+ TextTreeItem* item1_2 = new TextTreeItem(item1);
+ TextTreeItem* item1_2_1 = new TextTreeItem(item1_2);
+ item1_2->appendChild(item1_2_1);
+ item1->appendChild(item1_2);
+ rootItem->appendChild(item1);
+
+ QModelIndex index1 = model.index(0, 0);
+ view.setExpanded(index1, false);
+ //Collapse sibling of parent to check that it is not expanded again
+ QModelIndex index1_1 = model.index(0, 0, index1);
+ view.setExpanded(index1_1, false);
+ QModelIndex index1_2 = model.index(1, 0, index1);
+ view.setExpanded(index1_2, false);
+
+ TextTreeItem* item1_2_2 = new TextTreeItem(item1_2);
+ TextTreeItem* item1_2_2_1 = new TextTreeItem(item1_2_2);
+ TextTreeItem* item1_2_2_1_1 = new TextTreeItem(item1_2_2_1);
+ item1_2_2_1->appendChild(item1_2_2_1_1);
+ TextTreeItem* item1_2_2_1_2 = new TextTreeItem(item1_2_2_1);
+ item1_2_2_1->appendChild(item1_2_2_1_2);
+ item1_2_2->appendChild(item1_2_2_1);
+ TextTreeItem* item1_2_2_2 = new TextTreeItem(item1_2_2);
+ item1_2_2->appendChild(item1_2_2_2);
+
+ item1_2->appendChild(item1_2_2);
+
+ QVERIFY(view.isExpanded(index1));
+ QVERIFY(!view.isExpanded(index1_1));
+ QVERIFY(view.isExpanded(index1_2));
+ QModelIndex index1_2_2 = model.index(1, 0, index1_2);
+ QVERIFY(view.isExpanded(index1_2_2));
+ QModelIndex index1_2_2_1 = model.index(0, 0, index1_2_2);
+ QVERIFY(view.isExpanded(index1_2_2_1));
+}
+
+void AutoExpandableTreeViewTest::testChangeDeepNestedChild() {
+ AutoExpandableTreeView view;
+ TextTreeItem* rootItem = new TextTreeItem();
+ TreeModel model(rootItem);
+ view.setModel(&model);
+
+ TextTreeItem* item1 = new TextTreeItem(rootItem);
+ TextTreeItem* item1_1 = new TextTreeItem(item1);
+ TextTreeItem* item1_1_1 = new TextTreeItem(item1_1);
+ item1_1->appendChild(item1_1_1);
+ item1->appendChild(item1_1);
+ TextTreeItem* item1_2 = new TextTreeItem(item1);
+ TextTreeItem* item1_2_1 = new TextTreeItem(item1_2);
+ item1_2->appendChild(item1_2_1);
+ TextTreeItem* item1_2_2 = new TextTreeItem(item1_2);
+ TextTreeItem* item1_2_2_1 = new TextTreeItem(item1_2_2);
+ TextTreeItem* item1_2_2_1_1 = new TextTreeItem(item1_2_2_1);
+ item1_2_2_1->appendChild(item1_2_2_1_1);
+ TextTreeItem* item1_2_2_1_2 = new TextTreeItem(item1_2_2_1);
+ item1_2_2_1->appendChild(item1_2_2_1_2);
+ item1_2_2->appendChild(item1_2_2_1);
+ TextTreeItem* item1_2_2_2 = new TextTreeItem(item1_2_2);
+ item1_2_2->appendChild(item1_2_2_2);
+ item1_2->appendChild(item1_2_2);
+ item1->appendChild(item1_2);
+ rootItem->appendChild(item1);
+
+ QModelIndex index1 = model.index(0, 0);
+ view.setExpanded(index1, false);
+ //Collapse sibling of parent to check that it is not expanded again
+ QModelIndex index1_1 = model.index(0, 0, index1);
+ view.setExpanded(index1_1, false);
+ QModelIndex index1_2 = model.index(1, 0, index1);
+ view.setExpanded(index1_2, false);
+ QModelIndex index1_2_2 = model.index(1, 0, index1_2);
+ view.setExpanded(index1_2_2, false);
+ QModelIndex index1_2_2_1 = model.index(0, 0, index1_2_2);
+ view.setExpanded(index1_2_2_1, false);
+
+ item1_2_2_1->setText("New text");
+
+ QVERIFY(view.isExpanded(index1));
+ QVERIFY(!view.isExpanded(index1_1));
+ QVERIFY(view.isExpanded(index1_2));
+ QVERIFY(view.isExpanded(index1_2_2));
+ QVERIFY(!view.isExpanded(index1_2_2_1));
+}
+
+QTEST_MAIN(AutoExpandableTreeViewTest)
+
+#include "AutoExpandableTreeViewTest.moc"
Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/AutoExpandableTreeViewTest.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-30 07:25:48 UTC (rev 220)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-30 19:50:23 UTC (rev 221)
@@ -18,6 +18,7 @@
unit_tests(
ActionListWidget
+ AutoExpandableTreeView
CommandWidget
EditionDialog
LicenseWidget
@@ -53,6 +54,7 @@
mem_tests(
ActionListWidget
+ AutoExpandableTreeView
CommandWidget
EditionDialog
LicenseWidget
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|