Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 //== INCLUDES =================================================================
51 #include <QWidget>
52 #include <QVBoxLayout>
53 #include <QPainter>
54 #include <QGraphicsSceneMouseEvent>
55 #include <QGraphicsGridLayout>
56 #include <QGraphicsLinearLayout>
57 
58 #include "sceneTools.hh"
59 #include "sceneElement.hh"
60 #include "graphicsScene.hh"
61 #include "arrow.hh"
62 #include "zoomButton.hh"
63 #include "trash.hh"
64 #include "functionDisplay.hh"
65 
66 //== NAMESPACES ===============================================================
67 namespace VSI {
68 
69 //=============================================================================
70 //
71 // CLASS VSI::SceneTools - IMPLEMENTATION
72 //
73 //=============================================================================
74 
77  scene_ (_scene)
78 {
79  QRectF rect = scene_->sceneRect();
80 
81  arrows_[0] = new Arrow (_scene, this, Arrow::North);
82  arrows_[1] = new Arrow (_scene, this, Arrow::South);
83  arrows_[2] = new Arrow (_scene, this, Arrow::East);
84  arrows_[3] = new Arrow (_scene, this, Arrow::West);
85  arrows_[4] = new Arrow (_scene, this, Arrow::Center);
86 
87  QGraphicsGridLayout *aLayout = new QGraphicsGridLayout;
88  aLayout->addItem (arrows_[0], 0, 1, Qt::AlignCenter);
89  aLayout->addItem (arrows_[1], 2, 1, Qt::AlignCenter);
90  aLayout->addItem (arrows_[2], 1, 2, Qt::AlignCenter);
91  aLayout->addItem (arrows_[3], 1, 0, Qt::AlignCenter);
92  aLayout->addItem (arrows_[4], 1, 1, Qt::AlignCenter);
93 
94  trash_ = new Trash (_scene, this);
95 
96  QGraphicsLinearLayout *zLayout = new QGraphicsLinearLayout (Qt::Horizontal);
97  zoom_[0] = new ZoomButton (_scene, this, ZoomButton::In);
98  zoom_[1] = new ZoomButton (_scene, this, ZoomButton::Out);
99  zLayout->addItem (zoom_[0]);
100  zLayout->addItem (zoom_[1]);
101 
102  fDisplay_ = new FunctionDisplay (scene_);
103  scene_->addItem (fDisplay_);
104 
105  setGeometry (rect);
106  connect (scene_, SIGNAL (sceneRectChanged (const QRectF &)),
107  this, SLOT (sceneRectChanged (const QRectF &)));
108 
109  QGraphicsGridLayout *mLayout = new QGraphicsGridLayout;
110 
111  mLayout->addItem (aLayout, 0, 2, Qt::AlignTop | Qt::AlignRight);
112  mLayout->addItem (zLayout, 2, 0, Qt::AlignBottom | Qt::AlignLeft);
113  mLayout->addItem (trash_, 2, 2, Qt::AlignBottom | Qt::AlignRight);
114  mLayout->setColumnStretchFactor (1, 1);
115  mLayout->setRowStretchFactor (1, 1);
116  mLayout->setContentsMargins (7, 7, 7, 7);
117 
118  setLayout (mLayout);
119 }
120 
121 //------------------------------------------------------------------------------
122 
125 {
126 }
127 
128 //------------------------------------------------------------------------------
129 
130 // handle scene size changes
131 void SceneTools::sceneRectChanged (const QRectF &_rect)
132 {
133  setGeometry (_rect);
134 
135  fDisplay_->setPos(_rect.topLeft ());
136  updateArrows ();
137 }
138 
139 //------------------------------------------------------------------------------
140 
141 // update arrow depending on visible scene rect
142 void SceneTools::updateArrows ()
143 {
144  QRectF bb = scene_->elementsBoundingRect ();
145  QRectF rect = scene_->sceneRect();
146 
147  if (bb.top () < rect.top ())
148  arrows_[0]->setHighlight (true);
149  else
150  arrows_[0]->setHighlight (false);
151 
152  if (bb.bottom () > rect.bottom ())
153  arrows_[1]->setHighlight (true);
154  else
155  arrows_[1]->setHighlight (false);
156 
157  if (bb.right () > rect.right ())
158  arrows_[2]->setHighlight (true);
159  else
160  arrows_[2]->setHighlight (false);
161 
162  if (bb.left () < rect.left ())
163  arrows_[3]->setHighlight (true);
164  else
165  arrows_[3]->setHighlight (false);
166 
167  if (rect.center () != bb.center ())
168  arrows_[4]->setHighlight (true);
169  else
170  arrows_[4]->setHighlight (false);
171 }
172 
173 //------------------------------------------------------------------------------
174 
176 void SceneTools::mouseMove (QPointF _pos)
177 {
178  for (unsigned int i = 0; i < 5; i++)
179  {
180  if (arrows_[i]->contains (arrows_[i]->mapFromScene (_pos)))
181  arrows_[i]->activate ();
182  else
183  arrows_[i]->reset ();
184  }
185 
186  for (unsigned int i = 0; i < 2; i++)
187  {
188  if (zoom_[i]->contains (zoom_[i]->mapFromScene (_pos)))
189  zoom_[i]->activate (_pos);
190  else
191  zoom_[i]->deactivate ();
192  }
193 
194  if (trash_->contains (trash_->mapFromScene (_pos)))
195  trash_->activate ();
196  else
197  trash_->deactivate ();
198 }
199 
200 //------------------------------------------------------------------------------
201 
203 void SceneTools::mouseRelease (QPointF _pos, QGraphicsItem *_item)
204 {
205  for (unsigned int i = 0; i < 5; i++)
206  {
207  arrows_[i]->reset ();
208  }
209  for (unsigned int i = 0; i < 2; i++)
210  {
211  zoom_[i]->deactivate ();
212  }
213  trash_->deactivate ();
214 
215  if (trash_->contains (trash_->mapFromScene (_pos)))
216  {
217  SceneElement *se = dynamic_cast<SceneElement *> (_item);
218  if (se && scene_->removeElement (se))
219  delete se;
220  }
221 }
222 
223 //------------------------------------------------------------------------------
224 }
225 
bool removeElement(SceneElement *_element)
Remove the element from the scene.
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:176
QRectF elementsBoundingRect()
Bounding rectangle of all scene elements.
void activate()
Makes the trash opaque (will be called of an element is moved above this widget)
Definition: trash.cc:124
void reset()
Stop the timer.
Definition: arrow.cc:256
void deactivate()
Stop the timer.
Definition: zoomButton.cc:197
void mouseRelease(QPointF _pos, QGraphicsItem *_item)
Handles mouse release (will be called by the scene, if the element is dropped)
Definition: sceneTools.cc:203
Scene movement widget.
Definition: arrow.hh:68
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:186
~SceneTools()
Destructor.
Definition: sceneTools.cc:124
SceneTools(GraphicsScene *_scene)
Constructor.
Definition: sceneTools.cc:76
Scene zoom in/out widget.
Definition: zoomButton.hh:66
void activate()
Activates the timer for movement (will be called if an element is moved above)
Definition: arrow.cc:245
void setHighlight(bool _highlight)
Highlights the widget if the scene can be moved in this direction.
Definition: arrow.cc:293
void deactivate()
Makes the trash transparent (will be called of an element is moved away from this widget) ...
Definition: trash.cc:132