comparison data/model/NoteModel.h @ 1648:86bbccb79c9b single-point

Switch to a single external set of commands for modifying editables with events
author Chris Cannam
date Fri, 15 Mar 2019 10:57:35 +0000
parents 29a20719796e
children 1cc9a0d4b1b6
comparison
equal deleted inserted replaced
1647:29a20719796e 1648:86bbccb79c9b
16 #ifndef SV_NOTE_MODEL_H 16 #ifndef SV_NOTE_MODEL_H
17 #define SV_NOTE_MODEL_H 17 #define SV_NOTE_MODEL_H
18 18
19 #include "Model.h" 19 #include "Model.h"
20 #include "TabularModel.h" 20 #include "TabularModel.h"
21 #include "EventCommands.h"
21 #include "base/UnitDatabase.h" 22 #include "base/UnitDatabase.h"
22 #include "base/EventSeries.h" 23 #include "base/EventSeries.h"
23 #include "base/NoteData.h" 24 #include "base/NoteData.h"
24 #include "base/NoteExportable.h" 25 #include "base/NoteExportable.h"
25 #include "base/RealTime.h" 26 #include "base/RealTime.h"
30 #include <QMutex> 31 #include <QMutex>
31 #include <QMutexLocker> 32 #include <QMutexLocker>
32 33
33 class NoteModel : public Model, 34 class NoteModel : public Model,
34 public TabularModel, 35 public TabularModel,
35 public NoteExportable 36 public NoteExportable,
37 public EventEditable
36 { 38 {
37 Q_OBJECT 39 Q_OBJECT
38 40
39 public: 41 public:
40 enum Subtype { 42 enum Subtype {
190 EventVector getEventsCovering(sv_frame_t f) const { 192 EventVector getEventsCovering(sv_frame_t f) const {
191 return m_events.getEventsCovering(f); 193 return m_events.getEventsCovering(f);
192 } 194 }
193 195
194 /** 196 /**
195 * Editing commands and methods. 197 * Editing methods.
196 */ 198 */
197 199 void add(Event e) override {
198 class EditCommand : public Command
199 {
200 public:
201 //!!! borrowed ptr
202 EditCommand(NoteModel *model, QString name) :
203 m_model(model), m_executed(false), m_name(name) { }
204
205 QString getName() const override {
206 return m_name;
207 }
208
209 void setName(QString name) {
210 m_name = name;
211 }
212
213 void add(Event e) {
214 m_adding.insert(e);
215 m_model->add(e);
216 m_executed = true;
217 }
218
219 void remove(Event e) {
220 m_removing.insert(e);
221 m_model->remove(e);
222 m_executed = true;
223 }
224
225 void execute() override {
226 if (m_executed) return;
227 for (const Event &e: m_adding) m_model->add(e);
228 for (const Event &e: m_removing) m_model->remove(e);
229 m_executed = true;
230 }
231
232 void unexecute() override {
233 for (const Event &e: m_removing) m_model->add(e);
234 for (const Event &e: m_adding) m_model->remove(e);
235 m_executed = false;
236 }
237
238 EditCommand *finish() {
239 if (m_adding.empty() && m_removing.empty()) {
240 delete this;
241 return nullptr;
242 } else {
243 return this;
244 }
245 }
246
247 private:
248 NoteModel *m_model;
249 bool m_executed;
250 std::set<Event> m_adding;
251 std::set<Event> m_removing;
252 QString m_name;
253 };
254
255 void add(Event e) {
256 200
257 bool allChange = false; 201 bool allChange = false;
258 202
259 { 203 {
260 QMutexLocker locker(&m_mutex); 204 QMutexLocker locker(&m_mutex);
291 if (allChange) { 235 if (allChange) {
292 emit modelChanged(); 236 emit modelChanged();
293 } 237 }
294 } 238 }
295 239
296 void remove(Event e) { 240 void remove(Event e) override {
297 { 241 {
298 QMutexLocker locker(&m_mutex); 242 QMutexLocker locker(&m_mutex);
299 m_events.remove(e); 243 m_events.remove(e);
300 } 244 }
301 emit modelChangedWithin(e.getFrame(), 245 emit modelChangedWithin(e.getFrame(),
379 case 3: e1 = e0.withDuration(value.toInt()); break; 323 case 3: e1 = e0.withDuration(value.toInt()); break;
380 case 4: e1 = e0.withLevel(float(value.toDouble())); break; 324 case 4: e1 = e0.withLevel(float(value.toDouble())); break;
381 case 5: e1 = e0.withLabel(value.toString()); break; 325 case 5: e1 = e0.withLabel(value.toString()); break;
382 } 326 }
383 327
384 EditCommand *command = new EditCommand(this, tr("Edit Data")); 328 ChangeEventsCommand *command =
329 new ChangeEventsCommand(this, tr("Edit Data"));
385 command->remove(e0); 330 command->remove(e0);
386 command->add(e1); 331 command->add(e1);
387 return command->finish(); 332 return command->finish();
388 } 333 }
389 334