/[projects]/infoscreen/MainView.cpp
ViewVC logotype

Contents of /infoscreen/MainView.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20