297c65beaf67e137e0a61b2c410f80e108b40bdc
[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)
48     {
49         loadedScreens_.insert(orientation);
50
51         if(loadedScreens_.size() == 1)
52         {
53             currentOrientation_ = orientation;
54             setCurrentWidget(screens_[orientation]);
55         }
56     }
57
58     return ret;
59 }
60
61 bool WidgetScreen::load(QDomNode const& data, Reader* reader)
62 {
63     return load(LANDSCAPE, data, reader) && load(PORTRAIT, data, reader);
64 }
65
66 void WidgetScreen::setColor(Orientation orientation, QString const& color)
67 {
68     if(screens_.find(orientation) != screens_.end())
69     {
70         screens_[orientation]->setColor(color);
71     }
72 }
73
74 void WidgetScreen::setColor(QString const& color)
75 {
76     setColor(LANDSCAPE, color);
77     setColor(PORTRAIT, color);
78 }
79
80 void WidgetScreen::addScreen(ThemeScreen* screen, Orientation orientation)
81 {
82     if(screens_.find(orientation) != screens_.end())
83     {
84         removeWidget(screens_[orientation]);
85         delete screens_[orientation];
86         screens_.remove(orientation);
87     }
88
89     screens_[orientation] = screen;
90     addWidget(screen);
91     connect(screen, SIGNAL(minimizePressed()), this, SIGNAL(minimizePressed()));
92     connect(screen, SIGNAL(settingsPressed()), this, SIGNAL(settingsPressed()));
93     connect(screen, SIGNAL(closePressed()), this, SIGNAL(closePressed()));
94     connect(screen, SIGNAL(clicked()), this, SIGNAL(clicked()));
95 }
96
97 bool WidgetScreen::orientationEnabled(Orientation orientation) const
98 {
99     return screens_.find(orientation) != screens_.end();
100 }
101
102 bool WidgetScreen::orientationLoaded(Orientation orientation) const
103 {
104     return loadedScreens_.find(orientation) != loadedScreens_.end();
105 }
106
107 void WidgetScreen::reArrange()
108 {
109     QRect rect = QApplication::desktop()->screenGeometry();
110
111     Orientation o = LANDSCAPE;
112
113     if(rect.height() > rect.width())
114     {
115         o = PORTRAIT;
116     }
117
118     if(o != currentOrientation_)
119     {
120         if(screens_.find(o) != screens_.end())
121         {
122             currentOrientation_ = o;
123             setCurrentWidget(screens_[o]);
124             screens_[o]->reArrange();
125
126             if(o == LANDSCAPE)
127             {
128                 screens_[o]->forceRepaint();
129             }
130         }
131     }
132 }
133
134 void WidgetScreen::flip()
135 {
136     for(QMap<Orientation, ThemeScreen*>::iterator it = screens_.begin();
137         it != screens_.end(); it++)
138     {
139         it.value()->flip();
140     }
141 }
142
143 void WidgetScreen::clear()
144 {
145     for(QMap<Orientation, ThemeScreen*>::iterator it = screens_.begin();
146     it != screens_.end(); it++)
147     {
148         removeWidget(it.value());
149         delete it.value();
150     }
151
152     screens_.clear();
153     loadedScreens_.clear();
154 }
155
156 void WidgetScreen::removeUnloaded()
157 {
158     for(QMap<Orientation, ThemeScreen*>::iterator it = screens_.begin();
159     it != screens_.end(); it++)
160     {
161         if(loadedScreens_.find(it.key()) == loadedScreens_.end())
162         {
163             removeWidget(it.value());
164             delete it.value();
165             screens_.remove(it.key());
166         }
167     }
168 }