annotate framework/MainWindowBase.cpp @ 230:976d43d3c67d templating

Split out openSessionTemplate with template name argument rather than FileSource
author Chris Cannam
date Tue, 17 May 2011 17:04:22 +0100
parents a6398a7400c1
children 65d75297a37e
rev   line source
Chris@45 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@45 2
Chris@45 3 /*
Chris@45 4 Sonic Visualiser
Chris@45 5 An audio file viewer and annotation editor.
Chris@45 6 Centre for Digital Music, Queen Mary, University of London.
Chris@45 7 This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@45 8
Chris@45 9 This program is free software; you can redistribute it and/or
Chris@45 10 modify it under the terms of the GNU General Public License as
Chris@45 11 published by the Free Software Foundation; either version 2 of the
Chris@45 12 License, or (at your option) any later version. See the file
Chris@45 13 COPYING included with this distribution for more information.
Chris@45 14 */
Chris@45 15
Chris@45 16 #include "MainWindowBase.h"
Chris@46 17 #include "Document.h"
Chris@45 18
Chris@45 19
Chris@45 20 #include "view/Pane.h"
Chris@45 21 #include "view/PaneStack.h"
Chris@45 22 #include "data/model/WaveFileModel.h"
Chris@45 23 #include "data/model/SparseOneDimensionalModel.h"
Chris@45 24 #include "data/model/NoteModel.h"
Chris@45 25 #include "data/model/Labeller.h"
Chris@124 26 #include "data/model/TabularModel.h"
Chris@45 27 #include "view/ViewManager.h"
Chris@45 28
Chris@45 29 #include "layer/WaveformLayer.h"
Chris@45 30 #include "layer/TimeRulerLayer.h"
Chris@45 31 #include "layer/TimeInstantLayer.h"
Chris@45 32 #include "layer/TimeValueLayer.h"
Chris@45 33 #include "layer/Colour3DPlotLayer.h"
Chris@45 34 #include "layer/SliceLayer.h"
Chris@45 35 #include "layer/SliceableLayer.h"
Chris@45 36 #include "layer/ImageLayer.h"
Chris@184 37 #include "layer/NoteLayer.h"
Chris@184 38 #include "layer/RegionLayer.h"
Chris@45 39
Chris@45 40 #include "widgets/ListInputDialog.h"
Chris@105 41 #include "widgets/CommandHistory.h"
Chris@109 42 #include "widgets/ProgressDialog.h"
Chris@109 43 #include "widgets/MIDIFileImportDialog.h"
Chris@109 44 #include "widgets/CSVFormatDialog.h"
Chris@123 45 #include "widgets/ModelDataTableDialog.h"
Chris@45 46
Chris@45 47 #include "audioio/AudioCallbackPlaySource.h"
Chris@45 48 #include "audioio/AudioCallbackPlayTarget.h"
Chris@45 49 #include "audioio/AudioTargetFactory.h"
Chris@45 50 #include "audioio/PlaySpeedRangeMapper.h"
Chris@45 51 #include "data/fileio/DataFileReaderFactory.h"
Chris@45 52 #include "data/fileio/PlaylistFileReader.h"
Chris@45 53 #include "data/fileio/WavFileWriter.h"
Chris@45 54 #include "data/fileio/CSVFileWriter.h"
Chris@45 55 #include "data/fileio/MIDIFileWriter.h"
Chris@45 56 #include "data/fileio/BZipFileDevice.h"
Chris@45 57 #include "data/fileio/FileSource.h"
Chris@152 58 #include "data/fileio/AudioFileReaderFactory.h"
Chris@134 59 #include "rdf/RDFImporter.h"
Chris@45 60
Chris@45 61 #include "data/fft/FFTDataServer.h"
Chris@45 62
Chris@45 63 #include "base/RecentFiles.h"
Chris@45 64
Chris@45 65 #include "base/PlayParameterRepository.h"
Chris@45 66 #include "base/XmlExportable.h"
Chris@45 67 #include "base/Profiler.h"
Chris@45 68 #include "base/Preferences.h"
Chris@217 69 #include "base/TempWriteFile.h"
Chris@217 70 #include "base/Exceptions.h"
Chris@223 71 #include "base/ResourceFinder.h"
Chris@45 72
Chris@45 73 #include "data/osc/OSCQueue.h"
Chris@157 74 #include "data/midi/MIDIInput.h"
Chris@45 75
Chris@45 76 #include <QApplication>
Chris@45 77 #include <QMessageBox>
Chris@45 78 #include <QGridLayout>
Chris@45 79 #include <QLabel>
Chris@45 80 #include <QAction>
Chris@45 81 #include <QMenuBar>
Chris@45 82 #include <QToolBar>
Chris@45 83 #include <QInputDialog>
Chris@45 84 #include <QStatusBar>
Chris@45 85 #include <QTreeView>
Chris@45 86 #include <QFile>
Chris@45 87 #include <QFileInfo>
Chris@45 88 #include <QDir>
Chris@45 89 #include <QTextStream>
Chris@45 90 #include <QProcess>
Chris@45 91 #include <QShortcut>
Chris@45 92 #include <QSettings>
Chris@45 93 #include <QDateTime>
Chris@45 94 #include <QProcess>
Chris@45 95 #include <QCheckBox>
Chris@45 96 #include <QRegExp>
Chris@45 97 #include <QScrollArea>
Chris@168 98 #include <QDesktopWidget>
Chris@45 99
Chris@45 100 #include <iostream>
Chris@45 101 #include <cstdio>
Chris@45 102 #include <errno.h>
Chris@45 103
Chris@45 104 using std::cerr;
Chris@45 105 using std::endl;
Chris@45 106
Chris@45 107 using std::vector;
Chris@45 108 using std::map;
Chris@45 109 using std::set;
Chris@45 110
Chris@45 111
Chris@161 112 MainWindowBase::MainWindowBase(bool withAudioOutput,
Chris@161 113 bool withOSCSupport,
Chris@161 114 bool withMIDIInput) :
Chris@45 115 m_document(0),
Chris@45 116 m_paneStack(0),
Chris@45 117 m_viewManager(0),
Chris@45 118 m_timeRulerLayer(0),
Chris@45 119 m_audioOutput(withAudioOutput),
Chris@45 120 m_playSource(0),
Chris@45 121 m_playTarget(0),
Chris@113 122 m_oscQueue(0),
Chris@113 123 m_oscQueueStarter(0),
Chris@157 124 m_midiInput(0),
Chris@45 125 m_recentFiles("RecentFiles", 20),
Chris@54 126 m_recentTransforms("RecentTransforms", 20),
Chris@45 127 m_documentModified(false),
Chris@45 128 m_openingAudioFile(false),
Chris@45 129 m_abandoning(false),
Chris@121 130 m_labeller(0),
Chris@121 131 m_lastPlayStatusSec(0)
Chris@45 132 {
Chris@113 133 Profiler profiler("MainWindowBase::MainWindowBase");
Chris@113 134
Chris@45 135 connect(CommandHistory::getInstance(), SIGNAL(commandExecuted()),
Chris@45 136 this, SLOT(documentModified()));
Chris@45 137 connect(CommandHistory::getInstance(), SIGNAL(documentRestored()),
Chris@45 138 this, SLOT(documentRestored()));
Chris@45 139
Chris@45 140 m_viewManager = new ViewManager();
Chris@45 141 connect(m_viewManager, SIGNAL(selectionChanged()),
Chris@45 142 this, SLOT(updateMenuStates()));
Chris@45 143 connect(m_viewManager, SIGNAL(inProgressSelectionChanged()),
Chris@45 144 this, SLOT(inProgressSelectionChanged()));
Chris@45 145
Chris@105 146 // set a sensible default font size for views -- cannot do this
Chris@105 147 // in Preferences, which is in base and not supposed to use QtGui
Chris@105 148 int viewFontSize = QApplication::font().pointSize() * 0.9;
Chris@105 149 QSettings settings;
Chris@105 150 settings.beginGroup("Preferences");
Chris@105 151 viewFontSize = settings.value("view-font-size", viewFontSize).toInt();
Chris@105 152 settings.setValue("view-font-size", viewFontSize);
Chris@105 153 settings.endGroup();
Chris@105 154
Chris@45 155 Preferences::BackgroundMode mode =
Chris@45 156 Preferences::getInstance()->getBackgroundMode();
Chris@45 157 m_initialDarkBackground = m_viewManager->getGlobalDarkBackground();
Chris@45 158 if (mode != Preferences::BackgroundFromTheme) {
Chris@45 159 m_viewManager->setGlobalDarkBackground
Chris@45 160 (mode == Preferences::DarkBackground);
Chris@45 161 }
Chris@45 162
Chris@45 163 m_paneStack = new PaneStack(0, m_viewManager);
Chris@45 164 connect(m_paneStack, SIGNAL(currentPaneChanged(Pane *)),
Chris@45 165 this, SLOT(currentPaneChanged(Pane *)));
Chris@45 166 connect(m_paneStack, SIGNAL(currentLayerChanged(Pane *, Layer *)),
Chris@45 167 this, SLOT(currentLayerChanged(Pane *, Layer *)));
Chris@45 168 connect(m_paneStack, SIGNAL(rightButtonMenuRequested(Pane *, QPoint)),
Chris@45 169 this, SLOT(rightButtonMenuRequested(Pane *, QPoint)));
Chris@45 170 connect(m_paneStack, SIGNAL(contextHelpChanged(const QString &)),
Chris@45 171 this, SLOT(contextHelpChanged(const QString &)));
Chris@45 172 connect(m_paneStack, SIGNAL(paneAdded(Pane *)),
Chris@45 173 this, SLOT(paneAdded(Pane *)));
Chris@45 174 connect(m_paneStack, SIGNAL(paneHidden(Pane *)),
Chris@45 175 this, SLOT(paneHidden(Pane *)));
Chris@45 176 connect(m_paneStack, SIGNAL(paneAboutToBeDeleted(Pane *)),
Chris@45 177 this, SLOT(paneAboutToBeDeleted(Pane *)));
Chris@45 178 connect(m_paneStack, SIGNAL(dropAccepted(Pane *, QStringList)),
Chris@45 179 this, SLOT(paneDropAccepted(Pane *, QStringList)));
Chris@45 180 connect(m_paneStack, SIGNAL(dropAccepted(Pane *, QString)),
Chris@45 181 this, SLOT(paneDropAccepted(Pane *, QString)));
Chris@55 182 connect(m_paneStack, SIGNAL(paneDeleteButtonClicked(Pane *)),
Chris@55 183 this, SLOT(paneDeleteButtonClicked(Pane *)));
Chris@45 184
Chris@57 185 m_playSource = new AudioCallbackPlaySource(m_viewManager,
Chris@57 186 QApplication::applicationName());
Chris@45 187
Chris@45 188 connect(m_playSource, SIGNAL(sampleRateMismatch(size_t, size_t, bool)),
Chris@45 189 this, SLOT(sampleRateMismatch(size_t, size_t, bool)));
Chris@45 190 connect(m_playSource, SIGNAL(audioOverloadPluginDisabled()),
Chris@45 191 this, SLOT(audioOverloadPluginDisabled()));
Chris@130 192 connect(m_playSource, SIGNAL(audioTimeStretchMultiChannelDisabled()),
Chris@130 193 this, SLOT(audioTimeStretchMultiChannelDisabled()));
Chris@45 194
Chris@45 195 connect(m_viewManager, SIGNAL(outputLevelsChanged(float, float)),
Chris@45 196 this, SLOT(outputLevelsChanged(float, float)));
Chris@45 197
Chris@45 198 connect(m_viewManager, SIGNAL(playbackFrameChanged(unsigned long)),
Chris@45 199 this, SLOT(playbackFrameChanged(unsigned long)));
Chris@45 200
Chris@45 201 connect(m_viewManager, SIGNAL(globalCentreFrameChanged(unsigned long)),
Chris@45 202 this, SLOT(globalCentreFrameChanged(unsigned long)));
Chris@45 203
Chris@45 204 connect(m_viewManager, SIGNAL(viewCentreFrameChanged(View *, unsigned long)),
Chris@45 205 this, SLOT(viewCentreFrameChanged(View *, unsigned long)));
Chris@45 206
Chris@45 207 connect(m_viewManager, SIGNAL(viewZoomLevelChanged(View *, unsigned long, bool)),
Chris@45 208 this, SLOT(viewZoomLevelChanged(View *, unsigned long, bool)));
Chris@45 209
Chris@45 210 connect(Preferences::getInstance(),
Chris@45 211 SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@45 212 this,
Chris@45 213 SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@45 214
Chris@45 215 Labeller::ValueType labellerType = Labeller::ValueFromTwoLevelCounter;
Chris@45 216 settings.beginGroup("MainWindow");
Chris@230 217
Chris@45 218 labellerType = (Labeller::ValueType)
Chris@45 219 settings.value("labellertype", (int)labellerType).toInt();
Chris@45 220 int cycle = settings.value("labellercycle", 4).toInt();
Chris@230 221
Chris@230 222 m_defaultSessionTemplate = settings.value("sessiontemplate", "").toString();
Chris@230 223 if (m_defaultSessionTemplate == "") m_defaultSessionTemplate = "default";
Chris@230 224
Chris@45 225 settings.endGroup();
Chris@45 226
Chris@45 227 m_labeller = new Labeller(labellerType);
Chris@45 228 m_labeller->setCounterCycleSize(cycle);
Chris@113 229
Chris@161 230 if (withMIDIInput) {
Chris@161 231 m_midiInput = new MIDIInput(QApplication::applicationName(), this);
Chris@161 232 }
Chris@157 233
Chris@113 234 if (withOSCSupport) {
Chris@113 235 m_oscQueueStarter = new OSCQueueStarter(this);
Chris@113 236 connect(m_oscQueueStarter, SIGNAL(finished()), this, SLOT(oscReady()));
Chris@113 237 m_oscQueueStarter->start();
Chris@113 238 }
Chris@45 239 }
Chris@45 240
Chris@45 241 MainWindowBase::~MainWindowBase()
Chris@45 242 {
Chris@177 243 std::cerr << "MainWindowBase::~MainWindowBase" << std::endl;
Chris@70 244 if (m_playTarget) m_playTarget->shutdown();
Chris@70 245 // delete m_playTarget;
Chris@45 246 delete m_playSource;
Chris@45 247 delete m_viewManager;
Chris@45 248 delete m_oscQueue;
Chris@157 249 delete m_midiInput;
Chris@45 250 Profiles::getInstance()->dump();
Chris@45 251 }
Chris@45 252
Chris@113 253 void
Chris@168 254 MainWindowBase::resizeConstrained(QSize size)
Chris@168 255 {
Chris@168 256 QDesktopWidget *desktop = QApplication::desktop();
Chris@168 257 QRect available = desktop->availableGeometry();
Chris@168 258 QSize actual(std::min(size.width(), available.width()),
Chris@168 259 std::min(size.height(), available.height()));
Chris@168 260 resize(actual);
Chris@168 261 }
Chris@168 262
Chris@168 263 void
Chris@113 264 MainWindowBase::oscReady()
Chris@113 265 {
Chris@113 266 if (m_oscQueue && m_oscQueue->isOK()) {
Chris@113 267 connect(m_oscQueue, SIGNAL(messagesAvailable()), this, SLOT(pollOSC()));
Chris@113 268 QTimer *oscTimer = new QTimer(this);
Chris@113 269 connect(oscTimer, SIGNAL(timeout()), this, SLOT(pollOSC()));
Chris@113 270 oscTimer->start(1000);
Chris@113 271 std::cerr << "Finished setting up OSC interface" << std::endl;
Chris@113 272 }
Chris@113 273 }
Chris@113 274
Chris@45 275 QString
Chris@45 276 MainWindowBase::getOpenFileName(FileFinder::FileType type)
Chris@45 277 {
Chris@45 278 FileFinder *ff = FileFinder::getInstance();
Chris@45 279 switch (type) {
Chris@45 280 case FileFinder::SessionFile:
Chris@45 281 return ff->getOpenFileName(type, m_sessionFile);
Chris@45 282 case FileFinder::AudioFile:
Chris@45 283 return ff->getOpenFileName(type, m_audioFile);
Chris@45 284 case FileFinder::LayerFile:
Chris@45 285 return ff->getOpenFileName(type, m_sessionFile);
Chris@45 286 case FileFinder::LayerFileNoMidi:
Chris@45 287 return ff->getOpenFileName(type, m_sessionFile);
Chris@45 288 case FileFinder::SessionOrAudioFile:
Chris@45 289 return ff->getOpenFileName(type, m_sessionFile);
Chris@45 290 case FileFinder::ImageFile:
Chris@45 291 return ff->getOpenFileName(type, m_sessionFile);
Chris@45 292 case FileFinder::AnyFile:
Chris@45 293 if (getMainModel() != 0 &&
Chris@45 294 m_paneStack != 0 &&
Chris@45 295 m_paneStack->getCurrentPane() != 0) { // can import a layer
Chris@45 296 return ff->getOpenFileName(FileFinder::AnyFile, m_sessionFile);
Chris@45 297 } else {
Chris@45 298 return ff->getOpenFileName(FileFinder::SessionOrAudioFile,
Chris@45 299 m_sessionFile);
Chris@45 300 }
Chris@45 301 }
Chris@45 302 return "";
Chris@45 303 }
Chris@45 304
Chris@45 305 QString
Chris@45 306 MainWindowBase::getSaveFileName(FileFinder::FileType type)
Chris@45 307 {
Chris@45 308 FileFinder *ff = FileFinder::getInstance();
Chris@45 309 switch (type) {
Chris@45 310 case FileFinder::SessionFile:
Chris@45 311 return ff->getSaveFileName(type, m_sessionFile);
Chris@45 312 case FileFinder::AudioFile:
Chris@45 313 return ff->getSaveFileName(type, m_audioFile);
Chris@45 314 case FileFinder::LayerFile:
Chris@45 315 return ff->getSaveFileName(type, m_sessionFile);
Chris@45 316 case FileFinder::LayerFileNoMidi:
Chris@45 317 return ff->getSaveFileName(type, m_sessionFile);
Chris@45 318 case FileFinder::SessionOrAudioFile:
Chris@45 319 return ff->getSaveFileName(type, m_sessionFile);
Chris@45 320 case FileFinder::ImageFile:
Chris@45 321 return ff->getSaveFileName(type, m_sessionFile);
Chris@45 322 case FileFinder::AnyFile:
Chris@45 323 return ff->getSaveFileName(type, m_sessionFile);
Chris@45 324 }
Chris@45 325 return "";
Chris@45 326 }
Chris@45 327
Chris@45 328 void
Chris@45 329 MainWindowBase::registerLastOpenedFilePath(FileFinder::FileType type, QString path)
Chris@45 330 {
Chris@45 331 FileFinder *ff = FileFinder::getInstance();
Chris@45 332 ff->registerLastOpenedFilePath(type, path);
Chris@45 333 }
Chris@45 334
Chris@45 335 void
Chris@222 336 MainWindowBase::setDefaultSessionTemplate(QString name)
Chris@222 337 {
Chris@222 338 m_defaultSessionTemplate = name;
Chris@222 339 }
Chris@222 340
Chris@222 341 QString
Chris@222 342 MainWindowBase::getDefaultSessionTemplate() const
Chris@222 343 {
Chris@222 344 return m_defaultSessionTemplate;
Chris@222 345 }
Chris@222 346
Chris@222 347 void
Chris@45 348 MainWindowBase::updateMenuStates()
Chris@45 349 {
Chris@45 350 Pane *currentPane = 0;
Chris@45 351 Layer *currentLayer = 0;
Chris@45 352
Chris@45 353 if (m_paneStack) currentPane = m_paneStack->getCurrentPane();
Chris@45 354 if (currentPane) currentLayer = currentPane->getSelectedLayer();
Chris@45 355
Chris@73 356 bool havePrevPane = false, haveNextPane = false;
Chris@73 357 bool havePrevLayer = false, haveNextLayer = false;
Chris@73 358
Chris@73 359 if (currentPane) {
Chris@73 360 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@73 361 if (m_paneStack->getPane(i) == currentPane) {
Chris@73 362 if (i > 0) havePrevPane = true;
Chris@73 363 if (i < m_paneStack->getPaneCount()-1) haveNextPane = true;
Chris@73 364 break;
Chris@73 365 }
Chris@73 366 }
Chris@73 367 if (currentLayer) {
Chris@73 368 for (int i = 0; i < currentPane->getLayerCount(); ++i) {
Chris@73 369 if (currentPane->getLayer(i) == currentLayer) {
Chris@73 370 if (i > 0) havePrevLayer = true;
Chris@73 371 if (i < currentPane->getLayerCount()-1) haveNextLayer = true;
Chris@73 372 break;
Chris@73 373 }
Chris@73 374 }
Chris@73 375 }
Chris@73 376 }
Chris@73 377
Chris@45 378 bool haveCurrentPane =
Chris@45 379 (currentPane != 0);
Chris@45 380 bool haveCurrentLayer =
Chris@45 381 (haveCurrentPane &&
Chris@45 382 (currentLayer != 0));
Chris@45 383 bool haveMainModel =
Chris@45 384 (getMainModel() != 0);
Chris@45 385 bool havePlayTarget =
Chris@45 386 (m_playTarget != 0);
Chris@45 387 bool haveSelection =
Chris@45 388 (m_viewManager &&
Chris@45 389 !m_viewManager->getSelections().empty());
Chris@45 390 bool haveCurrentEditableLayer =
Chris@45 391 (haveCurrentLayer &&
Chris@45 392 currentLayer->isLayerEditable());
Chris@45 393 bool haveCurrentTimeInstantsLayer =
Chris@45 394 (haveCurrentLayer &&
Chris@45 395 dynamic_cast<TimeInstantLayer *>(currentLayer));
Chris@184 396 bool haveCurrentDurationLayer =
Chris@184 397 (haveCurrentLayer &&
Chris@184 398 (dynamic_cast<NoteLayer *>(currentLayer) ||
Chris@184 399 dynamic_cast<RegionLayer *>(currentLayer)));
Chris@45 400 bool haveCurrentColour3DPlot =
Chris@45 401 (haveCurrentLayer &&
Chris@45 402 dynamic_cast<Colour3DPlotLayer *>(currentLayer));
Chris@45 403 bool haveClipboardContents =
Chris@45 404 (m_viewManager &&
Chris@45 405 !m_viewManager->getClipboard().empty());
Chris@146 406 bool haveTabularLayer =
Chris@146 407 (haveCurrentLayer &&
Chris@146 408 dynamic_cast<TabularModel *>(currentLayer->getModel()));
Chris@45 409
Chris@45 410 emit canAddPane(haveMainModel);
Chris@45 411 emit canDeleteCurrentPane(haveCurrentPane);
Chris@45 412 emit canZoom(haveMainModel && haveCurrentPane);
Chris@45 413 emit canScroll(haveMainModel && haveCurrentPane);
Chris@45 414 emit canAddLayer(haveMainModel && haveCurrentPane);
Chris@45 415 emit canImportMoreAudio(haveMainModel);
Chris@45 416 emit canImportLayer(haveMainModel && haveCurrentPane);
Chris@45 417 emit canExportAudio(haveMainModel);
Chris@45 418 emit canExportLayer(haveMainModel &&
Chris@45 419 (haveCurrentEditableLayer || haveCurrentColour3DPlot));
Chris@45 420 emit canExportImage(haveMainModel && haveCurrentPane);
Chris@45 421 emit canDeleteCurrentLayer(haveCurrentLayer);
Chris@45 422 emit canRenameLayer(haveCurrentLayer);
Chris@45 423 emit canEditLayer(haveCurrentEditableLayer);
Chris@146 424 emit canEditLayerTabular(haveCurrentEditableLayer || haveTabularLayer);
Chris@45 425 emit canMeasureLayer(haveCurrentLayer);
Chris@45 426 emit canSelect(haveMainModel && haveCurrentPane);
Chris@188 427 emit canPlay(haveMainModel && havePlayTarget);
Chris@45 428 emit canFfwd(true);
Chris@45 429 emit canRewind(true);
Chris@87 430 emit canPaste(haveClipboardContents);
Chris@45 431 emit canInsertInstant(haveCurrentPane);
Chris@45 432 emit canInsertInstantsAtBoundaries(haveCurrentPane && haveSelection);
Chris@184 433 emit canInsertItemAtSelection(haveCurrentPane && haveSelection && haveCurrentDurationLayer);
Chris@45 434 emit canRenumberInstants(haveCurrentTimeInstantsLayer && haveSelection);
Chris@45 435 emit canPlaySelection(haveMainModel && havePlayTarget && haveSelection);
Chris@45 436 emit canClearSelection(haveSelection);
Chris@45 437 emit canEditSelection(haveSelection && haveCurrentEditableLayer);
Chris@45 438 emit canSave(m_sessionFile != "" && m_documentModified);
Chris@73 439 emit canSelectPreviousPane(havePrevPane);
Chris@73 440 emit canSelectNextPane(haveNextPane);
Chris@73 441 emit canSelectPreviousLayer(havePrevLayer);
Chris@73 442 emit canSelectNextLayer(haveNextLayer);
Chris@45 443 }
Chris@45 444
Chris@45 445 void
Chris@45 446 MainWindowBase::documentModified()
Chris@45 447 {
Chris@45 448 // std::cerr << "MainWindowBase::documentModified" << std::endl;
Chris@45 449
Chris@45 450 if (!m_documentModified) {
Chris@45 451 //!!! this in subclass implementation?
Chris@45 452 setWindowTitle(tr("%1 (modified)").arg(windowTitle()));
Chris@45 453 }
Chris@45 454
Chris@45 455 m_documentModified = true;
Chris@45 456 updateMenuStates();
Chris@45 457 }
Chris@45 458
Chris@45 459 void
Chris@45 460 MainWindowBase::documentRestored()
Chris@45 461 {
Chris@45 462 // std::cerr << "MainWindowBase::documentRestored" << std::endl;
Chris@45 463
Chris@45 464 if (m_documentModified) {
Chris@45 465 //!!! this in subclass implementation?
Chris@45 466 QString wt(windowTitle());
Chris@45 467 wt.replace(tr(" (modified)"), "");
Chris@45 468 setWindowTitle(wt);
Chris@45 469 }
Chris@45 470
Chris@45 471 m_documentModified = false;
Chris@45 472 updateMenuStates();
Chris@45 473 }
Chris@45 474
Chris@45 475 void
Chris@45 476 MainWindowBase::playLoopToggled()
Chris@45 477 {
Chris@45 478 QAction *action = dynamic_cast<QAction *>(sender());
Chris@45 479
Chris@45 480 if (action) {
Chris@45 481 m_viewManager->setPlayLoopMode(action->isChecked());
Chris@45 482 } else {
Chris@45 483 m_viewManager->setPlayLoopMode(!m_viewManager->getPlayLoopMode());
Chris@45 484 }
Chris@45 485 }
Chris@45 486
Chris@45 487 void
Chris@45 488 MainWindowBase::playSelectionToggled()
Chris@45 489 {
Chris@45 490 QAction *action = dynamic_cast<QAction *>(sender());
Chris@45 491
Chris@45 492 if (action) {
Chris@45 493 m_viewManager->setPlaySelectionMode(action->isChecked());
Chris@45 494 } else {
Chris@45 495 m_viewManager->setPlaySelectionMode(!m_viewManager->getPlaySelectionMode());
Chris@45 496 }
Chris@45 497 }
Chris@45 498
Chris@45 499 void
Chris@45 500 MainWindowBase::playSoloToggled()
Chris@45 501 {
Chris@45 502 QAction *action = dynamic_cast<QAction *>(sender());
Chris@45 503
Chris@45 504 if (action) {
Chris@45 505 m_viewManager->setPlaySoloMode(action->isChecked());
Chris@45 506 } else {
Chris@45 507 m_viewManager->setPlaySoloMode(!m_viewManager->getPlaySoloMode());
Chris@45 508 }
Chris@45 509
Chris@45 510 if (m_viewManager->getPlaySoloMode()) {
Chris@45 511 currentPaneChanged(m_paneStack->getCurrentPane());
Chris@45 512 } else {
Chris@45 513 m_viewManager->setPlaybackModel(0);
Chris@45 514 if (m_playSource) {
Chris@45 515 m_playSource->clearSoloModelSet();
Chris@45 516 }
Chris@45 517 }
Chris@45 518 }
Chris@45 519
Chris@45 520 void
Chris@45 521 MainWindowBase::currentPaneChanged(Pane *p)
Chris@45 522 {
Chris@45 523 updateMenuStates();
Chris@45 524 updateVisibleRangeDisplay(p);
Chris@45 525
Chris@45 526 if (!p) return;
Chris@45 527
Chris@45 528 if (!(m_viewManager &&
Chris@45 529 m_playSource &&
Chris@45 530 m_viewManager->getPlaySoloMode())) {
Chris@45 531 if (m_viewManager) m_viewManager->setPlaybackModel(0);
Chris@45 532 return;
Chris@45 533 }
Chris@45 534
Chris@45 535 Model *prevPlaybackModel = m_viewManager->getPlaybackModel();
Chris@60 536
Chris@93 537 // What we want here is not the currently playing frame (unless we
Chris@93 538 // are about to clear out the audio playback buffers -- which may
Chris@93 539 // or may not be possible, depending on the audio driver). What
Chris@93 540 // we want is the frame that was last committed to the soundcard
Chris@93 541 // buffers, as the audio driver will continue playing up to that
Chris@93 542 // frame before switching to whichever one we decide we want to
Chris@93 543 // switch to, regardless of our efforts.
Chris@93 544
Chris@93 545 int frame = m_playSource->getCurrentBufferedFrame();
Chris@93 546
Chris@93 547 // std::cerr << "currentPaneChanged: current frame (in ref model) = " << frame << std::endl;
Chris@45 548
Chris@45 549 View::ModelSet soloModels = p->getModels();
Chris@45 550
Chris@57 551 View::ModelSet sources;
Chris@57 552 for (View::ModelSet::iterator mi = soloModels.begin();
Chris@57 553 mi != soloModels.end(); ++mi) {
Chris@190 554 // If a model in this pane is derived from something else,
Chris@190 555 // then we want to play that model as well -- if the model
Chris@190 556 // that's derived from it is not something that is itself
Chris@190 557 // individually playable (e.g. a waveform)
Chris@190 558 if (*mi &&
Chris@190 559 !dynamic_cast<RangeSummarisableTimeValueModel *>(*mi) &&
Chris@190 560 (*mi)->getSourceModel()) {
Chris@57 561 sources.insert((*mi)->getSourceModel());
Chris@57 562 }
Chris@57 563 }
Chris@57 564 for (View::ModelSet::iterator mi = sources.begin();
Chris@57 565 mi != sources.end(); ++mi) {
Chris@57 566 soloModels.insert(*mi);
Chris@57 567 }
Chris@57 568
Chris@60 569 //!!! Need an "atomic" way of telling the play source that the
Chris@60 570 //playback model has changed, and changing it on ViewManager --
Chris@60 571 //the play source should be making the setPlaybackModel call to
Chris@60 572 //ViewManager
Chris@60 573
Chris@45 574 for (View::ModelSet::iterator mi = soloModels.begin();
Chris@45 575 mi != soloModels.end(); ++mi) {
Chris@45 576 if (dynamic_cast<RangeSummarisableTimeValueModel *>(*mi)) {
Chris@45 577 m_viewManager->setPlaybackModel(*mi);
Chris@45 578 }
Chris@45 579 }
Chris@45 580
Chris@45 581 RangeSummarisableTimeValueModel *a =
Chris@45 582 dynamic_cast<RangeSummarisableTimeValueModel *>(prevPlaybackModel);
Chris@45 583 RangeSummarisableTimeValueModel *b =
Chris@45 584 dynamic_cast<RangeSummarisableTimeValueModel *>(m_viewManager->
Chris@45 585 getPlaybackModel());
Chris@45 586
Chris@45 587 m_playSource->setSoloModelSet(soloModels);
Chris@45 588
Chris@45 589 if (a && b && (a != b)) {
Chris@60 590 if (m_playSource->isPlaying()) m_playSource->play(frame);
Chris@45 591 }
Chris@45 592 }
Chris@45 593
Chris@45 594 void
Chris@45 595 MainWindowBase::currentLayerChanged(Pane *p, Layer *)
Chris@45 596 {
Chris@45 597 updateMenuStates();
Chris@45 598 updateVisibleRangeDisplay(p);
Chris@45 599 }
Chris@45 600
Chris@45 601 void
Chris@45 602 MainWindowBase::selectAll()
Chris@45 603 {
Chris@45 604 if (!getMainModel()) return;
Chris@45 605 m_viewManager->setSelection(Selection(getMainModel()->getStartFrame(),
Chris@45 606 getMainModel()->getEndFrame()));
Chris@45 607 }
Chris@45 608
Chris@45 609 void
Chris@45 610 MainWindowBase::selectToStart()
Chris@45 611 {
Chris@45 612 if (!getMainModel()) return;
Chris@45 613 m_viewManager->setSelection(Selection(getMainModel()->getStartFrame(),
Chris@45 614 m_viewManager->getGlobalCentreFrame()));
Chris@45 615 }
Chris@45 616
Chris@45 617 void
Chris@45 618 MainWindowBase::selectToEnd()
Chris@45 619 {
Chris@45 620 if (!getMainModel()) return;
Chris@45 621 m_viewManager->setSelection(Selection(m_viewManager->getGlobalCentreFrame(),
Chris@45 622 getMainModel()->getEndFrame()));
Chris@45 623 }
Chris@45 624
Chris@45 625 void
Chris@45 626 MainWindowBase::selectVisible()
Chris@45 627 {
Chris@45 628 Model *model = getMainModel();
Chris@45 629 if (!model) return;
Chris@45 630
Chris@45 631 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 632 if (!currentPane) return;
Chris@45 633
Chris@45 634 size_t startFrame, endFrame;
Chris@45 635
Chris@45 636 if (currentPane->getStartFrame() < 0) startFrame = 0;
Chris@45 637 else startFrame = currentPane->getStartFrame();
Chris@45 638
Chris@45 639 if (currentPane->getEndFrame() > model->getEndFrame()) endFrame = model->getEndFrame();
Chris@45 640 else endFrame = currentPane->getEndFrame();
Chris@45 641
Chris@45 642 m_viewManager->setSelection(Selection(startFrame, endFrame));
Chris@45 643 }
Chris@45 644
Chris@45 645 void
Chris@45 646 MainWindowBase::clearSelection()
Chris@45 647 {
Chris@45 648 m_viewManager->clearSelections();
Chris@45 649 }
Chris@45 650
Chris@45 651 void
Chris@45 652 MainWindowBase::cut()
Chris@45 653 {
Chris@45 654 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 655 if (!currentPane) return;
Chris@45 656
Chris@45 657 Layer *layer = currentPane->getSelectedLayer();
Chris@45 658 if (!layer) return;
Chris@45 659
Chris@45 660 Clipboard &clipboard = m_viewManager->getClipboard();
Chris@45 661 clipboard.clear();
Chris@45 662
Chris@45 663 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@45 664
Chris@45 665 CommandHistory::getInstance()->startCompoundOperation(tr("Cut"), true);
Chris@45 666
Chris@45 667 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@45 668 i != selections.end(); ++i) {
Chris@86 669 layer->copy(currentPane, *i, clipboard);
Chris@45 670 layer->deleteSelection(*i);
Chris@45 671 }
Chris@45 672
Chris@45 673 CommandHistory::getInstance()->endCompoundOperation();
Chris@45 674 }
Chris@45 675
Chris@45 676 void
Chris@45 677 MainWindowBase::copy()
Chris@45 678 {
Chris@45 679 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 680 if (!currentPane) return;
Chris@45 681
Chris@45 682 Layer *layer = currentPane->getSelectedLayer();
Chris@45 683 if (!layer) return;
Chris@45 684
Chris@45 685 Clipboard &clipboard = m_viewManager->getClipboard();
Chris@45 686 clipboard.clear();
Chris@45 687
Chris@45 688 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@45 689
Chris@45 690 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@45 691 i != selections.end(); ++i) {
Chris@86 692 layer->copy(currentPane, *i, clipboard);
Chris@45 693 }
Chris@45 694 }
Chris@45 695
Chris@45 696 void
Chris@45 697 MainWindowBase::paste()
Chris@45 698 {
Chris@215 699 pasteRelative(0);
Chris@215 700 }
Chris@215 701
Chris@215 702 void
Chris@215 703 MainWindowBase::pasteAtPlaybackPosition()
Chris@215 704 {
Chris@215 705 unsigned long pos = getFrame();
Chris@215 706 Clipboard &clipboard = m_viewManager->getClipboard();
Chris@215 707 if (!clipboard.empty()) {
Chris@215 708 long firstEventFrame = clipboard.getPoints()[0].getFrame();
Chris@215 709 long offset = 0;
Chris@215 710 if (firstEventFrame < 0) {
Chris@221 711 offset = (long)pos - firstEventFrame;
Chris@215 712 } else if (firstEventFrame < pos) {
Chris@221 713 offset = pos - (unsigned long)firstEventFrame;
Chris@215 714 } else {
Chris@221 715 offset = -((unsigned long)firstEventFrame - pos);
Chris@215 716 }
Chris@215 717 pasteRelative(offset);
Chris@215 718 }
Chris@215 719 }
Chris@215 720
Chris@215 721 void
Chris@215 722 MainWindowBase::pasteRelative(int offset)
Chris@215 723 {
Chris@45 724 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 725 if (!currentPane) return;
Chris@45 726
Chris@45 727 Layer *layer = currentPane->getSelectedLayer();
Chris@45 728
Chris@45 729 Clipboard &clipboard = m_viewManager->getClipboard();
Chris@87 730
Chris@98 731 bool inCompound = false;
Chris@87 732
Chris@87 733 if (!layer || !layer->isLayerEditable()) {
Chris@87 734
Chris@87 735 CommandHistory::getInstance()->startCompoundOperation
Chris@87 736 (tr("Paste"), true);
Chris@87 737
Chris@87 738 // no suitable current layer: create one of the most
Chris@87 739 // appropriate sort
Chris@87 740 LayerFactory::LayerType type =
Chris@87 741 LayerFactory::getInstance()->getLayerTypeForClipboardContents(clipboard);
Chris@87 742 layer = m_document->createEmptyLayer(type);
Chris@87 743
Chris@87 744 if (!layer) {
Chris@87 745 CommandHistory::getInstance()->endCompoundOperation();
Chris@87 746 return;
Chris@45 747 }
Chris@87 748
Chris@87 749 m_document->addLayerToView(currentPane, layer);
Chris@87 750 m_paneStack->setCurrentLayer(currentPane, layer);
Chris@87 751
Chris@87 752 inCompound = true;
Chris@45 753 }
Chris@45 754
Chris@215 755 layer->paste(currentPane, clipboard, offset, true);
Chris@45 756
Chris@87 757 if (inCompound) CommandHistory::getInstance()->endCompoundOperation();
Chris@45 758 }
Chris@45 759
Chris@45 760 void
Chris@45 761 MainWindowBase::deleteSelected()
Chris@45 762 {
Chris@45 763 if (m_paneStack->getCurrentPane() &&
Chris@45 764 m_paneStack->getCurrentPane()->getSelectedLayer()) {
Chris@45 765
Chris@45 766 Layer *layer = m_paneStack->getCurrentPane()->getSelectedLayer();
Chris@45 767
Chris@45 768 if (m_viewManager &&
Chris@45 769 (m_viewManager->getToolMode() == ViewManager::MeasureMode)) {
Chris@45 770
Chris@45 771 layer->deleteCurrentMeasureRect();
Chris@45 772
Chris@45 773 } else {
Chris@45 774
Chris@45 775 MultiSelection::SelectionList selections =
Chris@45 776 m_viewManager->getSelections();
Chris@45 777
Chris@45 778 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@45 779 i != selections.end(); ++i) {
Chris@45 780 layer->deleteSelection(*i);
Chris@45 781 }
Chris@45 782 }
Chris@45 783 }
Chris@45 784 }
Chris@45 785
Chris@161 786 // FrameTimer method
Chris@161 787
Chris@161 788 unsigned long
Chris@161 789 MainWindowBase::getFrame() const
Chris@161 790 {
Chris@161 791 if (m_playSource && m_playSource->isPlaying()) {
Chris@161 792 return m_playSource->getCurrentPlayingFrame();
Chris@161 793 } else {
Chris@161 794 return m_viewManager->getPlaybackFrame();
Chris@161 795 }
Chris@161 796 }
Chris@161 797
Chris@45 798 void
Chris@45 799 MainWindowBase::insertInstant()
Chris@45 800 {
Chris@161 801 insertInstantAt(getFrame());
Chris@45 802 }
Chris@45 803
Chris@45 804 void
Chris@45 805 MainWindowBase::insertInstantsAtBoundaries()
Chris@45 806 {
Chris@45 807 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@45 808 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@45 809 i != selections.end(); ++i) {
Chris@45 810 size_t start = i->getStartFrame();
Chris@45 811 size_t end = i->getEndFrame();
Chris@45 812 if (start != end) {
Chris@184 813 insertInstantAt(start);
Chris@184 814 insertInstantAt(end);
Chris@45 815 }
Chris@45 816 }
Chris@45 817 }
Chris@45 818
Chris@45 819 void
Chris@45 820 MainWindowBase::insertInstantAt(size_t frame)
Chris@45 821 {
Chris@45 822 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 823 if (!pane) {
Chris@45 824 return;
Chris@45 825 }
Chris@45 826
Chris@74 827 frame = pane->alignFromReference(frame);
Chris@74 828
Chris@45 829 Layer *layer = dynamic_cast<TimeInstantLayer *>
Chris@45 830 (pane->getSelectedLayer());
Chris@45 831
Chris@45 832 if (!layer) {
Chris@45 833 for (int i = pane->getLayerCount(); i > 0; --i) {
Chris@45 834 layer = dynamic_cast<TimeInstantLayer *>(pane->getLayer(i - 1));
Chris@45 835 if (layer) break;
Chris@45 836 }
Chris@45 837
Chris@45 838 if (!layer) {
Chris@45 839 CommandHistory::getInstance()->startCompoundOperation
Chris@45 840 (tr("Add Point"), true);
Chris@45 841 layer = m_document->createEmptyLayer(LayerFactory::TimeInstants);
Chris@45 842 if (layer) {
Chris@45 843 m_document->addLayerToView(pane, layer);
Chris@45 844 m_paneStack->setCurrentLayer(pane, layer);
Chris@45 845 }
Chris@45 846 CommandHistory::getInstance()->endCompoundOperation();
Chris@45 847 }
Chris@45 848 }
Chris@45 849
Chris@45 850 if (layer) {
Chris@45 851
Chris@45 852 Model *model = layer->getModel();
Chris@45 853 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
Chris@45 854 (model);
Chris@45 855
Chris@45 856 if (sodm) {
Chris@45 857 SparseOneDimensionalModel::Point point(frame, "");
Chris@45 858
Chris@45 859 SparseOneDimensionalModel::Point prevPoint(0);
Chris@45 860 bool havePrevPoint = false;
Chris@45 861
Chris@45 862 SparseOneDimensionalModel::EditCommand *command =
Chris@45 863 new SparseOneDimensionalModel::EditCommand(sodm, tr("Add Point"));
Chris@45 864
Chris@74 865 if (m_labeller->requiresPrevPoint()) {
Chris@45 866
Chris@45 867 SparseOneDimensionalModel::PointList prevPoints =
Chris@45 868 sodm->getPreviousPoints(frame);
Chris@45 869
Chris@45 870 if (!prevPoints.empty()) {
Chris@45 871 prevPoint = *prevPoints.begin();
Chris@45 872 havePrevPoint = true;
Chris@45 873 }
Chris@45 874 }
Chris@45 875
Chris@45 876 if (m_labeller) {
Chris@45 877
Chris@45 878 m_labeller->setSampleRate(sodm->getSampleRate());
Chris@45 879
Chris@74 880 if (m_labeller->actingOnPrevPoint()) {
Chris@45 881 command->deletePoint(prevPoint);
Chris@45 882 }
Chris@45 883
Chris@45 884 m_labeller->label<SparseOneDimensionalModel::Point>
Chris@45 885 (point, havePrevPoint ? &prevPoint : 0);
Chris@45 886
Chris@74 887 if (m_labeller->actingOnPrevPoint()) {
Chris@45 888 command->addPoint(prevPoint);
Chris@45 889 }
Chris@45 890 }
Chris@45 891
Chris@45 892 command->addPoint(point);
Chris@45 893
Chris@45 894 command->setName(tr("Add Point at %1 s")
Chris@45 895 .arg(RealTime::frame2RealTime
Chris@45 896 (frame,
Chris@45 897 sodm->getSampleRate())
Chris@45 898 .toText(false).c_str()));
Chris@45 899
Chris@108 900 Command *c = command->finish();
Chris@108 901 if (c) CommandHistory::getInstance()->addCommand(c, false);
Chris@45 902 }
Chris@45 903 }
Chris@45 904 }
Chris@45 905
Chris@45 906 void
Chris@184 907 MainWindowBase::insertItemAtSelection()
Chris@184 908 {
Chris@184 909 MultiSelection::SelectionList selections = m_viewManager->getSelections();
Chris@184 910 for (MultiSelection::SelectionList::iterator i = selections.begin();
Chris@184 911 i != selections.end(); ++i) {
Chris@184 912 size_t start = i->getStartFrame();
Chris@184 913 size_t end = i->getEndFrame();
Chris@184 914 if (start < end) {
Chris@184 915 insertItemAt(start, end - start);
Chris@184 916 }
Chris@184 917 }
Chris@184 918 }
Chris@184 919
Chris@184 920 void
Chris@184 921 MainWindowBase::insertItemAt(size_t frame, size_t duration)
Chris@184 922 {
Chris@184 923 Pane *pane = m_paneStack->getCurrentPane();
Chris@184 924 if (!pane) {
Chris@184 925 return;
Chris@184 926 }
Chris@184 927
Chris@184 928 // ugh!
Chris@184 929
Chris@184 930 size_t alignedStart = pane->alignFromReference(frame);
Chris@184 931 size_t alignedEnd = pane->alignFromReference(frame + duration);
Chris@184 932 if (alignedStart >= alignedEnd) return;
Chris@184 933 size_t alignedDuration = alignedEnd - alignedStart;
Chris@184 934
Chris@184 935 Command *c = 0;
Chris@184 936
Chris@184 937 QString name = tr("Add Item at %1 s")
Chris@184 938 .arg(RealTime::frame2RealTime
Chris@184 939 (alignedStart,
Chris@184 940 getMainModel()->getSampleRate())
Chris@184 941 .toText(false).c_str());
Chris@184 942
Chris@184 943 Layer *layer = pane->getSelectedLayer();
Chris@184 944 if (!layer) return;
Chris@184 945
Chris@184 946 RegionModel *rm = dynamic_cast<RegionModel *>(layer->getModel());
Chris@184 947 if (rm) {
Chris@184 948 RegionModel::Point point(alignedStart,
Chris@185 949 rm->getValueMaximum() + 1,
Chris@184 950 alignedDuration,
Chris@184 951 "");
Chris@184 952 RegionModel::EditCommand *command =
Chris@184 953 new RegionModel::EditCommand(rm, tr("Add Point"));
Chris@184 954 command->addPoint(point);
Chris@184 955 command->setName(name);
Chris@184 956 c = command->finish();
Chris@184 957 }
Chris@184 958
Chris@184 959 if (c) {
Chris@184 960 CommandHistory::getInstance()->addCommand(c, false);
Chris@184 961 return;
Chris@184 962 }
Chris@184 963
Chris@184 964 NoteModel *nm = dynamic_cast<NoteModel *>(layer->getModel());
Chris@184 965 if (nm) {
Chris@184 966 NoteModel::Point point(alignedStart,
Chris@184 967 rm->getValueMinimum(),
Chris@184 968 alignedDuration,
Chris@184 969 1.f,
Chris@184 970 "");
Chris@184 971 NoteModel::EditCommand *command =
Chris@184 972 new NoteModel::EditCommand(nm, tr("Add Point"));
Chris@184 973 command->addPoint(point);
Chris@184 974 command->setName(name);
Chris@184 975 c = command->finish();
Chris@184 976 }
Chris@184 977
Chris@184 978 if (c) {
Chris@184 979 CommandHistory::getInstance()->addCommand(c, false);
Chris@184 980 return;
Chris@184 981 }
Chris@184 982 }
Chris@184 983
Chris@184 984 void
Chris@45 985 MainWindowBase::renumberInstants()
Chris@45 986 {
Chris@45 987 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 988 if (!pane) return;
Chris@45 989
Chris@45 990 Layer *layer = dynamic_cast<TimeInstantLayer *>(pane->getSelectedLayer());
Chris@45 991 if (!layer) return;
Chris@45 992
Chris@45 993 MultiSelection ms(m_viewManager->getSelection());
Chris@45 994
Chris@45 995 Model *model = layer->getModel();
Chris@45 996 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
Chris@45 997 (model);
Chris@45 998 if (!sodm) return;
Chris@45 999
Chris@45 1000 if (!m_labeller) return;
Chris@45 1001
Chris@45 1002 Labeller labeller(*m_labeller);
Chris@45 1003 labeller.setSampleRate(sodm->getSampleRate());
Chris@45 1004
Chris@45 1005 // This uses a command
Chris@45 1006
Chris@45 1007 labeller.labelAll<SparseOneDimensionalModel::Point>(*sodm, &ms);
Chris@45 1008 }
Chris@45 1009
Chris@45 1010 MainWindowBase::FileOpenStatus
Chris@45 1011 MainWindowBase::open(QString fileOrUrl, AudioFileOpenMode mode)
Chris@45 1012 {
Chris@134 1013 ProgressDialog dialog(tr("Opening file or URL..."), true, 2000, this);
Chris@134 1014 connect(&dialog, SIGNAL(showing()), this, SIGNAL(hideSplash()));
Chris@109 1015 return open(FileSource(fileOrUrl, &dialog), mode);
Chris@45 1016 }
Chris@45 1017
Chris@45 1018 MainWindowBase::FileOpenStatus
Chris@45 1019 MainWindowBase::open(FileSource source, AudioFileOpenMode mode)
Chris@45 1020 {
Chris@45 1021 FileOpenStatus status;
Chris@45 1022
Chris@45 1023 if (!source.isAvailable()) return FileOpenFailed;
Chris@45 1024 source.waitForData();
Chris@45 1025
Chris@45 1026 bool canImportLayer = (getMainModel() != 0 &&
Chris@45 1027 m_paneStack != 0 &&
Chris@45 1028 m_paneStack->getCurrentPane() != 0);
Chris@45 1029
Chris@152 1030 bool rdf = (source.getExtension().toLower() == "rdf" ||
Chris@152 1031 source.getExtension().toLower() == "n3" ||
Chris@152 1032 source.getExtension().toLower() == "ttl");
Chris@152 1033
Chris@152 1034 bool audio = AudioFileReaderFactory::getKnownExtensions().contains
Chris@152 1035 (source.getExtension().toLower());
Chris@145 1036
Chris@145 1037 bool rdfSession = false;
Chris@145 1038 if (rdf) {
Chris@145 1039 RDFImporter::RDFDocumentType rdfType =
Chris@145 1040 RDFImporter::identifyDocumentType
Chris@145 1041 (QUrl::fromLocalFile(source.getLocalFilename()).toString());
Chris@148 1042 // std::cerr << "RDF type: " << (int)rdfType << std::endl;
Chris@145 1043 if (rdfType == RDFImporter::AudioRefAndAnnotations ||
Chris@145 1044 rdfType == RDFImporter::AudioRef) {
Chris@145 1045 rdfSession = true;
Chris@145 1046 } else if (rdfType == RDFImporter::NotRDF) {
Chris@145 1047 rdf = false;
Chris@145 1048 }
Chris@145 1049 }
Chris@145 1050
Chris@145 1051 if (rdf) {
Chris@145 1052 if (rdfSession) {
Chris@147 1053 bool cancel = false;
Chris@147 1054 if (!canImportLayer || shouldCreateNewSessionForRDFAudio(&cancel)) {
Chris@145 1055 return openSession(source);
Chris@147 1056 } else if (cancel) {
Chris@147 1057 return FileOpenCancelled;
Chris@145 1058 } else {
Chris@145 1059 return openLayer(source);
Chris@145 1060 }
Chris@145 1061 } else {
Chris@145 1062 if ((status = openSession(source)) != FileOpenFailed) {
Chris@145 1063 return status;
Chris@145 1064 } else if (!canImportLayer) {
Chris@145 1065 return FileOpenWrongMode;
Chris@145 1066 } else if ((status = openLayer(source)) != FileOpenFailed) {
Chris@145 1067 return status;
Chris@145 1068 } else {
Chris@145 1069 return FileOpenFailed;
Chris@145 1070 }
Chris@145 1071 }
Chris@145 1072 }
Chris@145 1073
Chris@152 1074 if (audio && (status = openAudio(source, mode)) != FileOpenFailed) {
Chris@45 1075 return status;
Chris@45 1076 } else if ((status = openSession(source)) != FileOpenFailed) {
Chris@45 1077 return status;
Chris@45 1078 } else if ((status = openPlaylist(source, mode)) != FileOpenFailed) {
Chris@45 1079 return status;
Chris@45 1080 } else if (!canImportLayer) {
Chris@45 1081 return FileOpenWrongMode;
Chris@45 1082 } else if ((status = openImage(source)) != FileOpenFailed) {
Chris@45 1083 return status;
Chris@45 1084 } else if ((status = openLayer(source)) != FileOpenFailed) {
Chris@45 1085 return status;
Chris@45 1086 } else {
Chris@45 1087 return FileOpenFailed;
Chris@45 1088 }
Chris@45 1089 }
Chris@45 1090
Chris@45 1091 MainWindowBase::FileOpenStatus
Chris@227 1092 MainWindowBase::openAudio(FileSource source, AudioFileOpenMode mode,
Chris@227 1093 QString templateName)
Chris@45 1094 {
Chris@132 1095 // std::cerr << "MainWindowBase::openAudio(" << source.getLocation().toStdString() << ")" << std::endl;
Chris@45 1096
Chris@222 1097 if (templateName == "") {
Chris@222 1098 templateName = m_defaultSessionTemplate;
Chris@222 1099 }
Chris@220 1100
Chris@223 1101 std::cerr << "template is: \"" << templateName.toStdString() << "\"" << std::endl;
Chris@223 1102
Chris@45 1103 if (!source.isAvailable()) return FileOpenFailed;
Chris@45 1104 source.waitForData();
Chris@45 1105
Chris@45 1106 m_openingAudioFile = true;
Chris@45 1107
Chris@45 1108 size_t rate = 0;
Chris@45 1109
Chris@45 1110 if (Preferences::getInstance()->getResampleOnLoad()) {
Chris@45 1111 rate = m_playSource->getSourceSampleRate();
Chris@45 1112 }
Chris@45 1113
Chris@45 1114 WaveFileModel *newModel = new WaveFileModel(source, rate);
Chris@45 1115
Chris@45 1116 if (!newModel->isOK()) {
Chris@45 1117 delete newModel;
Chris@45 1118 m_openingAudioFile = false;
Chris@45 1119 return FileOpenFailed;
Chris@45 1120 }
Chris@45 1121
Chris@132 1122 // std::cerr << "mode = " << mode << std::endl;
Chris@45 1123
Chris@45 1124 if (mode == AskUser) {
Chris@45 1125 if (getMainModel()) {
Chris@45 1126
Chris@147 1127 QSettings settings;
Chris@147 1128 settings.beginGroup("MainWindow");
Chris@221 1129 int lastMode = settings.value("lastaudioopenmode", 0).toBool();
Chris@147 1130 settings.endGroup();
Chris@221 1131 int imode = 0;
Chris@45 1132
Chris@45 1133 QStringList items;
Chris@221 1134 items << tr("Close the current session and start a new one")
Chris@221 1135 << tr("Replace the main audio file in this session")
Chris@221 1136 << tr("Add the audio file to this session");
Chris@45 1137
Chris@45 1138 bool ok = false;
Chris@45 1139 QString item = ListInputDialog::getItem
Chris@45 1140 (this, tr("Select target for import"),
Chris@221 1141 tr("<b>Select a target for import</b><p>You already have an audio file loaded.<br>What would you like to do with the new audio file?"),
Chris@221 1142 items, lastMode, &ok);
Chris@45 1143
Chris@45 1144 if (!ok || item.isEmpty()) {
Chris@45 1145 delete newModel;
Chris@45 1146 m_openingAudioFile = false;
Chris@45 1147 return FileOpenCancelled;
Chris@45 1148 }
Chris@45 1149
Chris@221 1150 for (int i = 0; i < items.size(); ++i) {
Chris@221 1151 if (item == items[i]) imode = i;
Chris@221 1152 }
Chris@221 1153
Chris@147 1154 settings.beginGroup("MainWindow");
Chris@221 1155 settings.setValue("lastaudioopenmode", imode);
Chris@147 1156 settings.endGroup();
Chris@45 1157
Chris@221 1158 mode = (AudioFileOpenMode)imode;
Chris@45 1159
Chris@45 1160 } else {
Chris@221 1161 // no main model: make a new session
Chris@221 1162 mode = ReplaceSession;
Chris@45 1163 }
Chris@45 1164 }
Chris@45 1165
Chris@45 1166 if (mode == ReplaceCurrentPane) {
Chris@45 1167
Chris@45 1168 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 1169 if (pane) {
Chris@45 1170 if (getMainModel()) {
Chris@45 1171 View::ModelSet models(pane->getModels());
Chris@45 1172 if (models.find(getMainModel()) != models.end()) {
Chris@221 1173 // Current pane contains main model: replace that
Chris@45 1174 mode = ReplaceMainModel;
Chris@45 1175 }
Chris@221 1176 // Otherwise the current pane has a non-default model,
Chris@221 1177 // which we will deal with later
Chris@45 1178 } else {
Chris@221 1179 // We have no main model, so start a new session with
Chris@221 1180 // optional template
Chris@221 1181 mode = ReplaceSession;
Chris@45 1182 }
Chris@45 1183 } else {
Chris@221 1184 // We seem to have no current pane! Oh well
Chris@45 1185 mode = CreateAdditionalModel;
Chris@45 1186 }
Chris@45 1187 }
Chris@45 1188
Chris@45 1189 if (mode == CreateAdditionalModel && !getMainModel()) {
Chris@221 1190 mode = ReplaceSession;
Chris@221 1191 }
Chris@221 1192
Chris@221 1193 bool loadedTemplate = false;
Chris@221 1194
Chris@221 1195 if (mode == ReplaceSession) {
Chris@221 1196
Chris@230 1197 if (templateName != "") {
Chris@230 1198 FileOpenStatus tplStatus = openSessionTemplate(templateName);
Chris@230 1199 if (tplStatus != FileOpenFailed) {
Chris@230 1200 std::cerr << "Template load succeeded" << std::endl;
Chris@230 1201 loadedTemplate = true;
Chris@221 1202 }
Chris@221 1203 }
Chris@221 1204
Chris@221 1205 if (!loadedTemplate) {
Chris@221 1206 closeSession();
Chris@221 1207 createDocument();
Chris@221 1208 }
Chris@221 1209
Chris@45 1210 mode = ReplaceMainModel;
Chris@45 1211 }
Chris@45 1212
Chris@164 1213 emit activity(tr("Import audio file \"%1\"").arg(source.getLocation()));
Chris@164 1214
Chris@45 1215 if (mode == ReplaceMainModel) {
Chris@45 1216
Chris@45 1217 Model *prevMain = getMainModel();
Chris@45 1218 if (prevMain) {
Chris@45 1219 m_playSource->removeModel(prevMain);
Chris@108 1220 PlayParameterRepository::getInstance()->removePlayable(prevMain);
Chris@45 1221 }
Chris@108 1222 PlayParameterRepository::getInstance()->addPlayable(newModel);
Chris@45 1223
Chris@45 1224 m_document->setMainModel(newModel);
Chris@45 1225
Chris@45 1226 setupMenus();
Chris@45 1227
dan@210 1228 if (loadedTemplate || (m_sessionFile == "")) {
Chris@45 1229 //!!! shouldn't be dealing directly with title from here -- call a method
Chris@57 1230 setWindowTitle(tr("%1: %2")
Chris@57 1231 .arg(QApplication::applicationName())
Chris@45 1232 .arg(source.getLocation()));
Chris@45 1233 CommandHistory::getInstance()->clear();
Chris@45 1234 CommandHistory::getInstance()->documentSaved();
Chris@45 1235 m_documentModified = false;
Chris@45 1236 } else {
Chris@57 1237 setWindowTitle(tr("%1: %2 [%3]")
Chris@57 1238 .arg(QApplication::applicationName())
Chris@45 1239 .arg(QFileInfo(m_sessionFile).fileName())
Chris@45 1240 .arg(source.getLocation()));
Chris@45 1241 if (m_documentModified) {
Chris@45 1242 m_documentModified = false;
Chris@45 1243 documentModified(); // so as to restore "(modified)" window title
Chris@45 1244 }
Chris@45 1245 }
Chris@45 1246
Chris@45 1247 if (!source.isRemote()) m_audioFile = source.getLocalFilename();
Chris@45 1248
Chris@45 1249 } else if (mode == CreateAdditionalModel) {
Chris@45 1250
Chris@45 1251 CommandHistory::getInstance()->startCompoundOperation
Chris@219 1252 (tr("Import \"%1\"").arg(source.getBasename()), true);
Chris@45 1253
Chris@45 1254 m_document->addImportedModel(newModel);
Chris@45 1255
Chris@45 1256 AddPaneCommand *command = new AddPaneCommand(this);
Chris@45 1257 CommandHistory::getInstance()->addCommand(command);
Chris@45 1258
Chris@45 1259 Pane *pane = command->getPane();
Chris@45 1260
Chris@47 1261 if (m_timeRulerLayer) {
Chris@47 1262 m_document->addLayerToView(pane, m_timeRulerLayer);
Chris@47 1263 }
Chris@45 1264
Chris@45 1265 Layer *newLayer = m_document->createImportedLayer(newModel);
Chris@45 1266
Chris@45 1267 if (newLayer) {
Chris@45 1268 m_document->addLayerToView(pane, newLayer);
Chris@45 1269 }
Chris@45 1270
Chris@45 1271 CommandHistory::getInstance()->endCompoundOperation();
Chris@45 1272
Chris@45 1273 } else if (mode == ReplaceCurrentPane) {
Chris@45 1274
Chris@45 1275 // We know there is a current pane, otherwise we would have
Chris@45 1276 // reset the mode to CreateAdditionalModel above; and we know
Chris@45 1277 // the current pane does not contain the main model, otherwise
Chris@45 1278 // we would have reset it to ReplaceMainModel. But we don't
Chris@45 1279 // know whether the pane contains a waveform model at all.
Chris@45 1280
Chris@45 1281 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 1282 Layer *replace = 0;
Chris@45 1283
Chris@45 1284 for (int i = 0; i < pane->getLayerCount(); ++i) {
Chris@45 1285 Layer *layer = pane->getLayer(i);
Chris@45 1286 if (dynamic_cast<WaveformLayer *>(layer)) {
Chris@45 1287 replace = layer;
Chris@45 1288 break;
Chris@45 1289 }
Chris@45 1290 }
Chris@45 1291
Chris@45 1292 CommandHistory::getInstance()->startCompoundOperation
Chris@219 1293 (tr("Import \"%1\"").arg(source.getBasename()), true);
Chris@45 1294
Chris@45 1295 m_document->addImportedModel(newModel);
Chris@45 1296
Chris@45 1297 if (replace) {
Chris@45 1298 m_document->removeLayerFromView(pane, replace);
Chris@45 1299 }
Chris@45 1300
Chris@45 1301 Layer *newLayer = m_document->createImportedLayer(newModel);
Chris@45 1302
Chris@45 1303 if (newLayer) {
Chris@45 1304 m_document->addLayerToView(pane, newLayer);
Chris@45 1305 }
Chris@45 1306
Chris@45 1307 CommandHistory::getInstance()->endCompoundOperation();
Chris@45 1308 }
Chris@45 1309
Chris@45 1310 updateMenuStates();
Chris@45 1311 m_recentFiles.addFile(source.getLocation());
Chris@45 1312 if (!source.isRemote()) {
Chris@45 1313 // for file dialog
Chris@45 1314 registerLastOpenedFilePath(FileFinder::AudioFile,
Chris@45 1315 source.getLocalFilename());
Chris@45 1316 }
Chris@45 1317 m_openingAudioFile = false;
Chris@45 1318
Chris@45 1319 currentPaneChanged(m_paneStack->getCurrentPane());
Chris@45 1320
Chris@45 1321 return FileOpenSucceeded;
Chris@45 1322 }
Chris@45 1323
Chris@45 1324 MainWindowBase::FileOpenStatus
Chris@45 1325 MainWindowBase::openPlaylist(FileSource source, AudioFileOpenMode mode)
Chris@45 1326 {
Chris@135 1327 std::cerr << "MainWindowBase::openPlaylist(" << source.getLocation().toStdString() << ")" << std::endl;
Chris@135 1328
Chris@45 1329 std::set<QString> extensions;
Chris@45 1330 PlaylistFileReader::getSupportedExtensions(extensions);
Chris@152 1331 QString extension = source.getExtension().toLower();
Chris@45 1332 if (extensions.find(extension) == extensions.end()) return FileOpenFailed;
Chris@45 1333
Chris@45 1334 if (!source.isAvailable()) return FileOpenFailed;
Chris@45 1335 source.waitForData();
Chris@45 1336
Chris@45 1337 PlaylistFileReader reader(source.getLocalFilename());
Chris@45 1338 if (!reader.isOK()) return FileOpenFailed;
Chris@45 1339
Chris@45 1340 PlaylistFileReader::Playlist playlist = reader.load();
Chris@45 1341
Chris@45 1342 bool someSuccess = false;
Chris@45 1343
Chris@45 1344 for (PlaylistFileReader::Playlist::const_iterator i = playlist.begin();
Chris@45 1345 i != playlist.end(); ++i) {
Chris@45 1346
Chris@134 1347 ProgressDialog dialog(tr("Opening playlist..."), true, 2000, this);
Chris@134 1348 connect(&dialog, SIGNAL(showing()), this, SIGNAL(hideSplash()));
Chris@109 1349 FileOpenStatus status = openAudio(FileSource(*i, &dialog), mode);
Chris@45 1350
Chris@45 1351 if (status == FileOpenCancelled) {
Chris@45 1352 return FileOpenCancelled;
Chris@45 1353 }
Chris@45 1354
Chris@45 1355 if (status == FileOpenSucceeded) {
Chris@45 1356 someSuccess = true;
Chris@45 1357 mode = CreateAdditionalModel;
Chris@45 1358 }
Chris@45 1359 }
Chris@45 1360
Chris@45 1361 if (someSuccess) return FileOpenSucceeded;
Chris@45 1362 else return FileOpenFailed;
Chris@45 1363 }
Chris@45 1364
Chris@45 1365 MainWindowBase::FileOpenStatus
Chris@45 1366 MainWindowBase::openLayer(FileSource source)
Chris@45 1367 {
Chris@135 1368 std::cerr << "MainWindowBase::openLayer(" << source.getLocation().toStdString() << ")" << std::endl;
Chris@135 1369
Chris@45 1370 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 1371
Chris@45 1372 if (!pane) {
Chris@45 1373 // shouldn't happen, as the menu action should have been disabled
Chris@45 1374 std::cerr << "WARNING: MainWindowBase::openLayer: no current pane" << std::endl;
Chris@45 1375 return FileOpenWrongMode;
Chris@45 1376 }
Chris@45 1377
Chris@45 1378 if (!getMainModel()) {
Chris@45 1379 // shouldn't happen, as the menu action should have been disabled
Chris@45 1380 std::cerr << "WARNING: MainWindowBase::openLayer: No main model -- hence no default sample rate available" << std::endl;
Chris@45 1381 return FileOpenWrongMode;
Chris@45 1382 }
Chris@45 1383
Chris@45 1384 if (!source.isAvailable()) return FileOpenFailed;
Chris@45 1385 source.waitForData();
Chris@45 1386
Chris@45 1387 QString path = source.getLocalFilename();
Chris@45 1388
Chris@145 1389 RDFImporter::RDFDocumentType rdfType =
Chris@145 1390 RDFImporter::identifyDocumentType(QUrl::fromLocalFile(path).toString());
Chris@145 1391
Chris@148 1392 // std::cerr << "RDF type: (in layer) " << (int) rdfType << std::endl;
Chris@148 1393
Chris@145 1394 if (rdfType != RDFImporter::NotRDF) {
Chris@145 1395
Chris@145 1396 return openLayersFromRDF(source);
Chris@134 1397
Chris@152 1398 } else if (source.getExtension().toLower() == "svl" ||
Chris@152 1399 (source.getExtension().toLower() == "xml" &&
Chris@140 1400 (SVFileReader::identifyXmlFile(source.getLocalFilename())
Chris@140 1401 == SVFileReader::SVLayerFile))) {
Chris@45 1402
Chris@45 1403 PaneCallback callback(this);
Chris@45 1404 QFile file(path);
Chris@45 1405
Chris@45 1406 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Chris@45 1407 std::cerr << "ERROR: MainWindowBase::openLayer("
Chris@45 1408 << source.getLocation().toStdString()
Chris@45 1409 << "): Failed to open file for reading" << std::endl;
Chris@45 1410 return FileOpenFailed;
Chris@45 1411 }
Chris@45 1412
Chris@45 1413 SVFileReader reader(m_document, callback, source.getLocation());
Chris@79 1414 connect
Chris@79 1415 (&reader, SIGNAL(modelRegenerationFailed(QString, QString, QString)),
Chris@79 1416 this, SLOT(modelRegenerationFailed(QString, QString, QString)));
Chris@79 1417 connect
Chris@79 1418 (&reader, SIGNAL(modelRegenerationWarning(QString, QString, QString)),
Chris@79 1419 this, SLOT(modelRegenerationWarning(QString, QString, QString)));
Chris@45 1420 reader.setCurrentPane(pane);
Chris@45 1421
Chris@45 1422 QXmlInputSource inputSource(&file);
Chris@45 1423 reader.parse(inputSource);
Chris@45 1424
Chris@45 1425 if (!reader.isOK()) {
Chris@45 1426 std::cerr << "ERROR: MainWindowBase::openLayer("
Chris@45 1427 << source.getLocation().toStdString()
Chris@45 1428 << "): Failed to read XML file: "
Chris@45 1429 << reader.getErrorString().toStdString() << std::endl;
Chris@45 1430 return FileOpenFailed;
Chris@45 1431 }
Chris@45 1432
Chris@164 1433 emit activity(tr("Import layer XML file \"%1\"").arg(source.getLocation()));
Chris@164 1434
Chris@45 1435 m_recentFiles.addFile(source.getLocation());
Chris@45 1436
Chris@45 1437 if (!source.isRemote()) {
Chris@45 1438 registerLastOpenedFilePath(FileFinder::LayerFile, path); // for file dialog
Chris@45 1439 }
Chris@45 1440
Chris@75 1441 return FileOpenSucceeded;
Chris@75 1442
Chris@45 1443 } else {
Chris@45 1444
Chris@45 1445 try {
Chris@45 1446
Chris@109 1447 MIDIFileImportDialog midiDlg(this);
Chris@109 1448
Chris@109 1449 Model *model = DataFileReaderFactory::loadNonCSV
Chris@109 1450 (path, &midiDlg, getMainModel()->getSampleRate());
Chris@45 1451
Chris@109 1452 if (!model) {
Chris@196 1453 CSVFormat format(path);
Chris@196 1454 format.setSampleRate(getMainModel()->getSampleRate());
Chris@196 1455 CSVFormatDialog *dialog = new CSVFormatDialog(this, format);
Chris@109 1456 if (dialog->exec() == QDialog::Accepted) {
Chris@109 1457 model = DataFileReaderFactory::loadCSV
Chris@109 1458 (path, dialog->getFormat(),
Chris@109 1459 getMainModel()->getSampleRate());
Chris@109 1460 }
Chris@109 1461 }
Chris@109 1462
Chris@45 1463 if (model) {
Chris@45 1464
Chris@45 1465 std::cerr << "MainWindowBase::openLayer: Have model" << std::endl;
Chris@45 1466
Chris@164 1467 emit activity(tr("Import MIDI file \"%1\"").arg(source.getLocation()));
Chris@164 1468
Chris@45 1469 Layer *newLayer = m_document->createImportedLayer(model);
Chris@45 1470
Chris@45 1471 if (newLayer) {
Chris@45 1472
Chris@45 1473 m_document->addLayerToView(pane, newLayer);
Chris@88 1474 m_paneStack->setCurrentLayer(pane, newLayer);
Chris@88 1475
Chris@45 1476 m_recentFiles.addFile(source.getLocation());
Chris@45 1477
Chris@45 1478 if (!source.isRemote()) {
Chris@45 1479 registerLastOpenedFilePath
Chris@45 1480 (FileFinder::LayerFile,
Chris@45 1481 path); // for file dialog
Chris@45 1482 }
Chris@88 1483
Chris@45 1484 return FileOpenSucceeded;
Chris@45 1485 }
Chris@45 1486 }
Chris@45 1487 } catch (DataFileReaderFactory::Exception e) {
Chris@45 1488 if (e == DataFileReaderFactory::ImportCancelled) {
Chris@45 1489 return FileOpenCancelled;
Chris@45 1490 }
Chris@45 1491 }
Chris@45 1492 }
Chris@45 1493
Chris@45 1494 return FileOpenFailed;
Chris@45 1495 }
Chris@45 1496
Chris@45 1497 MainWindowBase::FileOpenStatus
Chris@45 1498 MainWindowBase::openImage(FileSource source)
Chris@45 1499 {
Chris@135 1500 std::cerr << "MainWindowBase::openImage(" << source.getLocation().toStdString() << ")" << std::endl;
Chris@135 1501
Chris@45 1502 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 1503
Chris@45 1504 if (!pane) {
Chris@45 1505 // shouldn't happen, as the menu action should have been disabled
Chris@45 1506 std::cerr << "WARNING: MainWindowBase::openImage: no current pane" << std::endl;
Chris@45 1507 return FileOpenWrongMode;
Chris@45 1508 }
Chris@45 1509
Chris@45 1510 if (!m_document->getMainModel()) {
Chris@45 1511 return FileOpenWrongMode;
Chris@45 1512 }
Chris@45 1513
Chris@45 1514 bool newLayer = false;
Chris@45 1515
Chris@45 1516 ImageLayer *il = dynamic_cast<ImageLayer *>(pane->getSelectedLayer());
Chris@45 1517 if (!il) {
Chris@45 1518 for (int i = pane->getLayerCount()-1; i >= 0; --i) {
Chris@45 1519 il = dynamic_cast<ImageLayer *>(pane->getLayer(i));
Chris@45 1520 if (il) break;
Chris@45 1521 }
Chris@45 1522 }
Chris@45 1523 if (!il) {
Chris@45 1524 il = dynamic_cast<ImageLayer *>
Chris@45 1525 (m_document->createEmptyLayer(LayerFactory::Image));
Chris@45 1526 if (!il) return FileOpenFailed;
Chris@45 1527 newLayer = true;
Chris@45 1528 }
Chris@45 1529
Chris@45 1530 // We don't put the image file in Recent Files
Chris@45 1531
Chris@45 1532 std::cerr << "openImage: trying location \"" << source.getLocation().toStdString() << "\" in image layer" << std::endl;
Chris@45 1533
Chris@45 1534 if (!il->addImage(m_viewManager->getGlobalCentreFrame(), source.getLocation())) {
Chris@45 1535 if (newLayer) {
Chris@52 1536 m_document->deleteLayer(il); // also releases its model
Chris@45 1537 }
Chris@45 1538 return FileOpenFailed;
Chris@45 1539 } else {
Chris@45 1540 if (newLayer) {
Chris@45 1541 m_document->addLayerToView(pane, il);
Chris@45 1542 }
Chris@45 1543 m_paneStack->setCurrentLayer(pane, il);
Chris@45 1544 }
Chris@45 1545
Chris@45 1546 return FileOpenSucceeded;
Chris@45 1547 }
Chris@45 1548
Chris@45 1549 MainWindowBase::FileOpenStatus
Chris@45 1550 MainWindowBase::openSessionFile(QString fileOrUrl)
Chris@45 1551 {
Chris@134 1552 ProgressDialog dialog(tr("Opening session..."), true, 2000, this);
Chris@134 1553 connect(&dialog, SIGNAL(showing()), this, SIGNAL(hideSplash()));
Chris@109 1554 return openSession(FileSource(fileOrUrl, &dialog));
Chris@45 1555 }
Chris@45 1556
Chris@45 1557 MainWindowBase::FileOpenStatus
Chris@45 1558 MainWindowBase::openSession(FileSource source)
Chris@45 1559 {
Chris@135 1560 std::cerr << "MainWindowBase::openSession(" << source.getLocation().toStdString() << ")" << std::endl;
Chris@135 1561
Chris@45 1562 if (!source.isAvailable()) return FileOpenFailed;
Chris@145 1563 source.waitForData();
Chris@141 1564
Chris@227 1565 if (source.getExtension().toLower() != "sv") {
Chris@145 1566
Chris@145 1567 RDFImporter::RDFDocumentType rdfType =
Chris@145 1568 RDFImporter::identifyDocumentType
Chris@145 1569 (QUrl::fromLocalFile(source.getLocalFilename()).toString());
Chris@145 1570
Chris@148 1571 // std::cerr << "RDF type: " << (int)rdfType << std::endl;
Chris@148 1572
Chris@145 1573 if (rdfType == RDFImporter::AudioRefAndAnnotations ||
Chris@145 1574 rdfType == RDFImporter::AudioRef) {
Chris@145 1575 return openSessionFromRDF(source);
Chris@145 1576 } else if (rdfType != RDFImporter::NotRDF) {
Chris@145 1577 return FileOpenFailed;
Chris@145 1578 }
Chris@145 1579
Chris@152 1580 if (source.getExtension().toLower() == "xml") {
Chris@140 1581 if (SVFileReader::identifyXmlFile(source.getLocalFilename()) ==
Chris@140 1582 SVFileReader::SVSessionFile) {
Chris@140 1583 std::cerr << "This XML file looks like a session file, attempting to open it as a session" << std::endl;
Chris@140 1584 } else {
Chris@140 1585 return FileOpenFailed;
Chris@140 1586 }
Chris@140 1587 } else {
Chris@140 1588 return FileOpenFailed;
Chris@140 1589 }
Chris@140 1590 }
Chris@45 1591
Chris@140 1592 QXmlInputSource *inputSource = 0;
Chris@140 1593 BZipFileDevice *bzFile = 0;
Chris@140 1594 QFile *rawFile = 0;
Chris@140 1595
Chris@152 1596 if (source.getExtension().toLower() == "sv") {
Chris@140 1597 bzFile = new BZipFileDevice(source.getLocalFilename());
Chris@140 1598 if (!bzFile->open(QIODevice::ReadOnly)) {
Chris@140 1599 delete bzFile;
Chris@140 1600 return FileOpenFailed;
Chris@140 1601 }
Chris@140 1602 inputSource = new QXmlInputSource(bzFile);
Chris@140 1603 } else {
Chris@140 1604 rawFile = new QFile(source.getLocalFilename());
Chris@140 1605 inputSource = new QXmlInputSource(rawFile);
Chris@140 1606 }
Chris@140 1607
Chris@140 1608 if (!checkSaveModified()) {
Chris@140 1609 if (bzFile) bzFile->close();
Chris@140 1610 delete inputSource;
Chris@140 1611 delete bzFile;
Chris@140 1612 delete rawFile;
Chris@140 1613 return FileOpenCancelled;
Chris@140 1614 }
Chris@45 1615
Chris@45 1616 QString error;
Chris@45 1617 closeSession();
Chris@45 1618 createDocument();
Chris@45 1619
Chris@45 1620 PaneCallback callback(this);
Chris@45 1621 m_viewManager->clearSelections();
Chris@45 1622
Chris@45 1623 SVFileReader reader(m_document, callback, source.getLocation());
Chris@79 1624 connect
Chris@79 1625 (&reader, SIGNAL(modelRegenerationFailed(QString, QString, QString)),
Chris@79 1626 this, SLOT(modelRegenerationFailed(QString, QString, QString)));
Chris@79 1627 connect
Chris@79 1628 (&reader, SIGNAL(modelRegenerationWarning(QString, QString, QString)),
Chris@79 1629 this, SLOT(modelRegenerationWarning(QString, QString, QString)));
Chris@140 1630
Chris@140 1631 reader.parse(*inputSource);
Chris@45 1632
Chris@45 1633 if (!reader.isOK()) {
Chris@45 1634 error = tr("SV XML file read error:\n%1").arg(reader.getErrorString());
Chris@45 1635 }
Chris@45 1636
Chris@140 1637 if (bzFile) bzFile->close();
Chris@140 1638
Chris@140 1639 delete inputSource;
Chris@140 1640 delete bzFile;
Chris@140 1641 delete rawFile;
Chris@45 1642
Chris@45 1643 bool ok = (error == "");
Chris@45 1644
Chris@45 1645 if (ok) {
Chris@45 1646
Chris@164 1647 emit activity(tr("Import session file \"%1\"").arg(source.getLocation()));
Chris@164 1648
Chris@57 1649 setWindowTitle(tr("%1: %2")
Chris@57 1650 .arg(QApplication::applicationName())
Chris@45 1651 .arg(source.getLocation()));
Chris@45 1652
Chris@45 1653 if (!source.isRemote()) m_sessionFile = source.getLocalFilename();
Chris@45 1654
Chris@45 1655 setupMenus();
Chris@45 1656
Chris@45 1657 CommandHistory::getInstance()->clear();
Chris@45 1658 CommandHistory::getInstance()->documentSaved();
Chris@45 1659 m_documentModified = false;
Chris@45 1660 updateMenuStates();
Chris@45 1661
Chris@227 1662 m_recentFiles.addFile(source.getLocation());
Chris@45 1663
Chris@45 1664 if (!source.isRemote()) {
Chris@45 1665 // for file dialog
Chris@45 1666 registerLastOpenedFilePath(FileFinder::SessionFile,
Chris@227 1667 source.getLocalFilename());
Chris@45 1668 }
Chris@45 1669
Chris@45 1670 } else {
Chris@57 1671 setWindowTitle(QApplication::applicationName());
Chris@45 1672 }
Chris@45 1673
Chris@45 1674 return ok ? FileOpenSucceeded : FileOpenFailed;
Chris@45 1675 }
Chris@45 1676
Chris@141 1677 MainWindowBase::FileOpenStatus
Chris@230 1678 MainWindowBase::openSessionTemplate(QString templateName)
Chris@230 1679 {
Chris@230 1680 // Template in the user's template directory takes
Chris@230 1681 // priority over a bundled one; we don't unbundle, but
Chris@230 1682 // open directly from the bundled file (where applicable)
Chris@230 1683 ResourceFinder rf;
Chris@230 1684 QString tfile = rf.getResourcePath("templates", templateName + ".svt");
Chris@230 1685 if (tfile != "") {
Chris@230 1686 std::cerr << "SV loading template file " << tfile.toStdString() << std::endl;
Chris@230 1687 return openSessionTemplate(FileSource("file:" + tfile));
Chris@230 1688 } else {
Chris@230 1689 return FileOpenFailed;
Chris@230 1690 }
Chris@230 1691 }
Chris@230 1692
Chris@230 1693 MainWindowBase::FileOpenStatus
Chris@227 1694 MainWindowBase::openSessionTemplate(FileSource source)
Chris@227 1695 {
Chris@227 1696 std::cerr << "MainWindowBase::openSessionTemplate(" << source.getLocation().toStdString() << ")" << std::endl;
Chris@227 1697
Chris@227 1698 if (!source.isAvailable()) return FileOpenFailed;
Chris@227 1699 source.waitForData();
Chris@227 1700
Chris@227 1701 QXmlInputSource *inputSource = 0;
Chris@227 1702 QFile *file = 0;
Chris@227 1703 bool isTemplate = false;
Chris@227 1704
Chris@227 1705 file = new QFile(source.getLocalFilename());
Chris@227 1706 inputSource = new QXmlInputSource(file);
Chris@227 1707
Chris@227 1708 if (!checkSaveModified()) {
Chris@227 1709 delete inputSource;
Chris@227 1710 delete file;
Chris@227 1711 return FileOpenCancelled;
Chris@227 1712 }
Chris@227 1713
Chris@227 1714 QString error;
Chris@227 1715 closeSession();
Chris@227 1716 createDocument();
Chris@227 1717
Chris@227 1718 PaneCallback callback(this);
Chris@227 1719 m_viewManager->clearSelections();
Chris@227 1720
Chris@227 1721 SVFileReader reader(m_document, callback, source.getLocation());
Chris@227 1722 connect
Chris@227 1723 (&reader, SIGNAL(modelRegenerationFailed(QString, QString, QString)),
Chris@227 1724 this, SLOT(modelRegenerationFailed(QString, QString, QString)));
Chris@227 1725 connect
Chris@227 1726 (&reader, SIGNAL(modelRegenerationWarning(QString, QString, QString)),
Chris@227 1727 this, SLOT(modelRegenerationWarning(QString, QString, QString)));
Chris@227 1728
Chris@227 1729 reader.parse(*inputSource);
Chris@227 1730
Chris@227 1731 if (!reader.isOK()) {
Chris@227 1732 error = tr("SV XML file read error:\n%1").arg(reader.getErrorString());
Chris@227 1733 }
Chris@227 1734
Chris@227 1735 delete inputSource;
Chris@227 1736 delete file;
Chris@227 1737
Chris@227 1738 bool ok = (error == "");
Chris@227 1739
Chris@227 1740 setWindowTitle(QApplication::applicationName());
Chris@227 1741
Chris@227 1742 if (ok) {
Chris@227 1743
Chris@227 1744 emit activity(tr("Open session template \"%1\"").arg(source.getLocation()));
Chris@227 1745
Chris@227 1746 setupMenus();
Chris@227 1747
Chris@227 1748 CommandHistory::getInstance()->clear();
Chris@227 1749 CommandHistory::getInstance()->documentSaved();
Chris@227 1750 m_documentModified = false;
Chris@227 1751 updateMenuStates();
Chris@227 1752 }
Chris@227 1753
Chris@227 1754 return ok ? FileOpenSucceeded : FileOpenFailed;
Chris@227 1755 }
Chris@227 1756
Chris@227 1757 MainWindowBase::FileOpenStatus
Chris@141 1758 MainWindowBase::openSessionFromRDF(FileSource source)
Chris@141 1759 {
Chris@141 1760 std::cerr << "MainWindowBase::openSessionFromRDF(" << source.getLocation().toStdString() << ")" << std::endl;
Chris@141 1761
Chris@141 1762 if (!source.isAvailable()) return FileOpenFailed;
Chris@141 1763 source.waitForData();
Chris@141 1764
Chris@145 1765 if (!checkSaveModified()) {
Chris@145 1766 return FileOpenCancelled;
Chris@141 1767 }
Chris@143 1768
Chris@145 1769 closeSession();
Chris@145 1770 createDocument();
Chris@145 1771
Chris@145 1772 FileOpenStatus status = openLayersFromRDF(source);
Chris@141 1773
Chris@141 1774 setupMenus();
Chris@141 1775
Chris@141 1776 setWindowTitle(tr("%1: %2")
Chris@141 1777 .arg(QApplication::applicationName())
Chris@141 1778 .arg(source.getLocation()));
Chris@141 1779 CommandHistory::getInstance()->clear();
Chris@141 1780 CommandHistory::getInstance()->documentSaved();
Chris@141 1781 m_documentModified = false;
Chris@145 1782
Chris@145 1783 return status;
Chris@145 1784 }
Chris@145 1785
Chris@145 1786 MainWindowBase::FileOpenStatus
Chris@145 1787 MainWindowBase::openLayersFromRDF(FileSource source)
Chris@145 1788 {
Chris@145 1789 size_t rate = 0;
Chris@145 1790
Chris@186 1791 std::cerr << "MainWindowBase::openLayersFromRDF" << std::endl;
Chris@186 1792
Chris@145 1793 ProgressDialog dialog(tr("Importing from RDF..."), true, 2000, this);
Chris@145 1794 connect(&dialog, SIGNAL(showing()), this, SIGNAL(hideSplash()));
Chris@145 1795
Chris@145 1796 if (getMainModel()) {
Chris@145 1797 rate = getMainModel()->getSampleRate();
Chris@145 1798 } else if (Preferences::getInstance()->getResampleOnLoad()) {
Chris@145 1799 rate = m_playSource->getSourceSampleRate();
Chris@145 1800 }
Chris@145 1801
Chris@145 1802 RDFImporter importer
Chris@145 1803 (QUrl::fromLocalFile(source.getLocalFilename()).toString(), rate);
Chris@145 1804
Chris@145 1805 if (!importer.isOK()) {
Chris@147 1806 if (importer.getErrorString() != "") {
Chris@147 1807 QMessageBox::critical
Chris@147 1808 (this, tr("Failed to import RDF"),
Chris@147 1809 tr("<b>Failed to import RDF</b><p>Importing data from RDF document at \"%1\" failed: %2</p>")
Chris@147 1810 .arg(source.getLocation()).arg(importer.getErrorString()));
Chris@147 1811 }
Chris@145 1812 return FileOpenFailed;
Chris@145 1813 }
Chris@145 1814
Chris@145 1815 std::vector<Model *> models = importer.getDataModels(&dialog);
Chris@145 1816
Chris@145 1817 dialog.setMessage(tr("Importing from RDF..."));
Chris@145 1818
Chris@145 1819 if (models.empty()) {
Chris@186 1820 QMessageBox::critical
Chris@186 1821 (this, tr("Failed to import RDF"),
Chris@186 1822 tr("<b>Failed to import RDF</b><p>No suitable data models found for import from RDF document at \"%1\"</p>").arg(source.getLocation()));
Chris@145 1823 return FileOpenFailed;
Chris@145 1824 }
Chris@145 1825
Chris@164 1826 emit activity(tr("Import RDF document \"%1\"").arg(source.getLocation()));
Chris@164 1827
Chris@145 1828 std::set<Model *> added;
Chris@145 1829
Chris@221 1830 for (int i = 0; i < (int)models.size(); ++i) {
Chris@145 1831
Chris@145 1832 Model *m = models[i];
Chris@145 1833 WaveFileModel *w = dynamic_cast<WaveFileModel *>(m);
Chris@145 1834
Chris@145 1835 if (w) {
Chris@145 1836
Chris@145 1837 Pane *pane = addPaneToStack();
Chris@145 1838 Layer *layer = 0;
Chris@145 1839
Chris@145 1840 if (m_timeRulerLayer) {
Chris@145 1841 m_document->addLayerToView(pane, m_timeRulerLayer);
Chris@145 1842 }
Chris@145 1843
Chris@145 1844 if (!getMainModel()) {
Chris@145 1845 m_document->setMainModel(w);
Chris@145 1846 layer = m_document->createMainModelLayer(LayerFactory::Waveform);
Chris@145 1847 } else {
Chris@145 1848 layer = m_document->createImportedLayer(w);
Chris@145 1849 }
Chris@145 1850
Chris@145 1851 m_document->addLayerToView(pane, layer);
Chris@145 1852
Chris@145 1853 added.insert(w);
Chris@145 1854
Chris@221 1855 for (int j = 0; j < (int)models.size(); ++j) {
Chris@145 1856
Chris@145 1857 Model *dm = models[j];
Chris@145 1858
Chris@145 1859 if (dm == m) continue;
Chris@145 1860 if (dm->getSourceModel() != m) continue;
Chris@145 1861
Chris@145 1862 layer = m_document->createImportedLayer(dm);
Chris@145 1863
Chris@145 1864 if (layer->isLayerOpaque() ||
Chris@145 1865 dynamic_cast<Colour3DPlotLayer *>(layer)) {
Chris@145 1866
Chris@156 1867 // these always go in a new pane, with nothing
Chris@156 1868 // else going in the same pane
Chris@156 1869
Chris@145 1870 Pane *singleLayerPane = addPaneToStack();
Chris@145 1871 if (m_timeRulerLayer) {
Chris@145 1872 m_document->addLayerToView(singleLayerPane, m_timeRulerLayer);
Chris@145 1873 }
Chris@145 1874 m_document->addLayerToView(singleLayerPane, layer);
Chris@145 1875
Chris@156 1876 } else if (layer->getLayerColourSignificance() ==
Chris@156 1877 Layer::ColourHasMeaningfulValue) {
Chris@156 1878
Chris@156 1879 // these can go in a pane with something else, but
Chris@156 1880 // only if none of the something elses also have
Chris@156 1881 // this quality
Chris@156 1882
Chris@156 1883 bool needNewPane = false;
Chris@156 1884 for (int i = 0; i < pane->getLayerCount(); ++i) {
Chris@156 1885 Layer *otherLayer = pane->getLayer(i);
Chris@156 1886 if (otherLayer &&
Chris@156 1887 (otherLayer->getLayerColourSignificance() ==
Chris@156 1888 Layer::ColourHasMeaningfulValue)) {
Chris@156 1889 needNewPane = true;
Chris@156 1890 break;
Chris@156 1891 }
Chris@156 1892 }
Chris@156 1893 if (needNewPane) {
Chris@156 1894 pane = addPaneToStack();
Chris@156 1895 }
Chris@156 1896
Chris@156 1897 m_document->addLayerToView(pane, layer);
Chris@156 1898
Chris@145 1899 } else {
Chris@145 1900
Chris@145 1901 if (pane->getLayerCount() > 4) {
Chris@145 1902 pane = addPaneToStack();
Chris@145 1903 }
Chris@145 1904
Chris@145 1905 m_document->addLayerToView(pane, layer);
Chris@145 1906 }
Chris@145 1907
Chris@145 1908 added.insert(dm);
Chris@145 1909 }
Chris@145 1910 }
Chris@145 1911 }
Chris@145 1912
Chris@221 1913 for (int i = 0; i < (int)models.size(); ++i) {
Chris@145 1914
Chris@145 1915 Model *m = models[i];
Chris@145 1916
Chris@145 1917 if (added.find(m) == added.end()) {
Chris@145 1918
Chris@145 1919 Layer *layer = m_document->createImportedLayer(m);
Chris@145 1920 if (!layer) return FileOpenFailed;
Chris@145 1921
Chris@145 1922 Pane *singleLayerPane = addPaneToStack();
Chris@145 1923 if (m_timeRulerLayer) {
Chris@145 1924 m_document->addLayerToView(singleLayerPane, m_timeRulerLayer);
Chris@145 1925 }
Chris@145 1926 m_document->addLayerToView(singleLayerPane, layer);
Chris@145 1927 }
Chris@145 1928 }
Chris@145 1929
Chris@145 1930 m_recentFiles.addFile(source.getLocation());
Chris@145 1931 return FileOpenSucceeded;
Chris@141 1932 }
Chris@141 1933
Chris@45 1934 void
Chris@45 1935 MainWindowBase::createPlayTarget()
Chris@45 1936 {
Chris@45 1937 if (m_playTarget) return;
Chris@45 1938
Chris@126 1939 QSettings settings;
Chris@126 1940 settings.beginGroup("Preferences");
Chris@126 1941 QString targetName = settings.value("audio-target", "").toString();
Chris@126 1942 settings.endGroup();
Chris@126 1943
Chris@126 1944 AudioTargetFactory *factory = AudioTargetFactory::getInstance();
Chris@126 1945
Chris@126 1946 factory->setDefaultCallbackTarget(targetName);
Chris@126 1947 m_playTarget = factory->createCallbackTarget(m_playSource);
Chris@126 1948
Chris@45 1949 if (!m_playTarget) {
Chris@104 1950 emit hideSplash();
Chris@126 1951
Chris@126 1952 if (factory->isAutoCallbackTarget(targetName)) {
Chris@128 1953 QMessageBox::warning
Chris@45 1954 (this, tr("Couldn't open audio device"),
Chris@126 1955 tr("<b>No audio available</b><p>Could not open an audio device for playback.<p>Automatic audio device detection failed. Audio playback will not be available during this session.</p>"),
Chris@45 1956 QMessageBox::Ok);
Chris@126 1957 } else {
Chris@126 1958 QMessageBox::warning
Chris@126 1959 (this, tr("Couldn't open audio device"),
Chris@126 1960 tr("<b>No audio available</b><p>Failed to open your preferred audio device (\"%1\").<p>Audio playback will not be available during this session.</p>")
Chris@126 1961 .arg(factory->getCallbackTargetDescription(targetName)),
Chris@126 1962 QMessageBox::Ok);
Chris@126 1963 }
Chris@45 1964 }
Chris@45 1965 }
Chris@45 1966
Chris@45 1967 WaveFileModel *
Chris@45 1968 MainWindowBase::getMainModel()
Chris@45 1969 {
Chris@45 1970 if (!m_document) return 0;
Chris@45 1971 return m_document->getMainModel();
Chris@45 1972 }
Chris@45 1973
Chris@45 1974 const WaveFileModel *
Chris@45 1975 MainWindowBase::getMainModel() const
Chris@45 1976 {
Chris@45 1977 if (!m_document) return 0;
Chris@45 1978 return m_document->getMainModel();
Chris@45 1979 }
Chris@45 1980
Chris@45 1981 void
Chris@45 1982 MainWindowBase::createDocument()
Chris@45 1983 {
Chris@45 1984 m_document = new Document;
Chris@45 1985
Chris@45 1986 connect(m_document, SIGNAL(layerAdded(Layer *)),
Chris@45 1987 this, SLOT(layerAdded(Layer *)));
Chris@45 1988 connect(m_document, SIGNAL(layerRemoved(Layer *)),
Chris@45 1989 this, SLOT(layerRemoved(Layer *)));
Chris@45 1990 connect(m_document, SIGNAL(layerAboutToBeDeleted(Layer *)),
Chris@45 1991 this, SLOT(layerAboutToBeDeleted(Layer *)));
Chris@45 1992 connect(m_document, SIGNAL(layerInAView(Layer *, bool)),
Chris@45 1993 this, SLOT(layerInAView(Layer *, bool)));
Chris@45 1994
Chris@45 1995 connect(m_document, SIGNAL(modelAdded(Model *)),
Chris@45 1996 this, SLOT(modelAdded(Model *)));
Chris@45 1997 connect(m_document, SIGNAL(mainModelChanged(WaveFileModel *)),
Chris@45 1998 this, SLOT(mainModelChanged(WaveFileModel *)));
Chris@45 1999 connect(m_document, SIGNAL(modelAboutToBeDeleted(Model *)),
Chris@45 2000 this, SLOT(modelAboutToBeDeleted(Model *)));
Chris@45 2001
Chris@78 2002 connect(m_document, SIGNAL(modelGenerationFailed(QString, QString)),
Chris@78 2003 this, SLOT(modelGenerationFailed(QString, QString)));
Chris@78 2004 connect(m_document, SIGNAL(modelRegenerationWarning(QString, QString, QString)),
Chris@78 2005 this, SLOT(modelRegenerationWarning(QString, QString, QString)));
Chris@78 2006 connect(m_document, SIGNAL(modelGenerationFailed(QString, QString)),
Chris@78 2007 this, SLOT(modelGenerationFailed(QString, QString)));
Chris@78 2008 connect(m_document, SIGNAL(modelRegenerationWarning(QString, QString, QString)),
Chris@78 2009 this, SLOT(modelRegenerationWarning(QString, QString, QString)));
Chris@78 2010 connect(m_document, SIGNAL(alignmentFailed(QString, QString)),
Chris@78 2011 this, SLOT(alignmentFailed(QString, QString)));
Chris@160 2012
Chris@160 2013 emit replacedDocument();
Chris@45 2014 }
Chris@45 2015
Chris@45 2016 bool
Chris@45 2017 MainWindowBase::saveSessionFile(QString path)
Chris@45 2018 {
Chris@217 2019 try {
Chris@217 2020
Chris@217 2021 TempWriteFile temp(path);
Chris@217 2022
Chris@217 2023 BZipFileDevice bzFile(temp.getTemporaryFilename());
Chris@217 2024 if (!bzFile.open(QIODevice::WriteOnly)) {
Chris@217 2025 std::cerr << "Failed to open session file \""
Chris@217 2026 << temp.getTemporaryFilename().toStdString()
Chris@217 2027 << "\" for writing: "
Chris@217 2028 << bzFile.errorString().toStdString() << std::endl;
Chris@217 2029 return false;
Chris@217 2030 }
Chris@217 2031
Chris@217 2032 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
Chris@217 2033
Chris@217 2034 QTextStream out(&bzFile);
Chris@226 2035 toXml(out, false);
Chris@217 2036 out.flush();
Chris@217 2037
Chris@217 2038 QApplication::restoreOverrideCursor();
Chris@217 2039
Chris@217 2040 if (!bzFile.isOK()) {
Chris@217 2041 QMessageBox::critical(this, tr("Failed to write file"),
Chris@217 2042 tr("<b>Save failed</b><p>Failed to write to file \"%1\": %2")
Chris@217 2043 .arg(path).arg(bzFile.errorString()));
Chris@217 2044 bzFile.close();
Chris@217 2045 return false;
Chris@217 2046 }
Chris@217 2047
Chris@217 2048 bzFile.close();
Chris@217 2049 temp.moveToTarget();
Chris@217 2050 return true;
Chris@217 2051
Chris@217 2052 } catch (FileOperationFailed &f) {
Chris@217 2053
Chris@217 2054 QMessageBox::critical(this, tr("Failed to write file"),
Chris@217 2055 tr("<b>Save failed</b><p>Failed to write to file \"%1\": %2")
Chris@217 2056 .arg(path).arg(f.what()));
Chris@45 2057 return false;
Chris@45 2058 }
Chris@45 2059 }
Chris@45 2060
Chris@224 2061 bool
Chris@224 2062 MainWindowBase::saveSessionTemplate(QString path)
Chris@224 2063 {
Chris@224 2064 try {
Chris@224 2065
Chris@224 2066 TempWriteFile temp(path);
Chris@224 2067
Chris@224 2068 QFile file(temp.getTemporaryFilename());
Chris@224 2069 if (!file.open(QIODevice::WriteOnly)) {
Chris@224 2070 std::cerr << "Failed to open session template file \""
Chris@224 2071 << temp.getTemporaryFilename().toStdString()
Chris@224 2072 << "\" for writing: "
Chris@224 2073 << file.errorString().toStdString() << std::endl;
Chris@224 2074 return false;
Chris@224 2075 }
Chris@224 2076
Chris@224 2077 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
Chris@224 2078
Chris@224 2079 QTextStream out(&file);
Chris@226 2080 toXml(out, true);
Chris@224 2081 out.flush();
Chris@224 2082
Chris@224 2083 QApplication::restoreOverrideCursor();
Chris@224 2084
Chris@224 2085 file.close();
Chris@224 2086 temp.moveToTarget();
Chris@224 2087 return true;
Chris@224 2088
Chris@224 2089 } catch (FileOperationFailed &f) {
Chris@224 2090
Chris@224 2091 QMessageBox::critical(this, tr("Failed to write file"),
Chris@224 2092 tr("<b>Save failed</b><p>Failed to write to file \"%1\": %2")
Chris@224 2093 .arg(path).arg(f.what()));
Chris@224 2094 return false;
Chris@224 2095 }
Chris@224 2096 }
Chris@224 2097
Chris@45 2098 void
Chris@226 2099 MainWindowBase::toXml(QTextStream &out, bool asTemplate)
Chris@45 2100 {
Chris@45 2101 QString indent(" ");
Chris@45 2102
Chris@45 2103 out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
Chris@45 2104 out << "<!DOCTYPE sonic-visualiser>\n";
Chris@45 2105 out << "<sv>\n";
Chris@45 2106
Chris@226 2107 if (asTemplate) {
Chris@226 2108 m_document->toXmlAsTemplate(out, "", "");
Chris@226 2109 } else {
Chris@226 2110 m_document->toXml(out, "", "");
Chris@226 2111 }
Chris@45 2112
Chris@45 2113 out << "<display>\n";
Chris@45 2114
Chris@45 2115 out << QString(" <window width=\"%1\" height=\"%2\"/>\n")
Chris@45 2116 .arg(width()).arg(height());
Chris@45 2117
Chris@45 2118 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@45 2119
Chris@45 2120 Pane *pane = m_paneStack->getPane(i);
Chris@45 2121
Chris@45 2122 if (pane) {
Chris@45 2123 pane->toXml(out, indent);
Chris@45 2124 }
Chris@45 2125 }
Chris@45 2126
Chris@45 2127 out << "</display>\n";
Chris@45 2128
Chris@45 2129 m_viewManager->getSelection().toXml(out);
Chris@45 2130
Chris@45 2131 out << "</sv>\n";
Chris@45 2132 }
Chris@45 2133
Chris@45 2134 Pane *
Chris@45 2135 MainWindowBase::addPaneToStack()
Chris@45 2136 {
Chris@45 2137 AddPaneCommand *command = new AddPaneCommand(this);
Chris@45 2138 CommandHistory::getInstance()->addCommand(command);
Chris@57 2139 Pane *pane = command->getPane();
Chris@57 2140 return pane;
Chris@45 2141 }
Chris@45 2142
Chris@45 2143 void
Chris@45 2144 MainWindowBase::zoomIn()
Chris@45 2145 {
Chris@45 2146 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 2147 if (currentPane) currentPane->zoom(true);
Chris@45 2148 }
Chris@45 2149
Chris@45 2150 void
Chris@45 2151 MainWindowBase::zoomOut()
Chris@45 2152 {
Chris@45 2153 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 2154 if (currentPane) currentPane->zoom(false);
Chris@45 2155 }
Chris@45 2156
Chris@45 2157 void
Chris@45 2158 MainWindowBase::zoomToFit()
Chris@45 2159 {
Chris@45 2160 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 2161 if (!currentPane) return;
Chris@45 2162
Chris@45 2163 Model *model = getMainModel();
Chris@45 2164 if (!model) return;
Chris@45 2165
Chris@45 2166 size_t start = model->getStartFrame();
Chris@45 2167 size_t end = model->getEndFrame();
Chris@60 2168 if (m_playSource) end = std::max(end, m_playSource->getPlayEndFrame());
Chris@45 2169 size_t pixels = currentPane->width();
Chris@45 2170
Chris@45 2171 size_t sw = currentPane->getVerticalScaleWidth();
Chris@45 2172 if (pixels > sw * 2) pixels -= sw * 2;
Chris@45 2173 else pixels = 1;
Chris@45 2174 if (pixels > 4) pixels -= 4;
Chris@45 2175
Chris@45 2176 size_t zoomLevel = (end - start) / pixels;
Chris@150 2177 if (zoomLevel < 1) zoomLevel = 1;
Chris@45 2178
Chris@45 2179 currentPane->setZoomLevel(zoomLevel);
Chris@45 2180 currentPane->setCentreFrame((start + end) / 2);
Chris@45 2181 }
Chris@45 2182
Chris@45 2183 void
Chris@45 2184 MainWindowBase::zoomDefault()
Chris@45 2185 {
Chris@45 2186 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 2187 if (currentPane) currentPane->setZoomLevel(1024);
Chris@45 2188 }
Chris@45 2189
Chris@45 2190 void
Chris@45 2191 MainWindowBase::scrollLeft()
Chris@45 2192 {
Chris@45 2193 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 2194 if (currentPane) currentPane->scroll(false, false);
Chris@45 2195 }
Chris@45 2196
Chris@45 2197 void
Chris@45 2198 MainWindowBase::jumpLeft()
Chris@45 2199 {
Chris@45 2200 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 2201 if (currentPane) currentPane->scroll(false, true);
Chris@45 2202 }
Chris@45 2203
Chris@45 2204 void
Chris@162 2205 MainWindowBase::peekLeft()
Chris@162 2206 {
Chris@162 2207 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@162 2208 if (currentPane) currentPane->scroll(false, false, false);
Chris@162 2209 }
Chris@162 2210
Chris@162 2211 void
Chris@45 2212 MainWindowBase::scrollRight()
Chris@45 2213 {
Chris@45 2214 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 2215 if (currentPane) currentPane->scroll(true, false);
Chris@45 2216 }
Chris@45 2217
Chris@45 2218 void
Chris@45 2219 MainWindowBase::jumpRight()
Chris@45 2220 {
Chris@45 2221 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@45 2222 if (currentPane) currentPane->scroll(true, true);
Chris@45 2223 }
Chris@45 2224
Chris@45 2225 void
Chris@162 2226 MainWindowBase::peekRight()
Chris@162 2227 {
Chris@162 2228 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@162 2229 if (currentPane) currentPane->scroll(true, false, false);
Chris@162 2230 }
Chris@162 2231
Chris@162 2232 void
Chris@45 2233 MainWindowBase::showNoOverlays()
Chris@45 2234 {
Chris@45 2235 m_viewManager->setOverlayMode(ViewManager::NoOverlays);
Chris@45 2236 }
Chris@45 2237
Chris@45 2238 void
Chris@45 2239 MainWindowBase::showMinimalOverlays()
Chris@45 2240 {
Chris@45 2241 m_viewManager->setOverlayMode(ViewManager::MinimalOverlays);
Chris@45 2242 }
Chris@45 2243
Chris@45 2244 void
Chris@45 2245 MainWindowBase::showStandardOverlays()
Chris@45 2246 {
Chris@45 2247 m_viewManager->setOverlayMode(ViewManager::StandardOverlays);
Chris@45 2248 }
Chris@45 2249
Chris@45 2250 void
Chris@45 2251 MainWindowBase::showAllOverlays()
Chris@45 2252 {
Chris@45 2253 m_viewManager->setOverlayMode(ViewManager::AllOverlays);
Chris@45 2254 }
Chris@45 2255
Chris@45 2256 void
Chris@211 2257 MainWindowBase::toggleTimeRulers()
Chris@211 2258 {
Chris@211 2259 bool haveRulers = false;
Chris@211 2260 bool someHidden = false;
Chris@211 2261
Chris@211 2262 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@211 2263
Chris@211 2264 Pane *pane = m_paneStack->getPane(i);
Chris@211 2265 if (!pane) continue;
Chris@211 2266
Chris@211 2267 for (int j = 0; j < pane->getLayerCount(); ++j) {
Chris@211 2268
Chris@211 2269 Layer *layer = pane->getLayer(j);
Chris@211 2270 if (!dynamic_cast<TimeRulerLayer *>(layer)) continue;
Chris@211 2271
Chris@211 2272 haveRulers = true;
Chris@211 2273 if (layer->isLayerDormant(pane)) someHidden = true;
Chris@211 2274 }
Chris@211 2275 }
Chris@211 2276
Chris@211 2277 if (haveRulers) {
Chris@211 2278
Chris@211 2279 bool show = someHidden;
Chris@211 2280
Chris@211 2281 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@211 2282
Chris@211 2283 Pane *pane = m_paneStack->getPane(i);
Chris@211 2284 if (!pane) continue;
Chris@211 2285
Chris@211 2286 for (int j = 0; j < pane->getLayerCount(); ++j) {
Chris@211 2287
Chris@211 2288 Layer *layer = pane->getLayer(j);
Chris@211 2289 if (!dynamic_cast<TimeRulerLayer *>(layer)) continue;
Chris@211 2290
Chris@211 2291 layer->showLayer(pane, show);
Chris@211 2292 }
Chris@211 2293 }
Chris@211 2294 }
Chris@211 2295 }
Chris@211 2296
Chris@211 2297 void
Chris@45 2298 MainWindowBase::toggleZoomWheels()
Chris@45 2299 {
Chris@45 2300 if (m_viewManager->getZoomWheelsEnabled()) {
Chris@45 2301 m_viewManager->setZoomWheelsEnabled(false);
Chris@45 2302 } else {
Chris@45 2303 m_viewManager->setZoomWheelsEnabled(true);
Chris@45 2304 }
Chris@45 2305 }
Chris@45 2306
Chris@45 2307 void
Chris@45 2308 MainWindowBase::togglePropertyBoxes()
Chris@45 2309 {
Chris@45 2310 if (m_paneStack->getLayoutStyle() == PaneStack::NoPropertyStacks) {
Chris@45 2311 if (Preferences::getInstance()->getPropertyBoxLayout() ==
Chris@45 2312 Preferences::VerticallyStacked) {
Chris@45 2313 m_paneStack->setLayoutStyle(PaneStack::PropertyStackPerPaneLayout);
Chris@45 2314 } else {
Chris@45 2315 m_paneStack->setLayoutStyle(PaneStack::SinglePropertyStackLayout);
Chris@45 2316 }
Chris@45 2317 } else {
Chris@45 2318 m_paneStack->setLayoutStyle(PaneStack::NoPropertyStacks);
Chris@45 2319 }
Chris@45 2320 }
Chris@45 2321
Chris@45 2322 void
Chris@45 2323 MainWindowBase::toggleStatusBar()
Chris@45 2324 {
Chris@45 2325 QSettings settings;
Chris@45 2326 settings.beginGroup("MainWindow");
Chris@45 2327 bool sb = settings.value("showstatusbar", true).toBool();
Chris@45 2328
Chris@45 2329 if (sb) {
Chris@45 2330 statusBar()->hide();
Chris@45 2331 } else {
Chris@45 2332 statusBar()->show();
Chris@45 2333 }
Chris@45 2334
Chris@45 2335 settings.setValue("showstatusbar", !sb);
Chris@45 2336
Chris@45 2337 settings.endGroup();
Chris@45 2338 }
Chris@45 2339
Chris@45 2340 void
Chris@45 2341 MainWindowBase::preferenceChanged(PropertyContainer::PropertyName name)
Chris@45 2342 {
Chris@45 2343 if (name == "Property Box Layout") {
Chris@45 2344 if (m_paneStack->getLayoutStyle() != PaneStack::NoPropertyStacks) {
Chris@45 2345 if (Preferences::getInstance()->getPropertyBoxLayout() ==
Chris@45 2346 Preferences::VerticallyStacked) {
Chris@45 2347 m_paneStack->setLayoutStyle(PaneStack::PropertyStackPerPaneLayout);
Chris@45 2348 } else {
Chris@45 2349 m_paneStack->setLayoutStyle(PaneStack::SinglePropertyStackLayout);
Chris@45 2350 }
Chris@45 2351 }
Chris@45 2352 } else if (name == "Background Mode" && m_viewManager) {
Chris@45 2353 Preferences::BackgroundMode mode =
Chris@45 2354 Preferences::getInstance()->getBackgroundMode();
Chris@45 2355 if (mode == Preferences::BackgroundFromTheme) {
Chris@45 2356 m_viewManager->setGlobalDarkBackground(m_initialDarkBackground);
Chris@45 2357 } else if (mode == Preferences::DarkBackground) {
Chris@45 2358 m_viewManager->setGlobalDarkBackground(true);
Chris@45 2359 } else {
Chris@45 2360 m_viewManager->setGlobalDarkBackground(false);
Chris@45 2361 }
Chris@45 2362 }
Chris@45 2363 }
Chris@45 2364
Chris@45 2365 void
Chris@45 2366 MainWindowBase::play()
Chris@45 2367 {
Chris@45 2368 if (m_playSource->isPlaying()) {
Chris@45 2369 stop();
Chris@45 2370 } else {
Chris@45 2371 playbackFrameChanged(m_viewManager->getPlaybackFrame());
Chris@45 2372 m_playSource->play(m_viewManager->getPlaybackFrame());
Chris@45 2373 }
Chris@45 2374 }
Chris@45 2375
Chris@45 2376 void
Chris@45 2377 MainWindowBase::ffwd()
Chris@45 2378 {
Chris@45 2379 if (!getMainModel()) return;
Chris@45 2380
Chris@45 2381 int frame = m_viewManager->getPlaybackFrame();
Chris@45 2382 ++frame;
Chris@45 2383
Chris@85 2384 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 2385 Layer *layer = getSnapLayer();
Chris@45 2386 size_t sr = getMainModel()->getSampleRate();
Chris@45 2387
Chris@45 2388 if (!layer) {
Chris@45 2389
Chris@45 2390 frame = RealTime::realTime2Frame
Chris@45 2391 (RealTime::frame2RealTime(frame, sr) + RealTime(2, 0), sr);
Chris@45 2392 if (frame > int(getMainModel()->getEndFrame())) {
Chris@45 2393 frame = getMainModel()->getEndFrame();
Chris@45 2394 }
Chris@45 2395
Chris@45 2396 } else {
Chris@45 2397
Chris@45 2398 size_t resolution = 0;
Chris@166 2399 if (pane) frame = pane->alignFromReference(frame);
Chris@85 2400 if (layer->snapToFeatureFrame(m_paneStack->getCurrentPane(),
Chris@85 2401 frame, resolution, Layer::SnapRight)) {
Chris@85 2402 if (pane) frame = pane->alignToReference(frame);
Chris@85 2403 } else {
Chris@45 2404 frame = getMainModel()->getEndFrame();
Chris@45 2405 }
Chris@45 2406 }
Chris@45 2407
Chris@45 2408 if (frame < 0) frame = 0;
Chris@45 2409
Chris@45 2410 if (m_viewManager->getPlaySelectionMode()) {
Chris@45 2411 frame = m_viewManager->constrainFrameToSelection(size_t(frame));
Chris@45 2412 }
Chris@45 2413
Chris@45 2414 m_viewManager->setPlaybackFrame(frame);
Chris@166 2415
Chris@166 2416 if (frame == getMainModel()->getEndFrame() &&
Chris@166 2417 m_playSource &&
Chris@166 2418 m_playSource->isPlaying() &&
Chris@166 2419 !m_viewManager->getPlayLoopMode()) {
Chris@166 2420 stop();
Chris@166 2421 }
Chris@45 2422 }
Chris@45 2423
Chris@45 2424 void
Chris@45 2425 MainWindowBase::ffwdEnd()
Chris@45 2426 {
Chris@45 2427 if (!getMainModel()) return;
Chris@45 2428
Chris@139 2429 if (m_playSource &&
Chris@139 2430 m_playSource->isPlaying() &&
Chris@139 2431 !m_viewManager->getPlayLoopMode()) {
Chris@139 2432 stop();
Chris@139 2433 }
Chris@139 2434
Chris@45 2435 size_t frame = getMainModel()->getEndFrame();
Chris@45 2436
Chris@45 2437 if (m_viewManager->getPlaySelectionMode()) {
Chris@45 2438 frame = m_viewManager->constrainFrameToSelection(frame);
Chris@45 2439 }
Chris@45 2440
Chris@45 2441 m_viewManager->setPlaybackFrame(frame);
Chris@45 2442 }
Chris@45 2443
Chris@45 2444 void
Chris@166 2445 MainWindowBase::ffwdSimilar()
Chris@166 2446 {
Chris@166 2447 if (!getMainModel()) return;
Chris@166 2448
Chris@166 2449 Layer *layer = getSnapLayer();
Chris@166 2450 if (!layer) { ffwd(); return; }
Chris@166 2451
Chris@166 2452 Pane *pane = m_paneStack->getCurrentPane();
Chris@166 2453 size_t sr = getMainModel()->getSampleRate();
Chris@166 2454
Chris@166 2455 int frame = m_viewManager->getPlaybackFrame();
Chris@166 2456
Chris@166 2457 size_t resolution = 0;
Chris@166 2458 if (pane) frame = pane->alignFromReference(frame);
Chris@166 2459 if (layer->snapToSimilarFeature(m_paneStack->getCurrentPane(),
Chris@166 2460 frame, resolution, Layer::SnapRight)) {
Chris@166 2461 if (pane) frame = pane->alignToReference(frame);
Chris@166 2462 } else {
Chris@166 2463 frame = getMainModel()->getEndFrame();
Chris@166 2464 }
Chris@166 2465
Chris@166 2466 if (frame < 0) frame = 0;
Chris@166 2467
Chris@166 2468 if (m_viewManager->getPlaySelectionMode()) {
Chris@166 2469 frame = m_viewManager->constrainFrameToSelection(size_t(frame));
Chris@166 2470 }
Chris@166 2471
Chris@166 2472 m_viewManager->setPlaybackFrame(frame);
Chris@166 2473
Chris@166 2474 if (frame == getMainModel()->getEndFrame() &&
Chris@166 2475 m_playSource &&
Chris@166 2476 m_playSource->isPlaying() &&
Chris@166 2477 !m_viewManager->getPlayLoopMode()) {
Chris@166 2478 stop();
Chris@166 2479 }
Chris@166 2480 }
Chris@166 2481
Chris@166 2482 void
Chris@45 2483 MainWindowBase::rewind()
Chris@45 2484 {
Chris@45 2485 if (!getMainModel()) return;
Chris@45 2486
Chris@45 2487 int frame = m_viewManager->getPlaybackFrame();
Chris@45 2488 if (frame > 0) --frame;
Chris@45 2489
Chris@85 2490 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 2491 Layer *layer = getSnapLayer();
Chris@45 2492 size_t sr = getMainModel()->getSampleRate();
Chris@45 2493
Chris@45 2494 // when rewinding during playback, we want to allow a period
Chris@45 2495 // following a rewind target point at which the rewind will go to
Chris@45 2496 // the prior point instead of the immediately neighbouring one
Chris@45 2497 if (m_playSource && m_playSource->isPlaying()) {
Chris@45 2498 RealTime ct = RealTime::frame2RealTime(frame, sr);
Chris@45 2499 ct = ct - RealTime::fromSeconds(0.25);
Chris@45 2500 if (ct < RealTime::zeroTime) ct = RealTime::zeroTime;
Chris@45 2501 frame = RealTime::realTime2Frame(ct, sr);
Chris@45 2502 }
Chris@45 2503
Chris@45 2504 if (!layer) {
Chris@45 2505
Chris@45 2506 frame = RealTime::realTime2Frame
Chris@45 2507 (RealTime::frame2RealTime(frame, sr) - RealTime(2, 0), sr);
Chris@45 2508 if (frame < int(getMainModel()->getStartFrame())) {
Chris@45 2509 frame = getMainModel()->getStartFrame();
Chris@45 2510 }
Chris@45 2511
Chris@45 2512 } else {
Chris@45 2513
Chris@45 2514 size_t resolution = 0;
Chris@166 2515 if (pane) frame = pane->alignFromReference(frame);
Chris@85 2516 if (layer->snapToFeatureFrame(m_paneStack->getCurrentPane(),
Chris@85 2517 frame, resolution, Layer::SnapLeft)) {
Chris@85 2518 if (pane) frame = pane->alignToReference(frame);
Chris@85 2519 } else {
Chris@45 2520 frame = getMainModel()->getStartFrame();
Chris@45 2521 }
Chris@45 2522 }
Chris@45 2523
Chris@45 2524 if (frame < 0) frame = 0;
Chris@45 2525
Chris@45 2526 if (m_viewManager->getPlaySelectionMode()) {
Chris@45 2527 frame = m_viewManager->constrainFrameToSelection(size_t(frame));
Chris@45 2528 }
Chris@45 2529
Chris@45 2530 m_viewManager->setPlaybackFrame(frame);
Chris@45 2531 }
Chris@45 2532
Chris@45 2533 void
Chris@45 2534 MainWindowBase::rewindStart()
Chris@45 2535 {
Chris@45 2536 if (!getMainModel()) return;
Chris@45 2537
Chris@45 2538 size_t frame = getMainModel()->getStartFrame();
Chris@45 2539
Chris@45 2540 if (m_viewManager->getPlaySelectionMode()) {
Chris@45 2541 frame = m_viewManager->constrainFrameToSelection(frame);
Chris@45 2542 }
Chris@45 2543
Chris@45 2544 m_viewManager->setPlaybackFrame(frame);
Chris@45 2545 }
Chris@45 2546
Chris@166 2547 void
Chris@166 2548 MainWindowBase::rewindSimilar()
Chris@166 2549 {
Chris@166 2550 if (!getMainModel()) return;
Chris@166 2551
Chris@166 2552 Layer *layer = getSnapLayer();
Chris@166 2553 if (!layer) { rewind(); return; }
Chris@166 2554
Chris@166 2555 Pane *pane = m_paneStack->getCurrentPane();
Chris@166 2556 size_t sr = getMainModel()->getSampleRate();
Chris@166 2557
Chris@166 2558 int frame = m_viewManager->getPlaybackFrame();
Chris@166 2559
Chris@166 2560 size_t resolution = 0;
Chris@166 2561 if (pane) frame = pane->alignFromReference(frame);
Chris@166 2562 if (layer->snapToSimilarFeature(m_paneStack->getCurrentPane(),
Chris@166 2563 frame, resolution, Layer::SnapLeft)) {
Chris@166 2564 if (pane) frame = pane->alignToReference(frame);
Chris@166 2565 } else {
Chris@166 2566 frame = getMainModel()->getStartFrame();
Chris@166 2567 }
Chris@166 2568
Chris@166 2569 if (frame < 0) frame = 0;
Chris@166 2570
Chris@166 2571 if (m_viewManager->getPlaySelectionMode()) {
Chris@166 2572 frame = m_viewManager->constrainFrameToSelection(size_t(frame));
Chris@166 2573 }
Chris@166 2574
Chris@166 2575 m_viewManager->setPlaybackFrame(frame);
Chris@166 2576 }
Chris@166 2577
Chris@45 2578 Layer *
Chris@45 2579 MainWindowBase::getSnapLayer() const
Chris@45 2580 {
Chris@45 2581 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 2582 if (!pane) return 0;
Chris@45 2583
Chris@45 2584 Layer *layer = pane->getSelectedLayer();
Chris@45 2585
Chris@45 2586 if (!dynamic_cast<TimeInstantLayer *>(layer) &&
Chris@45 2587 !dynamic_cast<TimeValueLayer *>(layer) &&
Chris@194 2588 !dynamic_cast<RegionLayer *>(layer) &&
Chris@45 2589 !dynamic_cast<TimeRulerLayer *>(layer)) {
Chris@45 2590
Chris@45 2591 layer = 0;
Chris@45 2592
Chris@45 2593 for (int i = pane->getLayerCount(); i > 0; --i) {
Chris@45 2594 Layer *l = pane->getLayer(i-1);
Chris@45 2595 if (dynamic_cast<TimeRulerLayer *>(l)) {
Chris@45 2596 layer = l;
Chris@45 2597 break;
Chris@45 2598 }
Chris@45 2599 }
Chris@45 2600 }
Chris@45 2601
Chris@45 2602 return layer;
Chris@45 2603 }
Chris@45 2604
Chris@45 2605 void
Chris@45 2606 MainWindowBase::stop()
Chris@45 2607 {
Chris@45 2608 m_playSource->stop();
Chris@45 2609
Chris@45 2610 if (m_paneStack && m_paneStack->getCurrentPane()) {
Chris@45 2611 updateVisibleRangeDisplay(m_paneStack->getCurrentPane());
Chris@45 2612 } else {
Chris@45 2613 m_myStatusMessage = "";
Chris@45 2614 statusBar()->showMessage("");
Chris@45 2615 }
Chris@45 2616 }
Chris@45 2617
Chris@45 2618 MainWindowBase::AddPaneCommand::AddPaneCommand(MainWindowBase *mw) :
Chris@45 2619 m_mw(mw),
Chris@45 2620 m_pane(0),
Chris@45 2621 m_prevCurrentPane(0),
Chris@45 2622 m_added(false)
Chris@45 2623 {
Chris@45 2624 }
Chris@45 2625
Chris@45 2626 MainWindowBase::AddPaneCommand::~AddPaneCommand()
Chris@45 2627 {
Chris@45 2628 if (m_pane && !m_added) {
Chris@45 2629 m_mw->m_paneStack->deletePane(m_pane);
Chris@45 2630 }
Chris@45 2631 }
Chris@45 2632
Chris@45 2633 QString
Chris@45 2634 MainWindowBase::AddPaneCommand::getName() const
Chris@45 2635 {
Chris@45 2636 return tr("Add Pane");
Chris@45 2637 }
Chris@45 2638
Chris@45 2639 void
Chris@45 2640 MainWindowBase::AddPaneCommand::execute()
Chris@45 2641 {
Chris@45 2642 if (!m_pane) {
Chris@45 2643 m_prevCurrentPane = m_mw->m_paneStack->getCurrentPane();
Chris@45 2644 m_pane = m_mw->m_paneStack->addPane();
Chris@45 2645
Chris@45 2646 connect(m_pane, SIGNAL(contextHelpChanged(const QString &)),
Chris@45 2647 m_mw, SLOT(contextHelpChanged(const QString &)));
Chris@45 2648 } else {
Chris@45 2649 m_mw->m_paneStack->showPane(m_pane);
Chris@45 2650 }
Chris@45 2651
Chris@45 2652 m_mw->m_paneStack->setCurrentPane(m_pane);
Chris@45 2653 m_added = true;
Chris@45 2654 }
Chris@45 2655
Chris@45 2656 void
Chris@45 2657 MainWindowBase::AddPaneCommand::unexecute()
Chris@45 2658 {
Chris@45 2659 m_mw->m_paneStack->hidePane(m_pane);
Chris@45 2660 m_mw->m_paneStack->setCurrentPane(m_prevCurrentPane);
Chris@45 2661 m_added = false;
Chris@45 2662 }
Chris@45 2663
Chris@45 2664 MainWindowBase::RemovePaneCommand::RemovePaneCommand(MainWindowBase *mw, Pane *pane) :
Chris@45 2665 m_mw(mw),
Chris@45 2666 m_pane(pane),
Chris@45 2667 m_added(true)
Chris@45 2668 {
Chris@45 2669 }
Chris@45 2670
Chris@45 2671 MainWindowBase::RemovePaneCommand::~RemovePaneCommand()
Chris@45 2672 {
Chris@45 2673 if (m_pane && !m_added) {
Chris@45 2674 m_mw->m_paneStack->deletePane(m_pane);
Chris@45 2675 }
Chris@45 2676 }
Chris@45 2677
Chris@45 2678 QString
Chris@45 2679 MainWindowBase::RemovePaneCommand::getName() const
Chris@45 2680 {
Chris@45 2681 return tr("Remove Pane");
Chris@45 2682 }
Chris@45 2683
Chris@45 2684 void
Chris@45 2685 MainWindowBase::RemovePaneCommand::execute()
Chris@45 2686 {
Chris@45 2687 m_prevCurrentPane = m_mw->m_paneStack->getCurrentPane();
Chris@45 2688 m_mw->m_paneStack->hidePane(m_pane);
Chris@45 2689 m_added = false;
Chris@45 2690 }
Chris@45 2691
Chris@45 2692 void
Chris@45 2693 MainWindowBase::RemovePaneCommand::unexecute()
Chris@45 2694 {
Chris@45 2695 m_mw->m_paneStack->showPane(m_pane);
Chris@45 2696 m_mw->m_paneStack->setCurrentPane(m_prevCurrentPane);
Chris@45 2697 m_added = true;
Chris@45 2698 }
Chris@45 2699
Chris@45 2700 void
Chris@45 2701 MainWindowBase::deleteCurrentPane()
Chris@45 2702 {
Chris@45 2703 CommandHistory::getInstance()->startCompoundOperation
Chris@45 2704 (tr("Delete Pane"), true);
Chris@45 2705
Chris@45 2706 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 2707 if (pane) {
Chris@45 2708 while (pane->getLayerCount() > 0) {
Chris@45 2709 Layer *layer = pane->getLayer(0);
Chris@45 2710 if (layer) {
Chris@45 2711 m_document->removeLayerFromView(pane, layer);
Chris@45 2712 } else {
Chris@45 2713 break;
Chris@45 2714 }
Chris@45 2715 }
Chris@45 2716
Chris@45 2717 RemovePaneCommand *command = new RemovePaneCommand(this, pane);
Chris@45 2718 CommandHistory::getInstance()->addCommand(command);
Chris@45 2719 }
Chris@45 2720
Chris@45 2721 CommandHistory::getInstance()->endCompoundOperation();
Chris@45 2722
Chris@45 2723 updateMenuStates();
Chris@45 2724 }
Chris@45 2725
Chris@45 2726 void
Chris@45 2727 MainWindowBase::deleteCurrentLayer()
Chris@45 2728 {
Chris@45 2729 Pane *pane = m_paneStack->getCurrentPane();
Chris@45 2730 if (pane) {
Chris@45 2731 Layer *layer = pane->getSelectedLayer();
Chris@45 2732 if (layer) {
Chris@45 2733 m_document->removeLayerFromView(pane, layer);
Chris@45 2734 }
Chris@45 2735 }
Chris@45 2736 updateMenuStates();
Chris@45 2737 }
Chris@45 2738
Chris@45 2739 void
Chris@123 2740 MainWindowBase::editCurrentLayer()
Chris@123 2741 {
Chris@123 2742 Layer *layer = 0;
Chris@123 2743 Pane *pane = m_paneStack->getCurrentPane();
Chris@123 2744 if (pane) layer = pane->getSelectedLayer();
Chris@123 2745 if (!layer) return;
Chris@123 2746
Chris@123 2747 Model *model = layer->getModel();
Chris@123 2748 if (!model) return;
Chris@123 2749
Chris@124 2750 TabularModel *tabular = dynamic_cast<TabularModel *>(model);
Chris@124 2751 if (!tabular) {
Chris@124 2752 //!!! how to prevent this function from being active if not
Chris@124 2753 //appropriate model type? or will we ultimately support
Chris@124 2754 //tabular display for all editable models?
Chris@124 2755 std::cerr << "NOTE: Not a tabular model" << std::endl;
Chris@124 2756 return;
Chris@124 2757 }
Chris@124 2758
Chris@123 2759 if (m_layerDataDialogMap.find(layer) != m_layerDataDialogMap.end()) {
Chris@126 2760 if (!m_layerDataDialogMap[layer].isNull()) {
Chris@126 2761 m_layerDataDialogMap[layer]->show();
Chris@126 2762 m_layerDataDialogMap[layer]->raise();
Chris@126 2763 return;
Chris@126 2764 }
Chris@123 2765 }
Chris@123 2766
Chris@125 2767 QString title = layer->getLayerPresentationName();
Chris@125 2768
Chris@125 2769 ModelDataTableDialog *dialog = new ModelDataTableDialog(tabular, title, this);
Chris@128 2770 dialog->setAttribute(Qt::WA_DeleteOnClose);
Chris@128 2771
Chris@128 2772 connectLayerEditDialog(dialog);
Chris@123 2773
Chris@128 2774 m_layerDataDialogMap[layer] = dialog;
Chris@128 2775 m_viewDataDialogMap[pane].insert(dialog);
Chris@128 2776
Chris@128 2777 dialog->show();
Chris@128 2778 }
Chris@128 2779
Chris@128 2780 void
Chris@128 2781 MainWindowBase::connectLayerEditDialog(ModelDataTableDialog *dialog)
Chris@128 2782 {
Chris@123 2783 connect(m_viewManager,
Chris@123 2784 SIGNAL(globalCentreFrameChanged(unsigned long)),
Chris@123 2785 dialog,
Chris@127 2786 SLOT(userScrolledToFrame(unsigned long)));
Chris@127 2787
Chris@127 2788 connect(m_viewManager,
Chris@127 2789 SIGNAL(playbackFrameChanged(unsigned long)),
Chris@127 2790 dialog,
Chris@127 2791 SLOT(playbackScrolledToFrame(unsigned long)));
Chris@127 2792
Chris@123 2793 connect(dialog,
Chris@123 2794 SIGNAL(scrollToFrame(unsigned long)),
Chris@123 2795 m_viewManager,
Chris@123 2796 SLOT(setGlobalCentreFrame(unsigned long)));
Chris@129 2797
Chris@129 2798 connect(dialog,
Chris@129 2799 SIGNAL(scrollToFrame(unsigned long)),
Chris@129 2800 m_viewManager,
Chris@129 2801 SLOT(setPlaybackFrame(unsigned long)));
Chris@128 2802 }
Chris@123 2803
Chris@123 2804 void
Chris@73 2805 MainWindowBase::previousPane()
Chris@73 2806 {
Chris@73 2807 if (!m_paneStack) return;
Chris@73 2808
Chris@73 2809 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@73 2810 if (!currentPane) return;
Chris@73 2811
Chris@73 2812 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@73 2813 if (m_paneStack->getPane(i) == currentPane) {
Chris@73 2814 if (i == 0) return;
Chris@73 2815 m_paneStack->setCurrentPane(m_paneStack->getPane(i-1));
Chris@73 2816 updateMenuStates();
Chris@73 2817 return;
Chris@73 2818 }
Chris@73 2819 }
Chris@73 2820 }
Chris@73 2821
Chris@73 2822 void
Chris@73 2823 MainWindowBase::nextPane()
Chris@73 2824 {
Chris@73 2825 if (!m_paneStack) return;
Chris@73 2826
Chris@73 2827 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@73 2828 if (!currentPane) return;
Chris@73 2829
Chris@73 2830 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@73 2831 if (m_paneStack->getPane(i) == currentPane) {
Chris@73 2832 if (i == m_paneStack->getPaneCount()-1) return;
Chris@73 2833 m_paneStack->setCurrentPane(m_paneStack->getPane(i+1));
Chris@73 2834 updateMenuStates();
Chris@73 2835 return;
Chris@73 2836 }
Chris@73 2837 }
Chris@73 2838 }
Chris@73 2839
Chris@73 2840 void
Chris@73 2841 MainWindowBase::previousLayer()
Chris@73 2842 {
Chris@73 2843 //!!! Not right -- pane lists layers in stacking order
Chris@73 2844
Chris@73 2845 if (!m_paneStack) return;
Chris@73 2846
Chris@73 2847 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@73 2848 if (!currentPane) return;
Chris@73 2849
Chris@73 2850 Layer *currentLayer = currentPane->getSelectedLayer();
Chris@73 2851 if (!currentLayer) return;
Chris@73 2852
Chris@73 2853 for (int i = 0; i < currentPane->getLayerCount(); ++i) {
Chris@73 2854 if (currentPane->getLayer(i) == currentLayer) {
Chris@73 2855 if (i == 0) return;
Chris@73 2856 m_paneStack->setCurrentLayer(currentPane,
Chris@73 2857 currentPane->getLayer(i-1));
Chris@73 2858 updateMenuStates();
Chris@73 2859 return;
Chris@73 2860 }
Chris@73 2861 }
Chris@73 2862 }
Chris@73 2863
Chris@73 2864 void
Chris@73 2865 MainWindowBase::nextLayer()
Chris@73 2866 {
Chris@73 2867 //!!! Not right -- pane lists layers in stacking order
Chris@73 2868
Chris@73 2869 if (!m_paneStack) return;
Chris@73 2870
Chris@73 2871 Pane *currentPane = m_paneStack->getCurrentPane();
Chris@73 2872 if (!currentPane) return;
Chris@73 2873
Chris@73 2874 Layer *currentLayer = currentPane->getSelectedLayer();
Chris@73 2875 if (!currentLayer) return;
Chris@73 2876
Chris@73 2877 for (int i = 0; i < currentPane->getLayerCount(); ++i) {
Chris@73 2878 if (currentPane->getLayer(i) == currentLayer) {
Chris@73 2879 if (i == currentPane->getLayerCount()-1) return;
Chris@73 2880 m_paneStack->setCurrentLayer(currentPane,
Chris@73 2881 currentPane->getLayer(i+1));
Chris@73 2882 updateMenuStates();
Chris@73 2883 return;
Chris@73 2884 }
Chris@73 2885 }
Chris@73 2886 }
Chris@73 2887
Chris@73 2888 void
Chris@45 2889 MainWindowBase::playbackFrameChanged(unsigned long frame)
Chris@45 2890 {
Chris@45 2891 if (!(m_playSource && m_playSource->isPlaying()) || !getMainModel()) return;
Chris@45 2892
Chris@187 2893 updatePositionStatusDisplays();
Chris@187 2894
Chris@45 2895 RealTime now = RealTime::frame2RealTime
Chris@45 2896 (frame, getMainModel()->getSampleRate());
Chris@45 2897
Chris@45 2898 if (now.sec == m_lastPlayStatusSec) return;
Chris@45 2899
Chris@45 2900 RealTime then = RealTime::frame2RealTime
Chris@45 2901 (m_playSource->getPlayEndFrame(), getMainModel()->getSampleRate());
Chris@45 2902
Chris@45 2903 QString nowStr;
Chris@45 2904 QString thenStr;
Chris@45 2905 QString remainingStr;
Chris@45 2906
Chris@45 2907 if (then.sec > 10) {
Chris@45 2908 nowStr = now.toSecText().c_str();
Chris@45 2909 thenStr = then.toSecText().c_str();
Chris@45 2910 remainingStr = (then - now).toSecText().c_str();
Chris@45 2911 m_lastPlayStatusSec = now.sec;
Chris@45 2912 } else {
Chris@45 2913 nowStr = now.toText(true).c_str();
Chris@45 2914 thenStr = then.toText(true).c_str();
Chris@45 2915 remainingStr = (then - now).toText(true).c_str();
Chris@45 2916 }
Chris@45 2917
Chris@45 2918 m_myStatusMessage = tr("Playing: %1 of %2 (%3 remaining)")
Chris@45 2919 .arg(nowStr).arg(thenStr).arg(remainingStr);
Chris@45 2920
Chris@45 2921 statusBar()->showMessage(m_myStatusMessage);
Chris@45 2922 }
Chris@45 2923
Chris@45 2924 void
Chris@45 2925 MainWindowBase::globalCentreFrameChanged(unsigned long )
Chris@45 2926 {
Chris@45 2927 if ((m_playSource && m_playSource->isPlaying()) || !getMainModel()) return;
Chris@45 2928 Pane *p = 0;
Chris@45 2929 if (!m_paneStack || !(p = m_paneStack->getCurrentPane())) return;
Chris@45 2930 if (!p->getFollowGlobalPan()) return;
Chris@45 2931 updateVisibleRangeDisplay(p);
Chris@45 2932 }
Chris@45 2933
Chris@45 2934 void
Chris@123 2935 MainWindowBase::viewCentreFrameChanged(View *v, unsigned long frame)
Chris@45 2936 {
Chris@130 2937 // std::cerr << "MainWindowBase::viewCentreFrameChanged(" << v << "," << frame << ")" << std::endl;
Chris@123 2938
Chris@123 2939 if (m_viewDataDialogMap.find(v) != m_viewDataDialogMap.end()) {
Chris@123 2940 for (DataDialogSet::iterator i = m_viewDataDialogMap[v].begin();
Chris@123 2941 i != m_viewDataDialogMap[v].end(); ++i) {
Chris@127 2942 (*i)->userScrolledToFrame(frame);
Chris@123 2943 }
Chris@123 2944 }
Chris@45 2945 if ((m_playSource && m_playSource->isPlaying()) || !getMainModel()) return;
Chris@45 2946 Pane *p = 0;
Chris@45 2947 if (!m_paneStack || !(p = m_paneStack->getCurrentPane())) return;
Chris@45 2948 if (v == p) updateVisibleRangeDisplay(p);
Chris@45 2949 }
Chris@45 2950
Chris@45 2951 void
Chris@45 2952 MainWindowBase::viewZoomLevelChanged(View *v, unsigned long , bool )
Chris@45 2953 {
Chris@45 2954 if ((m_playSource && m_playSource->isPlaying()) || !getMainModel()) return;
Chris@45 2955 Pane *p = 0;
Chris@45 2956 if (!m_paneStack || !(p = m_paneStack->getCurrentPane())) return;
Chris@45 2957 if (v == p) updateVisibleRangeDisplay(p);
Chris@45 2958 }
Chris@45 2959
Chris@45 2960 void
Chris@45 2961 MainWindowBase::layerAdded(Layer *)
Chris@45 2962 {
Chris@45 2963 // std::cerr << "MainWindowBase::layerAdded(" << layer << ")" << std::endl;
Chris@45 2964 updateMenuStates();
Chris@45 2965 }
Chris@45 2966
Chris@45 2967 void
Chris@45 2968 MainWindowBase::layerRemoved(Layer *)
Chris@45 2969 {
Chris@45 2970 // std::cerr << "MainWindowBase::layerRemoved(" << layer << ")" << std::endl;
Chris@45 2971 updateMenuStates();
Chris@45 2972 }
Chris@45 2973
Chris@45 2974 void
Chris@45 2975 MainWindowBase::layerAboutToBeDeleted(Layer *layer)
Chris@45 2976 {
Chris@132 2977 // std::cerr << "MainWindowBase::layerAboutToBeDeleted(" << layer << ")" << std::endl;
Chris@123 2978
Chris@128 2979 removeLayerEditDialog(layer);
Chris@123 2980
Chris@47 2981 if (m_timeRulerLayer && (layer == m_timeRulerLayer)) {
Chris@45 2982 // std::cerr << "(this is the time ruler layer)" << std::endl;
Chris@45 2983 m_timeRulerLayer = 0;
Chris@45 2984 }
Chris@45 2985 }
Chris@45 2986
Chris@45 2987 void
Chris@45 2988 MainWindowBase::layerInAView(Layer *layer, bool inAView)
Chris@45 2989 {
Chris@132 2990 // std::cerr << "MainWindowBase::layerInAView(" << layer << "," << inAView << ")" << std::endl;
Chris@128 2991
Chris@128 2992 if (!inAView) removeLayerEditDialog(layer);
Chris@45 2993
Chris@45 2994 // Check whether we need to add or remove model from play source
Chris@45 2995 Model *model = layer->getModel();
Chris@45 2996 if (model) {
Chris@45 2997 if (inAView) {
Chris@45 2998 m_playSource->addModel(model);
Chris@45 2999 } else {
Chris@45 3000 bool found = false;
Chris@45 3001 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@45 3002 Pane *pane = m_paneStack->getPane(i);
Chris@45 3003 if (!pane) continue;
Chris@45 3004 for (int j = 0; j < pane->getLayerCount(); ++j) {
Chris@45 3005 Layer *pl = pane->getLayer(j);
Chris@183 3006 if (pl &&
Chris@183 3007 !dynamic_cast<TimeRulerLayer *>(pl) &&
Chris@183 3008 (pl->getModel() == model)) {
Chris@45 3009 found = true;
Chris@45 3010 break;
Chris@45 3011 }
Chris@45 3012 }
Chris@45 3013 if (found) break;
Chris@45 3014 }
Chris@173 3015 if (!found) {
Chris@173 3016 m_playSource->removeModel(model);
Chris@173 3017 }
Chris@45 3018 }
Chris@45 3019 }
Chris@45 3020
Chris@45 3021 updateMenuStates();
Chris@45 3022 }
Chris@45 3023
Chris@45 3024 void
Chris@128 3025 MainWindowBase::removeLayerEditDialog(Layer *layer)
Chris@128 3026 {
Chris@128 3027 if (m_layerDataDialogMap.find(layer) != m_layerDataDialogMap.end()) {
Chris@128 3028
Chris@128 3029 ModelDataTableDialog *dialog = m_layerDataDialogMap[layer];
Chris@128 3030
Chris@128 3031 for (ViewDataDialogMap::iterator vi = m_viewDataDialogMap.begin();
Chris@128 3032 vi != m_viewDataDialogMap.end(); ++vi) {
Chris@128 3033 vi->second.erase(dialog);
Chris@128 3034 }
Chris@128 3035
Chris@128 3036 m_layerDataDialogMap.erase(layer);
Chris@128 3037 delete dialog;
Chris@128 3038 }
Chris@128 3039 }
Chris@128 3040
Chris@128 3041 void
Chris@45 3042 MainWindowBase::modelAdded(Model *model)
Chris@45 3043 {
Chris@45 3044 // std::cerr << "MainWindowBase::modelAdded(" << model << ")" << std::endl;
Chris@45 3045 m_playSource->addModel(model);
Chris@45 3046 }
Chris@45 3047
Chris@45 3048 void
Chris@45 3049 MainWindowBase::mainModelChanged(WaveFileModel *model)
Chris@45 3050 {
Chris@45 3051 // std::cerr << "MainWindowBase::mainModelChanged(" << model << ")" << std::endl;
Chris@45 3052 updateDescriptionLabel();
Chris@45 3053 if (model) m_viewManager->setMainModelSampleRate(model->getSampleRate());
Chris@45 3054 if (model && !m_playTarget && m_audioOutput) createPlayTarget();
Chris@45 3055 }
Chris@45 3056
Chris@45 3057 void
Chris@45 3058 MainWindowBase::modelAboutToBeDeleted(Model *model)
Chris@45 3059 {
Chris@45 3060 // std::cerr << "MainWindowBase::modelAboutToBeDeleted(" << model << ")" << std::endl;
Chris@45 3061 if (model == m_viewManager->getPlaybackModel()) {
Chris@45 3062 m_viewManager->setPlaybackModel(0);
Chris@45 3063 }
Chris@45 3064 m_playSource->removeModel(model);
Chris@45 3065 FFTDataServer::modelAboutToBeDeleted(model);
Chris@45 3066 }
Chris@45 3067
Chris@45 3068 void
Chris@55 3069 MainWindowBase::paneDeleteButtonClicked(Pane *pane)
Chris@55 3070 {
Chris@55 3071 bool found = false;
Chris@55 3072 for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
Chris@55 3073 if (m_paneStack->getPane(i) == pane) {
Chris@55 3074 found = true;
Chris@55 3075 break;
Chris@55 3076 }
Chris@55 3077 }
Chris@55 3078 if (!found) {
Chris@55 3079 std::cerr << "MainWindowBase::paneDeleteButtonClicked: Unknown pane "
Chris@55 3080 << pane << std::endl;
Chris@55 3081 return;
Chris@55 3082 }
Chris@55 3083
Chris@55 3084 CommandHistory::getInstance()->startCompoundOperation
Chris@55 3085 (tr("Delete Pane"), true);
Chris@55 3086
Chris@55 3087 while (pane->getLayerCount() > 0) {
Chris@55 3088 Layer *layer = pane->getLayer(0);
Chris@55 3089 if (layer) {
Chris@55 3090 m_document->removeLayerFromView(pane, layer);
Chris@55 3091 } else {
Chris@55 3092 break;
Chris@55 3093 }
Chris@55 3094 }
Chris@55 3095
Chris@55 3096 RemovePaneCommand *command = new RemovePaneCommand(this, pane);
Chris@55 3097 CommandHistory::getInstance()->addCommand(command);
Chris@55 3098
Chris@55 3099 CommandHistory::getInstance()->endCompoundOperation();
Chris@55 3100
Chris@55 3101 updateMenuStates();
Chris@55 3102 }
Chris@55 3103
Chris@55 3104 void
Chris@45 3105 MainWindowBase::pollOSC()
Chris@45 3106 {
Chris@45 3107 if (!m_oscQueue || m_oscQueue->isEmpty()) return;
Chris@45 3108 std::cerr << "MainWindowBase::pollOSC: have " << m_oscQueue->getMessagesAvailable() << " messages" << std::endl;
Chris@45 3109
Chris@45 3110 if (m_openingAudioFile) return;
Chris@45 3111
Chris@45 3112 OSCMessage message = m_oscQueue->readMessage();
Chris@45 3113
Chris@45 3114 if (message.getTarget() != 0) {
Chris@45 3115 return; //!!! for now -- this class is target 0, others not handled yet
Chris@45 3116 }
Chris@45 3117
Chris@45 3118 handleOSCMessage(message);
Chris@45 3119 }
Chris@45 3120
Chris@45 3121 void
Chris@45 3122 MainWindowBase::inProgressSelectionChanged()
Chris@45 3123 {
Chris@45 3124 Pane *currentPane = 0;
Chris@45 3125 if (m_paneStack) currentPane = m_paneStack->getCurrentPane();
Chris@45 3126 if (currentPane) updateVisibleRangeDisplay(currentPane);
Chris@45 3127 }
Chris@45 3128
Chris@45 3129 void
Chris@45 3130 MainWindowBase::contextHelpChanged(const QString &s)
Chris@45 3131 {
Chris@45 3132 if (s == "" && m_myStatusMessage != "") {
Chris@45 3133 statusBar()->showMessage(m_myStatusMessage);
Chris@45 3134 return;
Chris@45 3135 }
Chris@45 3136 statusBar()->showMessage(s);
Chris@45 3137 }
Chris@45 3138
Chris@45 3139 void
Chris@45 3140 MainWindowBase::openHelpUrl(QString url)
Chris@45 3141 {
Chris@45 3142 // This method mostly lifted from Qt Assistant source code
Chris@45 3143
Chris@45 3144 QProcess *process = new QProcess(this);
Chris@45 3145 connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater()));
Chris@45 3146
Chris@45 3147 QStringList args;
Chris@45 3148
Chris@45 3149 #ifdef Q_OS_MAC
Chris@45 3150 args.append(url);
Chris@45 3151 process->start("open", args);
Chris@45 3152 #else
Chris@45 3153 #ifdef Q_OS_WIN32
Chris@45 3154
Chris@45 3155 QString pf(getenv("ProgramFiles"));
Chris@45 3156 QString command = pf + QString("\\Internet Explorer\\IEXPLORE.EXE");
Chris@45 3157
Chris@45 3158 args.append(url);
Chris@45 3159 process->start(command, args);
Chris@45 3160
Chris@45 3161 #else
Chris@45 3162 #ifdef Q_WS_X11
Chris@45 3163 if (!qgetenv("KDE_FULL_SESSION").isEmpty()) {
Chris@45 3164 args.append("exec");
Chris@45 3165 args.append(url);
Chris@45 3166 process->start("kfmclient", args);
Chris@45 3167 } else if (!qgetenv("BROWSER").isEmpty()) {
Chris@45 3168 args.append(url);
Chris@45 3169 process->start(qgetenv("BROWSER"), args);
Chris@45 3170 } else {
Chris@45 3171 args.append(url);
Chris@45 3172 process->start("firefox", args);
Chris@45 3173 }
Chris@45 3174 #endif
Chris@45 3175 #endif
Chris@45 3176 #endif
Chris@45 3177 }
Chris@45 3178
Chris@180 3179