diff data/model/ModelDataTableModel.cpp @ 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 304fd0223fa6
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));
+//    }
+}
+
+