comparison main/MainWindow.cpp @ 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 97cd3d2c032a
children 72a2b4210e50
comparison
equal deleted inserted replaced
424:944167b96d01 425:eaab4c65c7bc
111 #include <QDateTime> 111 #include <QDateTime>
112 #include <QProcess> 112 #include <QProcess>
113 #include <QCheckBox> 113 #include <QCheckBox>
114 #include <QRegExp> 114 #include <QRegExp>
115 #include <QScrollArea> 115 #include <QScrollArea>
116 #include <QDesktopServices>
116 117
117 #include <iostream> 118 #include <iostream>
118 #include <cstdio> 119 #include <cstdio>
119 #include <errno.h> 120 #include <errno.h>
120 121
429 connect(&m_recentFiles, SIGNAL(recentChanged()), 430 connect(&m_recentFiles, SIGNAL(recentChanged()),
430 this, SLOT(setupRecentFilesMenu())); 431 this, SLOT(setupRecentFilesMenu()));
431 432
432 menu->addSeparator(); 433 menu->addSeparator();
433 434
434 m_templatesMenu = menu->addMenu(tr("Set Session Open Template")); 435 QString templatesMenuLabel = tr("Session Template for Audio Files");
436
437 #ifdef Q_OS_MAC
438 // Normally this menu will go next to Preferences on the File
439 // menu. But on OS/X Preferences doesn't appear on the File menu,
440 // so we put it next to the other Session stuff instead.
441 m_templatesMenu = menu->addMenu(templatesMenuLabel);
435 m_templatesMenu->setTearOffEnabled(true); 442 m_templatesMenu->setTearOffEnabled(true);
436 setupTemplatesMenu(); 443 setupTemplatesMenu();
437
438 menu->addSeparator(); 444 menu->addSeparator();
445 #endif
439 446
440 icon = il.load("filesave"); 447 icon = il.load("filesave");
441 icon.addPixmap(il.loadPixmap("filesave-22")); 448 icon.addPixmap(il.loadPixmap("filesave-22"));
442 action = new QAction(icon, tr("&Save Session"), this); 449 action = new QAction(icon, tr("&Save Session"), this);
443 action->setShortcut(tr("Ctrl+S")); 450 action->setShortcut(tr("Ctrl+S"));
501 connect(action, SIGNAL(triggered()), this, SLOT(exportImage())); 508 connect(action, SIGNAL(triggered()), this, SLOT(exportImage()));
502 connect(this, SIGNAL(canExportImage(bool)), action, SLOT(setEnabled(bool))); 509 connect(this, SIGNAL(canExportImage(bool)), action, SLOT(setEnabled(bool)));
503 menu->addAction(action); 510 menu->addAction(action);
504 511
505 menu->addSeparator(); 512 menu->addSeparator();
513
514 #ifndef Q_OS_MAC
515 // See note for Q_OS_MAC alternative above
516 m_templatesMenu = menu->addMenu(templatesMenuLabel);
517 m_templatesMenu->setTearOffEnabled(true);
518 setupTemplatesMenu();
519 #endif
506 520
507 action = new QAction(tr("&Preferences..."), this); 521 action = new QAction(tr("&Preferences..."), this);
508 action->setStatusTip(tr("Adjust the application preferences")); 522 action->setStatusTip(tr("Adjust the application preferences"));
509 connect(action, SIGNAL(triggered()), this, SLOT(preferences())); 523 connect(action, SIGNAL(triggered()), this, SLOT(preferences()));
510 menu->addAction(action); 524 menu->addAction(action);
1635 m_templatesMenu->clear(); 1649 m_templatesMenu->clear();
1636 1650
1637 QSettings settings; 1651 QSettings settings;
1638 settings.beginGroup("MainWindow"); 1652 settings.beginGroup("MainWindow");
1639 QString deflt = settings.value("sessiontemplate", "").toString(); 1653 QString deflt = settings.value("sessiontemplate", "").toString();
1640 setDefaultSessionTemplate(deflt); 1654 setDefaultSessionTemplate(deflt == "" ? "default" : "");
1641 settings.endGroup(); 1655 settings.endGroup();
1642 1656
1643 QActionGroup *templatesGroup = new QActionGroup(this); 1657 QActionGroup *templatesGroup = new QActionGroup(this);
1644 1658
1645 QAction *action = new QAction(tr("Default"), this); 1659 QAction *action = new QAction(tr("Default"), this);
1650 templatesGroup->addAction(action); 1664 templatesGroup->addAction(action);
1651 m_templatesMenu->addAction(action); 1665 m_templatesMenu->addAction(action);
1652 1666
1653 m_templatesMenu->addSeparator(); 1667 m_templatesMenu->addSeparator();
1654 1668
1655 QStringList templates = ResourceFinder().getResourceFiles("templates", "xml"); 1669 QStringList templates = ResourceFinder().getResourceFiles("templates", "svt");
1670
1671 bool havePersonal = false;
1672
1673 // (ordered by name)
1674 std::set<QString> byName;
1656 foreach (QString t, templates) { 1675 foreach (QString t, templates) {
1657 QString tname = QFileInfo(t).baseName(); 1676 if (!t.startsWith(":")) havePersonal = true;
1658 if (tname.toLower() == "default") continue; 1677 byName.insert(QFileInfo(t).baseName());
1659 action = new QAction(tname, this); 1678 }
1660 action->setObjectName(t); 1679
1680 foreach (QString t, byName) {
1681 if (t.toLower() == "default") continue;
1682 action = new QAction(t, this);
1661 connect(action, SIGNAL(triggered()), this, SLOT(changeTemplate())); 1683 connect(action, SIGNAL(triggered()), this, SLOT(changeTemplate()));
1662 action->setCheckable(true); 1684 action->setCheckable(true);
1663 action->setChecked(deflt == tname); 1685 action->setChecked(deflt == t);
1664 templatesGroup->addAction(action); 1686 templatesGroup->addAction(action);
1665 m_templatesMenu->addAction(action); 1687 m_templatesMenu->addAction(action);
1666 } 1688 }
1667 1689
1668 if (!templates.empty()) m_templatesMenu->addSeparator(); 1690 if (!templates.empty()) m_templatesMenu->addSeparator();
1669 1691
1670 action = new QAction(tr("Save Session as New Template..."), this); 1692 action = new QAction(tr("Save Template from Current Session..."), this);
1671 connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate())); 1693 connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate()));
1694 m_templatesMenu->addAction(action);
1695
1696 action = new QAction(tr("Manage Saved Templates"), this);
1697 connect(action, SIGNAL(triggered()), this, SLOT(manageSavedTemplates()));
1698 action->setEnabled(havePersonal);
1672 m_templatesMenu->addAction(action); 1699 m_templatesMenu->addAction(action);
1673 } 1700 }
1674 1701
1675 void 1702 void
1676 MainWindow::setupRecentTransformsMenu() 1703 MainWindow::setupRecentTransformsMenu()
2801 } 2828 }
2802 2829
2803 void 2830 void
2804 MainWindow::changeTemplate() 2831 MainWindow::changeTemplate()
2805 { 2832 {
2806 //!!! 2833 QObject *s = sender();
2834 QAction *action = qobject_cast<QAction *>(s);
2835
2836 if (!action) {
2837 std::cerr << "WARNING: MainWindow::changeTemplate: sender is not an action"
2838 << std::endl;
2839 return;
2840 }
2841
2842 QString n = action->objectName();
2843 if (n == "") n = action->text();
2844
2845 if (n == "") {
2846 std::cerr << "WARNING: MainWindow::changeTemplate: sender has no name"
2847 << std::endl;
2848 return;
2849 }
2850
2851 setDefaultSessionTemplate(n);
2852 }
2853
2854 void
2855 MainWindow::saveSessionAsTemplate()
2856 {
2857 QString name = QInputDialog::getText
2858 (this, tr("Enter template name"),
2859 tr("Please enter a name for the saved template:"));
2860 if (name == "") return;
2861
2862 //!!! sanitise!
2863
2864
2865 }
2866
2867 void
2868 MainWindow::manageSavedTemplates()
2869 {
2870 //!!! really we should watch this directory and recreate the menu when it changes
2871 ResourceFinder rf;
2872 QDesktopServices::openUrl("file:" + rf.getResourceSaveDir("templates"));
2807 } 2873 }
2808 2874
2809 void 2875 void
2810 MainWindow::paneAdded(Pane *pane) 2876 MainWindow::paneAdded(Pane *pane)
2811 { 2877 {