diff src/metadataview.cpp @ 28:4e16fbd10945

(issue3-5) Make the default sort order configurable
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 05 May 2015 18:46:35 +0200
parents 64a51a42c01f
children f10d4e035eec
line wrap: on
line diff
--- a/src/metadataview.cpp	Tue May 05 18:45:49 2015 +0200
+++ b/src/metadataview.cpp	Tue May 05 18:46:35 2015 +0200
@@ -8,6 +8,7 @@
 #include "metadataview.h"
 #include "qxtcsvmodel.h"
 #include "filterwidget.h"
+#include "constants.h"
 
 #include <QTextCodec>
 #include <QTableView>
@@ -18,6 +19,7 @@
 #include <QModelIndex>
 #include <QHeaderView>
 #include <QItemSelectionModel>
+#include <QSettings>
 
 MetaDataView::MetaDataView(QWidget *parent, Qt::WindowFlags f) :
     QWidget(parent, f) {
@@ -37,6 +39,8 @@
     QVBoxLayout *baseLayout = new QVBoxLayout;
 
     FilterWidget *filterWidget = new FilterWidget(mSortModel);
+    connect(filterWidget, &FilterWidget::filterHasChanged,
+            this, &MetaDataView::applyDefaultSort);
     baseLayout->addWidget(filterWidget);
 
     mView = new QTableView;
@@ -63,6 +67,7 @@
 
     mSortModel->setSourceModel(mCSVModel);
     qDebug() << "Parsed: " << mCSVModel->rowCount() << " rows.";
+    applyDefaultSort();
     return QString();
 }
 
@@ -159,3 +164,25 @@
     QModelIndex newIdx = mSortModel->index(row, col);
     selection->select(newIdx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
 }
+
+void MetaDataView::applyDefaultSort() {
+    QSettings settings;
+    QString sortField = settings.value(SORT_COLUMN_KEY, SORT_COLUMN_DEFAULT).toString();
+    bool sortAsc = settings.value(SORT_ORDER_KEY, SORT_ORDER_VALUE).toBool();
+
+    int idx = -1;
+    for (int i=0; i < mSortModel->columnCount(); i++) {
+        QString entry = mSortModel->headerData(i, Qt::Horizontal).toString();
+        if (entry.toLower() == sortField.toLower()) {
+            idx = 1;
+            break;
+        }
+    }
+    if (idx == -1) {
+        qDebug() << "Failed to find configured sort column: " << sortField;
+        return;
+    }
+    qDebug() << "Applying default sort order on column " << idx;
+    mView->sortByColumn(idx, sortAsc ? Qt::AscendingOrder : Qt::DescendingOrder);
+}
+
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)