svcore  1.9
IntervalModel.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006-2008 Chris Cannam and QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef _INTERVAL_MODEL_H_
17 #define _INTERVAL_MODEL_H_
18 
19 #include "SparseValueModel.h"
20 #include "base/RealTime.h"
21 
28 template <typename PointType>
29 class IntervalModel : public SparseValueModel<PointType>
30 {
31 public:
32  IntervalModel(int sampleRate, int resolution,
33  bool notifyOnAdd = true) :
34  SparseValueModel<PointType>(sampleRate, resolution, notifyOnAdd)
35  { }
36 
37  IntervalModel(int sampleRate, int resolution,
38  float valueMinimum, float valueMaximum,
39  bool notifyOnAdd = true) :
40  SparseValueModel<PointType>(sampleRate, resolution,
41  valueMinimum, valueMaximum,
42  notifyOnAdd)
43  { }
44 
51  virtual typename SparseValueModel<PointType>::PointList getPoints(long start, long end) const;
52 
58  virtual typename SparseValueModel<PointType>::PointList getPoints(long frame) const;
59 
60  virtual const typename SparseModel<PointType>::PointList &getPoints() const {
62  }
63 
68  virtual QVariant getData(int row, int column, int role) const
69  {
70  if (column < 2) {
72  (row, column, role);
73  }
74 
77  if (i == SparseModel<PointType>::m_points.end()) return QVariant();
78 
79  switch (column) {
80  case 2:
81  if (role == Qt::EditRole || role == TabularModel::SortRole) return i->value;
82  else return QString("%1 %2").arg(i->value).arg
84  case 3: return int(i->duration);
85  default: return QVariant();
86  }
87  }
88 
89  virtual Command *getSetDataCommand(int row, int column, const QVariant &value, int role)
90  {
91  typedef IntervalModel<PointType> I;
92 
93  if (column < 2) {
95  (row, column, value, role);
96  }
97 
98  if (role != Qt::EditRole) return 0;
99  typename I::PointList::const_iterator i
100  = I::getPointListIteratorForRow(row);
101  if (i == I::m_points.end()) return 0;
102  typename I::EditCommand *command = new typename I::EditCommand
103  (this, I::tr("Edit Data"));
104 
105  PointType point(*i);
106  command->deletePoint(point);
107 
108  switch (column) {
109  // column cannot be 0 or 1, those cases were handled above
110  case 2: point.value = value.toDouble(); break;
111  case 3: point.duration = value.toInt(); break;
112  }
113 
114  command->addPoint(point);
115  return command->finish();
116  }
117 
118  virtual bool isColumnTimeValue(int column) const
119  {
120  // NB duration is not a "time value" -- that's for columns
121  // whose sort ordering is exactly that of the frame time
122  return (column < 2);
123  }
124 };
125 
126 template <typename PointType>
128 IntervalModel<PointType>::getPoints(long start, long end) const
129 {
130  typedef IntervalModel<PointType> I;
131 
132  if (start > end) return typename I::PointList();
133 
134  QMutex &mutex(I::m_mutex);
135  QMutexLocker locker(&mutex);
136 
137  PointType endPoint(end);
138 
139  typename I::PointListConstIterator endItr = I::m_points.upper_bound(endPoint);
140 
141  if (endItr != I::m_points.end()) ++endItr;
142  if (endItr != I::m_points.end()) ++endItr;
143 
144  typename I::PointList rv;
145 
146  for (typename I::PointListConstIterator i = endItr; i != I::m_points.begin(); ) {
147  --i;
148  if (i->frame < start) {
149  if (i->frame + long(i->duration) >= start) {
150  rv.insert(*i);
151  }
152  } else if (i->frame <= end) {
153  rv.insert(*i);
154  }
155  }
156 
157  return rv;
158 }
159 
160 template <typename PointType>
163 {
164  typedef IntervalModel<PointType> I;
165 
166  QMutex &mutex(I::m_mutex);
167  QMutexLocker locker(&mutex);
168 
169  if (I::m_resolution == 0) return typename I::PointList();
170 
171  long start = (frame / I::m_resolution) * I::m_resolution;
172  long end = start + I::m_resolution;
173 
174  PointType endPoint(end);
175 
176  typename I::PointListConstIterator endItr = I::m_points.upper_bound(endPoint);
177 
178  typename I::PointList rv;
179 
180  for (typename I::PointListConstIterator i = endItr; i != I::m_points.begin(); ) {
181  --i;
182  if (i->frame < start) {
183  if (i->frame + long(i->duration) >= start) {
184  rv.insert(*i);
185  }
186  } else if (i->frame <= end) {
187  rv.insert(*i);
188  }
189  }
190 
191  return rv;
192 }
193 
194 #endif
std::multiset< PointType, typename PointType::OrderComparator > PointList
Definition: SparseModel.h:69
IntervalModel(int sampleRate, int resolution, float valueMinimum, float valueMaximum, bool notifyOnAdd=true)
Definition: IntervalModel.h:37
static QMutex mutex
Definition: Debug.cpp:32
virtual QVariant getData(int row, int column, int role) const
Definition: SparseModel.h:315
virtual QVariant getData(int row, int column, int role) const
TabularModel methods.
Definition: IntervalModel.h:68
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: SparseModel.h:333
Model containing sparse data (points with some properties) of which one of the properties is an arbit...
virtual const PointList & getPoints() const
Get all points.
Definition: SparseModel.h:537
virtual const SparseModel< PointType >::PointList & getPoints() const
Get all points.
Definition: IntervalModel.h:60
Model containing sparse data (points with some properties) of which the properties include a duration...
Definition: IntervalModel.h:29
IntervalModel(int sampleRate, int resolution, bool notifyOnAdd=true)
Definition: IntervalModel.h:32
virtual Command * getSetDataCommand(int row, int column, const QVariant &value, int role)
Definition: IntervalModel.h:89
virtual bool isColumnTimeValue(int column) const
PointListIterator getPointListIteratorForRow(int row)
Definition: SparseModel.h:405
Model containing sparse data (points with some properties).
Definition: SparseModel.h:42