2 * This file is part of jSpeed.
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.
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.
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/>.
19 #include <QtCore/QDir>
20 #include <QtCore/QString>
21 #include <QtCore/QDebug>
22 #include <QtCore/QTimer>
23 #include <QtGui/QApplication>
24 #include <QtGui/QDesktopWidget>
25 #include <QtGui/QStackedWidget>
26 #include <QtXml/QDomNode>
27 #include <QtXml/QDomDocument>
28 #include <QtXml/QDomAttr>
29 #include <QMaemo5InformationBox>
32 #include "zipreader.h"
33 #include "filereader.h"
34 #include "themescreen.h"
35 #include "detailscreen.h"
40 QString const RESOURCE_DIR = ":/resources/themes/";
41 QString const THEME_SUFFIX = ".jspeed";
44 Theme::Theme(DetailScreen* detailScreen, QWidget* parent): WidgetScreen(parent),
45 portraitId_(-1), landscapeId_(-1), reader_(0),
46 portrait_(0), landscape_(0), detailScreen_(detailScreen), portraitMode_(false)
58 removeWidget(landscape_);
66 removeWidget(portrait_);
72 QString theme = Settings::instance().value("theme", "default").toString();
74 QString themeDir = getThemeDir();
76 if(QFile::exists(themeDir + theme + THEME_SUFFIX))
78 reader_ = new ZipReader(themeDir + theme + THEME_SUFFIX);
86 QMaemo5InformationBox::information(this,
87 tr("Unable to load theme: %1.").arg(error_),
88 QMaemo5InformationBox::NoTimeout);
93 Settings::instance().setValue("theme", theme);
95 if(QFile::exists(RESOURCE_DIR + theme))
102 reader_ = new FileReader(RESOURCE_DIR + theme);
106 error_ = "No themes found";
113 Q_ASSERT(reader_ != 0);
117 error_ = reader_->errorString();
123 if(!reader_->readFile("theme.xml", xmlData))
125 error_ = "Unable to find <b>theme.xml</b> from theme file";
134 if(!doc.setContent(xmlData, false, &msg, &line, &column))
136 error_ = "Invalid xml file, " + msg + " (line "+line+", column "+column+")";
140 QDomNodeList detailConfigs = doc.elementsByTagName("detailscreen");
142 if(detailConfigs.size() > 1)
144 error_ = "Multiple <detailscreen> tags specified";
148 if(detailConfigs.size() == 1)
150 detailScreen_->removeElements();
152 QDomNode color = detailConfigs.at(0).attributes().namedItem("color");
156 detailScreen_->setColor(color.toAttr().value());
159 detailScreen_->load(detailConfigs.at(0), reader_);
162 QDomNodeList orientations = doc.elementsByTagName("orientation");
164 if(orientations.isEmpty())
166 error_ = "No <orientation> tags found";
172 for(int i = 0; i < orientations.size(); i++)
174 QDomNode data = orientations.at(i);
175 QString type = data.attributes().namedItem("name").toAttr().value();
177 if(type == "landscape")
181 error_ = "More than one <orientation name=\"landscape\"> specified";
185 landscape_ = new ThemeScreen();
186 ok = ok && landscape_->load(data, reader_);
189 else if(type == "portrait")
193 error_ = "More than one <orientation name=\"portrait\"> specified";
197 portrait_ = new ThemeScreen();
198 ok = ok && portrait_->load(data, reader_);
202 error_ = "Invalid orientation: " + type;
211 if(!portrait_ && !landscape_)
213 error_ = "No valid orientation tags found";
221 landscapeId_ = addWidget(landscape_);
222 connectSignals(landscape_);
227 portraitId_ = addWidget(portrait_);
228 connectSignals(portrait_);
231 if(landscape_ && portrait_)
233 QRect rect = QApplication::desktop()->screenGeometry();
235 if(rect.height() > rect.width())
237 setCurrentIndex(portraitId_);
238 portraitMode_ = true;
242 setCurrentIndex(landscapeId_);
243 portraitMode_ = false;
258 QString Theme::getThemeDir()
260 return Settings::getDir() + "themes" + QDir::separator();
263 QString const& Theme::getThemeSuffix()
268 QString const& Theme::error() const
273 bool Theme::portraitEnabled() const
275 return portrait_ != 0;
278 bool Theme::landscapeEnabled() const
280 return landscape_ != 0;
283 void Theme::reArrange()
285 QRect rect = QApplication::desktop()->screenGeometry();
287 bool portrait = rect.height() > rect.width();
289 if(portrait != portraitMode_)
291 if((portrait && portraitId_ == -1) ||
292 (!portrait && landscapeId_ == -1))
297 portraitMode_ = portrait;
301 setCurrentIndex(portraitId_);
302 portrait_->reArrange();
306 setCurrentIndex(landscapeId_);
307 landscape_->forceRepaint();
308 //QTimer::singleShot(5000, landscape_, SLOT(forceRepaint()));
326 void Theme::connectSignals(ThemeScreen* screen)
328 connect(screen, SIGNAL(minimizePressed()), this, SIGNAL(minimizePressed()));
329 connect(screen, SIGNAL(settingsPressed()), this, SIGNAL(settingsPressed()));
330 connect(screen, SIGNAL(closePressed()), this, SIGNAL(closePressed()));
331 connect(screen, SIGNAL(clicked()), this, SIGNAL(clicked()));