Added zpos attribute to all elements. Changed architecture to allow detailscreen...
[jspeed] / src / widgetscreen.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QString>
20 #include <QtCore/QDebug>
21 #include <QtGui/QApplication>
22 #include <QtGui/QDesktopWidget>
23 #include <QtXml/QDomNode>
24 #include "widgetscreen.h"
25 #include "themescreen.h"
26
27 WidgetScreen::WidgetScreen(QWidget* parent): QStackedWidget(parent), AbstractScreen(),
28 currentOrientation_(LANDSCAPE)
29 {
30 }
31
32 WidgetScreen::~WidgetScreen()
33 {
34     clear();
35 }
36
37 bool WidgetScreen::load(Orientation orientation, QDomNode const& data, Reader* reader)
38 {
39     if(screens_.find(orientation) == screens_.end())
40     {
41         qDebug() << "Orientation " << orientation << " not set";
42         return false;
43     }
44
45     bool ret = screens_[orientation]->load(data, reader);
46
47     if(ret && screens_.size() == 1)
48     {
49         currentOrientation_ = orientation;
50     }
51
52     if(ret)
53     {
54         loadedScreens_.insert(orientation);
55     }
56
57     return ret;
58 }
59
60 void WidgetScreen::addScreen(ThemeScreen* screen, Orientation orientation)
61 {
62     if(screens_.find(orientation) != screens_.end())
63     {
64         removeWidget(screens_[orientation]);
65         delete screens_[orientation];
66         screens_.remove(orientation);
67     }
68
69     screens_[orientation] = screen;
70     addWidget(screen);
71     connect(screen, SIGNAL(minimizePressed()), this, SIGNAL(minimizePressed()));
72     connect(screen, SIGNAL(settingsPressed()), this, SIGNAL(settingsPressed()));
73     connect(screen, SIGNAL(closePressed()), this, SIGNAL(closePressed()));
74     connect(screen, SIGNAL(clicked()), this, SIGNAL(clicked()));
75 }
76
77 bool WidgetScreen::orientationEnabled(Orientation orientation) const
78 {
79     return screens_.find(orientation) != screens_.end();
80 }
81
82 bool WidgetScreen::orientationLoaded(Orientation orientation) const
83 {
84     return loadedScreens_.find(orientation) != loadedScreens_.end();
85 }
86
87 void WidgetScreen::reArrange()
88 {
89     QRect rect = QApplication::desktop()->screenGeometry();
90
91     Orientation o = LANDSCAPE;
92
93     if(rect.height() > rect.width())
94     {
95         o = PORTRAIT;
96     }
97
98     if(o != currentOrientation_)
99     {
100         if(screens_.find(o) != screens_.end())
101         {
102             setCurrentWidget(screens_[o]);
103             screens_[o]->reArrange();
104             screens_[o]->forceRepaint();
105             currentOrientation_ = o;
106         }
107     }
108 }
109
110 void WidgetScreen::flip()
111 {
112     for(QMap<Orientation, ThemeScreen*>::iterator it = screens_.begin();
113         it != screens_.end(); it++)
114     {
115         it.value()->flip();
116     }
117 }
118
119 void WidgetScreen::setColor(QString const& color)
120 {
121     Q_UNUSED(color);
122 }
123
124 void WidgetScreen::clear()
125 {
126     for(QMap<Orientation, ThemeScreen*>::iterator it = screens_.begin();
127     it != screens_.end(); it++)
128     {
129         removeWidget(it.value());
130         delete it.value();
131     }
132
133     screens_.clear();
134     loadedScreens_.clear();
135 }
136
137 void WidgetScreen::removeUnloaded()
138 {
139     for(QMap<Orientation, ThemeScreen*>::iterator it = screens_.begin();
140     it != screens_.end(); it++)
141     {
142         if(loadedScreens_.find(it.key()) == loadedScreens_.end())
143         {
144             removeWidget(it.value());
145             delete it.value();
146             screens_.remove(it.key());
147         }
148     }
149 }