Theme download link fixed in web page.
[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             screens_[o]->reArrange();
124             setCurrentWidget(screens_[o]);
125             screens_[o]->forceRepaint();
126         }
127     }
128 }
129
130 void WidgetScreen::flip()
131 {
132     for(QMap<Orientation, ThemeScreen*>::iterator it = screens_.begin();
133         it != screens_.end(); it++)
134     {
135         it.value()->flip();
136     }
137 }
138
139 void WidgetScreen::clear()
140 {
141     for(QMap<Orientation, ThemeScreen*>::iterator it = screens_.begin();
142     it != screens_.end(); it++)
143     {
144         removeWidget(it.value());
145         delete it.value();
146     }
147
148     screens_.clear();
149     loadedScreens_.clear();
150 }
151
152 void WidgetScreen::removeUnloaded()
153 {
154     for(QMap<Orientation, ThemeScreen*>::iterator it = screens_.begin();
155     it != screens_.end(); it++)
156     {
157         if(loadedScreens_.find(it.key()) == loadedScreens_.end())
158         {
159             removeWidget(it.value());
160             delete it.value();
161             screens_.remove(it.key());
162         }
163     }
164 }