changeset 436:6d827453657f templating

Add default session template to Preferences; tidy up menu functions and states
author Chris Cannam
date Mon, 23 May 2011 16:13:30 +0100 (2011-05-23)
parents 586eb6764a2b
children e3644cd9cf99
files main/MainWindow.cpp main/MainWindow.h main/PreferencesDialog.cpp main/PreferencesDialog.h
diffstat 4 files changed, 130 insertions(+), 125 deletions(-) [+]
line wrap: on
line diff
--- a/main/MainWindow.cpp	Tue May 17 17:22:30 2011 +0100
+++ b/main/MainWindow.cpp	Mon May 23 16:13:30 2011 +0100
@@ -505,16 +505,24 @@
     QString templatesMenuLabel = tr("Apply Session Template");
     m_templatesMenu = menu->addMenu(templatesMenuLabel);
     m_templatesMenu->setTearOffEnabled(true);
-    setupTemplatesMenu();
+    // We need to have a main model for this option to be useful:
+    // canExportAudio captures that
+    connect(this, SIGNAL(canExportAudio(bool)), m_templatesMenu, SLOT(setEnabled(bool)));
+
+    // Set up the menu in a moment, after m_manageTemplatesAction constructed
 
     action = new QAction(tr("Export Session as Template..."), this);
     connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate()));
+    // We need to have something in the session for this to be useful:
+    // canDeleteCurrentLayer captures that
+    connect(this, SIGNAL(canExportAudio(bool)), action, SLOT(setEnabled(bool)));
     menu->addAction(action);
 
-    action = new QAction(tr("Manage Saved Templates"), this);
-    connect(action, SIGNAL(triggered()), this, SLOT(manageSavedTemplates()));
-//!!!    action->setEnabled(havePersonal);
-    menu->addAction(action);
+    m_manageTemplatesAction = new QAction(tr("Manage Exported Templates"), this);
+    connect(m_manageTemplatesAction, SIGNAL(triggered()), this, SLOT(manageSavedTemplates()));
+    menu->addAction(m_manageTemplatesAction);
+
+    setupTemplatesMenu();
 
     action = new QAction(tr("&Preferences..."), this);
     action->setStatusTip(tr("Adjust the application preferences"));
@@ -1646,7 +1654,7 @@
 {
     m_templatesMenu->clear();
 
-    QAction *defaultAction = new QAction(tr("Default"), this);
+    QAction *defaultAction = new QAction(tr("Classic Waveform"), this);
     defaultAction->setObjectName("default");
     connect(defaultAction, SIGNAL(triggered()), this, SLOT(applyTemplate()));
     m_templatesMenu->addAction(defaultAction);
@@ -1657,9 +1665,12 @@
 
     QStringList templates = ResourceFinder().getResourceFiles("templates", "svt");
 
+    bool havePersonal = false;
+
     // (ordered by name)
     std::set<QString> byName;
     foreach (QString t, templates) {
+        if (!t.startsWith(":")) havePersonal = true;
         byName.insert(QFileInfo(t).baseName());
     }
 
@@ -1678,91 +1689,16 @@
         connect(m_templateWatcher, SIGNAL(directoryChanged(const QString &)),
                 this, SLOT(setupTemplatesMenu()));
     }
+
+    QAction *setDefaultAction = new QAction(tr("Choose Default Template..."), this);
+    setDefaultAction->setObjectName("set_default_template");
+    connect(setDefaultAction, SIGNAL(triggered()), this, SLOT(preferences()));
+    m_templatesMenu->addSeparator();
+    m_templatesMenu->addAction(setDefaultAction);
+
+    m_manageTemplatesAction->setEnabled(havePersonal);
 }
 
-#ifdef NOT_DEFINED
-//!!!
-void
-MainWindow::setupTemplatesMenu()
-{
-    m_templatesMenu->clear();
-
-    QSettings settings;
-    settings.beginGroup("MainWindow");
-    QString deflt = settings.value("sessiontemplate", "").toString();
-    setDefaultSessionTemplate(deflt == "" ? "default" : deflt);
-
-    QActionGroup *templatesGroup = new QActionGroup(this);
-
-    bool haveFoundDefault = false;
-
-    QAction *defaultAction = new QAction(tr("Default"), this);
-    defaultAction->setObjectName("default");
-    connect(defaultAction, SIGNAL(triggered()), this, SLOT(applyTemplate()));
-/
-    defaultAction->setCheckable(true);
-    if (deflt == "" || deflt == "default") {
-        defaultAction->setChecked(true);
-        haveFoundDefault = true;
-    }
-    templatesGroup->addAction(defaultAction);
-    m_templatesMenu->addAction(defaultAction);
-
-    m_templatesMenu->addSeparator();
-
-    QAction *action = 0;
-
-    QStringList templates = ResourceFinder().getResourceFiles("templates", "svt");
-
-    bool havePersonal = false;
-
-    // (ordered by name)
-    std::set<QString> byName;
-    foreach (QString t, templates) {
-        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(applyTemplate()));
-        action->setCheckable(true);
-        if (deflt == t) {
-            action->setChecked(true);
-            haveFoundDefault = true;
-        }
-        templatesGroup->addAction(action);
-        m_templatesMenu->addAction(action);
-    }
-
-    if (!templates.empty()) m_templatesMenu->addSeparator();
-
-    if (!haveFoundDefault) {
-        defaultAction->setChecked(true);
-        setDefaultSessionTemplate("default");
-        settings.setValue("sessiontemplate", "");
-    }
-
-    settings.endGroup();
-/*!!!
-    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);
-*/
-    if (!m_templateWatcher) {
-        m_templateWatcher = new QFileSystemWatcher(this);
-        m_templateWatcher->addPath(ResourceFinder().getResourceSaveDir("templates"));
-        connect(m_templateWatcher, SIGNAL(directoryChanged(const QString &)),
-                this, SLOT(setupTemplatesMenu()));
-    }
-}
-#endif
 
 void
 MainWindow::setupRecentTransformsMenu()
@@ -2926,35 +2862,6 @@
 }
 
 void
-MainWindow::changeDefaultTemplate()
-{
-    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);
-
-    QSettings settings;
-    settings.beginGroup("MainWindow");
-    settings.setValue("sessiontemplate", n);
-    settings.endGroup();
-}
-
-void
 MainWindow::saveSessionAsTemplate()
 {
     QString name = QInputDialog::getText
@@ -4155,9 +4062,15 @@
 void
 MainWindow::preferences()
 {
+    bool goToTemplateTab =
+        (sender() && sender()->objectName() == "set_default_template");
+
     if (!m_preferencesDialog.isNull()) {
         m_preferencesDialog->show();
         m_preferencesDialog->raise();
+        if (goToTemplateTab) {
+            m_preferencesDialog->switchToTab(PreferencesDialog::TemplateTab);
+        }
         return;
     }
 
@@ -4172,6 +4085,9 @@
     m_preferencesDialog->setAttribute(Qt::WA_DeleteOnClose);
 
     m_preferencesDialog->show();
+    if (goToTemplateTab) {
+        m_preferencesDialog->switchToTab(PreferencesDialog::TemplateTab);
+    }
 }
 
 void
--- a/main/MainWindow.h	Tue May 17 17:22:30 2011 +0100
+++ b/main/MainWindow.h	Mon May 23 16:13:30 2011 +0100
@@ -85,7 +85,6 @@
     virtual void openLocation();
     virtual void openRecentFile();
     virtual void applyTemplate();
-    virtual void changeDefaultTemplate();
     virtual void exportAudio();
     virtual void importLayer();
     virtual void exportLayer();
@@ -214,6 +213,7 @@
     QAction                 *m_playAction;
     QAction                 *m_playSelectionAction;
     QAction                 *m_playLoopAction;
+    QAction                 *m_manageTemplatesAction;
 
     bool                     m_soloModified;
     bool                     m_prevSolo;
--- a/main/PreferencesDialog.cpp	Tue May 17 17:22:30 2011 +0100
+++ b/main/PreferencesDialog.cpp	Mon May 23 16:13:30 2011 +0100
@@ -31,12 +31,16 @@
 #include <QFileDialog>
 #include <QMessageBox>
 #include <QSpinBox>
+#include <QListWidget>
 #include <QSettings>
 
+#include <set>
+
 #include "widgets/WindowTypeSelector.h"
 #include "widgets/IconLoader.h"
 #include "base/Preferences.h"
 #include "audioio/AudioTargetFactory.h"
+#include "base/ResourceFinder.h"
 
 PreferencesDialog::PreferencesDialog(QWidget *parent, Qt::WFlags flags) :
     QDialog(parent, flags),
@@ -50,10 +54,10 @@
     QGridLayout *grid = new QGridLayout;
     setLayout(grid);
 
-    QTabWidget *tab = new QTabWidget;
-    grid->addWidget(tab, 0, 0);
+    m_tabs = new QTabWidget;
+    grid->addWidget(m_tabs, 0, 0);
     
-    tab->setTabPosition(QTabWidget::North);
+    m_tabs->setTabPosition(QTabWidget::North);
 
     // Create this first, as slots that get called from the ctor will
     // refer to it
@@ -257,7 +261,8 @@
 
     subgrid->setRowStretch(row, 10);
     
-    tab->addTab(frame, tr("&General"));
+    m_tabOrdering[GeneralTab] = m_tabs->count();
+    m_tabs->addTab(frame, tr("&General"));
 
     // Appearance tab
 
@@ -295,7 +300,8 @@
 
     subgrid->setRowStretch(row, 10);
     
-    tab->addTab(frame, tr("&Appearance"));
+    m_tabOrdering[AppearanceTab] = m_tabs->count();
+    m_tabs->addTab(frame, tr("&Appearance"));
 
     // Analysis tab
 
@@ -328,7 +334,53 @@
     
     subgrid->setRowStretch(row, 10);
     
-    tab->addTab(frame, tr("Anal&ysis"));
+    m_tabOrdering[AnalysisTab] = m_tabs->count();
+    m_tabs->addTab(frame, tr("Anal&ysis"));
+
+    // Template tab
+
+    frame = new QFrame;
+    subgrid = new QGridLayout;
+    frame->setLayout(subgrid);
+    row = 0;
+    
+    subgrid->addWidget(new QLabel(tr("Default session template for audio files:")), row++, 0);
+
+    QListWidget *lw = new QListWidget();
+    subgrid->addWidget(lw, row, 0);
+    subgrid->setRowStretch(row, 10);
+    row++;
+
+    settings.beginGroup("MainWindow");
+    m_currentTemplate = settings.value("sessiontemplate", "").toString();
+    settings.endGroup();
+
+    lw->addItem(tr("Classic Waveform"));
+    if (m_currentTemplate == "" || m_currentTemplate == "default") {
+        lw->setCurrentRow(lw->count()-1);
+    }
+    m_templates.push_back("");
+
+    QStringList templates = ResourceFinder().getResourceFiles("templates", "svt");
+
+    std::set<QString> byName;
+    foreach (QString t, templates) {
+        byName.insert(QFileInfo(t).baseName());
+    }
+
+    foreach (QString t, byName) {
+        if (t.toLower() == "default") continue;
+        m_templates.push_back(t);
+        lw->addItem(t);
+        if (m_currentTemplate == t) {
+            lw->setCurrentRow(lw->count()-1);
+        }
+    }
+
+    connect(lw, SIGNAL(currentRowChanged(int)), this, SLOT(defaultTemplateChanged(int)));
+
+    m_tabOrdering[TemplateTab] = m_tabs->count();
+    m_tabs->addTab(frame, tr("Session &Template"));
 
     QDialogButtonBox *bb = new QDialogButtonBox(Qt::Horizontal);
     grid->addWidget(bb, 1, 0);
@@ -351,6 +403,14 @@
 }
 
 void
+PreferencesDialog::switchToTab(Tab t)
+{
+    if (m_tabOrdering.contains(t)) {
+        m_tabs->setCurrentIndex(m_tabOrdering[t]);
+    }
+}
+
+void
 PreferencesDialog::windowTypeChanged(WindowType type)
 {
     m_windowType = type;
@@ -417,6 +477,13 @@
 }
 
 void
+PreferencesDialog::defaultTemplateChanged(int i)
+{
+    m_currentTemplate = m_templates[i];
+    m_applyButton->setEnabled(true);
+}
+
+void
 PreferencesDialog::tempDirRootChanged(QString r)
 {
     m_tempDirRoot = r;
@@ -488,10 +555,15 @@
         AudioTargetFactory::getInstance()->getCallbackTargetNames();
 
     QSettings settings;
+
     settings.beginGroup("Preferences");
     settings.setValue("audio-target", devices[m_audioDevice]);
     settings.endGroup();
 
+    settings.beginGroup("MainWindow");
+    settings.setValue("sessiontemplate", m_currentTemplate);
+    settings.endGroup();
+
     m_applyButton->setEnabled(false);
 
     if (m_changesOnRestart) {
--- a/main/PreferencesDialog.h	Tue May 17 17:22:30 2011 +0100
+++ b/main/PreferencesDialog.h	Mon May 23 16:13:30 2011 +0100
@@ -17,12 +17,14 @@
 #define _PREFERENCES_DIALOG_H_
 
 #include <QDialog>
+#include <QMap>
 
 #include "base/Window.h"
 
 class WindowTypeSelector;
 class QPushButton;
 class QLineEdit;
+class QTabWidget;
 
 class PreferencesDialog : public QDialog
 {
@@ -32,6 +34,14 @@
     PreferencesDialog(QWidget *parent = 0, Qt::WFlags flags = 0);
     virtual ~PreferencesDialog();
 
+    enum Tab {
+        GeneralTab,
+        AppearanceTab,
+        AnalysisTab,
+        TemplateTab
+    };
+    void switchToTab(Tab tab);
+
 public slots:
     void applicationClosing(bool quickly);
 
@@ -49,6 +59,7 @@
     void timeToTextModeChanged(int mode);
     void viewFontSizeChanged(int sz);
     void showSplashChanged(int state);
+    void defaultTemplateChanged(int);
 
     void tempDirButtonClicked();
 
@@ -60,7 +71,13 @@
     WindowTypeSelector *m_windowTypeSelector;
     QPushButton *m_applyButton;
 
+    QTabWidget *m_tabs;
+    QMap<Tab, int> m_tabOrdering;
+
     QLineEdit *m_tempDirRootEdit;
+
+    QString m_currentTemplate;
+    QStringList m_templates;
     
     WindowType m_windowType;
     int m_spectrogramSmoothing;