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