changeset 2242:e9c77a4c865e single-point

Add /exportlayer OSC command
author Chris Cannam
date Thu, 28 Mar 2019 14:34:29 +0000
parents c517286ee999
children 33e62bc04685
files README.OSC main/MainWindow.cpp main/OSCHandler.cpp
diffstat 3 files changed, 32 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/README.OSC	Thu Mar 28 13:42:03 2019 +0000
+++ b/README.OSC	Thu Mar 28 14:34:29 2019 +0000
@@ -86,6 +86,13 @@
      WAV file.  This action will try to fail rather than overwrite
      an existing file, but you probably shouldn't rely on that.
 
+  /exportlayer <filename>
+
+     Export the current layer to a file, of type determined from the
+     file's suffix. See /setcurrent for how to change which layer is
+     current. This action will try to fail rather than overwrite an
+     existing file, but you probably shouldn't rely on that.
+
   /jump <t>
   /jump end
   /jump selection
--- a/main/MainWindow.cpp	Thu Mar 28 13:42:03 2019 +0000
+++ b/main/MainWindow.cpp	Thu Mar 28 14:34:29 2019 +0000
@@ -85,7 +85,6 @@
 #include "layer/ColourDatabase.h"
 #include "widgets/ModelDataTableDialog.h"
 #include "rdf/PluginRDFIndexer.h"
-#include "rdf/RDFExporter.h"
 
 #include "Surveyer.h"
 #include "NetworkPermissionTester.h"
@@ -3002,81 +3001,14 @@
     if (!model) return;
 
     FileFinder::FileType type = FileFinder::LayerFileNoMidi;
-
     if (dynamic_cast<NoteModel *>(model)) type = FileFinder::LayerFile;
-
     QString path = getSaveFileName(type);
 
     if (path == "") return;
 
-    if (QFileInfo(path).suffix() == "") path += ".svl";
-
-    QString suffix = QFileInfo(path).suffix().toLower();
-
     QString error;
 
-    if (suffix == "xml" || suffix == "svl") {
-
-        QFile file(path);
-        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
-            error = tr("Failed to open file %1 for writing").arg(path);
-        } else {
-            QTextStream out(&file);
-            out.setCodec(QTextCodec::codecForName("UTF-8"));
-            out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-                << "<!DOCTYPE sonic-visualiser>\n"
-                << "<sv>\n"
-                << "  <data>\n";
-
-            model->toXml(out, "    ");
-
-            out << "  </data>\n"
-                << "  <display>\n";
-
-            layer->toXml(out, "    ");
-
-            out << "  </display>\n"
-                << "</sv>\n";
-        }
-
-    } else if (suffix == "mid" || suffix == "midi") {
-
-        NoteModel *nm = dynamic_cast<NoteModel *>(model);
-
-        if (!nm) {
-            error = tr("Can't export non-note layers to MIDI");
-        } else {
-            MIDIFileWriter writer(path, nm, nm->getSampleRate());
-            writer.write();
-            if (!writer.isOK()) {
-                error = writer.getError();
-            }
-        }
-
-    } else if (suffix == "ttl" || suffix == "n3") {
-
-        if (!RDFExporter::canExportModel(model)) {
-            error = tr("Sorry, cannot export this layer type to RDF (supported types are: region, note, text, time instants, time values)");
-        } else {
-            RDFExporter exporter(path, model);
-            exporter.write();
-            if (!exporter.isOK()) {
-                error = exporter.getError();
-            }
-        }
-
-    } else {
-
-        CSVFileWriter writer(path, model,
-                             ((suffix == "csv") ? "," : "\t"));
-        writer.write();
-
-        if (!writer.isOK()) {
-            error = writer.getError();
-        }
-    }
-
-    if (error != "") {
+    if (!exportLayerTo(layer, path, error)) {
         QMessageBox::critical(this, tr("Failed to write file"), error);
     } else {
         m_recentFiles.addFile(path);
--- a/main/OSCHandler.cpp	Thu Mar 28 13:42:03 2019 +0000
+++ b/main/OSCHandler.cpp	Thu Mar 28 14:34:29 2019 +0000
@@ -118,6 +118,30 @@
             }
         }
 
+    } else if (message.getMethod() == "exportlayer") {
+
+        QString path;
+        if (message.getArgCount() == 1 &&
+            message.getArg(0).canConvert(QVariant::String)) {
+            path = message.getArg(0).toString();
+            if (QFileInfo(path).exists()) {
+                SVDEBUG << "OSCHandler: Refusing to overwrite existing file in layer export" << endl;
+            } else {
+                Pane *currentPane = nullptr;
+                Layer *currentLayer = nullptr;
+                if (m_paneStack) currentPane = m_paneStack->getCurrentPane();
+                if (currentPane) currentLayer = currentPane->getSelectedLayer();
+                if (currentLayer) {
+                    QString error;
+                    if (!exportLayerTo(currentLayer, path, error)) {
+                        SVCERR << "OSCHandler: Failed to export current layer to " << path << ": " << error << endl;
+                    }
+                } else {
+                    SVCERR << "OSCHandler: No current layer to export" << endl;
+                }
+            }
+        }
+
     } else if (message.getMethod() == "jump" ||
                message.getMethod() == "play") {