5448748f8ea911299cb053efbff98dc7a28dc2af
[jspeed] / src / rectangle.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/QGraphicsRectItem>
20 #include <QtCore/QString>
21 #include <QtCore/QDebug>
22 #include "rectangle.h"
23 #include "reader.h"
24 #include "graphicsscene.h"
25
26 namespace
27 {
28     const GraphicsElement::AttributeDetails ATTRIBUTES[Rectangle::ATTRIBUTE_COUNT] =
29     {
30      {"xpos", true},
31      {"ypos", true},
32      {"width", true},
33      {"height", true},
34      {"color", false}
35     };
36 }
37
38 Rectangle::Rectangle(Reader* reader): GraphicsElement(reader),
39 x_(0), y_(0), width_(0), height_(0)
40 {
41     element_ = new QGraphicsRectItem();
42 }
43
44 bool Rectangle::setAttribute(QString const& name, QString const& value)
45 {
46     int intVal = 0;
47     int attrId = -1;
48
49     if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
50     {
51         Attribute attr = static_cast<Attribute>(attrId);
52
53         switch(attr)
54         {
55         case XPOS:
56             x_ = intVal;
57             break;
58         case YPOS:
59             y_ = intVal;
60             break;
61         case WIDTH:
62             width_ = intVal;
63             break;
64         case HEIGHT:
65             height_ = intVal;
66             break;
67         case COLOR:
68             element_->setBrush(QBrush(QColor(value)));
69             break;
70
71         default:
72             qDebug() << "Unknown attribute: " << attr;
73             return false;
74         }
75
76         return true;
77     }
78     else
79     {
80         return false;
81     }
82 }
83
84 void Rectangle::addToScene(GraphicsScene* scene)
85 {
86     int width = width_;
87
88     int maxSize = scene->width();
89
90     if(scene->height() > maxSize)
91     {
92         maxSize = scene->height();
93     }
94
95     if(width <= 0)
96     {
97         width = maxSize;
98     }
99
100     int height = height_;
101
102     if(height <= 0)
103     {
104         height = maxSize;
105     }
106
107     element_->setRect(x_, y_, width, height);
108     scene->addItem(element_);
109 }
110
111 void Rectangle::update()
112 {
113 }