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

Annotation of /infoscreen/mainview.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 902 - (hide annotations) (download)
Fri Jun 25 07:17:40 2010 UTC (13 years, 10 months ago) by torben
File size: 8856 byte(s)
make the clock configurable
1 torben 899 #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 902 if (showClock == true) {
72     clockOverlay = new ClockOverlay(this);
73     clockOverlay->move(10,10);
74     clockOverlay->resize(150,75);
75     }
76 torben 883
77    
78    
79 torben 872 if (currentMode == ModeSimpleWeb) {
80     web->setVisible(true);
81     web->start(url,screenid);
82     }
83 torben 528
84 torben 875 if (enableScreenManager) {
85 torben 876 screenManager = new ScreenManager(screenManagerOn, screenManagerOff);
86 torben 875 } else {
87     qDebug() << "ScreenManager is disabled";
88     }
89 torben 873
90 torben 875
91 torben 873 qDebug() << "Starting timer...";
92     timer = new QTimer(this);
93     connect(timer, SIGNAL(timeout() ), this, SLOT(onTimer() ));
94     timer->start(100);
95 torben 537 }
96 torben 535
97 torben 638 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 torben 876 enableScreenManager = settings->value("enablescreenmanager").toBool();
117     if (enableScreenManager) {
118     screenManagerOn = settings->value("screenmanager_on").toTime();
119     screenManagerOff = settings->value("screenmanager_off").toTime();
120     }
121 torben 638
122 torben 902 showClock = settings->value("showclock").toBool();
123    
124 torben 881 xmlInterval = settings->value("xmlinterval", 30).toInt();
125     xmlInterval = (xmlInterval * 60 * 1000); //convert to milliseconds
126 torben 875
127 torben 881
128 torben 638 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 torben 537 void MainView::closeEvent ( QCloseEvent * event )
149     {
150     Q_UNUSED(event);
151     exit(0); //force application shutdown
152 torben 509 }
153    
154 torben 532 void MainView::keyPressEvent ( QKeyEvent* event )
155     {
156 torben 533 int key = event->key();
157     if (key == ' ' || key == Qt::Key_Return || key == Qt::Key_Enter) {
158 torben 537 close();
159 torben 533 }
160 torben 532 }
161 torben 509
162     void MainView::onTimer()
163     {
164 torben 875 if (enableScreenManager == true) {
165     screenManager->timerTick();
166     }
167 torben 524
168 torben 638 if (currentMode == ModeXml) {
169     readXml();
170     }
171 torben 527
172 torben 873 if (currentMode == ModeXml || currentMode == ModeLocal) {
173     switchScreens();
174     }
175 torben 902 if (showClock == true) {
176     clockOverlay->timerTick();
177     }
178 torben 528 }
179 torben 527
180 torben 524
181 torben 638 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 torben 708 if (ext == "avi" || ext == "mpg" || ext == "mpeg") {
200 torben 638 item.module = ModuleVideo;
201     item.runtime = 1;
202     }
203    
204 torben 708 if (ext == "jpg" || ext == "jpeg" || ext == "png" ) {
205 torben 638 item.module = ModuleImage;
206     item.runtime = 10;
207     }
208    
209 torben 867 if (ext == "svg") {
210     item.module = ModuleSvg;
211     item.runtime = 10;
212     }
213 torben 869
214     if (ext == "htm" || ext == "html") {
215     item.module = ModuleWeb;
216     item.runtime = 10;
217     }
218 torben 867
219 torben 638 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 torben 528 bool MainView::readXml()
230     {
231 torben 881 if ( lastXml.isNull() || lastXml.elapsed() > xmlInterval) {
232     qDebug() << "Reading XML";
233 torben 529
234 torben 527
235 torben 537 bool res = xmlHandler.readXml( xmlUrl );
236 torben 535
237 torben 528 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 torben 524 }
249 torben 509 }
250 torben 528
251     void MainView::switchScreens()
252     {
253 torben 537 if (video->isVisible() && video->isPlaying() ) {
254     return; //wait until current clip has finished
255     }
256 torben 528
257     if (lastScreenSwitch.isNull() || lastScreenSwitch.elapsed() > (currentItem.runtime*1000)) {
258 torben 529 QTime now = QTime::currentTime();
259 torben 528 if (lastScreenSwitch.isNull())
260 torben 529 currentItemIdx = -1;
261    
262     bool found = false;
263     int tries = 0;
264    
265 torben 534 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 torben 529
271 torben 638 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 torben 534 found = true;
276 torben 638 }
277 torben 534 }
278 torben 529 }
279    
280     if (found) {
281 torben 867 switch(currentItem.module) {
282     case ModuleImage:
283 torben 529 ensureVisible(picture);
284     picture->loadFromUrl( currentItem.url );
285 torben 867 break;
286     case ModuleWeb:
287 torben 537 ensureVisible(web);
288     web->load(currentItem.url);
289 torben 867 break;
290     case ModuleVideo:
291 torben 537 ensureVisible(video);
292     video->loadUrl(currentItem.url);
293 torben 867 break;
294     case ModuleSvg:
295     ensureVisible(svg);
296     svg->load(currentItem.url);
297     break;
298     default:
299 torben 638 // ModuleUnknown - what should we do??
300 torben 867 break;
301 torben 537 }
302 torben 529
303     } else {
304 torben 542 errorInfoScreen("Der er ingen information at vise");
305 torben 885 currentItem = ScreenItem();
306     currentItem.runtime = 60; //switch screens again in 1 minute
307 torben 529 }
308 torben 528 lastScreenSwitch = QTime::currentTime();
309 torben 529
310 torben 528 }
311     }
312 torben 529
313 torben 542 void MainView::errorInfoScreen(QString msg)
314 torben 537 {
315 torben 891 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 torben 537 }
322    
323 torben 529 void MainView::ensureVisible(QWidget* widget)
324     {
325 torben 870 layout->setCurrentWidget(widget);
326 torben 529 }

  ViewVC Help
Powered by ViewVC 1.1.20