annotate data/model/DeferredNotifier.h @ 1825:029d7d3f0a3a

Add option to avoid pulling in the WinRT dependency for this very small feature
author Chris Cannam
date Fri, 06 Mar 2020 12:12:46 +0000
parents 6d09d68165a4
children
rev   line source
Chris@1651 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1651 2
Chris@1651 3 /*
Chris@1651 4 Sonic Visualiser
Chris@1651 5 An audio file viewer and annotation editor.
Chris@1651 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1651 7
Chris@1651 8 This program is free software; you can redistribute it and/or
Chris@1651 9 modify it under the terms of the GNU General Public License as
Chris@1651 10 published by the Free Software Foundation; either version 2 of the
Chris@1651 11 License, or (at your option) any later version. See the file
Chris@1651 12 COPYING included with this distribution for more information.
Chris@1651 13 */
Chris@1651 14
Chris@1651 15 #ifndef SV_DEFERRED_NOTIFIER_H
Chris@1651 16 #define SV_DEFERRED_NOTIFIER_H
Chris@1651 17
Chris@1651 18 #include "Model.h"
Chris@1651 19
Chris@1651 20 #include "base/Extents.h"
Chris@1651 21
Chris@1651 22 #include <QMutex>
Chris@1651 23 #include <QMutexLocker>
Chris@1651 24
Chris@1651 25 class DeferredNotifier
Chris@1651 26 {
Chris@1651 27 public:
Chris@1651 28 enum Mode {
Chris@1651 29 NOTIFY_ALWAYS,
Chris@1651 30 NOTIFY_DEFERRED
Chris@1651 31 };
Chris@1651 32
Chris@1752 33 DeferredNotifier(Model *m, ModelId id, Mode mode) :
Chris@1752 34 m_model(m), m_modelId(id), m_mode(mode) { }
Chris@1651 35
Chris@1651 36 Mode getMode() const {
Chris@1651 37 return m_mode;
Chris@1651 38 }
Chris@1651 39 void switchMode(Mode newMode) {
Chris@1651 40 m_mode = newMode;
Chris@1651 41 }
Chris@1651 42
Chris@1651 43 void update(sv_frame_t frame, sv_frame_t duration) {
Chris@1651 44 if (m_mode == NOTIFY_ALWAYS) {
Chris@1752 45 m_model->modelChangedWithin(m_modelId, frame, frame + duration);
Chris@1651 46 } else {
Chris@1651 47 QMutexLocker locker(&m_mutex);
Chris@1651 48 m_extents.sample(frame);
Chris@1651 49 m_extents.sample(frame + duration);
Chris@1651 50 }
Chris@1651 51 }
Chris@1651 52
Chris@1651 53 void makeDeferredNotifications() {
Chris@1651 54 bool shouldEmit = false;
Chris@1651 55 sv_frame_t from, to;
Chris@1651 56 { QMutexLocker locker(&m_mutex);
Chris@1651 57 if (m_extents.isSet()) {
Chris@1651 58 shouldEmit = true;
Chris@1651 59 from = m_extents.getMin();
Chris@1651 60 to = m_extents.getMax();
Chris@1651 61 }
Chris@1651 62 }
Chris@1651 63 if (shouldEmit) {
Chris@1752 64 m_model->modelChangedWithin(m_modelId, from, to);
Chris@1651 65 QMutexLocker locker(&m_mutex);
Chris@1651 66 m_extents.reset();
Chris@1651 67 }
Chris@1651 68 }
Chris@1651 69
Chris@1651 70 private:
Chris@1651 71 Model *m_model;
Chris@1752 72 ModelId m_modelId;
Chris@1651 73 Mode m_mode;
Chris@1651 74 QMutex m_mutex;
Chris@1651 75 Extents<sv_frame_t> m_extents;
Chris@1651 76 };
Chris@1651 77
Chris@1651 78 #endif