-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPresenter.cpp
More file actions
409 lines (406 loc) · 13.1 KB
/
Presenter.cpp
File metadata and controls
409 lines (406 loc) · 13.1 KB
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
#include "Presenter.hpp"
#include "Renderer.hpp"
#include "View.hpp"
#include <QPainter>
#include <iostream>
#include <iomanip>
#include <cstdio>
//----------------------------------------------------------------------------
using namespace std;
//----------------------------------------------------------------------------
Presenter::Presenter(Renderer& renderer,unsigned pageCount)
: renderer(renderer),lineWidth(3),lineColor(Qt::black),mode(Normal),page(0),showTimer(false)
// Constructor
{
// Compute the appropriate thumbnail sizes
thumbX=1;
while ((thumbX*thumbX)<pageCount)
thumbX++;
thumbY=thumbX;
thumbSize=QSize(screens.common().width()/thumbX-10,screens.common().height()/thumbY-10);
thumbSpacing=QSize(screens.common().width()/thumbX,screens.common().height()/thumbY);
connect(&renderer, SIGNAL(pageRendered(unsigned)), this, SLOT(pageChanged(unsigned)));
connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
}
//----------------------------------------------------------------------------
static void printMinutes(unsigned seconds)
// Print seconds formatted as minutes:seconds
{
cout << setw(5) << (seconds/60) << ":" << setfill('0') << setw(2) << (seconds%60) << setfill(' ');
}
//----------------------------------------------------------------------------
Presenter::~Presenter()
// Destructor
{
// Show timing at the end
if (!slidesLog.empty()) {
slidesLog.push_back(pair<unsigned,unsigned>(~0u,time(0)));
cout << setw(8) << "page" << " " << setw(8) << "duration" << " " << setw(8) << "enter" << " " << setw(8) << "leave" << endl;
for (unsigned index=0;index<slidesLog.size()-1;index++) {
unsigned duration=slidesLog[index+1].second-slidesLog[index].second;
unsigned enter=slidesLog[index].second-slidesLog[0].second;
unsigned leave=slidesLog[index+1].second-slidesLog[0].second;
cout << setw(8) << (slidesLog[index].first+1) << " ";
printMinutes(duration);
cout << " ";
printMinutes(enter);
cout << " ";
printMinutes(leave);
cout << endl;
}
}
}
//----------------------------------------------------------------------------
void Presenter::setProfile(const vector<unsigned>& profile)
// Set a presentation profile
{
this->profile=profile;
}
//----------------------------------------------------------------------------
void Presenter::createViews()
// Create a full screen view on each screen
{
for (unsigned index=0;index<screens.screenCount();index++) {
View* v=new View(*this,screens.screen(index).target);
v->move(screens.screen(index).geometry.topLeft());
v->resize(screens.screen(index).geometry.width(),screens.screen(index).geometry.height());
if (!index)
v->activateWindow();
v->show();
views.push_back(v);
}
}
//----------------------------------------------------------------------------
Scribble* Presenter::getCurrentScribble(bool createIfNeeded)
// Get the current scribble (if any)
{
switch (mode) {
case Overview: return nullptr;
case Normal:
if (scribbles.count(page)||createIfNeeded) {
return &(scribbles[page]);
} else return nullptr;
case Black: return nullptr;
case White: return &scratchScribble;
}
throw; // unreachable
}
//----------------------------------------------------------------------------
void Presenter::paintPage(QPainter& painter,View* view)
// Draw the current page
{
// Rendet the PDF page
painter.fillRect(painter.viewport(),QBrush(Qt::black));
QImage* img=renderer.getPage(page);
if (img)
painter.drawImage(view->target.topLeft(),*img);
// Timer functionality
if (showTimer&&(view==views.front())) {
unsigned duration=time(0)-slidesLog.front().second;
// Show the time
char buffer[50];
snprintf(buffer,sizeof(buffer),"%u:%02u",duration/60,duration%60);
QRectF bounds=painter.boundingRect(QRect(0,0,1,1),buffer);
painter.setBackground(Qt::white);
painter.setBackgroundMode(Qt::OpaqueMode);
painter.drawText(view->width()-bounds.width(),bounds.height(),buffer);
// Show the progress if a profile is available
if (!profile.empty()) {
// Compute bar bounds
unsigned maxDuration=profile.back();
if (!maxDuration) maxDuration=1;
unsigned showDuration=duration;
if (showDuration>maxDuration) showDuration=maxDuration;
unsigned expectedDuration=(page<profile.size())?profile[page]:maxDuration;
if (expectedDuration>maxDuration) expectedDuration=maxDuration;
unsigned right=view->width()-bounds.width();
// And paint
painter.setBrush(Qt::white);
painter.setPen(Qt::black);
painter.drawRect(0,0,(right*expectedDuration)/maxDuration,5);
painter.drawRect(0,8,(right*showDuration)/maxDuration,5);
}
}
}
//----------------------------------------------------------------------------
void Presenter::paintOverview(QPainter& painter,View* view)
// Draw the overview page
{
painter.fillRect(painter.viewport(),QBrush(Qt::black));
for (unsigned index=0;index<renderer.getPageCount();index++) {
QImage* img=(index==page)?renderer.getThumbnailPage(index):renderer.getDarkThumbnailPage(index);
if (!img) continue;
unsigned x=index%thumbX,y=index/thumbX;
unsigned px=view->target.left()+(x*thumbSpacing.width()),py=view->target.top()+(y*thumbSpacing.height());
painter.drawImage(px,py,*img);
}
}
//----------------------------------------------------------------------------
void Presenter::paint(QPainter& painter,View* view)
// Draw the current state
{
switch (mode) {
case Overview:
if (view==views.front()) {
paintOverview(painter,view);
break;
}
// fallthrough
case Normal:
paintPage(painter,view);
if (auto scribble=getCurrentScribble()) {
painter.setRenderHint(QPainter::Antialiasing);
scribble->paint(painter,view->target);
}
break;
case Black:
painter.fillRect(painter.viewport(),QBrush(Qt::black));
break;
case White:
painter.fillRect(painter.viewport(),QBrush(Qt::white));
if (auto scribble=getCurrentScribble()) {
painter.setRenderHint(QPainter::Antialiasing);
scribble->paint(painter,view->target);
}
break;
}
}
//----------------------------------------------------------------------------
QSize Presenter::presentationSize() const
// The size of the presentation area
{
return QSize(screens.common().width(),screens.common().height());
}
//----------------------------------------------------------------------------
QSize Presenter::thumbnailSize() const
// The size of thumbnails
{
return thumbSize;
}
//----------------------------------------------------------------------------
void Presenter::invalidateViews()
// Invalidate all views
{
for (unsigned index=0;index<views.size();index++)
views[index]->update();
}
//----------------------------------------------------------------------------
void Presenter::goTo(unsigned page)
// Go to a specific page
{
if (page!=this->page) {
this->page=page;
if (!slidesLog.empty())
slidesLog.push_back(pair<unsigned,unsigned>(page,time(0)));
invalidateViews();
}
}
//----------------------------------------------------------------------------
void Presenter::incPage(unsigned step)
// Increment the current page
{
if (page+step>=renderer.getPageCount())
goTo(renderer.getPageCount()-1); else
goTo(page+step);
}
//----------------------------------------------------------------------------
void Presenter::decPage(unsigned steps)
// Decrement the current page
{
if (page<=steps)
goTo(0); else
goTo(page-steps);
}
//----------------------------------------------------------------------------
void Presenter::firstPage()
// Go to the first page
{
if (((mode==Normal)||(mode==Overview))&&(page)) {
goTo(0);
}
}
//----------------------------------------------------------------------------
void Presenter::pageUp()
// Go to the previous page
{
if (mode==Normal) {
decPage(1);
} else if (mode==Overview) {
decPage(thumbX);
}
}
//----------------------------------------------------------------------------
void Presenter::previousPage()
// Go to the previous page
{
if (((mode==Normal)||(mode==Overview))&&(page)) {
decPage(1);
}
}
//----------------------------------------------------------------------------
void Presenter::nextPage()
// Go to the next page
{
if (((mode==Normal)||(mode==Overview))&&((page+1)<renderer.getPageCount())) {
incPage(1);
}
}
//----------------------------------------------------------------------------
void Presenter::pageDown()
// Go to the next page
{
if (mode==Normal) {
incPage(1);
} else if (mode==Overview) {
incPage(thumbX);
}
}
//----------------------------------------------------------------------------
void Presenter::lastPage()
// Go to the last page
{
if (((mode==Normal)||(mode==Overview))&&((page+1)<renderer.getPageCount())) {
goTo(renderer.getPageCount()-1);
}
}
//----------------------------------------------------------------------------
void Presenter::confirmPage()
// Confirm the current page
{
if (mode!=Normal) {
mode=Normal;
invalidateViews();
} else {
incPage(1);
}
}
//----------------------------------------------------------------------------
void Presenter::toggleWhite()
// Toggle to white
{
if (mode==White)
mode=Normal; else
mode=White;
invalidateViews();
}
//----------------------------------------------------------------------------
void Presenter::toggleBlack()
// Toggle to black
{
if (mode==Black)
mode=Normal; else
mode=Black;
invalidateViews();
}
//----------------------------------------------------------------------------
void Presenter::toggleThumbnails()
// Toggle between thumbnail view
{
if (mode==Overview)
mode=Normal; else
mode=Overview;
invalidateViews();
}
//----------------------------------------------------------------------------
void Presenter::clicked(unsigned x,unsigned y)
// Handle a mouse click
{
if (mode==Overview) {
unsigned tx=x/thumbSpacing.width();
unsigned ty=y/thumbSpacing.height();
unsigned p=tx+(thumbX*ty);
if ((tx<thumbX)&&(ty<thumbY)&&(p<renderer.getPageCount())) {
mode=Normal;
if (page!=p)
goTo(p); else
invalidateViews();
}
}
}
//----------------------------------------------------------------------------
void Presenter::toggleTimer()
// Toggle the timer display
{
if (showTimer) {
showTimer=false;
timer.stop();
} else {
showTimer=true;
timer.start(1000);
}
if (showTimer&&slidesLog.empty())
resetTimer();
invalidateViews();
}
//----------------------------------------------------------------------------
void Presenter::resetTimer()
// Reset the timer
{
slidesLog.clear();
slidesLog.push_back(pair<unsigned,unsigned>(page,time(0)));
}
//----------------------------------------------------------------------------
void Presenter::pageChanged(unsigned index)
// A page changed
{
if (mode==Normal) {
if (page==index)
invalidateViews();
} else if (mode==Overview) {
invalidateViews();
}
}
//----------------------------------------------------------------------------
void Presenter::tick()
// A second passed
{
if (showTimer) {
View* v=views.front();
QRect rect=QRect(0,0,v->width(),v->fontMetrics().height());
v->update(rect);
}
}
//----------------------------------------------------------------------------
void Presenter::clearScribble()
// Clear the scribble
{
if (auto scribble=getCurrentScribble()) {
scribble->clear();
invalidateViews();
}
}
//----------------------------------------------------------------------------
void Presenter::drawLine(int x1,int y1,int x2,int y2,double intensity)
// Add a line
{
if (auto scribble=getCurrentScribble(true)) {
QRect bb=scribble->drawLine(x1,y1,x2,y2,lineWidth*intensity,lineColor);
if (!bb.isEmpty())
for (auto view:views)
view->update(bb.translated(view->target.topLeft()));
}
}
//----------------------------------------------------------------------------
void Presenter::eraseLine(int x1,int y1,int x2,int y2,double intensity)
// Erase a previously drawn line
{
if (auto scribble=getCurrentScribble()) {
QRect bb=scribble->eraseLine(x1,y1,x2,y2,2*lineWidth*intensity);
if (!bb.isEmpty())
for (auto view:views)
view->update(bb.translated(view->target.topLeft()));
}
}
//----------------------------------------------------------------------------
void Presenter::setLineWidth(unsigned width)
// Set the line width
{
lineWidth=width;
}
//----------------------------------------------------------------------------
void Presenter::setLineColor(QColor color)
// Set the line color
{
lineColor=color;
}
//----------------------------------------------------------------------------