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