Added some effects to graphics elements and changed default theme a bit.
[jspeed] / src / graphicselement.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/QByteArray>
21 #include <QtCore/QDebug>
22 #include <QtGui/QGraphicsScene>
23 #include <QtGui/QFont>
24 #include <QtGui/QFontDatabase>
25 #include <QtGui/QGraphicsEffect>
26 #include <QtGui/QGraphicsDropShadowEffect>
27 #include <QtGui/QGraphicsItem>
28 #include "graphicselement.h"
29 #include "reader.h"
30 #include "textelement.h"
31 #include "imageelement.h"
32 #include "rectangle.h"
33 #include "pointer.h"
34 #include "settings.h"
35 #include "effect.h"
36
37 GraphicsElement::GraphicsElement(Reader* reader): reader_(reader), error_(""), effect_(0)
38 {
39 }
40
41 GraphicsElement* GraphicsElement::getElement(QString const& name, Reader* reader)
42 {
43     if(name == "image")
44     {
45         return new ImageElement(reader);
46     }
47
48     if(name == "text")
49     {
50         return new TextElement(reader);
51     }
52
53     if(name == "rectangle")
54     {
55         return new Rectangle(reader);
56     }
57
58     if(name == "pointer")
59     {
60         bool animate = Settings::instance().value("animate_pointer", true).toBool();
61         return new Pointer(reader, animate);
62     }
63
64     qDebug() << "Element not found: " << name;
65     return 0;
66 }
67
68 QString const& GraphicsElement::getError() const
69 {
70     return error_;
71 }
72
73 void GraphicsElement::setError(QString const& error)
74 {
75     error_ = error;
76 }
77
78 bool GraphicsElement::readFile(QString const& filename, QByteArray& data)
79 {
80     if(!reader_->readFile(filename, data))
81     {
82         setError(reader_->errorString());
83         return false;
84     }
85
86     return true;
87 }
88
89 bool GraphicsElement::getFont(QString const& name, QFont& font)
90 {
91     QByteArray data;
92
93     QMap<QString, QString>::const_iterator it = loadedFonts_.find(name);
94
95     if(it != loadedFonts_.end())
96     {
97         font = QFont(it.value());
98         return true;
99     }
100
101     if(!reader_->fileExists(name))
102     {
103         font = QFont(name);
104         return true;
105     }
106
107     if(!readFile(name, data))
108     {
109         return false;
110     }
111
112     int id = QFontDatabase::addApplicationFontFromData(data);
113
114     if(id == -1)
115     {
116         setError("Unable to load font: " + name);
117         loadedFonts_[name] = "";
118         return false;
119     }
120
121     QStringList families = QFontDatabase::applicationFontFamilies(id);
122
123     if(families.size() == 0)
124     {
125         setError("No fonts found in " + name);
126         return false;
127     }
128
129     loadedFonts_[name] = families.at(0);
130     font = QFont(families.at(0));
131
132     return true;
133 }
134
135 int GraphicsElement::getAttribute(QString const& name,
136                                   QString const& value,
137                                   const GraphicsElement::AttributeDetails details[],
138                                   int count,
139                                   int& intValue)
140 {
141     QString lower = name.toLower();
142
143     for(int i = 0; i < count; i++)
144     {
145         if(details[i].name == lower)
146         {
147             if(!details[i].isInt)
148             {
149                 return i;
150             }
151             else
152             {
153                 bool ok = true;
154                 int tmp = value.toInt(&ok);
155
156                 if(ok)
157                 {
158                     intValue = tmp;
159                     return i;
160                 }
161                 else
162                 {
163                     setError("Value for " + name + " is not integer ("+value+")");
164                     return -1;
165                 }
166             }
167         }
168     }
169
170     setError("Unknown attribute: " + name);
171     return -1;
172 }
173
174 bool GraphicsElement::setEffect(QString const& name)
175 {
176    /* QGraphicsDropShadowEffect* eff = new QGraphicsDropShadowEffect;
177     eff->setOffset(1);
178     eff->setBlurRadius(3);
179
180     getElement()->setGraphicsEffect(eff);
181
182     return true;*/
183
184     effect_ = Effect::getEffect(name);
185
186     if(!effect_)
187     {
188         return false;
189     }
190
191     return true;
192 }
193
194 bool GraphicsElement::setEffectAttribute(QString const& name, QString const& value)
195 {
196     //return true;
197     if(!effect_)
198     {
199         qDebug() << "Effect not set";
200         return false;
201     }
202
203     return effect_->setAttribute(name, value);
204 }
205
206 void GraphicsElement::applyEffect()
207 {
208     //return;
209     effect_->apply(getElement());
210 }