KDE/kdevelop/lib/util

Adam Treat treat at kde.org
Tue Sep 13 22:23:04 UTC 2005


SVN commit 460433 by treat:

* Implement basic sorting for the KDevItemModel.  The implementation
is made with virtual methods so the sort can be changed.  Added a
sortingEnabled property to turn it off.  Default is set to on.
* Add kate modelines.

CCMAIL: kdevelop-devel at kdevelop.org


 M  +45 -1     kdevitemmodel.cpp  
 M  +43 -0     kdevitemmodel.h  


--- trunk/KDE/kdevelop/lib/util/kdevitemmodel.cpp #460432:460433
@@ -19,9 +19,12 @@
 
 #include "kdevitemmodel.h"
 
+#include <QtCore/QVector>
+
 KDevItemModel::KDevItemModel(QObject *parent)
   : QAbstractItemModel(parent),
-    m_collection(QString::fromUtf8("<root>"), 0)
+    m_collection(QString::fromUtf8("<root>"), 0),
+    m_sortingEnabled(true)
 {
 }
 
@@ -158,4 +161,45 @@
   return false;
 }
 
+void KDevItemModel::sort(int column, Qt::SortOrder order)
+{
+  Q_UNUSED(column);
+  if (!m_sortingEnabled)
+    return;
+  sortItems(&m_collection, order);
+  emit layoutChanged();
+}
+
+void KDevItemModel::sortItems(KDevItem *rootItem, Qt::SortOrder order)
+{
+  KDevItemCollection *collection = dynamic_cast<KDevItemCollection*>( rootItem );
+  if (!collection)
+    return;
+
+  // store the original order of indexes
+  QVector< QPair<KDevItem*, int> > sorting(collection->items().count());
+  for (int i = 0; i < sorting.count(); ++i)
+  {
+    sorting[i].first = collection->items().at(i);
+    sorting[i].second = i;
+  }
+
+  // do the sorting
+  LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
+  qSort(sorting.begin(), sorting.end(), compare);
+
+  // update the persistent indexes for the top level
+  for (int r = 0; r < sorting.count(); ++r)
+  {
+    KDevItem *item = sorting.at(r).first;
+    sortItems(item, order);
+    collection->replace(r, item);
+    QModelIndex from = createIndex(sorting.at(r).second, 0, item);
+    QModelIndex to = createIndex(r, 0, item);
+    changePersistentIndex(from, to);
+  }
+}
+
 #include "kdevitemmodel.moc"
+
+// kate: space-indent on; indent-width 2; tab-width 2; replace-tabs on
--- trunk/KDE/kdevelop/lib/util/kdevitemmodel.h #460432:460433
@@ -22,11 +22,15 @@
 
 #include <QtCore/QAbstractItemModel>
 #include <QtGui/QIcon>
+#include <QtCore/QPair>
 
 class KDevItem;
 class KDevItemGroup;
 class KDevItemCollection;
+class KDevItemModel;
 
+typedef bool(*LessThan) (const QPair<KDevItem*, int>&, const QPair<KDevItem*, int>&);
+
 class KDevItem
 {
 public:
@@ -46,6 +50,16 @@
   virtual QString toolTip() const = 0;
   virtual QString whatsThis() const = 0;
 
+  virtual bool operator<(const KDevItem &other) const
+  {
+    return name() < other.name();
+  }
+
+  virtual bool operator>(const KDevItem &other) const
+  {
+    return name() > other.name();
+  }
+
 private:
   KDevItemGroup *m_parent;
 };
@@ -78,6 +92,7 @@
   virtual QIcon icon() const { return QIcon(); }
   virtual QString toolTip() const { return QString(); }
   virtual QString whatsThis() const { return QString(); }
+  virtual const QList<KDevItem *> &items() const { return m_items; };
 
   virtual int itemCount() const { return m_items.count(); }
   virtual int indexOf(KDevItem *item) const { return m_items.indexOf(item); }
@@ -98,6 +113,13 @@
 
   void remove(int index) { m_items.removeAt(index); }
 
+  virtual void replace(int index, KDevItem *item)
+  {
+    Q_ASSERT(index >= 0);
+    Q_ASSERT(index < m_items.count());
+    m_items.replace(index, item);
+  }
+
 private:
   QString m_name;
   QList<KDevItem *> m_items;
@@ -106,6 +128,7 @@
 class KDevItemModel: public QAbstractItemModel
 {
   Q_OBJECT
+  Q_PROPERTY(bool sortingEnabled READ isSortingEnabled WRITE setSortingEnabled)
 public:
   explicit KDevItemModel(QObject *parent = 0);
   virtual ~KDevItemModel();
@@ -127,14 +150,34 @@
   virtual KDevItem *item(const QModelIndex &index) const;
   virtual QModelIndex indexOf(KDevItem *item) const;
 
+  bool isSortingEnabled() const { return m_sortingEnabled; }
+  virtual void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
+
 public slots:
   void refresh();
+  void setSortingEnabled( bool sortingEnabled ) { m_sortingEnabled = sortingEnabled; }
 
 protected:
   int positionOf(KDevItem *item) const;
+  virtual void sortItems( KDevItem *rootItem, Qt::SortOrder order = Qt::AscendingOrder );
 
+  static bool itemLessThan(const QPair<KDevItem*, int> &left,
+                           const QPair<KDevItem*, int> &right)
+  {
+    return * (left.first) < *(right.first);
+  }
+
+  static bool itemGreaterThan(const QPair<KDevItem*, int> &left,
+                              const QPair<KDevItem*, int> &right)
+  {
+    return * (left.first) > *(right.first);
+  }
+
 private:
   KDevItemCollection m_collection;
+  bool m_sortingEnabled;
 };
 
 #endif // KDEVITEMMODEL_H
+
+// kate: space-indent on; indent-width 2; tab-width 2; replace-tabs on




More information about the KDevelop-devel mailing list