annotate view/ViewManager.cpp @ 1353:86429ff00f05

Merge from branch zoom
author Chris Cannam
date Wed, 10 Oct 2018 08:44:27 +0100
parents 646e713a4632
children c8a6fd3f9dff
rev   line source
Chris@127 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@127 2
Chris@127 3 /*
Chris@127 4 Sonic Visualiser
Chris@127 5 An audio file viewer and annotation editor.
Chris@127 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@127 8
Chris@127 9 This program is free software; you can redistribute it and/or
Chris@127 10 modify it under the terms of the GNU General Public License as
Chris@127 11 published by the Free Software Foundation; either version 2 of the
Chris@127 12 License, or (at your option) any later version. See the file
Chris@127 13 COPYING included with this distribution for more information.
Chris@127 14 */
Chris@127 15
Chris@127 16 #include "ViewManager.h"
Chris@128 17 #include "base/AudioPlaySource.h"
Chris@1210 18 #include "base/AudioRecordTarget.h"
Chris@502 19 #include "base/RealTime.h"
Chris@128 20 #include "data/model/Model.h"
Chris@376 21 #include "widgets/CommandHistory.h"
Chris@211 22 #include "View.h"
Chris@503 23 #include "Overview.h"
Chris@127 24
Chris@133 25 #include <QSettings>
Chris@292 26 #include <QApplication>
Chris@133 27
Chris@127 28 #include <iostream>
Chris@127 29
Chris@643 30 //#define DEBUG_VIEW_MANAGER 1
Chris@127 31
Chris@127 32 ViewManager::ViewManager() :
Chris@127 33 m_playSource(0),
Chris@1210 34 m_recordTarget(0),
Chris@127 35 m_globalCentreFrame(0),
Chris@1327 36 m_globalZoom(ZoomLevel::FramesPerPixel, 1024),
Chris@127 37 m_playbackFrame(0),
Chris@301 38 m_playbackModel(0),
Chris@127 39 m_mainModelSampleRate(0),
Chris@127 40 m_lastLeft(0),
Chris@127 41 m_lastRight(0),
Chris@127 42 m_inProgressExclusive(true),
Chris@127 43 m_toolMode(NavigateMode),
Chris@127 44 m_playLoopMode(false),
Chris@127 45 m_playSelectionMode(false),
Chris@301 46 m_playSoloMode(false),
Chris@314 47 m_alignMode(false),
Chris@741 48 m_overlayMode(StandardOverlays),
Chris@292 49 m_zoomWheelsEnabled(true),
Chris@607 50 m_showCentreLine(true),
Chris@326 51 m_illuminateLocalFeatures(true),
Chris@326 52 m_showWorkTitle(false),
matthiasm@822 53 m_showDuration(true),
Chris@292 54 m_lightPalette(QApplication::palette()),
Chris@292 55 m_darkPalette(QApplication::palette())
Chris@127 56 {
Chris@133 57 QSettings settings;
Chris@133 58 settings.beginGroup("MainWindow");
Chris@133 59 m_overlayMode = OverlayMode
Chris@133 60 (settings.value("overlay-mode", int(m_overlayMode)).toInt());
Chris@607 61
Chris@607 62 if (m_overlayMode != NoOverlays &&
Chris@741 63 m_overlayMode != StandardOverlays &&
Chris@607 64 m_overlayMode != AllOverlays) {
Chris@741 65 m_overlayMode = StandardOverlays;
Chris@607 66 }
Chris@607 67
Chris@133 68 m_zoomWheelsEnabled =
Chris@133 69 settings.value("zoom-wheels-enabled", m_zoomWheelsEnabled).toBool();
Chris@607 70 m_showCentreLine =
Chris@607 71 settings.value("show-centre-line", m_showCentreLine).toBool();
Chris@133 72 settings.endGroup();
Chris@292 73
Chris@292 74 if (getGlobalDarkBackground()) {
Chris@343 75 /*
Chris@682 76 cerr << "dark palette:" << endl;
Chris@682 77 cerr << "window = " << QApplication::palette().color(QPalette::Window).name() << endl;
Chris@682 78 cerr << "windowtext = " << QApplication::palette().color(QPalette::WindowText).name() << endl;
Chris@682 79 cerr << "base = " << QApplication::palette().color(QPalette::Base).name() << endl;
Chris@682 80 cerr << "alternatebase = " << QApplication::palette().color(QPalette::AlternateBase).name() << endl;
Chris@682 81 cerr << "text = " << QApplication::palette().color(QPalette::Text).name() << endl;
Chris@682 82 cerr << "button = " << QApplication::palette().color(QPalette::Button).name() << endl;
Chris@682 83 cerr << "buttontext = " << QApplication::palette().color(QPalette::ButtonText).name() << endl;
Chris@682 84 cerr << "brighttext = " << QApplication::palette().color(QPalette::BrightText).name() << endl;
Chris@682 85 cerr << "light = " << QApplication::palette().color(QPalette::Light).name() << endl;
Chris@682 86 cerr << "dark = " << QApplication::palette().color(QPalette::Dark).name() << endl;
Chris@682 87 cerr << "mid = " << QApplication::palette().color(QPalette::Mid).name() << endl;
Chris@343 88 */
Chris@293 89 m_lightPalette = QPalette(QColor("#000000"), // WindowText
Chris@293 90 QColor("#dddfe4"), // Button
Chris@293 91 QColor("#ffffff"), // Light
Chris@293 92 QColor("#555555"), // Dark
Chris@293 93 QColor("#c7c7c7"), // Mid
Chris@293 94 QColor("#000000"), // Text
Chris@293 95 QColor("#ffffff"), // BrightText
Chris@293 96 QColor("#ffffff"), // Base
Chris@293 97 QColor("#efefef")); // Window
Chris@293 98
Chris@293 99
Chris@292 100 } else {
Chris@343 101 /*
Chris@682 102 cerr << "light palette:" << endl;
Chris@682 103 cerr << "window = " << QApplication::palette().color(QPalette::Window).name() << endl;
Chris@682 104 cerr << "windowtext = " << QApplication::palette().color(QPalette::WindowText).name() << endl;
Chris@682 105 cerr << "base = " << QApplication::palette().color(QPalette::Base).name() << endl;
Chris@682 106 cerr << "alternatebase = " << QApplication::palette().color(QPalette::AlternateBase).name() << endl;
Chris@682 107 cerr << "text = " << QApplication::palette().color(QPalette::Text).name() << endl;
Chris@682 108 cerr << "button = " << QApplication::palette().color(QPalette::Button).name() << endl;
Chris@682 109 cerr << "buttontext = " << QApplication::palette().color(QPalette::ButtonText).name() << endl;
Chris@682 110 cerr << "brighttext = " << QApplication::palette().color(QPalette::BrightText).name() << endl;
Chris@682 111 cerr << "light = " << QApplication::palette().color(QPalette::Light).name() << endl;
Chris@682 112 cerr << "dark = " << QApplication::palette().color(QPalette::Dark).name() << endl;
Chris@682 113 cerr << "mid = " << QApplication::palette().color(QPalette::Mid).name() << endl;
Chris@343 114 */
Chris@293 115 m_darkPalette = QPalette(QColor("#ffffff"), // WindowText
Chris@293 116 QColor("#3e3e3e"), // Button
Chris@293 117 QColor("#808080"), // Light
Chris@293 118 QColor("#1e1e1e"), // Dark
Chris@293 119 QColor("#404040"), // Mid
Chris@293 120 QColor("#ffffff"), // Text
Chris@293 121 QColor("#ffffff"), // BrightText
Chris@293 122 QColor("#000000"), // Base
Chris@293 123 QColor("#202020")); // Window
Chris@292 124 }
Chris@127 125 }
Chris@127 126
Chris@127 127 ViewManager::~ViewManager()
Chris@127 128 {
Chris@127 129 }
Chris@127 130
Chris@902 131 sv_frame_t
Chris@127 132 ViewManager::getGlobalCentreFrame() const
Chris@127 133 {
Chris@127 134 #ifdef DEBUG_VIEW_MANAGER
Chris@682 135 cerr << "ViewManager::getGlobalCentreFrame: returning " << m_globalCentreFrame << endl;
Chris@127 136 #endif
Chris@127 137 return m_globalCentreFrame;
Chris@127 138 }
Chris@127 139
Chris@180 140 void
Chris@902 141 ViewManager::setGlobalCentreFrame(sv_frame_t f)
Chris@180 142 {
Chris@180 143 #ifdef DEBUG_VIEW_MANAGER
Chris@682 144 cerr << "ViewManager::setGlobalCentreFrame to " << f << endl;
Chris@180 145 #endif
Chris@180 146 m_globalCentreFrame = f;
Chris@211 147 emit globalCentreFrameChanged(f);
Chris@180 148 }
Chris@180 149
Chris@1327 150 ZoomLevel
Chris@127 151 ViewManager::getGlobalZoom() const
Chris@127 152 {
Chris@127 153 #ifdef DEBUG_VIEW_MANAGER
Chris@682 154 cerr << "ViewManager::getGlobalZoom: returning " << m_globalZoom << endl;
Chris@127 155 #endif
Chris@127 156 return m_globalZoom;
Chris@127 157 }
Chris@127 158
Chris@902 159 sv_frame_t
Chris@127 160 ViewManager::getPlaybackFrame() const
Chris@127 161 {
Chris@1210 162 if (isRecording()) {
Chris@1210 163 m_playbackFrame = m_recordTarget->getRecordDuration();
Chris@1210 164 #ifdef DEBUG_VIEW_MANAGER
Chris@1210 165 cout << "ViewManager::getPlaybackFrame(recording) -> " << m_playbackFrame << endl;
Chris@1210 166 #endif
Chris@1210 167 } else if (isPlaying()) {
Chris@1266 168 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
Chris@1185 169 #ifdef DEBUG_VIEW_MANAGER
Chris@1185 170 cout << "ViewManager::getPlaybackFrame(playing) -> " << m_playbackFrame << endl;
Chris@1185 171 #endif
Chris@1185 172 } else {
Chris@1185 173 #ifdef DEBUG_VIEW_MANAGER
Chris@1185 174 cout << "ViewManager::getPlaybackFrame(not playing) -> " << m_playbackFrame << endl;
Chris@1185 175 #endif
Chris@127 176 }
Chris@127 177 return m_playbackFrame;
Chris@127 178 }
Chris@127 179
Chris@127 180 void
Chris@902 181 ViewManager::setPlaybackFrame(sv_frame_t f)
Chris@127 182 {
Chris@1185 183 #ifdef DEBUG_VIEW_MANAGER
Chris@1185 184 cerr << "ViewManager::setPlaybackFrame(" << f << ")" << endl;
Chris@1185 185 #endif
Chris@968 186 if (f < 0) f = 0;
Chris@127 187 if (m_playbackFrame != f) {
Chris@1266 188 m_playbackFrame = f;
Chris@1266 189 emit playbackFrameChanged(f);
Chris@1266 190 if (isPlaying()) {
Chris@1266 191 m_playSource->play(f);
Chris@1266 192 }
Chris@127 193 }
Chris@127 194 }
Chris@127 195
Chris@301 196 Model *
Chris@301 197 ViewManager::getPlaybackModel() const
Chris@301 198 {
Chris@301 199 return m_playbackModel;
Chris@301 200 }
Chris@301 201
Chris@301 202 void
Chris@301 203 ViewManager::setPlaybackModel(Model *model)
Chris@301 204 {
Chris@301 205 m_playbackModel = model;
Chris@301 206 }
Chris@301 207
Chris@902 208 sv_frame_t
Chris@902 209 ViewManager::alignPlaybackFrameToReference(sv_frame_t frame) const
Chris@333 210 {
Chris@830 211 #ifdef DEBUG_VIEW_MANAGER
Chris@830 212 cerr << "ViewManager::alignPlaybackFrameToReference(" << frame << "): playback model is " << m_playbackModel << endl;
Chris@830 213 #endif
Chris@830 214 if (!m_playbackModel) {
Chris@830 215 return frame;
Chris@830 216 } else {
Chris@902 217 sv_frame_t f = m_playbackModel->alignToReference(frame);
Chris@830 218 #ifdef DEBUG_VIEW_MANAGER
Chris@830 219 cerr << "aligned frame = " << f << endl;
Chris@830 220 #endif
Chris@830 221 return f;
Chris@830 222 }
Chris@333 223 }
Chris@333 224
Chris@902 225 sv_frame_t
Chris@902 226 ViewManager::alignReferenceToPlaybackFrame(sv_frame_t frame) const
Chris@333 227 {
Chris@830 228 #ifdef DEBUG_VIEW_MANAGER
Chris@830 229 cerr << "ViewManager::alignReferenceToPlaybackFrame(" << frame << "): playback model is " << m_playbackModel << endl;
Chris@830 230 #endif
Chris@830 231 if (!m_playbackModel) {
Chris@830 232 return frame;
Chris@830 233 } else {
Chris@902 234 sv_frame_t f = m_playbackModel->alignFromReference(frame);
Chris@830 235 #ifdef DEBUG_VIEW_MANAGER
Chris@830 236 cerr << "aligned frame = " << f << endl;
Chris@830 237 #endif
Chris@830 238 return f;
Chris@830 239 }
Chris@333 240 }
Chris@333 241
Chris@127 242 bool
Chris@127 243 ViewManager::haveInProgressSelection() const
Chris@127 244 {
Chris@127 245 return !m_inProgressSelection.isEmpty();
Chris@127 246 }
Chris@127 247
Chris@127 248 const Selection &
Chris@127 249 ViewManager::getInProgressSelection(bool &exclusive) const
Chris@127 250 {
Chris@127 251 exclusive = m_inProgressExclusive;
Chris@127 252 return m_inProgressSelection;
Chris@127 253 }
Chris@127 254
Chris@127 255 void
Chris@127 256 ViewManager::setInProgressSelection(const Selection &selection, bool exclusive)
Chris@127 257 {
Chris@127 258 m_inProgressExclusive = exclusive;
Chris@127 259 m_inProgressSelection = selection;
Chris@127 260 if (exclusive) clearSelections();
Chris@127 261 emit inProgressSelectionChanged();
Chris@127 262 }
Chris@127 263
Chris@127 264 void
Chris@127 265 ViewManager::clearInProgressSelection()
Chris@127 266 {
Chris@127 267 m_inProgressSelection = Selection();
Chris@127 268 emit inProgressSelectionChanged();
Chris@127 269 }
Chris@127 270
Chris@127 271 const MultiSelection &
Chris@127 272 ViewManager::getSelection() const
Chris@127 273 {
Chris@127 274 return m_selections;
Chris@127 275 }
Chris@127 276
Chris@127 277 const MultiSelection::SelectionList &
Chris@127 278 ViewManager::getSelections() const
Chris@127 279 {
Chris@127 280 return m_selections.getSelections();
Chris@127 281 }
Chris@127 282
Chris@127 283 void
Chris@127 284 ViewManager::setSelection(const Selection &selection)
Chris@127 285 {
Chris@127 286 MultiSelection ms(m_selections);
Chris@127 287 ms.setSelection(selection);
Chris@127 288 setSelections(ms);
Chris@127 289 }
Chris@127 290
Chris@127 291 void
Chris@127 292 ViewManager::addSelection(const Selection &selection)
Chris@127 293 {
Chris@127 294 MultiSelection ms(m_selections);
Chris@127 295 ms.addSelection(selection);
Chris@127 296 setSelections(ms);
Chris@127 297 }
Chris@127 298
Chris@127 299 void
Chris@762 300 ViewManager::addSelectionQuietly(const Selection &selection)
Chris@762 301 {
Chris@762 302 MultiSelection ms(m_selections);
Chris@762 303 ms.addSelection(selection);
Chris@762 304 setSelections(ms, true);
Chris@762 305 }
Chris@762 306
Chris@762 307 void
Chris@127 308 ViewManager::removeSelection(const Selection &selection)
Chris@127 309 {
Chris@127 310 MultiSelection ms(m_selections);
Chris@127 311 ms.removeSelection(selection);
Chris@127 312 setSelections(ms);
Chris@127 313 }
Chris@127 314
Chris@127 315 void
Chris@127 316 ViewManager::clearSelections()
Chris@127 317 {
Chris@127 318 MultiSelection ms(m_selections);
Chris@127 319 ms.clearSelections();
Chris@127 320 setSelections(ms);
Chris@127 321 }
Chris@127 322
Chris@127 323 void
Chris@762 324 ViewManager::setSelections(const MultiSelection &ms, bool quietly)
Chris@127 325 {
Chris@127 326 if (m_selections.getSelections() == ms.getSelections()) return;
Chris@127 327 SetSelectionCommand *command = new SetSelectionCommand(this, ms);
Chris@127 328 CommandHistory::getInstance()->addCommand(command);
Chris@762 329 if (!quietly) {
Chris@762 330 emit selectionChangedByUser();
Chris@762 331 }
Chris@127 332 }
Chris@127 333
Chris@902 334 sv_frame_t
Chris@902 335 ViewManager::constrainFrameToSelection(sv_frame_t frame) const
Chris@271 336 {
Chris@271 337 MultiSelection::SelectionList sl = getSelections();
Chris@271 338 if (sl.empty()) return frame;
Chris@271 339
Chris@368 340 for (MultiSelection::SelectionList::const_iterator i = sl.begin();
Chris@368 341 i != sl.end(); ++i) {
Chris@368 342
Chris@368 343 if (frame < i->getEndFrame()) {
Chris@368 344 if (frame < i->getStartFrame()) {
Chris@368 345 return i->getStartFrame();
Chris@368 346 } else {
Chris@368 347 return frame;
Chris@368 348 }
Chris@368 349 }
Chris@271 350 }
Chris@271 351
Chris@368 352 return sl.begin()->getStartFrame();
Chris@271 353 }
Chris@271 354
Chris@127 355 void
Chris@127 356 ViewManager::signalSelectionChange()
Chris@127 357 {
Chris@127 358 emit selectionChanged();
Chris@127 359 }
Chris@127 360
Chris@127 361 ViewManager::SetSelectionCommand::SetSelectionCommand(ViewManager *vm,
Chris@1266 362 const MultiSelection &ms) :
Chris@127 363 m_vm(vm),
Chris@127 364 m_oldSelection(vm->m_selections),
Chris@127 365 m_newSelection(ms)
Chris@127 366 {
Chris@127 367 }
Chris@127 368
Chris@127 369 ViewManager::SetSelectionCommand::~SetSelectionCommand() { }
Chris@127 370
Chris@127 371 void
Chris@127 372 ViewManager::SetSelectionCommand::execute()
Chris@127 373 {
Chris@127 374 m_vm->m_selections = m_newSelection;
Chris@127 375 m_vm->signalSelectionChange();
Chris@127 376 }
Chris@127 377
Chris@127 378 void
Chris@127 379 ViewManager::SetSelectionCommand::unexecute()
Chris@127 380 {
Chris@127 381 m_vm->m_selections = m_oldSelection;
Chris@127 382 m_vm->signalSelectionChange();
Chris@127 383 }
Chris@127 384
Chris@127 385 QString
Chris@127 386 ViewManager::SetSelectionCommand::getName() const
Chris@127 387 {
Chris@127 388 if (m_newSelection.getSelections().empty()) return tr("Clear Selection");
Chris@502 389 if (m_newSelection.getSelections().size() > 1) return tr("Select Multiple Regions");
Chris@502 390 else return tr("Select Region");
Chris@127 391 }
Chris@127 392
Chris@127 393 Selection
Chris@902 394 ViewManager::getContainingSelection(sv_frame_t frame, bool defaultToFollowing) const
Chris@127 395 {
Chris@127 396 return m_selections.getContainingSelection(frame, defaultToFollowing);
Chris@127 397 }
Chris@127 398
Chris@127 399 void
Chris@127 400 ViewManager::setToolMode(ToolMode mode)
Chris@127 401 {
Chris@127 402 m_toolMode = mode;
Chris@127 403
Chris@127 404 emit toolModeChanged();
Chris@502 405
Chris@502 406 switch (mode) {
Chris@502 407 case NavigateMode: emit activity(tr("Enter Navigate mode")); break;
Chris@502 408 case SelectMode: emit activity(tr("Enter Select mode")); break;
Chris@502 409 case EditMode: emit activity(tr("Enter Edit mode")); break;
Chris@502 410 case DrawMode: emit activity(tr("Enter Draw mode")); break;
Chris@502 411 case EraseMode: emit activity(tr("Enter Erase mode")); break;
Chris@502 412 case MeasureMode: emit activity(tr("Enter Measure mode")); break;
gyorgyf@645 413 case NoteEditMode: emit activity(tr("Enter NoteEdit mode")); break; // GF: NoteEditMode activity (I'm not yet certain why we need to emit this.)
Chris@502 414 };
Chris@127 415 }
Chris@127 416
Chris@711 417 ViewManager::ToolMode
Chris@711 418 ViewManager::getToolModeFor(const View *v) const
Chris@711 419 {
Chris@711 420 if (m_toolModeOverrides.find(v) == m_toolModeOverrides.end()) {
Chris@711 421 return getToolMode();
Chris@711 422 } else {
Chris@711 423 return m_toolModeOverrides.find(v)->second;
Chris@711 424 }
Chris@711 425 }
Chris@711 426
Chris@711 427 void
Chris@711 428 ViewManager::setToolModeFor(const View *v, ToolMode mode)
Chris@711 429 {
Chris@711 430 m_toolModeOverrides[v] = mode;
Chris@711 431 }
Chris@711 432
Chris@711 433 void
Chris@711 434 ViewManager::clearToolModeOverrides()
Chris@711 435 {
Chris@711 436 m_toolModeOverrides.clear();
Chris@711 437 }
Chris@711 438
Chris@127 439 void
Chris@127 440 ViewManager::setPlayLoopMode(bool mode)
Chris@127 441 {
Chris@177 442 if (m_playLoopMode != mode) {
Chris@127 443
Chris@177 444 m_playLoopMode = mode;
Chris@177 445
Chris@177 446 emit playLoopModeChanged();
Chris@177 447 emit playLoopModeChanged(mode);
Chris@502 448
Chris@502 449 if (mode) emit activity(tr("Switch on Loop mode"));
Chris@502 450 else emit activity(tr("Switch off Loop mode"));
Chris@177 451 }
Chris@127 452 }
Chris@127 453
Chris@127 454 void
Chris@127 455 ViewManager::setPlaySelectionMode(bool mode)
Chris@127 456 {
Chris@177 457 if (m_playSelectionMode != mode) {
Chris@127 458
Chris@177 459 m_playSelectionMode = mode;
Chris@177 460
Chris@177 461 emit playSelectionModeChanged();
Chris@177 462 emit playSelectionModeChanged(mode);
Chris@502 463
Chris@502 464 if (mode) emit activity(tr("Switch on Play Selection mode"));
Chris@502 465 else emit activity(tr("Switch off Play Selection mode"));
Chris@177 466 }
Chris@127 467 }
Chris@127 468
Chris@301 469 void
Chris@301 470 ViewManager::setPlaySoloMode(bool mode)
Chris@301 471 {
Chris@301 472 if (m_playSoloMode != mode) {
Chris@301 473
Chris@301 474 m_playSoloMode = mode;
Chris@301 475
Chris@301 476 emit playSoloModeChanged();
Chris@301 477 emit playSoloModeChanged(mode);
Chris@502 478
Chris@502 479 if (mode) emit activity(tr("Switch on Play Solo mode"));
Chris@502 480 else emit activity(tr("Switch off Play Solo mode"));
Chris@301 481 }
Chris@301 482 }
Chris@301 483
Chris@314 484 void
Chris@314 485 ViewManager::setAlignMode(bool mode)
Chris@314 486 {
Chris@314 487 if (m_alignMode != mode) {
Chris@314 488
Chris@314 489 m_alignMode = mode;
Chris@314 490
Chris@314 491 emit alignModeChanged();
Chris@314 492 emit alignModeChanged(mode);
Chris@502 493
Chris@502 494 if (mode) emit activity(tr("Switch on Alignment mode"));
Chris@502 495 else emit activity(tr("Switch off Alignment mode"));
Chris@314 496 }
Chris@314 497 }
Chris@314 498
Chris@902 499 sv_samplerate_t
Chris@224 500 ViewManager::getPlaybackSampleRate() const
Chris@224 501 {
Chris@224 502 if (m_playSource) {
Chris@224 503 return m_playSource->getSourceSampleRate();
Chris@224 504 }
Chris@224 505 return 0;
Chris@224 506 }
Chris@224 507
Chris@902 508 sv_samplerate_t
Chris@1181 509 ViewManager::getDeviceSampleRate() const
Chris@127 510 {
Chris@127 511 if (m_playSource) {
Chris@1266 512 return m_playSource->getDeviceSampleRate();
Chris@127 513 }
Chris@127 514 return 0;
Chris@127 515 }
Chris@127 516
Chris@127 517 void
Chris@127 518 ViewManager::setAudioPlaySource(AudioPlaySource *source)
Chris@127 519 {
Chris@127 520 if (!m_playSource) {
Chris@1266 521 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
Chris@127 522 }
Chris@127 523 m_playSource = source;
Chris@127 524 }
Chris@127 525
Chris@127 526 void
Chris@1210 527 ViewManager::setAudioRecordTarget(AudioRecordTarget *target)
Chris@1210 528 {
Chris@1210 529 if (!m_recordTarget) {
Chris@1266 530 QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
Chris@1210 531 }
Chris@1210 532 m_recordTarget = target;
Chris@1210 533 }
Chris@1210 534
Chris@1210 535 void
Chris@248 536 ViewManager::playStatusChanged(bool /* playing */)
Chris@127 537 {
Chris@689 538 #ifdef DEBUG_VIEW_MANAGER
Chris@689 539 cerr << "ViewManager::playStatusChanged" << endl;
Chris@689 540 #endif
Chris@127 541 checkPlayStatus();
Chris@127 542 }
Chris@127 543
Chris@127 544 void
Chris@1210 545 ViewManager::recordStatusChanged(bool /* recording */)
Chris@1210 546 {
Chris@1210 547 #ifdef DEBUG_VIEW_MANAGER
Chris@1210 548 cerr << "ViewManager::recordStatusChanged" << endl;
Chris@1210 549 #endif
Chris@1210 550 checkPlayStatus();
Chris@1210 551 }
Chris@1210 552
Chris@1210 553 void
Chris@127 554 ViewManager::checkPlayStatus()
Chris@127 555 {
Chris@1210 556 if (isRecording()) {
Chris@1210 557
Chris@1266 558 float left = 0, right = 0;
Chris@1266 559 if (m_recordTarget->getInputLevels(left, right)) {
Chris@1266 560 if (left != m_lastLeft || right != m_lastRight) {
Chris@1266 561 emit monitoringLevelsChanged(left, right);
Chris@1266 562 m_lastLeft = left;
Chris@1266 563 m_lastRight = right;
Chris@1266 564 }
Chris@1266 565 }
Chris@1210 566
Chris@1266 567 m_playbackFrame = m_recordTarget->getRecordDuration();
Chris@1210 568
Chris@1210 569 #ifdef DEBUG_VIEW_MANAGER
Chris@1266 570 cerr << "ViewManager::checkPlayStatus: Recording, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << endl;
Chris@1210 571 #endif
Chris@1210 572
Chris@1266 573 emit playbackFrameChanged(m_playbackFrame);
Chris@1210 574
Chris@1266 575 QTimer::singleShot(500, this, SLOT(checkPlayStatus()));
Chris@1210 576
Chris@1210 577 } else if (isPlaying()) {
Chris@127 578
Chris@1266 579 float left = 0, right = 0;
Chris@1266 580 if (m_playSource->getOutputLevels(left, right)) {
Chris@1266 581 if (left != m_lastLeft || right != m_lastRight) {
Chris@1266 582 emit monitoringLevelsChanged(left, right);
Chris@1266 583 m_lastLeft = left;
Chris@1266 584 m_lastRight = right;
Chris@1266 585 }
Chris@1266 586 }
Chris@127 587
Chris@1266 588 m_playbackFrame = m_playSource->getCurrentPlayingFrame();
Chris@127 589
Chris@127 590 #ifdef DEBUG_VIEW_MANAGER
Chris@1266 591 cerr << "ViewManager::checkPlayStatus: Playing, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << endl;
Chris@127 592 #endif
Chris@127 593
Chris@1266 594 emit playbackFrameChanged(m_playbackFrame);
Chris@127 595
Chris@1266 596 QTimer::singleShot(20, this, SLOT(checkPlayStatus()));
Chris@127 597
Chris@127 598 } else {
Chris@127 599
Chris@1266 600 if (m_lastLeft != 0.0 || m_lastRight != 0.0) {
Chris@1266 601 emit monitoringLevelsChanged(0.0, 0.0);
Chris@1266 602 m_lastLeft = 0.0;
Chris@1266 603 m_lastRight = 0.0;
Chris@1266 604 }
Chris@127 605
Chris@127 606 #ifdef DEBUG_VIEW_MANAGER
Chris@1266 607 cerr << "ViewManager::checkPlayStatus: Not playing or recording" << endl;
Chris@127 608 #endif
Chris@127 609 }
Chris@127 610 }
Chris@127 611
Chris@127 612 bool
Chris@127 613 ViewManager::isPlaying() const
Chris@127 614 {
Chris@127 615 return m_playSource && m_playSource->isPlaying();
Chris@127 616 }
Chris@127 617
Chris@1210 618 bool
Chris@1210 619 ViewManager::isRecording() const
Chris@1210 620 {
Chris@1210 621 return m_recordTarget && m_recordTarget->isRecording();
Chris@1210 622 }
Chris@1210 623
Chris@127 624 void
Chris@902 625 ViewManager::viewCentreFrameChanged(sv_frame_t f, bool locked,
Chris@211 626 PlaybackFollowMode mode)
Chris@127 627 {
Chris@211 628 View *v = dynamic_cast<View *>(sender());
Chris@211 629
Chris@398 630 #ifdef DEBUG_VIEW_MANAGER
Chris@682 631 cerr << "ViewManager::viewCentreFrameChanged(" << f << ", " << locked << ", " << mode << "), view is " << v << endl;
Chris@398 632 #endif
Chris@394 633
Chris@127 634 if (locked) {
Chris@211 635 m_globalCentreFrame = f;
Chris@211 636 emit globalCentreFrameChanged(f);
Chris@211 637 } else {
Chris@211 638 if (v) emit viewCentreFrameChanged(v, f);
Chris@127 639 }
Chris@127 640
Chris@815 641 if (!dynamic_cast<Overview *>(v) || (mode != PlaybackIgnore)) {
Chris@527 642 if (m_mainModelSampleRate != 0) {
Chris@527 643 emit activity(tr("Scroll to %1")
Chris@527 644 .arg(RealTime::frame2RealTime
Chris@527 645 (f, m_mainModelSampleRate).toText().c_str()));
Chris@527 646 }
Chris@503 647 }
Chris@502 648
Chris@815 649 if (mode == PlaybackScrollPageWithCentre ||
Chris@815 650 mode == PlaybackScrollContinuous) {
Chris@815 651 seek(f);
Chris@211 652 }
Chris@211 653 }
Chris@211 654
Chris@211 655 void
Chris@902 656 ViewManager::seek(sv_frame_t f)
Chris@211 657 {
Chris@127 658 #ifdef DEBUG_VIEW_MANAGER
Chris@682 659 cerr << "ViewManager::seek(" << f << ")" << endl;
Chris@127 660 #endif
Chris@127 661
Chris@1210 662 if (isRecording()) {
Chris@1210 663 // ignore
Chris@1210 664 #ifdef DEBUG_VIEW_MANAGER
Chris@1210 665 cerr << "ViewManager::seek: Ignoring during recording" << endl;
Chris@1210 666 #endif
Chris@1210 667 return;
Chris@1210 668 }
Chris@1210 669
Chris@1210 670 if (isPlaying()) {
Chris@1266 671 sv_frame_t playFrame = m_playSource->getCurrentPlayingFrame();
Chris@1266 672 sv_frame_t diff = std::max(f, playFrame) - std::min(f, playFrame);
Chris@1266 673 if (diff > 20000) {
Chris@1266 674 m_playbackFrame = f;
Chris@1266 675 m_playSource->play(f);
Chris@127 676 #ifdef DEBUG_VIEW_MANAGER
Chris@1266 677 cerr << "ViewManager::seek: reseeking from " << playFrame << " to " << f << endl;
Chris@127 678 #endif
Chris@211 679 emit playbackFrameChanged(f);
Chris@1266 680 }
Chris@127 681 } else {
Chris@211 682 if (m_playbackFrame != f) {
Chris@211 683 m_playbackFrame = f;
Chris@211 684 emit playbackFrameChanged(f);
Chris@211 685 }
Chris@127 686 }
Chris@127 687 }
Chris@127 688
Chris@127 689 void
Chris@1327 690 ViewManager::viewZoomLevelChanged(ZoomLevel z, bool locked)
Chris@127 691 {
Chris@222 692 View *v = dynamic_cast<View *>(sender());
Chris@222 693
Chris@222 694 if (!v) {
Chris@587 695 SVDEBUG << "ViewManager::viewZoomLevelChanged: WARNING: sender is not a view" << endl;
Chris@222 696 return;
Chris@222 697 }
Chris@222 698
Chris@222 699 //!!! emit zoomLevelChanged();
Chris@133 700
Chris@127 701 if (locked) {
Chris@1266 702 m_globalZoom = z;
Chris@127 703 }
Chris@127 704
Chris@127 705 #ifdef DEBUG_VIEW_MANAGER
Chris@682 706 cerr << "ViewManager::viewZoomLevelChanged(" << v << ", " << z << ", " << locked << ")" << endl;
Chris@127 707 #endif
Chris@222 708
Chris@222 709 emit viewZoomLevelChanged(v, z, locked);
Chris@503 710
Chris@503 711 if (!dynamic_cast<Overview *>(v)) {
Chris@1327 712 if (z.zone == ZoomLevel::FramesPerPixel) {
Chris@1327 713 emit activity(tr("Zoom to %n sample(s) per pixel", "", z.level));
Chris@1327 714 } else {
Chris@1327 715 emit activity(tr("Zoom to %n pixels per sample", "", z.level));
Chris@1327 716 }
Chris@503 717 }
Chris@127 718 }
Chris@127 719
Chris@127 720 void
Chris@127 721 ViewManager::setOverlayMode(OverlayMode mode)
Chris@127 722 {
Chris@127 723 if (m_overlayMode != mode) {
Chris@127 724 m_overlayMode = mode;
Chris@127 725 emit overlayModeChanged();
Chris@502 726 emit activity(tr("Change overlay level"));
Chris@127 727 }
Chris@133 728
Chris@133 729 QSettings settings;
Chris@133 730 settings.beginGroup("MainWindow");
Chris@133 731 settings.setValue("overlay-mode", int(m_overlayMode));
Chris@133 732 settings.endGroup();
Chris@127 733 }
Chris@127 734
Chris@133 735 void
Chris@133 736 ViewManager::setZoomWheelsEnabled(bool enabled)
Chris@133 737 {
Chris@133 738 if (m_zoomWheelsEnabled != enabled) {
Chris@133 739 m_zoomWheelsEnabled = enabled;
Chris@133 740 emit zoomWheelsEnabledChanged();
Chris@502 741 if (enabled) emit activity("Show zoom wheels");
Chris@502 742 else emit activity("Hide zoom wheels");
Chris@133 743 }
Chris@127 744
Chris@133 745 QSettings settings;
Chris@133 746 settings.beginGroup("MainWindow");
Chris@133 747 settings.setValue("zoom-wheels-enabled", m_zoomWheelsEnabled);
Chris@133 748 settings.endGroup();
Chris@133 749 }
Chris@133 750
Chris@292 751 void
Chris@607 752 ViewManager::setShowCentreLine(bool show)
Chris@607 753 {
Chris@607 754 if (m_showCentreLine != show) {
Chris@607 755 m_showCentreLine = show;
Chris@607 756 emit showCentreLineChanged();
Chris@607 757 if (show) emit activity("Show centre line");
Chris@607 758 else emit activity("Hide centre line");
Chris@607 759 }
Chris@607 760
Chris@607 761 QSettings settings;
Chris@607 762 settings.beginGroup("MainWindow");
Chris@607 763 settings.setValue("show-centre-line", int(m_showCentreLine));
Chris@607 764 settings.endGroup();
Chris@607 765 }
Chris@607 766
Chris@607 767 void
Chris@292 768 ViewManager::setGlobalDarkBackground(bool dark)
Chris@292 769 {
Chris@292 770 // also save the current palette, in case the user has changed it
Chris@292 771 // since construction
Chris@292 772 if (getGlobalDarkBackground()) {
Chris@292 773 m_darkPalette = QApplication::palette();
Chris@292 774 } else {
Chris@292 775 m_lightPalette = QApplication::palette();
Chris@292 776 }
Chris@292 777
Chris@737 778 #ifndef Q_OS_MAC
Chris@292 779 if (dark) {
Chris@292 780 QApplication::setPalette(m_darkPalette);
Chris@292 781 } else {
Chris@292 782 QApplication::setPalette(m_lightPalette);
Chris@292 783 }
Chris@737 784 #endif
Chris@292 785 }
Chris@292 786
Chris@292 787 bool
Chris@292 788 ViewManager::getGlobalDarkBackground() const
Chris@292 789 {
Chris@292 790 bool dark = false;
Chris@292 791 QColor windowBg = QApplication::palette().color(QPalette::Window);
Chris@292 792 if (windowBg.red() + windowBg.green() + windowBg.blue() < 384) {
Chris@292 793 dark = true;
Chris@292 794 }
Chris@292 795 return dark;
Chris@292 796 }
Chris@292 797
Chris@894 798 int
Chris@894 799 ViewManager::scalePixelSize(int pixels)
Chris@894 800 {
Chris@908 801 static double ratio = 0.0;
Chris@908 802 if (ratio == 0.0) {
Chris@908 803 double baseEm;
Chris@894 804 #ifdef Q_OS_MAC
Chris@908 805 baseEm = 17.0;
Chris@894 806 #else
Chris@908 807 baseEm = 15.0;
Chris@894 808 #endif
Chris@908 809 double em = QFontMetrics(QFont()).height();
Chris@894 810 ratio = em / baseEm;
Chris@1224 811
Chris@1224 812 SVDEBUG << "ViewManager::scalePixelSize: ratio is " << ratio
Chris@1224 813 << " (em = " << em << ")" << endl;
Chris@894 814 }
Chris@894 815
Chris@894 816 int scaled = int(pixels * ratio + 0.5);
Chris@1273 817 // SVDEBUG << "scaledSize: " << pixels << " -> " << scaled << " at ratio " << ratio << endl;
Chris@894 818 if (pixels != 0 && scaled == 0) scaled = 1;
Chris@894 819 return scaled;
Chris@894 820 }
Chris@946 821