changeset 425:eaab4c65c7bc templating

Add a bit more substance to the templates menu, and improve some naming
author Chris Cannam
date Mon, 09 May 2011 16:57:29 +0100
parents 944167b96d01
children 72a2b4210e50
files main/MainWindow.cpp main/MainWindow.h sonic-visualiser.qrc
diffstat 3 files changed, 83 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/main/MainWindow.cpp	Mon May 09 16:56:50 2011 +0100
+++ b/main/MainWindow.cpp	Mon May 09 16:57:29 2011 +0100
@@ -113,6 +113,7 @@
 #include <QCheckBox>
 #include <QRegExp>
 #include <QScrollArea>
+#include <QDesktopServices>
 
 #include <iostream>
 #include <cstdio>
@@ -431,11 +432,17 @@
 
     menu->addSeparator();
 
-    m_templatesMenu = menu->addMenu(tr("Set Session Open Template"));
+    QString templatesMenuLabel = tr("Session Template for Audio Files");
+
+#ifdef Q_OS_MAC
+    // Normally this menu will go next to Preferences on the File
+    // menu.  But on OS/X Preferences doesn't appear on the File menu,
+    // so we put it next to the other Session stuff instead.
+    m_templatesMenu = menu->addMenu(templatesMenuLabel);
     m_templatesMenu->setTearOffEnabled(true);
     setupTemplatesMenu();
-
     menu->addSeparator();
+#endif
 
     icon = il.load("filesave");
     icon.addPixmap(il.loadPixmap("filesave-22"));
@@ -504,6 +511,13 @@
 
     menu->addSeparator();
 
+#ifndef Q_OS_MAC
+    // See note for Q_OS_MAC alternative above
+    m_templatesMenu = menu->addMenu(templatesMenuLabel);
+    m_templatesMenu->setTearOffEnabled(true);
+    setupTemplatesMenu();
+#endif
+
     action = new QAction(tr("&Preferences..."), this);
     action->setStatusTip(tr("Adjust the application preferences"));
     connect(action, SIGNAL(triggered()), this, SLOT(preferences()));
@@ -1637,7 +1651,7 @@
     QSettings settings;
     settings.beginGroup("MainWindow");
     QString deflt = settings.value("sessiontemplate", "").toString();
-    setDefaultSessionTemplate(deflt);
+    setDefaultSessionTemplate(deflt == "" ? "default" : "");
     settings.endGroup();
 
     QActionGroup *templatesGroup = new QActionGroup(this);
@@ -1652,24 +1666,37 @@
 
     m_templatesMenu->addSeparator();
 
-    QStringList templates = ResourceFinder().getResourceFiles("templates", "xml");
+    QStringList templates = ResourceFinder().getResourceFiles("templates", "svt");
+
+    bool havePersonal = false;
+
+    // (ordered by name)
+    std::set<QString> byName;
     foreach (QString t, templates) {
-        QString tname = QFileInfo(t).baseName();
-        if (tname.toLower() == "default") continue;
-        action = new QAction(tname, this);
-        action->setObjectName(t);
+        if (!t.startsWith(":")) havePersonal = true;
+        byName.insert(QFileInfo(t).baseName());
+    }
+
+    foreach (QString t, byName) {
+        if (t.toLower() == "default") continue;
+        action = new QAction(t, this);
         connect(action, SIGNAL(triggered()), this, SLOT(changeTemplate()));
         action->setCheckable(true);
-        action->setChecked(deflt == tname);
+        action->setChecked(deflt == t);
         templatesGroup->addAction(action);
         m_templatesMenu->addAction(action);
     }
 
     if (!templates.empty()) m_templatesMenu->addSeparator();
 
-    action = new QAction(tr("Save Session as New Template..."), this);
+    action = new QAction(tr("Save Template from Current Session..."), this);
     connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate()));
     m_templatesMenu->addAction(action);
+
+    action = new QAction(tr("Manage Saved Templates"), this);
+    connect(action, SIGNAL(triggered()), this, SLOT(manageSavedTemplates()));
+    action->setEnabled(havePersonal);
+    m_templatesMenu->addAction(action);
 }
 
 void
@@ -2803,7 +2830,46 @@
 void
 MainWindow::changeTemplate()
 {
-    //!!!
+    QObject *s = sender();
+    QAction *action = qobject_cast<QAction *>(s);
+
+    if (!action) {
+	std::cerr << "WARNING: MainWindow::changeTemplate: sender is not an action"
+		  << std::endl;
+	return;
+    }
+
+    QString n = action->objectName();
+    if (n == "") n = action->text();
+
+    if (n == "") {
+        std::cerr << "WARNING: MainWindow::changeTemplate: sender has no name"
+                  << std::endl;
+        return;
+    }
+
+    setDefaultSessionTemplate(n);
+}
+
+void
+MainWindow::saveSessionAsTemplate()
+{
+    QString name = QInputDialog::getText
+        (this, tr("Enter template name"),
+         tr("Please enter a name for the saved template:"));
+    if (name == "") return;
+    
+    //!!! sanitise!
+
+    
+}
+
+void
+MainWindow::manageSavedTemplates()
+{
+    //!!! really we should watch this directory and recreate the menu when it changes
+    ResourceFinder rf;
+    QDesktopServices::openUrl("file:" + rf.getResourceSaveDir("templates"));
 }
 
 void
--- a/main/MainWindow.h	Mon May 09 16:56:50 2011 +0100
+++ b/main/MainWindow.h	Mon May 09 16:57:29 2011 +0100
@@ -171,6 +171,9 @@
     virtual void midiEventsAvailable();
     virtual void playStatusChanged(bool);
 
+    virtual void saveSessionAsTemplate();
+    virtual void manageSavedTemplates();
+
     virtual void website();
     virtual void help();
     virtual void about();
--- a/sonic-visualiser.qrc	Mon May 09 16:56:50 2011 +0100
+++ b/sonic-visualiser.qrc	Mon May 09 16:57:29 2011 +0100
@@ -104,8 +104,9 @@
     <file>samples/stick.wav</file>
     <file>samples/strike.wav</file>
     <file>samples/tap.wav</file>
-    <file>templates/default.xml</file>
-    <file>templates/testtemplate.xml</file>
+    <file>templates/default.svt</file>
+    <file>templates/tk.svt</file>
+    <file>templates/testtemplate.svt</file>
     <file>i18n/sonic-visualiser_ru.qm</file>
     <file>i18n/sonic-visualiser_en_GB.qm</file>
     <file>i18n/sonic-visualiser_en_US.qm</file>