[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    extragear/network/kmldonkey/kmldonkey
From:       Aleksey Markelov <markelovai () gmail ! com>
Date:       2009-08-29 8:27:34
Message-ID: 1251534454.722423.16692.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1016873 by amarkelov:

AbstractInfoModel sorting wrapper, id lookup

 M  +19 -4     abstractinfomodel.cpp  
 M  +19 -2     abstractinfomodel.h  


--- trunk/extragear/network/kmldonkey/kmldonkey/abstractinfomodel.cpp #1016872:1016873
@@ -34,12 +34,14 @@
 QVariant InfoItem::data(int column, int role) const
 {
     Q_ASSERT(column < m_data.size());
+    if (role == Qt::EditRole) role = Qt::DisplayRole;
 
     const CellData &cellData = m_data.at(column);
     for (int i = 0; i < cellData.count(); ++i) {
         const RoleData &roleData = cellData.at(i);
         if (roleData.first == role) return roleData.second;
     }
+    if (role == IdRole) return id;
     return QVariant();
 }
 
@@ -52,7 +54,10 @@
     for  (int i = 0; i < cellData.count(); ++i) {
         RoleData &roleData = cellData[i];
         if (roleData.first != role) continue;
-        if (roleData.second != data) {
+        if (data.isNull()) {
+            cellData.remove(i);
+            updatedColumns |= 0x01<<column;
+        } else if (roleData.second.type() != data.type() || roleData.second != data) {
             roleData.second = data;
             updatedColumns |= 0x01<<column;
         }
@@ -169,10 +174,20 @@
     return item->data(index.column(), role);
 }
 
-InfoItem *AbstractInfoModel::itemAt(const QModelIndex &index) const
+InfoItem *AbstractInfoModel::itemAt(int row) const
 {
-    Q_ASSERT(index.row() < rowCount() && index.column() < columnCount());
-    return itemsList.at(index.row());
+    Q_ASSERT(row < rowCount());
+    return itemsList.at(row);
 }
 
+bool InfoSortFilterProxyModel::lessThan(const QModelIndex &left,const QModelIndex &right) const
+{
+    bool less = QSortFilterProxyModel::lessThan(left, right);
+    if (less) return true;
+    bool more = QSortFilterProxyModel::lessThan(right,left);
+    if (more) return false;
+    //this way there will be no equal items and sorting will be stable
+    return left.data(InfoItem::IdRole).toInt() < right.data(InfoItem::IdRole).toInt();
+}
+
 } // namespace Q4
--- trunk/extragear/network/kmldonkey/kmldonkey/abstractinfomodel.h #1016872:1016873
@@ -24,6 +24,7 @@
 #define KMLDONKEY_ABSTRACTINFOMODEL_H
 
 #include <QAbstractTableModel>
+#include <QSortFilterProxyModel>
 #include <QVector>
 #include <QPair>
 
@@ -35,7 +36,10 @@
 class InfoItem
 {
 public:
-    enum { SortRole = Qt::UserRole + 1 };
+    enum {
+        IdRole = Qt::UserRole,
+        SortRole = Qt::UserRole + 1
+    };
 
     InfoItem(int newid) : id(newid) {}
     virtual ~InfoItem() {}
@@ -72,6 +76,9 @@
     void dropCache();
 
     void clipboardAction(const QString &action, QItemSelectionModel *smodel);
+    InfoItem *itemAt(const QModelIndex &index) const { return itemAt(index.row()); }
+    InfoItem *itemAt(int row) const;
+    InfoItem *findItemById(int id) const { return itemsHash.value(id); }
 protected slots:
     void removeItem(int i);
     void updateItem(int i);
@@ -80,7 +87,6 @@
     virtual void dropCacheImp() {}
     virtual QStringList headerLabels() const = 0;
     virtual InfoItem *prepareNewItem(int num) = 0;
-    InfoItem *itemAt(const QModelIndex &index) const;
 private:
     QVector<QVariant> headers;
     typedef QHash<int, InfoItem *> InfoItemHash;
@@ -89,6 +95,17 @@
     InfoItemList itemsList;
 };
 
+/** 
+ * This class should solve sorting stability problem with QSortFilterProxyModel
+ * and dynamicSortFilter enabled by comparing equal items by id's of InfoItem
+ */
+class InfoSortFilterProxyModel : public QSortFilterProxyModel
+{
+public:
+    InfoSortFilterProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
+    bool lessThan(const QModelIndex &left,const QModelIndex &right) const;
+};
+
 } // namespace Q4
 
 
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic