comparison main/MainWindow.cpp @ 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
parents 586eb6764a2b
children e3644cd9cf99
comparison
equal deleted inserted replaced
435:586eb6764a2b 436:6d827453657f
503 menu->addSeparator(); 503 menu->addSeparator();
504 504
505 QString templatesMenuLabel = tr("Apply Session Template"); 505 QString templatesMenuLabel = tr("Apply Session Template");
506 m_templatesMenu = menu->addMenu(templatesMenuLabel); 506 m_templatesMenu = menu->addMenu(templatesMenuLabel);
507 m_templatesMenu->setTearOffEnabled(true); 507 m_templatesMenu->setTearOffEnabled(true);
508 setupTemplatesMenu(); 508 // We need to have a main model for this option to be useful:
509 // canExportAudio captures that
510 connect(this, SIGNAL(canExportAudio(bool)), m_templatesMenu, SLOT(setEnabled(bool)));
511
512 // Set up the menu in a moment, after m_manageTemplatesAction constructed
509 513
510 action = new QAction(tr("Export Session as Template..."), this); 514 action = new QAction(tr("Export Session as Template..."), this);
511 connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate())); 515 connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate()));
512 menu->addAction(action); 516 // We need to have something in the session for this to be useful:
513 517 // canDeleteCurrentLayer captures that
514 action = new QAction(tr("Manage Saved Templates"), this); 518 connect(this, SIGNAL(canExportAudio(bool)), action, SLOT(setEnabled(bool)));
515 connect(action, SIGNAL(triggered()), this, SLOT(manageSavedTemplates())); 519 menu->addAction(action);
516 //!!! action->setEnabled(havePersonal); 520
517 menu->addAction(action); 521 m_manageTemplatesAction = new QAction(tr("Manage Exported Templates"), this);
522 connect(m_manageTemplatesAction, SIGNAL(triggered()), this, SLOT(manageSavedTemplates()));
523 menu->addAction(m_manageTemplatesAction);
524
525 setupTemplatesMenu();
518 526
519 action = new QAction(tr("&Preferences..."), this); 527 action = new QAction(tr("&Preferences..."), this);
520 action->setStatusTip(tr("Adjust the application preferences")); 528 action->setStatusTip(tr("Adjust the application preferences"));
521 connect(action, SIGNAL(triggered()), this, SLOT(preferences())); 529 connect(action, SIGNAL(triggered()), this, SLOT(preferences()));
522 menu->addAction(action); 530 menu->addAction(action);
1644 void 1652 void
1645 MainWindow::setupTemplatesMenu() 1653 MainWindow::setupTemplatesMenu()
1646 { 1654 {
1647 m_templatesMenu->clear(); 1655 m_templatesMenu->clear();
1648 1656
1649 QAction *defaultAction = new QAction(tr("Default"), this); 1657 QAction *defaultAction = new QAction(tr("Classic Waveform"), this);
1650 defaultAction->setObjectName("default"); 1658 defaultAction->setObjectName("default");
1651 connect(defaultAction, SIGNAL(triggered()), this, SLOT(applyTemplate())); 1659 connect(defaultAction, SIGNAL(triggered()), this, SLOT(applyTemplate()));
1652 m_templatesMenu->addAction(defaultAction); 1660 m_templatesMenu->addAction(defaultAction);
1653 1661
1654 m_templatesMenu->addSeparator(); 1662 m_templatesMenu->addSeparator();
1655 1663
1656 QAction *action = 0; 1664 QAction *action = 0;
1657 1665
1658 QStringList templates = ResourceFinder().getResourceFiles("templates", "svt"); 1666 QStringList templates = ResourceFinder().getResourceFiles("templates", "svt");
1659 1667
1668 bool havePersonal = false;
1669
1660 // (ordered by name) 1670 // (ordered by name)
1661 std::set<QString> byName; 1671 std::set<QString> byName;
1662 foreach (QString t, templates) { 1672 foreach (QString t, templates) {
1673 if (!t.startsWith(":")) havePersonal = true;
1663 byName.insert(QFileInfo(t).baseName()); 1674 byName.insert(QFileInfo(t).baseName());
1664 } 1675 }
1665 1676
1666 foreach (QString t, byName) { 1677 foreach (QString t, byName) {
1667 if (t.toLower() == "default") continue; 1678 if (t.toLower() == "default") continue;
1676 m_templateWatcher = new QFileSystemWatcher(this); 1687 m_templateWatcher = new QFileSystemWatcher(this);
1677 m_templateWatcher->addPath(ResourceFinder().getResourceSaveDir("templates")); 1688 m_templateWatcher->addPath(ResourceFinder().getResourceSaveDir("templates"));
1678 connect(m_templateWatcher, SIGNAL(directoryChanged(const QString &)), 1689 connect(m_templateWatcher, SIGNAL(directoryChanged(const QString &)),
1679 this, SLOT(setupTemplatesMenu())); 1690 this, SLOT(setupTemplatesMenu()));
1680 } 1691 }
1681 } 1692
1682 1693 QAction *setDefaultAction = new QAction(tr("Choose Default Template..."), this);
1683 #ifdef NOT_DEFINED 1694 setDefaultAction->setObjectName("set_default_template");
1684 //!!! 1695 connect(setDefaultAction, SIGNAL(triggered()), this, SLOT(preferences()));
1685 void
1686 MainWindow::setupTemplatesMenu()
1687 {
1688 m_templatesMenu->clear();
1689
1690 QSettings settings;
1691 settings.beginGroup("MainWindow");
1692 QString deflt = settings.value("sessiontemplate", "").toString();
1693 setDefaultSessionTemplate(deflt == "" ? "default" : deflt);
1694
1695 QActionGroup *templatesGroup = new QActionGroup(this);
1696
1697 bool haveFoundDefault = false;
1698
1699 QAction *defaultAction = new QAction(tr("Default"), this);
1700 defaultAction->setObjectName("default");
1701 connect(defaultAction, SIGNAL(triggered()), this, SLOT(applyTemplate()));
1702 /
1703 defaultAction->setCheckable(true);
1704 if (deflt == "" || deflt == "default") {
1705 defaultAction->setChecked(true);
1706 haveFoundDefault = true;
1707 }
1708 templatesGroup->addAction(defaultAction);
1709 m_templatesMenu->addAction(defaultAction);
1710
1711 m_templatesMenu->addSeparator(); 1696 m_templatesMenu->addSeparator();
1712 1697 m_templatesMenu->addAction(setDefaultAction);
1713 QAction *action = 0; 1698
1714 1699 m_manageTemplatesAction->setEnabled(havePersonal);
1715 QStringList templates = ResourceFinder().getResourceFiles("templates", "svt"); 1700 }
1716 1701
1717 bool havePersonal = false;
1718
1719 // (ordered by name)
1720 std::set<QString> byName;
1721 foreach (QString t, templates) {
1722 if (!t.startsWith(":")) havePersonal = true;
1723 byName.insert(QFileInfo(t).baseName());
1724 }
1725
1726 foreach (QString t, byName) {
1727 if (t.toLower() == "default") continue;
1728 action = new QAction(t, this);
1729 connect(action, SIGNAL(triggered()), this, SLOT(applyTemplate()));
1730 action->setCheckable(true);
1731 if (deflt == t) {
1732 action->setChecked(true);
1733 haveFoundDefault = true;
1734 }
1735 templatesGroup->addAction(action);
1736 m_templatesMenu->addAction(action);
1737 }
1738
1739 if (!templates.empty()) m_templatesMenu->addSeparator();
1740
1741 if (!haveFoundDefault) {
1742 defaultAction->setChecked(true);
1743 setDefaultSessionTemplate("default");
1744 settings.setValue("sessiontemplate", "");
1745 }
1746
1747 settings.endGroup();
1748 /*!!!
1749 action = new QAction(tr("Save Template from Current Session..."), this);
1750 connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAsTemplate()));
1751 m_templatesMenu->addAction(action);
1752
1753 action = new QAction(tr("Manage Saved Templates"), this);
1754 connect(action, SIGNAL(triggered()), this, SLOT(manageSavedTemplates()));
1755 action->setEnabled(havePersonal);
1756 m_templatesMenu->addAction(action);
1757 */
1758 if (!m_templateWatcher) {
1759 m_templateWatcher = new QFileSystemWatcher(this);
1760 m_templateWatcher->addPath(ResourceFinder().getResourceSaveDir("templates"));
1761 connect(m_templateWatcher, SIGNAL(directoryChanged(const QString &)),
1762 this, SLOT(setupTemplatesMenu()));
1763 }
1764 }
1765 #endif
1766 1702
1767 void 1703 void
1768 MainWindow::setupRecentTransformsMenu() 1704 MainWindow::setupRecentTransformsMenu()
1769 { 1705 {
1770 m_recentTransformsMenu->clear(); 1706 m_recentTransformsMenu->clear();
2924 openSessionTemplate(n); 2860 openSessionTemplate(n);
2925 } 2861 }
2926 } 2862 }
2927 2863
2928 void 2864 void
2929 MainWindow::changeDefaultTemplate()
2930 {
2931 QObject *s = sender();
2932 QAction *action = qobject_cast<QAction *>(s);
2933
2934 if (!action) {
2935 std::cerr << "WARNING: MainWindow::changeTemplate: sender is not an action"
2936 << std::endl;
2937 return;
2938 }
2939
2940 QString n = action->objectName();
2941 if (n == "") n = action->text();
2942
2943 if (n == "") {
2944 std::cerr << "WARNING: MainWindow::changeTemplate: sender has no name"
2945 << std::endl;
2946 return;
2947 }
2948
2949 setDefaultSessionTemplate(n);
2950
2951 QSettings settings;
2952 settings.beginGroup("MainWindow");
2953 settings.setValue("sessiontemplate", n);
2954 settings.endGroup();
2955 }
2956
2957 void
2958 MainWindow::saveSessionAsTemplate() 2865 MainWindow::saveSessionAsTemplate()
2959 { 2866 {
2960 QString name = QInputDialog::getText 2867 QString name = QInputDialog::getText
2961 (this, tr("Enter template name"), 2868 (this, tr("Enter template name"),
2962 tr("Please enter a name for the saved template:")); 2869 tr("Please enter a name for the saved template:"));
4153 } 4060 }
4154 4061
4155 void 4062 void
4156 MainWindow::preferences() 4063 MainWindow::preferences()
4157 { 4064 {
4065 bool goToTemplateTab =
4066 (sender() && sender()->objectName() == "set_default_template");
4067
4158 if (!m_preferencesDialog.isNull()) { 4068 if (!m_preferencesDialog.isNull()) {
4159 m_preferencesDialog->show(); 4069 m_preferencesDialog->show();
4160 m_preferencesDialog->raise(); 4070 m_preferencesDialog->raise();
4071 if (goToTemplateTab) {
4072 m_preferencesDialog->switchToTab(PreferencesDialog::TemplateTab);
4073 }
4161 return; 4074 return;
4162 } 4075 }
4163 4076
4164 m_preferencesDialog = new PreferencesDialog(this); 4077 m_preferencesDialog = new PreferencesDialog(this);
4165 4078
4170 // without resetting its own widgets, so its state will be 4083 // without resetting its own widgets, so its state will be
4171 // incorrect when next shown unless we construct it afresh 4084 // incorrect when next shown unless we construct it afresh
4172 m_preferencesDialog->setAttribute(Qt::WA_DeleteOnClose); 4085 m_preferencesDialog->setAttribute(Qt::WA_DeleteOnClose);
4173 4086
4174 m_preferencesDialog->show(); 4087 m_preferencesDialog->show();
4088 if (goToTemplateTab) {
4089 m_preferencesDialog->switchToTab(PreferencesDialog::TemplateTab);
4090 }
4175 } 4091 }
4176 4092
4177 void 4093 void
4178 MainWindow::mouseEnteredWidget() 4094 MainWindow::mouseEnteredWidget()
4179 { 4095 {