File: ViewManager.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (909 lines) | stat: -rw-r--r-- 24,899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006 Chris Cannam and QMUL.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "ViewManager.h"
#include "base/AudioPlaySource.h"
#include "base/AudioRecordTarget.h"
#include "base/RealTime.h"
#include "data/model/Model.h"
#include "widgets/CommandHistory.h"
#include "View.h"
#include "Overview.h"

#include "system/System.h"

#include <QSettings>
#include <QApplication>
#include <QStyleFactory>

//#define DEBUG_VIEW_MANAGER 1

namespace sv {

ViewManager::ViewManager() :
    m_playSource(nullptr),
    m_recordTarget(nullptr),
    m_globalCentreFrame(0),
    m_globalZoom(ZoomLevel::FramesPerPixel, 1024),
    m_playbackFrame(0),
    m_mainModelSampleRate(0),
    m_lastLeft(0), 
    m_lastRight(0),
    m_inProgressExclusive(true),
    m_toolMode(NavigateMode),
    m_playLoopMode(false),
    m_playSelectionMode(false),
    m_playSoloMode(false),
    m_alignMode(false),
    m_overlayMode(StandardOverlays),
    m_zoomWheelsEnabled(true),
    m_opportunisticEditingEnabled(true),
    m_showCentreLine(true),
    m_illuminateLocalFeatures(true),
    m_showWorkTitle(false),
    m_showDuration(true),
    m_lightPalette(QApplication::palette()),
    m_darkPalette(QApplication::palette())
{
    QSettings settings;
    settings.beginGroup("MainWindow");
    m_overlayMode = OverlayMode
        (settings.value("overlay-mode", int(m_overlayMode)).toInt());

    if (m_overlayMode != NoOverlays &&
        m_overlayMode != StandardOverlays &&
        m_overlayMode != AllOverlays) {
        m_overlayMode = StandardOverlays;
    }

    m_zoomWheelsEnabled =
        settings.value("zoom-wheels-enabled", m_zoomWheelsEnabled).toBool();
    m_showCentreLine =
        settings.value("show-centre-line", m_showCentreLine).toBool();
    settings.endGroup();

    if (getGlobalDarkBackground()) {
        // i.e. widgets are already dark; create a light palette in
        // case we are asked to switch to it, but don't create a dark
        // one because it will be assigned from the actual application
        // palette if we switch
        
        m_lightPalette = QPalette(QColor("#000000"),  // WindowText
                                  QColor("#dddfe4"),  // Button
                                  QColor("#ffffff"),  // Light
                                  QColor("#555555"),  // Dark
                                  QColor("#c7c7c7"),  // Mid
                                  QColor("#000000"),  // Text
                                  QColor("#ffffff"),  // BrightText
                                  QColor("#ffffff"),  // Base
                                  QColor("#efefef")); // Window

        m_lightPalette.setColor(QPalette::Highlight, Qt::darkBlue);
        if (!OSReportsDarkThemeActive()) {
            int r, g, b;
            if (OSQueryAccentColour(&r, &g, &b)) {
                m_lightPalette.setColor(QPalette::Highlight, QColor(r, g, b));
            }
        }

    } else {
        // i.e. widgets are currently light; create a dark palette in
        // case we are asked to switch to it, but don't create a light
        // one because it will be assigned from the actual application
        // palette if we switch
        
        m_darkPalette = QPalette(QColor("#f0f0f0"),  // WindowText
                                 QColor("#3e3e3e"),  // Button
                                 QColor("#808080"),  // Light
                                 QColor("#1e1e1e"),  // Dark
                                 QColor("#404040"),  // Mid
                                 QColor("#f0f0f0"),  // Text
                                 QColor("#ffffff"),  // BrightText
                                 QColor("#000000"),  // Base
                                 QColor("#202020")); // Window

        m_darkPalette.setColor(QPalette::Highlight, QColor(25, 130, 220));
        if (OSReportsDarkThemeActive()) {
            int r, g, b;
            if (OSQueryAccentColour(&r, &g, &b)) {
                m_darkPalette.setColor(QPalette::Highlight, QColor(r, g, b));
            }
        }
        
        m_darkPalette.setColor(QPalette::Link, QColor(50, 175, 255));
        m_darkPalette.setColor(QPalette::LinkVisited, QColor(50, 175, 255));
        
        m_darkPalette.setColor(QPalette::Disabled, QPalette::WindowText,
                               QColor("#808080"));
        m_darkPalette.setColor(QPalette::Disabled, QPalette::Text,
                               QColor("#808080"));
        m_darkPalette.setColor(QPalette::Disabled, QPalette::Shadow,
                               QColor("#000000"));
    }
}

ViewManager::~ViewManager()
{
}

sv_frame_t
ViewManager::getGlobalCentreFrame() const
{
#ifdef DEBUG_VIEW_MANAGER
    SVCERR << "ViewManager::getGlobalCentreFrame: returning " << m_globalCentreFrame << endl;
#endif
    return m_globalCentreFrame;
}

void
ViewManager::setGlobalCentreFrame(sv_frame_t f)
{
#ifdef DEBUG_VIEW_MANAGER
    SVCERR << "ViewManager::setGlobalCentreFrame to " << f << endl;
#endif
    m_globalCentreFrame = f;
    emit globalCentreFrameChanged(f);
}

ZoomLevel
ViewManager::getGlobalZoom() const
{
#ifdef DEBUG_VIEW_MANAGER
    SVCERR << "ViewManager::getGlobalZoom: returning " << m_globalZoom << endl;
#endif
    return m_globalZoom;
}

sv_frame_t
ViewManager::getPlaybackFrame() const
{
    if (isRecording()) {
        m_playbackFrame = m_recordTarget->getRecordDuration();
#ifdef DEBUG_VIEW_MANAGER
        SVCERR << "ViewManager::getPlaybackFrame(recording) -> " << m_playbackFrame << endl;
#endif
    } else if (isPlaying()) {
        m_playbackFrame = m_playSource->getCurrentPlayingFrame();
#ifdef DEBUG_VIEW_MANAGER
        SVCERR << "ViewManager::getPlaybackFrame(playing) -> " << m_playbackFrame << endl;
#endif
    } else {
#ifdef DEBUG_VIEW_MANAGER
        SVCERR << "ViewManager::getPlaybackFrame(not playing) -> " << m_playbackFrame << endl;
#endif
    }
    return m_playbackFrame;
}

void
ViewManager::setPlaybackFrame(sv_frame_t f)
{
#ifdef DEBUG_VIEW_MANAGER
    SVCERR << "ViewManager::setPlaybackFrame(" << f << ")" << endl;
#endif
    if (f < 0) f = 0;
    if (m_playbackFrame != f) {
        m_playbackFrame = f;
        emit playbackFrameChanged(f);
        if (isPlaying()) {
            m_playSource->play(f);
        }
    }
}

ModelId
ViewManager::getPlaybackModel() const
{
    return m_playbackModel;
}

void
ViewManager::setPlaybackModel(ModelId model)
{
    m_playbackModel = model;
}

sv_frame_t
ViewManager::alignPlaybackFrameToReference(sv_frame_t frame) const
{
#ifdef DEBUG_VIEW_MANAGER
    SVCERR << "ViewManager::alignPlaybackFrameToReference(" << frame << "): playback model is " << m_playbackModel << endl;
#endif
    if (m_playbackModel.isNone() || !m_alignMode) {
        return frame;
    } else {
        auto playbackModel = ModelById::get(m_playbackModel);
        if (!playbackModel) {
            return frame;
        } 
        sv_frame_t f = playbackModel->alignToReference(frame);
#ifdef DEBUG_VIEW_MANAGER
        SVCERR << "aligned frame = " << f << endl;
#endif
        return f;
    }
}

sv_frame_t
ViewManager::alignReferenceToPlaybackFrame(sv_frame_t frame) const
{
#ifdef DEBUG_VIEW_MANAGER
    SVCERR << "ViewManager::alignReferenceToPlaybackFrame(" << frame << "): playback model is " << m_playbackModel << endl;
#endif
    if (m_playbackModel.isNone() || !m_alignMode) {
        return frame;
    } else {
        auto playbackModel = ModelById::get(m_playbackModel);
        if (!playbackModel) {
            return frame;
        } 
        sv_frame_t f = playbackModel->alignFromReference(frame);
#ifdef DEBUG_VIEW_MANAGER
        SVCERR << "aligned frame = " << f << endl;
#endif
        return f;
    }
}

bool
ViewManager::haveInProgressSelection() const
{
    return !m_inProgressSelection.isEmpty();
}

const Selection &
ViewManager::getInProgressSelection(bool &exclusive) const
{
    exclusive = m_inProgressExclusive;
    return m_inProgressSelection;
}

void
ViewManager::setInProgressSelection(const Selection &selection, bool exclusive)
{
    m_inProgressExclusive = exclusive;
    m_inProgressSelection = selection;
    if (exclusive) clearSelections();
    emit inProgressSelectionChanged();
}

void
ViewManager::clearInProgressSelection()
{
    m_inProgressSelection = Selection();
    emit inProgressSelectionChanged();
}

MultiSelection
ViewManager::getSelection() const
{
    return m_selections;
}

MultiSelection::SelectionList
ViewManager::getSelections() const
{
    return m_selections.getSelections();
}

void
ViewManager::setSelection(const Selection &selection)
{
    MultiSelection ms(m_selections);
    ms.setSelection(selection);
    setSelections(ms);
}

void
ViewManager::addSelection(const Selection &selection)
{
    MultiSelection ms(m_selections);
    ms.addSelection(selection);
    setSelections(ms);
}

void
ViewManager::addSelectionQuietly(const Selection &selection)
{
    MultiSelection ms(m_selections);
    ms.addSelection(selection);
    setSelections(ms, true);
}

void
ViewManager::removeSelection(const Selection &selection)
{
    MultiSelection ms(m_selections);
    ms.removeSelection(selection);
    setSelections(ms);
}

void
ViewManager::clearSelections()
{
    MultiSelection ms(m_selections);
    ms.clearSelections();
    setSelections(ms);
}

void
ViewManager::setSelections(const MultiSelection &ms, bool quietly)
{
    if (m_selections.getSelections() == ms.getSelections()) return;
    SetSelectionCommand *command = new SetSelectionCommand(this, ms);
    CommandHistory::getInstance()->addCommand(command);
    if (!quietly) {
        emit selectionChangedByUser();
    }
}

sv_frame_t
ViewManager::constrainFrameToSelection(sv_frame_t frame) const
{
    MultiSelection::SelectionList sl = getSelections();
    if (sl.empty()) return frame;

    for (MultiSelection::SelectionList::const_iterator i = sl.begin();
         i != sl.end(); ++i) {

        if (frame < i->getEndFrame()) {
            if (frame < i->getStartFrame()) {
                return i->getStartFrame();
            } else {
                return frame;
            }
        }
    }

    return sl.begin()->getStartFrame();
}

void
ViewManager::signalSelectionChange()
{
    emit selectionChanged();
}

ViewManager::SetSelectionCommand::SetSelectionCommand(ViewManager *vm,
                                                      const MultiSelection &ms) :
    m_vm(vm),
    m_oldSelection(vm->m_selections),
    m_newSelection(ms)
{
}

ViewManager::SetSelectionCommand::~SetSelectionCommand() { }

void
ViewManager::SetSelectionCommand::execute()
{
    m_vm->m_selections = m_newSelection;
    m_vm->signalSelectionChange();
}

void
ViewManager::SetSelectionCommand::unexecute()
{
    m_vm->m_selections = m_oldSelection;
    m_vm->signalSelectionChange();
}

QString
ViewManager::SetSelectionCommand::getName() const
{
    if (m_newSelection.getSelections().empty()) return tr("Clear Selection");
    if (m_newSelection.getSelections().size() > 1) return tr("Select Multiple Regions");
    else return tr("Select Region");
}

Selection
ViewManager::getContainingSelection(sv_frame_t frame, bool defaultToFollowing) const
{
    return m_selections.getContainingSelection(frame, defaultToFollowing);
}

void
ViewManager::setToolMode(ToolMode mode)
{
    m_toolMode = mode;

    emit toolModeChanged();

    switch (mode) {
    case NavigateMode: emit activity(tr("Enter Navigate mode")); break;
    case SelectMode: emit activity(tr("Enter Select mode")); break;
    case EditMode: emit activity(tr("Enter Edit mode")); break;
    case DrawMode: emit activity(tr("Enter Draw mode")); break;
    case EraseMode: emit activity(tr("Enter Erase mode")); break;
    case MeasureMode: emit activity(tr("Enter Measure mode")); break;
    case NoteEditMode: emit activity(tr("Enter NoteEdit mode")); break; // GF: NoteEditMode activity (I'm not yet certain why we need to emit this.)
    };
}

ViewManager::ToolMode
ViewManager::getToolModeFor(const View *v) const
{
    if (m_toolModeOverrides.find(v) == m_toolModeOverrides.end()) {
        return getToolMode();
    } else {
        return m_toolModeOverrides.find(v)->second;
    }
}

void
ViewManager::setToolModeFor(const View *v, ToolMode mode)
{
    m_toolModeOverrides[v] = mode;
}

void
ViewManager::clearToolModeOverrides()
{
    m_toolModeOverrides.clear();
}

void
ViewManager::setPlayLoopMode(bool mode)
{
    if (m_playLoopMode != mode) {

        m_playLoopMode = mode;

        emit playLoopModeChanged();
        emit playLoopModeChanged(mode);

        if (mode) emit activity(tr("Switch on Loop mode"));
        else emit activity(tr("Switch off Loop mode"));
    }
}

void
ViewManager::setPlaySelectionMode(bool mode)
{
    if (m_playSelectionMode != mode) {

        m_playSelectionMode = mode;

        emit playSelectionModeChanged();
        emit playSelectionModeChanged(mode);

        if (mode) emit activity(tr("Switch on Play Selection mode"));
        else emit activity(tr("Switch off Play Selection mode"));
    }
}

void
ViewManager::setPlaySoloMode(bool mode)
{
    if (m_playSoloMode != mode) {

        m_playSoloMode = mode;

        emit playSoloModeChanged();
        emit playSoloModeChanged(mode);

        if (mode) emit activity(tr("Switch on Play Solo mode"));
        else emit activity(tr("Switch off Play Solo mode"));
    }
}

void
ViewManager::setAlignMode(bool mode)
{
    if (m_alignMode != mode) {

        m_alignMode = mode;

        emit alignModeChanged();
        emit alignModeChanged(mode);

        if (mode) emit activity(tr("Switch on Alignment mode"));
        else emit activity(tr("Switch off Alignment mode"));
    }
}

sv_samplerate_t 
ViewManager::getPlaybackSampleRate() const
{
    if (m_playSource) {
        return m_playSource->getSourceSampleRate();
    }
    return 0;
}

sv_samplerate_t
ViewManager::getDeviceSampleRate() const
{
    if (m_playSource) {
        return m_playSource->getDeviceSampleRate();
    }
    return 0;
}

void
ViewManager::setAudioPlaySource(AudioPlaySource *source)
{
    if (!m_playSource) {
        QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
    }
    m_playSource = source;
}

void
ViewManager::setAudioRecordTarget(AudioRecordTarget *target)
{
    if (!m_recordTarget) {
        QTimer::singleShot(100, this, SLOT(checkPlayStatus()));
    }
    m_recordTarget = target;
}

void
ViewManager::playStatusChanged(bool /* playing */)
{
#ifdef DEBUG_VIEW_MANAGER
    SVCERR << "ViewManager::playStatusChanged" << endl;
#endif
    checkPlayStatus();
}

void
ViewManager::recordStatusChanged(bool /* recording */)
{
#ifdef DEBUG_VIEW_MANAGER
    SVCERR << "ViewManager::recordStatusChanged" << endl;
#endif
    checkPlayStatus();
}

void
ViewManager::checkPlayStatus()
{
    if (isRecording()) {

        float left = 0, right = 0;
        if (m_recordTarget->getInputLevels(left, right)) {
            if (left != m_lastLeft || right != m_lastRight) {
                emit monitoringLevelsChanged(left, right);
                m_lastLeft = left;
                m_lastRight = right;
            }
        }

        m_playbackFrame = m_recordTarget->getRecordDuration();

#ifdef DEBUG_VIEW_MANAGER
        SVCERR << "ViewManager::checkPlayStatus: Recording, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << endl;
#endif

        emit playbackFrameChanged(m_playbackFrame);

        QTimer::singleShot(500, this, SLOT(checkPlayStatus()));

    } else if (isPlaying()) {

        float left = 0, right = 0;
        if (m_playSource->getOutputLevels(left, right)) {
            if (left != m_lastLeft || right != m_lastRight) {
                emit monitoringLevelsChanged(left, right);
                m_lastLeft = left;
                m_lastRight = right;
            }
        }

        m_playbackFrame = m_playSource->getCurrentPlayingFrame();

#ifdef DEBUG_VIEW_MANAGER
        SVCERR << "ViewManager::checkPlayStatus: Playing, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << endl;
#endif

        emit playbackFrameChanged(m_playbackFrame);

        QTimer::singleShot(20, this, SLOT(checkPlayStatus()));

    } else {

        if (m_lastLeft != 0.0 || m_lastRight != 0.0) {
            emit monitoringLevelsChanged(0.0, 0.0);
            m_lastLeft = 0.0;
            m_lastRight = 0.0;
        }

#ifdef DEBUG_VIEW_MANAGER
        SVCERR << "ViewManager::checkPlayStatus: Not playing or recording" << endl;
#endif
    }
}

bool
ViewManager::isPlaying() const
{
    return m_playSource && m_playSource->isPlaying();
}

bool
ViewManager::isRecording() const
{
    return m_recordTarget && m_recordTarget->isRecording();
}

void
ViewManager::viewCentreFrameChanged(sv_frame_t f, bool locked,
                                    PlaybackFollowMode mode)
{
    View *v = dynamic_cast<View *>(sender());

#ifdef DEBUG_VIEW_MANAGER
    SVCERR << "ViewManager::viewCentreFrameChanged(" << f << ", " << locked << ", " << mode << "), view is " << v << endl;
#endif

    if (locked) {
        m_globalCentreFrame = f;
        emit globalCentreFrameChanged(f);
    } else {
        if (v) emit viewCentreFrameChanged(v, f);
    }

    if (!dynamic_cast<Overview *>(v) || (mode != PlaybackIgnore)) {
        if (m_mainModelSampleRate != 0) {
            emit activity(tr("Scroll to %1")
                          .arg(RealTime::frame2RealTime
                               (f, m_mainModelSampleRate).toText().c_str()));
        }
    }

    if (mode == PlaybackScrollPageWithCentre ||
        mode == PlaybackScrollContinuous) {
        seek(f);
    }
}

void
ViewManager::seek(sv_frame_t f)
{
#ifdef DEBUG_VIEW_MANAGER 
    SVCERR << "ViewManager::seek(" << f << ")" << endl;
#endif

    if (isRecording()) {
        // ignore
#ifdef DEBUG_VIEW_MANAGER
        SVCERR << "ViewManager::seek: Ignoring during recording" << endl;
#endif
        return;
    }
    
    if (isPlaying()) {
        sv_frame_t playFrame = m_playSource->getCurrentPlayingFrame();
        sv_frame_t diff = std::max(f, playFrame) - std::min(f, playFrame);
        if (diff > 20000) {
            m_playbackFrame = f;
            m_playSource->play(f);
#ifdef DEBUG_VIEW_MANAGER 
            SVCERR << "ViewManager::seek: reseeking from " << playFrame << " to " << f << endl;
#endif
            emit playbackFrameChanged(f);
        }
    } else {
        if (m_playbackFrame != f) {
            m_playbackFrame = f;
            emit playbackFrameChanged(f);
        }
    }
}

void
ViewManager::viewZoomLevelChanged(ZoomLevel z, bool locked)
{
    View *v = dynamic_cast<View *>(sender());

    if (!v) {
        SVDEBUG << "ViewManager::viewZoomLevelChanged: WARNING: sender is not a view" << endl;
        return;
    }

//!!!    emit zoomLevelChanged();
    
    if (locked) {
        m_globalZoom = z;
    }

#ifdef DEBUG_VIEW_MANAGER 
    SVCERR << "ViewManager::viewZoomLevelChanged(" << v << ", " << z << ", " << locked << ")" << endl;
#endif

    emit viewZoomLevelChanged(v, z, locked);

    if (!dynamic_cast<Overview *>(v)) {
        if (z.zone == ZoomLevel::FramesPerPixel) {
            emit activity(tr("Zoom to %n sample(s) per pixel", "", z.level));
        } else {
            emit activity(tr("Zoom to %n pixels per sample", "", z.level));
        }
    }
}

void
ViewManager::setOverlayMode(OverlayMode mode)
{
    if (m_overlayMode != mode) {
        m_overlayMode = mode;
        emit overlayModeChanged();
        emit activity(tr("Change overlay level"));
    }

    QSettings settings;
    settings.beginGroup("MainWindow");
    settings.setValue("overlay-mode", int(m_overlayMode));
    settings.endGroup();
}

void
ViewManager::setZoomWheelsEnabled(bool enabled)
{
    if (m_zoomWheelsEnabled != enabled) {
        m_zoomWheelsEnabled = enabled;
        emit zoomWheelsEnabledChanged();
        if (enabled) emit activity("Show zoom wheels");
        else emit activity("Hide zoom wheels");
    }

    QSettings settings;
    settings.beginGroup("MainWindow");
    settings.setValue("zoom-wheels-enabled", m_zoomWheelsEnabled);
    settings.endGroup();
}

void
ViewManager::setOpportunisticEditingEnabled(bool enabled)
{
    if (m_opportunisticEditingEnabled != enabled) {
        m_opportunisticEditingEnabled = enabled;
        emit opportunisticEditingEnabledChanged();
    }
}

void
ViewManager::setShowCentreLine(bool show)
{
    if (m_showCentreLine != show) {
        m_showCentreLine = show;
        emit showCentreLineChanged();
        if (show) emit activity("Show centre line");
        else emit activity("Hide centre line");
    }

    QSettings settings;
    settings.beginGroup("MainWindow");
    settings.setValue("show-centre-line", int(m_showCentreLine));
    settings.endGroup();
}

void
ViewManager::setGlobalDarkBackground(bool dark)
{
    // also save the current palette, in case the user has changed it
    // since construction
    if (getGlobalDarkBackground()) {
        m_darkPalette = QApplication::palette();
#ifdef DEBUG_VIEW_MANAGER
        SVDEBUG << "ViewManager::setGlobalDarkBackground(" << dark << "): "
                << "copied dark palette from existing widgets (window colour = "
                << m_darkPalette.color(QPalette::Window).name() << ")" << endl;
#endif
    } else {
        m_lightPalette = QApplication::palette();
#ifdef DEBUG_VIEW_MANAGER
        SVDEBUG << "ViewManager::setGlobalDarkBackground(" << dark << "): "
                << "copied light palette from existing widgets (window colour = "
                << m_lightPalette.color(QPalette::Window).name() << ")" << endl;
#endif
    }

#ifdef Q_OS_MAC
    return;
#endif
    
    if (dark) {

#ifdef Q_OS_WIN32
        // The Windows Vista style doesn't use the palette for many of
        // its controls. They can be styled with stylesheets, but that
        // takes a lot of fiddly and fragile custom bits. Easier and
        // more reliable to switch to a non-Vista style which does use
        // the palette.

        QStyle *plainWindowsStyle = QStyleFactory::create("windows");
        if (!plainWindowsStyle) {
            SVCERR << "Failed to load plain Windows style" << endl;
        } else {
            qApp->setStyle(plainWindowsStyle);
        }
#endif

        QApplication::setPalette(m_darkPalette);

#ifdef DEBUG_VIEW_MANAGER
        SVDEBUG << "ViewManager::setGlobalDarkBackground(" << dark << "): "
                << "set dark palette to widgets (window colour = "
                << m_darkPalette.color(QPalette::Window).name() << ")" << endl;
#endif
        
    } else {

#ifdef Q_OS_WIN32
        // Switch back to Vista style
        
        QStyle *fancyWindowsStyle = QStyleFactory::create("windowsvista");
        if (!fancyWindowsStyle) {
            SVCERR << "Failed to load fancy Windows style" << endl;
        } else {
            qApp->setStyle(fancyWindowsStyle);
        }
#endif
        
        QApplication::setPalette(m_lightPalette);

#ifdef DEBUG_VIEW_MANAGER
        SVDEBUG << "ViewManager::setGlobalDarkBackground(" << dark << "): "
                << "set light palette to widgets (window colour = "
                << m_lightPalette.color(QPalette::Window).name() << ")" << endl;
#endif
    }
}

bool
ViewManager::getGlobalDarkBackground() const
{
    bool dark = false;
    QColor windowBg = QApplication::palette().color(QPalette::Window);
    auto red = windowBg.red(), green = windowBg.green(), blue = windowBg.blue();
#ifdef DEBUG_VIEW_MANAGER
//    SVDEBUG << "ViewManager::getGlobalDarkBackground: red = " << red
//            << ", green = " << green << ", blue = " << blue << ", total = "
//            << red + green + blue << endl;
#endif
    if (red + green + blue < 384) {
        dark = true;
    }
    return dark;
}

int
ViewManager::scalePixelSize(int pixels)
{
    static double ratio = 0.0;
    if (ratio == 0.0) {
        double baseEm;
#ifdef Q_OS_MAC
        baseEm = 17.0;
#else
        baseEm = 15.0;
#endif
        double em = QFontMetrics(QFont()).height();
        ratio = em / baseEm;

        SVDEBUG << "ViewManager::scalePixelSize: ratio is " << ratio
                << " (em = " << em << ")" << endl;
    }

    int scaled = int(pixels * ratio + 0.5);
//    SVDEBUG << "scaledSize: " << pixels << " -> " << scaled << " at ratio " << ratio << endl;
    if (pixels != 0 && scaled == 0) scaled = 1;
    return scaled;
}

} // end namespace sv