Developer Documentation

sceneTools.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43
44 //== INCLUDES =================================================================
45 #include <QWidget>
46 #include <QVBoxLayout>
47 #include <QPainter>
48 #include <QGraphicsSceneMouseEvent>
49 #include <QGraphicsGridLayout>
50 #include <QGraphicsLinearLayout>
51
52 #include "sceneTools.hh"
53 #include "sceneElement.hh"
54 #include "graphicsScene.hh"
55 #include "arrow.hh"
56 #include "zoomButton.hh"
57 #include "trash.hh"
58 #include "functionDisplay.hh"
59
60 //== NAMESPACES ===============================================================
61 namespace VSI {
62
63 //=============================================================================
64 //
65 // CLASS VSI::SceneTools - IMPLEMENTATION
66 //
67 //=============================================================================
68
70 SceneTools::SceneTools (GraphicsScene *_scene) :
71 scene_ (_scene)
72 {
73 QRectF rect = scene_->sceneRect();
74
75 arrows_[0] = new Arrow (_scene, this, Arrow::North);
76 arrows_[1] = new Arrow (_scene, this, Arrow::South);
77 arrows_[2] = new Arrow (_scene, this, Arrow::East);
78 arrows_[3] = new Arrow (_scene, this, Arrow::West);
79 arrows_[4] = new Arrow (_scene, this, Arrow::Center);
80
81 QGraphicsGridLayout *aLayout = new QGraphicsGridLayout;
82 aLayout->addItem (arrows_[0], 0, 1, Qt::AlignCenter);
83 aLayout->addItem (arrows_[1], 2, 1, Qt::AlignCenter);
84 aLayout->addItem (arrows_[2], 1, 2, Qt::AlignCenter);
85 aLayout->addItem (arrows_[3], 1, 0, Qt::AlignCenter);
86 aLayout->addItem (arrows_[4], 1, 1, Qt::AlignCenter);
87
88 trash_ = new Trash (_scene, this);
89
90 QGraphicsLinearLayout *zLayout = new QGraphicsLinearLayout (Qt::Horizontal);
91 zoom_[0] = new ZoomButton (_scene, this, ZoomButton::In);
92 zoom_[1] = new ZoomButton (_scene, this, ZoomButton::Out);
93 zLayout->addItem (zoom_[0]);
94 zLayout->addItem (zoom_[1]);
95
96 fDisplay_ = new FunctionDisplay (scene_);
97 scene_->addItem (fDisplay_);
98
99 setGeometry (rect);
100 connect (scene_, SIGNAL (sceneRectChanged (const QRectF &)),
101 this, SLOT (sceneRectChanged (const QRectF &)));
102
103 QGraphicsGridLayout *mLayout = new QGraphicsGridLayout;
104
105 mLayout->addItem (aLayout, 0, 2, Qt::AlignTop | Qt::AlignRight);
106 mLayout->addItem (zLayout, 2, 0, Qt::AlignBottom | Qt::AlignLeft);
107 mLayout->addItem (trash_, 2, 2, Qt::AlignBottom | Qt::AlignRight);
108 mLayout->setColumnStretchFactor (1, 1);
109 mLayout->setRowStretchFactor (1, 1);
110 mLayout->setContentsMargins (7, 7, 7, 7);
111
112 setLayout (mLayout);
113 }
114
115 //------------------------------------------------------------------------------
116
118 SceneTools::~SceneTools ()
119 {
120 }
121
122 //------------------------------------------------------------------------------
123
124 // handle scene size changes
125 void SceneTools::sceneRectChanged (const QRectF &_rect)
126 {
127 setGeometry (_rect);
128
129 fDisplay_->setPos(_rect.topLeft ());
130 updateArrows ();
131 }
132
133 //------------------------------------------------------------------------------
134
135 // update arrow depending on visible scene rect
136 void SceneTools::updateArrows ()
137 {
138 QRectF bb = scene_->elementsBoundingRect ();
139 QRectF rect = scene_->sceneRect();
140
141 if (bb.top () < rect.top ())
142 arrows_[0]->setHighlight (true);
143 else
144 arrows_[0]->setHighlight (false);
145
146 if (bb.bottom () > rect.bottom ())
147 arrows_[1]->setHighlight (true);
148 else
149 arrows_[1]->setHighlight (false);
150
151 if (bb.right () > rect.right ())
152 arrows_[2]->setHighlight (true);
153 else
154 arrows_[2]->setHighlight (false);
155
156 if (bb.left () < rect.left ())
157 arrows_[3]->setHighlight (true);
158 else
159 arrows_[3]->setHighlight (false);
160
161 if (rect.center () != bb.center ())
162 arrows_[4]->setHighlight (true);
163 else
164 arrows_[4]->setHighlight (false);
165 }
166
167 //------------------------------------------------------------------------------
168
170 void SceneTools::mouseMove (QPointF _pos)
171 {
172 for (unsigned int i = 0; i < 5; i++)
173 {
174 if (arrows_[i]->contains (arrows_[i]->mapFromScene (_pos)))
175 arrows_[i]->activate ();
176 else
177 arrows_[i]->reset ();
178 }
179
180 for (unsigned int i = 0; i < 2; i++)
181 {
182 if (zoom_[i]->contains (zoom_[i]->mapFromScene (_pos)))
183 zoom_[i]->activate (_pos);
184 else
185 zoom_[i]->deactivate ();
186 }
187
188 if (trash_->contains (trash_->mapFromScene (_pos)))
189 trash_->activate ();
190 else
191 trash_->deactivate ();
192 }
193
194 //------------------------------------------------------------------------------
195
197 void SceneTools::mouseRelease (QPointF _pos, QGraphicsItem *_item)
198 {
199 for (unsigned int i = 0; i < 5; i++)
200 {
201 arrows_[i]->reset ();
202 }
203 for (unsigned int i = 0; i < 2; i++)
204 {
205 zoom_[i]->deactivate ();
206 }
207 trash_->deactivate ();
208
209 if (trash_->contains (trash_->mapFromScene (_pos)))
210 {
211 SceneElement *se = dynamic_cast<SceneElement *> (_item);
212 if (se && scene_->removeElement (se))
213 delete se;
214 }
215 }
216
217 //------------------------------------------------------------------------------
218 }
219
VSI::SceneTools::mouseRelease
void mouseRelease(QPointF _pos, QGraphicsItem *_item)
Handles mouse release (will be called by the scene, if the element is dropped)
Definition: sceneTools.cc:197
VSI::ZoomButton::activate
void activate(QPointF _pos)
Activates the timer for zoom with center at _pos (will be called if an element is moved above) ...
Definition: zoomButton.cc:180
VSI::SceneTools::mouseMove
void mouseMove(QPointF _pos)
Handles mouse movement (will be called by the scene, if the mouse with a draged element is moved) ...
Definition: sceneTools.cc:170
VSI::SceneTools::SceneTools
SceneTools(GraphicsScene *_scene)
Constructor.
Definition: sceneTools.cc:70
VSI::Arrow::activate
void activate()
Activates the timer for movement (will be called if an element is moved above)
Definition: arrow.cc:239
VSI::SceneTools::~SceneTools
~SceneTools()
Destructor.
Definition: sceneTools.cc:118
VSI::GraphicsScene::elementsBoundingRect
QRectF elementsBoundingRect()
Bounding rectangle of all scene elements.
Definition: graphicsScene.cc:337
VSI::Trash::activate
void activate()
Makes the trash opaque (will be called of an element is moved above this widget)
Definition: trash.cc:118
VSI::Trash::deactivate
void deactivate()
Makes the trash transparent (will be called of an element is moved away from this widget) ...
Definition: trash.cc:126
VSI::FunctionDisplay
Definition: functionDisplay.hh:65
VSI::ZoomButton
Scene zoom in/out widget.
Definition: zoomButton.hh:60
VSI::GraphicsScene::removeElement
bool removeElement(SceneElement *_element)
Remove the element from the scene.
Definition: graphicsScene.cc:309
VSI::Trash
Definition: trash.hh:63
VSI::SceneElement
Definition: sceneElement.hh:73
VSI::Arrow::setHighlight
void setHighlight(bool _highlight)
Highlights the widget if the scene can be moved in this direction.
Definition: arrow.cc:287
VSI::Arrow
Scene movement widget.
Definition: arrow.hh:62
VSI::Arrow::reset
void reset()
Stop the timer.
Definition: arrow.cc:250
VSI
Definition: baseWidget.cc:72
VSI::ZoomButton::deactivate
void deactivate()
Stop the timer.
Definition: zoomButton.cc:191
VSI::GraphicsScene
Definition: graphicsScene.hh:80