Chris@413: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@413: Chris@413: /* Chris@413: Sonic Visualiser Chris@413: An audio file viewer and annotation editor. Chris@413: Centre for Digital Music, Queen Mary, University of London. Chris@413: This file copyright 2008 QMUL. Chris@413: Chris@413: This program is free software; you can redistribute it and/or Chris@413: modify it under the terms of the GNU General Public License as Chris@413: published by the Free Software Foundation; either version 2 of the Chris@413: License, or (at your option) any later version. See the file Chris@413: COPYING included with this distribution for more information. Chris@413: */ Chris@413: Chris@413: #include "ModelDataTableModel.h" Chris@413: Chris@420: #include "TabularModel.h" Chris@420: #include "Model.h" Chris@413: Chris@422: #include Chris@417: #include Chris@420: #include Chris@417: Chris@420: ModelDataTableModel::ModelDataTableModel(TabularModel *m) : Chris@421: m_model(m), Chris@421: m_sortColumn(0), Chris@421: m_sortOrdering(Qt::AscendingOrder) Chris@413: { Chris@420: Model *baseModel = dynamic_cast(m); Chris@420: Chris@420: connect(baseModel, SIGNAL(modelChanged()), this, SLOT(modelChanged())); Chris@420: connect(baseModel, SIGNAL(modelChanged(size_t, size_t)), Chris@413: this, SLOT(modelChanged(size_t, size_t))); Chris@413: } Chris@413: Chris@413: ModelDataTableModel::~ModelDataTableModel() Chris@413: { Chris@413: } Chris@413: Chris@413: QVariant Chris@413: ModelDataTableModel::data(const QModelIndex &index, int role) const Chris@413: { Chris@424: if (role != Qt::EditRole && role != Qt::DisplayRole) return QVariant(); Chris@413: if (!index.isValid()) return QVariant(); Chris@420: return m_model->getData(getUnsorted(index.row()), index.column(), role); Chris@413: } Chris@413: Chris@413: bool Chris@413: ModelDataTableModel::setData(const QModelIndex &index, const QVariant &value, int role) Chris@413: { Chris@420: if (!index.isValid()) return false; Chris@421: std::cerr << "ModelDataTableModel::setData(" << index.row() << ", " << index.column() << ", " << value.toString().toStdString() << ", " << role << ")" << std::endl; Chris@421: Command *command = m_model->getSetDataCommand(getUnsorted(index.row()), Chris@421: index.column(), Chris@421: value, role); Chris@420: if (command) { Chris@421: std::cerr << "emitting executeCommand" << std::endl; Chris@420: emit executeCommand(command); Chris@420: return true; Chris@420: } else { Chris@416: return false; Chris@416: } Chris@413: } Chris@413: Chris@413: Qt::ItemFlags Chris@413: ModelDataTableModel::flags(const QModelIndex &index) const Chris@413: { Chris@416: Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsEditable | Chris@416: Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsSelectable; Chris@416: return flags; Chris@413: } Chris@413: Chris@413: QVariant Chris@413: ModelDataTableModel::headerData(int section, Qt::Orientation orientation, int role) const Chris@413: { Chris@425: if (orientation == Qt::Vertical && role == Qt::DisplayRole) { Chris@425: return section + 1; Chris@425: } Chris@413: if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { Chris@420: return m_model->getHeading(section); Chris@425: } Chris@413: return QVariant(); Chris@413: } Chris@413: Chris@413: QModelIndex Chris@413: ModelDataTableModel::index(int row, int column, const QModelIndex &parent) const Chris@413: { Chris@413: return createIndex(row, column, 0); Chris@413: } Chris@413: Chris@413: QModelIndex Chris@413: ModelDataTableModel::parent(const QModelIndex &index) const Chris@413: { Chris@413: return QModelIndex(); Chris@413: } Chris@413: Chris@413: int Chris@413: ModelDataTableModel::rowCount(const QModelIndex &parent) const Chris@413: { Chris@413: if (parent.isValid()) return 0; Chris@420: return m_model->getRowCount(); Chris@413: } Chris@413: Chris@413: int Chris@413: ModelDataTableModel::columnCount(const QModelIndex &parent) const Chris@413: { Chris@413: if (parent.isValid()) return 0; Chris@420: return m_model->getColumnCount(); Chris@416: } Chris@416: Chris@416: QModelIndex Chris@416: ModelDataTableModel::getModelIndexForFrame(size_t frame) const Chris@416: { Chris@420: int row = m_model->getRowForFrame(frame); Chris@420: return createIndex(getSorted(row), 0, 0); Chris@413: } Chris@413: Chris@419: size_t Chris@419: ModelDataTableModel::getFrameForModelIndex(const QModelIndex &index) const Chris@419: { Chris@420: return m_model->getFrameForRow(getUnsorted(index.row())); Chris@420: } Chris@420: Chris@420: void Chris@420: ModelDataTableModel::sort(int column, Qt::SortOrder sortOrder) Chris@420: { Chris@420: std::cerr << "ModelDataTableModel::sort(" << column << ", " << sortOrder Chris@420: << ")" << std::endl; Chris@422: if (m_sortColumn != column) { Chris@422: m_sort.clear(); Chris@422: } Chris@420: m_sortColumn = column; Chris@420: m_sortOrdering = sortOrder; Chris@420: emit layoutChanged(); Chris@419: } Chris@419: Chris@413: void Chris@413: ModelDataTableModel::modelChanged() Chris@413: { Chris@420: m_sort.clear(); Chris@413: emit layoutChanged(); Chris@413: } Chris@413: Chris@413: void Chris@416: ModelDataTableModel::modelChanged(size_t f0, size_t f1) Chris@416: { Chris@420: //!!! inefficient Chris@420: m_sort.clear(); Chris@416: emit layoutChanged(); Chris@416: } Chris@413: Chris@420: int Chris@420: ModelDataTableModel::getSorted(int row) Chris@413: { Chris@420: if (m_model->isColumnTimeValue(m_sortColumn)) { Chris@420: if (m_sortOrdering == Qt::AscendingOrder) { Chris@420: return row; Chris@420: } else { Chris@420: return rowCount() - row - 1; Chris@420: } Chris@420: } Chris@413: Chris@420: if (m_sort.empty()) { Chris@420: resort(); Chris@413: } Chris@422: int result = 0; Chris@422: if (row >= 0 && row < m_sort.size()) { Chris@422: result = m_sort[row]; Chris@422: } Chris@422: if (m_sortOrdering == Qt::DescendingOrder) { Chris@422: result = rowCount() - result - 1; Chris@422: } Chris@422: Chris@422: return result; Chris@413: } Chris@413: Chris@420: int Chris@420: ModelDataTableModel::getUnsorted(int row) Chris@413: { Chris@420: if (m_model->isColumnTimeValue(m_sortColumn)) { Chris@420: if (m_sortOrdering == Qt::AscendingOrder) { Chris@420: return row; Chris@420: } else { Chris@420: return rowCount() - row - 1; Chris@420: } Chris@413: } Chris@422: Chris@420: if (m_sort.empty()) { Chris@420: resort(); Chris@420: } Chris@422: Chris@422: int result = 0; Chris@422: if (row >= 0 && row < m_sort.size()) { Chris@422: if (m_sortOrdering == Qt::AscendingOrder) { Chris@422: result = m_rsort[row]; Chris@422: } else { Chris@422: result = m_rsort[rowCount() - row - 1]; Chris@422: } Chris@422: } Chris@422: Chris@422: return result; Chris@413: } Chris@413: Chris@420: void Chris@420: ModelDataTableModel::resort() Chris@413: { Chris@422: bool numeric = (m_model->getSortType(m_sortColumn) == Chris@422: TabularModel::SortNumeric); Chris@422: Chris@422: m_sort.clear(); Chris@422: m_rsort.clear(); Chris@422: Chris@422: if (numeric) resortNumeric(); Chris@422: else resortAlphabetical(); Chris@422: Chris@422: std::map tmp; Chris@422: Chris@422: // rsort maps from sorted row number to original row number Chris@422: Chris@422: for (int i = 0; i < m_rsort.size(); ++i) { Chris@422: tmp[m_rsort[i]] = i; Chris@422: } Chris@422: Chris@422: // tmp now maps from original row number to sorted row number Chris@422: Chris@422: for (std::map::const_iterator i = tmp.begin(); i != tmp.end(); ++i) { Chris@422: m_sort.push_back(i->second); Chris@422: } Chris@422: Chris@422: // and sort now maps from original row number to sorted row number Chris@413: } Chris@413: Chris@422: void Chris@422: ModelDataTableModel::resortNumeric() Chris@422: { Chris@422: typedef std::multimap MapType; Chris@422: Chris@422: MapType rowMap; Chris@422: int rows = m_model->getRowCount(); Chris@422: Chris@422: for (int i = 0; i < rows; ++i) { Chris@424: QVariant value = m_model->getData(i, m_sortColumn, TabularModel::SortRole); Chris@422: rowMap.insert(MapType::value_type(value.toDouble(), i)); Chris@422: } Chris@422: Chris@422: for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) { Chris@422: m_rsort.push_back(i->second); Chris@422: } Chris@422: Chris@422: // rsort now maps from sorted row number to original row number Chris@422: } Chris@422: Chris@422: void Chris@422: ModelDataTableModel::resortAlphabetical() Chris@422: { Chris@422: typedef std::multimap MapType; Chris@422: Chris@422: MapType rowMap; Chris@422: int rows = m_model->getRowCount(); Chris@422: Chris@422: for (int i = 0; i < rows; ++i) { Chris@422: QVariant value = Chris@422: m_model->getData(i, m_sortColumn, TabularModel::SortRole); Chris@422: rowMap.insert(MapType::value_type(value.toString(), i)); Chris@422: } Chris@422: Chris@422: for (MapType::iterator i = rowMap.begin(); i != rowMap.end(); ++i) { Chris@422: m_rsort.push_back(i->second); Chris@422: } Chris@422: Chris@422: // rsort now maps from sorted row number to original row number Chris@422: } Chris@422: