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