Chris@1651: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1651: Chris@1651: /* Chris@1651: Sonic Visualiser Chris@1651: An audio file viewer and annotation editor. Chris@1651: Centre for Digital Music, Queen Mary, University of London. Chris@1651: Chris@1651: This program is free software; you can redistribute it and/or Chris@1651: modify it under the terms of the GNU General Public License as Chris@1651: published by the Free Software Foundation; either version 2 of the Chris@1651: License, or (at your option) any later version. See the file Chris@1651: COPYING included with this distribution for more information. Chris@1651: */ Chris@1651: Chris@1651: #ifndef SV_DEFERRED_NOTIFIER_H Chris@1651: #define SV_DEFERRED_NOTIFIER_H Chris@1651: Chris@1651: #include "Model.h" Chris@1651: Chris@1651: #include "base/Extents.h" Chris@1651: Chris@1651: #include Chris@1651: #include Chris@1651: Chris@1651: class DeferredNotifier Chris@1651: { Chris@1651: public: Chris@1651: enum Mode { Chris@1651: NOTIFY_ALWAYS, Chris@1651: NOTIFY_DEFERRED Chris@1651: }; Chris@1651: Chris@1651: DeferredNotifier(Model *m, Mode mode) : m_model(m), m_mode(mode) { } Chris@1651: Chris@1651: Mode getMode() const { Chris@1651: return m_mode; Chris@1651: } Chris@1651: void switchMode(Mode newMode) { Chris@1651: m_mode = newMode; Chris@1651: } Chris@1651: Chris@1651: void update(sv_frame_t frame, sv_frame_t duration) { Chris@1651: if (m_mode == NOTIFY_ALWAYS) { Chris@1651: m_model->modelChangedWithin(frame, frame + duration); Chris@1651: } else { Chris@1651: QMutexLocker locker(&m_mutex); Chris@1651: m_extents.sample(frame); Chris@1651: m_extents.sample(frame + duration); Chris@1651: } Chris@1651: } Chris@1651: Chris@1651: void makeDeferredNotifications() { Chris@1651: bool shouldEmit = false; Chris@1651: sv_frame_t from, to; Chris@1651: { QMutexLocker locker(&m_mutex); Chris@1651: if (m_extents.isSet()) { Chris@1651: shouldEmit = true; Chris@1651: from = m_extents.getMin(); Chris@1651: to = m_extents.getMax(); Chris@1651: } Chris@1651: } Chris@1651: if (shouldEmit) { Chris@1651: m_model->modelChangedWithin(from, to); Chris@1651: QMutexLocker locker(&m_mutex); Chris@1651: m_extents.reset(); Chris@1651: } Chris@1651: } Chris@1651: Chris@1651: private: Chris@1651: Model *m_model; Chris@1651: Mode m_mode; Chris@1651: QMutex m_mutex; Chris@1651: Extents m_extents; Chris@1651: }; Chris@1651: Chris@1651: #endif