Mercurial > retraceit
comparison src/metadataview.cpp @ 2:97d2c8869c39
First prototype implementation of table view and player
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 23 Mar 2015 16:34:42 +0100 |
parents | |
children | 248d5d1cdb38 |
comparison
equal
deleted
inserted
replaced
1:7a2637c3eb83 | 2:97d2c8869c39 |
---|---|
1 #include "metadataview.h" | |
2 #include "qxtcsvmodel.h" | |
3 | |
4 #include <QTextCodec> | |
5 #include <QTableView> | |
6 #include <QSortFilterProxyModel> | |
7 #include <QVBoxLayout> | |
8 #include <QLabel> | |
9 #include <QDebug> | |
10 #include <QHeaderView> | |
11 #include <QModelIndex> | |
12 #include <QItemSelectionModel> | |
13 | |
14 MetaDataView::MetaDataView(QWidget *parent, Qt::WindowFlags f) : | |
15 QWidget(parent, f) { | |
16 /* Create models */ | |
17 mSortModel = new QSortFilterProxyModel; | |
18 mCSVModel = new QxtCsvModel; | |
19 setupGUI(); | |
20 | |
21 connect(mView->selectionModel(), &QItemSelectionModel::selectionChanged, | |
22 this, &MetaDataView::viewSelectionChanged); | |
23 return; | |
24 } | |
25 | |
26 void MetaDataView::setupGUI() { | |
27 QVBoxLayout *baseLayout = new QVBoxLayout; | |
28 mView = new QTableView; | |
29 mView->setModel(mSortModel); | |
30 | |
31 mView->horizontalHeader()->setStretchLastSection(true); | |
32 mView->resizeColumnsToContents(); | |
33 // mView->setColumnWidth(0, 60); | |
34 mView->setSelectionBehavior(QAbstractItemView::SelectRows); | |
35 mView->setSelectionMode(QAbstractItemView::SingleSelection); | |
36 mView->setSortingEnabled(true); | |
37 mView->setEditTriggers(QAbstractItemView::NoEditTriggers); | |
38 | |
39 baseLayout->addWidget(mView); | |
40 | |
41 setLayout(baseLayout); | |
42 } | |
43 | |
44 QString MetaDataView::parseMetaData(const QString& filePath) { | |
45 mCSVModel->setSource(filePath, true, ';', QTextCodec::codecForName("UTF8")); | |
46 if (!mCSVModel->rowCount()) { | |
47 return tr("Failed to parse file: '%1'").arg(filePath); | |
48 } | |
49 | |
50 mSortModel->setSourceModel(mCSVModel); | |
51 qDebug() << "Parsed: " << mCSVModel->rowCount() << " rows."; | |
52 return QString(); | |
53 } | |
54 | |
55 void MetaDataView::viewSelectionChanged(const QItemSelection& selected, | |
56 const QItemSelection& deselected) { | |
57 /* One row selected */ | |
58 Q_ASSERT(selected.indexes().count() == mCSVModel->columnCount()); | |
59 const QModelIndex idx = selected.indexes()[0]; | |
60 emit selectionChanged(idx.data().toString(), QString()); | |
61 qDebug() << "Selection changed: " << idx.data(); | |
62 } | |
63 | |
64 void MetaDataView::selectNextRow() { | |
65 QItemSelectionModel *selection = mView->selectionModel(); | |
66 QModelIndexList selected = selection->selectedIndexes(); | |
67 int row = 0, | |
68 col = 0; | |
69 if (selected.isEmpty()) { | |
70 qDebug() << "Selection empty. Start at row 0"; | |
71 if (!mSortModel->hasIndex(row, col)) { | |
72 qDebug() << "Empty model. Failed to advance."; | |
73 return; | |
74 } | |
75 } else { | |
76 QModelIndex old = selection->selectedIndexes().first(); | |
77 if (!mSortModel->hasIndex(old.row() + 1, old.column())) { | |
78 qDebug() << "No more rows."; | |
79 return; | |
80 } | |
81 row = old.row() + 1; | |
82 col = old.column(); | |
83 } | |
84 QModelIndex newIdx = mSortModel->index(row, col); | |
85 selection->select(newIdx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); | |
86 } |