File: SelectionTracker.h

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (317 lines) | stat: -rw-r--r-- 10,916 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/***************************************************************************
     SelectionTracker.h  -  tracker for selection changes
                             -------------------
    begin                : Tue Feb 25 2014
    copyright            : (C) 2014 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef SELECTION_TRACKER_H
#define SELECTION_TRACKER_H

#include "config.h"
#include "libkwavegui_export.h"

#include <QtGlobal>
#include <QList>
#include <QObject>
#include <QPointer>
#include <QRecursiveMutex>
#include <QUuid>
#include <QVector>

#include "libkwave/Sample.h"
#include "libkwave/SignalManager.h"
#include "libkwave/undo/UndoAction.h"
#include "libkwave/undo/UndoHandler.h"

namespace Kwave
{

    class Track;
    class UndoTransaction;

    class LIBKWAVEGUI_EXPORT SelectionTracker: public QObject,
                                          private Kwave::UndoHandler
    {
        Q_OBJECT
    public:
        /**
         * Constructor
         * @param signal the signal manager to track
         * @param offset index of the first selected sample
         * @param length number of selected samples
         * @param tracks list of selected tracks or null pointer for "all"
         */
        SelectionTracker(Kwave::SignalManager *signal,
                         sample_index_t offset,
                         sample_index_t length,
                         const QVector<unsigned int> *tracks);

        /** Destructor */
        ~SelectionTracker() override;

        /**
         * Returns all currently selected tracks
         */
        QList<QUuid> allTracks();

        /**
         * changes the selected range covered by the cache
         * @internal used during undo/redo
         * @param tracks list of selected tracks
         * @param offset index of the first selected sample
         * @param length number of selected samples
         */
        void selectRange(QList<QUuid> tracks,
                         sample_index_t offset, sample_index_t length);

        /**
         * Returns the start of the selected range
         * @return the first sample index
         */
        sample_index_t offset() const { return m_offset; }

        /**
         * Returns the length of the selected range
         * @return number of selected samples
         */
        sample_index_t length() const { return m_length; }

        /**
         * Returns the start of the selected range
         * @see offset()
         * @return the first sample index
         */
        sample_index_t first() const { return m_offset; }

        /**
         * Returns the end of the selected range
         * @return the last sample index
         */
        sample_index_t last() const {
            return m_offset + ((m_length) ? (m_length - 1) : 0);
        }

    signals:

        /**
         * Signals that a track has been inserted.
         * @param uuid unique ID of the track
         */
        void sigTrackInserted(const QUuid &uuid);

        /**
         * Signals that a track has been deleted.
         * @param uuid unique ID of the track
         */
        void sigTrackDeleted(const QUuid &uuid);

        /**
         * signals that the offset of the selection has changed
         * @param offset new index of the selected area [samples]
         */
        void sigOffsetChanged(sample_index_t offset);

        /**
         * signals that the length of the selection has changed
         * @param length new length of the selection [samples]
         */
        void sigLengthChanged(sample_index_t length);

        /**
         * signals that a range of samples has become invalid
         * @param track UUID of the track or null for "all tracks"
         * @param first index of the first invalidated sample
         * @param last index of the last invalidated sample
         */
        void sigInvalidated(const QUuid *track,
                            sample_index_t first,
                            sample_index_t last);

    private slots:

        /**
         * Connected to the signal's sigTrackInserted.
         * @param index the index [0...tracks()-1] of the inserted track
         * @param track pointer to the track instance
         * @see Signal::sigTrackInserted
         * @internal
         */
        void slotTrackInserted(unsigned int index, Kwave::Track *track);

        /**
         * Connected to the signal's sigTrackInserted.
         * @param index the index of the inserted track
         * @param track pointer to the track instance
         * @see Signal::sigTrackDeleted
         * @internal
         */
        void slotTrackDeleted(unsigned int index, Kwave::Track *track);

        /**
         * Connected to the signal's sigSamplesInserted.
         * @param track index of the source track [0...tracks-1]
         * @param offset position from which the data was inserted
         * @param length number of samples inserted
         * @see Signal::sigSamplesInserted
         * @internal
         */
        void slotSamplesInserted(unsigned int track, sample_index_t offset,
                                 sample_index_t length);

        /**
         * Connected to the signal's sigSamplesDeleted.
         * @param track index of the source track [0...tracks-1]
         * @param offset position from which the data was removed
         * @param length number of samples deleted
         * @see Signal::sigSamplesDeleted
         * @internal
         */
        void slotSamplesDeleted(unsigned int track, sample_index_t offset,
                                sample_index_t length);

        /**
         * Connected to the signal's sigSamplesModified
         * @param track index of the source track [0...tracks-1]
         * @param offset position from which the data was modified
         * @param length number of samples modified
         * @see Signal::sigSamplesModified
         * @internal
         */
        void slotSamplesModified(unsigned int track, sample_index_t offset,
                                 sample_index_t length);

    protected:

        /**
         * Called by an undo manager to notify us that it is time to
         * save data for undo.
         *
         * @param undo an undo transaction to append some undo data
         * @retval true if successful
         * @retval false if saving undo data failed, e.g. out of memory
         *               or aborted
         */
        bool saveUndoData(Kwave::UndoTransaction &undo) override;

    private:

        /**
         * Undo action for tracking selection changes
         */
        class LIBKWAVEGUI_EXPORT Undo: public Kwave::UndoAction
        {
        public:

            /**
             * Constructor
             * @param selection pointer to the corresponding selection tracker
             */
            explicit Undo(Kwave::SelectionTracker *selection);

            /** Destructor */
            ~Undo() override;

            /**
             * Returns a verbose short description of the action.
             */
            QString description() override;

            /**
             * Returns the required amount of memory that is needed for storing
             * undo data for the operation. This will be called to determine the
             * free memory to be reserved.
             * @note this is the first step (after the constructor)
             */
            qint64 undoSize() override;

            /**
             * Returns the difference of needed memory that is needed for
             * redo.
             */
            qint64 redoSize() override;

            /**
             * Stores the data needed for undo.
             * @param manager the SignalManager for modifying the signal
             * @note this is the second step, after size() has been called
             * @return true if successful, false if failed (e.g. out of memory)
             */
            bool store(Kwave::SignalManager &manager) override;

            /**
             * Takes back an action by creating a new undo action (for further
             * redo) and restoring the previous state.
             * @param manager the SignalManager for modifying the signal
             * @param with_redo if true a UndoAction for redo will be created
             * @note The return value is allowed to be the same object. This
             *       is useful for objects that can re-use their data for
             *       undo/redo. You have to check for this when deleting an
             *       UndoAction object after undo.
             */
            Kwave::UndoAction *undo(Kwave::SignalManager &manager,
                                            bool with_redo) override;

            /**
             * This undo action does not contribute to the modification
             * of the signal.
             * @return true always
             */
            bool containsModification() const override {
                return false;
            }

            /** dump, for debugging purposes */
            void dump(const QString &indent) override {
                qDebug("%s%s", DBG(indent), DBG(description()));
            }

        private:

            /** pointer to the overview cache */
            QPointer<Kwave::SelectionTracker> m_tracker;

            /** list of selected tracks */
            QList<QUuid> m_tracks;

            /** start of the selection, first sample */
            sample_index_t m_offset;

            /** number of selected samples */
            sample_index_t m_length;

        };

    private:

        /** signal with the data to be shown */
        QPointer<Kwave::SignalManager> m_signal;

        /** first sample index in the source */
        sample_index_t m_offset;

        /** length of the source in samples, or zero for "whole signal" */
        sample_index_t m_length;

        /** list of currently selected source tracks */
        QList<QUuid> m_tracks;

        /** if true, track the selection only, otherwise the whole signal */
        bool m_selection_only;

        /** mutex for threadsafe access to the selection */
        QRecursiveMutex m_lock;
    };
}

#endif /* SELECTION_TRACKER_H */