-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy paths5runner.cpp
208 lines (179 loc) · 5.71 KB
/
s5runner.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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information ([email protected])
**
** This file is part of the Graphics Dojo project on Qt Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at [email protected].
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include <QtCore>
#include <QtGui>
#include <QtWebKitWidgets>
#if QT_VERSION <= 0x050000
#error You must use Qt >= 5.0.0
#endif
class S5Runner : public QWebView
{
Q_OBJECT
public:
S5Runner(QWidget *parent = 0)
: QWebView(parent)
, overlay(Qt::transparent)
, invert(false) {
connect(this, SIGNAL(titleChanged(const QString&)), SLOT(setWindowTitle(const QString&)));
timer.start(60*1000);
connect(&timer, SIGNAL(timeout()), SLOT(tick()));
minutes = 30;
}
private slots:
void tick() {
minutes--;
update();
}
private:
QColor overlay;
bool invert;
QTimer timer;
int minutes;
void paintEvent(QPaintEvent *event) {
if (overlay.alpha() != 0) {
QPainter p(this);
p.fillRect(event->rect(), overlay);
p.end();
} else {
QWebView::paintEvent(event);
QPainter p(this);
if (page()->isContentEditable()) {
QTextOption opts(Qt::AlignCenter);
p.save();
p.setOpacity(0.5);
p.setPen(Qt::white);
p.drawText(QRect(0, 0, width(), 25), "Edit Mode", opts);
p.restore();
}
if (invert) {
p.setCompositionMode(QPainter::CompositionMode_Difference);
p.fillRect(event->rect(), Qt::white);
}
if (true) {
QTextOption opts(Qt::AlignCenter);
QRect corner(width() - 100, height() - 50, 50, 50);
QFont f = font();
f.setPixelSize(30);
p.setFont(f);
p.setOpacity(0.6);
p.drawText(corner, QString::number(minutes), opts);
}
p.end();
}
}
void keyPressEvent(QKeyEvent *event) {
// Esc resets everything
if (event->key() == Qt::Key_Escape) {
page()->setContentEditable(false);
overlay = Qt::transparent;
showNormal();
update();
event->accept();
return;
}
// F5 reloads the web content
if (event->key() == Qt::Key_F5) {
reload();
event->accept();
return;
}
// F3 allows editing
if (event->key() == Qt::Key_F3) {
page()->setContentEditable(!page()->isContentEditable());
update();
event->accept();
return;
}
// F toggles full-screen
if (!page()->isContentEditable() && event->key() == Qt::Key_F) {
if (isFullScreen())
showNormal();
else
showFullScreen();
event->accept();
return;
}
// B sets the screen to black
if (!page()->isContentEditable() && event->key() == Qt::Key_B) {
overlay = (overlay == Qt::black) ? Qt::transparent : Qt::black;
update();
event->accept();
return;
}
// W sets the screen to white
if (!page()->isContentEditable() && event->key() == Qt::Key_W) {
overlay = (overlay == Qt::white) ? Qt::transparent : Qt::white;
update();
event->accept();
return;
}
// N toggles night-mode
if (!page()->isContentEditable() && event->key() == Qt::Key_N) {
invert = !invert;
update();
event->accept();
return;
}
// Navigation keys
if (!page()->isContentEditable()) {
int sendkey = -1;
switch (event->key()) {
case Qt::Key_Left:
sendkey = 37;
break;
case Qt::Key_Right:
sendkey = 39;
break;
default:
break;
}
if (sendkey > 0) {
QWebFrame *f = page()->mainFrame();
QString trigger = QString("var k = { which: %1 }; keys(k)").arg(sendkey);
f->evaluateJavaScript(trigger);
event->accept();
return;
}
} else {
QWebView::keyPressEvent(event);
}
}
};
#include "s5runner.moc"
int main(int argc, char **argv)
{
#ifdef Q_WS_X11
QApplication::setGraphicsSystem("raster");
#endif
QApplication app(argc, argv);
QString fname;
if (argc == 2)
fname = QString::fromLocal8Bit(argv[1]);
else
fname = QFileDialog::getOpenFileName(0, "Open Presentation");
S5Runner w;
w.resize(800, 600);
w.load(QUrl::fromLocalFile(fname));
w.show();
return app.exec();
}