File: UndoAddMetaDataAction.cpp

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 (179 lines) | stat: -rw-r--r-- 6,061 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
/***************************************************************************
 UndoAddMetaDataAction.cpp  -  Undo action for insertion of labels
                             -------------------
    begin                : Wed Aug 16 2006
    copyright            : (C) 2006 by Thomas Eschenbacher
    email                : Thomas Eschenbacher <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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <algorithm>
#include <new>

#include <QVariant>

#include <KLocalizedString>

#include "libkwave/MetaData.h"
#include "libkwave/SignalManager.h"
#include "libkwave/undo/UndoAddMetaDataAction.h"
#include "libkwave/undo/UndoDeleteMetaDataAction.h"

//***************************************************************************
Kwave::UndoAddMetaDataAction::UndoAddMetaDataAction(
    const Kwave::MetaDataList &meta_data)
    :Kwave::UndoAction(),
     m_description(),
     m_offset(0),
     m_length(SAMPLE_INDEX_MAX)
{
    sample_index_t first = SAMPLE_INDEX_MAX;
    sample_index_t last  = 0;

    // sanity check: list should not be empty
    Q_ASSERT(!meta_data.isEmpty());
    if (meta_data.isEmpty()) return;

    /*
     * loop over the list to find out the first/last sample offset
     * and the list of affected tracks
     */
    foreach (const Kwave::MetaData &m, meta_data)
    {
        // search over a list of known properties which contain range/position
        QStringList properties = Kwave::MetaData::positionBoundPropertyNames();
        foreach (const QString &tag, properties) {
            // check for a "start" property
            QVariant v = m[tag];
            bool ok = false;
            sample_index_t pos = static_cast<sample_index_t>(
                v.toULongLong(&ok));
            if (ok && (pos < first)) first = pos;
            if (ok && (pos > last))  last  = pos;
        }
    }

    // fix first/last in case that nothing was found, select everything
    if (first > last) {
        m_offset = SAMPLE_INDEX_MAX;
        m_length = 0;
    } else {
        m_offset = first;
        m_length = (last - first) + 1;
    }

    /*
     * determine the description of the action
     */

    for (;;) {
        QString name;
        QList<Kwave::MetaData> values = meta_data.values();
        Q_ASSERT(!values.isEmpty());
        if (!values.isEmpty()) {
            const Kwave::MetaData &m = values.first();
            if (m.hasProperty(Kwave::MetaData::STDPROP_TYPE))
                name = m[Kwave::MetaData::STDPROP_TYPE].toString();
        }

        // if the meta data list contains only one object: try to find
        // out the object's name
        if ((meta_data.count() == 1) && name.length()) {
            m_description = i18nc(
                "name of the undo action for inserting a meta data object",
                "Insert %1",
                name
            );
            break;
        }

        // check if the list contains only objects of the same type
        bool all_same_type = true;
        foreach (const Kwave::MetaData &m, meta_data) {
            QString n = m[Kwave::MetaData::STDPROP_TYPE].toString();
            if (!n.length() || (n != name)) {
                all_same_type = false;
                break;
            }
        }
        if (all_same_type) {
            m_description = i18nc(
                "name of the undo action for inserting multiple "
                "meta data objects of the same type: "
                "%1=number of elements, %2=name of one element in singular",
                "Insert %1 %2 objects", meta_data.count(), name
            );
            break;
        }

        m_description = i18n("Insert Meta Data");
        break;
    }
}

//***************************************************************************
Kwave::UndoAddMetaDataAction::~UndoAddMetaDataAction()
{
}

//***************************************************************************
QString Kwave::UndoAddMetaDataAction::description()
{
    return m_description;
}

//***************************************************************************
qint64 Kwave::UndoAddMetaDataAction::undoSize()
{
    return sizeof(*this);
}

//***************************************************************************
qint64 Kwave::UndoAddMetaDataAction::redoSize()
{
    return sizeof(UndoDeleteMetaDataAction);
}

//***************************************************************************
bool Kwave::UndoAddMetaDataAction::store(Kwave::SignalManager &)
{
    // nothing to do, all data has already
    // been stored in the constructor
    return true;
}

//***************************************************************************
Kwave::UndoAction *Kwave::UndoAddMetaDataAction::undo(
    Kwave::SignalManager &manager, bool with_redo)
{
    Kwave::UndoAction *redo = nullptr;

    Kwave::MetaDataList meta_data =
        manager.metaData().copy(m_offset, m_length);

    // store data for redo
    if (with_redo && !meta_data.isEmpty()) {
        redo = new(std::nothrow) Kwave::UndoDeleteMetaDataAction(meta_data);
        Q_ASSERT(redo);
        if (redo) redo->store(manager);
    }

    // remove the meta data from the signal manager
    manager.metaData().deleteRange(m_offset, m_length);

    return redo;
}

//***************************************************************************
//***************************************************************************