comparison 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
comparison
equal deleted inserted replaced
27:5bd14af5bce5 28:4e16fbd10945
6 * See LICENSE.txt for details. 6 * See LICENSE.txt for details.
7 */ 7 */
8 #include "metadataview.h" 8 #include "metadataview.h"
9 #include "qxtcsvmodel.h" 9 #include "qxtcsvmodel.h"
10 #include "filterwidget.h" 10 #include "filterwidget.h"
11 #include "constants.h"
11 12
12 #include <QTextCodec> 13 #include <QTextCodec>
13 #include <QTableView> 14 #include <QTableView>
14 #include <QSortFilterProxyModel> 15 #include <QSortFilterProxyModel>
15 #include <QVBoxLayout> 16 #include <QVBoxLayout>
16 #include <QLabel> 17 #include <QLabel>
17 #include <QDebug> 18 #include <QDebug>
18 #include <QModelIndex> 19 #include <QModelIndex>
19 #include <QHeaderView> 20 #include <QHeaderView>
20 #include <QItemSelectionModel> 21 #include <QItemSelectionModel>
22 #include <QSettings>
21 23
22 MetaDataView::MetaDataView(QWidget *parent, Qt::WindowFlags f) : 24 MetaDataView::MetaDataView(QWidget *parent, Qt::WindowFlags f) :
23 QWidget(parent, f) { 25 QWidget(parent, f) {
24 /* Create models */ 26 /* Create models */
25 mSortModel = new QSortFilterProxyModel; 27 mSortModel = new QSortFilterProxyModel;
35 37
36 void MetaDataView::setupGUI() { 38 void MetaDataView::setupGUI() {
37 QVBoxLayout *baseLayout = new QVBoxLayout; 39 QVBoxLayout *baseLayout = new QVBoxLayout;
38 40
39 FilterWidget *filterWidget = new FilterWidget(mSortModel); 41 FilterWidget *filterWidget = new FilterWidget(mSortModel);
42 connect(filterWidget, &FilterWidget::filterHasChanged,
43 this, &MetaDataView::applyDefaultSort);
40 baseLayout->addWidget(filterWidget); 44 baseLayout->addWidget(filterWidget);
41 45
42 mView = new QTableView; 46 mView = new QTableView;
43 mView->setModel(mSortModel); 47 mView->setModel(mSortModel);
44 48
61 return tr("Failed to parse file: '%1'").arg(filePath); 65 return tr("Failed to parse file: '%1'").arg(filePath);
62 } 66 }
63 67
64 mSortModel->setSourceModel(mCSVModel); 68 mSortModel->setSourceModel(mCSVModel);
65 qDebug() << "Parsed: " << mCSVModel->rowCount() << " rows."; 69 qDebug() << "Parsed: " << mCSVModel->rowCount() << " rows.";
70 applyDefaultSort();
66 return QString(); 71 return QString();
67 } 72 }
68 73
69 void MetaDataView::dataChanged() 74 void MetaDataView::dataChanged()
70 { 75 {
157 col = old.column(); 162 col = old.column();
158 } 163 }
159 QModelIndex newIdx = mSortModel->index(row, col); 164 QModelIndex newIdx = mSortModel->index(row, col);
160 selection->select(newIdx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); 165 selection->select(newIdx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
161 } 166 }
167
168 void MetaDataView::applyDefaultSort() {
169 QSettings settings;
170 QString sortField = settings.value(SORT_COLUMN_KEY, SORT_COLUMN_DEFAULT).toString();
171 bool sortAsc = settings.value(SORT_ORDER_KEY, SORT_ORDER_VALUE).toBool();
172
173 int idx = -1;
174 for (int i=0; i < mSortModel->columnCount(); i++) {
175 QString entry = mSortModel->headerData(i, Qt::Horizontal).toString();
176 if (entry.toLower() == sortField.toLower()) {
177 idx = 1;
178 break;
179 }
180 }
181 if (idx == -1) {
182 qDebug() << "Failed to find configured sort column: " << sortField;
183 return;
184 }
185 qDebug() << "Applying default sort order on column " << idx;
186 mView->sortByColumn(idx, sortAsc ? Qt::AscendingOrder : Qt::DescendingOrder);
187 }
188
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)