File: LabelItem.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 (197 lines) | stat: -rw-r--r-- 7,024 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
/***************************************************************************
           LabelItem.cpp -  label item within a SignalView
                             -------------------
    begin                : Sat Mar 26 2011
    copyright            : (C) 2011 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.                                   *
 *                                                                         *
 ***************************************************************************/


#include <QAction>
#include <QIcon>
#include <QMenu>

#include <KLocalizedString>

#include "libkwave/Label.h"
#include "libkwave/SignalManager.h"
#include "libkwave/String.h"
#include "libkwave/Utils.h"
#include "libkwave/undo/UndoModifyMetaDataAction.h"
#include "libkwave/undo/UndoTransactionGuard.h"

#include "libgui/LabelItem.h"
#include "libgui/SignalView.h"

//***************************************************************************
Kwave::LabelItem::LabelItem(Kwave::SignalView &view,
                            Kwave::SignalManager &signal_manager,
                            unsigned int index,
                            const Kwave::Label &label)
    :Kwave::ViewItem(view, signal_manager),
     m_index(index),
     m_initial_pos(label.pos()),
     m_current_pos(label.pos()),
     m_description(label.name()),
     m_undo_transaction(nullptr)
{
}

//***************************************************************************
Kwave::LabelItem::~LabelItem()
{
    if (m_undo_transaction) {
        // restore the previous data
        qDebug("Kwave::LabelItem::~LabelItem() -> aborted -> reverting!");
        m_undo_transaction->abort();
        delete m_undo_transaction;
        m_undo_transaction = nullptr;
    }
}

//***************************************************************************
Kwave::ViewItem::Flags Kwave::LabelItem::flags() const
{
    return Kwave::ViewItem::CanGrabAndMove;
}

//***************************************************************************
QString Kwave::LabelItem::toolTip(sample_index_t &ofs)
{
    Q_UNUSED(ofs)

    QString description = (m_description.length()) ?
        i18nc("tooltip of a label, %1=index, %2=description/name",
                "Label #%1 (%2)", m_index, m_description) :
        i18nc("tooltip of a label, without description, %1=index",
                "Label #%1", m_index);

    QString hms = Kwave::ms2hms(m_view.samples2ms(m_current_pos));
    QString tip = _("%1\n%2\n%3").arg(description).arg(m_current_pos).arg(hms);

    return tip;
}

//***************************************************************************
void Kwave::LabelItem::appendContextMenu(QMenu *parent)
{
    Q_ASSERT(parent);
    if (!parent) return;

    // locate the "label" menu
    QMenu *label_menu = nullptr;
    foreach (const QAction *action, parent->actions()) {
        if (action->text() == i18n("Label")) {
            label_menu = action->menu();
            break;
        }
    }

    // the context menu of a label has been activated
    if (label_menu) {

        // find the "New" action and disable it
        foreach (QAction *action, label_menu->actions()) {
            if (action->text() == i18n("New")) {
                action->setEnabled(false);
                break;
            }
        }

        QAction *action_label_delete = label_menu->addAction(
            QIcon::fromTheme(_("list-remove")),
            i18n("&Delete"), this, SLOT(contextMenuLabelDelete()));
        Q_ASSERT(action_label_delete);
        if (!action_label_delete) return;

        QAction *action_label_properties = label_menu->addAction(
            QIcon::fromTheme(_("configure")),
            i18n("&Properties..."), this, SLOT(contextMenuLabelProperties()));
        Q_ASSERT(action_label_properties);
        if (!action_label_properties) return;
    }

}

//***************************************************************************
void Kwave::LabelItem::contextMenuLabelDelete()
{
    emit sigCommand(_("label:delete(%1)").arg(m_index));
}

//***************************************************************************
void Kwave::LabelItem::contextMenuLabelProperties()
{
    emit sigCommand(_("nomacro:label:edit(%1)").arg(m_index));
}

//***************************************************************************
QCursor Kwave::LabelItem::mouseCursor() const
{
    return Qt::SizeHorCursor;
}

//***************************************************************************
void Kwave::LabelItem::moveTo(const QPoint &mouse_pos)
{
    const sample_index_t new_pos = m_view.offset() +
                                   m_view.pixels2samples(mouse_pos.x());

    Kwave::Label label = m_signal_manager.findLabel(new_pos);
    if (label.isNull()) {

        // this is the first move ?
        if (!m_undo_transaction) {
            // there probably will be something to undo later
            // create an undo transaction guard
            m_undo_transaction = new(std::nothrow)
                Kwave::UndoTransactionGuard(
                    m_signal_manager, i18n("Move Label"));
            Q_ASSERT(m_undo_transaction);
            if (!m_undo_transaction) return;

            // save the previous label data for undo
            Kwave::Label lbl = m_signal_manager.findLabel(m_initial_pos);
            if (!m_undo_transaction->registerUndoAction(new(std::nothrow)
                UndoModifyMetaDataAction(Kwave::MetaDataList(lbl)))) {
                qWarning("Kwave::LabelItem::done(): saving undo data failed!");
                return;
            }
        }

        if (m_signal_manager.modifyLabel(m_index, new_pos,
                                         m_description, false)) {
            Kwave::Label lbl = m_signal_manager.findLabel(new_pos);
            if (!lbl.isNull()) {
                m_index       = m_signal_manager.labelIndex(lbl);
                m_current_pos = lbl.pos();
            }
        }
    }
}

//***************************************************************************
void Kwave::LabelItem::done()
{
    if (m_undo_transaction) {
        // close the undo transaction (regularly, with undo action)
        if (m_current_pos == m_initial_pos)
            m_undo_transaction->abort();
        delete m_undo_transaction;
        m_undo_transaction = nullptr;
    }
}

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

#include "moc_LabelItem.cpp"