1 |
#include "mainview.h" |
2 |
#include <QApplication> |
3 |
#include <QPushButton> |
4 |
#include <QLabel> |
5 |
#include <QStackedLayout> |
6 |
#include <QSvgWidget> |
7 |
|
8 |
#include <QTimer> |
9 |
#include <QKeyEvent> |
10 |
#include <QSettings> |
11 |
#include <QMessageBox> |
12 |
|
13 |
#include "webview.h" |
14 |
#include "clientsiderender.h" |
15 |
#include "pictureview.h" |
16 |
#include "videoview.h" |
17 |
#include "clockoverlay.h" |
18 |
#include "httpwrapper.h" |
19 |
#include "screenmanager.h" |
20 |
|
21 |
|
22 |
|
23 |
MainView::MainView(QWidget* parent) |
24 |
: QWidget(parent), timer(0) |
25 |
{ |
26 |
loadSettings(); |
27 |
|
28 |
if ( currentMode == ModeXml) { |
29 |
xmlUrl = url + "?screen_id=" + screenid; |
30 |
qDebug() << "Starting XML mode"; |
31 |
qDebug() << "xmlUrl" << xmlUrl; |
32 |
qDebug() << "xmlInterval" << xmlInterval/(60*1000); |
33 |
} |
34 |
|
35 |
if (currentMode == ModeSimpleWeb){ |
36 |
qDebug() << "Starting plain browser mode"; |
37 |
} |
38 |
|
39 |
if (currentMode == ModeLocal ) { |
40 |
qDebug() << "Starting local mode"; |
41 |
qDebug() << "path" << path; |
42 |
|
43 |
readLocalFiles(); |
44 |
} |
45 |
|
46 |
this->resize(640,480); |
47 |
this->setWindowState( Qt::WindowFullScreen ); |
48 |
this->grabKeyboard(); |
49 |
|
50 |
qApp->setOverrideCursor( QCursor( Qt::BlankCursor) ); |
51 |
|
52 |
render = new ClientSideRender(this); |
53 |
|
54 |
web = new WebView(this); |
55 |
|
56 |
picture = new PictureView(this); |
57 |
|
58 |
video = new VideoView(this); |
59 |
|
60 |
svg = new QSvgWidget(this); |
61 |
|
62 |
layout = new QStackedLayout(); |
63 |
layout->addWidget(web); |
64 |
layout->addWidget(render); |
65 |
layout->addWidget(picture); |
66 |
layout->addWidget(video); |
67 |
layout->addWidget(svg); |
68 |
layout->setContentsMargins(0,0,0,0); |
69 |
setLayout(layout); |
70 |
|
71 |
if (showClock == true) { |
72 |
clockOverlay = new ClockOverlay(this); |
73 |
clockOverlay->move(10,10); |
74 |
clockOverlay->resize(150,75); |
75 |
} |
76 |
|
77 |
|
78 |
|
79 |
if (currentMode == ModeSimpleWeb) { |
80 |
web->setVisible(true); |
81 |
web->start(url,screenid); |
82 |
} |
83 |
|
84 |
if (enableScreenManager) { |
85 |
screenManager = new ScreenManager(screenManagerOn, screenManagerOff); |
86 |
} else { |
87 |
qDebug() << "ScreenManager is disabled"; |
88 |
} |
89 |
|
90 |
|
91 |
qDebug() << "Starting timer..."; |
92 |
timer = new QTimer(this); |
93 |
connect(timer, SIGNAL(timeout() ), this, SLOT(onTimer() )); |
94 |
timer->start(100); |
95 |
} |
96 |
|
97 |
void MainView::loadSettings() |
98 |
{ |
99 |
settings = new QSettings("Caddi", "infoscreen"); |
100 |
|
101 |
qDebug() << "Loading settings" << settings->fileName(); |
102 |
|
103 |
QString mode = settings->value("mode").toString().toLower(); |
104 |
if (mode == "simpleweb") { |
105 |
currentMode = ModeSimpleWeb; |
106 |
} else if (mode == "xml") { |
107 |
currentMode = ModeXml; |
108 |
} else if (mode == "local") { |
109 |
currentMode = ModeLocal; |
110 |
} else { |
111 |
currentMode = ModeNone; |
112 |
QMessageBox::warning(this, "infoscreen", "no operation mode set or mode given an invalid value"); |
113 |
exit(1); |
114 |
} |
115 |
|
116 |
enableScreenManager = settings->value("enablescreenmanager").toBool(); |
117 |
if (enableScreenManager) { |
118 |
screenManagerOn = settings->value("screenmanager_on").toTime(); |
119 |
screenManagerOff = settings->value("screenmanager_off").toTime(); |
120 |
} |
121 |
|
122 |
showClock = settings->value("showclock").toBool(); |
123 |
|
124 |
xmlInterval = settings->value("xmlinterval", 30).toInt(); |
125 |
xmlInterval = (xmlInterval * 60 * 1000); //convert to milliseconds |
126 |
|
127 |
|
128 |
if (currentMode == ModeSimpleWeb || currentMode == ModeXml) { |
129 |
url = settings->value("url").toString(); |
130 |
screenid = settings->value("screenid").toString(); |
131 |
if (url == "" || screenid == "") { |
132 |
QMessageBox::warning(this,"infoscreen","Could not find url or screenid in config file " + settings->fileName()); |
133 |
|
134 |
exit(1); //Normal qApp->exit() doesn't terminate the init sequence so use std C exit function |
135 |
} |
136 |
} |
137 |
|
138 |
if (currentMode == ModeLocal) { |
139 |
path = settings->value("path").toString(); |
140 |
|
141 |
if (path == "") { |
142 |
QMessageBox::warning(this,"infoscreen","Could not find path in config file " + settings->fileName()); |
143 |
exit(1); |
144 |
} |
145 |
} |
146 |
} |
147 |
|
148 |
void MainView::closeEvent ( QCloseEvent * event ) |
149 |
{ |
150 |
Q_UNUSED(event); |
151 |
exit(0); //force application shutdown |
152 |
} |
153 |
|
154 |
void MainView::keyPressEvent ( QKeyEvent* event ) |
155 |
{ |
156 |
int key = event->key(); |
157 |
if (key == ' ' || key == Qt::Key_Return || key == Qt::Key_Enter) { |
158 |
close(); |
159 |
} |
160 |
|
161 |
if (key == 'f' || key == 'F') { |
162 |
Qt::WindowStates current = this->windowState(); |
163 |
if (current == Qt::WindowFullScreen) { |
164 |
this->setWindowState(Qt::WindowActive); |
165 |
} else { |
166 |
this->setWindowState(Qt::WindowFullScreen); |
167 |
} |
168 |
} |
169 |
|
170 |
} |
171 |
|
172 |
void MainView::onTimer() |
173 |
{ |
174 |
if (enableScreenManager == true) { |
175 |
screenManager->timerTick(); |
176 |
} |
177 |
|
178 |
if (currentMode == ModeXml) { |
179 |
readXml(); |
180 |
} |
181 |
|
182 |
if (currentMode == ModeXml || currentMode == ModeLocal) { |
183 |
switchScreens(); |
184 |
} |
185 |
if (showClock == true) { |
186 |
clockOverlay->timerTick(); |
187 |
} |
188 |
} |
189 |
|
190 |
|
191 |
void MainView::readLocalFiles() |
192 |
{ |
193 |
QDir dir(path); |
194 |
if (! dir.exists()) { |
195 |
QMessageBox::warning(this,"infoscreen","Local Source directory not found: " + path); |
196 |
exit(1); |
197 |
} |
198 |
QFileInfoList files = dir.entryInfoList(QDir::Files, QDir::Name); //only files, sort by name |
199 |
|
200 |
for (int i=0; i<files.size(); ++i) { |
201 |
QFileInfo file = files[i]; |
202 |
qDebug() << "Found" << file.fileName(); |
203 |
|
204 |
ScreenItem item; |
205 |
item.url = file.filePath(); |
206 |
item.module = ModuleUnknown; |
207 |
|
208 |
QString ext = file.suffix().toLower(); |
209 |
if (ext == "avi" || ext == "mpg" || ext == "mpeg") { |
210 |
item.module = ModuleVideo; |
211 |
item.runtime = 1; |
212 |
} |
213 |
|
214 |
if (ext == "jpg" || ext == "jpeg" || ext == "png" ) { |
215 |
item.module = ModuleImage; |
216 |
item.runtime = 10; |
217 |
} |
218 |
|
219 |
if (ext == "svg") { |
220 |
item.module = ModuleSvg; |
221 |
item.runtime = 10; |
222 |
} |
223 |
|
224 |
if (ext == "htm" || ext == "html") { |
225 |
item.module = ModuleWeb; |
226 |
item.runtime = 10; |
227 |
} |
228 |
|
229 |
if (item.module != ModuleUnknown) { //no need to enqueue unknown modules |
230 |
screenItems.push_back( item ); |
231 |
} |
232 |
|
233 |
} |
234 |
|
235 |
qDebug() << "Found " << screenItems.size() << " playable items"; |
236 |
} |
237 |
|
238 |
|
239 |
bool MainView::readXml() |
240 |
{ |
241 |
if ( lastXml.isNull() || lastXml.elapsed() > xmlInterval) { |
242 |
qDebug() << "Reading XML"; |
243 |
|
244 |
|
245 |
bool res = xmlHandler.readXml( xmlUrl ); |
246 |
|
247 |
lastXml = QTime::currentTime(); |
248 |
|
249 |
screenItems = xmlHandler.getScreenSet(); |
250 |
|
251 |
if ( currentItemIdx >= screenItems.size() ) |
252 |
currentItemIdx = screenItems.size()-1; //avoid overflow |
253 |
|
254 |
|
255 |
return true; |
256 |
} else { |
257 |
return false; |
258 |
} |
259 |
} |
260 |
|
261 |
void MainView::switchScreens() |
262 |
{ |
263 |
if (video->isVisible() && video->isPlaying() ) { |
264 |
return; //wait until current clip has finished |
265 |
} |
266 |
|
267 |
if (lastScreenSwitch.isNull() || lastScreenSwitch.elapsed() > (currentItem.runtime*1000)) { |
268 |
QTime now = QTime::currentTime(); |
269 |
if (lastScreenSwitch.isNull()) |
270 |
currentItemIdx = -1; |
271 |
|
272 |
bool found = false; |
273 |
int tries = 0; |
274 |
|
275 |
if (screenItems.size() > 0) { //only try if we have a any screens |
276 |
while (found == false && tries <= screenItems.size()) { //find next with valid display time |
277 |
tries++; |
278 |
currentItemIdx = (currentItemIdx+1) % screenItems.size(); |
279 |
currentItem = screenItems.at(currentItemIdx); |
280 |
|
281 |
if (currentItem.start.isValid() && currentItem.stop.isValid()) { |
282 |
if (currentItem.start <= now && now <= currentItem.stop ) |
283 |
found = true; |
284 |
} else { // if start or stop time was invalid - show them always |
285 |
found = true; |
286 |
} |
287 |
} |
288 |
} |
289 |
|
290 |
if (found) { |
291 |
switch(currentItem.module) { |
292 |
case ModuleImage: |
293 |
ensureVisible(picture); |
294 |
picture->loadFromUrl( currentItem.url ); |
295 |
break; |
296 |
case ModuleWeb: |
297 |
ensureVisible(web); |
298 |
web->load(currentItem.url); |
299 |
break; |
300 |
case ModuleVideo: |
301 |
ensureVisible(video); |
302 |
video->loadUrl(currentItem.url); |
303 |
break; |
304 |
case ModuleSvg: |
305 |
ensureVisible(svg); |
306 |
svg->load(currentItem.url); |
307 |
break; |
308 |
default: |
309 |
// ModuleUnknown - what should we do?? |
310 |
break; |
311 |
} |
312 |
|
313 |
} else { |
314 |
errorInfoScreen("Der er ingen information at vise"); |
315 |
currentItem = ScreenItem(); |
316 |
currentItem.runtime = 60; //switch screens again in 1 minute |
317 |
} |
318 |
lastScreenSwitch = QTime::currentTime(); |
319 |
|
320 |
} |
321 |
} |
322 |
|
323 |
void MainView::errorInfoScreen(QString msg) |
324 |
{ |
325 |
ensureVisible(web); |
326 |
QString html("<html><body text='#505050' bgcolor='#000000'>"); |
327 |
html.append("<table width='100%' height='100%'><tr><td align='center' valign='middel'><h1>") |
328 |
.append(msg) |
329 |
.append("</h1></td></tr></table></body></html>"); |
330 |
web->setHtml(html); |
331 |
} |
332 |
|
333 |
void MainView::ensureVisible(QWidget* widget) |
334 |
{ |
335 |
layout->setCurrentWidget(widget); |
336 |
} |