645883680db59b492bf85c0092ae6ab977dbb8e0
[jspeed] / src / themescreen.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 <QtGui/QWidget>
20 #include <QtGui/QGraphicsItem>
21 #include <QtCore/QString>
22 #include <QtCore/QDebug>
23 #include <QtXml/QDomNode>
24 #include <QtXml/QDomNodeList>
25 #include "themescreen.h"
26 #include "graphicselement.h"
27 #include "reader.h"
28 #include "graphicsscene.h"
29
30 ThemeScreen::ThemeScreen(QWidget* parent): GraphicsScreen(parent)
31 {
32 }
33
34 bool ThemeScreen::load(QDomNode const& data, Reader* reader)
35 {
36     QDomNodeList childNodes = data.childNodes();
37
38     if(childNodes.size() == 0)
39     {
40         error_ = "No elements specified for " + data.nodeName();
41         return false;
42     }
43
44     for(int i = 0; i < childNodes.size(); i++)
45     {
46         GraphicsElement* element = GraphicsElement::getElement(childNodes.at(i).nodeName(), reader);
47
48         if(element == 0)
49         {
50             qDebug() << "Warning: invalid element: " << childNodes.at(i).nodeName();
51             continue;
52         }
53
54         elements_.push_back(element);
55
56         QDomNodeList options = childNodes.at(i).childNodes();
57
58         for(int i = 0; i < options.size(); i++)
59         {
60             if(!element->setAttribute(options.at(i).nodeName(), options.at(i).toElement().text()))
61             {
62                 qDebug() << "Warning: invalid option: " << options.at(i).nodeName();
63             }
64         }
65
66         element->addToScene(getScene());
67
68     }
69
70     return true;
71 }
72
73 void ThemeScreen::removeElements()
74 {
75     for(int i = 0; i < elements_.size(); i++)
76     {
77         QGraphicsItem* item = elements_.at(i)->getElement();
78
79         if(item)
80         {
81             getScene()->removeItem(item);
82             delete item;
83         }
84
85         delete elements_.at(i);
86     }
87
88     elements_.clear();
89 }
90
91 void ThemeScreen::update()
92 {
93     for(int i = 0; i < elements_.size(); i++)
94     {
95         elements_.at(i)->update();
96     }
97 }