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

Annotation of /infoscreen/mainview.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 876 - (hide annotations) (download)
Tue Jun 22 21:01:37 2010 UTC (13 years, 10 months ago) by torben
Original Path: infoscreen/MainView.cpp
File size: 8196 byte(s)
... and move screenmanager on/off times to config file
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 512 #include "MyWebView.h"
14 torben 515 #include "clientsiderender.h"
15 torben 524 #include "pictureview.h"
16 torben 527 #include "videoview.h"
17 torben 511
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     }
33 torben 535
34 torben 638 if (currentMode == ModeSimpleWeb){
35     qDebug() << "Starting plain browser mode";
36 torben 535 }
37    
38 torben 638 if (currentMode == ModeLocal ) {
39     qDebug() << "Starting local mode";
40     qDebug() << "path" << path;
41 torben 535
42 torben 638 readLocalFiles();
43     }
44 torben 535
45 torben 534 this->resize(400,400);
46     this->setWindowState( Qt::WindowFullScreen );
47     this->grabKeyboard();
48 torben 516
49 torben 534 qApp->setOverrideCursor( QCursor( Qt::BlankCursor) );
50 torben 510
51 torben 534 render = new ClientSideRender(this);
52 torben 524
53 torben 534 web = new MyWebView(this);
54 torben 511
55 torben 534 picture = new PictureView(this);
56 torben 517
57 torben 534 video = new VideoView(this);
58 torben 517
59 torben 867 svg = new QSvgWidget(this);
60    
61 torben 870 layout = new QStackedLayout();
62     layout->addWidget(web);
63     layout->addWidget(render);
64     layout->addWidget(picture);
65     layout->addWidget(video);
66     layout->addWidget(svg);
67 torben 534 layout->setContentsMargins(0,0,0,0);
68     setLayout(layout);
69 torben 510
70 torben 872 if (currentMode == ModeSimpleWeb) {
71     web->setVisible(true);
72     web->start(url,screenid);
73     }
74 torben 528
75 torben 875 if (enableScreenManager) {
76 torben 876 screenManager = new ScreenManager(screenManagerOn, screenManagerOff);
77 torben 875 } else {
78     qDebug() << "ScreenManager is disabled";
79     }
80 torben 873
81 torben 875
82 torben 873 qDebug() << "Starting timer...";
83     timer = new QTimer(this);
84     connect(timer, SIGNAL(timeout() ), this, SLOT(onTimer() ));
85     timer->start(100);
86 torben 537 }
87 torben 535
88 torben 638 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 torben 876 enableScreenManager = settings->value("enablescreenmanager").toBool();
108     if (enableScreenManager) {
109     screenManagerOn = settings->value("screenmanager_on").toTime();
110     screenManagerOff = settings->value("screenmanager_off").toTime();
111     }
112 torben 638
113 torben 875
114 torben 638 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 torben 537 void MainView::closeEvent ( QCloseEvent * event )
135     {
136     Q_UNUSED(event);
137     exit(0); //force application shutdown
138 torben 509 }
139    
140 torben 532 void MainView::keyPressEvent ( QKeyEvent* event )
141     {
142 torben 533 int key = event->key();
143     if (key == ' ' || key == Qt::Key_Return || key == Qt::Key_Enter) {
144 torben 537 close();
145 torben 533 }
146 torben 532 }
147 torben 509
148     void MainView::onTimer()
149     {
150 torben 875 if (enableScreenManager == true) {
151     screenManager->timerTick();
152     }
153 torben 524
154 torben 638 if (currentMode == ModeXml) {
155     readXml();
156     }
157 torben 527
158 torben 873 if (currentMode == ModeXml || currentMode == ModeLocal) {
159     switchScreens();
160     }
161 torben 528 }
162 torben 527
163 torben 524
164 torben 638 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 torben 708 if (ext == "avi" || ext == "mpg" || ext == "mpeg") {
183 torben 638 item.module = ModuleVideo;
184     item.runtime = 1;
185     }
186    
187 torben 708 if (ext == "jpg" || ext == "jpeg" || ext == "png" ) {
188 torben 638 item.module = ModuleImage;
189     item.runtime = 10;
190     }
191    
192 torben 867 if (ext == "svg") {
193     item.module = ModuleSvg;
194     item.runtime = 10;
195     }
196 torben 869
197     if (ext == "htm" || ext == "html") {
198     item.module = ModuleWeb;
199     item.runtime = 10;
200     }
201 torben 867
202 torben 638 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 torben 528 bool MainView::readXml()
213     {
214     const int TIMEOUT = 30*60*1000; // 30 minutter
215     if ( lastXml.isNull() || lastXml.elapsed() > TIMEOUT) {
216 torben 529
217 torben 527
218 torben 537 bool res = xmlHandler.readXml( xmlUrl );
219 torben 535
220 torben 528 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 torben 524 }
232 torben 509 }
233 torben 528
234     void MainView::switchScreens()
235     {
236 torben 537 if (video->isVisible() && video->isPlaying() ) {
237     return; //wait until current clip has finished
238     }
239 torben 528
240     if (lastScreenSwitch.isNull() || lastScreenSwitch.elapsed() > (currentItem.runtime*1000)) {
241    
242 torben 529 QTime now = QTime::currentTime();
243 torben 528 if (lastScreenSwitch.isNull())
244 torben 529 currentItemIdx = -1;
245    
246     bool found = false;
247     int tries = 0;
248    
249 torben 534 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 torben 529
255 torben 638 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 torben 534 found = true;
260 torben 638 }
261 torben 534 }
262 torben 529 }
263    
264     if (found) {
265 torben 867 switch(currentItem.module) {
266     case ModuleImage:
267 torben 529 ensureVisible(picture);
268     picture->loadFromUrl( currentItem.url );
269 torben 867 break;
270     case ModuleWeb:
271 torben 537 ensureVisible(web);
272     web->load(currentItem.url);
273 torben 867 break;
274     case ModuleVideo:
275 torben 537 ensureVisible(video);
276     video->loadUrl(currentItem.url);
277 torben 867 break;
278     case ModuleSvg:
279     ensureVisible(svg);
280     svg->load(currentItem.url);
281     break;
282     default:
283 torben 638 // ModuleUnknown - what should we do??
284 torben 867 break;
285 torben 537 }
286 torben 529
287     } else {
288 torben 542 errorInfoScreen("Der er ingen information at vise");
289 torben 529 }
290 torben 528 lastScreenSwitch = QTime::currentTime();
291 torben 529
292 torben 528 }
293     }
294 torben 529
295 torben 542 void MainView::errorInfoScreen(QString msg)
296 torben 537 {
297     ensureVisible(web);
298     web->setHtml("<html>\
299     <body text='#505050' bgcolor='#000000'>\
300 torben 542 <table width='100%' height='100%'><tr><td align='center' valign='middel'><h1>" + msg+ "</h1></td></tr></table>\
301 torben 537 </body></html>");
302     }
303    
304 torben 529 void MainView::ensureVisible(QWidget* widget)
305     {
306 torben 870 layout->setCurrentWidget(widget);
307 torben 529 }

  ViewVC Help
Powered by ViewVC 1.1.20