1 |
#include "MainView.h" |
2 |
#include <QApplication> |
3 |
#include <QPushButton> |
4 |
#include <QLabel> |
5 |
#include <QVBoxLayout> |
6 |
|
7 |
#include <QTimer> |
8 |
#include <QKeyEvent> |
9 |
#include <QSettings> |
10 |
#include <QMessageBox> |
11 |
|
12 |
#include "MyWebView.h" |
13 |
#include "clientsiderender.h" |
14 |
#include "pictureview.h" |
15 |
#include "videoview.h" |
16 |
|
17 |
#include "httpwrapper.h" |
18 |
|
19 |
|
20 |
MainView::MainView(QWidget* parent) |
21 |
: QWidget(parent), timer(0) |
22 |
{ |
23 |
|
24 |
settings = new QSettings("Caddi", "infoscreen"); |
25 |
url = settings->value("url").toString(); |
26 |
bool xmlMode = settings->value("xml").toBool(); |
27 |
screenid = settings->value("screenid").toString(); |
28 |
|
29 |
if (url == "" || screenid == "") { |
30 |
QMessageBox::warning(this,"infoscreen","Could not find url or screenid in config file " + settings->fileName()); |
31 |
|
32 |
exit(1); //Normal qApp->exit() doesn't terminate the init sequence so use std C exit function |
33 |
} |
34 |
|
35 |
|
36 |
|
37 |
this->resize(400,400); |
38 |
this->setWindowState( Qt::WindowFullScreen ); |
39 |
this->grabKeyboard(); |
40 |
|
41 |
qApp->setOverrideCursor( QCursor( Qt::BlankCursor) ); |
42 |
|
43 |
render = new ClientSideRender(this); |
44 |
render->setVisible( false ); |
45 |
|
46 |
web = new MyWebView(this); |
47 |
web->setVisible( false); |
48 |
|
49 |
picture = new PictureView(this); |
50 |
picture->setVisible( false ); |
51 |
|
52 |
video = new VideoView(this); |
53 |
video->setVisible(false); |
54 |
|
55 |
QVBoxLayout* layout = new QVBoxLayout(); |
56 |
layout->addWidget(web,1); |
57 |
layout->addWidget(render,1); |
58 |
layout->addWidget(picture,1); |
59 |
layout->addWidget(video,1); |
60 |
layout->addStretch(); |
61 |
layout->setContentsMargins(0,0,0,0); |
62 |
setLayout(layout); |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
if (xmlMode == true) { |
68 |
xmlUrl = url + "?screen_id=" + screenid; |
69 |
qDebug() << "Starting XML mode"; |
70 |
qDebug() << "xmlUrl" << xmlUrl; |
71 |
|
72 |
timer = new QTimer(this); |
73 |
connect(timer, SIGNAL(timeout() ), this, SLOT(onTimer() )); |
74 |
timer->start(100); |
75 |
} else { |
76 |
qDebug() << "Starting plain browser mode"; |
77 |
web->setVisible(true); |
78 |
web->start(url,screenid); |
79 |
} |
80 |
} |
81 |
|
82 |
void MainView::closeEvent ( QCloseEvent * event ) |
83 |
{ |
84 |
Q_UNUSED(event); |
85 |
exit(0); //force application shutdown |
86 |
} |
87 |
|
88 |
void MainView::keyPressEvent ( QKeyEvent* event ) |
89 |
{ |
90 |
int key = event->key(); |
91 |
if (key == ' ' || key == Qt::Key_Return || key == Qt::Key_Enter) { |
92 |
close(); |
93 |
} |
94 |
} |
95 |
|
96 |
void MainView::onTimer() |
97 |
{ |
98 |
screenManager.timerTick(); |
99 |
|
100 |
readXml(); |
101 |
|
102 |
switchScreens(); |
103 |
} |
104 |
|
105 |
|
106 |
bool MainView::readXml() |
107 |
{ |
108 |
const int TIMEOUT = 30*60*1000; // 30 minutter |
109 |
if ( lastXml.isNull() || lastXml.elapsed() > TIMEOUT) { |
110 |
|
111 |
|
112 |
bool res = xmlHandler.readXml( xmlUrl ); |
113 |
|
114 |
lastXml = QTime::currentTime(); |
115 |
|
116 |
screenItems = xmlHandler.getScreenSet(); |
117 |
|
118 |
if ( currentItemIdx >= screenItems.size() ) |
119 |
currentItemIdx = screenItems.size()-1; //avoid overflow |
120 |
|
121 |
|
122 |
return true; |
123 |
} else { |
124 |
return false; |
125 |
} |
126 |
} |
127 |
|
128 |
void MainView::switchScreens() |
129 |
{ |
130 |
if (video->isVisible() && video->isPlaying() ) { |
131 |
return; //wait until current clip has finished |
132 |
} |
133 |
|
134 |
if (lastScreenSwitch.isNull() || lastScreenSwitch.elapsed() > (currentItem.runtime*1000)) { |
135 |
|
136 |
QTime now = QTime::currentTime(); |
137 |
if (lastScreenSwitch.isNull()) |
138 |
currentItemIdx = -1; |
139 |
|
140 |
bool found = false; |
141 |
int tries = 0; |
142 |
|
143 |
if (screenItems.size() > 0) { //only try if we have a any screens |
144 |
while (found == false && tries <= screenItems.size()) { //find next with valid display time |
145 |
tries++; |
146 |
currentItemIdx = (currentItemIdx+1) % screenItems.size(); |
147 |
currentItem = screenItems.at(currentItemIdx); |
148 |
|
149 |
if (currentItem.start <= now && now <= currentItem.stop ) |
150 |
found = true; |
151 |
} |
152 |
} |
153 |
|
154 |
if (found) { |
155 |
|
156 |
if (currentItem.module == "info_image") { |
157 |
ensureVisible(picture); |
158 |
|
159 |
picture->loadFromUrl( currentItem.url ); |
160 |
|
161 |
} |
162 |
if (currentItem.module == "info_web") { |
163 |
ensureVisible(web); |
164 |
web->load(currentItem.url); |
165 |
} |
166 |
if (currentItem.module == "info_video") { |
167 |
ensureVisible(video); |
168 |
video->loadUrl(currentItem.url); |
169 |
} |
170 |
|
171 |
|
172 |
} else { |
173 |
qDebug() << "no screen"; |
174 |
noInfoScreen(); |
175 |
} |
176 |
lastScreenSwitch = QTime::currentTime(); |
177 |
|
178 |
} |
179 |
} |
180 |
|
181 |
void MainView::noInfoScreen() |
182 |
{ |
183 |
ensureVisible(web); |
184 |
web->setHtml("<html>\ |
185 |
<body text='#505050' bgcolor='#000000'>\ |
186 |
<table width='100%' height='100%'><tr><td align='center' valign='middel'><h1>Der er ingen information at vise</h1></td></tr></table>\ |
187 |
</body></html>"); |
188 |
} |
189 |
|
190 |
void MainView::ensureVisible(QWidget* widget) |
191 |
{ |
192 |
if (! widget->isVisible()) { |
193 |
hideAll(); |
194 |
widget->setVisible(true); |
195 |
} |
196 |
} |
197 |
|
198 |
void MainView::hideAll() |
199 |
{ |
200 |
render->setVisible( false ); |
201 |
web->setVisible( false); |
202 |
picture->setVisible( false ); |
203 |
video->setVisible(false); |
204 |
} |