view 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
line wrap: on
line source
#include "metadataview.h"
#include "qxtcsvmodel.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);
    return;
}

void MetaDataView::setupGUI() {
    QVBoxLayout *baseLayout = new QVBoxLayout;
    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::viewSelectionChanged(const QItemSelection& selected,
                                        const QItemSelection& deselected) {
    /* One row selected */
    Q_ASSERT(selected.indexes().count() == mCSVModel->columnCount());
    const QModelIndex idx = selected.indexes()[0];
    emit selectionChanged(idx.data().toString(), QString());
    qDebug() << "Selection changed: " << idx.data();
}

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);
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)