annotate data/model/RegionModel.h @ 630:11a664058dd8

* Start revamping the CSV import dialog so as to show a "purpose" for each column. These are estimated from the file now, but changing them does not actually do anything yet.
author Chris Cannam
date Fri, 16 Jul 2010 16:51:39 +0000
parents 080d8bdd8762
children 3a5ee4b6c9ad
rev   line source
Chris@441 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@441 2
Chris@441 3 /*
Chris@441 4 Sonic Visualiser
Chris@441 5 An audio file viewer and annotation editor.
Chris@441 6 Centre for Digital Music, Queen Mary, University of London.
Chris@441 7 This file copyright 2006 Chris Cannam.
Chris@441 8
Chris@441 9 This program is free software; you can redistribute it and/or
Chris@441 10 modify it under the terms of the GNU General Public License as
Chris@441 11 published by the Free Software Foundation; either version 2 of the
Chris@441 12 License, or (at your option) any later version. See the file
Chris@441 13 COPYING included with this distribution for more information.
Chris@441 14 */
Chris@441 15
Chris@441 16 #ifndef _REGION_MODEL_H_
Chris@441 17 #define _REGION_MODEL_H_
Chris@441 18
Chris@441 19 #include "IntervalModel.h"
Chris@441 20 #include "base/RealTime.h"
Chris@441 21
Chris@441 22 /**
Chris@441 23 * RegionModel -- a concrete IntervalModel for intervals associated
Chris@441 24 * with a value, which we call regions for no very compelling reason.
Chris@441 25 */
Chris@441 26
Chris@441 27 /**
Chris@441 28 * Region "point" type. A region is something that has an onset time,
Chris@441 29 * a single value, and a duration. Like other points, it can also
Chris@441 30 * have a label.
Chris@441 31 *
Chris@441 32 * This is called RegionRec instead of Region to avoid name collisions
Chris@441 33 * with the X11 Region struct. Bah.
Chris@441 34 */
Chris@441 35
Chris@441 36 struct RegionRec
Chris@441 37 {
Chris@441 38 public:
Chris@441 39 RegionRec(long _frame) : frame(_frame), value(0.0f), duration(0) { }
Chris@441 40 RegionRec(long _frame, float _value, size_t _duration, QString _label) :
Chris@441 41 frame(_frame), value(_value), duration(_duration), label(_label) { }
Chris@441 42
Chris@441 43 int getDimensions() const { return 3; }
Chris@441 44
Chris@441 45 long frame;
Chris@441 46 float value;
Chris@441 47 size_t duration;
Chris@441 48 QString label;
Chris@441 49
Chris@441 50 QString getLabel() const { return label; }
Chris@441 51
Chris@441 52 void toXml(QTextStream &stream,
Chris@441 53 QString indent = "",
Chris@441 54 QString extraAttributes = "") const
Chris@441 55 {
Chris@441 56 stream <<
Chris@441 57 QString("%1<point frame=\"%2\" value=\"%3\" duration=\"%4\" label=\"%5\" %6/>\n")
Chris@627 58 .arg(indent).arg(frame).arg(value).arg(duration)
Chris@627 59 .arg(XmlExportable::encodeEntities(label)).arg(extraAttributes);
Chris@441 60 }
Chris@441 61
Chris@441 62 QString toDelimitedDataString(QString delimiter, size_t sampleRate) const
Chris@441 63 {
Chris@441 64 QStringList list;
Chris@441 65 list << RealTime::frame2RealTime(frame, sampleRate).toString().c_str();
Chris@441 66 list << QString("%1").arg(value);
Chris@441 67 list << RealTime::frame2RealTime(duration, sampleRate).toString().c_str();
Chris@441 68 if (label != "") list << label;
Chris@441 69 return list.join(delimiter);
Chris@441 70 }
Chris@441 71
Chris@441 72 struct Comparator {
Chris@441 73 bool operator()(const RegionRec &p1,
Chris@441 74 const RegionRec &p2) const {
Chris@441 75 if (p1.frame != p2.frame) return p1.frame < p2.frame;
Chris@441 76 if (p1.value != p2.value) return p1.value < p2.value;
Chris@441 77 if (p1.duration != p2.duration) return p1.duration < p2.duration;
Chris@441 78 return p1.label < p2.label;
Chris@441 79 }
Chris@441 80 };
Chris@441 81
Chris@441 82 struct OrderComparator {
Chris@441 83 bool operator()(const RegionRec &p1,
Chris@441 84 const RegionRec &p2) const {
Chris@441 85 return p1.frame < p2.frame;
Chris@441 86 }
Chris@441 87 };
Chris@441 88 };
Chris@441 89
Chris@441 90
Chris@441 91 class RegionModel : public IntervalModel<RegionRec>
Chris@441 92 {
Chris@441 93 Q_OBJECT
Chris@441 94
Chris@441 95 public:
Chris@441 96 RegionModel(size_t sampleRate, size_t resolution,
Chris@441 97 bool notifyOnAdd = true) :
Chris@441 98 IntervalModel<RegionRec>(sampleRate, resolution, notifyOnAdd),
Chris@442 99 m_valueQuantization(0),
Chris@442 100 m_haveDistinctValues(false)
Chris@441 101 {
Chris@441 102 }
Chris@441 103
Chris@441 104 RegionModel(size_t sampleRate, size_t resolution,
Chris@459 105 float valueMinimum, float valueMaximum,
Chris@459 106 bool notifyOnAdd = true) :
Chris@441 107 IntervalModel<RegionRec>(sampleRate, resolution,
Chris@441 108 valueMinimum, valueMaximum,
Chris@441 109 notifyOnAdd),
Chris@442 110 m_valueQuantization(0),
Chris@442 111 m_haveDistinctValues(false)
Chris@441 112 {
Chris@441 113 }
Chris@441 114
Chris@441 115 virtual ~RegionModel()
Chris@441 116 {
Chris@441 117 }
Chris@441 118
Chris@441 119 float getValueQuantization() const { return m_valueQuantization; }
Chris@441 120 void setValueQuantization(float q) { m_valueQuantization = q; }
Chris@441 121
Chris@442 122 bool haveDistinctValues() const { return m_haveDistinctValues; }
Chris@442 123
Chris@441 124 QString getTypeName() const { return tr("Region"); }
Chris@441 125
Chris@441 126 virtual void toXml(QTextStream &out,
Chris@441 127 QString indent = "",
Chris@441 128 QString extraAttributes = "") const
Chris@441 129 {
Chris@441 130 std::cerr << "RegionModel::toXml: extraAttributes = \""
Chris@441 131 << extraAttributes.toStdString() << std::endl;
Chris@441 132
Chris@441 133 IntervalModel<RegionRec>::toXml
Chris@441 134 (out,
Chris@441 135 indent,
Chris@452 136 QString("%1 subtype=\"region\" valueQuantization=\"%2\"")
Chris@441 137 .arg(extraAttributes).arg(m_valueQuantization));
Chris@441 138 }
Chris@441 139
Chris@441 140 /**
Chris@441 141 * TabularModel methods.
Chris@441 142 */
Chris@441 143
Chris@441 144 virtual int getColumnCount() const
Chris@441 145 {
Chris@618 146 return 5;
Chris@441 147 }
Chris@441 148
Chris@441 149 virtual QString getHeading(int column) const
Chris@441 150 {
Chris@441 151 switch (column) {
Chris@441 152 case 0: return tr("Time");
Chris@441 153 case 1: return tr("Frame");
Chris@441 154 case 2: return tr("Value");
Chris@441 155 case 3: return tr("Duration");
Chris@441 156 case 4: return tr("Label");
Chris@441 157 default: return tr("Unknown");
Chris@441 158 }
Chris@441 159 }
Chris@441 160
Chris@441 161 virtual QVariant getData(int row, int column, int role) const
Chris@441 162 {
Chris@441 163 if (column < 4) {
Chris@441 164 return IntervalModel<RegionRec>::getData(row, column, role);
Chris@441 165 }
Chris@441 166
Chris@608 167 PointListConstIterator i = getPointListIteratorForRow(row);
Chris@441 168 if (i == m_points.end()) return QVariant();
Chris@441 169
Chris@441 170 switch (column) {
Chris@441 171 case 4: return i->label;
Chris@441 172 default: return QVariant();
Chris@441 173 }
Chris@441 174 }
Chris@441 175
Chris@441 176 virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
Chris@441 177 {
Chris@441 178 if (column < 4) {
Chris@441 179 return IntervalModel<RegionRec>::getSetDataCommand
Chris@441 180 (row, column, value, role);
Chris@441 181 }
Chris@441 182
Chris@441 183 if (role != Qt::EditRole) return false;
Chris@441 184 PointListIterator i = getPointListIteratorForRow(row);
Chris@441 185 if (i == m_points.end()) return false;
Chris@441 186 EditCommand *command = new EditCommand(this, tr("Edit Data"));
Chris@441 187
Chris@441 188 Point point(*i);
Chris@441 189 command->deletePoint(point);
Chris@441 190
Chris@441 191 switch (column) {
Chris@441 192 case 4: point.label = value.toString(); break;
Chris@441 193 }
Chris@441 194
Chris@441 195 command->addPoint(point);
Chris@441 196 return command->finish();
Chris@441 197 }
Chris@441 198
Chris@441 199 virtual SortType getSortType(int column) const
Chris@441 200 {
Chris@618 201 if (column == 4) return SortAlphabetical;
Chris@441 202 return SortNumeric;
Chris@441 203 }
Chris@441 204
Chris@442 205 virtual void addPoint(const Point &point)
Chris@442 206 {
Chris@442 207 if (point.value != 0.f) m_haveDistinctValues = true;
Chris@442 208 IntervalModel<RegionRec>::addPoint(point);
Chris@442 209 }
Chris@442 210
Chris@441 211 protected:
Chris@441 212 float m_valueQuantization;
Chris@442 213 bool m_haveDistinctValues;
Chris@441 214 };
Chris@441 215
Chris@441 216 #endif