File: UndoDeleteMetaDataAction.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 (151 lines) | stat: -rw-r--r-- 5,273 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
/***************************************************************************
 UndoAddLabelAction.cpp  -  Undo action for deleting 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 <new>

#include <KLocalizedString>

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

//***************************************************************************
Kwave::UndoDeleteMetaDataAction::UndoDeleteMetaDataAction(
    const Kwave::MetaDataList &meta_data)
    :UndoAction(), m_meta_data(meta_data)
{
}

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

//***************************************************************************
QString Kwave::UndoDeleteMetaDataAction::description()
{
    // sanity check: list should not be empty
    Q_ASSERT(!m_meta_data.isEmpty());
    if (m_meta_data.isEmpty()) return _("");

    QString name;
    {
        const Kwave::MetaData &m = m_meta_data.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 ((m_meta_data.count() == 1) && name.length()) {
        return i18nc(
            "name of the undo action for deleting a meta data object",
            "Delete %1",
            name
        );
    }

    // check if the list contains only objects of the same type
    bool all_same_type = true;
    foreach (const Kwave::MetaData &m, 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) {
        return i18nc(
            "name of the undo action for deleting multiple "
            "meta data objects of the same type: "
            "%1=number of elements, %2=name of one element in singular",
            "Delete %1 %2 objects", m_meta_data.count(), name
        );
    }

    return i18n("Delete Meta Data");
}

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

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

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

//***************************************************************************
Kwave::UndoAction *Kwave::UndoDeleteMetaDataAction::undo(
    Kwave::SignalManager &manager, bool with_redo)
{
    Q_ASSERT(!m_meta_data.isEmpty());
    if (m_meta_data.isEmpty()) return nullptr;

    Kwave::UndoAction *redo = nullptr;

    // add the stored meta data to the signal managers' meta data
    manager.metaData().add(m_meta_data);

    // store data for redo
    if (with_redo) {
        redo = new(std::nothrow) Kwave::UndoAddMetaDataAction(m_meta_data);
        Q_ASSERT(redo);
        if (redo) redo->store(manager);
    }

    return redo;
}

//***************************************************************************
void Kwave::UndoDeleteMetaDataAction::dump(const QString &indent)
{
    foreach (const Kwave::MetaData &m, m_meta_data) {
        qDebug("%sundo delete meta data object '%s'",
               DBG(indent), DBG(m.id()));

        // dump all properties of the object
        foreach (const QString &key, m.keys()) {
            QVariant v = m[key];
            QString value;
            if (v.typeId() == QMetaType::QVariantList) {
                foreach (const QVariant &v1, v.toList())
                    value += _("'") + v1.toString() + _("' ");
            } else {
                value = v.toString();
            }
            qDebug("%s    '%s' = '%s", DBG(indent), DBG(key), DBG(value));
        }
    }
}

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