3a8dcc5d397d7bf6f8106799324907a893922827
[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      {"zpos", true},
33      {"width", true},
34      {"height", true},
35      {"color", false}
36     };
37 }
38
39 Rectangle::Rectangle(Reader* reader): GraphicsElement(reader),
40 x_(0), y_(0), width_(0), height_(0)
41 {
42     element_ = new QGraphicsRectItem();
43 }
44
45 bool Rectangle::setAttribute(QString const& name, QString const& value)
46 {
47     int intVal = 0;
48     int attrId = -1;
49
50     if((attrId = getAttribute(name, value, ATTRIBUTES, ATTRIBUTE_COUNT, intVal)) != -1)
51     {
52         Attribute attr = static_cast<Attribute>(attrId);
53
54         switch(attr)
55         {
56         case XPOS:
57             x_ = intVal;
58             break;
59         case YPOS:
60             y_ = intVal;
61             break;
62         case ZPOS:
63             element_->setZValue(intVal);
64             break;
65         case WIDTH:
66             width_ = intVal;
67             break;
68         case HEIGHT:
69             height_ = intVal;
70             break;
71         case COLOR:
72             element_->setBrush(QBrush(QColor(value)));
73             break;
74
75         default:
76             qDebug() << "Unknown attribute: " << attr;
77             return false;
78         }
79
80         return true;
81     }
82     else
83     {
84         return false;
85     }
86 }
87
88 void Rectangle::addToScene(GraphicsScene* scene)
89 {
90     int width = width_;
91
92     int maxSize = scene->width();
93
94     if(scene->height() > maxSize)
95     {
96         maxSize = scene->height();
97     }
98
99     if(width <= 0)
100     {
101         width = maxSize;
102     }
103
104     int height = height_;
105
106     if(height <= 0)
107     {
108         height = maxSize;
109     }
110
111     element_->setRect(x_, y_, width, height);
112     scene->addItem(element_);
113 }
114
115 void Rectangle::update()
116 {
117 }
118
119 QGraphicsItem* Rectangle::getElement() const
120 {
121     return element_;
122 }