kwave  18.07.70
LabelItem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  LabelItem.cpp - label item within a SignalView
3  -------------------
4  begin : Sat Mar 26 2011
5  copyright : (C) 2011 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QAction>
21 #include <QIcon>
22 #include <QMenu>
23 
24 #include <KLocalizedString>
25 
26 #include "libkwave/Label.h"
27 #include "libkwave/SignalManager.h"
28 #include "libkwave/String.h"
29 #include "libkwave/Utils.h"
32 
33 #include "libgui/LabelItem.h"
34 #include "libgui/SignalView.h"
35 
36 //***************************************************************************
38  Kwave::SignalManager &signal_manager,
39  unsigned int index,
40  const Kwave::Label &label)
41  :Kwave::ViewItem(view, signal_manager),
42  m_index(index),
43  m_initial_pos(label.pos()),
44  m_current_pos(label.pos()),
45  m_description(label.name()),
46  m_undo_transaction(Q_NULLPTR)
47 {
48 }
49 
50 //***************************************************************************
52 {
53  if (m_undo_transaction) {
54  // restore the previous data
55  qDebug("Kwave::LabelItem::~LabelItem() -> aborted -> reverting!");
57  delete m_undo_transaction;
58  m_undo_transaction = Q_NULLPTR;
59  }
60 }
61 
62 //***************************************************************************
63 Kwave::ViewItem::Flags Kwave::LabelItem::flags() const
64 {
66 }
67 
68 //***************************************************************************
70 {
71  Q_UNUSED(ofs);
72 
73  QString description = (m_description.length()) ?
74  i18nc("tooltip of a label, %1=index, %2=description/name",
75  "Label #%1 (%2)", m_index, m_description) :
76  i18nc("tooltip of a label, without description, %1=index",
77  "Label #%1", m_index);
78 
80  QString tip = _("%1\n%2\n%3").arg(description).arg(m_current_pos).arg(hms);
81 
82  return tip;
83 }
84 
85 //***************************************************************************
87 {
88  Q_ASSERT(parent);
89  if (!parent) return;
90 
91  // locate the "label" menu
92  QMenu *label_menu = Q_NULLPTR;
93  foreach (const QAction *action, parent->actions()) {
94  if (action->text() == i18n("Label")) {
95  label_menu = action->menu();
96  break;
97  }
98  }
99 
100  // the context menu of a label has been activated
101  if (label_menu) {
102 
103  // find the "New" action and disable it
104  foreach (QAction *action, label_menu->actions()) {
105  if (action->text() == i18n("New")) {
106  action->setEnabled(false);
107  break;
108  }
109  }
110 
111  QAction *action_label_delete = label_menu->addAction(
112  QIcon::fromTheme(_("list-remove")),
113  i18n("&Delete"), this, SLOT(contextMenuLabelDelete()));
114  Q_ASSERT(action_label_delete);
115  if (!action_label_delete) return;
116 
117  QAction *action_label_properties = label_menu->addAction(
118  QIcon::fromTheme(_("configure")),
119  i18n("&Properties..."), this, SLOT(contextMenuLabelProperties()));
120  Q_ASSERT(action_label_properties);
121  if (!action_label_properties) return;
122  }
123 
124 }
125 
126 //***************************************************************************
128 {
129  emit sigCommand(_("label:delete(%1)").arg(m_index));
130 }
131 
132 //***************************************************************************
134 {
135  emit sigCommand(_("nomacro:label:edit(%1)").arg(m_index));
136 }
137 
138 //***************************************************************************
140 {
141  return Qt::SizeHorCursor;
142 }
143 
144 //***************************************************************************
145 void Kwave::LabelItem::moveTo(const QPoint &mouse_pos)
146 {
147  const sample_index_t new_pos = m_view.offset() +
148  m_view.pixels2samples(mouse_pos.x());
149 
150  Kwave::Label label = m_signal_manager.findLabel(new_pos);
151  if (label.isNull()) {
152 
153  // this is the first move ?
154  if (!m_undo_transaction) {
155  // there probably will be something to undo later
156  // create an undo transaction guard
157  m_undo_transaction = new(std::nothrow)
159  m_signal_manager, i18n("Move Label"));
160  Q_ASSERT(m_undo_transaction);
161  if (!m_undo_transaction) return;
162 
163  // save the previous label data for undo
165  if (!m_undo_transaction->registerUndoAction(new(std::nothrow)
167  qWarning("Kwave::LabelItem::done(): saving undo data failed!");
168  return;
169  }
170  }
171 
172  if (m_signal_manager.modifyLabel(m_index, new_pos,
173  m_description, false)) {
174  Kwave::Label lbl = m_signal_manager.findLabel(new_pos);
175  if (!lbl.isNull()) {
177  m_current_pos = lbl.pos();
178  }
179  }
180  }
181 }
182 
183 //***************************************************************************
185 {
186  if (m_undo_transaction) {
187  // close the undo transaction (regularly, with undo action)
190  delete m_undo_transaction;
191  m_undo_transaction = Q_NULLPTR;
192  }
193 }
194 
195 //***************************************************************************
196 //***************************************************************************
virtual void appendContextMenu(QMenu *parent) Q_DECL_OVERRIDE
Definition: LabelItem.cpp:86
void contextMenuLabelProperties()
Definition: LabelItem.cpp:133
sample_index_t m_current_pos
Definition: LabelItem.h:113
Definition: App.h:33
sample_index_t m_initial_pos
Definition: LabelItem.h:110
void contextMenuLabelDelete()
Definition: LabelItem.cpp:127
virtual void done() Q_DECL_OVERRIDE
Definition: LabelItem.cpp:184
QString Q_DECL_EXPORT ms2hms(double ms)
Definition: Utils.cpp:104
virtual sample_index_t pos() const
Definition: Label.cpp:56
virtual QCursor mouseCursor() const Q_DECL_OVERRIDE
Definition: LabelItem.cpp:139
unsigned int m_index
Definition: LabelItem.h:107
quint64 sample_index_t
Definition: Sample.h:28
sample_index_t offset() const
Definition: SignalView.h:110
Kwave::SignalManager & m_signal_manager
Definition: ViewItem.h:119
Kwave::SignalView & m_view
Definition: ViewItem.h:116
bool registerUndoAction(UndoAction *action)
virtual ~LabelItem() Q_DECL_OVERRIDE
Definition: LabelItem.cpp:51
virtual QString toolTip(sample_index_t &ofs) Q_DECL_OVERRIDE
Definition: LabelItem.cpp:69
double samples2ms(sample_index_t samples)
Definition: SignalView.cpp:162
virtual Kwave::ViewItem::Flags flags() const Q_DECL_OVERRIDE
Definition: LabelItem.cpp:63
const char name[16]
Definition: memcpy.c:510
int labelIndex(const Kwave::Label &label) const
QString m_description
Definition: LabelItem.h:116
virtual bool isNull() const
Definition: MetaData.cpp:69
void sigCommand(const QString &command)
sample_index_t pixels2samples(int pixels) const
Definition: SignalView.cpp:155
#define _(m)
Definition: memcpy.c:66
Kwave::UndoTransactionGuard * m_undo_transaction
Definition: LabelItem.h:119
Kwave::Label findLabel(sample_index_t pos)
bool modifyLabel(int index, sample_index_t pos, const QString &name, bool with_undo)
virtual void moveTo(const QPoint &mouse_pos) Q_DECL_OVERRIDE
Definition: LabelItem.cpp:145
LabelItem(Kwave::SignalView &view, Kwave::SignalManager &signal_manager, unsigned int index, const Kwave::Label &label)
Definition: LabelItem.cpp:37