annotate main/MainWindow.cpp @ 33:544ab25d2372

* Add support for plugin classification using category files. Add separate menus listing plugins by category, maker, and plugin name.
author Chris Cannam
date Thu, 21 Sep 2006 16:43:50 +0000
parents 61259228d029
children 8ad306d8a568
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 Sonic Visualiser
Chris@0 5 An audio file viewer and annotation editor.
Chris@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@0 7 This file copyright 2006 Chris Cannam.
Chris@0 8
Chris@0 9 This program is free software; you can redistribute it and/or
Chris@0 10 modify it under the terms of the GNU General Public License as
Chris@0 11 published by the Free Software Foundation; either version 2 of the
Chris@0 12 License, or (at your option) any later version. See the file
Chris@0 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@1 16 #include "version.h"
Chris@0 17
Chris@0 18 #include "MainWindow.h"
Chris@1 19 #include "document/Document.h"
Chris@0 20 #include "PreferencesDialog.h"
Chris@0 21
Chris@1 22 #include "view/Pane.h"
Chris@1 23 #include "view/PaneStack.h"
Chris@1 24 #include "data/model/WaveFileModel.h"
Chris@1 25 #include "data/model/SparseOneDimensionalModel.h"
Chris@1 26 #include "view/ViewManager.h"
Chris@0 27 #include "base/Preferences.h"
Chris@0 28 #include "layer/WaveformLayer.h"
Chris@0 29 #include "layer/TimeRulerLayer.h"
Chris@0 30 #include "layer/TimeInstantLayer.h"
Chris@0 31 #include "layer/TimeValueLayer.h"
Chris@0 32 #include "layer/Colour3DPlotLayer.h"
Chris@0 33 #include "widgets/Fader.h"
Chris@1 34 #include "view/Panner.h"
Chris@0 35 #include "widgets/PropertyBox.h"
Chris@0 36 #include "widgets/PropertyStack.h"
Chris@0 37 #include "widgets/AudioDial.h"
Chris@0 38 #include "widgets/LayerTree.h"
Chris@0 39 #include "widgets/ListInputDialog.h"
Chris@0 40 #include "audioio/AudioCallbackPlaySource.h"
Chris@0 41 #include "audioio/AudioCallbackPlayTarget.h"
Chris@0 42 #include "audioio/AudioTargetFactory.h"
Chris@1 43 #include "data/fileio/AudioFileReaderFactory.h"
Chris@1 44 #include "data/fileio/DataFileReaderFactory.h"
Chris@1 45 #include "data/fileio/WavFileWriter.h"
Chris@1 46 #include "data/fileio/CSVFileWriter.h"
Chris@1 47 #include "data/fileio/BZipFileDevice.h"
Chris@1 48 #include "base/RecentFiles.h"
Chris@0 49 #include "transform/TransformFactory.h"
Chris@0 50 #include "base/PlayParameterRepository.h"
Chris@0 51 #include "base/XmlExportable.h"
Chris@0 52 #include "base/CommandHistory.h"
Chris@0 53 #include "base/Profiler.h"
Chris@0 54 #include "base/Clipboard.h"
Chris@0 55
Chris@0 56 // For version information
Chris@0 57 #include "vamp/vamp.h"
Chris@0 58 #include "vamp-sdk/PluginBase.h"
Chris@0 59 #include "plugin/api/ladspa.h"
Chris@0 60 #include "plugin/api/dssi.h"
Chris@0 61
Chris@0 62 #include <QApplication>
Chris@0 63 #include <QPushButton>
Chris@0 64 #include <QFileDialog>
Chris@0 65 #include <QMessageBox>
Chris@0 66 #include <QGridLayout>
Chris@0 67 #include <QLabel>
Chris@0 68 #include <QAction>
Chris@0 69 #include <QMenuBar>
Chris@0 70 #include <QToolBar>
Chris@0 71 #include <QInputDialog>
Chris@0 72 #include <QStatusBar>
Chris@0 73 #include <QTreeView>
Chris@0 74 #include <QFile>
Chris@0 75 #include <QTextStream>
Chris@0 76 #include <QProcess>
Chris@7 77 #include <QShortcut>
Chris@5 78 #include <QSettings>
Chris@11 79 #include <QDateTime>
Chris@11 80 #include <QProcess>
Chris@16 81 #include <QCheckBox>
Chris@0 82
Chris@0 83 #include <iostream>
Chris@0 84 #include <cstdio>
Chris@0 85 #include <errno.h>
Chris@0 86
Chris@0 87 using std::cerr;
Chris@0 88 using std::endl;
Chris@0 89
Chris@33 90 using std::vector;
Chris@33 91 using std::map;
Chris@33 92 using std::set;
Chris@33 93
Chris@0 94
Chris@0 95 MainWindow::MainWindow() :
Chris@0 96 m_document(0),
Chris@0 97 m_paneStack(0),
Chris@0 98 m_viewManager(0),
Chris@0 99 m_panner(0),
Chris@0 100 m_timeRulerLayer(0),
Chris@0 101 m_playSource(0),
Chris@0 102 m_playTarget(0),
Chris@0 103 m_mainMenusCreated(false),
Chris@0 104 m_paneMenu(0),
Chris@0 105 m_layerMenu(0),
Chris@0 106 m_existingLayersMenu(0),
Chris@0 107 m_rightButtonMenu(0),
Chris@0 108 m_rightButtonLayerMenu(0),
Chris@0 109 m_documentModified(false),
Chris@0 110 m_preferencesDialog(0)
Chris@0 111 {
Chris@0 112 setWindowTitle(tr("Sonic Visualiser"));
Chris@0 113
Chris@0 114 UnitDatabase::getInstance()->registerUnit("Hz");
Chris@0 115 UnitDatabase::getInstance()->registerUnit("dB");
Chris@0 116
Chris@0 117 connect(CommandHistory::getInstance(), SIGNAL(commandExecuted()),
Chris@0 118 this, SLOT(documentModified()));
Chris@0 119 connect(CommandHistory::getInstance(), SIGNAL(documentRestored()),
Chris@0 120 this, SLOT(documentRestored()));
Chris@0 121
Chris@0 122 QFrame *frame = new QFrame;
Chris@0 123 setCentralWidget(frame);
Chris@0 124
Chris@0 125 QGridLayout *layout = new QGridLayout;
Chris@0 126
Chris@0 127 m_viewManager = new ViewManager();
Chris@0 128 connect(m_viewManager, SIGNAL(selectionChanged()),
Chris@0 129 this, SLOT(updateMenuStates()));
Chris@0 130
Chris@0 131 m_descriptionLabel = new QLabel;
Chris@0 132
Chris@0 133 m_paneStack = new PaneStack(frame, m_viewManager);
Chris@0 134 connect(m_paneStack, SIGNAL(currentPaneChanged(Pane *)),
Chris@0 135 this, SLOT(currentPaneChanged(Pane *)));
Chris@0 136 connect(m_paneStack, SIGNAL(currentLayerChanged(Pane *, Layer *)),
Chris@0 137 this, SLOT(currentLayerChanged(Pane *, Layer *)));
Chris@0 138 connect(m_paneStack, SIGNAL(rightButtonMenuRequested(Pane *, QPoint)),
Chris@0 139 this, SLOT(rightButtonMenuRequested(Pane *, QPoint)));
Chris@0 140
Chris@0 141 m_panner = new Panner(frame);
Chris@0 142 m_panner->setViewManager(m_viewManager);
Chris@0 143 m_panner->setFixedHeight(40);
Chris@0 144
Chris@0 145 m_panLayer = new WaveformLayer;
Chris@0 146 m_panLayer->setChannelMode(WaveformLayer::MergeChannels);
Chris@0 147 // m_panLayer->setScale(WaveformLayer::MeterScale);
Chris@0 148 m_panLayer->setAutoNormalize(true);
Chris@0 149 m_panLayer->setBaseColour(Qt::darkGreen);
Chris@0 150 m_panLayer->setAggressiveCacheing(true);
Chris@0 151 m_panner->addLayer(m_panLayer);
Chris@0 152
Chris@0 153 m_playSource = new AudioCallbackPlaySource(m_viewManager);
Chris@0 154
Chris@0 155 connect(m_playSource, SIGNAL(sampleRateMismatch(size_t, size_t, bool)),
Chris@0 156 this, SLOT(sampleRateMismatch(size_t, size_t, bool)));
Chris@0 157
Chris@0 158 m_fader = new Fader(frame, false);
Chris@0 159
Chris@0 160 m_playSpeed = new AudioDial(frame);
Chris@12 161 m_playSpeed->setMinimum(0);
Chris@25 162 m_playSpeed->setMaximum(199);
Chris@25 163 m_playSpeed->setValue(100);
Chris@0 164 m_playSpeed->setFixedWidth(24);
Chris@0 165 m_playSpeed->setFixedHeight(24);
Chris@0 166 m_playSpeed->setNotchesVisible(true);
Chris@25 167 m_playSpeed->setPageStep(10);
Chris@25 168 m_playSpeed->setToolTip(tr("Playback speed: +0%"));
Chris@25 169 m_playSpeed->setDefaultValue(100);
Chris@0 170 connect(m_playSpeed, SIGNAL(valueChanged(int)),
Chris@0 171 this, SLOT(playSpeedChanged(int)));
Chris@0 172
Chris@26 173 m_playSharpen = new QPushButton(frame);
Chris@17 174 m_playSharpen->setToolTip(tr("Sharpen percussive transients"));
Chris@26 175 m_playSharpen->setFixedSize(20, 20);
Chris@26 176 // m_playSharpen->setFlat(true);
Chris@16 177 m_playSharpen->setEnabled(false);
Chris@26 178 m_playSharpen->setCheckable(true);
Chris@26 179 m_playSharpen->setChecked(false);
Chris@26 180 m_playSharpen->setIcon(QIcon(":icons/sharpen.png"));
Chris@26 181 connect(m_playSharpen, SIGNAL(clicked()), this, SLOT(playSharpenToggled()));
Chris@26 182
Chris@26 183 m_playMono = new QPushButton(frame);
Chris@26 184 m_playMono->setToolTip(tr("Run time stretcher in mono only"));
Chris@26 185 m_playMono->setFixedSize(20, 20);
Chris@26 186 // m_playMono->setFlat(true);
Chris@26 187 m_playMono->setEnabled(false);
Chris@26 188 m_playMono->setCheckable(true);
Chris@26 189 m_playMono->setChecked(false);
Chris@26 190 m_playMono->setIcon(QIcon(":icons/mono.png"));
Chris@26 191 connect(m_playMono, SIGNAL(clicked()), this, SLOT(playMonoToggled()));
Chris@21 192
Chris@21 193 QSettings settings;
Chris@21 194 settings.beginGroup("MainWindow");
Chris@24 195 m_playSharpen->setChecked(settings.value("playsharpen", true).toBool());
Chris@26 196 m_playMono->setChecked(settings.value("playmono", false).toBool());
Chris@21 197 settings.endGroup();
Chris@21 198
Chris@26 199 layout->addWidget(m_paneStack, 0, 0, 1, 5);
Chris@0 200 layout->addWidget(m_panner, 1, 0);
Chris@0 201 layout->addWidget(m_fader, 1, 1);
Chris@0 202 layout->addWidget(m_playSpeed, 1, 2);
Chris@16 203 layout->addWidget(m_playSharpen, 1, 3);
Chris@26 204 layout->addWidget(m_playMono, 1, 4);
Chris@16 205
Chris@16 206 layout->setColumnStretch(0, 10);
Chris@16 207
Chris@0 208 frame->setLayout(layout);
Chris@0 209
Chris@0 210 connect(m_viewManager, SIGNAL(outputLevelsChanged(float, float)),
Chris@0 211 this, SLOT(outputLevelsChanged(float, float)));
Chris@0 212
Chris@0 213 connect(Preferences::getInstance(),
Chris@0 214 SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@0 215 this,
Chris@0 216 SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@0 217
Chris@0 218 setupMenus();
Chris@0 219 setupToolbars();
Chris@0 220
Chris@7 221 statusBar()->addWidget(m_descriptionLabel);
Chris@0 222
Chris@0 223 newSession();
Chris@0 224 }
Chris@0 225
Chris@0 226 MainWindow::~MainWindow()
Chris@0 227 {
Chris@0 228 closeSession();
Chris@0 229 delete m_playTarget;
Chris@0 230 delete m_playSource;
Chris@0 231 delete m_viewManager;
Chris@0 232 Profiles::getInstance()->dump();
Chris@0 233 }
Chris@0 234
Chris@0 235 void
Chris@0 236 MainWindow::setupMenus()
Chris@0 237 {
Chris@0 238 QAction *action = 0;
Chris@0 239 QMenu *menu = 0;
Chris@0 240 QToolBar *toolbar = 0;
Chris@0 241
Chris@0 242 if (!m_mainMenusCreated) {
Chris@0 243 m_rightButtonMenu = new QMenu();
Chris@0 244 }
Chris@0 245
Chris@0 246 if (m_rightButtonLayerMenu) {
Chris@0 247 m_rightButtonLayerMenu->clear();
Chris@0 248 } else {
Chris@0 249 m_rightButtonLayerMenu = m_rightButtonMenu->addMenu(tr("&Layer"));
Chris@0 250 m_rightButtonMenu->addSeparator();
Chris@0 251 }
Chris@0 252
Chris@0 253 if (!m_mainMenusCreated) {
Chris@0 254
Chris@0 255 CommandHistory::getInstance()->registerMenu(m_rightButtonMenu);
Chris@0 256 m_rightButtonMenu->addSeparator();
Chris@0 257
Chris@0 258 menu = menuBar()->addMenu(tr("&File"));
Chris@0 259 toolbar = addToolBar(tr("File Toolbar"));
Chris@0 260
Chris@0 261 QIcon icon(":icons/filenew.png");
Chris@0 262 icon.addFile(":icons/filenew-22.png");
Chris@0 263 action = new QAction(icon, tr("&New Session"), this);
Chris@0 264 action->setShortcut(tr("Ctrl+N"));
Chris@0 265 action->setStatusTip(tr("Clear the current Sonic Visualiser session and start a new one"));
Chris@0 266 connect(action, SIGNAL(triggered()), this, SLOT(newSession()));
Chris@0 267 menu->addAction(action);
Chris@0 268 toolbar->addAction(action);
Chris@0 269
Chris@0 270 icon = QIcon(":icons/fileopen.png");
Chris@0 271 icon.addFile(":icons/fileopen-22.png");
Chris@0 272
Chris@0 273 action = new QAction(icon, tr("&Open Session..."), this);
Chris@0 274 action->setShortcut(tr("Ctrl+O"));
Chris@0 275 action->setStatusTip(tr("Open a previously saved Sonic Visualiser session file"));
Chris@0 276 connect(action, SIGNAL(triggered()), this, SLOT(openSession()));
Chris@0 277 menu->addAction(action);
Chris@0 278
Chris@0 279 action = new QAction(icon, tr("&Open..."), this);
Chris@0 280 action->setStatusTip(tr("Open a session file, audio file, or layer"));
Chris@0 281 connect(action, SIGNAL(triggered()), this, SLOT(openSomething()));
Chris@0 282 toolbar->addAction(action);
Chris@0 283
Chris@0 284 icon = QIcon(":icons/filesave.png");
Chris@0 285 icon.addFile(":icons/filesave-22.png");
Chris@0 286 action = new QAction(icon, tr("&Save Session"), this);
Chris@0 287 action->setShortcut(tr("Ctrl+S"));
Chris@0 288 action->setStatusTip(tr("Save the current session into a Sonic Visualiser session file"));
Chris@0 289 connect(action, SIGNAL(triggered()), this, SLOT(saveSession()));
Chris@0 290 connect(this, SIGNAL(canSave(bool)), action, SLOT(setEnabled(bool)));
Chris@0 291 menu->addAction(action);
Chris@0 292 toolbar->addAction(action);
Chris@0 293
Chris@0 294 icon = QIcon(":icons/filesaveas.png");
Chris@0 295 icon.addFile(":icons/filesaveas-22.png");
Chris@0 296 action = new QAction(icon, tr("Save Session &As..."), this);
Chris@0 297 action->setStatusTip(tr("Save the current session into a new Sonic Visualiser session file"));
Chris@0 298 connect(action, SIGNAL(triggered()), this, SLOT(saveSessionAs()));
Chris@0 299 menu->addAction(action);
Chris@0 300 toolbar->addAction(action);
Chris@0 301
Chris@0 302 menu->addSeparator();
Chris@0 303
Chris@0 304 action = new QAction(tr("&Import Audio File..."), this);
Chris@0 305 action->setShortcut(tr("Ctrl+I"));
Chris@0 306 action->setStatusTip(tr("Import an existing audio file"));
Chris@0 307 connect(action, SIGNAL(triggered()), this, SLOT(importAudio()));
Chris@0 308 menu->addAction(action);
Chris@0 309
Chris@0 310 action = new QAction(tr("Import Secondary Audio File..."), this);
Chris@0 311 action->setShortcut(tr("Ctrl+Shift+I"));
Chris@0 312 action->setStatusTip(tr("Import an extra audio file as a separate layer"));
Chris@0 313 connect(action, SIGNAL(triggered()), this, SLOT(importMoreAudio()));
Chris@0 314 connect(this, SIGNAL(canImportMoreAudio(bool)), action, SLOT(setEnabled(bool)));
Chris@0 315 menu->addAction(action);
Chris@0 316
Chris@0 317 action = new QAction(tr("&Export Audio File..."), this);
Chris@0 318 action->setStatusTip(tr("Export selection as an audio file"));
Chris@0 319 connect(action, SIGNAL(triggered()), this, SLOT(exportAudio()));
Chris@0 320 connect(this, SIGNAL(canExportAudio(bool)), action, SLOT(setEnabled(bool)));
Chris@0 321 menu->addAction(action);
Chris@0 322
Chris@0 323 menu->addSeparator();
Chris@0 324
Chris@0 325 action = new QAction(tr("Import Annotation &Layer..."), this);
Chris@0 326 action->setShortcut(tr("Ctrl+L"));
Chris@0 327 action->setStatusTip(tr("Import layer data from an existing file"));
Chris@0 328 connect(action, SIGNAL(triggered()), this, SLOT(importLayer()));
Chris@0 329 connect(this, SIGNAL(canImportLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 330 menu->addAction(action);
Chris@0 331
Chris@0 332 action = new QAction(tr("Export Annotation Layer..."), this);
Chris@0 333 action->setStatusTip(tr("Export layer data to a file"));
Chris@0 334 connect(action, SIGNAL(triggered()), this, SLOT(exportLayer()));
Chris@0 335 connect(this, SIGNAL(canExportLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 336 menu->addAction(action);
Chris@0 337
Chris@0 338 menu->addSeparator();
Chris@0 339 m_recentFilesMenu = menu->addMenu(tr("&Recent Files"));
Chris@0 340 menu->addMenu(m_recentFilesMenu);
Chris@0 341 setupRecentFilesMenu();
Chris@0 342 connect(RecentFiles::getInstance(), SIGNAL(recentFilesChanged()),
Chris@0 343 this, SLOT(setupRecentFilesMenu()));
Chris@0 344
Chris@0 345 menu->addSeparator();
Chris@0 346 action = new QAction(tr("&Preferences..."), this);
Chris@0 347 action->setStatusTip(tr("Adjust the application preferences"));
Chris@0 348 connect(action, SIGNAL(triggered()), this, SLOT(preferences()));
Chris@0 349 menu->addAction(action);
Chris@0 350
Chris@0 351 /*!!!
Chris@0 352 menu->addSeparator();
Chris@0 353
Chris@0 354 action = new QAction(tr("Play / Pause"), this);
Chris@0 355 action->setShortcut(tr("Space"));
Chris@0 356 action->setStatusTip(tr("Start or stop playback from the current position"));
Chris@0 357 connect(action, SIGNAL(triggered()), this, SLOT(play()));
Chris@0 358 menu->addAction(action);
Chris@0 359 */
Chris@0 360
Chris@0 361 menu->addSeparator();
Chris@0 362 action = new QAction(QIcon(":/icons/exit.png"),
Chris@0 363 tr("&Quit"), this);
Chris@0 364 action->setShortcut(tr("Ctrl+Q"));
Chris@0 365 connect(action, SIGNAL(triggered()), this, SLOT(close()));
Chris@0 366 menu->addAction(action);
Chris@0 367
Chris@0 368 menu = menuBar()->addMenu(tr("&Edit"));
Chris@0 369 CommandHistory::getInstance()->registerMenu(menu);
Chris@0 370
Chris@0 371 menu->addSeparator();
Chris@0 372
Chris@0 373 action = new QAction(QIcon(":/icons/editcut.png"),
Chris@0 374 tr("Cu&t"), this);
Chris@0 375 action->setShortcut(tr("Ctrl+X"));
Chris@0 376 connect(action, SIGNAL(triggered()), this, SLOT(cut()));
Chris@0 377 connect(this, SIGNAL(canEditSelection(bool)), action, SLOT(setEnabled(bool)));
Chris@0 378 menu->addAction(action);
Chris@0 379 m_rightButtonMenu->addAction(action);
Chris@0 380
Chris@0 381 action = new QAction(QIcon(":/icons/editcopy.png"),
Chris@0 382 tr("&Copy"), this);
Chris@0 383 action->setShortcut(tr("Ctrl+C"));
Chris@0 384 connect(action, SIGNAL(triggered()), this, SLOT(copy()));
Chris@0 385 connect(this, SIGNAL(canEditSelection(bool)), action, SLOT(setEnabled(bool)));
Chris@0 386 menu->addAction(action);
Chris@0 387 m_rightButtonMenu->addAction(action);
Chris@0 388
Chris@0 389 action = new QAction(QIcon(":/icons/editpaste.png"),
Chris@0 390 tr("&Paste"), this);
Chris@0 391 action->setShortcut(tr("Ctrl+V"));
Chris@0 392 connect(action, SIGNAL(triggered()), this, SLOT(paste()));
Chris@0 393 connect(this, SIGNAL(canPaste(bool)), action, SLOT(setEnabled(bool)));
Chris@0 394 menu->addAction(action);
Chris@0 395 m_rightButtonMenu->addAction(action);
Chris@0 396
Chris@0 397 action = new QAction(tr("&Delete Selected Items"), this);
Chris@0 398 action->setShortcut(tr("Del"));
Chris@0 399 connect(action, SIGNAL(triggered()), this, SLOT(deleteSelected()));
Chris@0 400 connect(this, SIGNAL(canEditSelection(bool)), action, SLOT(setEnabled(bool)));
Chris@0 401 menu->addAction(action);
Chris@0 402 m_rightButtonMenu->addAction(action);
Chris@0 403
Chris@0 404 menu->addSeparator();
Chris@0 405 m_rightButtonMenu->addSeparator();
Chris@0 406
Chris@0 407 action = new QAction(tr("Select &All"), this);
Chris@0 408 action->setShortcut(tr("Ctrl+A"));
Chris@0 409 connect(action, SIGNAL(triggered()), this, SLOT(selectAll()));
Chris@0 410 connect(this, SIGNAL(canSelect(bool)), action, SLOT(setEnabled(bool)));
Chris@0 411 menu->addAction(action);
Chris@0 412 m_rightButtonMenu->addAction(action);
Chris@0 413
Chris@0 414 action = new QAction(tr("Select &Visible Range"), this);
Chris@0 415 action->setShortcut(tr("Ctrl+Shift+A"));
Chris@0 416 connect(action, SIGNAL(triggered()), this, SLOT(selectVisible()));
Chris@0 417 connect(this, SIGNAL(canSelect(bool)), action, SLOT(setEnabled(bool)));
Chris@0 418 menu->addAction(action);
Chris@0 419
Chris@0 420 action = new QAction(tr("Select to &Start"), this);
Chris@0 421 action->setShortcut(tr("Shift+Left"));
Chris@0 422 connect(action, SIGNAL(triggered()), this, SLOT(selectToStart()));
Chris@0 423 connect(this, SIGNAL(canSelect(bool)), action, SLOT(setEnabled(bool)));
Chris@0 424 menu->addAction(action);
Chris@0 425
Chris@0 426 action = new QAction(tr("Select to &End"), this);
Chris@0 427 action->setShortcut(tr("Shift+Right"));
Chris@0 428 connect(action, SIGNAL(triggered()), this, SLOT(selectToEnd()));
Chris@0 429 connect(this, SIGNAL(canSelect(bool)), action, SLOT(setEnabled(bool)));
Chris@0 430 menu->addAction(action);
Chris@0 431
Chris@0 432 action = new QAction(tr("C&lear Selection"), this);
Chris@0 433 action->setShortcut(tr("Esc"));
Chris@0 434 connect(action, SIGNAL(triggered()), this, SLOT(clearSelection()));
Chris@0 435 connect(this, SIGNAL(canClearSelection(bool)), action, SLOT(setEnabled(bool)));
Chris@0 436 menu->addAction(action);
Chris@0 437 m_rightButtonMenu->addAction(action);
Chris@0 438
Chris@0 439 menu->addSeparator();
Chris@0 440
Chris@0 441 action = new QAction(tr("&Insert Instant at Playback Position"), this);
Chris@0 442 action->setShortcut(tr("Enter"));
Chris@0 443 connect(action, SIGNAL(triggered()), this, SLOT(insertInstant()));
Chris@0 444 connect(this, SIGNAL(canInsertInstant(bool)), action, SLOT(setEnabled(bool)));
Chris@0 445 menu->addAction(action);
Chris@0 446
Chris@7 447 // Laptop shortcut (no keypad Enter key)
Chris@7 448 connect(new QShortcut(tr(";"), this), SIGNAL(activated()),
Chris@7 449 this, SLOT(insertInstant()));
Chris@7 450
Chris@0 451 menu = menuBar()->addMenu(tr("&View"));
Chris@0 452
Chris@0 453 QActionGroup *overlayGroup = new QActionGroup(this);
Chris@0 454
Chris@0 455 action = new QAction(tr("&No Text Overlays"), this);
Chris@0 456 action->setShortcut(tr("0"));
Chris@0 457 action->setStatusTip(tr("Show no texts for frame times, layer names etc"));
Chris@0 458 connect(action, SIGNAL(triggered()), this, SLOT(showNoOverlays()));
Chris@0 459 action->setCheckable(true);
Chris@0 460 action->setChecked(false);
Chris@0 461 overlayGroup->addAction(action);
Chris@0 462 menu->addAction(action);
Chris@0 463
Chris@0 464 action = new QAction(tr("Basic &Text Overlays"), this);
Chris@0 465 action->setShortcut(tr("9"));
Chris@0 466 action->setStatusTip(tr("Show texts for frame times etc, but not layer names etc"));
Chris@0 467 connect(action, SIGNAL(triggered()), this, SLOT(showBasicOverlays()));
Chris@0 468 action->setCheckable(true);
Chris@0 469 action->setChecked(true);
Chris@0 470 overlayGroup->addAction(action);
Chris@0 471 menu->addAction(action);
Chris@0 472
Chris@0 473 action = new QAction(tr("&All Text Overlays"), this);
Chris@0 474 action->setShortcut(tr("8"));
Chris@0 475 action->setStatusTip(tr("Show texts for frame times, layer names etc"));
Chris@7 476 connect(action, SIGNAL(triggered()), this, SLOT(showAllTextOverlays()));
Chris@0 477 action->setCheckable(true);
Chris@0 478 action->setChecked(false);
Chris@0 479 overlayGroup->addAction(action);
Chris@0 480 menu->addAction(action);
Chris@0 481
Chris@0 482 menu->addSeparator();
Chris@0 483
Chris@0 484 action = new QAction(tr("Scroll &Left"), this);
Chris@0 485 action->setShortcut(tr("Left"));
Chris@0 486 action->setStatusTip(tr("Scroll the current pane to the left"));
Chris@0 487 connect(action, SIGNAL(triggered()), this, SLOT(scrollLeft()));
Chris@0 488 connect(this, SIGNAL(canScroll(bool)), action, SLOT(setEnabled(bool)));
Chris@0 489 menu->addAction(action);
Chris@0 490
Chris@0 491 action = new QAction(tr("Scroll &Right"), this);
Chris@0 492 action->setShortcut(tr("Right"));
Chris@0 493 action->setStatusTip(tr("Scroll the current pane to the right"));
Chris@0 494 connect(action, SIGNAL(triggered()), this, SLOT(scrollRight()));
Chris@0 495 connect(this, SIGNAL(canScroll(bool)), action, SLOT(setEnabled(bool)));
Chris@0 496 menu->addAction(action);
Chris@0 497
Chris@0 498 action = new QAction(tr("Jump Left"), this);
Chris@0 499 action->setShortcut(tr("Ctrl+Left"));
Chris@0 500 action->setStatusTip(tr("Scroll the current pane a big step to the left"));
Chris@0 501 connect(action, SIGNAL(triggered()), this, SLOT(jumpLeft()));
Chris@0 502 connect(this, SIGNAL(canScroll(bool)), action, SLOT(setEnabled(bool)));
Chris@0 503 menu->addAction(action);
Chris@0 504
Chris@0 505 action = new QAction(tr("Jump Right"), this);
Chris@0 506 action->setShortcut(tr("Ctrl+Right"));
Chris@0 507 action->setStatusTip(tr("Scroll the current pane a big step to the right"));
Chris@0 508 connect(action, SIGNAL(triggered()), this, SLOT(jumpRight()));
Chris@0 509 connect(this, SIGNAL(canScroll(bool)), action, SLOT(setEnabled(bool)));
Chris@0 510 menu->addAction(action);
Chris@0 511
Chris@0 512 menu->addSeparator();
Chris@0 513
Chris@0 514 action = new QAction(QIcon(":/icons/zoom-in.png"),
Chris@0 515 tr("Zoom &In"), this);
Chris@0 516 action->setShortcut(tr("Up"));
Chris@0 517 action->setStatusTip(tr("Increase the zoom level"));
Chris@0 518 connect(action, SIGNAL(triggered()), this, SLOT(zoomIn()));
Chris@0 519 connect(this, SIGNAL(canZoom(bool)), action, SLOT(setEnabled(bool)));
Chris@0 520 menu->addAction(action);
Chris@0 521
Chris@0 522 action = new QAction(QIcon(":/icons/zoom-out.png"),
Chris@0 523 tr("Zoom &Out"), this);
Chris@0 524 action->setShortcut(tr("Down"));
Chris@0 525 action->setStatusTip(tr("Decrease the zoom level"));
Chris@0 526 connect(action, SIGNAL(triggered()), this, SLOT(zoomOut()));
Chris@0 527 connect(this, SIGNAL(canZoom(bool)), action, SLOT(setEnabled(bool)));
Chris@0 528 menu->addAction(action);
Chris@0 529
Chris@0 530 action = new QAction(tr("Restore &Default Zoom"), this);
Chris@0 531 connect(action, SIGNAL(triggered()), this, SLOT(zoomDefault()));
Chris@0 532 connect(this, SIGNAL(canZoom(bool)), action, SLOT(setEnabled(bool)));
Chris@0 533 menu->addAction(action);
Chris@0 534
Chris@0 535 action = new QAction(tr("Zoom to &Fit"), this);
Chris@0 536 action->setStatusTip(tr("Zoom to show the whole file"));
Chris@0 537 connect(action, SIGNAL(triggered()), this, SLOT(zoomToFit()));
Chris@0 538 connect(this, SIGNAL(canZoom(bool)), action, SLOT(setEnabled(bool)));
Chris@0 539 menu->addAction(action);
Chris@7 540
Chris@7 541 action = new QAction(tr("Show &Zoom Wheels"), this);
Chris@7 542 action->setShortcut(tr("Z"));
Chris@7 543 action->setStatusTip(tr("Show thumbwheels for zooming horizontally and vertically"));
Chris@7 544 connect(action, SIGNAL(triggered()), this, SLOT(toggleZoomWheels()));
Chris@7 545 action->setCheckable(true);
Chris@7 546 action->setChecked(m_viewManager->getZoomWheelsEnabled());
Chris@7 547 menu->addAction(action);
Chris@0 548
Chris@0 549 /*!!! This one doesn't work properly yet
Chris@0 550
Chris@0 551 menu->addSeparator();
Chris@0 552
Chris@0 553 action = new QAction(tr("Show &Layer Hierarchy"), this);
Chris@0 554 action->setShortcut(tr("Alt+L"));
Chris@0 555 connect(action, SIGNAL(triggered()), this, SLOT(showLayerTree()));
Chris@0 556 menu->addAction(action);
Chris@0 557 */
Chris@0 558 }
Chris@0 559
Chris@0 560 if (m_paneMenu) {
Chris@0 561 m_paneActions.clear();
Chris@0 562 m_paneMenu->clear();
Chris@0 563 } else {
Chris@0 564 m_paneMenu = menuBar()->addMenu(tr("&Pane"));
Chris@0 565 }
Chris@0 566
Chris@0 567 if (m_layerMenu) {
Chris@0 568 m_layerTransformActions.clear();
Chris@0 569 m_layerActions.clear();
Chris@0 570 m_layerMenu->clear();
Chris@0 571 } else {
Chris@0 572 m_layerMenu = menuBar()->addMenu(tr("&Layer"));
Chris@0 573 }
Chris@0 574
Chris@0 575 TransformFactory::TransformList transforms =
Chris@0 576 TransformFactory::getInstance()->getAllTransforms();
Chris@0 577
Chris@33 578 vector<QString> types =
Chris@0 579 TransformFactory::getInstance()->getAllTransformTypes();
Chris@0 580
Chris@33 581 map<QString, map<QString, QMenu *> > categoryMenus;
Chris@33 582 map<QString, map<QString, QMenu *> > makerMenus;
Chris@33 583
Chris@33 584 map<QString, QMenu *> byPluginNameMenus;
Chris@33 585 map<QString, map<QString, QMenu *> > pluginNameMenus;
Chris@33 586
Chris@33 587 for (vector<QString>::iterator i = types.begin(); i != types.end(); ++i) {
Chris@33 588
Chris@33 589 if (i != types.begin()) {
Chris@33 590 m_layerMenu->addSeparator();
Chris@33 591 m_rightButtonLayerMenu->addSeparator();
Chris@33 592 }
Chris@33 593
Chris@33 594 QString byCategoryLabel = tr("%1 by Category").arg(*i);
Chris@33 595 QMenu *byCategoryMenu = m_layerMenu->addMenu(byCategoryLabel);
Chris@33 596 m_rightButtonLayerMenu->addMenu(byCategoryMenu);
Chris@33 597
Chris@33 598 vector<QString> categories =
Chris@33 599 TransformFactory::getInstance()->getTransformCategories(*i);
Chris@33 600
Chris@33 601 for (vector<QString>::iterator j = categories.begin();
Chris@33 602 j != categories.end(); ++j) {
Chris@33 603
Chris@33 604 QString category = *j;
Chris@33 605 if (category == "") category = tr("Unclassified");
Chris@33 606
Chris@33 607 if (categories.size() < 2) {
Chris@33 608 categoryMenus[*i][category] = byCategoryMenu;
Chris@33 609 continue;
Chris@33 610 }
Chris@33 611
Chris@33 612 QStringList components = category.split(" > ");
Chris@33 613 QString key;
Chris@33 614
Chris@33 615 for (QStringList::iterator k = components.begin();
Chris@33 616 k != components.end(); ++k) {
Chris@33 617
Chris@33 618 QString parentKey = key;
Chris@33 619 if (key != "") key += " > ";
Chris@33 620 key += *k;
Chris@33 621
Chris@33 622 if (categoryMenus[*i].find(key) == categoryMenus[*i].end()) {
Chris@33 623 if (parentKey == "") {
Chris@33 624 categoryMenus[*i][key] = byCategoryMenu->addMenu(*k);
Chris@33 625 } else {
Chris@33 626 categoryMenus[*i][key] =
Chris@33 627 categoryMenus[*i][parentKey]->addMenu(*k);
Chris@33 628 }
Chris@33 629 }
Chris@33 630 }
Chris@33 631 }
Chris@33 632
Chris@33 633 QString byMakerLabel = tr("%1 by Maker").arg(*i);
Chris@33 634 QMenu *byMakerMenu = m_layerMenu->addMenu(byMakerLabel);
Chris@33 635 m_rightButtonLayerMenu->addMenu(byMakerMenu);
Chris@33 636
Chris@33 637 vector<QString> makers =
Chris@33 638 TransformFactory::getInstance()->getTransformMakers(*i);
Chris@33 639
Chris@33 640 for (vector<QString>::iterator j = makers.begin();
Chris@33 641 j != makers.end(); ++j) {
Chris@33 642
Chris@33 643 QString maker = *j;
Chris@33 644 if (maker == "") maker = tr("Unknown");
Chris@33 645
Chris@33 646 makerMenus[*i][maker] = byMakerMenu->addMenu(maker);
Chris@33 647 }
Chris@33 648
Chris@33 649 QString byPluginNameLabel = tr("%1 by Plugin Name").arg(*i);
Chris@33 650 byPluginNameMenus[*i] = m_layerMenu->addMenu(byPluginNameLabel);
Chris@33 651 m_rightButtonLayerMenu->addMenu(byPluginNameMenus[*i]);
Chris@0 652 }
Chris@0 653
Chris@0 654 for (unsigned int i = 0; i < transforms.size(); ++i) {
Chris@0 655
Chris@0 656 QString description = transforms[i].description;
Chris@0 657 if (description == "") description = transforms[i].name;
Chris@0 658
Chris@33 659 QString type = transforms[i].type;
Chris@33 660
Chris@33 661 QString category = transforms[i].category;
Chris@33 662 if (category == "") category = tr("Unclassified");
Chris@33 663
Chris@33 664 QString maker = transforms[i].maker;
Chris@33 665 if (maker == "") maker = tr("Unknown");
Chris@33 666
Chris@33 667 QString pluginName = description.section(": ", 0, 0);
Chris@33 668 QString output = description.section(": ", 1);
Chris@33 669
Chris@33 670 action = new QAction(tr("%1...").arg(description), this);
Chris@0 671 connect(action, SIGNAL(triggered()), this, SLOT(addLayer()));
Chris@0 672 m_layerTransformActions[action] = transforms[i].name;
Chris@0 673 connect(this, SIGNAL(canAddLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@33 674
Chris@33 675 if (categoryMenus[type].find(category) == categoryMenus[type].end()) {
Chris@33 676 std::cerr << "WARNING: MainWindow::setupMenus: Internal error: "
Chris@33 677 << "No category menu for transform \""
Chris@33 678 << description.toStdString() << "\" (category = \""
Chris@33 679 << category.toStdString() << "\")" << std::endl;
Chris@33 680 } else {
Chris@33 681 categoryMenus[type][category]->addAction(action);
Chris@33 682 }
Chris@33 683
Chris@33 684 if (makerMenus[type].find(maker) == makerMenus[type].end()) {
Chris@33 685 std::cerr << "WARNING: MainWindow::setupMenus: Internal error: "
Chris@33 686 << "No maker menu for transform \""
Chris@33 687 << description.toStdString() << "\" (maker = \""
Chris@33 688 << maker.toStdString() << "\")" << std::endl;
Chris@33 689 } else {
Chris@33 690 makerMenus[type][maker]->addAction(action);
Chris@33 691 }
Chris@33 692
Chris@33 693 action = new QAction(tr("%1...").arg(output == "" ? pluginName : output), this);
Chris@33 694 connect(action, SIGNAL(triggered()), this, SLOT(addLayer()));
Chris@33 695 m_layerTransformActions[action] = transforms[i].name;
Chris@33 696 connect(this, SIGNAL(canAddLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@33 697
Chris@33 698 if (pluginNameMenus[type].find(pluginName) ==
Chris@33 699 pluginNameMenus[type].end()) {
Chris@33 700
Chris@33 701 if (output == "") {
Chris@33 702 byPluginNameMenus[type]->addAction(action);
Chris@33 703 } else {
Chris@33 704 pluginNameMenus[type][pluginName] =
Chris@33 705 byPluginNameMenus[type]->addMenu(pluginName);
Chris@33 706 connect(this, SIGNAL(canAddLayer(bool)),
Chris@33 707 pluginNameMenus[type][pluginName],
Chris@33 708 SLOT(setEnabled(bool)));
Chris@33 709 }
Chris@33 710 }
Chris@33 711
Chris@33 712 if (pluginNameMenus[type].find(pluginName) !=
Chris@33 713 pluginNameMenus[type].end()) {
Chris@33 714 pluginNameMenus[type][pluginName]->addAction(action);
Chris@33 715 }
Chris@0 716 }
Chris@0 717
Chris@0 718 m_rightButtonLayerMenu->addSeparator();
Chris@0 719
Chris@0 720 menu = m_paneMenu;
Chris@0 721
Chris@0 722 action = new QAction(QIcon(":/icons/pane.png"), tr("Add &New Pane"), this);
Chris@0 723 action->setShortcut(tr("Alt+N"));
Chris@0 724 action->setStatusTip(tr("Add a new pane containing only a time ruler"));
Chris@0 725 connect(action, SIGNAL(triggered()), this, SLOT(addPane()));
Chris@0 726 connect(this, SIGNAL(canAddPane(bool)), action, SLOT(setEnabled(bool)));
Chris@0 727 m_paneActions[action] = PaneConfiguration(LayerFactory::TimeRuler);
Chris@0 728 menu->addAction(action);
Chris@0 729
Chris@0 730 menu->addSeparator();
Chris@0 731
Chris@0 732 menu = m_layerMenu;
Chris@0 733
Chris@0 734 menu->addSeparator();
Chris@0 735
Chris@0 736 LayerFactory::LayerTypeSet emptyLayerTypes =
Chris@0 737 LayerFactory::getInstance()->getValidEmptyLayerTypes();
Chris@0 738
Chris@0 739 for (LayerFactory::LayerTypeSet::iterator i = emptyLayerTypes.begin();
Chris@0 740 i != emptyLayerTypes.end(); ++i) {
Chris@0 741
Chris@0 742 QIcon icon;
Chris@0 743 QString mainText, tipText, channelText;
Chris@0 744 LayerFactory::LayerType type = *i;
Chris@0 745 QString name = LayerFactory::getInstance()->getLayerPresentationName(type);
Chris@0 746
Chris@0 747 icon = QIcon(QString(":/icons/%1.png")
Chris@0 748 .arg(LayerFactory::getInstance()->getLayerIconName(type)));
Chris@0 749
Chris@0 750 mainText = tr("Add New %1 Layer").arg(name);
Chris@0 751 tipText = tr("Add a new empty layer of type %1").arg(name);
Chris@0 752
Chris@0 753 action = new QAction(icon, mainText, this);
Chris@0 754 action->setStatusTip(tipText);
Chris@0 755
Chris@0 756 if (type == LayerFactory::Text) {
Chris@0 757 action->setShortcut(tr("Alt+T"));
Chris@0 758 }
Chris@0 759
Chris@0 760 connect(action, SIGNAL(triggered()), this, SLOT(addLayer()));
Chris@0 761 connect(this, SIGNAL(canAddLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 762 m_layerActions[action] = type;
Chris@0 763 menu->addAction(action);
Chris@0 764 m_rightButtonLayerMenu->addAction(action);
Chris@0 765 }
Chris@0 766
Chris@0 767 m_rightButtonLayerMenu->addSeparator();
Chris@0 768 menu->addSeparator();
Chris@0 769
Chris@0 770 int channels = 1;
Chris@0 771 if (getMainModel()) channels = getMainModel()->getChannelCount();
Chris@0 772
Chris@0 773 if (channels < 1) channels = 1;
Chris@0 774
Chris@0 775 LayerFactory::LayerType backgroundTypes[] = {
Chris@0 776 LayerFactory::Waveform,
Chris@0 777 LayerFactory::Spectrogram,
Chris@0 778 LayerFactory::MelodicRangeSpectrogram,
Chris@7 779 LayerFactory::PeakFrequencySpectrogram,
Chris@7 780 LayerFactory::Spectrum
Chris@0 781 };
Chris@0 782
Chris@0 783 for (unsigned int i = 0;
Chris@0 784 i < sizeof(backgroundTypes)/sizeof(backgroundTypes[0]); ++i) {
Chris@0 785
Chris@0 786 for (int menuType = 0; menuType <= 1; ++menuType) { // pane, layer
Chris@0 787
Chris@0 788 if (menuType == 0) menu = m_paneMenu;
Chris@0 789 else menu = m_layerMenu;
Chris@0 790
Chris@0 791 QMenu *submenu = 0;
Chris@0 792
Chris@0 793 for (int c = 0; c <= channels; ++c) {
Chris@0 794
Chris@0 795 if (c == 1 && channels == 1) continue;
Chris@0 796 bool isDefault = (c == 0);
Chris@0 797 bool isOnly = (isDefault && (channels == 1));
Chris@0 798
Chris@0 799 if (menuType == 1) {
Chris@0 800 if (isDefault) isOnly = true;
Chris@0 801 else continue;
Chris@0 802 }
Chris@0 803
Chris@0 804 QIcon icon;
Chris@0 805 QString mainText, shortcutText, tipText, channelText;
Chris@0 806 LayerFactory::LayerType type = backgroundTypes[i];
Chris@0 807 bool mono = true;
Chris@0 808
Chris@0 809 switch (type) {
Chris@0 810
Chris@0 811 case LayerFactory::Waveform:
Chris@0 812 icon = QIcon(":/icons/waveform.png");
Chris@0 813 mainText = tr("Add &Waveform");
Chris@0 814 if (menuType == 0) {
Chris@0 815 shortcutText = tr("Alt+W");
Chris@0 816 tipText = tr("Add a new pane showing a waveform view");
Chris@0 817 } else {
Chris@0 818 tipText = tr("Add a new layer showing a waveform view");
Chris@0 819 }
Chris@0 820 mono = false;
Chris@0 821 break;
Chris@0 822
Chris@0 823 case LayerFactory::Spectrogram:
Chris@0 824 mainText = tr("Add &Spectrogram");
Chris@0 825 if (menuType == 0) {
Chris@0 826 shortcutText = tr("Alt+S");
Chris@0 827 tipText = tr("Add a new pane showing a dB spectrogram");
Chris@0 828 } else {
Chris@0 829 tipText = tr("Add a new layer showing a dB spectrogram");
Chris@0 830 }
Chris@0 831 break;
Chris@0 832
Chris@0 833 case LayerFactory::MelodicRangeSpectrogram:
Chris@0 834 mainText = tr("Add &Melodic Range Spectrogram");
Chris@0 835 if (menuType == 0) {
Chris@0 836 shortcutText = tr("Alt+M");
Chris@0 837 tipText = tr("Add a new pane showing a spectrogram set up for a pitch overview");
Chris@0 838 } else {
Chris@0 839 tipText = tr("Add a new layer showing a spectrogram set up for a pitch overview");
Chris@0 840 }
Chris@0 841 break;
Chris@0 842
Chris@0 843 case LayerFactory::PeakFrequencySpectrogram:
Chris@0 844 mainText = tr("Add &Peak Frequency Spectrogram");
Chris@0 845 if (menuType == 0) {
Chris@0 846 shortcutText = tr("Alt+P");
Chris@0 847 tipText = tr("Add a new pane showing a spectrogram set up for tracking frequencies");
Chris@0 848 } else {
Chris@0 849 tipText = tr("Add a new layer showing a spectrogram set up for tracking frequencies");
Chris@0 850 }
Chris@0 851 break;
Chris@0 852
Chris@7 853 case LayerFactory::Spectrum:
Chris@7 854 mainText = tr("Add Spectr&um");
Chris@7 855 if (menuType == 0) {
Chris@7 856 shortcutText = tr("Alt+U");
Chris@7 857 tipText = tr("Add a new pane showing a frequency spectrum");
Chris@7 858 } else {
Chris@7 859 tipText = tr("Add a new layer showing a frequency spectrum");
Chris@7 860 }
Chris@7 861 break;
Chris@7 862
Chris@0 863 default: break;
Chris@0 864 }
Chris@0 865
Chris@0 866 if (isOnly) {
Chris@0 867
Chris@0 868 action = new QAction(icon, mainText, this);
Chris@0 869 action->setShortcut(shortcutText);
Chris@0 870 action->setStatusTip(tipText);
Chris@0 871 if (menuType == 0) {
Chris@0 872 connect(action, SIGNAL(triggered()), this, SLOT(addPane()));
Chris@0 873 connect(this, SIGNAL(canAddPane(bool)), action, SLOT(setEnabled(bool)));
Chris@0 874 m_paneActions[action] = PaneConfiguration(type);
Chris@0 875 } else {
Chris@0 876 connect(action, SIGNAL(triggered()), this, SLOT(addLayer()));
Chris@0 877 connect(this, SIGNAL(canAddLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 878 m_layerActions[action] = type;
Chris@0 879 }
Chris@0 880 menu->addAction(action);
Chris@0 881
Chris@0 882 } else {
Chris@0 883
Chris@0 884 QString actionText;
Chris@0 885 if (c == 0)
Chris@0 886 if (mono) actionText = tr("&All Channels Mixed");
Chris@0 887 else actionText = tr("&All Channels");
Chris@0 888 else actionText = tr("Channel &%1").arg(c);
Chris@0 889
Chris@0 890 if (!submenu) {
Chris@0 891 submenu = menu->addMenu(mainText);
Chris@0 892 }
Chris@0 893
Chris@0 894 action = new QAction(icon, actionText, this);
Chris@0 895 if (isDefault) action->setShortcut(shortcutText);
Chris@0 896 action->setStatusTip(tipText);
Chris@0 897 if (menuType == 0) {
Chris@0 898 connect(action, SIGNAL(triggered()), this, SLOT(addPane()));
Chris@0 899 connect(this, SIGNAL(canAddPane(bool)), action, SLOT(setEnabled(bool)));
Chris@0 900 m_paneActions[action] = PaneConfiguration(type, c - 1);
Chris@0 901 } else {
Chris@0 902 connect(action, SIGNAL(triggered()), this, SLOT(addLayer()));
Chris@0 903 connect(this, SIGNAL(canAddLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 904 m_layerActions[action] = type;
Chris@0 905 }
Chris@0 906 submenu->addAction(action);
Chris@0 907 }
Chris@0 908 }
Chris@0 909 }
Chris@0 910 }
Chris@0 911
Chris@0 912 menu = m_paneMenu;
Chris@0 913
Chris@0 914 menu->addSeparator();
Chris@0 915
Chris@0 916 action = new QAction(QIcon(":/icons/editdelete.png"), tr("&Delete Pane"), this);
Chris@0 917 action->setShortcut(tr("Alt+D"));
Chris@0 918 action->setStatusTip(tr("Delete the currently selected pane"));
Chris@0 919 connect(action, SIGNAL(triggered()), this, SLOT(deleteCurrentPane()));
Chris@0 920 connect(this, SIGNAL(canDeleteCurrentPane(bool)), action, SLOT(setEnabled(bool)));
Chris@0 921 menu->addAction(action);
Chris@0 922
Chris@0 923 menu = m_layerMenu;
Chris@0 924
Chris@0 925 action = new QAction(QIcon(":/icons/timeruler.png"), tr("Add &Time Ruler"), this);
Chris@0 926 action->setStatusTip(tr("Add a new layer showing a time ruler"));
Chris@0 927 connect(action, SIGNAL(triggered()), this, SLOT(addLayer()));
Chris@0 928 connect(this, SIGNAL(canAddLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 929 m_layerActions[action] = LayerFactory::TimeRuler;
Chris@0 930 menu->addAction(action);
Chris@0 931
Chris@0 932 menu->addSeparator();
Chris@0 933
Chris@0 934 m_existingLayersMenu = menu->addMenu(tr("Add &Existing Layer"));
Chris@0 935 m_rightButtonLayerMenu->addMenu(m_existingLayersMenu);
Chris@0 936 setupExistingLayersMenu();
Chris@0 937
Chris@0 938 m_rightButtonLayerMenu->addSeparator();
Chris@0 939 menu->addSeparator();
Chris@0 940
Chris@0 941 action = new QAction(tr("&Rename Layer..."), this);
Chris@0 942 action->setShortcut(tr("Alt+R"));
Chris@0 943 action->setStatusTip(tr("Rename the currently active layer"));
Chris@0 944 connect(action, SIGNAL(triggered()), this, SLOT(renameCurrentLayer()));
Chris@0 945 connect(this, SIGNAL(canRenameLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 946 menu->addAction(action);
Chris@0 947 m_rightButtonLayerMenu->addAction(action);
Chris@0 948
Chris@0 949 action = new QAction(QIcon(":/icons/editdelete.png"), tr("&Delete Layer"), this);
Chris@0 950 action->setShortcut(tr("Alt+Shift+D"));
Chris@0 951 action->setStatusTip(tr("Delete the currently active layer"));
Chris@0 952 connect(action, SIGNAL(triggered()), this, SLOT(deleteCurrentLayer()));
Chris@0 953 connect(this, SIGNAL(canDeleteCurrentLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 954 menu->addAction(action);
Chris@0 955 m_rightButtonLayerMenu->addAction(action);
Chris@0 956
Chris@0 957 if (!m_mainMenusCreated) {
Chris@0 958
Chris@0 959 menu = menuBar()->addMenu(tr("&Help"));
Chris@0 960
Chris@0 961 action = new QAction(tr("&Help Reference"), this);
Chris@0 962 action->setStatusTip(tr("Open the Sonic Visualiser reference manual"));
Chris@0 963 connect(action, SIGNAL(triggered()), this, SLOT(help()));
Chris@0 964 menu->addAction(action);
Chris@0 965
Chris@0 966 action = new QAction(tr("Sonic Visualiser on the &Web"), this);
Chris@0 967 action->setStatusTip(tr("Open the Sonic Visualiser website"));
Chris@0 968 connect(action, SIGNAL(triggered()), this, SLOT(website()));
Chris@0 969 menu->addAction(action);
Chris@0 970
Chris@0 971 action = new QAction(tr("&About Sonic Visualiser"), this);
Chris@0 972 action->setStatusTip(tr("Show information about Sonic Visualiser"));
Chris@0 973 connect(action, SIGNAL(triggered()), this, SLOT(about()));
Chris@0 974 menu->addAction(action);
Chris@0 975 /*
Chris@0 976 action = new QAction(tr("About &Qt"), this);
Chris@0 977 action->setStatusTip(tr("Show information about Qt"));
Chris@0 978 connect(action, SIGNAL(triggered()),
Chris@0 979 QApplication::getInstance(), SLOT(aboutQt()));
Chris@0 980 menu->addAction(action);
Chris@0 981 */
Chris@0 982 }
Chris@0 983
Chris@0 984 m_mainMenusCreated = true;
Chris@0 985 }
Chris@0 986
Chris@0 987 void
Chris@0 988 MainWindow::setupRecentFilesMenu()
Chris@0 989 {
Chris@0 990 m_recentFilesMenu->clear();
Chris@33 991 vector<QString> files = RecentFiles::getInstance()->getRecentFiles();
Chris@0 992 for (size_t i = 0; i < files.size(); ++i) {
Chris@0 993 QAction *action = new QAction(files[i], this);
Chris@0 994 connect(action, SIGNAL(triggered()), this, SLOT(openRecentFile()));
Chris@0 995 m_recentFilesMenu->addAction(action);
Chris@0 996 }
Chris@0 997 }
Chris@0 998
Chris@0 999 void
Chris@0 1000 MainWindow::setupExistingLayersMenu()
Chris@0 1001 {
Chris@0 1002 if (!m_existingLayersMenu) return; // should have been created by setupMenus
Chris@0 1003
Chris@0 1004 // std::cerr << "MainWindow::setupExistingLayersMenu" << std::endl;
Chris@0 1005
Chris@0 1006 m_existingLayersMenu->clear();
Chris@0 1007 m_existingLayerActions.clear();
Chris@0 1008
Chris@33 1009 vector<Layer *> orderedLayers;
Chris@33 1010 set<Layer *> observedLayers;
Chris@0 1011
Chris@0 1012 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@0 1013
Chris@0 1014 Pane *pane = m_paneStack->getPane(i);
Chris@0 1015 if (!pane) continue;
Chris@0 1016
Chris@0 1017 for (int j = 0; j < pane->getLayerCount(); ++j) {
Chris@0 1018
Chris@0 1019 Layer *layer = pane->getLayer(j);
Chris@0 1020 if (!layer) continue;
Chris@0 1021 if (observedLayers.find(layer) != observedLayers.end()) {
Chris@0 1022 std::cerr << "found duplicate layer " << layer << std::endl;
Chris@0 1023 continue;
Chris@0 1024 }
Chris@0 1025
Chris@0 1026 // std::cerr << "found new layer " << layer << " (name = "
Chris@0 1027 // << layer->getLayerPresentationName().toStdString() << ")" << std::endl;
Chris@0 1028
Chris@0 1029 orderedLayers.push_back(layer);
Chris@0 1030 observedLayers.insert(layer);
Chris@0 1031 }
Chris@0 1032 }
Chris@0 1033
Chris@33 1034 map<QString, int> observedNames;
Chris@0 1035
Chris@0 1036 for (int i = 0; i < orderedLayers.size(); ++i) {
Chris@0 1037
Chris@0 1038 QString name = orderedLayers[i]->getLayerPresentationName();
Chris@0 1039 int n = ++observedNames[name];
Chris@0 1040 if (n > 1) name = QString("%1 <%2>").arg(name).arg(n);
Chris@0 1041
Chris@0 1042 QAction *action = new QAction(name, this);
Chris@0 1043 connect(action, SIGNAL(triggered()), this, SLOT(addLayer()));
Chris@0 1044 connect(this, SIGNAL(canAddLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1045 m_existingLayerActions[action] = orderedLayers[i];
Chris@0 1046
Chris@0 1047 m_existingLayersMenu->addAction(action);
Chris@0 1048 }
Chris@0 1049 }
Chris@0 1050
Chris@0 1051 void
Chris@0 1052 MainWindow::setupToolbars()
Chris@0 1053 {
Chris@0 1054 QToolBar *toolbar = addToolBar(tr("Transport Toolbar"));
Chris@0 1055
Chris@0 1056 QAction *action = toolbar->addAction(QIcon(":/icons/rewind-start.png"),
Chris@0 1057 tr("Rewind to Start"));
Chris@0 1058 action->setShortcut(tr("Home"));
Chris@0 1059 action->setStatusTip(tr("Rewind to the start"));
Chris@0 1060 connect(action, SIGNAL(triggered()), this, SLOT(rewindStart()));
Chris@0 1061 connect(this, SIGNAL(canPlay(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1062
Chris@0 1063 action = toolbar->addAction(QIcon(":/icons/rewind.png"),
Chris@0 1064 tr("Rewind"));
Chris@0 1065 action->setShortcut(tr("PageUp"));
Chris@0 1066 action->setStatusTip(tr("Rewind to the previous time instant in the current layer"));
Chris@0 1067 connect(action, SIGNAL(triggered()), this, SLOT(rewind()));
Chris@0 1068 connect(this, SIGNAL(canRewind(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1069
Chris@0 1070 action = toolbar->addAction(QIcon(":/icons/playpause.png"),
Chris@0 1071 tr("Play / Pause"));
Chris@0 1072 action->setCheckable(true);
Chris@0 1073 action->setShortcut(tr("Space"));
Chris@0 1074 action->setStatusTip(tr("Start or stop playback from the current position"));
Chris@0 1075 connect(action, SIGNAL(triggered()), this, SLOT(play()));
Chris@0 1076 connect(m_playSource, SIGNAL(playStatusChanged(bool)),
Chris@0 1077 action, SLOT(setChecked(bool)));
Chris@0 1078 connect(this, SIGNAL(canPlay(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1079
Chris@0 1080 action = toolbar->addAction(QIcon(":/icons/ffwd.png"),
Chris@0 1081 tr("Fast Forward"));
Chris@0 1082 action->setShortcut(tr("PageDown"));
Chris@0 1083 action->setStatusTip(tr("Fast forward to the next time instant in the current layer"));
Chris@0 1084 connect(action, SIGNAL(triggered()), this, SLOT(ffwd()));
Chris@0 1085 connect(this, SIGNAL(canFfwd(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1086
Chris@0 1087 action = toolbar->addAction(QIcon(":/icons/ffwd-end.png"),
Chris@0 1088 tr("Fast Forward to End"));
Chris@0 1089 action->setShortcut(tr("End"));
Chris@0 1090 action->setStatusTip(tr("Fast-forward to the end"));
Chris@0 1091 connect(action, SIGNAL(triggered()), this, SLOT(ffwdEnd()));
Chris@0 1092 connect(this, SIGNAL(canPlay(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1093
Chris@0 1094 toolbar = addToolBar(tr("Play Mode Toolbar"));
Chris@0 1095
Chris@0 1096 action = toolbar->addAction(QIcon(":/icons/playselection.png"),
Chris@0 1097 tr("Constrain Playback to Selection"));
Chris@0 1098 action->setCheckable(true);
Chris@0 1099 action->setChecked(m_viewManager->getPlaySelectionMode());
Chris@0 1100 action->setShortcut(tr("s"));
Chris@0 1101 action->setStatusTip(tr("Constrain playback to the selected area"));
Chris@0 1102 connect(action, SIGNAL(triggered()), this, SLOT(playSelectionToggled()));
Chris@0 1103 connect(this, SIGNAL(canPlaySelection(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1104
Chris@0 1105 action = toolbar->addAction(QIcon(":/icons/playloop.png"),
Chris@0 1106 tr("Loop Playback"));
Chris@0 1107 action->setCheckable(true);
Chris@0 1108 action->setChecked(m_viewManager->getPlayLoopMode());
Chris@0 1109 action->setShortcut(tr("l"));
Chris@0 1110 action->setStatusTip(tr("Loop playback"));
Chris@0 1111 connect(action, SIGNAL(triggered()), this, SLOT(playLoopToggled()));
Chris@0 1112 connect(this, SIGNAL(canPlay(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1113
Chris@0 1114 toolbar = addToolBar(tr("Edit Toolbar"));
Chris@0 1115 CommandHistory::getInstance()->registerToolbar(toolbar);
Chris@0 1116
Chris@0 1117 toolbar = addToolBar(tr("Tools Toolbar"));
Chris@0 1118 QActionGroup *group = new QActionGroup(this);
Chris@0 1119
Chris@0 1120 action = toolbar->addAction(QIcon(":/icons/navigate.png"),
Chris@0 1121 tr("Navigate"));
Chris@0 1122 action->setCheckable(true);
Chris@0 1123 action->setChecked(true);
Chris@0 1124 action->setShortcut(tr("1"));
Chris@0 1125 connect(action, SIGNAL(triggered()), this, SLOT(toolNavigateSelected()));
Chris@0 1126 group->addAction(action);
Chris@0 1127 m_toolActions[ViewManager::NavigateMode] = action;
Chris@0 1128
Chris@0 1129 action = toolbar->addAction(QIcon(":/icons/select.png"),
Chris@0 1130 tr("Select"));
Chris@0 1131 action->setCheckable(true);
Chris@0 1132 action->setShortcut(tr("2"));
Chris@0 1133 connect(action, SIGNAL(triggered()), this, SLOT(toolSelectSelected()));
Chris@0 1134 group->addAction(action);
Chris@0 1135 m_toolActions[ViewManager::SelectMode] = action;
Chris@0 1136
Chris@0 1137 action = toolbar->addAction(QIcon(":/icons/move.png"),
Chris@0 1138 tr("Edit"));
Chris@0 1139 action->setCheckable(true);
Chris@0 1140 action->setShortcut(tr("3"));
Chris@0 1141 connect(action, SIGNAL(triggered()), this, SLOT(toolEditSelected()));
Chris@0 1142 connect(this, SIGNAL(canEditLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1143 group->addAction(action);
Chris@0 1144 m_toolActions[ViewManager::EditMode] = action;
Chris@0 1145
Chris@0 1146 action = toolbar->addAction(QIcon(":/icons/draw.png"),
Chris@0 1147 tr("Draw"));
Chris@0 1148 action->setCheckable(true);
Chris@0 1149 action->setShortcut(tr("4"));
Chris@0 1150 connect(action, SIGNAL(triggered()), this, SLOT(toolDrawSelected()));
Chris@0 1151 connect(this, SIGNAL(canEditLayer(bool)), action, SLOT(setEnabled(bool)));
Chris@0 1152 group->addAction(action);
Chris@0 1153 m_toolActions[ViewManager::DrawMode] = action;
Chris@0 1154
Chris@0 1155 // action = toolbar->addAction(QIcon(":/icons/text.png"),
Chris@0 1156 // tr("Text"));
Chris@0 1157 // action->setCheckable(true);
Chris@0 1158 // action->setShortcut(tr("5"));
Chris@0 1159 // connect(action, SIGNAL(triggered()), this, SLOT(toolTextSelected()));
Chris@0 1160 // group->addAction(action);
Chris@0 1161 // m_toolActions[ViewManager::TextMode] = action;
Chris@0 1162
Chris@0 1163 toolNavigateSelected();
Chris@0 1164 }
Chris@0 1165
Chris@0 1166 void
Chris@0 1167 MainWindow::updateMenuStates()
Chris@0 1168 {
Chris@0 1169 bool haveCurrentPane =
Chris@0 1170 (m_paneStack &&
Chris@0 1171 (m_paneStack->getCurrentPane() != 0));
Chris@0 1172 bool haveCurrentLayer =
Chris@0 1173 (haveCurrentPane &&
Chris@0 1174 (m_paneStack->getCurrentPane()->getSelectedLayer()));
Chris@0 1175 bool haveMainModel =
Chris@0 1176 (getMainModel() != 0);
Chris@0 1177 bool havePlayTarget =
Chris@0 1178 (m_playTarget != 0);
Chris@0 1179 bool haveSelection =
Chris@0 1180 (m_viewManager &&
Chris@0 1181 !m_viewManager->getSelections().empty());
Chris@0 1182 bool haveCurrentEditableLayer =
Chris@0 1183 (haveCurrentLayer &&
Chris@0 1184 m_paneStack->getCurrentPane()->getSelectedLayer()->
Chris@0 1185 isLayerEditable());
Chris@0 1186 bool haveCurrentTimeInstantsLayer =
Chris@0 1187 (haveCurrentLayer &&
Chris@0 1188 dynamic_cast<TimeInstantLayer *>
Chris@0 1189 (m_paneStack->getCurrentPane()->getSelectedLayer()));
Chris@0 1190 bool haveCurrentTimeValueLayer =
Chris@0 1191 (haveCurrentLayer &&
Chris@0 1192 dynamic_cast<TimeValueLayer *>
Chris@0 1193 (m_paneStack->getCurrentPane()->getSelectedLayer()));
Chris@0 1194 bool haveCurrentColour3DPlot =
Chris@0 1195 (haveCurrentLayer &&
Chris@0 1196 dynamic_cast<Colour3DPlotLayer *>
Chris@0 1197 (m_paneStack->getCurrentPane()->getSelectedLayer()));
Chris@0 1198 bool haveClipboardContents =
Chris@0 1199 (m_viewManager &&
Chris@0 1200 !m_viewManager->getClipboard().empty());
Chris@0 1201
Chris@0 1202 emit canAddPane(haveMainModel);
Chris@0 1203 emit canDeleteCurrentPane(haveCurrentPane);
Chris@0 1204 emit canZoom(haveMainModel && haveCurrentPane);
Chris@0 1205 emit canScroll(haveMainModel && haveCurrentPane);
Chris@0 1206 emit canAddLayer(haveMainModel && haveCurrentPane);
Chris@0 1207 emit canImportMoreAudio(haveMainModel);
Chris@0 1208 emit canImportLayer(haveMainModel && haveCurrentPane);
Chris@0 1209 emit canExportAudio(haveMainModel);
Chris@0 1210 emit canExportLayer(haveMainModel &&
Chris@0 1211 (haveCurrentEditableLayer || haveCurrentColour3DPlot));
Chris@0 1212 emit canDeleteCurrentLayer(haveCurrentLayer);
Chris@0 1213 emit canRenameLayer(haveCurrentLayer);
Chris@0 1214 emit canEditLayer(haveCurrentEditableLayer);
Chris@0 1215 emit canSelect(haveMainModel && haveCurrentPane);
Chris@0 1216 emit canPlay(/*!!! haveMainModel && */ havePlayTarget);
Chris@0 1217 emit canFfwd(haveCurrentTimeInstantsLayer || haveCurrentTimeValueLayer);
Chris@0 1218 emit canRewind(haveCurrentTimeInstantsLayer || haveCurrentTimeValueLayer);
Chris@0 1219 emit canPaste(haveCurrentEditableLayer && haveClipboardContents);
Chris@0 1220 emit canInsertInstant(haveCurrentPane);
Chris@0 1221 emit canPlaySelection(haveMainModel && havePlayTarget && haveSelection);
Chris@0 1222 emit canClearSelection(haveSelection);
Chris@0 1223 emit canEditSelection(haveSelection && haveCurrentEditableLayer);
Chris@0 1224 emit canSave(m_sessionFile != "" && m_documentModified);
Chris@0 1225 }
Chris@0 1226
Chris@0 1227 void
Chris@0 1228 MainWindow::updateDescriptionLabel()
Chris@0 1229 {
Chris@0 1230 if (!getMainModel()) {
Chris@0 1231 m_descriptionLabel->setText(tr("No audio file loaded."));
Chris@0 1232 return;
Chris@0 1233 }
Chris@0 1234
Chris@0 1235 QString description;
Chris@0 1236
Chris@0 1237 size_t ssr = getMainModel()->getSampleRate();
Chris@0 1238 size_t tsr = ssr;
Chris@0 1239 if (m_playSource) tsr = m_playSource->getTargetSampleRate();
Chris@0 1240
Chris@0 1241 if (ssr != tsr) {
Chris@0 1242 description = tr("%1Hz (resampling to %2Hz)").arg(ssr).arg(tsr);
Chris@0 1243 } else {
Chris@0 1244 description = QString("%1Hz").arg(ssr);
Chris@0 1245 }
Chris@0 1246
Chris@0 1247 description = QString("%1 - %2")
Chris@0 1248 .arg(RealTime::frame2RealTime(getMainModel()->getEndFrame(), ssr)
Chris@0 1249 .toText(false).c_str())
Chris@0 1250 .arg(description);
Chris@0 1251
Chris@0 1252 m_descriptionLabel->setText(description);
Chris@0 1253 }
Chris@0 1254
Chris@0 1255 void
Chris@0 1256 MainWindow::documentModified()
Chris@0 1257 {
Chris@0 1258 // std::cerr << "MainWindow::documentModified" << std::endl;
Chris@0 1259
Chris@0 1260 if (!m_documentModified) {
Chris@0 1261 setWindowTitle(tr("%1 (modified)").arg(windowTitle()));
Chris@0 1262 }
Chris@0 1263
Chris@0 1264 m_documentModified = true;
Chris@0 1265 updateMenuStates();
Chris@0 1266 }
Chris@0 1267
Chris@0 1268 void
Chris@0 1269 MainWindow::documentRestored()
Chris@0 1270 {
Chris@0 1271 // std::cerr << "MainWindow::documentRestored" << std::endl;
Chris@0 1272
Chris@0 1273 if (m_documentModified) {
Chris@0 1274 QString wt(windowTitle());
Chris@0 1275 wt.replace(tr(" (modified)"), "");
Chris@0 1276 setWindowTitle(wt);
Chris@0 1277 }
Chris@0 1278
Chris@0 1279 m_documentModified = false;
Chris@0 1280 updateMenuStates();
Chris@0 1281 }
Chris@0 1282
Chris@0 1283 void
Chris@0 1284 MainWindow::playLoopToggled()
Chris@0 1285 {
Chris@0 1286 QAction *action = dynamic_cast<QAction *>(sender());
Chris@0 1287
Chris@0 1288 if (action) {
Chris@0 1289 m_viewManager->setPlayLoopMode(action->isChecked());
Chris@0 1290 } else {
Chris@0 1291 m_viewManager->setPlayLoopMode(!m_viewManager->getPlayLoopMode());
Chris@0 1292 }
Chris@0 1293 }
Chris@0 1294
Chris@0 1295 void
Chris@0 1296 MainWindow::playSelectionToggled()
Chris@0 1297 {
Chris@0 1298 QAction *action = dynamic_cast<QAction *>(sender());
Chris@0 1299
Chris@0 1300 if (action) {
Chris@0 1301 m_viewManager->setPlaySelectionMode(action->isChecked());
Chris@0 1302 } else {
Chris@0 1303 m_viewManager->setPlaySelectionMode(!m_viewManager->getPlaySelectionMode());
Chris@0 1304 }
Chris@0 1305 }
Chris@0 1306
Chris@0 1307 void
Chris@0 1308 MainWindow::currentPaneChanged(Pane *)
Chris@0 1309 {
Chris@0 1310 updateMenuStates();
Chris@0 1311 }
Chris@0 1312
Chris@0 1313 void
Chris@0 1314 MainWindow::currentLayerChanged(Pane *, Layer *)
Chris@0 1315 {
Chris@0 1316 updateMenuStates();
Chris@0 1317 }
Chris@0 1318
Chris@0 1319 void
Chris@0 1320 MainWindow::toolNavigateSelected()
Chris@0 1321 {
Chris@0 1322 m_viewManager->setToolMode(ViewManager::NavigateMode);
Chris@0 1323 }
Chris@0 1324
Chris@0 1325 void
Chris@0 1326 MainWindow::toolSelectSelected()
Chris@0 1327 {
Chris@0 1328 m_viewManager->setToolMode(ViewManager::SelectMode);
Chris@0 1329 }
Chris@0 1330
Chris@0 1331 void
Chris@0 1332 MainWindow::toolEditSelected()
Chris@0 1333 {
Chris@0 1334 m_viewManager->setToolMode(ViewManager::EditMode);
Chris@0 1335 }
Chris@0 1336
Chris@0 1337 void
Chris@0 1338 MainWindow::toolDrawSelected()
Chris@0 1339 {
Chris@0 1340 m_viewManager->setToolMode(ViewManager::DrawMode);
Chris@0 1341 }
Chris@0 1342
Chris@0 1343 //void
Chris@0 1344 //MainWindow::toolTextSelected()
Chris@0 1345 //{
Chris@0 1346 // m_viewManager->setToolMode(ViewManager::TextMode);
Chris@0 1347 //}
Chris@0 1348
Chris@0 1349 void
Chris@0 1350 MainWindow::selectAll()
Chris@0 1351 {
Chris@0 1352 if (!getMainModel()) return;
Chris@0 1353 m_viewManager->setSelection(Selection(getMainModel()->getStartFrame(),
Chris@0 1354 getMainModel()->getEndFrame()));
Chris@0 1355 }
Chris@0 1356
Chris@0 1357 void
Chris@0 1358 MainWindow::selectToStart()
Chris@0 1359 {
Chris@0 1360 if (!getMainModel()) return;
Chris@0 1361 m_viewManager->setSelection(Selection(getMainModel()->getStartFrame(),
Chris@0 1362 m_viewManager->getGlobalCentreFrame()));
Chris@0 1363 }
Chris@0 1364
Chris@0 1365 void
Chris@0 1366 MainWindow::selectToEnd()
Chris@0 1367 {
Chris@0 1368 if (!getMainModel()) return;
Chris@0 1369 m_viewManager->setSelection(Selection(m_viewManager->getGlobalCentreFrame(),
Chris@0 1370 getMainModel()->getEndFrame()));
Chris@0 1371 }
Chris@0 1372
Chris@0 1373 void
Chris@0 1374 MainWindow::selectVisible()
Chris@0 1375 {
Chris@0 1376 Model *model = getMainModel();
Chris@0 1377 if (!model) return;
Chris@0 1378
Chris@0 1379 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 1380 if (!currentPane) return;
Chris@0 1381
Chris@0 1382 size_t startFrame, endFrame;
Chris@0 1383
Chris@0 1384 if (currentPane->getStartFrame() < 0) startFrame = 0;
Chris@0 1385 else startFrame = currentPane->getStartFrame();
Chris@0 1386
Chris@0 1387 if (currentPane->getEndFrame() > model->getEndFrame()) endFrame = model->getEndFrame();
Chris@0 1388 else endFrame = currentPane->getEndFrame();
Chris@0 1389
Chris@0 1390 m_viewManager->setSelection(Selection(startFrame, endFrame));
Chris@0 1391 }
Chris@0 1392
Chris@0 1393 void
Chris@0 1394 MainWindow::clearSelection()
Chris@0 1395 {
Chris@0 1396 m_viewManager->clearSelections();
Chris@0 1397 }
Chris@0 1398
Chris@0 1399 void
Chris@0 1400 MainWindow::cut()
Chris@0 1401 {
Chris@0 1402 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 1403 if (!currentPane) return;
Chris@0 1404
Chris@0 1405 Layer *layer = currentPane->getSelectedLayer();
Chris@0 1406 if (!layer) return;
Chris@0 1407
Chris@0 1408 Clipboard &clipboard = m_viewManager->getClipboard();
Chris@0 1409 clipboard.clear();
Chris@0 1410
Chris@0 1411 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@0 1412
Chris@0 1413 CommandHistory::getInstance()->startCompoundOperation(tr("Cut"), true);
Chris@0 1414
Chris@0 1415 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@0 1416 i != selections.end(); ++i) {
Chris@0 1417 layer->copy(*i, clipboard);
Chris@0 1418 layer->deleteSelection(*i);
Chris@0 1419 }
Chris@0 1420
Chris@0 1421 CommandHistory::getInstance()->endCompoundOperation();
Chris@0 1422 }
Chris@0 1423
Chris@0 1424 void
Chris@0 1425 MainWindow::copy()
Chris@0 1426 {
Chris@0 1427 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 1428 if (!currentPane) return;
Chris@0 1429
Chris@0 1430 Layer *layer = currentPane->getSelectedLayer();
Chris@0 1431 if (!layer) return;
Chris@0 1432
Chris@0 1433 Clipboard &clipboard = m_viewManager->getClipboard();
Chris@0 1434 clipboard.clear();
Chris@0 1435
Chris@0 1436 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@0 1437
Chris@0 1438 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@0 1439 i != selections.end(); ++i) {
Chris@0 1440 layer->copy(*i, clipboard);
Chris@0 1441 }
Chris@0 1442 }
Chris@0 1443
Chris@0 1444 void
Chris@0 1445 MainWindow::paste()
Chris@0 1446 {
Chris@0 1447 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 1448 if (!currentPane) return;
Chris@0 1449
Chris@0 1450 //!!! if we have no current layer, we should create one of the most
Chris@0 1451 // appropriate type
Chris@0 1452
Chris@0 1453 Layer *layer = currentPane->getSelectedLayer();
Chris@0 1454 if (!layer) return;
Chris@0 1455
Chris@0 1456 Clipboard &clipboard = m_viewManager->getClipboard();
Chris@0 1457 Clipboard::PointList contents = clipboard.getPoints();
Chris@0 1458 /*
Chris@0 1459 long minFrame = 0;
Chris@0 1460 bool have = false;
Chris@0 1461 for (int i = 0; i < contents.size(); ++i) {
Chris@0 1462 if (!contents[i].haveFrame()) continue;
Chris@0 1463 if (!have || contents[i].getFrame() < minFrame) {
Chris@0 1464 minFrame = contents[i].getFrame();
Chris@0 1465 have = true;
Chris@0 1466 }
Chris@0 1467 }
Chris@0 1468
Chris@0 1469 long frameOffset = long(m_viewManager->getGlobalCentreFrame()) - minFrame;
Chris@0 1470
Chris@0 1471 layer->paste(clipboard, frameOffset);
Chris@0 1472 */
Chris@0 1473 layer->paste(clipboard, 0, true);
Chris@0 1474 }
Chris@0 1475
Chris@0 1476 void
Chris@0 1477 MainWindow::deleteSelected()
Chris@0 1478 {
Chris@0 1479 if (m_paneStack->getCurrentPane() &&
Chris@0 1480 m_paneStack->getCurrentPane()->getSelectedLayer()) {
Chris@0 1481
Chris@0 1482 MultiSelection::SelectionList selections =
Chris@0 1483 m_viewManager->getSelections();
Chris@0 1484
Chris@0 1485 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@0 1486 i != selections.end(); ++i) {
Chris@0 1487
Chris@0 1488 m_paneStack->getCurrentPane()->getSelectedLayer()->deleteSelection(*i);
Chris@0 1489 }
Chris@0 1490 }
Chris@0 1491 }
Chris@0 1492
Chris@0 1493 void
Chris@0 1494 MainWindow::insertInstant()
Chris@0 1495 {
Chris@0 1496 int frame = m_viewManager->getPlaybackFrame();
Chris@0 1497
Chris@0 1498 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 1499 if (!pane) {
Chris@0 1500 return;
Chris@0 1501 }
Chris@0 1502
Chris@0 1503 Layer *layer = dynamic_cast<TimeInstantLayer *>
Chris@0 1504 (pane->getSelectedLayer());
Chris@0 1505
Chris@0 1506 if (!layer) {
Chris@0 1507 for (int i = pane->getLayerCount(); i > 0; --i) {
Chris@0 1508 layer = dynamic_cast<TimeInstantLayer *>(pane->getLayer(i - 1));
Chris@0 1509 if (layer) break;
Chris@0 1510 }
Chris@0 1511
Chris@0 1512 if (!layer) {
Chris@0 1513 CommandHistory::getInstance()->startCompoundOperation
Chris@0 1514 (tr("Add Point"), true);
Chris@0 1515 layer = m_document->createEmptyLayer(LayerFactory::TimeInstants);
Chris@0 1516 if (layer) {
Chris@0 1517 m_document->addLayerToView(pane, layer);
Chris@0 1518 m_paneStack->setCurrentLayer(pane, layer);
Chris@0 1519 }
Chris@0 1520 CommandHistory::getInstance()->endCompoundOperation();
Chris@0 1521 }
Chris@0 1522 }
Chris@0 1523
Chris@0 1524 if (layer) {
Chris@0 1525
Chris@0 1526 Model *model = layer->getModel();
Chris@0 1527 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
Chris@0 1528 (model);
Chris@0 1529
Chris@0 1530 if (sodm) {
Chris@0 1531 SparseOneDimensionalModel::Point point
Chris@0 1532 (frame, QString("%1").arg(sodm->getPointCount() + 1));
Chris@0 1533 CommandHistory::getInstance()->addCommand
Chris@0 1534 (new SparseOneDimensionalModel::AddPointCommand(sodm, point,
Chris@0 1535 tr("Add Points")),
Chris@0 1536 true, true); // bundled
Chris@0 1537 }
Chris@0 1538 }
Chris@0 1539 }
Chris@0 1540
Chris@0 1541 void
Chris@0 1542 MainWindow::importAudio()
Chris@0 1543 {
Chris@0 1544 QString orig = m_audioFile;
Chris@0 1545
Chris@0 1546 // std::cerr << "orig = " << orig.toStdString() << std::endl;
Chris@0 1547
Chris@0 1548 if (orig == "") orig = ".";
Chris@0 1549
Chris@0 1550 QString path = QFileDialog::getOpenFileName
Chris@0 1551 (this, tr("Select an audio file"), orig,
Chris@0 1552 tr("Audio files (%1)\nAll files (*.*)")
Chris@0 1553 .arg(AudioFileReaderFactory::getKnownExtensions()));
Chris@0 1554
Chris@0 1555 if (path != "") {
Chris@0 1556 if (!openAudioFile(path, ReplaceMainModel)) {
Chris@0 1557 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 1558 tr("Audio file \"%1\" could not be opened").arg(path));
Chris@0 1559 }
Chris@0 1560 }
Chris@0 1561 }
Chris@0 1562
Chris@0 1563 void
Chris@0 1564 MainWindow::importMoreAudio()
Chris@0 1565 {
Chris@0 1566 QString orig = m_audioFile;
Chris@0 1567
Chris@0 1568 // std::cerr << "orig = " << orig.toStdString() << std::endl;
Chris@0 1569
Chris@0 1570 if (orig == "") orig = ".";
Chris@0 1571
Chris@0 1572 QString path = QFileDialog::getOpenFileName
Chris@0 1573 (this, tr("Select an audio file"), orig,
Chris@0 1574 tr("Audio files (%1)\nAll files (*.*)")
Chris@0 1575 .arg(AudioFileReaderFactory::getKnownExtensions()));
Chris@0 1576
Chris@0 1577 if (path != "") {
Chris@0 1578 if (!openAudioFile(path, CreateAdditionalModel)) {
Chris@0 1579 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 1580 tr("Audio file \"%1\" could not be opened").arg(path));
Chris@0 1581 }
Chris@0 1582 }
Chris@0 1583 }
Chris@0 1584
Chris@0 1585 void
Chris@0 1586 MainWindow::exportAudio()
Chris@0 1587 {
Chris@0 1588 if (!getMainModel()) return;
Chris@0 1589
Chris@0 1590 QString path = QFileDialog::getSaveFileName
Chris@0 1591 (this, tr("Select a file to export to"), ".",
Chris@0 1592 tr("WAV audio files (*.wav)\nAll files (*.*)"));
Chris@0 1593
Chris@0 1594 if (path == "") return;
Chris@0 1595
Chris@0 1596 if (!path.endsWith(".wav")) path = path + ".wav";
Chris@0 1597
Chris@0 1598 bool ok = false;
Chris@0 1599 QString error;
Chris@0 1600
Chris@0 1601 WavFileWriter *writer = 0;
Chris@0 1602 MultiSelection ms = m_viewManager->getSelection();
Chris@0 1603 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@0 1604
Chris@0 1605 bool multiple = false;
Chris@0 1606
Chris@0 1607 if (selections.empty()) {
Chris@0 1608
Chris@0 1609 writer = new WavFileWriter(path, getMainModel()->getSampleRate(),
Chris@0 1610 getMainModel(), 0);
Chris@0 1611
Chris@0 1612 } else if (selections.size() == 1) {
Chris@0 1613
Chris@0 1614 QStringList items;
Chris@0 1615 items << tr("Export the selected region only")
Chris@0 1616 << tr("Export the whole audio file");
Chris@0 1617
Chris@0 1618 bool ok = false;
Chris@0 1619 QString item = ListInputDialog::getItem
Chris@0 1620 (this, tr("Select region to export"),
Chris@0 1621 tr("Which region from the original audio file do you want to export?"),
Chris@0 1622 items, 0, &ok);
Chris@0 1623
Chris@0 1624 if (!ok || item.isEmpty()) return;
Chris@0 1625
Chris@0 1626 if (item == items[0]) {
Chris@0 1627
Chris@0 1628 writer = new WavFileWriter(path, getMainModel()->getSampleRate(),
Chris@0 1629 getMainModel(), &ms);
Chris@0 1630
Chris@0 1631 } else {
Chris@0 1632
Chris@0 1633 writer = new WavFileWriter(path, getMainModel()->getSampleRate(),
Chris@0 1634 getMainModel(), 0);
Chris@0 1635 }
Chris@0 1636 } else {
Chris@0 1637
Chris@0 1638 QStringList items;
Chris@0 1639 items << tr("Export the selected regions into a single audio file")
Chris@0 1640 << tr("Export the selected regions into separate files")
Chris@0 1641 << tr("Export the whole audio file");
Chris@0 1642
Chris@0 1643 bool ok = false;
Chris@0 1644 QString item = ListInputDialog::getItem
Chris@0 1645 (this, tr("Select region to export"),
Chris@0 1646 tr("Multiple regions of the original audio file are selected.\nWhat do you want to export?"),
Chris@0 1647 items, 0, &ok);
Chris@0 1648
Chris@0 1649 if (!ok || item.isEmpty()) return;
Chris@0 1650
Chris@0 1651 if (item == items[0]) {
Chris@0 1652
Chris@0 1653 writer = new WavFileWriter(path, getMainModel()->getSampleRate(),
Chris@0 1654 getMainModel(), &ms);
Chris@0 1655
Chris@0 1656 } else if (item == items[2]) {
Chris@0 1657
Chris@0 1658 writer = new WavFileWriter(path, getMainModel()->getSampleRate(),
Chris@0 1659 getMainModel(), 0);
Chris@0 1660
Chris@0 1661 } else {
Chris@0 1662
Chris@0 1663 multiple = true;
Chris@0 1664
Chris@0 1665 int n = 1;
Chris@0 1666 QString base = path;
Chris@0 1667 base.replace(".wav", "");
Chris@0 1668
Chris@0 1669 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@0 1670 i != selections.end(); ++i) {
Chris@0 1671
Chris@0 1672 MultiSelection subms;
Chris@0 1673 subms.setSelection(*i);
Chris@0 1674
Chris@0 1675 QString subpath = QString("%1.%2.wav").arg(base).arg(n);
Chris@0 1676 ++n;
Chris@0 1677
Chris@0 1678 if (QFileInfo(subpath).exists()) {
Chris@0 1679 error = tr("Fragment file %1 already exists, aborting").arg(subpath);
Chris@0 1680 break;
Chris@0 1681 }
Chris@0 1682
Chris@0 1683 WavFileWriter subwriter(subpath, getMainModel()->getSampleRate(),
Chris@0 1684 getMainModel(), &subms);
Chris@0 1685 subwriter.write();
Chris@0 1686 ok = subwriter.isOK();
Chris@0 1687
Chris@0 1688 if (!ok) {
Chris@0 1689 error = subwriter.getError();
Chris@0 1690 break;
Chris@0 1691 }
Chris@0 1692 }
Chris@0 1693 }
Chris@0 1694 }
Chris@0 1695
Chris@0 1696 if (writer) {
Chris@0 1697 writer->write();
Chris@0 1698 ok = writer->isOK();
Chris@0 1699 error = writer->getError();
Chris@0 1700 delete writer;
Chris@0 1701 }
Chris@0 1702
Chris@0 1703 if (ok) {
Chris@0 1704 if (!multiple) {
Chris@0 1705 RecentFiles::getInstance()->addFile(path);
Chris@0 1706 }
Chris@0 1707 } else {
Chris@0 1708 QMessageBox::critical(this, tr("Failed to write file"), error);
Chris@0 1709 }
Chris@0 1710 }
Chris@0 1711
Chris@0 1712 void
Chris@0 1713 MainWindow::importLayer()
Chris@0 1714 {
Chris@0 1715 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 1716
Chris@0 1717 if (!pane) {
Chris@0 1718 // shouldn't happen, as the menu action should have been disabled
Chris@0 1719 std::cerr << "WARNING: MainWindow::importLayer: no current pane" << std::endl;
Chris@0 1720 return;
Chris@0 1721 }
Chris@0 1722
Chris@0 1723 if (!getMainModel()) {
Chris@0 1724 // shouldn't happen, as the menu action should have been disabled
Chris@0 1725 std::cerr << "WARNING: MainWindow::importLayer: No main model -- hence no default sample rate available" << std::endl;
Chris@0 1726 return;
Chris@0 1727 }
Chris@0 1728
Chris@0 1729 QString path = QFileDialog::getOpenFileName
Chris@0 1730 (this, tr("Select file"), ".",
Chris@0 1731 tr("All supported files (%1)\nSonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nSpace-separated .lab files (*.lab)\nMIDI files (*.mid)\nText files (*.txt)\nAll files (*.*)").arg(DataFileReaderFactory::getKnownExtensions()));
Chris@0 1732
Chris@0 1733 if (path != "") {
Chris@0 1734
Chris@0 1735 if (!openLayerFile(path)) {
Chris@0 1736 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 1737 tr("File %1 could not be opened.").arg(path));
Chris@0 1738 return;
Chris@0 1739 }
Chris@0 1740 }
Chris@0 1741 }
Chris@0 1742
Chris@0 1743 bool
Chris@0 1744 MainWindow::openLayerFile(QString path)
Chris@0 1745 {
Chris@0 1746 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 1747
Chris@0 1748 if (!pane) {
Chris@0 1749 // shouldn't happen, as the menu action should have been disabled
Chris@0 1750 std::cerr << "WARNING: MainWindow::openLayerFile: no current pane" << std::endl;
Chris@0 1751 return false;
Chris@0 1752 }
Chris@0 1753
Chris@0 1754 if (!getMainModel()) {
Chris@0 1755 // shouldn't happen, as the menu action should have been disabled
Chris@0 1756 std::cerr << "WARNING: MainWindow::openLayerFile: No main model -- hence no default sample rate available" << std::endl;
Chris@0 1757 return false;
Chris@0 1758 }
Chris@0 1759
Chris@0 1760 if (path.endsWith(".svl") || path.endsWith(".xml")) {
Chris@0 1761
Chris@0 1762 PaneCallback callback(this);
Chris@0 1763 QFile file(path);
Chris@0 1764
Chris@0 1765 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Chris@0 1766 std::cerr << "ERROR: MainWindow::openLayerFile("
Chris@0 1767 << path.toStdString()
Chris@0 1768 << "): Failed to open file for reading" << std::endl;
Chris@0 1769 return false;
Chris@0 1770 }
Chris@0 1771
Chris@0 1772 SVFileReader reader(m_document, callback);
Chris@0 1773 reader.setCurrentPane(pane);
Chris@0 1774
Chris@0 1775 QXmlInputSource inputSource(&file);
Chris@0 1776 reader.parse(inputSource);
Chris@0 1777
Chris@0 1778 if (!reader.isOK()) {
Chris@0 1779 std::cerr << "ERROR: MainWindow::openLayerFile("
Chris@0 1780 << path.toStdString()
Chris@0 1781 << "): Failed to read XML file: "
Chris@0 1782 << reader.getErrorString().toStdString() << std::endl;
Chris@0 1783 return false;
Chris@0 1784 }
Chris@0 1785
Chris@0 1786 RecentFiles::getInstance()->addFile(path);
Chris@0 1787 return true;
Chris@0 1788
Chris@0 1789 } else {
Chris@0 1790
Chris@0 1791 Model *model = DataFileReaderFactory::load(path, getMainModel()->getSampleRate());
Chris@0 1792
Chris@0 1793 if (model) {
Chris@0 1794 Layer *newLayer = m_document->createImportedLayer(model);
Chris@0 1795 if (newLayer) {
Chris@0 1796 m_document->addLayerToView(pane, newLayer);
Chris@0 1797 RecentFiles::getInstance()->addFile(path);
Chris@0 1798 return true;
Chris@0 1799 }
Chris@0 1800 }
Chris@0 1801 }
Chris@0 1802
Chris@0 1803 return false;
Chris@0 1804 }
Chris@0 1805
Chris@0 1806 void
Chris@0 1807 MainWindow::exportLayer()
Chris@0 1808 {
Chris@0 1809 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 1810 if (!pane) return;
Chris@0 1811
Chris@0 1812 Layer *layer = pane->getSelectedLayer();
Chris@0 1813 if (!layer) return;
Chris@0 1814
Chris@0 1815 Model *model = layer->getModel();
Chris@0 1816 if (!model) return;
Chris@0 1817
Chris@0 1818 QString path = QFileDialog::getSaveFileName
Chris@0 1819 (this, tr("Select a file to export to"), ".",
Chris@0 1820 tr("Sonic Visualiser Layer XML files (*.svl)\nComma-separated data files (*.csv)\nText files (*.txt)\nAll files (*.*)"));
Chris@0 1821
Chris@0 1822 if (path == "") return;
Chris@0 1823
Chris@0 1824 if (QFileInfo(path).suffix() == "") path += ".svl";
Chris@0 1825
Chris@0 1826 QString error;
Chris@0 1827
Chris@0 1828 if (path.endsWith(".xml") || path.endsWith(".svl")) {
Chris@0 1829
Chris@0 1830 QFile file(path);
Chris@0 1831 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Chris@0 1832 error = tr("Failed to open file %1 for writing").arg(path);
Chris@0 1833 } else {
Chris@0 1834 QTextStream out(&file);
Chris@0 1835 out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Chris@0 1836 << "<!DOCTYPE sonic-visualiser>\n"
Chris@0 1837 << "<sv>\n"
Chris@0 1838 << " <data>\n";
Chris@0 1839
Chris@0 1840 model->toXml(out, " ");
Chris@0 1841
Chris@0 1842 out << " </data>\n"
Chris@0 1843 << " <display>\n";
Chris@0 1844
Chris@0 1845 layer->toXml(out, " ");
Chris@0 1846
Chris@0 1847 out << " </display>\n"
Chris@0 1848 << "</sv>\n";
Chris@0 1849 }
Chris@0 1850
Chris@0 1851 } else {
Chris@0 1852
Chris@0 1853 CSVFileWriter writer(path, model,
Chris@0 1854 (path.endsWith(".csv") ? "," : "\t"));
Chris@0 1855 writer.write();
Chris@0 1856
Chris@0 1857 if (!writer.isOK()) {
Chris@0 1858 error = writer.getError();
Chris@0 1859 }
Chris@0 1860 }
Chris@0 1861
Chris@0 1862 if (error != "") {
Chris@0 1863 QMessageBox::critical(this, tr("Failed to write file"), error);
Chris@0 1864 } else {
Chris@0 1865 RecentFiles::getInstance()->addFile(path);
Chris@0 1866 }
Chris@0 1867 }
Chris@0 1868
Chris@0 1869 bool
Chris@0 1870 MainWindow::openAudioFile(QString path, AudioFileOpenMode mode)
Chris@0 1871 {
Chris@0 1872 if (!(QFileInfo(path).exists() &&
Chris@0 1873 QFileInfo(path).isFile() &&
Chris@0 1874 QFileInfo(path).isReadable())) {
Chris@0 1875 return false;
Chris@0 1876 }
Chris@0 1877
Chris@0 1878 WaveFileModel *newModel = new WaveFileModel(path);
Chris@0 1879
Chris@0 1880 if (!newModel->isOK()) {
Chris@0 1881 delete newModel;
Chris@0 1882 return false;
Chris@0 1883 }
Chris@0 1884
Chris@0 1885 bool setAsMain = true;
Chris@0 1886 static bool prevSetAsMain = true;
Chris@0 1887
Chris@0 1888 if (mode == CreateAdditionalModel) setAsMain = false;
Chris@0 1889 else if (mode == AskUser) {
Chris@0 1890 if (m_document->getMainModel()) {
Chris@0 1891
Chris@0 1892 QStringList items;
Chris@0 1893 items << tr("Replace the existing main waveform")
Chris@0 1894 << tr("Load this file into a new waveform pane");
Chris@0 1895
Chris@0 1896 bool ok = false;
Chris@0 1897 QString item = ListInputDialog::getItem
Chris@0 1898 (this, tr("Select target for import"),
Chris@0 1899 tr("You already have an audio waveform loaded.\nWhat would you like to do with the new audio file?"),
Chris@0 1900 items, prevSetAsMain ? 0 : 1, &ok);
Chris@0 1901
Chris@0 1902 if (!ok || item.isEmpty()) {
Chris@0 1903 delete newModel;
Chris@0 1904 return false;
Chris@0 1905 }
Chris@0 1906
Chris@0 1907 setAsMain = (item == items[0]);
Chris@0 1908 prevSetAsMain = setAsMain;
Chris@0 1909 }
Chris@0 1910 }
Chris@0 1911
Chris@0 1912 if (setAsMain) {
Chris@0 1913
Chris@0 1914 Model *prevMain = getMainModel();
Chris@0 1915 if (prevMain) m_playSource->removeModel(prevMain);
Chris@0 1916
Chris@0 1917 PlayParameterRepository::getInstance()->clear();
Chris@0 1918
Chris@0 1919 // The clear() call will have removed the parameters for the
Chris@0 1920 // main model. Re-add them with the new one.
Chris@0 1921 PlayParameterRepository::getInstance()->addModel(newModel);
Chris@0 1922
Chris@0 1923 m_document->setMainModel(newModel);
Chris@0 1924 setupMenus();
Chris@0 1925
Chris@0 1926 if (m_sessionFile == "") {
Chris@0 1927 setWindowTitle(tr("Sonic Visualiser: %1")
Chris@0 1928 .arg(QFileInfo(path).fileName()));
Chris@0 1929 CommandHistory::getInstance()->clear();
Chris@0 1930 CommandHistory::getInstance()->documentSaved();
Chris@0 1931 m_documentModified = false;
Chris@0 1932 } else {
Chris@0 1933 setWindowTitle(tr("Sonic Visualiser: %1 [%2]")
Chris@0 1934 .arg(QFileInfo(m_sessionFile).fileName())
Chris@0 1935 .arg(QFileInfo(path).fileName()));
Chris@0 1936 if (m_documentModified) {
Chris@0 1937 m_documentModified = false;
Chris@0 1938 documentModified(); // so as to restore "(modified)" window title
Chris@0 1939 }
Chris@0 1940 }
Chris@0 1941
Chris@0 1942 m_audioFile = path;
Chris@0 1943
Chris@0 1944 } else { // !setAsMain
Chris@0 1945
Chris@0 1946 CommandHistory::getInstance()->startCompoundOperation
Chris@0 1947 (tr("Import \"%1\"").arg(QFileInfo(path).fileName()), true);
Chris@0 1948
Chris@0 1949 m_document->addImportedModel(newModel);
Chris@0 1950
Chris@0 1951 AddPaneCommand *command = new AddPaneCommand(this);
Chris@0 1952 CommandHistory::getInstance()->addCommand(command);
Chris@0 1953
Chris@0 1954 Pane *pane = command->getPane();
Chris@0 1955
Chris@0 1956 if (!m_timeRulerLayer) {
Chris@0 1957 m_timeRulerLayer = m_document->createMainModelLayer
Chris@0 1958 (LayerFactory::TimeRuler);
Chris@0 1959 }
Chris@0 1960
Chris@0 1961 m_document->addLayerToView(pane, m_timeRulerLayer);
Chris@0 1962
Chris@0 1963 Layer *newLayer = m_document->createImportedLayer(newModel);
Chris@0 1964
Chris@0 1965 if (newLayer) {
Chris@0 1966 m_document->addLayerToView(pane, newLayer);
Chris@0 1967 }
Chris@0 1968
Chris@0 1969 CommandHistory::getInstance()->endCompoundOperation();
Chris@0 1970 }
Chris@0 1971
Chris@0 1972 updateMenuStates();
Chris@0 1973 RecentFiles::getInstance()->addFile(path);
Chris@0 1974
Chris@0 1975 return true;
Chris@0 1976 }
Chris@0 1977
Chris@0 1978 void
Chris@0 1979 MainWindow::createPlayTarget()
Chris@0 1980 {
Chris@0 1981 if (m_playTarget) return;
Chris@0 1982
Chris@0 1983 m_playTarget = AudioTargetFactory::createCallbackTarget(m_playSource);
Chris@0 1984 if (!m_playTarget) {
Chris@0 1985 QMessageBox::warning
Chris@0 1986 (this, tr("Couldn't open audio device"),
Chris@0 1987 tr("Could not open an audio device for playback.\nAudio playback will not be available during this session.\n"),
Chris@0 1988 QMessageBox::Ok, 0);
Chris@0 1989 }
Chris@0 1990 connect(m_fader, SIGNAL(valueChanged(float)),
Chris@0 1991 m_playTarget, SLOT(setOutputGain(float)));
Chris@0 1992 }
Chris@0 1993
Chris@0 1994 WaveFileModel *
Chris@0 1995 MainWindow::getMainModel()
Chris@0 1996 {
Chris@0 1997 if (!m_document) return 0;
Chris@0 1998 return m_document->getMainModel();
Chris@0 1999 }
Chris@0 2000
Chris@0 2001 void
Chris@0 2002 MainWindow::newSession()
Chris@0 2003 {
Chris@0 2004 if (!checkSaveModified()) return;
Chris@0 2005
Chris@0 2006 closeSession();
Chris@0 2007 createDocument();
Chris@0 2008
Chris@0 2009 Pane *pane = m_paneStack->addPane();
Chris@0 2010
Chris@0 2011 if (!m_timeRulerLayer) {
Chris@0 2012 m_timeRulerLayer = m_document->createMainModelLayer
Chris@0 2013 (LayerFactory::TimeRuler);
Chris@0 2014 }
Chris@0 2015
Chris@0 2016 m_document->addLayerToView(pane, m_timeRulerLayer);
Chris@0 2017
Chris@0 2018 Layer *waveform = m_document->createMainModelLayer(LayerFactory::Waveform);
Chris@0 2019 m_document->addLayerToView(pane, waveform);
Chris@0 2020
Chris@0 2021 m_panner->registerView(pane);
Chris@0 2022
Chris@0 2023 CommandHistory::getInstance()->clear();
Chris@0 2024 CommandHistory::getInstance()->documentSaved();
Chris@0 2025 documentRestored();
Chris@0 2026 updateMenuStates();
Chris@0 2027 }
Chris@0 2028
Chris@0 2029 void
Chris@0 2030 MainWindow::createDocument()
Chris@0 2031 {
Chris@0 2032 m_document = new Document;
Chris@0 2033
Chris@0 2034 connect(m_document, SIGNAL(layerAdded(Layer *)),
Chris@0 2035 this, SLOT(layerAdded(Layer *)));
Chris@0 2036 connect(m_document, SIGNAL(layerRemoved(Layer *)),
Chris@0 2037 this, SLOT(layerRemoved(Layer *)));
Chris@0 2038 connect(m_document, SIGNAL(layerAboutToBeDeleted(Layer *)),
Chris@0 2039 this, SLOT(layerAboutToBeDeleted(Layer *)));
Chris@0 2040 connect(m_document, SIGNAL(layerInAView(Layer *, bool)),
Chris@0 2041 this, SLOT(layerInAView(Layer *, bool)));
Chris@0 2042
Chris@0 2043 connect(m_document, SIGNAL(modelAdded(Model *)),
Chris@0 2044 this, SLOT(modelAdded(Model *)));
Chris@0 2045 connect(m_document, SIGNAL(mainModelChanged(WaveFileModel *)),
Chris@0 2046 this, SLOT(mainModelChanged(WaveFileModel *)));
Chris@0 2047 connect(m_document, SIGNAL(modelAboutToBeDeleted(Model *)),
Chris@0 2048 this, SLOT(modelAboutToBeDeleted(Model *)));
Chris@0 2049
Chris@0 2050 connect(m_document, SIGNAL(modelGenerationFailed(QString)),
Chris@0 2051 this, SLOT(modelGenerationFailed(QString)));
Chris@0 2052 connect(m_document, SIGNAL(modelRegenerationFailed(QString)),
Chris@0 2053 this, SLOT(modelRegenerationFailed(QString)));
Chris@0 2054 }
Chris@0 2055
Chris@0 2056 void
Chris@0 2057 MainWindow::closeSession()
Chris@0 2058 {
Chris@0 2059 if (!checkSaveModified()) return;
Chris@0 2060
Chris@0 2061 while (m_paneStack->getPaneCount() > 0) {
Chris@0 2062
Chris@0 2063 Pane *pane = m_paneStack->getPane(m_paneStack->getPaneCount() - 1);
Chris@0 2064
Chris@0 2065 while (pane->getLayerCount() > 0) {
Chris@0 2066 m_document->removeLayerFromView
Chris@0 2067 (pane, pane->getLayer(pane->getLayerCount() - 1));
Chris@0 2068 }
Chris@0 2069
Chris@0 2070 m_panner->unregisterView(pane);
Chris@0 2071 m_paneStack->deletePane(pane);
Chris@0 2072 }
Chris@0 2073
Chris@0 2074 while (m_paneStack->getHiddenPaneCount() > 0) {
Chris@0 2075
Chris@0 2076 Pane *pane = m_paneStack->getHiddenPane
Chris@0 2077 (m_paneStack->getHiddenPaneCount() - 1);
Chris@0 2078
Chris@0 2079 while (pane->getLayerCount() > 0) {
Chris@0 2080 m_document->removeLayerFromView
Chris@0 2081 (pane, pane->getLayer(pane->getLayerCount() - 1));
Chris@0 2082 }
Chris@0 2083
Chris@0 2084 m_panner->unregisterView(pane);
Chris@0 2085 m_paneStack->deletePane(pane);
Chris@0 2086 }
Chris@0 2087
Chris@0 2088 delete m_document;
Chris@0 2089 m_document = 0;
Chris@0 2090 m_viewManager->clearSelections();
Chris@0 2091 m_timeRulerLayer = 0; // document owned this
Chris@0 2092
Chris@0 2093 m_sessionFile = "";
Chris@0 2094 setWindowTitle(tr("Sonic Visualiser"));
Chris@0 2095
Chris@0 2096 CommandHistory::getInstance()->clear();
Chris@0 2097 CommandHistory::getInstance()->documentSaved();
Chris@0 2098 documentRestored();
Chris@0 2099 }
Chris@0 2100
Chris@0 2101 void
Chris@0 2102 MainWindow::openSession()
Chris@0 2103 {
Chris@0 2104 if (!checkSaveModified()) return;
Chris@0 2105
Chris@0 2106 QString orig = m_audioFile;
Chris@0 2107 if (orig == "") orig = ".";
Chris@0 2108 else orig = QFileInfo(orig).absoluteDir().canonicalPath();
Chris@0 2109
Chris@0 2110 QString path = QFileDialog::getOpenFileName
Chris@0 2111 (this, tr("Select a session file"), orig,
Chris@0 2112 tr("Sonic Visualiser session files (*.sv)\nAll files (*.*)"));
Chris@0 2113
Chris@0 2114 if (path.isEmpty()) return;
Chris@0 2115
Chris@0 2116 if (!(QFileInfo(path).exists() &&
Chris@0 2117 QFileInfo(path).isFile() &&
Chris@0 2118 QFileInfo(path).isReadable())) {
Chris@0 2119 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 2120 tr("File \"%1\" does not exist or is not a readable file").arg(path));
Chris@0 2121 return;
Chris@0 2122 }
Chris@0 2123
Chris@0 2124 if (!openSessionFile(path)) {
Chris@0 2125 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 2126 tr("Session file \"%1\" could not be opened").arg(path));
Chris@0 2127 }
Chris@0 2128 }
Chris@0 2129
Chris@0 2130 void
Chris@0 2131 MainWindow::openSomething()
Chris@0 2132 {
Chris@0 2133 QString orig = m_audioFile;
Chris@0 2134 if (orig == "") orig = ".";
Chris@0 2135 else orig = QFileInfo(orig).absoluteDir().canonicalPath();
Chris@0 2136
Chris@0 2137 bool canImportLayer = (getMainModel() != 0 &&
Chris@0 2138 m_paneStack != 0 &&
Chris@0 2139 m_paneStack->getCurrentPane() != 0);
Chris@0 2140
Chris@0 2141 QString importSpec;
Chris@0 2142
Chris@0 2143 if (canImportLayer) {
Chris@0 2144 importSpec = tr("All supported files (*.sv %1 %2)\nSonic Visualiser session files (*.sv)\nAudio files (%1)\nLayer files (%2)\nAll files (*.*)")
Chris@0 2145 .arg(AudioFileReaderFactory::getKnownExtensions())
Chris@0 2146 .arg(DataFileReaderFactory::getKnownExtensions());
Chris@0 2147 } else {
Chris@0 2148 importSpec = tr("All supported files (*.sv %1)\nSonic Visualiser session files (*.sv)\nAudio files (%1)\nAll files (*.*)")
Chris@0 2149 .arg(AudioFileReaderFactory::getKnownExtensions());
Chris@0 2150 }
Chris@0 2151
Chris@0 2152 QString path = QFileDialog::getOpenFileName
Chris@0 2153 (this, tr("Select a file to open"), orig, importSpec);
Chris@0 2154
Chris@0 2155 if (path.isEmpty()) return;
Chris@0 2156
Chris@0 2157 if (!(QFileInfo(path).exists() &&
Chris@0 2158 QFileInfo(path).isFile() &&
Chris@0 2159 QFileInfo(path).isReadable())) {
Chris@0 2160 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 2161 tr("File \"%1\" does not exist or is not a readable file").arg(path));
Chris@0 2162 return;
Chris@0 2163 }
Chris@0 2164
Chris@0 2165 if (path.endsWith(".sv")) {
Chris@0 2166
Chris@0 2167 if (!checkSaveModified()) return;
Chris@0 2168
Chris@0 2169 if (!openSessionFile(path)) {
Chris@0 2170 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 2171 tr("Session file \"%1\" could not be opened").arg(path));
Chris@0 2172 }
Chris@0 2173
Chris@0 2174 } else {
Chris@0 2175
Chris@0 2176 if (!openAudioFile(path, AskUser)) {
Chris@0 2177
Chris@0 2178 if (!canImportLayer || !openLayerFile(path)) {
Chris@0 2179
Chris@0 2180 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 2181 tr("File \"%1\" could not be opened").arg(path));
Chris@0 2182 }
Chris@0 2183 }
Chris@0 2184 }
Chris@0 2185 }
Chris@0 2186
Chris@0 2187 void
Chris@0 2188 MainWindow::openRecentFile()
Chris@0 2189 {
Chris@0 2190 QObject *obj = sender();
Chris@0 2191 QAction *action = dynamic_cast<QAction *>(obj);
Chris@0 2192
Chris@0 2193 if (!action) {
Chris@0 2194 std::cerr << "WARNING: MainWindow::openRecentFile: sender is not an action"
Chris@0 2195 << std::endl;
Chris@0 2196 return;
Chris@0 2197 }
Chris@0 2198
Chris@0 2199 QString path = action->text();
Chris@0 2200 if (path == "") return;
Chris@0 2201
Chris@0 2202 if (path.endsWith("sv")) {
Chris@0 2203
Chris@0 2204 if (!checkSaveModified()) return ;
Chris@0 2205
Chris@0 2206 if (!openSessionFile(path)) {
Chris@0 2207 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 2208 tr("Session file \"%1\" could not be opened").arg(path));
Chris@0 2209 }
Chris@0 2210
Chris@0 2211 } else {
Chris@0 2212
Chris@0 2213 if (!openAudioFile(path, AskUser)) {
Chris@0 2214
Chris@0 2215 bool canImportLayer = (getMainModel() != 0 &&
Chris@0 2216 m_paneStack != 0 &&
Chris@0 2217 m_paneStack->getCurrentPane() != 0);
Chris@0 2218
Chris@0 2219 if (!canImportLayer || !openLayerFile(path)) {
Chris@0 2220
Chris@0 2221 QMessageBox::critical(this, tr("Failed to open file"),
Chris@0 2222 tr("File \"%1\" could not be opened").arg(path));
Chris@0 2223 }
Chris@0 2224 }
Chris@0 2225 }
Chris@0 2226 }
Chris@0 2227
Chris@0 2228 bool
Chris@0 2229 MainWindow::openSomeFile(QString path)
Chris@0 2230 {
Chris@0 2231 if (openAudioFile(path)) {
Chris@0 2232 return true;
Chris@0 2233 } else if (openSessionFile(path)) {
Chris@0 2234 return true;
Chris@0 2235 } else {
Chris@0 2236 return false;
Chris@0 2237 }
Chris@0 2238 }
Chris@0 2239
Chris@0 2240 bool
Chris@0 2241 MainWindow::openSessionFile(QString path)
Chris@0 2242 {
Chris@0 2243 BZipFileDevice bzFile(path);
Chris@0 2244 if (!bzFile.open(QIODevice::ReadOnly)) {
Chris@0 2245 std::cerr << "Failed to open session file \"" << path.toStdString()
Chris@0 2246 << "\": " << bzFile.errorString().toStdString() << std::endl;
Chris@0 2247 return false;
Chris@0 2248 }
Chris@0 2249
Chris@0 2250 QString error;
Chris@0 2251 closeSession();
Chris@0 2252 createDocument();
Chris@0 2253
Chris@0 2254 PaneCallback callback(this);
Chris@0 2255 m_viewManager->clearSelections();
Chris@0 2256
Chris@0 2257 SVFileReader reader(m_document, callback);
Chris@0 2258 QXmlInputSource inputSource(&bzFile);
Chris@0 2259 reader.parse(inputSource);
Chris@0 2260
Chris@0 2261 if (!reader.isOK()) {
Chris@0 2262 error = tr("SV XML file read error:\n%1").arg(reader.getErrorString());
Chris@0 2263 }
Chris@0 2264
Chris@0 2265 bzFile.close();
Chris@0 2266
Chris@0 2267 bool ok = (error == "");
Chris@0 2268
Chris@0 2269 if (ok) {
Chris@0 2270 setWindowTitle(tr("Sonic Visualiser: %1")
Chris@0 2271 .arg(QFileInfo(path).fileName()));
Chris@0 2272 m_sessionFile = path;
Chris@0 2273 setupMenus();
Chris@0 2274 CommandHistory::getInstance()->clear();
Chris@0 2275 CommandHistory::getInstance()->documentSaved();
Chris@0 2276 m_documentModified = false;
Chris@0 2277 updateMenuStates();
Chris@0 2278 RecentFiles::getInstance()->addFile(path);
Chris@0 2279 } else {
Chris@0 2280 setWindowTitle(tr("Sonic Visualiser"));
Chris@0 2281 }
Chris@0 2282
Chris@0 2283 return ok;
Chris@0 2284 }
Chris@0 2285
Chris@0 2286 void
Chris@0 2287 MainWindow::closeEvent(QCloseEvent *e)
Chris@0 2288 {
Chris@0 2289 if (!checkSaveModified()) {
Chris@0 2290 e->ignore();
Chris@0 2291 return;
Chris@0 2292 }
Chris@0 2293
Chris@5 2294 QSettings settings;
Chris@5 2295 settings.beginGroup("MainWindow");
Chris@5 2296 settings.setValue("size", size());
Chris@5 2297 settings.setValue("position", pos());
Chris@5 2298 settings.endGroup();
Chris@5 2299
Chris@0 2300 e->accept();
Chris@0 2301 return;
Chris@0 2302 }
Chris@0 2303
Chris@0 2304 bool
Chris@11 2305 MainWindow::commitData(bool mayAskUser)
Chris@11 2306 {
Chris@11 2307 if (mayAskUser) {
Chris@11 2308 return checkSaveModified();
Chris@11 2309 } else {
Chris@11 2310 if (!m_documentModified) return true;
Chris@11 2311
Chris@11 2312 // If we can't check with the user first, then we can't save
Chris@11 2313 // to the original session file (even if we have it) -- have
Chris@11 2314 // to use a temporary file
Chris@11 2315
Chris@11 2316 QString svDirBase = ".sv1";
Chris@11 2317 QString svDir = QDir::home().filePath(svDirBase);
Chris@11 2318
Chris@11 2319 if (!QFileInfo(svDir).exists()) {
Chris@11 2320 if (!QDir::home().mkdir(svDirBase)) return false;
Chris@11 2321 } else {
Chris@11 2322 if (!QFileInfo(svDir).isDir()) return false;
Chris@11 2323 }
Chris@11 2324
Chris@11 2325 // This name doesn't have to be unguessable
Chris@11 2326
Chris@11 2327 QString fname = QString("tmp-%1-%2.sv")
Chris@11 2328 .arg(QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"))
Chris@11 2329 .arg(QProcess().pid());
Chris@11 2330 QString fpath = QDir(svDir).filePath(fname);
Chris@11 2331 if (saveSessionFile(fpath)) {
Chris@11 2332 RecentFiles::getInstance()->addFile(fpath);
Chris@11 2333 return true;
Chris@11 2334 } else {
Chris@11 2335 return false;
Chris@11 2336 }
Chris@11 2337 }
Chris@11 2338 }
Chris@11 2339
Chris@11 2340 bool
Chris@0 2341 MainWindow::checkSaveModified()
Chris@0 2342 {
Chris@0 2343 // Called before some destructive operation (e.g. new session,
Chris@0 2344 // exit program). Return true if we can safely proceed, false to
Chris@0 2345 // cancel.
Chris@0 2346
Chris@0 2347 if (!m_documentModified) return true;
Chris@0 2348
Chris@0 2349 int button =
Chris@0 2350 QMessageBox::warning(this,
Chris@0 2351 tr("Session modified"),
Chris@0 2352 tr("The current session has been modified.\nDo you want to save it?"),
Chris@0 2353 QMessageBox::Yes,
Chris@0 2354 QMessageBox::No,
Chris@0 2355 QMessageBox::Cancel);
Chris@0 2356
Chris@0 2357 if (button == QMessageBox::Yes) {
Chris@0 2358 saveSession();
Chris@0 2359 if (m_documentModified) { // save failed -- don't proceed!
Chris@0 2360 return false;
Chris@0 2361 } else {
Chris@0 2362 return true; // saved, so it's safe to continue now
Chris@0 2363 }
Chris@0 2364 } else if (button == QMessageBox::No) {
Chris@0 2365 m_documentModified = false; // so we know to abandon it
Chris@0 2366 return true;
Chris@0 2367 }
Chris@0 2368
Chris@0 2369 // else cancel
Chris@0 2370 return false;
Chris@0 2371 }
Chris@0 2372
Chris@0 2373 void
Chris@0 2374 MainWindow::saveSession()
Chris@0 2375 {
Chris@0 2376 if (m_sessionFile != "") {
Chris@0 2377 if (!saveSessionFile(m_sessionFile)) {
Chris@0 2378 QMessageBox::critical(this, tr("Failed to save file"),
Chris@0 2379 tr("Session file \"%1\" could not be saved.").arg(m_sessionFile));
Chris@0 2380 } else {
Chris@0 2381 CommandHistory::getInstance()->documentSaved();
Chris@0 2382 documentRestored();
Chris@0 2383 }
Chris@0 2384 } else {
Chris@0 2385 saveSessionAs();
Chris@0 2386 }
Chris@0 2387 }
Chris@0 2388
Chris@0 2389 void
Chris@0 2390 MainWindow::saveSessionAs()
Chris@0 2391 {
Chris@0 2392 QString orig = m_audioFile;
Chris@0 2393 if (orig == "") orig = ".";
Chris@0 2394 else orig = QFileInfo(orig).absoluteDir().canonicalPath();
Chris@0 2395
Chris@0 2396 QString path;
Chris@0 2397 bool good = false;
Chris@0 2398
Chris@0 2399 while (!good) {
Chris@0 2400
Chris@0 2401 path = QFileDialog::getSaveFileName
Chris@0 2402 (this, tr("Select a file to save to"), orig,
Chris@0 2403 tr("Sonic Visualiser session files (*.sv)\nAll files (*.*)"), 0,
Chris@0 2404 QFileDialog::DontConfirmOverwrite); // we'll do that
Chris@0 2405
Chris@0 2406 if (path.isEmpty()) return;
Chris@0 2407
Chris@0 2408 if (!path.endsWith(".sv")) path = path + ".sv";
Chris@0 2409
Chris@0 2410 QFileInfo fi(path);
Chris@0 2411
Chris@0 2412 if (fi.isDir()) {
Chris@0 2413 QMessageBox::critical(this, tr("Directory selected"),
Chris@0 2414 tr("File \"%1\" is a directory").arg(path));
Chris@0 2415 continue;
Chris@0 2416 }
Chris@0 2417
Chris@0 2418 if (fi.exists()) {
Chris@0 2419 if (QMessageBox::question(this, tr("File exists"),
Chris@0 2420 tr("The file \"%1\" already exists.\nDo you want to overwrite it?").arg(path),
Chris@0 2421 QMessageBox::Ok,
Chris@0 2422 QMessageBox::Cancel) == QMessageBox::Ok) {
Chris@0 2423 good = true;
Chris@0 2424 } else {
Chris@0 2425 continue;
Chris@0 2426 }
Chris@0 2427 }
Chris@0 2428
Chris@0 2429 good = true;
Chris@0 2430 }
Chris@0 2431
Chris@0 2432 if (!saveSessionFile(path)) {
Chris@0 2433 QMessageBox::critical(this, tr("Failed to save file"),
Chris@0 2434 tr("Session file \"%1\" could not be saved.").arg(m_sessionFile));
Chris@0 2435 } else {
Chris@0 2436 setWindowTitle(tr("Sonic Visualiser: %1")
Chris@0 2437 .arg(QFileInfo(path).fileName()));
Chris@0 2438 m_sessionFile = path;
Chris@0 2439 CommandHistory::getInstance()->documentSaved();
Chris@0 2440 documentRestored();
Chris@0 2441 RecentFiles::getInstance()->addFile(path);
Chris@0 2442 }
Chris@0 2443 }
Chris@0 2444
Chris@0 2445 bool
Chris@0 2446 MainWindow::saveSessionFile(QString path)
Chris@0 2447 {
Chris@0 2448 BZipFileDevice bzFile(path);
Chris@0 2449 if (!bzFile.open(QIODevice::WriteOnly)) {
Chris@0 2450 std::cerr << "Failed to open session file \"" << path.toStdString()
Chris@0 2451 << "\" for writing: "
Chris@0 2452 << bzFile.errorString().toStdString() << std::endl;
Chris@0 2453 return false;
Chris@0 2454 }
Chris@0 2455
Chris@0 2456 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
Chris@0 2457
Chris@0 2458 QTextStream out(&bzFile);
Chris@0 2459 toXml(out);
Chris@0 2460 out.flush();
Chris@0 2461
Chris@0 2462 QApplication::restoreOverrideCursor();
Chris@0 2463
Chris@0 2464 if (bzFile.errorString() != "") {
Chris@0 2465 QMessageBox::critical(this, tr("Failed to write file"),
Chris@0 2466 tr("Failed to write to file \"%1\": %2")
Chris@0 2467 .arg(path).arg(bzFile.errorString()));
Chris@0 2468 bzFile.close();
Chris@0 2469 return false;
Chris@0 2470 }
Chris@0 2471
Chris@0 2472 bzFile.close();
Chris@0 2473 return true;
Chris@0 2474 }
Chris@0 2475
Chris@0 2476 void
Chris@0 2477 MainWindow::toXml(QTextStream &out)
Chris@0 2478 {
Chris@0 2479 QString indent(" ");
Chris@0 2480
Chris@0 2481 out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
Chris@0 2482 out << "<!DOCTYPE sonic-visualiser>\n";
Chris@0 2483 out << "<sv>\n";
Chris@0 2484
Chris@0 2485 m_document->toXml(out, "", "");
Chris@0 2486
Chris@0 2487 out << "<display>\n";
Chris@0 2488
Chris@0 2489 out << QString(" <window width=\"%1\" height=\"%2\"/>\n")
Chris@0 2490 .arg(width()).arg(height());
Chris@0 2491
Chris@0 2492 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@0 2493
Chris@0 2494 Pane *pane = m_paneStack->getPane(i);
Chris@0 2495
Chris@0 2496 if (pane) {
Chris@0 2497 pane->toXml(out, indent);
Chris@0 2498 }
Chris@0 2499 }
Chris@0 2500
Chris@0 2501 out << "</display>\n";
Chris@0 2502
Chris@0 2503 m_viewManager->getSelection().toXml(out);
Chris@0 2504
Chris@0 2505 out << "</sv>\n";
Chris@0 2506 }
Chris@0 2507
Chris@0 2508 Pane *
Chris@0 2509 MainWindow::addPaneToStack()
Chris@0 2510 {
Chris@0 2511 AddPaneCommand *command = new AddPaneCommand(this);
Chris@0 2512 CommandHistory::getInstance()->addCommand(command);
Chris@0 2513 return command->getPane();
Chris@0 2514 }
Chris@0 2515
Chris@0 2516 void
Chris@0 2517 MainWindow::zoomIn()
Chris@0 2518 {
Chris@0 2519 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 2520 if (currentPane) currentPane->zoom(true);
Chris@0 2521 }
Chris@0 2522
Chris@0 2523 void
Chris@0 2524 MainWindow::zoomOut()
Chris@0 2525 {
Chris@0 2526 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 2527 if (currentPane) currentPane->zoom(false);
Chris@0 2528 }
Chris@0 2529
Chris@0 2530 void
Chris@0 2531 MainWindow::zoomToFit()
Chris@0 2532 {
Chris@0 2533 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 2534 if (!currentPane) return;
Chris@0 2535
Chris@0 2536 Model *model = getMainModel();
Chris@0 2537 if (!model) return;
Chris@0 2538
Chris@0 2539 size_t start = model->getStartFrame();
Chris@0 2540 size_t end = model->getEndFrame();
Chris@0 2541 size_t pixels = currentPane->width();
Chris@0 2542 size_t zoomLevel = (end - start) / pixels;
Chris@0 2543
Chris@0 2544 currentPane->setZoomLevel(zoomLevel);
Chris@0 2545 currentPane->setStartFrame(start);
Chris@0 2546 }
Chris@0 2547
Chris@0 2548 void
Chris@0 2549 MainWindow::zoomDefault()
Chris@0 2550 {
Chris@0 2551 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 2552 if (currentPane) currentPane->setZoomLevel(1024);
Chris@0 2553 }
Chris@0 2554
Chris@0 2555 void
Chris@0 2556 MainWindow::scrollLeft()
Chris@0 2557 {
Chris@0 2558 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 2559 if (currentPane) currentPane->scroll(false, false);
Chris@0 2560 }
Chris@0 2561
Chris@0 2562 void
Chris@0 2563 MainWindow::jumpLeft()
Chris@0 2564 {
Chris@0 2565 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 2566 if (currentPane) currentPane->scroll(false, true);
Chris@0 2567 }
Chris@0 2568
Chris@0 2569 void
Chris@0 2570 MainWindow::scrollRight()
Chris@0 2571 {
Chris@0 2572 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 2573 if (currentPane) currentPane->scroll(true, false);
Chris@0 2574 }
Chris@0 2575
Chris@0 2576 void
Chris@0 2577 MainWindow::jumpRight()
Chris@0 2578 {
Chris@0 2579 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@0 2580 if (currentPane) currentPane->scroll(true, true);
Chris@0 2581 }
Chris@0 2582
Chris@0 2583 void
Chris@0 2584 MainWindow::showNoOverlays()
Chris@0 2585 {
Chris@0 2586 m_viewManager->setOverlayMode(ViewManager::NoOverlays);
Chris@0 2587 }
Chris@0 2588
Chris@0 2589 void
Chris@0 2590 MainWindow::showBasicOverlays()
Chris@0 2591 {
Chris@0 2592 m_viewManager->setOverlayMode(ViewManager::BasicOverlays);
Chris@0 2593 }
Chris@0 2594
Chris@0 2595 void
Chris@7 2596 MainWindow::showAllTextOverlays()
Chris@0 2597 {
Chris@0 2598 m_viewManager->setOverlayMode(ViewManager::AllOverlays);
Chris@0 2599 }
Chris@0 2600
Chris@0 2601 void
Chris@7 2602 MainWindow::toggleZoomWheels()
Chris@7 2603 {
Chris@7 2604 if (m_viewManager->getZoomWheelsEnabled()) {
Chris@7 2605 m_viewManager->setZoomWheelsEnabled(false);
Chris@7 2606 } else {
Chris@7 2607 m_viewManager->setZoomWheelsEnabled(true);
Chris@7 2608 }
Chris@7 2609 }
Chris@7 2610
Chris@7 2611 void
Chris@0 2612 MainWindow::play()
Chris@0 2613 {
Chris@0 2614 if (m_playSource->isPlaying()) {
Chris@0 2615 m_playSource->stop();
Chris@0 2616 } else {
Chris@0 2617 m_playSource->play(m_viewManager->getPlaybackFrame());
Chris@0 2618 }
Chris@0 2619 }
Chris@0 2620
Chris@0 2621 void
Chris@0 2622 MainWindow::ffwd()
Chris@0 2623 {
Chris@0 2624 if (!getMainModel()) return;
Chris@0 2625
Chris@0 2626 int frame = m_viewManager->getPlaybackFrame();
Chris@0 2627 ++frame;
Chris@0 2628
Chris@0 2629 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 2630 if (!pane) return;
Chris@0 2631
Chris@0 2632 Layer *layer = pane->getSelectedLayer();
Chris@0 2633
Chris@0 2634 if (!dynamic_cast<TimeInstantLayer *>(layer) &&
Chris@0 2635 !dynamic_cast<TimeValueLayer *>(layer)) return;
Chris@0 2636
Chris@0 2637 size_t resolution = 0;
Chris@0 2638 if (!layer->snapToFeatureFrame(pane, frame, resolution, Layer::SnapRight)) {
Chris@0 2639 frame = getMainModel()->getEndFrame();
Chris@0 2640 }
Chris@0 2641
Chris@0 2642 m_viewManager->setPlaybackFrame(frame);
Chris@0 2643 }
Chris@0 2644
Chris@0 2645 void
Chris@0 2646 MainWindow::ffwdEnd()
Chris@0 2647 {
Chris@0 2648 if (!getMainModel()) return;
Chris@0 2649 m_viewManager->setPlaybackFrame(getMainModel()->getEndFrame());
Chris@0 2650 }
Chris@0 2651
Chris@0 2652 void
Chris@0 2653 MainWindow::rewind()
Chris@0 2654 {
Chris@0 2655 if (!getMainModel()) return;
Chris@0 2656
Chris@0 2657 int frame = m_viewManager->getPlaybackFrame();
Chris@0 2658 if (frame > 0) --frame;
Chris@0 2659
Chris@0 2660 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 2661 if (!pane) return;
Chris@0 2662
Chris@0 2663 Layer *layer = pane->getSelectedLayer();
Chris@0 2664
Chris@0 2665 if (!dynamic_cast<TimeInstantLayer *>(layer) &&
Chris@0 2666 !dynamic_cast<TimeValueLayer *>(layer)) return;
Chris@0 2667
Chris@0 2668 size_t resolution = 0;
Chris@0 2669 if (!layer->snapToFeatureFrame(pane, frame, resolution, Layer::SnapLeft)) {
Chris@0 2670 frame = getMainModel()->getEndFrame();
Chris@0 2671 }
Chris@0 2672
Chris@0 2673 m_viewManager->setPlaybackFrame(frame);
Chris@0 2674 }
Chris@0 2675
Chris@0 2676 void
Chris@0 2677 MainWindow::rewindStart()
Chris@0 2678 {
Chris@0 2679 if (!getMainModel()) return;
Chris@0 2680 m_viewManager->setPlaybackFrame(getMainModel()->getStartFrame());
Chris@0 2681 }
Chris@0 2682
Chris@0 2683 void
Chris@0 2684 MainWindow::stop()
Chris@0 2685 {
Chris@0 2686 m_playSource->stop();
Chris@0 2687 }
Chris@0 2688
Chris@0 2689 void
Chris@0 2690 MainWindow::addPane()
Chris@0 2691 {
Chris@0 2692 QObject *s = sender();
Chris@0 2693 QAction *action = dynamic_cast<QAction *>(s);
Chris@0 2694
Chris@0 2695 if (!action) {
Chris@0 2696 std::cerr << "WARNING: MainWindow::addPane: sender is not an action"
Chris@0 2697 << std::endl;
Chris@0 2698 return;
Chris@0 2699 }
Chris@0 2700
Chris@0 2701 PaneActionMap::iterator i = m_paneActions.find(action);
Chris@0 2702
Chris@0 2703 if (i == m_paneActions.end()) {
Chris@0 2704 std::cerr << "WARNING: MainWindow::addPane: unknown action "
Chris@0 2705 << action->objectName().toStdString() << std::endl;
Chris@0 2706 return;
Chris@0 2707 }
Chris@0 2708
Chris@0 2709 CommandHistory::getInstance()->startCompoundOperation
Chris@0 2710 (action->text(), true);
Chris@0 2711
Chris@0 2712 AddPaneCommand *command = new AddPaneCommand(this);
Chris@0 2713 CommandHistory::getInstance()->addCommand(command);
Chris@0 2714
Chris@0 2715 Pane *pane = command->getPane();
Chris@0 2716
Chris@7 2717 if (i->second.layer == LayerFactory::Spectrum) {
Chris@7 2718 pane->setPlaybackFollow(View::PlaybackScrollContinuous);
Chris@7 2719 }
Chris@7 2720
Chris@8 2721 if (i->second.layer != LayerFactory::TimeRuler &&
Chris@8 2722 i->second.layer != LayerFactory::Spectrum) {
Chris@8 2723
Chris@0 2724 if (!m_timeRulerLayer) {
Chris@0 2725 // std::cerr << "no time ruler layer, creating one" << std::endl;
Chris@0 2726 m_timeRulerLayer = m_document->createMainModelLayer
Chris@0 2727 (LayerFactory::TimeRuler);
Chris@0 2728 }
Chris@0 2729
Chris@0 2730 // std::cerr << "adding time ruler layer " << m_timeRulerLayer << std::endl;
Chris@0 2731
Chris@0 2732 m_document->addLayerToView(pane, m_timeRulerLayer);
Chris@0 2733 }
Chris@0 2734
Chris@0 2735 Layer *newLayer = m_document->createLayer(i->second.layer);
Chris@0 2736 m_document->setModel(newLayer, m_document->getMainModel());
Chris@0 2737 m_document->setChannel(newLayer, i->second.channel);
Chris@0 2738 m_document->addLayerToView(pane, newLayer);
Chris@0 2739
Chris@0 2740 m_paneStack->setCurrentPane(pane);
Chris@0 2741
Chris@0 2742 CommandHistory::getInstance()->endCompoundOperation();
Chris@0 2743
Chris@0 2744 updateMenuStates();
Chris@0 2745 }
Chris@0 2746
Chris@0 2747 MainWindow::AddPaneCommand::AddPaneCommand(MainWindow *mw) :
Chris@0 2748 m_mw(mw),
Chris@0 2749 m_pane(0),
Chris@0 2750 m_prevCurrentPane(0),
Chris@0 2751 m_added(false)
Chris@0 2752 {
Chris@0 2753 }
Chris@0 2754
Chris@0 2755 MainWindow::AddPaneCommand::~AddPaneCommand()
Chris@0 2756 {
Chris@0 2757 if (m_pane && !m_added) {
Chris@0 2758 m_mw->m_paneStack->deletePane(m_pane);
Chris@0 2759 }
Chris@0 2760 }
Chris@0 2761
Chris@0 2762 QString
Chris@0 2763 MainWindow::AddPaneCommand::getName() const
Chris@0 2764 {
Chris@0 2765 return tr("Add Pane");
Chris@0 2766 }
Chris@0 2767
Chris@0 2768 void
Chris@0 2769 MainWindow::AddPaneCommand::execute()
Chris@0 2770 {
Chris@0 2771 if (!m_pane) {
Chris@0 2772 m_prevCurrentPane = m_mw->m_paneStack->getCurrentPane();
Chris@0 2773 m_pane = m_mw->m_paneStack->addPane();
Chris@0 2774 } else {
Chris@0 2775 m_mw->m_paneStack->showPane(m_pane);
Chris@0 2776 }
Chris@0 2777
Chris@0 2778 m_mw->m_paneStack->setCurrentPane(m_pane);
Chris@0 2779 m_mw->m_panner->registerView(m_pane);
Chris@0 2780 m_added = true;
Chris@0 2781 }
Chris@0 2782
Chris@0 2783 void
Chris@0 2784 MainWindow::AddPaneCommand::unexecute()
Chris@0 2785 {
Chris@0 2786 m_mw->m_paneStack->hidePane(m_pane);
Chris@0 2787 m_mw->m_paneStack->setCurrentPane(m_prevCurrentPane);
Chris@0 2788 m_mw->m_panner->unregisterView(m_pane);
Chris@0 2789 m_added = false;
Chris@0 2790 }
Chris@0 2791
Chris@0 2792 MainWindow::RemovePaneCommand::RemovePaneCommand(MainWindow *mw, Pane *pane) :
Chris@0 2793 m_mw(mw),
Chris@0 2794 m_pane(pane),
Chris@0 2795 m_added(true)
Chris@0 2796 {
Chris@0 2797 }
Chris@0 2798
Chris@0 2799 MainWindow::RemovePaneCommand::~RemovePaneCommand()
Chris@0 2800 {
Chris@0 2801 if (m_pane && !m_added) {
Chris@0 2802 m_mw->m_paneStack->deletePane(m_pane);
Chris@0 2803 }
Chris@0 2804 }
Chris@0 2805
Chris@0 2806 QString
Chris@0 2807 MainWindow::RemovePaneCommand::getName() const
Chris@0 2808 {
Chris@0 2809 return tr("Remove Pane");
Chris@0 2810 }
Chris@0 2811
Chris@0 2812 void
Chris@0 2813 MainWindow::RemovePaneCommand::execute()
Chris@0 2814 {
Chris@0 2815 m_prevCurrentPane = m_mw->m_paneStack->getCurrentPane();
Chris@0 2816 m_mw->m_paneStack->hidePane(m_pane);
Chris@0 2817 m_mw->m_panner->unregisterView(m_pane);
Chris@0 2818 m_added = false;
Chris@0 2819 }
Chris@0 2820
Chris@0 2821 void
Chris@0 2822 MainWindow::RemovePaneCommand::unexecute()
Chris@0 2823 {
Chris@0 2824 m_mw->m_paneStack->showPane(m_pane);
Chris@0 2825 m_mw->m_paneStack->setCurrentPane(m_prevCurrentPane);
Chris@0 2826 m_mw->m_panner->registerView(m_pane);
Chris@0 2827 m_added = true;
Chris@0 2828 }
Chris@0 2829
Chris@0 2830 void
Chris@0 2831 MainWindow::addLayer()
Chris@0 2832 {
Chris@0 2833 QObject *s = sender();
Chris@0 2834 QAction *action = dynamic_cast<QAction *>(s);
Chris@0 2835
Chris@0 2836 if (!action) {
Chris@0 2837 std::cerr << "WARNING: MainWindow::addLayer: sender is not an action"
Chris@0 2838 << std::endl;
Chris@0 2839 return;
Chris@0 2840 }
Chris@0 2841
Chris@0 2842 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 2843
Chris@0 2844 if (!pane) {
Chris@0 2845 std::cerr << "WARNING: MainWindow::addLayer: no current pane" << std::endl;
Chris@0 2846 return;
Chris@0 2847 }
Chris@0 2848
Chris@0 2849 ExistingLayerActionMap::iterator ei = m_existingLayerActions.find(action);
Chris@0 2850
Chris@0 2851 if (ei != m_existingLayerActions.end()) {
Chris@0 2852 Layer *newLayer = ei->second;
Chris@0 2853 m_document->addLayerToView(pane, newLayer);
Chris@0 2854 m_paneStack->setCurrentLayer(pane, newLayer);
Chris@0 2855 return;
Chris@0 2856 }
Chris@0 2857
Chris@0 2858 TransformActionMap::iterator i = m_layerTransformActions.find(action);
Chris@0 2859
Chris@0 2860 if (i == m_layerTransformActions.end()) {
Chris@0 2861
Chris@0 2862 LayerActionMap::iterator i = m_layerActions.find(action);
Chris@0 2863
Chris@0 2864 if (i == m_layerActions.end()) {
Chris@0 2865 std::cerr << "WARNING: MainWindow::addLayer: unknown action "
Chris@0 2866 << action->objectName().toStdString() << std::endl;
Chris@0 2867 return;
Chris@0 2868 }
Chris@0 2869
Chris@0 2870 LayerFactory::LayerType type = i->second;
Chris@0 2871
Chris@0 2872 LayerFactory::LayerTypeSet emptyTypes =
Chris@0 2873 LayerFactory::getInstance()->getValidEmptyLayerTypes();
Chris@0 2874
Chris@0 2875 Layer *newLayer;
Chris@0 2876
Chris@0 2877 if (emptyTypes.find(type) != emptyTypes.end()) {
Chris@0 2878
Chris@0 2879 newLayer = m_document->createEmptyLayer(type);
Chris@0 2880 m_toolActions[ViewManager::DrawMode]->trigger();
Chris@0 2881
Chris@0 2882 } else {
Chris@0 2883
Chris@0 2884 newLayer = m_document->createMainModelLayer(type);
Chris@0 2885 }
Chris@0 2886
Chris@0 2887 m_document->addLayerToView(pane, newLayer);
Chris@0 2888 m_paneStack->setCurrentLayer(pane, newLayer);
Chris@0 2889
Chris@0 2890 return;
Chris@0 2891 }
Chris@0 2892
Chris@0 2893 TransformName transform = i->second;
Chris@0 2894 TransformFactory *factory = TransformFactory::getInstance();
Chris@0 2895
Chris@0 2896 QString configurationXml;
Chris@0 2897
Chris@0 2898 int channel = -1;
Chris@0 2899 // pick up the default channel from any existing layers on the same pane
Chris@0 2900 for (int j = 0; j < pane->getLayerCount(); ++j) {
Chris@0 2901 int c = LayerFactory::getInstance()->getChannel(pane->getLayer(j));
Chris@0 2902 if (c != -1) {
Chris@0 2903 channel = c;
Chris@0 2904 break;
Chris@0 2905 }
Chris@0 2906 }
Chris@0 2907
Chris@33 2908 // We always ask for configuration, even if the plugin isn't
Chris@33 2909 // supposed to be configurable, because we need to let the user
Chris@33 2910 // change the execution context (block size etc).
Chris@0 2911
Chris@27 2912 PluginTransform::ExecutionContext context(channel);
Chris@27 2913
Chris@33 2914 bool ok =
Chris@33 2915 factory->getConfigurationForTransform
Chris@33 2916 (transform, m_document->getMainModel(), context, configurationXml);
Chris@33 2917 if (!ok) return;
Chris@0 2918
Chris@0 2919 Layer *newLayer = m_document->createDerivedLayer(transform,
Chris@0 2920 m_document->getMainModel(),
Chris@27 2921 context,
Chris@0 2922 configurationXml);
Chris@0 2923
Chris@0 2924 if (newLayer) {
Chris@0 2925 m_document->addLayerToView(pane, newLayer);
Chris@27 2926 m_document->setChannel(newLayer, context.channel);
Chris@0 2927 }
Chris@0 2928
Chris@0 2929 updateMenuStates();
Chris@0 2930 }
Chris@0 2931
Chris@0 2932 void
Chris@0 2933 MainWindow::deleteCurrentPane()
Chris@0 2934 {
Chris@0 2935 CommandHistory::getInstance()->startCompoundOperation
Chris@0 2936 (tr("Delete Pane"), true);
Chris@0 2937
Chris@0 2938 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 2939 if (pane) {
Chris@0 2940 while (pane->getLayerCount() > 0) {
Chris@0 2941 Layer *layer = pane->getLayer(0);
Chris@0 2942 if (layer) {
Chris@0 2943 m_document->removeLayerFromView(pane, layer);
Chris@0 2944 } else {
Chris@0 2945 break;
Chris@0 2946 }
Chris@0 2947 }
Chris@0 2948
Chris@0 2949 RemovePaneCommand *command = new RemovePaneCommand(this, pane);
Chris@0 2950 CommandHistory::getInstance()->addCommand(command);
Chris@0 2951 }
Chris@0 2952
Chris@0 2953 CommandHistory::getInstance()->endCompoundOperation();
Chris@0 2954
Chris@0 2955 updateMenuStates();
Chris@0 2956 }
Chris@0 2957
Chris@0 2958 void
Chris@0 2959 MainWindow::deleteCurrentLayer()
Chris@0 2960 {
Chris@0 2961 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 2962 if (pane) {
Chris@0 2963 Layer *layer = pane->getSelectedLayer();
Chris@0 2964 if (layer) {
Chris@0 2965 m_document->removeLayerFromView(pane, layer);
Chris@0 2966 }
Chris@0 2967 }
Chris@0 2968 updateMenuStates();
Chris@0 2969 }
Chris@0 2970
Chris@0 2971 void
Chris@0 2972 MainWindow::renameCurrentLayer()
Chris@0 2973 {
Chris@0 2974 Pane *pane = m_paneStack->getCurrentPane();
Chris@0 2975 if (pane) {
Chris@0 2976 Layer *layer = pane->getSelectedLayer();
Chris@0 2977 if (layer) {
Chris@0 2978 bool ok = false;
Chris@0 2979 QString newName = QInputDialog::getText
Chris@0 2980 (this, tr("Rename Layer"),
Chris@0 2981 tr("New name for this layer:"),
Chris@0 2982 QLineEdit::Normal, layer->objectName(), &ok);
Chris@0 2983 if (ok) {
Chris@0 2984 layer->setObjectName(newName);
Chris@0 2985 setupExistingLayersMenu();
Chris@0 2986 }
Chris@0 2987 }
Chris@0 2988 }
Chris@0 2989 }
Chris@0 2990
Chris@0 2991 void
Chris@0 2992 MainWindow::playSpeedChanged(int speed)
Chris@0 2993 {
Chris@25 2994 bool slow = false;
Chris@25 2995 bool something = false;
Chris@25 2996 float factor;
Chris@25 2997
Chris@25 2998 if (speed < 100) {
Chris@25 2999 slow = true;
Chris@25 3000 speed = 100 - speed;
Chris@25 3001 } else {
Chris@25 3002 speed = speed - 100;
Chris@25 3003 }
Chris@25 3004
Chris@25 3005 // speed is 0 -> 100
Chris@25 3006
Chris@25 3007 if (speed == 0) {
Chris@25 3008 factor = 1.0;
Chris@25 3009 } else {
Chris@25 3010 factor = speed;
Chris@25 3011 factor = 1.0 + (factor * factor) / 1000.f;
Chris@25 3012 something = true;
Chris@25 3013 }
Chris@21 3014
Chris@21 3015 int pc = lrintf((factor - 1.0) * 100);
Chris@21 3016
Chris@25 3017 if (!slow) factor = 1.0 / factor;
Chris@25 3018
Chris@25 3019 std::cerr << "speed = " << speed << " factor = " << factor << std::endl;
Chris@21 3020
Chris@21 3021 m_playSpeed->setToolTip(tr("Playback speed: %1%2%")
Chris@25 3022 .arg(!slow ? "+" : "-")
Chris@21 3023 .arg(pc));
Chris@21 3024
Chris@25 3025 m_playSharpen->setEnabled(something);
Chris@26 3026 m_playMono->setEnabled(something);
Chris@25 3027 bool sharpen = (something && m_playSharpen->isChecked());
Chris@26 3028 bool mono = (something && m_playMono->isChecked());
Chris@26 3029 m_playSource->setTimeStretch(factor, sharpen, mono);
Chris@16 3030 }
Chris@16 3031
Chris@16 3032 void
Chris@16 3033 MainWindow::playSharpenToggled()
Chris@16 3034 {
Chris@21 3035 QSettings settings;
Chris@21 3036 settings.beginGroup("MainWindow");
Chris@21 3037 settings.setValue("playsharpen", m_playSharpen->isChecked());
Chris@21 3038 settings.endGroup();
Chris@21 3039
Chris@16 3040 playSpeedChanged(m_playSpeed->value());
Chris@0 3041 }
Chris@0 3042
Chris@0 3043 void
Chris@26 3044 MainWindow::playMonoToggled()
Chris@26 3045 {
Chris@26 3046 QSettings settings;
Chris@26 3047 settings.beginGroup("MainWindow");
Chris@26 3048 settings.setValue("playmono", m_playMono->isChecked());
Chris@26 3049 settings.endGroup();
Chris@26 3050
Chris@26 3051 playSpeedChanged(m_playSpeed->value());
Chris@26 3052 }
Chris@26 3053
Chris@26 3054 void
Chris@0 3055 MainWindow::outputLevelsChanged(float left, float right)
Chris@0 3056 {
Chris@0 3057 m_fader->setPeakLeft(left);
Chris@0 3058 m_fader->setPeakRight(right);
Chris@0 3059 }
Chris@0 3060
Chris@0 3061 void
Chris@0 3062 MainWindow::sampleRateMismatch(size_t requested, size_t actual,
Chris@0 3063 bool willResample)
Chris@0 3064 {
Chris@0 3065 if (!willResample) {
Chris@0 3066 //!!! more helpful message needed
Chris@0 3067 QMessageBox::information
Chris@0 3068 (this, tr("Sample rate mismatch"),
Chris@0 3069 tr("The sample rate of this audio file (%1 Hz) does not match\nthe current playback rate (%2 Hz).\n\nThe file will play at the wrong speed.")
Chris@0 3070 .arg(requested).arg(actual));
Chris@0 3071 }
Chris@0 3072
Chris@0 3073 /*!!! Let's not do this for now, and see how we go -- now that we're putting
Chris@0 3074 sample rate information in the status bar
Chris@0 3075
Chris@0 3076 QMessageBox::information
Chris@0 3077 (this, tr("Sample rate mismatch"),
Chris@0 3078 tr("The sample rate of this audio file (%1 Hz) does not match\nthat of the output audio device (%2 Hz).\n\nThe file will be resampled automatically during playback.")
Chris@0 3079 .arg(requested).arg(actual));
Chris@0 3080 */
Chris@0 3081
Chris@0 3082 updateDescriptionLabel();
Chris@0 3083 }
Chris@0 3084
Chris@0 3085 void
Chris@0 3086 MainWindow::layerAdded(Layer *layer)
Chris@0 3087 {
Chris@0 3088 // std::cerr << "MainWindow::layerAdded(" << layer << ")" << std::endl;
Chris@0 3089 // setupExistingLayersMenu();
Chris@0 3090 updateMenuStates();
Chris@0 3091 }
Chris@0 3092
Chris@0 3093 void
Chris@0 3094 MainWindow::layerRemoved(Layer *layer)
Chris@0 3095 {
Chris@0 3096 // std::cerr << "MainWindow::layerRemoved(" << layer << ")" << std::endl;
Chris@0 3097 setupExistingLayersMenu();
Chris@0 3098 updateMenuStates();
Chris@0 3099 }
Chris@0 3100
Chris@0 3101 void
Chris@0 3102 MainWindow::layerAboutToBeDeleted(Layer *layer)
Chris@0 3103 {
Chris@0 3104 // std::cerr << "MainWindow::layerAboutToBeDeleted(" << layer << ")" << std::endl;
Chris@0 3105 if (layer == m_timeRulerLayer) {
Chris@0 3106 // std::cerr << "(this is the time ruler layer)" << std::endl;
Chris@0 3107 m_timeRulerLayer = 0;
Chris@0 3108 }
Chris@0 3109 }
Chris@0 3110
Chris@0 3111 void
Chris@0 3112 MainWindow::layerInAView(Layer *layer, bool inAView)
Chris@0 3113 {
Chris@0 3114 // std::cerr << "MainWindow::layerInAView(" << layer << "," << inAView << ")" << std::endl;
Chris@0 3115
Chris@0 3116 // Check whether we need to add or remove model from play source
Chris@0 3117 Model *model = layer->getModel();
Chris@0 3118 if (model) {
Chris@0 3119 if (inAView) {
Chris@0 3120 m_playSource->addModel(model);
Chris@0 3121 } else {
Chris@0 3122 bool found = false;
Chris@0 3123 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@0 3124 Pane *pane = m_paneStack->getPane(i);
Chris@0 3125 if (!pane) continue;
Chris@0 3126 for (int j = 0; j < pane->getLayerCount(); ++j) {
Chris@0 3127 Layer *pl = pane->getLayer(j);
Chris@0 3128 if (pl && pl->getModel() == model) {
Chris@0 3129 found = true;
Chris@0 3130 break;
Chris@0 3131 }
Chris@0 3132 }
Chris@0 3133 if (found) break;
Chris@0 3134 }
Chris@0 3135 if (!found) m_playSource->removeModel(model);
Chris@0 3136 }
Chris@0 3137 }
Chris@0 3138
Chris@0 3139 setupExistingLayersMenu();
Chris@0 3140 updateMenuStates();
Chris@0 3141 }
Chris@0 3142
Chris@0 3143 void
Chris@0 3144 MainWindow::modelAdded(Model *model)
Chris@0 3145 {
Chris@0 3146 // std::cerr << "MainWindow::modelAdded(" << model << ")" << std::endl;
Chris@0 3147 m_playSource->addModel(model);
Chris@0 3148 }
Chris@0 3149
Chris@0 3150 void
Chris@0 3151 MainWindow::mainModelChanged(WaveFileModel *model)
Chris@0 3152 {
Chris@0 3153 // std::cerr << "MainWindow::mainModelChanged(" << model << ")" << std::endl;
Chris@0 3154 updateDescriptionLabel();
Chris@0 3155 m_panLayer->setModel(model);
Chris@0 3156 if (model) m_viewManager->setMainModelSampleRate(model->getSampleRate());
Chris@0 3157 if (model && !m_playTarget) createPlayTarget();
Chris@0 3158 }
Chris@0 3159
Chris@0 3160 void
Chris@0 3161 MainWindow::modelAboutToBeDeleted(Model *model)
Chris@0 3162 {
Chris@0 3163 // std::cerr << "MainWindow::modelAboutToBeDeleted(" << model << ")" << std::endl;
Chris@0 3164 m_playSource->removeModel(model);
Chris@0 3165 }
Chris@0 3166
Chris@0 3167 void
Chris@0 3168 MainWindow::modelGenerationFailed(QString transformName)
Chris@0 3169 {
Chris@0 3170 QMessageBox::warning
Chris@0 3171 (this,
Chris@0 3172 tr("Failed to generate layer"),
Chris@0 3173 tr("The layer transform \"%1\" failed to run.\nThis probably means that a plugin failed to initialise.")
Chris@0 3174 .arg(transformName),
Chris@0 3175 QMessageBox::Ok, 0);
Chris@0 3176 }
Chris@0 3177
Chris@0 3178 void
Chris@0 3179 MainWindow::modelRegenerationFailed(QString layerName, QString transformName)
Chris@0 3180 {
Chris@0 3181 QMessageBox::warning
Chris@0 3182 (this,
Chris@0 3183 tr("Failed to regenerate layer"),
Chris@0 3184 tr("Failed to regenerate derived layer \"%1\".\nThe layer transform \"%2\" failed to run.\nThis probably means the layer used a plugin that is not currently available.")
Chris@0 3185 .arg(layerName).arg(transformName),
Chris@0 3186 QMessageBox::Ok, 0);
Chris@0 3187 }
Chris@0 3188
Chris@0 3189 void
Chris@0 3190 MainWindow::rightButtonMenuRequested(Pane *pane, QPoint position)
Chris@0 3191 {
Chris@0 3192 // std::cerr << "MainWindow::rightButtonMenuRequested(" << pane << ", " << position.x() << ", " << position.y() << ")" << std::endl;
Chris@0 3193 m_paneStack->setCurrentPane(pane);
Chris@0 3194 m_rightButtonMenu->popup(position);
Chris@0 3195 }
Chris@0 3196
Chris@0 3197 void
Chris@0 3198 MainWindow::showLayerTree()
Chris@0 3199 {
Chris@0 3200 QTreeView *view = new QTreeView();
Chris@0 3201 LayerTreeModel *tree = new LayerTreeModel(m_paneStack);
Chris@0 3202 view->expand(tree->index(0, 0, QModelIndex()));
Chris@0 3203 view->setModel(tree);
Chris@0 3204 view->show();
Chris@0 3205 }
Chris@0 3206
Chris@0 3207 void
Chris@0 3208 MainWindow::preferenceChanged(PropertyContainer::PropertyName name)
Chris@0 3209 {
Chris@0 3210 if (name == "Property Box Layout") {
Chris@0 3211 if (Preferences::getInstance()->getPropertyBoxLayout() ==
Chris@0 3212 Preferences::VerticallyStacked) {
Chris@0 3213 m_paneStack->setLayoutStyle(PaneStack::PropertyStackPerPaneLayout);
Chris@0 3214 } else {
Chris@0 3215 m_paneStack->setLayoutStyle(PaneStack::SinglePropertyStackLayout);
Chris@0 3216 }
Chris@0 3217 }
Chris@0 3218 }
Chris@0 3219
Chris@0 3220 void
Chris@0 3221 MainWindow::preferences()
Chris@0 3222 {
Chris@0 3223 if (!m_preferencesDialog.isNull()) {
Chris@0 3224 m_preferencesDialog->show();
Chris@0 3225 m_preferencesDialog->raise();
Chris@0 3226 return;
Chris@0 3227 }
Chris@0 3228
Chris@0 3229 m_preferencesDialog = new PreferencesDialog(this);
Chris@0 3230
Chris@0 3231 // DeleteOnClose is safe here, because m_preferencesDialog is a
Chris@0 3232 // QPointer that will be zeroed when the dialog is deleted. We
Chris@0 3233 // use it in preference to leaving the dialog lying around because
Chris@0 3234 // if you Cancel the dialog, it resets the preferences state
Chris@0 3235 // without resetting its own widgets, so its state will be
Chris@0 3236 // incorrect when next shown unless we construct it afresh
Chris@0 3237 m_preferencesDialog->setAttribute(Qt::WA_DeleteOnClose);
Chris@0 3238
Chris@0 3239 m_preferencesDialog->show();
Chris@0 3240 }
Chris@0 3241
Chris@0 3242 void
Chris@0 3243 MainWindow::website()
Chris@0 3244 {
Chris@0 3245 openHelpUrl(tr("http://www.sonicvisualiser.org/"));
Chris@0 3246 }
Chris@0 3247
Chris@0 3248 void
Chris@0 3249 MainWindow::help()
Chris@0 3250 {
Chris@0 3251 openHelpUrl(tr("http://www.sonicvisualiser.org/doc/reference/en/"));
Chris@0 3252 }
Chris@0 3253
Chris@0 3254 void
Chris@0 3255 MainWindow::openHelpUrl(QString url)
Chris@0 3256 {
Chris@0 3257 // This method mostly lifted from Qt Assistant source code
Chris@0 3258
Chris@0 3259 QProcess *process = new QProcess(this);
Chris@0 3260 connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater()));
Chris@0 3261
Chris@0 3262 QStringList args;
Chris@0 3263
Chris@0 3264 #ifdef Q_OS_MAC
Chris@0 3265 args.append(url);
Chris@0 3266 process->start("open", args);
Chris@0 3267 #else
Chris@0 3268 #ifdef Q_OS_WIN32
Chris@0 3269
Chris@0 3270 QString pf(getenv("ProgramFiles"));
Chris@0 3271 QString command = pf + QString("\\Internet Explorer\\IEXPLORE.EXE");
Chris@0 3272
Chris@0 3273 args.append(url);
Chris@0 3274 process->start(command, args);
Chris@0 3275
Chris@0 3276 #else
Chris@0 3277 #ifdef Q_WS_X11
Chris@0 3278 if (!qgetenv("KDE_FULL_SESSION").isEmpty()) {
Chris@0 3279 args.append("exec");
Chris@0 3280 args.append(url);
Chris@0 3281 process->start("kfmclient", args);
Chris@0 3282 } else if (!qgetenv("BROWSER").isEmpty()) {
Chris@0 3283 args.append(url);
Chris@0 3284 process->start(qgetenv("BROWSER"), args);
Chris@0 3285 } else {
Chris@0 3286 args.append(url);
Chris@0 3287 process->start("firefox", args);
Chris@0 3288 }
Chris@0 3289 #endif
Chris@0 3290 #endif
Chris@0 3291 #endif
Chris@0 3292 }
Chris@0 3293
Chris@0 3294 void
Chris@0 3295 MainWindow::about()
Chris@0 3296 {
Chris@0 3297 bool debug = false;
Chris@0 3298 QString version = "(unknown version)";
Chris@0 3299
Chris@0 3300 #ifdef BUILD_DEBUG
Chris@0 3301 debug = true;
Chris@0 3302 #endif
Chris@0 3303 #ifdef SV_VERSION
Chris@0 3304 #ifdef SVNREV
Chris@0 3305 version = tr("Release %1 : Revision %2").arg(SV_VERSION).arg(SVNREV);
Chris@0 3306 #else
Chris@0 3307 version = tr("Release %1").arg(SV_VERSION);
Chris@0 3308 #endif
Chris@0 3309 #else
Chris@0 3310 #ifdef SVNREV
Chris@0 3311 version = tr("Unreleased : Revision %1").arg(SVNREV);
Chris@0 3312 #endif
Chris@0 3313 #endif
Chris@0 3314
Chris@0 3315 QString aboutText;
Chris@0 3316
Chris@0 3317 aboutText += tr("<h3>About Sonic Visualiser</h3>");
Chris@0 3318 aboutText += tr("<p>Sonic Visualiser is a program for viewing and exploring audio data for semantic music analysis and annotation.</p>");
Chris@0 3319 aboutText += tr("<p>%1 : %2 build</p>")
Chris@0 3320 .arg(version)
Chris@0 3321 .arg(debug ? tr("Debug") : tr("Release"));
Chris@0 3322
Chris@0 3323 #ifdef BUILD_STATIC
Chris@0 3324 aboutText += tr("<p>Statically linked");
Chris@0 3325 #ifndef QT_SHARED
Chris@0 3326 aboutText += tr("<br>With Qt (v%1) &copy; Trolltech AS").arg(QT_VERSION_STR);
Chris@0 3327 #endif
Chris@0 3328 #ifdef HAVE_JACK
Chris@0 3329 aboutText += tr("<br>With JACK audio output (v%1) &copy; Paul Davis and Jack O'Quin").arg(JACK_VERSION);
Chris@0 3330 #endif
Chris@0 3331 #ifdef HAVE_PORTAUDIO
Chris@0 3332 aboutText += tr("<br>With PortAudio audio output &copy; Ross Bencina and Phil Burk");
Chris@0 3333 #endif
Chris@0 3334 #ifdef HAVE_OGGZ
Chris@0 3335 aboutText += tr("<br>With Ogg file decoder (oggz v%1, fishsound v%2) &copy; CSIRO Australia").arg(OGGZ_VERSION).arg(FISHSOUND_VERSION);
Chris@0 3336 #endif
Chris@0 3337 #ifdef HAVE_MAD
Chris@0 3338 aboutText += tr("<br>With MAD mp3 decoder (v%1) &copy; Underbit Technologies Inc").arg(MAD_VERSION);
Chris@0 3339 #endif
Chris@0 3340 #ifdef HAVE_SAMPLERATE
Chris@0 3341 aboutText += tr("<br>With libsamplerate (v%1) &copy; Erik de Castro Lopo").arg(SAMPLERATE_VERSION);
Chris@0 3342 #endif
Chris@0 3343 #ifdef HAVE_SNDFILE
Chris@0 3344 aboutText += tr("<br>With libsndfile (v%1) &copy; Erik de Castro Lopo").arg(SNDFILE_VERSION);
Chris@0 3345 #endif
Chris@0 3346 #ifdef HAVE_FFTW3
Chris@0 3347 aboutText += tr("<br>With FFTW3 (v%1) &copy; Matteo Frigo and MIT").arg(FFTW3_VERSION);
Chris@0 3348 #endif
Chris@0 3349 #ifdef HAVE_VAMP
Chris@0 3350 aboutText += tr("<br>With Vamp plugin support (API v%1, SDK v%2) &copy; Chris Cannam").arg(VAMP_API_VERSION).arg(VAMP_SDK_VERSION);
Chris@0 3351 #endif
Chris@0 3352 aboutText += tr("<br>With LADSPA plugin support (API v%1) &copy; Richard Furse, Paul Davis, Stefan Westerfeld").arg(LADSPA_VERSION);
Chris@0 3353 aboutText += tr("<br>With DSSI plugin support (API v%1) &copy; Chris Cannam, Steve Harris, Sean Bolton").arg(DSSI_VERSION);
Chris@0 3354 aboutText += "</p>";
Chris@0 3355 #endif
Chris@0 3356
Chris@0 3357 aboutText +=
Chris@0 3358 "<p>Sonic Visualiser Copyright &copy; 2005 - 2006 Chris Cannam<br>"
Chris@0 3359 "Centre for Digital Music, Queen Mary, University of London.</p>"
Chris@0 3360 "<p>This program is free software; you can redistribute it and/or<br>"
Chris@0 3361 "modify it under the terms of the GNU General Public License as<br>"
Chris@0 3362 "published by the Free Software Foundation; either version 2 of the<br>"
Chris@0 3363 "License, or (at your option) any later version.<br>See the file "
Chris@0 3364 "COPYING included with this distribution for more information.</p>";
Chris@0 3365
Chris@0 3366 QMessageBox::about(this, tr("About Sonic Visualiser"), aboutText);
Chris@0 3367 }
Chris@0 3368
Chris@0 3369
Chris@0 3370 #ifdef INCLUDE_MOCFILES
Chris@0 3371 #include "MainWindow.moc.cpp"
Chris@0 3372 #endif