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