/
controlscreen.cpp
2252 lines (2127 loc) · 94.6 KB
/
controlscreen.cpp
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of BeamerPresenter.
* Copyright (C) 2019 stiglers-eponym
* BeamerPresenter 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 3 of the License, or
* (at your option) any later version.
* BeamerPresenter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with BeamerPresenter. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cmath>
#include "controlscreen.h"
#include "../names.h"
#ifdef DISABLE_TOOL_TIP
#else
static const QString preview0_tooltip = "Preview of the current presentation slide";
static const QString preview1_tooltip = "Preview of the next presentation slide";
static const QString preview2_tooltip = "Preview of the next presentation slide but one";
static const QString slider_tooltip = "Position of multimedia content on the presentation window";
#endif
// TODO: tidy up! reorganize signals, slots, events, ...
ControlScreen::ControlScreen(QString presentationPath, QString notesPath, PagePart const part, const qreal pagePartThreshold, QWidget* parent) :
QMainWindow(parent),
ui(new Ui::ControlScreen),
pagePart(part)
{
// Check if files are valid.
{
setAttribute(Qt::WA_AlwaysShowToolTips);
// Check whether presentation is empty.
if (presentationPath.isEmpty()) {
qCritical() << "No presentation file specified";
close();
deleteLater();
exit(1);
}
// Check whether presentation exists.
QFileInfo checkPresentation(presentationPath);
if (!checkPresentation.exists() || (!checkPresentation.isFile() && !checkPresentation.isSymLink()) ) {
qCritical() << "Not a file: " << presentationPath;
close();
deleteLater();
exit(1);
}
// Check whether notes are given in a separate file.
if (notesPath == presentationPath)
notesPath = "";
else if (!notesPath.isEmpty()) {
// Check whether the notes file exists.
QFileInfo checkNotes(notesPath);
if (!checkNotes.exists() || (!checkNotes.isFile() && !checkNotes.isSymLink()) ) {
qCritical() << "Ignoring invalid notes files: " << notesPath;
notesPath = "";
}
}
}
// Check whether pagePart is compatible with notesPath.
if (notesPath != "" && pagePart != FullPage && pagePartThreshold <= 0) {
qCritical() << "Provided additional notes file, but page-part is not full page. Ignoring option for page-part.";
pagePart = FullPage;
}
// Create the presentation pdf document.
presentation = new PdfDoc(presentationPath);
// Load the document and check whether it was loaded successfully.
if (!presentation->loadDocument()) {
qCritical() << "Could not open document: " << presentationPath;
close();
deleteLater();
exit(1);
}
// Save the total number of pages.
numberOfPages = presentation->getDoc()->numPages();
// Check aspect ratio of given presentation file and compare it to
// threshold if pagePart != FullPage
if (notesPath == "" && pagePart != FullPage && pagePartThreshold > 0.) {
QSizeF const size = presentation->getPageSize(0);
qreal const aspectRatio = size.width() / size.height();
if (aspectRatio < pagePartThreshold)
pagePart = FullPage;
}
// Some numbers for cache management.
// Maximum number of cached pages is by default the total number of pages.
maxCacheNumber = numberOfPages;
// last_delete points to the last page which has not been deleted yet.
// No pages have been deleted from cache yet.
last_delete = numberOfPages - 1;
// Set up presentation screen.
// The presentation screen is shown immediately.
presentationScreen = new PresentationScreen(presentation, pagePart);
// Set the window title.
presentationScreen->setWindowTitle("BeamerPresenter: " + presentationPath);
#ifdef Q_OS_UNIX
setWindowIcon(QIcon(ICON_PATH "beamerpresenter.svg"));
#endif
// Create the UI for the control screen.
ui->setupUi(this);
// Load the notes pdf document if a separate notes file is given.
if (!notesPath.isEmpty()) {
// Create the notes document.
notes = new PdfDoc(notesPath);
// Load the document and check whether it was loaded successfully.
if (!notes->loadDocument()) {
qCritical() << "File could not be opened as PDF: " << notesPath;
// Notes path is reset to "" and no notes file is loaded.
notesPath = "";
delete notes;
}
}
// Set the window title.
if (notesPath.isEmpty()) {
setWindowTitle("BeamerPresenter: " + presentationPath);
// The files are equal.
notes = presentation;
}
else
setWindowTitle("BeamerPresenter: " + notesPath);
// Set up the slide widgets.
if (notesPath.isEmpty() && pagePart == FullPage) {
// No notes are given.
// Set up the notes widget as a draw slide.
// The old notes_widget (type MediaSlide) is not required anymore. It will be replaced by a DrawSlide object.
delete ui->notes_widget;
// Create the draw slide.
ui->notes_widget = new DrawSlide(presentation, FullPage, this);
// ui->notes_widget can get focus.
ui->notes_widget->setFocusPolicy(Qt::ClickFocus);
// drawSlide equals notes_widget.
drawSlide = static_cast<DrawSlide*>(ui->notes_widget);
// Connect drawSlide to other widgets.
// Copy paths from draw slide to presentation slide and vice versa when drawing on one of the slides.
// Copy paths after moving the mouse while drawing. This assumes that only the last path is changed or a new path is created.
connect(drawSlide->getPathOverlay(), &PathOverlay::pathsChangedQuick, presentationScreen->slide->getPathOverlay(), &PathOverlay::setPathsQuick);
connect(presentationScreen->slide->getPathOverlay(), &PathOverlay::pathsChangedQuick, drawSlide->getPathOverlay(), &PathOverlay::setPathsQuick);
// Copy all paths. This completely updates all paths after using the erasor.
connect(drawSlide->getPathOverlay(), &PathOverlay::pathsChanged, presentationScreen->slide->getPathOverlay(), &PathOverlay::setPaths);
connect(presentationScreen->slide->getPathOverlay(), &PathOverlay::pathsChanged, drawSlide->getPathOverlay(), &PathOverlay::setPaths);
// Send pointer position (when using a pointer, torch or magnifier tool).
connect(drawSlide->getPathOverlay(), &PathOverlay::pointerPositionChanged, presentationScreen->slide->getPathOverlay(), &PathOverlay::setPointerPosition);
connect(presentationScreen->slide->getPathOverlay(), &PathOverlay::pointerPositionChanged, drawSlide->getPathOverlay(), &PathOverlay::setPointerPosition);
connect(drawSlide->getPathOverlay(), &PathOverlay::stylusPositionChanged, presentationScreen->slide->getPathOverlay(), &PathOverlay::setStylusPosition);
connect(presentationScreen->slide->getPathOverlay(), &PathOverlay::stylusPositionChanged, drawSlide->getPathOverlay(), &PathOverlay::setStylusPosition);
// Send relax signal when the mouse is released, which ends drawing a path.
connect(drawSlide->getPathOverlay(), &PathOverlay::sendRelaxPointer, presentationScreen->slide->getPathOverlay(), &PathOverlay::relaxPointer);
connect(drawSlide->getPathOverlay(), &PathOverlay::sendRelaxStylus, presentationScreen->slide->getPathOverlay(), &PathOverlay::relaxStylus);
connect(presentationScreen->slide->getPathOverlay(), &PathOverlay::sendRelaxPointer, drawSlide->getPathOverlay(), &PathOverlay::relaxPointer);
connect(presentationScreen->slide->getPathOverlay(), &PathOverlay::sendRelaxStylus, drawSlide->getPathOverlay(), &PathOverlay::relaxStylus);
// Request rendering an enlarged page as required for the magnifier.
connect(drawSlide->getPathOverlay(), &PathOverlay::sendUpdateEnlargedPage, presentationScreen->slide->getPathOverlay(), &PathOverlay::updateEnlargedPage);
connect(presentationScreen->slide->getPathOverlay(), &PathOverlay::sendUpdateEnlargedPage, drawSlide->getPathOverlay(), &PathOverlay::updateEnlargedPage);
// Paths are drawn on a transparent QPixmap for faster rendering.
// Signals used to request updates for this QPixmap:
connect(drawSlide->getPathOverlay(), &PathOverlay::sendUpdatePathCache, presentationScreen->slide->getPathOverlay(), &PathOverlay::updatePathCache);
connect(presentationScreen->slide->getPathOverlay(), &PathOverlay::sendUpdatePathCache, drawSlide->getPathOverlay(), &PathOverlay::updatePathCache);
// drawSlide should be muted, because it shows the same video content as the presentation slide.
drawSlide->setMuted(true);
#ifdef DISABLE_TOOL_TIP
#else
ui->current_slide->setToolTip(preview1_tooltip);
ui->next_slide->setToolTip(preview2_tooltip);
#endif
}
else {
ui->notes_widget->setDoc(notes, static_cast<PagePart>(-pagePart));
ui->notes_widget->overwriteCacheMap(new CacheMap(notes, static_cast<PagePart>(-pagePart), this));
#ifdef DISABLE_TOOL_TIP
#else
ui->current_slide->setToolTip(preview0_tooltip);
ui->next_slide->setToolTip(preview1_tooltip);
#endif
}
// Send pdf documents to slide widgets on control screen.
ui->current_slide->setDoc(presentation, pagePart);
ui->next_slide->setDoc(presentation, pagePart);
// Set common cache for preview slides.
previewCache = new CacheMap(presentation, pagePart, this);
ui->current_slide->overwriteCacheMap(previewCache);
ui->next_slide->overwriteCacheMap(previewCache);
// Connect cache maps.
connect(previewCache, &CacheMap::cacheSizeChanged, this, &ControlScreen::updateCacheSize);
connect(previewCache, &CacheMap::cacheThreadFinished, this, &ControlScreen::cacheThreadFinished);
connect(ui->notes_widget->getCacheMap(), &CacheMap::cacheSizeChanged, this, &ControlScreen::updateCacheSize);
connect(ui->notes_widget->getCacheMap(), &CacheMap::cacheThreadFinished, this, &ControlScreen::cacheThreadFinished);
connect(presentationScreen->slide->getCacheMap(), &CacheMap::cacheSizeChanged, this, &ControlScreen::updateCacheSize);
connect(presentationScreen->slide->getCacheMap(), &CacheMap::cacheThreadFinished, this, &ControlScreen::cacheThreadFinished);
// Create widget showing table of content (TocBox) on the control screen.
// tocBox is empty by default and will be updated when it is shown for the first time.
tocBox = new TocBox(this);
// tocBox will be shown on top of the notes widget and should therefore have the same geometry.
tocBox->setGeometry(ui->notes_widget->geometry());
// By default tocBox is hidden.
tocBox->hide();
// Set the pdf document in the TOC box.
tocBox->setPdf(presentation);
// Create widget showing thumbnail slides on the control screen.
// overviewBox is empty by default and will be updated when it is shown for the first time.
overviewBox = new OverviewBox(this);
// overviewBox will be shown on top of the notes widget and should therefore have the same geometry.
overviewBox->setGeometry(ui->notes_widget->geometry());
// By default overviewBox is hidden.
overviewBox->hide();
// Set up other widgets, which have been created by ui.
// Display number of pages.
ui->text_number_slides->setText(QString::number(numberOfPages));
// Set number of pages (upper bound) for editable text showing current slide number.
ui->text_current_slide->setNumberOfPages(numberOfPages);
// Focus on the notes slide widget.
ui->notes_widget->setFocus();
// Set up tool selector.
// Tool selector can send new draw tools to the presentation slide.
connect(ui->tool_selector, &ToolSelector::sendNewTool, this, &ControlScreen::distributeTools);
connect(ui->tool_selector, &ToolSelector::sendNewStylusTool, this, &ControlScreen::distributeStylusTools);
// Tool selector can send KeyActions to control screen.
connect(ui->tool_selector, &ToolSelector::sendAction, this, &ControlScreen::handleKeyAction);
// Page requests from the labels:
// These are emitted if links are clicked.
// These events are send to control screen and presentation screen.
connect(ui->notes_widget, &PreviewSlide::sendNewPageNumber, this, &ControlScreen::receiveNewPageNumber);
connect(ui->current_slide, &PreviewSlide::sendNewPageNumber, this, &ControlScreen::receiveNewPageNumber);
connect(ui->next_slide, &PreviewSlide::sendNewPageNumber, this, &ControlScreen::receiveNewPageNumber);
connect(ui->notes_widget, &PreviewSlide::sendNewPageNumber, presentationScreen, &PresentationScreen::receiveNewPage);
connect(ui->current_slide, &PreviewSlide::sendNewPageNumber, presentationScreen, &PresentationScreen::receiveNewPage);
connect(ui->next_slide, &PreviewSlide::sendNewPageNumber, presentationScreen, &PresentationScreen::receiveNewPage);
// For action links of type "go to page" (interpreted as change page number)
connect(ui->notes_widget, &PreviewSlide::focusPageNumberEdit, this, &ControlScreen::focusPageNumberEdit);
connect(ui->current_slide, &PreviewSlide::focusPageNumberEdit, this, &ControlScreen::focusPageNumberEdit);
connect(ui->next_slide, &PreviewSlide::focusPageNumberEdit, this, &ControlScreen::focusPageNumberEdit);
connect(presentationScreen->slide, &PreviewSlide::focusPageNumberEdit, this, &ControlScreen::focusPageNumberEdit);
// For action links of type "presentation" (interpreted as full screen)
// TODO: correct handling full screen.
//connect(ui->notes_widget, &PreviewSlide::sendShowFullscreen, this, &ControlScreen::showFullScreen);
//connect(ui->current_slide, &PreviewSlide::sendShowFullscreen, this, &ControlScreen::showFullScreen);
//connect(ui->next_slide, &PreviewSlide::sendShowFullscreen, this, &ControlScreen::showFullScreen);
//connect(presentationScreen->slide, &PreviewSlide::sendShowFullscreen, this, &ControlScreen::showFullScreen);
connect(ui->notes_widget, &PreviewSlide::sendShowFullscreen, presentationScreen, &PresentationScreen::showFullScreen);
connect(ui->current_slide, &PreviewSlide::sendShowFullscreen, presentationScreen, &PresentationScreen::showFullScreen);
connect(ui->next_slide, &PreviewSlide::sendShowFullscreen, presentationScreen, &PresentationScreen::showFullScreen);
connect(presentationScreen->slide, &PreviewSlide::sendShowFullscreen, presentationScreen, &PresentationScreen::showFullScreen);
// Navigation signals emitted by presentation screen.
// Show the same page on control screen as on presentation screen.
connect(presentationScreen->slide, &PresentationSlide::sendAdaptPage, this, &ControlScreen::adaptPage);
// Send a new page number from presentation slide (after following a link) to control screen.
connect(presentationScreen->slide, &PresentationSlide::sendNewPageNumber, this, &ControlScreen::receiveNewPageNumber);
// Presentation screen sends new page numbers after scrolling.
connect(presentationScreen, &PresentationScreen::sendNewPageNumber, this, &ControlScreen::receiveNewPageNumber);
// Other signals emitted by presentation screen.
// All key events are handled by control screen.
connect(presentationScreen, &PresentationScreen::sendKeyEvent, this, &ControlScreen::keyPressEvent);
// Close window (emitted after a link of action type "quit" or "close" was clicked).
connect(presentationScreen, &PresentationScreen::sendCloseSignal, this, &ControlScreen::close);
// Send request for multimedia sliders from presentation screen to control screen.
// Then send the multimedia sliders to draw slide, where they are connected to synchronize multimedia content.
connect(presentationScreen->slide, &MediaSlide::requestMultimediaSliders, this, &ControlScreen::addMultimediaSliders);
// Signals sent back to presentation screen.
connect(this, &ControlScreen::sendNewPageNumber, presentationScreen, &PresentationScreen::renderPage);
connect(presentationScreen->slide, &PresentationSlide::requestUpdateNotes, this, &ControlScreen::renderPage);
// Close presentation screen when closing control screen.
connect(this, &ControlScreen::sendCloseSignal, presentationScreen, &PresentationScreen::close);
// Close window (emitted after a link of action type "quit" or "close" was clicked).
connect(ui->notes_widget, &PreviewSlide::sendCloseSignal, presentationScreen, &PresentationScreen::close);
connect(ui->notes_widget, &PreviewSlide::sendCloseSignal, this, &ControlScreen::close);
connect(presentationScreen->slide, &PreviewSlide::sendCloseSignal, presentationScreen, &PresentationScreen::close);
connect(presentationScreen->slide, &PreviewSlide::sendCloseSignal, this, &ControlScreen::close);
// Connect timer label to presentation PDF and editable total duration.
// The widget ui->edit_timer is completely controled by ui->label_timer.
ui->label_timer->init(ui->edit_timer, presentation);
// Notify timer about new page numbers.
connect(presentationScreen, &PresentationScreen::pageChanged, ui->label_timer, &Timer::setPage);
// Send alert (time passed is larger than expected total duration).
connect(ui->label_timer, &Timer::sendAlert, this, &ControlScreen::receiveTimerAlert);
// Stop alert.
connect(ui->label_timer, &Timer::sendNoAlert, this, &ControlScreen::resetTimerAlert);
// Exit editing timer with escape key.
connect(ui->label_timer, &Timer::sendEscape, this, &ControlScreen::resetFocus);
// Set up clock.
// Clock should show current time.
ui->label_clock->setText(QTime::currentTime().toString("hh:mm:ss"));
// Clock is updated by the same timer as ui->label_timer.
connect(ui->label_timer->getTimer(), &QTimer::timeout, ui->label_clock, [&](){ui->label_clock->setText(QTime::currentTime().toString("hh:mm:ss"));});
// Signals emitted by the page number editor.
// Pressing return in page number editor changes the page in the presentation.
connect(ui->text_current_slide, &PageNumberEdit::sendPageNumberReturn, presentationScreen, &PresentationScreen::receiveNewPage);
// Editing the number in page number editor without pressing return changes the notes pages on the control screen.
connect(ui->text_current_slide, &PageNumberEdit::sendPageNumberEdit, this, &ControlScreen::receiveNewPageNumber);
// Using arrow keys in page number editor shifts the notes pages on the control screen.
// Currently the key bindings in the page number editor cannot be configured.
connect(ui->text_current_slide, &PageNumberEdit::sendPageShiftEdit, this, [&](int const shift){renderPage(currentPageNumber+shift);});
// Using arrow keys "up" and "down" navigate to the previous or next page skipping overlays.
connect(ui->text_current_slide, &PageNumberEdit::sendNextSlideStart, this, &ControlScreen::receiveNextSlideStart);
connect(ui->text_current_slide, &PageNumberEdit::sendPreviousSlideEnd, this, &ControlScreen::receivePreviousSlideEnd);
// The escape key resets the focus to the notes slide.
connect(ui->text_current_slide, &PageNumberEdit::sendEscape, this, &ControlScreen::resetFocus);
// Set up cache handling.
// Set slide widgets for cache thread. The widgets are const for the cache thread.
// The cache timer just makes sure that slides are rendered to cache after the main thread finished all other tasks.
connect(cacheTimer, &QTimer::timeout, this, &ControlScreen::updateCacheStep);
// Send rendered pages from cache thread to control screen.
// Clear presentation cache when presentation screen is resized.
connect(presentationScreen, &PresentationScreen::presentationResizeEvent, this, &ControlScreen::presentationResized);
// Signals emitted by the TOC box (table of contents).
// Send a destination in pdf (e.g. a section).
connect(tocBox, &TocBox::sendNewPage, presentationScreen, [&](int const pageNumber){presentationScreen->renderPage(pageNumber, false);});
connect(tocBox, &TocBox::sendNewPage, this, &ControlScreen::receiveNewPageNumber);
// Signals emitted by the overview box
// Send new page number to presentation screen and control screen. TODO: check this.
connect(overviewBox, &OverviewBox::sendPageNumber, presentationScreen, [&](int const pageNumber){presentationScreen->renderPage(pageNumber, false);});
connect(overviewBox, &OverviewBox::sendPageNumber, this, &ControlScreen::receiveNewPageNumber);
// Exit overview box.
connect(overviewBox, &OverviewBox::sendReturn, this, &ControlScreen::showNotes);
#ifdef DISABLE_TOOL_TIP
#else
ui->text_number_slides->setToolTip("Total number of pages");
#endif
}
ControlScreen::~ControlScreen()
{
// Hide widgets which are shown above the notes widget.
showNotes();
// Delete widgets which would be shown above the notes widget.
delete tocBox;
delete overviewBox;
// Stop cache processes.
cacheTimer->disconnect();
interruptCacheProcesses(10000);
delete cacheTimer;
// Disconnect draw slide.
if (drawSlide != nullptr && drawSlide != ui->notes_widget)
drawSlide->disconnect();
// Disconnect all (remaining) widgets from all signals.
ui->notes_widget->disconnect();
ui->current_slide->disconnect();
ui->next_slide->disconnect();
ui->label_timer->disconnect();
ui->text_current_slide->disconnect();
presentationScreen->slide->disconnect();
presentationScreen->disconnect();
disconnect();
// Clear cache maps.
ui->current_slide->overwriteCacheMap(nullptr);
ui->next_slide->overwriteCacheMap(nullptr);
if (previewCache != nullptr)
previewCache->clearCache();
if (previewCacheX != nullptr)
previewCacheX->clearCache();
ui->notes_widget->getCacheMap()->clearCache();
if (drawSlideCache != nullptr)
drawSlideCache->clearCache();
presentationScreen->slide->getCacheMap()->clearCache();
// Delete notes cache.
delete ui->notes_widget->getCacheMap();
ui->notes_widget->overwriteCacheMap(nullptr);
// Delete notes pdf.
if (notes != presentation)
delete notes;
// Delete keymap.
delete keymap;
// Delete preview cache.
delete previewCacheX;
delete previewCache;
// Delete draw slide.
if (drawSlide != ui->notes_widget)
delete drawSlide;
delete drawSlideCache;
// Delete presentation screen.
delete presentationScreen;
// Delete presentation pdf.
delete presentation;
// Delete the user interface.
delete ui;
}
void ControlScreen::recalcLayout(const int pageNumber)
{
#ifdef DEBUG_RENDERING
qDebug() << "recalc layout" << size() << oldSize << pageNumber;
#endif
if (size() != oldSize) {
// Delete preview cache
previewCache->clearCache();
if (previewCacheX != nullptr)
previewCacheX->clearCache();
if (drawSlideCache != nullptr)
drawSlideCache->clearCache();
}
// Calculate the size of the side bar.
/// Aspect ratio (height/width) of the window.
qreal screenRatio = qreal(height()) / width();
/// Size of notes page (or presentation slide if drawSlide is shown).
QSizeF notesSize;
if (drawSlide == nullptr)
notesSize = notes->getPageSize(pageNumber);
else
notesSize = presentation->getPageSize(pageNumber);
/// Aspect ratio (height/width) of the slide shown on the notes widget.
qreal notesSizeRatio = notesSize.height() / notesSize.width();
// Correct the aspect ratio if the pdf includes slides and notes.
if (pagePart != FullPage)
notesSizeRatio *= 2;
/// Relative width of the notes slide on the control screen.
/// If the page size is scaled such that screen height == notes height, then relativeNoteWidth = screen width / notes width
qreal relativeNotesWidth = screenRatio / notesSizeRatio;
// Make sure that width of notes does not become too large.
if (relativeNotesWidth > maxNotesWidth)
relativeNotesWidth = maxNotesWidth;
// width of the sidebar:
int sideWidth = int((1-relativeNotesWidth)*width());
// Adapt widths of different widgets to the new sidebar width.
// Adapt top level grid layout (notes widget and sidebar).
ui->gridLayout->setColumnStretch(0, width()-sideWidth);
ui->gridLayout->setColumnStretch(1, sideWidth);
// Adapt notes widget geometry.
ui->notes_widget->setGeometry(0, 0, width()-sideWidth, height());
// Adapt preview slides sizes.
ui->current_slide->setMaximumSize(sideWidth, height()/2);
ui->next_slide->setMaximumSize(sideWidth, height()/2);
// Width of tool selector.
ui->tool_selector->setMaximumWidth(sideWidth);
// Width of timer labels. The colored box is expanding from its minimum width sideWidth/2.
ui->label_timer->setMinimumWidth(sideWidth/2);
// Width of total time label. The editable box is expanding to its maximum width sideWidth/3.
ui->edit_timer->setMaximumWidth(sideWidth/3);
// Geometry of overview widget: same as of notes widgets.
overviewBox->setGeometry(0, 0, width()-sideWidth, height());
// Geometry of TOC widget: same as of notes widgets, but with extra margins in horizontal direction.
tocBox->setGeometry(int(0.1*(width()-sideWidth)), 0, int(0.8*(width()-sideWidth)), height());
// Adapt size of draw slide if necessary.
if (drawSlide != nullptr) {
/// Scale of draw slide relative to presentation slide.
qreal scale = drawSlide->getResolution() / presentationScreen->slide->getResolution();
if (scale < 1e-5)
scale = 1.;
// Set scaled tool sizes for draw slide.
drawSlide->getPathOverlay()->setTool(presentationScreen->slide->getPathOverlay()->getTool(), presentationScreen->slide->getResolution());
drawSlide->getPathOverlay()->setEraserSize(scale*presentationScreen->slide->getPathOverlay()->getEraserSize());
if (drawSlide != ui->notes_widget) {
// Adapt geometry of draw slide: It should have the same geometry as the notes slide.
drawSlide->setGeometry(ui->notes_widget->rect());
}
}
// Adjust font sizes.
// The font size is adapted to sideWidth.
// But if height() is small compared to sideWidth, one should better adapt it to height().
// This is solved by adapting sideWidth to min(sideWidth, 0.4*height()).
// From here on sideWidth is not anymore the real width of the sidebar!
if (5*sideWidth > 2*height())
sideWidth = 2*height() / 5;
// Get the current font. Up to the font size it is the same for all labels containing text.
QFont font = ui->label_timer->font();
// Smaller font: used for editable total time and "/" between timer and editable total time.
font.setPixelSize(sideWidth/10+5);
ui->text_slash_2->setFont(font);
ui->edit_timer->setFont(font);
// Larger font: used for all other labels.
font.setPixelSize(sideWidth/8+7);
ui->label_timer->setFont(font);
ui->label_clock->setFont(font);
ui->text_slash->setFont(font);
ui->text_current_slide->setFont(font);
ui->text_number_slides->setFont(font);
// Adjust the layout containing preview slides and tool selector.
// Without this the size of the preview slides would not be updated directly.
ui->gridLayout->activate();
ui->overviewLayout->activate();
// Make sure that the sizes of both preview slides are exactly the same.
if (ui->current_slide->size() != ui->next_slide->size()) {
QSize minsize = ui->current_slide->size().boundedTo(ui->next_slide->size());
ui->current_slide->setMaximumSize(minsize);
ui->next_slide->setMaximumSize(minsize);
}
// Notify layout system that geometry has changed.
updateGeometry();
}
void ControlScreen::focusPageNumberEdit()
{
this->activateWindow();
ui->text_current_slide->setFocus();
}
void ControlScreen::addMultimediaSliders(int const n)
{
// Add n sliders at the bottom of the sidebar
QList<QSlider*> sliderList = QList<QSlider*>();
for (int i=0; i<n; i++) {
QSlider* slider = new QSlider(Qt::Horizontal, this);
ui->overviewLayout->addWidget(slider);
sliderList.append(slider);
#ifdef DISABLE_TOOL_TIP
#else
slider->setToolTip(slider_tooltip);
#endif
}
// Send the sliders to the presentation label, where they will be connected to multimedia objects.
// The presentation label takes ownership of the sliders and will delete them when going to the next slide.
presentationScreen->slide->setMultimediaSliders(sliderList);
}
void ControlScreen::resetFocus()
{
// Make sure that the control screen shows the same page as the presentation screen and set focus to the notes label.
if (currentPageNumber != presentationScreen->getPageNumber())
renderPage(presentationScreen->getPageNumber());
ui->notes_widget->setFocus();
}
void ControlScreen::receiveTimerAlert()
{
QPalette palette = QPalette();
palette.setColor(QPalette::Window, Qt::red);
palette.setColor(QPalette::Base, Qt::red);
ui->edit_timer->setPalette(palette);
}
void ControlScreen::resetTimerAlert()
{
QPalette palette = QPalette();
palette.setColor(QPalette::Window, Qt::darkGray);
palette.setColor(QPalette::Base, Qt::white);
ui->edit_timer->setPalette(palette);
}
void ControlScreen::renderPage(int const pageNumber, bool const full)
{
// Update all slide widgets on the control screen to show the given page.
#ifdef DEBUG_RENDERING
qDebug() << "Render page" << pageNumber << full;
#endif
// Update currentPageNumber.
// Negative page numbers are interpreted as signal for going to the last page.
if (pageNumber < 0 || pageNumber >= numberOfPages)
currentPageNumber = numberOfPages - 1;
else
currentPageNumber = pageNumber;
// Quick version if this window is not visible. This does not update the slide widgets.
if (!isVisible()) {
if (full) {
// Some extras which may take some time
if (presentationScreen->slide->getPathOverlay()->getTool().tool == Magnifier)
presentationScreen->slide->getPathOverlay()->updateEnlargedPage();
presentationScreen->slide->updateCacheVideos(presentationScreen->pageIndex+1);
}
return;
}
// Recalculate layout if the window size has changed.
if (full && size() != oldSize) {
recalcLayout(currentPageNumber);
oldSize = size();
}
if (drawSlide == nullptr) {
// Update notes.
ui->notes_widget->renderPage(currentPageNumber, false);
// Update current and next slide previews.
// If we have not reached the last page (there exists a next page):
ui->current_slide->renderPage(currentPageNumber);
ui->next_slide->renderPage(currentPageNumber+1);
}
else {
// TODO: improve this part.
// It is possible that presentationScreen->slide contains drawings which have not been copied to drawSlide yet.
QString label = presentation->getLabel(currentPageNumber);
if (drawSlide->getPage() != nullptr && !drawSlide->getPathOverlay()->getPaths().contains(label)) {
qint16 const sx=presentationScreen->slide->getXshift(), sy=presentationScreen->slide->getYshift();
qreal res = presentationScreen->slide->getResolution();
drawSlide->getPathOverlay()->setPaths(label, presentationScreen->slide->getPathOverlay()->getPaths()[label], sx, sy, res);
}
// Update current slide
drawSlide->renderPage(currentPageNumber, false);
// Synchronize videos
connectVideos(drawSlide, presentationScreen->slide);
// Update next slide previews
// If we have not reached the last page (there exists a next page):
ui->current_slide->renderPage(currentPageNumber+1);
ui->next_slide->renderPage(currentPageNumber+2);
}
// Update the page number
ui->text_current_slide->setText(QString::number(currentPageNumber+1));
if (full) {
// Some extras which may take some time
if (presentationScreen->slide->getPathOverlay()->getTool().tool == Magnifier) {
ui->current_slide->repaint();
ui->next_slide->repaint();
presentationScreen->slide->getPathOverlay()->updateEnlargedPage();
if (drawSlide != nullptr)
drawSlide->getPathOverlay()->updateEnlargedPage();
}
presentationScreen->slide->updateCacheVideos(presentationScreen->pageIndex+1);
}
}
void ControlScreen::updateCache()
{
// (Re)start updating cache.
// This (re)initializes the variables for cache management and starts the cacheTimer, which manages cache updates in a separate thread.
if (maxCacheSize == 0 || maxCacheNumber == 0) {
// The cache managment cannot be turned of.
// If cache is disabled by setting maxCacheSize=0 or maxCacheNumber=0,
// cache needs to be cleared manually.
presentationScreen->slide->getCacheMap()->clearCache();
ui->notes_widget->getCacheMap()->clearCache();
previewCache->clearCache();
if (previewCacheX != nullptr)
previewCacheX->clearCache();
if (drawSlideCache != nullptr)
drawSlideCache->clearCache();
return;
}
// Stop running cache updates
cacheTimer->stop();
// Number of currently cached slides
int const cacheNumber = presentationScreen->slide->getCacheMap()->length();
if (
cacheNumber == numberOfPages
&& ui->notes_widget->getCacheMap()->length() == numberOfPages
&& previewCache->length() == numberOfPages
&& (drawSlideCache == nullptr || drawSlideCache->length() == numberOfPages)
&& (previewCacheX == nullptr || previewCacheX->length() == numberOfPages)
) {
// All slides are cached
return;
}
// Size of currently cached slides in memory (set to -infinity if it should be ignored)
if (maxCacheSize > 0) {
cacheSize = presentationScreen->slide->getCacheMap()->getSizeBytes()
+ ui->notes_widget->getCacheMap()->getSizeBytes()
+ previewCache->getSizeBytes();
if (previewCacheX != nullptr)
cacheSize += previewCacheX->getSizeBytes();
if (drawSlideCache != nullptr)
cacheSize += drawSlideCache->getSizeBytes();
}
else
// This is approximately -infinity and means that the cache size is unlimited:
cacheSize = -8589934591L; // -8GiB
// There should be a simply connected region of cached pages between first_cached and last_cached.
if (first_cached > currentPageNumber || last_cached < currentPageNumber) {
// We are outside the simply connected cache region.
// Reset cache numbers and start a new simply connected region, which is initially empty.
first_cached = currentPageNumber;
last_cached = currentPageNumber-1;
first_delete = 0;
last_delete = numberOfPages-1;
#ifdef DEBUG_CACHE
qDebug() << "Reset cache region" << first_delete << first_cached << currentPageNumber << last_cached << last_delete;
#endif
}
else {
// TODO: check whether this is necessary.
// We are in the simply connected cache region.
// Make sure that deleting cached pages starts outside this region.
// Cached pages will only be deleted if the memory or the number of cached pages is limited.
last_delete = last_delete > currentPageNumber + cacheNumber ? last_delete : currentPageNumber + cacheNumber;
last_delete = last_delete >= numberOfPages ? numberOfPages-1 : last_delete;
first_delete = first_delete > currentPageNumber - cacheNumber/2 ? currentPageNumber - cacheNumber/2 : first_delete;
first_delete = first_delete < 0 ? 0 : first_delete;
}
if (last_cached < numberOfPages-1 || first_cached > 0)
// The simply connected region does not include all pages.
// Start the update steps by starting the cacheTimer.
// cacheTimer will call updateCacheStep().
cacheTimer->start();
}
void ControlScreen::updateCacheStep()
{
/*
* Select a page for rendering to cache and tell the CacheThreads of CacheMaps to render that page.
* Delete cached pages if necessary due to limited memory or a limited number of cached slides.
* This function will notice when no more pages need to be rendered to cache and stop the cacheTimer.
*
* Outline of the cache management:
*
* 0. updateCache initializes some variables and starts cacheTimer.
* 1. cacheTimer calls updateCacheStep in a loop whenever the main thread is not busy.
* 2. updateCacheStep deletes pages (using freeCachePage) as long as the cache uses
* too much memory (or as long as too many slides are cached).
* 3. updateCacheStep checks whether a new pages should be rendered to cache.
* TODO: This process of checking and finding pages should be more understandable.
* - If no more cached pages are needed, it stops cacheTimer.
* - If it finds a page, which should be rendered to cache:
* a. it stops cacheTimer
* b. it hands the page to ControlScreen::cachePage.
* 4. ControlScreen::cachePage calls CacheMap::updateCache for all slide widgets.
* This starts CacheThreads which render the different pages in parallel in own threads.
* For each new thread the counter ControlScreen::cacheThreadsRunning is incremented.
* 5. When the rendering is done, CacheMap gets the results from CacheThread and
* ControlScreen::cacheThreadFinished is called.
* 6. cacheThreadFinished decrements cacheThreadsRunning.
* If cacheThreadsRunning==0, starts cacheTimer again.
*/
#ifdef DEBUG_CACHE
qDebug() << "Update cache step" << cacheThreadsRunning << cacheSize << maxCacheSize << maxCacheNumber;
#endif
// TODO: improve this, make it more deterministic, avoid caching pages which will directly be freed again
if (
presentationScreen->slide->getCacheMap()->length() == numberOfPages
&& ui->notes_widget->getCacheMap()->length() == numberOfPages
&& previewCache->length() == numberOfPages
&& (drawSlideCache == nullptr || drawSlideCache->length() == numberOfPages)
&& (previewCacheX == nullptr || previewCacheX->length() == numberOfPages)
) {
// All slides are cached
qInfo() << "All slides rendered to cache. Cache size:" << cacheSize << "bytes.";
cacheTimer->stop();
return;
}
if (
last_cached > last_delete
|| first_cached < first_delete
|| first_cached > currentPageNumber
|| last_cached < currentPageNumber-1
) {
cacheTimer->stop();
#ifdef DEBUG_CACHE
qDebug() << "Stopped cache timer" << first_delete << first_cached << currentPageNumber << last_cached << last_delete;
#endif
return;
}
// Free space if necessary
while (cacheSize > maxCacheSize || (maxCacheNumber < numberOfPages && presentationScreen->slide->getCacheMap()->length() > maxCacheNumber) ) {
// Start deleting later slides if less than 1/4 of the caches slides are previous slides
if (last_delete > 4*currentPageNumber - 3*first_delete) {
if (freeCachePage(last_delete))
break;
last_delete--;
last_cached = last_delete > last_cached ? last_cached : last_delete;
}
else {
if (freeCachePage(first_delete))
break;
first_delete++;
first_cached = first_cached > first_delete ? first_cached : first_delete;
}
if (last_cached > last_delete || first_cached < first_delete) {
cacheTimer->stop();
#ifdef DEBUG_CACHE
qDebug() << "Stopped cache timer: need to reset cache region." << first_delete << first_cached << currentPageNumber << last_cached << last_delete;
#endif
return;
}
}
if (last_cached+1 == numberOfPages) {
if (
first_cached > first_delete
&& 2*maxCacheSize > 3*cacheSize
&& (maxCacheNumber == numberOfPages || 2*maxCacheNumber > 3*presentationScreen->slide->getCacheMap()->length())
) {
// cache first_cached-1
cachePage(--first_cached);
return;
}
else {
cacheTimer->stop();
#ifdef DEBUG_CACHE
qDebug() << "Stopped cache timer" << first_delete << first_cached << currentPageNumber << last_cached << last_delete;
#endif
return;
}
}
// Don't continue if it is likely that the next cached page would directly be deleted.
else if (
// More than 2/3 of available cache space is occupied.
2*maxCacheSize < 3*cacheSize
// Enough slides (compared to cache size) after current slide are contained in cache.
&& (last_cached == numberOfPages || 3*(last_cached - currentPageNumber)*cacheSize > 2*presentationScreen->slide->getCacheMap()->length()*maxCacheSize)
// The remaining cache space is smaller than twice the average space needed per presentation slide.
&& (maxCacheSize - cacheSize)*presentationScreen->slide->getCacheMap()->length() < 2*cacheSize
) {
cacheTimer->stop();
#ifdef DEBUG_CACHE
qDebug() << "Stopped cache timer" << first_delete << first_cached << currentPageNumber << last_cached << last_delete;
#endif
return;
}
else {
// Cache the page last_cached+1
cachePage(++last_cached);
}
}
bool ControlScreen::freeCachePage(const int page)
{
if (drawSlideCache != nullptr) {
cacheSize -= drawSlideCache->clearPage(page);
if (cacheSize <= maxCacheSize && (maxCacheNumber >= numberOfPages || presentationScreen->slide->getCacheMap()->length() <= maxCacheNumber))
return true;
}
cacheSize -= ui->notes_widget->getCacheMap()->clearPage(page);
if (cacheSize <= maxCacheSize && (maxCacheNumber >= numberOfPages || presentationScreen->slide->getCacheMap()->length() <= maxCacheNumber))
return true;
if (previewCacheX != nullptr)
cacheSize -= previewCacheX->clearPage(page);
cacheSize -= previewCache->clearPage(page);
if (cacheSize <= maxCacheSize && (maxCacheNumber >= numberOfPages || presentationScreen->slide->getCacheMap()->length() <= maxCacheNumber))
return true;
cacheSize -= presentationScreen->slide->getCacheMap()->clearPage(page);
#ifdef DEBUG_CACHE
qDebug() << "Freed page" << page << ". Cache size" << cacheSize << "B";
#endif
return false;
}
void ControlScreen::cachePage(const int page)
{
#ifdef DEBUG_CACHE
qDebug() << "Cache page" << page << cacheThreadsRunning << cacheSize;
#endif
cacheTimer->stop();
cacheThreadsRunning = 0;
if (presentationScreen->slide->getCacheMap()->updateCache(page))
cacheThreadsRunning++;
if(ui->notes_widget->getCacheMap()->updateCache(page))
cacheThreadsRunning++;
if (previewCache->updateCache(page))
cacheThreadsRunning++;
if (drawSlideCache != nullptr && drawSlideCache->updateCache(page))
cacheThreadsRunning++;
if (previewCacheX != nullptr && previewCacheX->updateCache(page))
cacheThreadsRunning++;
if (cacheThreadsRunning == 0)
cacheTimer->start();
}
void ControlScreen::setCacheNumber(int const number)
{
if (number < 0)
maxCacheNumber = numberOfPages;
else if (number == 0) {
interruptCacheProcesses(0);
maxCacheNumber = 0;
}
else
maxCacheNumber = number;
}
void ControlScreen::cacheThreadFinished()
{
if (--cacheThreadsRunning == 0)
cacheTimer->start();
}
void ControlScreen::setCacheSize(qint64 const size)
{
if (cacheSize == 0)
interruptCacheProcesses(0);
maxCacheSize = size;
}
void ControlScreen::setTocLevel(quint8 const level)
{
if (level<1) {
qWarning() << "toc-depth set to minimum value 1";
tocBox->setUnfoldLevel(1);
}
else if (level>4) {
qWarning() << "toc-depth set to maximum value 4";
tocBox->setUnfoldLevel(4);
}
else
tocBox->setUnfoldLevel(level);
}
void ControlScreen::receiveNewPageNumber(int const pageNumber)
{
renderPage(pageNumber);
updateCache();
}
void ControlScreen::receiveDest(QString const& dest)
{
showNotes();
int const pageNumber = presentation->destToSlide(dest);
if (pageNumber>=0 && pageNumber<numberOfPages) {
ui->label_timer->continueTimer();
emit sendNewPageNumber(pageNumber, true);
renderPage(pageNumber);
updateCache();
}
}
void ControlScreen::receivePreviousSlideEnd()
{
// Go to the end of the previous real slide (not: previous overlay) on the control screen without changing the presentation screen.
if (currentPageNumber >= 0) {
renderPage(notes->getPreviousSlideEnd(currentPageNumber));
updateCache();
}
}
void ControlScreen::receiveNextSlideStart()
{
// Go to the beginning of the next real slide (not: next overlay) on the control screen without changing the presentation screen.
if (currentPageNumber < numberOfPages - 1) {
renderPage(notes->getNextSlideIndex(currentPageNumber));
updateCache();
}
}
void ControlScreen::adaptPage()
{
// Synchronize the presentation page to the notes page.
// This function is called after the page of the presentation is changed.
ui->label_timer->continueTimer();
// Go to page shifted relative to the page shown on the presentation screen page.
if (presentationScreen->slide->getDuration() < 0 || presentationScreen->slide->getDuration() > 0.5) {
renderPage(presentationScreen->getPageNumber());
updateCache();
}
}
void ControlScreen::keyPressEvent(QKeyEvent* event)
{
// Key codes are given as key + modifiers.
quint32 const key = quint32(event->key()) + quint32(event->modifiers());
if (tools.contains(key)) {
presentationScreen->slide->getPathOverlay()->setTool(tools[key]);
if (drawSlide != nullptr)
drawSlide->getPathOverlay()->setTool(tools[key], presentationScreen->slide->getResolution());
#ifdef DEBUG_TOOL_ACTIONS
qDebug() << "set tool" << tools[key].tool << tools[key].color << tools[key].size << tools[key].extras.magnification;
#endif
}
QMap<quint32, QList<KeyAction>>::iterator map_it = keymap->find(key);
if (map_it == keymap->end())
return;
for (QList<KeyAction>::const_iterator action_it=map_it->cbegin(); action_it!=map_it->cend(); action_it++)
if (handleKeyAction(*action_it))
break;
event->accept();
}
bool ControlScreen::handleKeyAction(KeyAction const action)