# HG changeset patch # User Chris Cannam # Date 1213281225 0 # Node ID eafef13bb0b3699580c2c8038139a856fd7432bb # Parent 6a96bff0bd59e020151f22d48f0caa5dd1b05459 * Add more data-editor support to various models diff -r 6a96bff0bd59 -r eafef13bb0b3 data/model/ImageModel.h --- a/data/model/ImageModel.h Thu Jun 12 10:47:11 2008 +0000 +++ b/data/model/ImageModel.h Thu Jun 12 14:33:45 2008 +0000 @@ -136,6 +136,75 @@ ImagePoint m_oldPoint; ImagePoint m_newPoint; }; + + /** + * TabularModel methods. + */ + + virtual int getColumnCount() const + { + return 4; + } + + virtual QString getHeading(int column) const + { + switch (column) { + case 0: return tr("Time"); + case 1: return tr("Frame"); + case 2: return tr("Image"); + case 3: return tr("Label"); + default: return tr("Unknown"); + } + } + + virtual QVariant getData(int row, int column, int role) const + { + PointListIterator i = getPointListIteratorForRow(row); + if (i == m_points.end()) return QVariant(); + + switch (column) { + case 0: { + if (role == SortRole) return int(i->frame); + RealTime rt = RealTime::frame2RealTime(i->frame, getSampleRate()); + return rt.toText().c_str(); + } + case 1: return int(i->frame); + case 2: return i->image; + case 3: return i->label; + default: return QVariant(); + } + } + + virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role) + { + if (role != Qt::EditRole) return false; + PointListIterator i = getPointListIteratorForRow(row); + if (i == m_points.end()) return false; + EditCommand *command = new EditCommand(this, tr("Edit Data")); + + Point point(*i); + command->deletePoint(point); + + switch (column) { + case 0: case 1: point.frame = value.toInt(); break; + case 2: point.image = value.toString(); break; + case 3: point.label = value.toString(); break; + } + + command->addPoint(point); + return command->finish(); + } + + virtual bool isColumnTimeValue(int column) const + { + return (column < 2); + } + + virtual SortType getSortType(int column) const + { + if (column > 2) return SortAlphabetical; + return SortNumeric; + } }; diff -r 6a96bff0bd59 -r eafef13bb0b3 data/model/ModelDataTableModel.cpp --- a/data/model/ModelDataTableModel.cpp Thu Jun 12 10:47:11 2008 +0000 +++ b/data/model/ModelDataTableModel.cpp Thu Jun 12 14:33:45 2008 +0000 @@ -41,6 +41,7 @@ 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); } @@ -236,8 +237,7 @@ int rows = m_model->getRowCount(); for (int i = 0; i < rows; ++i) { - QVariant value = - m_model->getData(i, m_sortColumn, TabularModel::SortRole); + QVariant value = m_model->getData(i, m_sortColumn, TabularModel::SortRole); rowMap.insert(MapType::value_type(value.toDouble(), i)); } diff -r 6a96bff0bd59 -r eafef13bb0b3 data/model/NoteModel.h --- a/data/model/NoteModel.h Thu Jun 12 10:47:11 2008 +0000 +++ b/data/model/NoteModel.h Thu Jun 12 14:33:45 2008 +0000 @@ -162,6 +162,83 @@ .arg(extraAttributes).arg(m_valueQuantization)); } + /** + * TabularModel methods. + */ + + virtual int getColumnCount() const + { + return 6; + } + + virtual QString getHeading(int column) const + { + switch (column) { + case 0: return tr("Time"); + case 1: return tr("Frame"); + case 2: return tr("Pitch"); + case 3: return tr("Duration"); + case 4: return tr("Level"); + case 5: return tr("Label"); + default: return tr("Unknown"); + } + } + + virtual QVariant getData(int row, int column, int role) const + { + PointListIterator i = getPointListIteratorForRow(row); + if (i == m_points.end()) return QVariant(); + + switch (column) { + case 0: { + if (role == SortRole) return int(i->frame); + RealTime rt = RealTime::frame2RealTime(i->frame, getSampleRate()); + return rt.toText().c_str(); + } + case 1: return int(i->frame); + case 2: + if (role == Qt::EditRole || role == SortRole) return i->value; + else return QString("%1 %2").arg(i->value).arg(getScaleUnits()); + case 3: return int(i->duration); + case 4: return i->level; + case 5: return i->label; + default: return QVariant(); + } + } + + virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role) + { + if (role != Qt::EditRole) return false; + PointListIterator i = getPointListIteratorForRow(row); + if (i == m_points.end()) return false; + EditCommand *command = new EditCommand(this, tr("Edit Data")); + + Point point(*i); + command->deletePoint(point); + + switch (column) { + case 0: case 1: point.frame = value.toInt(); break; + case 2: point.value = value.toDouble(); break; + case 3: point.duration = value.toInt(); break; + case 4: point.level = value.toDouble(); break; + case 5: point.label = value.toString(); break; + } + + command->addPoint(point); + return command->finish(); + } + + virtual bool isColumnTimeValue(int column) const + { + return (column < 2); + } + + virtual SortType getSortType(int column) const + { + if (column == 5) return SortAlphabetical; + return SortNumeric; + } + protected: float m_valueQuantization; }; diff -r 6a96bff0bd59 -r eafef13bb0b3 data/model/SparseOneDimensionalModel.h --- a/data/model/SparseOneDimensionalModel.h Thu Jun 12 10:47:11 2008 +0000 +++ b/data/model/SparseOneDimensionalModel.h Thu Jun 12 14:33:45 2008 +0000 @@ -132,9 +132,6 @@ virtual QVariant getData(int row, int column, int role) const { - if (role != Qt::EditRole && - role != Qt::DisplayRole && - role != SortRole) return QVariant(); PointListIterator i = getPointListIteratorForRow(row); if (i == m_points.end()) return QVariant(); @@ -150,6 +147,25 @@ } } + virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role) + { + if (role != Qt::EditRole) return false; + PointListIterator i = getPointListIteratorForRow(row); + if (i == m_points.end()) return false; + EditCommand *command = new EditCommand(this, tr("Edit Data")); + + Point point(*i); + command->deletePoint(point); + + switch (column) { + case 0: case 1: point.frame = value.toInt(); break; + case 2: point.label = value.toString(); break; + } + + command->addPoint(point); + return command->finish(); + } + virtual bool isColumnTimeValue(int column) const { return (column < 2); diff -r 6a96bff0bd59 -r eafef13bb0b3 data/model/SparseTimeValueModel.h --- a/data/model/SparseTimeValueModel.h Thu Jun 12 10:47:11 2008 +0000 +++ b/data/model/SparseTimeValueModel.h Thu Jun 12 14:33:45 2008 +0000 @@ -122,9 +122,6 @@ virtual QVariant getData(int row, int column, int role) const { - if (role != Qt::EditRole && - role != Qt::DisplayRole && - role != SortRole) return QVariant(); PointListIterator i = getPointListIteratorForRow(row); if (i == m_points.end()) return QVariant(); @@ -143,22 +140,18 @@ } } - virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role) const + virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role) { if (role != Qt::EditRole) return false; PointListIterator i = getPointListIteratorForRow(row); - if (i == m_points.end()) { - std::cerr << "Failed to find point iterator for row " << row << std::endl; - return false; - } + if (i == m_points.end()) return false; EditCommand *command = new EditCommand(this, tr("Edit Data")); Point point(*i); command->deletePoint(point); switch (column) { - case 0: break; - case 1: break; + case 0: case 1: point.frame = value.toInt(); break; case 2: point.value = value.toDouble(); break; case 3: point.label = value.toString(); break; } diff -r 6a96bff0bd59 -r eafef13bb0b3 data/model/TabularModel.h --- a/data/model/TabularModel.h Thu Jun 12 10:47:11 2008 +0000 +++ b/data/model/TabularModel.h Thu Jun 12 14:33:45 2008 +0000 @@ -50,7 +50,7 @@ virtual int getRowForFrame(long frame) const = 0; virtual bool isEditable() const { return false; } - virtual Command *getSetDataCommand(int row, int column, const QVariant &, int role) const { return 0; } + virtual Command *getSetDataCommand(int row, int column, const QVariant &, int role) { return 0; } }; #endif diff -r 6a96bff0bd59 -r eafef13bb0b3 data/model/TextModel.h --- a/data/model/TextModel.h Thu Jun 12 10:47:11 2008 +0000 +++ b/data/model/TextModel.h Thu Jun 12 14:33:45 2008 +0000 @@ -101,6 +101,76 @@ } QString getTypeName() const { return tr("Text"); } + + /** + * TabularModel methods. + */ + + virtual int getColumnCount() const + { + return 4; + } + + virtual QString getHeading(int column) const + { + switch (column) { + case 0: return tr("Time"); + case 1: return tr("Frame"); + case 2: return tr("Height"); + case 3: return tr("Label"); + default: return tr("Unknown"); + } + } + + virtual QVariant getData(int row, int column, int role) const + { + PointListIterator i = getPointListIteratorForRow(row); + if (i == m_points.end()) return QVariant(); + + switch (column) { + case 0: { + if (role == SortRole) return int(i->frame); + RealTime rt = RealTime::frame2RealTime(i->frame, getSampleRate()); + return rt.toText().c_str(); + } + case 1: return int(i->frame); + case 2: return i->height; + case 3: return i->label; + default: return QVariant(); + } + } + + virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role) + { + if (role != Qt::EditRole) return false; + PointListIterator i = getPointListIteratorForRow(row); + if (i == m_points.end()) return false; + EditCommand *command = new EditCommand(this, tr("Edit Data")); + + Point point(*i); + command->deletePoint(point); + + switch (column) { + case 0: case 1: point.frame = value.toInt(); break; + case 2: point.height = value.toDouble(); break; + case 3: point.label = value.toString(); break; + } + + command->addPoint(point); + return command->finish(); + } + + virtual bool isColumnTimeValue(int column) const + { + return (column < 2); + } + + virtual SortType getSortType(int column) const + { + if (column == 3) return SortAlphabetical; + return SortNumeric; + } + };