annotate data/model/DeferredNotifier.h @ 1687:1bbea26ea21a single-point

Think we may need this header with some compilers
author Chris Cannam
date Thu, 04 Apr 2019 14:10:57 +0100
parents 7a56bb85030f
children 6d09d68165a4
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@1651 33 DeferredNotifier(Model *m, Mode mode) : m_model(m), m_mode(mode) { }
Chris@1651 34
Chris@1651 35 Mode getMode() const {
Chris@1651 36 return m_mode;
Chris@1651 37 }
Chris@1651 38 void switchMode(Mode newMode) {
Chris@1651 39 m_mode = newMode;
Chris@1651 40 }
Chris@1651 41
Chris@1651 42 void update(sv_frame_t frame, sv_frame_t duration) {
Chris@1651 43 if (m_mode == NOTIFY_ALWAYS) {
Chris@1651 44 m_model->modelChangedWithin(frame, frame + duration);
Chris@1651 45 } else {
Chris@1651 46 QMutexLocker locker(&m_mutex);
Chris@1651 47 m_extents.sample(frame);
Chris@1651 48 m_extents.sample(frame + duration);
Chris@1651 49 }
Chris@1651 50 }
Chris@1651 51
Chris@1651 52 void makeDeferredNotifications() {
Chris@1651 53 bool shouldEmit = false;
Chris@1651 54 sv_frame_t from, to;
Chris@1651 55 { QMutexLocker locker(&m_mutex);
Chris@1651 56 if (m_extents.isSet()) {
Chris@1651 57 shouldEmit = true;
Chris@1651 58 from = m_extents.getMin();
Chris@1651 59 to = m_extents.getMax();
Chris@1651 60 }
Chris@1651 61 }
Chris@1651 62 if (shouldEmit) {
Chris@1651 63 m_model->modelChangedWithin(from, to);
Chris@1651 64 QMutexLocker locker(&m_mutex);
Chris@1651 65 m_extents.reset();
Chris@1651 66 }
Chris@1651 67 }
Chris@1651 68
Chris@1651 69 private:
Chris@1651 70 Model *m_model;
Chris@1651 71 Mode m_mode;
Chris@1651 72 QMutex m_mutex;
Chris@1651 73 Extents<sv_frame_t> m_extents;
Chris@1651 74 };
Chris@1651 75
Chris@1651 76 #endif