[rkward-cvs] SF.net SVN: rkward: [2169] branches/KDE4_port/rkward

tfry at users.sourceforge.net tfry at users.sourceforge.net
Sun Nov 4 17:13:35 UTC 2007


Revision: 2169
          http://rkward.svn.sourceforge.net/rkward/?rev=2169&view=rev
Author:   tfry
Date:     2007-11-04 09:13:35 -0800 (Sun, 04 Nov 2007)

Log Message:
-----------
Make sorting and filtering work again in RKObjectListView

Modified Paths:
--------------
    branches/KDE4_port/rkward/core/rkmodificationtracker.cpp
    branches/KDE4_port/rkward/core/rkmodificationtracker.h
    branches/KDE4_port/rkward/misc/rkobjectlistview.cpp
    branches/KDE4_port/rkward/misc/rkobjectlistview.h
    branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp

Modified: branches/KDE4_port/rkward/core/rkmodificationtracker.cpp
===================================================================
--- branches/KDE4_port/rkward/core/rkmodificationtracker.cpp	2007-11-04 16:03:22 UTC (rev 2168)
+++ branches/KDE4_port/rkward/core/rkmodificationtracker.cpp	2007-11-04 17:13:35 UTC (rev 2169)
@@ -202,12 +202,6 @@
 
 ///////////////// RKObjectListModel ///////////////////////////
 
-#define COL_NAME 0
-#define COL_LABEL 1
-#define COL_TYPE 2
-#define COL_CLASS 3
-#define NUM_COLS (COL_CLASS + 1)
-
 RKObjectListModel::RKObjectListModel (QObject *parent) : QAbstractItemModel (parent) {
 	RK_TRACE (OBJECTS);
 }
@@ -256,7 +250,7 @@
 int RKObjectListModel::columnCount (const QModelIndex&) const {
 	//RK_TRACE (OBJECTS); // no need to trace this
 
-	return NUM_COLS;
+	return ColumnCount;
 }
 
 QVariant RKObjectListModel::data (const QModelIndex& index, int role) const {
@@ -265,22 +259,22 @@
 	int col = index.column ();
 	RObject *object = static_cast<RObject*> (index.internalPointer ());
 
-	if ((!object) || (col >= NUM_COLS)) {
+	if ((!object) || (col >= ColumnCount)) {
 		RK_ASSERT (false);
 		return QVariant ();
 	}
 
 	if (role == Qt::DisplayRole) {
-		if (col == COL_NAME) return object->getShortName ();
-		if (col == COL_LABEL) return object->getLabel ();
-		if (col == COL_TYPE) {
+		if (col == NameColumn) return object->getShortName ();
+		if (col == LabelColumn) return object->getLabel ();
+		if (col == TypeColumn) {
 			if (object->isVariable ()) return RObject::typeToText (object->getDataType ());
 			return QVariant ();
 		}
-		if (col == COL_CLASS) return object->makeClassString ("; ");
+		if (col == ClassColumn) return object->makeClassString ("; ");
 		RK_ASSERT (false);
 	} else if (role == Qt::DecorationRole) {
-		if (col == COL_NAME) return RKStandardIcons::iconForObject (object);
+		if (col == NameColumn) return RKStandardIcons::iconForObject (object);
 	} else if (role == Qt::ToolTipRole) {
 		return object->getObjectDescription ();
 	}
@@ -294,10 +288,10 @@
 	if (orientation != Qt::Horizontal) return QVariant ();
 	if (role != Qt::DisplayRole) return QVariant ();
 
-	if (section == COL_NAME) return i18n ("Name");
-	if (section == COL_LABEL) return i18n ("Label");
-	if (section == COL_TYPE) return i18n ("Type");
-	if (section == COL_CLASS) return i18n ("Class");
+	if (section == NameColumn) return i18n ("Name");
+	if (section == LabelColumn) return i18n ("Label");
+	if (section == TypeColumn) return i18n ("Type");
+	if (section == ClassColumn) return i18n ("Class");
 
 	RK_ASSERT (false);
 	return QVariant ();

Modified: branches/KDE4_port/rkward/core/rkmodificationtracker.h
===================================================================
--- branches/KDE4_port/rkward/core/rkmodificationtracker.h	2007-11-04 16:03:22 UTC (rev 2168)
+++ branches/KDE4_port/rkward/core/rkmodificationtracker.h	2007-11-04 17:13:35 UTC (rev 2169)
@@ -28,6 +28,14 @@
 
 /** An item model for the RObjectList . Technically this is the base class for RKModificationTracker. The two could be merged, fully, but this way, it's a little easier to see what belongs where, logically. */
 class RKObjectListModel : public QAbstractItemModel {
+public:
+	enum Column {
+		NameColumn=0,
+		LabelColumn,
+		TypeColumn,
+		ClassColumn,
+		ColumnCount = ClassColumn + 1
+	};
 protected:
 	RKObjectListModel (QObject *parent);
 	virtual ~RKObjectListModel ();

Modified: branches/KDE4_port/rkward/misc/rkobjectlistview.cpp
===================================================================
--- branches/KDE4_port/rkward/misc/rkobjectlistview.cpp	2007-11-04 16:03:22 UTC (rev 2168)
+++ branches/KDE4_port/rkward/misc/rkobjectlistview.cpp	2007-11-04 17:13:35 UTC (rev 2169)
@@ -37,17 +37,13 @@
 RKObjectListView::RKObjectListView (QWidget *parent) : QTreeView (parent) {
 	RK_TRACE (APP);
 
-// KDE4: TODO: sorting
-//	setSorting (100);
-	settings = new RKObjectListViewSettings ();
-	connect (settings, SIGNAL (settingsChanged ()), this, SLOT (objectBrowserSettingsChanged ()));
+	settings = new RKObjectListViewSettings (this);
+	setSortingEnabled (true);
 
 	menu = new Q3PopupMenu (this);
 	menu->insertItem (i18n ("Show Objects"), settings->showObjectsMenu ());
 	menu->insertItem (i18n ("Show Fields"), settings->showFieldsMenu ());
 	menu->insertItem (i18n ("Configure Defaults"), this, SLOT (popupConfigure ()));
-
-	objectBrowserSettingsChanged ();
 }
 
 RKObjectListView::~RKObjectListView () {
@@ -60,11 +56,13 @@
 	if (!object) return;
 	if (only_if_none_current && currentIndex ().isValid ()) return;
 
-	QModelIndex index = RKGlobals::tracker ()->indexFor (object);
+	QModelIndex index = settings->mapFromSource (RKGlobals::tracker ()->indexFor (object));
 	if (index.isValid ()) {
 		scrollTo (index);
 		setCurrentIndex (index);
 		resizeColumnToContents (0);
+	} else {
+		RK_ASSERT (false);
 	}
 }
 
@@ -80,7 +78,7 @@
 	RObject::ObjectList list;
 	QModelIndexList selected = selectedIndexes ();
 	for (int i = 0; i < selected.size (); ++i) {
-		QModelIndex index = selected[i];
+		QModelIndex index = settings->mapToSource (selected[i]);
 		if (index.column () != 0) continue;
 		if (!index.isValid ()) continue;
 		list.append (static_cast<RObject*> (index.internalPointer ()));
@@ -88,42 +86,6 @@
 	return list;
 }
 
-void RKObjectListView::objectBrowserSettingsChanged () {
-/*	setColumnWidthMode (0, Q3ListView::Maximum);
-	if (settings->settingActive (RKObjectListViewSettings::ShowFieldsLabel)) {
-		if (columnWidth (1) == 0) setColumnWidth (1, 50);
-		setColumnWidthMode (1, Q3ListView::Maximum);
-	} else {
-		setColumnWidthMode (1, Q3ListView::Manual);
-		hideColumn (1);
-	}
-
-	if (settings->settingActive (RKObjectListViewSettings::ShowFieldsType)) {
-		if (columnWidth (2) == 0) setColumnWidth (2, 50);
-		setColumnWidthMode (2, Q3ListView::Maximum);
-	} else {
-		setColumnWidthMode (2, Q3ListView::Manual);
-		hideColumn (2);
-	}
-
-	if (settings->settingActive (RKObjectListViewSettings::ShowFieldsClass)) {
-		if (columnWidth (3) == 0) setColumnWidth (3, 50);
-		setColumnWidthMode (3, Q3ListView::Maximum);
-	} else {
-		setColumnWidthMode (3, Q3ListView::Manual);
-		hideColumn (3);
-	}
-
-	triggerUpdate ();
-
-	for (Q3ListViewItemIterator it (this); it.current (); ++it) {
-		RObject *object = findItemObject (static_cast<RKListViewItem*> (it.current ()));
-		RK_ASSERT (object);
-
-		it.current ()->setVisible (settings->shouldShowObject (object));
-	} */
-}
-
 // KDE4 TODO: does this really need to be virtual?
 //virtual 
 void RKObjectListView::popupConfigure () {
@@ -148,12 +110,14 @@
 
 	setUniformRowHeights (true);		// KDE4: can we do this?
 
-	// KDE4: initialization logic is likely wrong, now.
-	setModel (RKGlobals::tracker ());
+	settings->setSourceModel (RKGlobals::tracker ());
+	setModel (settings);
 
-	setExpanded (RKGlobals::tracker ()->indexFor (RObjectList::getObjectList ()), true);
-	setExpanded (RKGlobals::tracker ()->indexFor (RObjectList::getGlobalEnv ()), true);
-	setMinimumHeight (rowHeight (RKGlobals::tracker ()->indexFor (RObjectList::getGlobalEnv ())) * 5);
+	QModelIndex genv = settings->mapFromSource (RKGlobals::tracker ()->indexFor (RObjectList::getGlobalEnv ()));
+	QModelIndex olist = settings->mapFromSource (RKGlobals::tracker ()->indexFor (RObjectList::getObjectList ()));
+	setExpanded (olist, true);
+	setExpanded (genv, true);
+	setMinimumHeight (rowHeight (genv) * 5);
 	resizeColumnToContents (0);
 
 	connect (RObjectList::getObjectList (), SIGNAL (updateComplete ()), this, SLOT (updateComplete ()));
@@ -168,19 +132,17 @@
 	RK_TRACE (APP);
 
 	setEnabled (true);
-	update_in_progress = false;
 }
 
 void RKObjectListView::updateStarted () {
 	RK_TRACE (APP);
 
 	setEnabled (false);
-	update_in_progress = true;
 }
 
 //////////////////// RKObjectListViewSettings //////////////////////////
 
-RKObjectListViewSettings::RKObjectListViewSettings () {
+RKObjectListViewSettings::RKObjectListViewSettings (QObject* parent) : QSortFilterProxyModel (parent) {
 	RK_TRACE (APP);
 
 	settings = new State[SettingsCount];
@@ -222,29 +184,75 @@
 	return (settings[setting] >= Yes);
 }
 
-bool RKObjectListViewSettings::shouldShowObject (RObject *object) {
+bool RKObjectListViewSettings::filterAcceptsColumn (int source_column, const QModelIndex&) const {
 	RK_TRACE (APP);
 
-	if (object->getShortName ().startsWith (".")) {
-		if ((!object->isType (RObject::GlobalEnv)) && (settings[ShowObjectsHidden] <= No)) return false;
+	if (source_column == RKObjectListModel::NameColumn) return true;
+	if (source_column == RKObjectListModel::LabelColumn) return (settings[ShowFieldsLabel] >= Yes);
+	if (source_column == RKObjectListModel::TypeColumn) return (settings[ShowFieldsType] >= Yes);
+	if (source_column == RKObjectListModel::ClassColumn) return (settings[ShowFieldsClass] >= Yes);
+
+	RK_ASSERT (false);
+	return false;
+}
+
+bool RKObjectListViewSettings::filterAcceptsRow (int source_row, const QModelIndex& source_parent) const {
+	RK_TRACE (APP);
+
+	// always show the root item
+	if (!source_parent.isValid ()) return true;
+
+	RObject* object = static_cast<RObject*> (source_parent.internalPointer ());
+	RK_ASSERT (object->isContainer ());
+	object = static_cast<RContainerObject*> (object)->findChildByIndex (source_row);
+	RK_ASSERT (object);
+
+	// always show the global evnt
+	if (object->isType (RObject::GlobalEnv)) return true;
+
+	if (settings[ShowObjectsHidden] <= No) {
+		if (object->getShortName ().startsWith ('.')) return false;
 	}
+
+	bool base_filter = QSortFilterProxyModel::filterAcceptsRow (source_row, source_parent);
+
 	if (object->isType (RObject::ToplevelEnv)) {
-		if (object->isType (RObject::GlobalEnv)) return true;
-		return (settings[ShowObjectsAllEnvironments] >= Yes);
+		return (base_filter && (settings[ShowObjectsAllEnvironments] >= Yes));
 	} else if (object->isType (RObject::Function)) {
-		return (settings[ShowObjectsFunction] >= Yes);
+		return (base_filter && (settings[ShowObjectsFunction] >= Yes));
 	} else if (object->isType (RObject::Container)) {
-		return (settings[ShowObjectsContainer] >= Yes);
+		return (base_filter && (settings[ShowObjectsContainer] >= Yes));
 	} else if (object->isVariable ()) {
-		return (settings[ShowObjectsVariable] >= Yes);
+		return (base_filter && (settings[ShowObjectsVariable] >= Yes));
 	}
-	return true;
+
+	return base_filter;
 }
 
+bool RKObjectListViewSettings::lessThan (const QModelIndex& left, const QModelIndex& right) const {
+	// don't trace this. Used in sorting
+
+	if (!(left.isValid () && right.isValid ())) return false;
+
+	RObject* left_object = static_cast<RObject*> (left.internalPointer ());
+	RObject* right_object = static_cast<RObject*> (right.internalPointer ());
+
+	// for top-level environments, always use the search order
+	if (left_object->isType (RObject::ToplevelEnv) && right_object->isType (RObject::ToplevelEnv)) {
+		RContainerObject* left_parent = left_object->getContainer ();
+		RContainerObject* right_parent = right_object->getContainer ();
+		if (!(left_parent && right_parent)) return false;
+
+		return (left_parent->getIndexOf (left_object) < right_parent->getIndexOf (right_object));
+	}
+
+	return (QSortFilterProxyModel::lessThan (left, right));
+}
+
 void RKObjectListViewSettings::createContextMenus () {
 	RK_TRACE (APP);
 
-	show_objects_menu = new Q3PopupMenu (0);
+	show_objects_menu = new QMenu (0);
 	insertPopupItem (show_objects_menu, ShowObjectsAllEnvironments, i18n ("All Environments"));
 	insertPopupItem (show_objects_menu, ShowObjectsContainer, i18n ("Objects with children"));
 	insertPopupItem (show_objects_menu, ShowObjectsVariable, i18n ("Variables"));
@@ -252,7 +260,7 @@
 	show_objects_menu->insertSeparator ();
 	insertPopupItem (show_objects_menu, ShowObjectsHidden, i18n ("Hidden Objects"));
 
-	show_fields_menu = new Q3PopupMenu (0);
+	show_fields_menu = new QMenu (0);
 	insertPopupItem (show_fields_menu, ShowFieldsType, i18n ("Type"));
 	insertPopupItem (show_fields_menu, ShowFieldsLabel, i18n ("Label"));
 	insertPopupItem (show_fields_menu, ShowFieldsClass, i18n ("Class"));
@@ -271,10 +279,11 @@
 		show_fields_menu->setItemEnabled (i, optionConfigurable ((Settings) i));
 	}
 
+	invalidateFilter ();
 	emit (settingsChanged ());
 }
 
-void RKObjectListViewSettings::insertPopupItem (Q3PopupMenu *menu, Settings setting, const QString &text) {
+void RKObjectListViewSettings::insertPopupItem (QMenu *menu, Settings setting, const QString &text) {
 	RK_TRACE (APP);
 
 	menu->insertItem (text, setting);

Modified: branches/KDE4_port/rkward/misc/rkobjectlistview.h
===================================================================
--- branches/KDE4_port/rkward/misc/rkobjectlistview.h	2007-11-04 16:03:22 UTC (rev 2168)
+++ branches/KDE4_port/rkward/misc/rkobjectlistview.h	2007-11-04 17:13:35 UTC (rev 2169)
@@ -18,6 +18,7 @@
 #define RKOBJECTLISTVIEW_H
 
 #include <QTreeView>
+#include <QSortFilterProxyModel>
 #include <qtooltip.h>
 #include <qmap.h>
 #include <Q3PopupMenu>
@@ -69,15 +70,10 @@
 	void updateStarted ();
 	void selectionChanged (const QItemSelection & selected, const QItemSelection & deselected);
 
-	void objectBrowserSettingsChanged ();
-
 	virtual void popupConfigure ();
 protected:
 	void contextMenuEvent (QContextMenuEvent* event);
 private:
-	bool update_in_progress;
-	bool changes;
-
 	Q3PopupMenu *menu;
 	RObject *menu_object;
 
@@ -85,11 +81,11 @@
 };
 
 /** Represents the filter/view settings possible for an RKListView. */
-class RKObjectListViewSettings : public QObject {
+class RKObjectListViewSettings : public QSortFilterProxyModel {
 	Q_OBJECT
 public:
 /** ctor. copies the default settings from RKSettingsModuleObjectBrowser */ 
-	RKObjectListViewSettings ();
+	RKObjectListViewSettings (QObject* parent=0);
 	~RKObjectListViewSettings ();
 
 	enum Settings {
@@ -118,22 +114,26 @@
 
 	bool shouldShowObject (RObject *object);
 
-	Q3PopupMenu *showObjectsMenu () { return show_objects_menu; };
-	Q3PopupMenu *showFieldsMenu () { return show_fields_menu; };
+	QMenu *showObjectsMenu () { return show_objects_menu; };
+	QMenu *showFieldsMenu () { return show_fields_menu; };
 signals:
 	void settingsChanged ();
 public slots:
 	void globalSettingsChanged (RKSettings::SettingsPage);
 	void toggleSetting (int which);
+protected:
+	bool filterAcceptsRow (int source_row, const QModelIndex& source_parent) const;
+	bool filterAcceptsColumn (int source_column, const QModelIndex& source_parent) const;
+	bool lessThan (const QModelIndex& left, const QModelIndex& right) const;
 private:
 	State *settings;
 	bool *settings_default;
-	void insertPopupItem (Q3PopupMenu *menu, Settings setting, const QString &text);
+	void insertPopupItem (QMenu *menu, Settings setting, const QString &text);
 	void createContextMenus ();
 	void updateSelf ();
 
-	Q3PopupMenu *show_objects_menu;
-	Q3PopupMenu *show_fields_menu;
+	QMenu *show_objects_menu;
+	QMenu *show_fields_menu;
 };
 
 #endif

Modified: branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp
===================================================================
--- branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp	2007-11-04 16:03:22 UTC (rev 2168)
+++ branches/KDE4_port/rkward/windows/rkcommandeditorwindow.cpp	2007-11-04 17:13:35 UTC (rev 2169)
@@ -539,6 +539,8 @@
 	current_symbol = symbol;
 
 	reset ();
+
+#warning deal with object removals while the completion model is active! Maybe cache all information?
 }
 
 void RKCodeCompletionModel::completionInvoked (KTextEditor::View*, const KTextEditor::Range&, InvocationType) {


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list