comparison data/model/DeferredNotifier.h @ 1651:7a56bb85030f single-point

Introduce deferred notifier, + start converting sparse time-value model (perhaps we should rename it too)
author Chris Cannam
date Mon, 18 Mar 2019 14:17:20 +0000
parents
children 6d09d68165a4
comparison
equal deleted inserted replaced
1650:bbfb5a1e4b84 1651:7a56bb85030f
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
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef SV_DEFERRED_NOTIFIER_H
16 #define SV_DEFERRED_NOTIFIER_H
17
18 #include "Model.h"
19
20 #include "base/Extents.h"
21
22 #include <QMutex>
23 #include <QMutexLocker>
24
25 class DeferredNotifier
26 {
27 public:
28 enum Mode {
29 NOTIFY_ALWAYS,
30 NOTIFY_DEFERRED
31 };
32
33 DeferredNotifier(Model *m, Mode mode) : m_model(m), m_mode(mode) { }
34
35 Mode getMode() const {
36 return m_mode;
37 }
38 void switchMode(Mode newMode) {
39 m_mode = newMode;
40 }
41
42 void update(sv_frame_t frame, sv_frame_t duration) {
43 if (m_mode == NOTIFY_ALWAYS) {
44 m_model->modelChangedWithin(frame, frame + duration);
45 } else {
46 QMutexLocker locker(&m_mutex);
47 m_extents.sample(frame);
48 m_extents.sample(frame + duration);
49 }
50 }
51
52 void makeDeferredNotifications() {
53 bool shouldEmit = false;
54 sv_frame_t from, to;
55 { QMutexLocker locker(&m_mutex);
56 if (m_extents.isSet()) {
57 shouldEmit = true;
58 from = m_extents.getMin();
59 to = m_extents.getMax();
60 }
61 }
62 if (shouldEmit) {
63 m_model->modelChangedWithin(from, to);
64 QMutexLocker locker(&m_mutex);
65 m_extents.reset();
66 }
67 }
68
69 private:
70 Model *m_model;
71 Mode m_mode;
72 QMutex m_mutex;
73 Extents<sv_frame_t> m_extents;
74 };
75
76 #endif