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(400,400); |
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 |
|
162 |
void MainView::onTimer() |
163 |
{ |
164 |
if (enableScreenManager == true) { |
165 |
screenManager->timerTick(); |
166 |
} |
167 |
|
168 |
if (currentMode == ModeXml) { |
169 |
readXml(); |
170 |
} |
171 |
|
172 |
if (currentMode == ModeXml || currentMode == ModeLocal) { |
173 |
switchScreens(); |
174 |
} |
175 |
if (showClock == true) { |
176 |
clockOverlay->timerTick(); |
177 |
} |
178 |
} |
179 |
|
180 |
|
181 |
void MainView::readLocalFiles() |
182 |
{ |
183 |
QDir dir(path); |
184 |
if (! dir.exists()) { |
185 |
QMessageBox::warning(this,"infoscreen","Local Source directory not found: " + path); |
186 |
exit(1); |
187 |
} |
188 |
QFileInfoList files = dir.entryInfoList(QDir::Files, QDir::Name); //only files, sort by name |
189 |
|
190 |
for (int i=0; i<files.size(); ++i) { |
191 |
QFileInfo file = files[i]; |
192 |
qDebug() << "Found" << file.fileName(); |
193 |
|
194 |
ScreenItem item; |
195 |
item.url = file.filePath(); |
196 |
item.module = ModuleUnknown; |
197 |
|
198 |
QString ext = file.suffix().toLower(); |
199 |
if (ext == "avi" || ext == "mpg" || ext == "mpeg") { |
200 |
item.module = ModuleVideo; |
201 |
item.runtime = 1; |
202 |
} |
203 |
|
204 |
if (ext == "jpg" || ext == "jpeg" || ext == "png" ) { |
205 |
item.module = ModuleImage; |
206 |
item.runtime = 10; |
207 |
} |
208 |
|
209 |
if (ext == "svg") { |
210 |
item.module = ModuleSvg; |
211 |
item.runtime = 10; |
212 |
} |
213 |
|
214 |
if (ext == "htm" || ext == "html") { |
215 |
item.module = ModuleWeb; |
216 |
item.runtime = 10; |
217 |
} |
218 |
|
219 |
if (item.module != ModuleUnknown) { //no need to enqueue unknown modules |
220 |
screenItems.push_back( item ); |
221 |
} |
222 |
|
223 |
} |
224 |
|
225 |
qDebug() << "Found " << screenItems.size() << " playable items"; |
226 |
} |
227 |
|
228 |
|
229 |
bool MainView::readXml() |
230 |
{ |
231 |
if ( lastXml.isNull() || lastXml.elapsed() > xmlInterval) { |
232 |
qDebug() << "Reading XML"; |
233 |
|
234 |
|
235 |
bool res = xmlHandler.readXml( xmlUrl ); |
236 |
|
237 |
lastXml = QTime::currentTime(); |
238 |
|
239 |
screenItems = xmlHandler.getScreenSet(); |
240 |
|
241 |
if ( currentItemIdx >= screenItems.size() ) |
242 |
currentItemIdx = screenItems.size()-1; //avoid overflow |
243 |
|
244 |
|
245 |
return true; |
246 |
} else { |
247 |
return false; |
248 |
} |
249 |
} |
250 |
|
251 |
void MainView::switchScreens() |
252 |
{ |
253 |
if (video->isVisible() && video->isPlaying() ) { |
254 |
return; //wait until current clip has finished |
255 |
} |
256 |
|
257 |
if (lastScreenSwitch.isNull() || lastScreenSwitch.elapsed() > (currentItem.runtime*1000)) { |
258 |
QTime now = QTime::currentTime(); |
259 |
if (lastScreenSwitch.isNull()) |
260 |
currentItemIdx = -1; |
261 |
|
262 |
bool found = false; |
263 |
int tries = 0; |
264 |
|
265 |
if (screenItems.size() > 0) { //only try if we have a any screens |
266 |
while (found == false && tries <= screenItems.size()) { //find next with valid display time |
267 |
tries++; |
268 |
currentItemIdx = (currentItemIdx+1) % screenItems.size(); |
269 |
currentItem = screenItems.at(currentItemIdx); |
270 |
|
271 |
if (currentItem.start.isValid() && currentItem.stop.isValid()) { |
272 |
if (currentItem.start <= now && now <= currentItem.stop ) |
273 |
found = true; |
274 |
} else { // if start or stop time was invalid - show them always |
275 |
found = true; |
276 |
} |
277 |
} |
278 |
} |
279 |
|
280 |
if (found) { |
281 |
switch(currentItem.module) { |
282 |
case ModuleImage: |
283 |
ensureVisible(picture); |
284 |
picture->loadFromUrl( currentItem.url ); |
285 |
break; |
286 |
case ModuleWeb: |
287 |
ensureVisible(web); |
288 |
web->load(currentItem.url); |
289 |
break; |
290 |
case ModuleVideo: |
291 |
ensureVisible(video); |
292 |
video->loadUrl(currentItem.url); |
293 |
break; |
294 |
case ModuleSvg: |
295 |
ensureVisible(svg); |
296 |
svg->load(currentItem.url); |
297 |
break; |
298 |
default: |
299 |
// ModuleUnknown - what should we do?? |
300 |
break; |
301 |
} |
302 |
|
303 |
} else { |
304 |
errorInfoScreen("Der er ingen information at vise"); |
305 |
currentItem = ScreenItem(); |
306 |
currentItem.runtime = 60; //switch screens again in 1 minute |
307 |
} |
308 |
lastScreenSwitch = QTime::currentTime(); |
309 |
|
310 |
} |
311 |
} |
312 |
|
313 |
void MainView::errorInfoScreen(QString msg) |
314 |
{ |
315 |
ensureVisible(web); |
316 |
QString html("<html><body text='#505050' bgcolor='#000000'>"); |
317 |
html.append("<table width='100%' height='100%'><tr><td align='center' valign='middel'><h1>") |
318 |
.append(msg) |
319 |
.append("</h1></td></tr></table></body></html>"); |
320 |
web->setHtml(html); |
321 |
} |
322 |
|
323 |
void MainView::ensureVisible(QWidget* widget) |
324 |
{ |
325 |
layout->setCurrentWidget(widget); |
326 |
} |