[kwave] /: allow special value -1 as index for label:delete(...) to delete _all_ labels

Thomas Eschenbacher null at kde.org
Sat Feb 25 12:52:04 UTC 2017


Git commit bd05f88d5dd1de5dd6f3dbd3dd3220cdf3d43f9e by Thomas Eschenbacher.
Committed on 25/02/2017 at 12:52.
Pushed by eschenbacher into branch 'master'.

allow special value -1 as index for label:delete(...) to delete _all_ labels

M  +1    -0    CHANGES
M  +5    -3    doc/en/index.docbook
M  +32   -9    libkwave/SignalManager.cpp

https://commits.kde.org/kwave/bd05f88d5dd1de5dd6f3dbd3dd3220cdf3d43f9e

diff --git a/CHANGES b/CHANGES
index 70d85c0d..fad166c8 100644
--- a/CHANGES
+++ b/CHANGES
@@ -44,6 +44,7 @@
   * renamed command "delete_label" -> "label:delete"
   * renamed command "edit_label"   -> "label:edit"
   * implemented loading and saving of labels
+  * allow special value -1 as index for label:delete to delete _all_ labels
 
 0.9.2 [2016-06-26]
 
diff --git a/doc/en/index.docbook b/doc/en/index.docbook
index 59403a03..89218ac7 100644
--- a/doc/en/index.docbook
+++ b/doc/en/index.docbook
@@ -3320,7 +3320,7 @@
 	<!-- @COMMAND@ delete_track(index) -->
 	<sect2 id="cmd_sect_delete_track"><title id="cmd_title_delete_track">&no-i18n-cmd_delete_track;</title>
 	<simplesect>
-	    <title>&i18n-cmd_syntax;<command>&no-i18n-cmd_label_delete;</command>(<replaceable>index</replaceable>)</title>
+	    <title>&i18n-cmd_syntax;<command>&no-i18n-cmd_delete_track;</command>(<replaceable>index</replaceable>)</title>
 	    <para>
 		Deletes a track, identified by its index (starting from zero).
 		If no track with the given index exists, this command exits with an error.
@@ -3483,12 +3483,14 @@
 	    <simplesect>
 		<title>&i18n-cmd_syntax;<command>&no-i18n-cmd_label_delete;</command>(<replaceable>index</replaceable>)</title>
 		<para>
-		    Deletes a label, identified by its index (starting from zero).
+		    Deletes a label, identified by its index (starting from zero), or all
+		    labels when using the special value -1 as index.
 		    If no label with the given index exists, this command does nothing.
 		</para>
 	    </simplesect>
 	    <simplesect><title>Parameters</title><informaltable frame='none'><tgroup cols='2'><tbody>
-			    <row><entry><parameter>index</parameter>:</entry><entry>index of the label to delete, starting with 0</entry></row>
+			    <row><entry><parameter>index</parameter>:</entry><entry>index of the label to delete,
+			    starting with 0 or -1 to delete all labels</entry></row>
 			    </tbody></tgroup></informaltable></simplesect>
 	</sect2>
 
diff --git a/libkwave/SignalManager.cpp b/libkwave/SignalManager.cpp
index a21ad8e9..48f4554b 100644
--- a/libkwave/SignalManager.cpp
+++ b/libkwave/SignalManager.cpp
@@ -1905,20 +1905,43 @@ Kwave::Label Kwave::SignalManager::addLabel(sample_index_t pos,
 void Kwave::SignalManager::deleteLabel(int index, bool with_undo)
 {
     Kwave::LabelList labels(m_meta_data);
+    int count = Kwave::toInt(labels.count());
+    if (!count) return;
 
-    if ((index < 0) || (index >= Kwave::toInt(labels.count()))) return;
+    if (index == -1) {
+	// special handling for index == -1 -> delete all labels
 
-    Kwave::MetaData label(labels.at(index));
+	if (with_undo) startUndoTransaction(i18n("Delete All Labels"));
 
-    // register the undo action
-    if (with_undo) {
-	Kwave::UndoTransactionGuard undo(*this, i18n("Delete Label"));
-	if (!registerUndoAction(new(std::nothrow)
-	    UndoDeleteMetaDataAction(Kwave::MetaDataList(label))))
-	    return;
+	for (index = count - 1; index >= 0; index--) {
+	    Kwave::MetaData label(labels.at(index));
+	    if (with_undo) {
+		if (!registerUndoAction(new(std::nothrow)
+		    UndoDeleteMetaDataAction(Kwave::MetaDataList(label))))
+		    break;
+	    }
+	    m_meta_data.remove(label);
+	}
+    } else {
+	// delete a single label
+	if ((index < 0) || (index >= count)) return;
+
+	Kwave::MetaData label(labels.at(index));
+
+	// register the undo action
+	if (with_undo) {
+	    startUndoTransaction(i18n("Delete Label"));
+	    if (!registerUndoAction(new(std::nothrow)
+		UndoDeleteMetaDataAction(Kwave::MetaDataList(label)))) {
+		abortUndoTransaction();
+		return;
+	    }
+	}
+
+	m_meta_data.remove(label);
     }
 
-    m_meta_data.remove(label);
+    if (with_undo) closeUndoTransaction();
 
     // register this as a modification
     setModified(true);


More information about the kde-doc-english mailing list