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

Annotation of /infoscreen/mainview.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20