Mercurial > hg > svcore
view data/model/ModelDataTableModel.cpp @ 425:f5e8f12d2e58
* Add audio device selection to preferences
* Add (not yet functional) insert, delete, edit buttons to data edit window
* Add proper set methods for time fields in data edit window (using general
sparse model base class)
author | Chris Cannam |
---|---|
date | Fri, 13 Jun 2008 21:09:43 +0000 |
parents | eafef13bb0b3 |
children | 2386582f67cd |
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ /* Sonic Visualiser An audio file viewer and annotation editor. Centre for Digital Music, Queen Mary, University of London. This file copyright 2008 QMUL. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the file COPYING included with this distribution for more information. */ #include "ModelDataTableModel.h" #include "TabularModel.h" #include "Model.h" #include <map> #include <algorithm> #include <iostream> ModelDataTableModel::ModelDataTableModel(TabularModel *m) : m_model(m), m_sortColumn(0), m_sortOrdering(Qt::AscendingOrder) { Model *baseModel = dynamic_cast<Model *>(m); connect(baseModel, SIGNAL(modelChanged()), this, SLOT(modelChanged())); connect(baseModel, SIGNAL(modelChanged(size_t, size_t)), this, SLOT(modelChanged(size_t, size_t))); } ModelDataTableModel::~ModelDataTableModel() { } QVariant ModelDataTableModel::data(const QModelIndex &index, int role) const { if (role != Qt::EditRole && role != Qt::DisplayRole) return QVariant(); if (!index.isValid()) return QVariant(); return m_model->getData(getUnsorted(index.row()), index.column(), role); } bool ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; std::cerr << "ModelDataTableModel::setData(" << index.row() << ", " << index.column() << ", " << value.toString().toStdString() << ", " << role << ")" << std::endl; Command *command = m_model->getSetDataCommand(getUnsorted(index.row()), index.column(), value, role); if (command) { std::cerr << "emitting executeCommand" << std::endl; emit executeCommand(command); return true; } else { return false; } } Qt::ItemFlags ModelDataTableModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsSelectable; return flags; } QVariant ModelDataTableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Vertical && role == Qt::DisplayRole) { return section + 1; } if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { return m_model->getHeading(section); } return QVariant(); } QModelIndex ModelDataTableModel::index(int row, int column, const QModelIndex &parent) const { return createIndex(row, column, 0); } QModelIndex ModelDataTableModel::parent(const QModelIndex &index) const { return QModelIndex(); } int ModelDataTableModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return m_model->getRowCount(); } int ModelDataTableModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return m_model->getColumnCount(); } QModelIndex ModelDataTableModel::getModelIndexForFrame(size_t frame) const { int row = m_model->getRowForFrame(frame); return createIndex(getSorted(row), 0, 0); } size_t ModelDataTableModel::getFrameForModelIndex(const QModelIndex &index) const { return m_model->getFrameForRow(getUnsorted(index.row())); } void ModelDataTableModel::sort(int column, Qt::SortOrder sortOrder) { std::cerr << "ModelDataTableModel::sort(" << column << ", " << sortOrder << ")" << std::endl; if (m_sortColumn != column) { m_sort.clear(); } m_sortColumn = column; m_sortOrdering = sortOrder; emit layoutChanged(); } void ModelDataTableModel::modelChanged() { m_sort.clear(); emit layoutChanged(); } void ModelDataTableModel::modelChanged(size_t f0, size_t f1) { //!!! inefficient m_sort.clear(); emit layoutChanged(); } int ModelDataTableModel::getSorted(int row) { if (m_model->isColumnTimeValue(m_sortColumn)) { if (m_sortOrdering == Qt::AscendingOrder) { return row; } else { return rowCount() - row - 1; } } if (m_sort.empty()) { resort(); } int result = 0; if (row >= 0 && row < m_sort.size()) { result = m_sort[row]; } if (m_sortOrdering == Qt::DescendingOrder) { result = rowCount() - result - 1; } return result; } int ModelDataTableModel::getUnsorted(int row) { if (m_model->isColumnTimeValue(m_sortColumn)) { if (m_sortOrdering == Qt::AscendingOrder) { return row; } else { return rowCount() - row - 1; } } if (m_sort.empty()) { resort(); } int result = 0; if (row >= 0 && row < m_sort.size()) { if (m_sortOrdering == Qt::AscendingOrder) { result = m_rsort[row]; } else { result = m_rsort[rowCount() - row - 1]; } } return result; } void ModelDataTableModel::resort() { bool numeric = (m_model->getSortType(m_sortColumn) == TabularModel::SortNumeric); m_sort.clear(); m_rsort.clear(); if (numeric) resortNumeric(); else resortAlphabetical(); std::map<int, int> tmp; // rsort maps from sorted row number to original row number for (int i = 0; i < m_rsort.size(); ++i) { tmp[m_rsort[i]] = i; } // tmp now maps from original row number to sorted row number for (std::map<int, int>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) { m_sort.push_back(i->second); } // and sort now maps from original row number to sorted row number } void ModelDataTableModel::resortNumeric() { typedef std::multimap<double, int> MapType; MapType rowMap; int rows = m_model->getRowCount(); for (int i = 0; i < rows; ++i) { QVariant value = m_model->getData(i, m_sortColumn, TabularModel::SortRole); rowMap.insert(MapType::value_type(value.toDouble(), i)); } for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) { m_rsort.push_back(i->second); } // rsort now maps from sorted row number to original row number } void ModelDataTableModel::resortAlphabetical() { typedef std::multimap<QString, int> MapType; MapType rowMap; int rows = m_model->getRowCount(); for (int i = 0; i < rows; ++i) { QVariant value = m_model->getData(i, m_sortColumn, TabularModel::SortRole); rowMap.insert(MapType::value_type(value.toString(), i)); } for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) { m_rsort.push_back(i->second); } // rsort now maps from sorted row number to original row number }