comparison main/MainWindow.cpp @ 435:586eb6764a2b templating

Replace the change-default-template menu functions with apply-template functions that act immediately. The rest of the menu arrangement is still somewhat in flux
author Chris Cannam
date Tue, 17 May 2011 17:22:30 +0100
parents db0c86c4e5e9
children 6d827453657f
comparison
equal deleted inserted replaced
434:db0c86c4e5e9 435:586eb6764a2b
433 connect(&m_recentFiles, SIGNAL(recentChanged()), 433 connect(&m_recentFiles, SIGNAL(recentChanged()),
434 this, SLOT(setupRecentFilesMenu())); 434 this, SLOT(setupRecentFilesMenu()));
435 435
436 menu->addSeparator(); 436 menu->addSeparator();
437 437
438 QString templatesMenuLabel = tr("Session Template for Audio Files");
439
440 #ifdef Q_OS_MAC
441 // Normally this menu will go next to Preferences on the File
442 // menu. But on OS/X Preferences doesn't appear on the File menu,
443 // so we put it next to the other Session stuff instead.
444 m_templatesMenu = menu->addMenu(templatesMenuLabel);
445 m_templatesMenu->setTearOffEnabled(true);
446 setupTemplatesMenu();
447 menu->addSeparator();
448 #endif
449
450 icon = il.load("filesave"); 438 icon = il.load("filesave");
451 icon.addPixmap(il.loadPixmap("filesave-22")); 439 icon.addPixmap(il.loadPixmap("filesave-22"));
452 action = new QAction(icon, tr("&Save Session"), this); 440 action = new QAction(icon, tr("&Save Session"), this);
453 action->setShortcut(tr("Ctrl+S")); 441 action->setShortcut(tr("Ctrl+S"));
454 action->setStatusTip(tr("Save the current session into a Sonic Visualiser session file")); 442 action->setStatusTip(tr("Save the current session into a Sonic Visualiser session file"));
512 connect(this, SIGNAL(canExportImage(bool)), action, SLOT(setEnabled(bool))); 500 connect(this, SIGNAL(canExportImage(bool)), action, SLOT(setEnabled(bool)));
513 menu->addAction(action); 501 menu->addAction(action);
514 502
515 menu->addSeparator(); 503 menu->addSeparator();
516 504
517 #ifndef Q_OS_MAC 505 QString templatesMenuLabel = tr("Apply Session Template");
518 // See note for Q_OS_MAC alternative above
519 m_templatesMenu = menu->addMenu(templatesMenuLabel); 506 m_templatesMenu = menu->addMenu(templatesMenuLabel);
520 m_templatesMenu->setTearOffEnabled(true); 507 m_templatesMenu->setTearOffEnabled(true);
521 setupTemplatesMenu(); 508 setupTemplatesMenu();
522 #endif 509
510 action = new QAction(tr("Export Session as Template..."), this);
511 connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate()));
512 menu->addAction(action);
513
514 action = new QAction(tr("Manage Saved Templates"), this);
515 connect(action, SIGNAL(triggered()), this, SLOT(manageSavedTemplates()));
516 //!!! action->setEnabled(havePersonal);
517 menu->addAction(action);
523 518
524 action = new QAction(tr("&Preferences..."), this); 519 action = new QAction(tr("&Preferences..."), this);
525 action->setStatusTip(tr("Adjust the application preferences")); 520 action->setStatusTip(tr("Adjust the application preferences"));
526 connect(action, SIGNAL(triggered()), this, SLOT(preferences())); 521 connect(action, SIGNAL(triggered()), this, SLOT(preferences()));
527 menu->addAction(action); 522 menu->addAction(action);
1649 void 1644 void
1650 MainWindow::setupTemplatesMenu() 1645 MainWindow::setupTemplatesMenu()
1651 { 1646 {
1652 m_templatesMenu->clear(); 1647 m_templatesMenu->clear();
1653 1648
1649 QAction *defaultAction = new QAction(tr("Default"), this);
1650 defaultAction->setObjectName("default");
1651 connect(defaultAction, SIGNAL(triggered()), this, SLOT(applyTemplate()));
1652 m_templatesMenu->addAction(defaultAction);
1653
1654 m_templatesMenu->addSeparator();
1655
1656 QAction *action = 0;
1657
1658 QStringList templates = ResourceFinder().getResourceFiles("templates", "svt");
1659
1660 // (ordered by name)
1661 std::set<QString> byName;
1662 foreach (QString t, templates) {
1663 byName.insert(QFileInfo(t).baseName());
1664 }
1665
1666 foreach (QString t, byName) {
1667 if (t.toLower() == "default") continue;
1668 action = new QAction(t, this);
1669 connect(action, SIGNAL(triggered()), this, SLOT(applyTemplate()));
1670 m_templatesMenu->addAction(action);
1671 }
1672
1673 if (!templates.empty()) m_templatesMenu->addSeparator();
1674
1675 if (!m_templateWatcher) {
1676 m_templateWatcher = new QFileSystemWatcher(this);
1677 m_templateWatcher->addPath(ResourceFinder().getResourceSaveDir("templates"));
1678 connect(m_templateWatcher, SIGNAL(directoryChanged(const QString &)),
1679 this, SLOT(setupTemplatesMenu()));
1680 }
1681 }
1682
1683 #ifdef NOT_DEFINED
1684 //!!!
1685 void
1686 MainWindow::setupTemplatesMenu()
1687 {
1688 m_templatesMenu->clear();
1689
1654 QSettings settings; 1690 QSettings settings;
1655 settings.beginGroup("MainWindow"); 1691 settings.beginGroup("MainWindow");
1656 QString deflt = settings.value("sessiontemplate", "").toString(); 1692 QString deflt = settings.value("sessiontemplate", "").toString();
1657 setDefaultSessionTemplate(deflt == "" ? "default" : deflt); 1693 setDefaultSessionTemplate(deflt == "" ? "default" : deflt);
1658 1694
1660 1696
1661 bool haveFoundDefault = false; 1697 bool haveFoundDefault = false;
1662 1698
1663 QAction *defaultAction = new QAction(tr("Default"), this); 1699 QAction *defaultAction = new QAction(tr("Default"), this);
1664 defaultAction->setObjectName("default"); 1700 defaultAction->setObjectName("default");
1665 connect(defaultAction, SIGNAL(triggered()), this, SLOT(changeTemplate())); 1701 connect(defaultAction, SIGNAL(triggered()), this, SLOT(applyTemplate()));
1702 /
1666 defaultAction->setCheckable(true); 1703 defaultAction->setCheckable(true);
1667 if (deflt == "" || deflt == "default") { 1704 if (deflt == "" || deflt == "default") {
1668 defaultAction->setChecked(true); 1705 defaultAction->setChecked(true);
1669 haveFoundDefault = true; 1706 haveFoundDefault = true;
1670 } 1707 }
1687 } 1724 }
1688 1725
1689 foreach (QString t, byName) { 1726 foreach (QString t, byName) {
1690 if (t.toLower() == "default") continue; 1727 if (t.toLower() == "default") continue;
1691 action = new QAction(t, this); 1728 action = new QAction(t, this);
1692 connect(action, SIGNAL(triggered()), this, SLOT(changeTemplate())); 1729 connect(action, SIGNAL(triggered()), this, SLOT(applyTemplate()));
1693 action->setCheckable(true); 1730 action->setCheckable(true);
1694 if (deflt == t) { 1731 if (deflt == t) {
1695 action->setChecked(true); 1732 action->setChecked(true);
1696 haveFoundDefault = true; 1733 haveFoundDefault = true;
1697 } 1734 }
1706 setDefaultSessionTemplate("default"); 1743 setDefaultSessionTemplate("default");
1707 settings.setValue("sessiontemplate", ""); 1744 settings.setValue("sessiontemplate", "");
1708 } 1745 }
1709 1746
1710 settings.endGroup(); 1747 settings.endGroup();
1711 1748 /*!!!
1712 action = new QAction(tr("Save Template from Current Session..."), this); 1749 action = new QAction(tr("Save Template from Current Session..."), this);
1713 connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate())); 1750 connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate()));
1714 m_templatesMenu->addAction(action); 1751 m_templatesMenu->addAction(action);
1715 1752
1716 action = new QAction(tr("Manage Saved Templates"), this); 1753 action = new QAction(tr("Manage Saved Templates"), this);
1717 connect(action, SIGNAL(triggered()), this, SLOT(manageSavedTemplates())); 1754 connect(action, SIGNAL(triggered()), this, SLOT(manageSavedTemplates()));
1718 action->setEnabled(havePersonal); 1755 action->setEnabled(havePersonal);
1719 m_templatesMenu->addAction(action); 1756 m_templatesMenu->addAction(action);
1720 1757 */
1721 if (!m_templateWatcher) { 1758 if (!m_templateWatcher) {
1722 m_templateWatcher = new QFileSystemWatcher(this); 1759 m_templateWatcher = new QFileSystemWatcher(this);
1723 m_templateWatcher->addPath(ResourceFinder().getResourceSaveDir("templates")); 1760 m_templateWatcher->addPath(ResourceFinder().getResourceSaveDir("templates"));
1724 connect(m_templateWatcher, SIGNAL(directoryChanged(const QString &)), 1761 connect(m_templateWatcher, SIGNAL(directoryChanged(const QString &)),
1725 this, SLOT(setupTemplatesMenu())); 1762 this, SLOT(setupTemplatesMenu()));
1726 } 1763 }
1727 } 1764 }
1765 #endif
1728 1766
1729 void 1767 void
1730 MainWindow::setupRecentTransformsMenu() 1768 MainWindow::setupRecentTransformsMenu()
1731 { 1769 {
1732 m_recentTransformsMenu->clear(); 1770 m_recentTransformsMenu->clear();
2855 tr("<b>Audio required</b><p>Unable to load layer data from \"%1\" without an audio file.<br>Please load at least one audio file before importing annotations.").arg(path)); 2893 tr("<b>Audio required</b><p>Unable to load layer data from \"%1\" without an audio file.<br>Please load at least one audio file before importing annotations.").arg(path));
2856 } 2894 }
2857 } 2895 }
2858 2896
2859 void 2897 void
2860 MainWindow::changeTemplate() 2898 MainWindow::applyTemplate()
2899 {
2900 QObject *s = sender();
2901 QAction *action = qobject_cast<QAction *>(s);
2902
2903 if (!action) {
2904 std::cerr << "WARNING: MainWindow::applyTemplate: sender is not an action"
2905 << std::endl;
2906 return;
2907 }
2908
2909 QString n = action->objectName();
2910 if (n == "") n = action->text();
2911
2912 if (n == "") {
2913 std::cerr << "WARNING: MainWindow::applyTemplate: sender has no name"
2914 << std::endl;
2915 return;
2916 }
2917
2918 QString mainModelLocation;
2919 WaveFileModel *mm = getMainModel();
2920 if (mm) mainModelLocation = mm->getLocation();
2921 if (mainModelLocation != "") {
2922 openAudio(mainModelLocation, ReplaceSession, n);
2923 } else {
2924 openSessionTemplate(n);
2925 }
2926 }
2927
2928 void
2929 MainWindow::changeDefaultTemplate()
2861 { 2930 {
2862 QObject *s = sender(); 2931 QObject *s = sender();
2863 QAction *action = qobject_cast<QAction *>(s); 2932 QAction *action = qobject_cast<QAction *>(s);
2864 2933
2865 if (!action) { 2934 if (!action) {