Mercurial > retraceit
view src/metadataview.cpp @ 6:031ce03cfdf2
Update translations
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 25 Mar 2015 14:24:55 +0100 |
parents | e4748da7140b |
children | 73efe717b944 |
line wrap: on
line source
#include "metadataview.h" #include "qxtcsvmodel.h" #include "filterwidget.h" #include <QTextCodec> #include <QTableView> #include <QSortFilterProxyModel> #include <QVBoxLayout> #include <QLabel> #include <QDebug> #include <QHeaderView> #include <QModelIndex> #include <QItemSelectionModel> MetaDataView::MetaDataView(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) { /* Create models */ mSortModel = new QSortFilterProxyModel; mCSVModel = new QxtCsvModel; setupGUI(); connect(mView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MetaDataView::viewSelectionChanged); connect(mSortModel, &QSortFilterProxyModel::dataChanged, this, &MetaDataView::dataChanged); return; } void MetaDataView::setupGUI() { QVBoxLayout *baseLayout = new QVBoxLayout; FilterWidget *filterWidget = new FilterWidget(mSortModel); baseLayout->addWidget(filterWidget); mView = new QTableView; mView->setModel(mSortModel); mView->horizontalHeader()->setStretchLastSection(true); mView->resizeColumnsToContents(); // mView->setColumnWidth(0, 60); mView->setSelectionBehavior(QAbstractItemView::SelectRows); mView->setSelectionMode(QAbstractItemView::SingleSelection); mView->setSortingEnabled(true); mView->setEditTriggers(QAbstractItemView::NoEditTriggers); baseLayout->addWidget(mView); setLayout(baseLayout); } QString MetaDataView::parseMetaData(const QString& filePath) { mCSVModel->setSource(filePath, true, ';', QTextCodec::codecForName("UTF8")); if (!mCSVModel->rowCount()) { return tr("Failed to parse file: '%1'").arg(filePath); } mSortModel->setSourceModel(mCSVModel); qDebug() << "Parsed: " << mCSVModel->rowCount() << " rows."; return QString(); } void MetaDataView::dataChanged() { QItemSelectionModel *selection = mView->selectionModel(); QModelIndexList selected = selection->selectedIndexes(); qDebug() << "Data Changed."; if (selected.isEmpty()) { /* Nothing selected still we need to emit this signal to update * the viewer otherwise selection changed handles it. */ emit selectionChanged(QString(), 0, mSortModel->rowCount() - 1, QDateTime()); } } void MetaDataView::viewSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { if (selected.indexes().isEmpty()) { /* Nothing selected */ return; } /* One row selected */ Q_ASSERT(selected.indexes().count() == mCSVModel->columnCount()); const QModelIndex idx = selected.indexes()[0]; const QString dateString = selected.indexes()[1].data().toString(); bool ok; qint64 secondsSinceEpoch = dateString.toLongLong(&ok); if (!ok) { // TODO emit error qDebug() << "Unparsable date."; } QDateTime timestamp = QDateTime::fromMSecsSinceEpoch(secondsSinceEpoch * 1000); emit selectionChanged(idx.data().toString(), idx.row(), mSortModel->rowCount() - 1, timestamp); qDebug() << "Selection changed: " << idx.data(); } void MetaDataView::selectRow(int row) { QItemSelectionModel *selection = mView->selectionModel(); if (!mSortModel->hasIndex(row, 0)) { qDebug() << "Invalid row: " << row; return; } QModelIndex newIdx = mSortModel->index(row, 0); selection->select(newIdx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); } void MetaDataView::selectPrevRow() { QItemSelectionModel *selection = mView->selectionModel(); QModelIndexList selected = selection->selectedIndexes(); int row = 0, col = 0; if (selected.isEmpty()) { qDebug() << "Selection empty. Start at row 0"; if (!mSortModel->hasIndex(row, col)) { qDebug() << "Empty model. Failed to advance."; return; } } else { QModelIndex old = selection->selectedIndexes().first(); if (!mSortModel->hasIndex(old.row() - 1, old.column())) { qDebug() << "No less rows."; return; } row = old.row() - 1; col = old.column(); } QModelIndex newIdx = mSortModel->index(row, col); selection->select(newIdx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); } void MetaDataView::selectNextRow() { QItemSelectionModel *selection = mView->selectionModel(); QModelIndexList selected = selection->selectedIndexes(); int row = 0, col = 0; if (selected.isEmpty()) { qDebug() << "Selection empty. Start at row 0"; if (!mSortModel->hasIndex(row, col)) { qDebug() << "Empty model. Failed to advance."; return; } } else { QModelIndex old = selection->selectedIndexes().first(); if (!mSortModel->hasIndex(old.row() + 1, old.column())) { qDebug() << "No more rows."; return; } row = old.row() + 1; col = old.column(); } QModelIndex newIdx = mSortModel->index(row, col); selection->select(newIdx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows); }