changeset 428:3e1d190048f4

* a bit of progress on retaining current row when sorting changes &c
author Chris Cannam
date Tue, 17 Jun 2008 16:07:56 +0000
parents 72ec275e458b
children 6036b38177e2
files data/model/ModelDataTableModel.cpp data/model/ModelDataTableModel.h
diffstat 2 files changed, 50 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/data/model/ModelDataTableModel.cpp	Mon Jun 16 14:48:42 2008 +0000
+++ b/data/model/ModelDataTableModel.cpp	Tue Jun 17 16:07:56 2008 +0000
@@ -25,7 +25,8 @@
 ModelDataTableModel::ModelDataTableModel(TabularModel *m) :
     m_model(m),
     m_sortColumn(0),
-    m_sortOrdering(Qt::AscendingOrder)
+    m_sortOrdering(Qt::AscendingOrder),
+    m_currentRow(0)
 {
     Model *baseModel = dynamic_cast<Model *>(m);
 
@@ -156,23 +157,35 @@
     return m_model->getFrameForRow(getUnsorted(index.row()));
 }
 
+QModelIndex
+ModelDataTableModel::getModelIndexForRow(int row) const
+{
+    return createIndex(row, 0, 0);
+}
+
 void
 ModelDataTableModel::sort(int column, Qt::SortOrder sortOrder)
 {
     std::cerr << "ModelDataTableModel::sort(" << column << ", " << sortOrder
               << ")" << std::endl;
+    int prevCurrent = getCurrentRow();
     if (m_sortColumn != column) {
-        m_sort.clear();
+        clearSort();
     }
     m_sortColumn = column;
     m_sortOrdering = sortOrder;
+    int current = getCurrentRow();
+    if (current != prevCurrent) {
+         std::cerr << "Current row changed from " << prevCurrent << " to " << current << " for underlying row " << m_currentRow << std::endl;
+         emit currentChanged(createIndex(current, 0, 0));
+    }
     emit layoutChanged();
 }
 
 void
 ModelDataTableModel::modelChanged()
 {
-    m_sort.clear();
+    clearSort();
     emit layoutChanged();
 }
 
@@ -180,7 +193,7 @@
 ModelDataTableModel::modelChanged(size_t f0, size_t f1)
 {
     //!!! inefficient
-    m_sort.clear();
+    clearSort();
     emit layoutChanged();
 }
 
@@ -306,3 +319,28 @@
     // rsort now maps from sorted row number to original row number
 }
 
+int
+ModelDataTableModel::getCurrentRow()
+{
+    return getSorted(m_currentRow);
+}
+
+void
+ModelDataTableModel::setCurrentRow(int row)
+{
+    m_currentRow = getUnsorted(row);
+}
+
+void
+ModelDataTableModel::clearSort()
+{
+//    int prevCurrent = getCurrentRow();
+    m_sort.clear();
+//    int current = getCurrentRow(); //!!! no --  not until the sort criteria have changed
+//    if (current != prevCurrent) {
+//        std::cerr << "Current row changed from " << prevCurrent << " to " << current << " for underlying row " << m_currentRow << std::endl;
+//        emit currentRowChanged(createIndex(current, 0, 0));
+//    }
+}
+
+    
--- a/data/model/ModelDataTableModel.h	Mon Jun 16 14:48:42 2008 +0000
+++ b/data/model/ModelDataTableModel.h	Tue Jun 17 16:07:56 2008 +0000
@@ -54,11 +54,17 @@
     QModelIndex getModelIndexForFrame(size_t frame) const;
     size_t getFrameForModelIndex(const QModelIndex &) const;
 
+    QModelIndex getModelIndexForRow(int row) const;
+
     void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
 
+    void setCurrentRow(int row);
+    int getCurrentRow();
+
 signals:
     void frameSelected(size_t);
     void addCommand(Command *);
+    void currentChanged(const QModelIndex &);
 
 protected slots:
     void modelChanged();
@@ -68,6 +74,7 @@
     TabularModel *m_model;
     int m_sortColumn;
     Qt::SortOrder m_sortOrdering;
+    int m_currentRow;
     typedef std::vector<int> RowList;
     mutable RowList m_sort;
     mutable RowList m_rsort;
@@ -76,6 +83,7 @@
     void resort() const;
     void resortNumeric() const;
     void resortAlphabetical() const;
+    void clearSort();
 };
 
 #endif