OpenASIP 2.2
Loading...
Searching...
No Matches
EditPart.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2010 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file EditPart.cc
26 *
27 * Definition of EditPart class.
28 *
29 * @author Ari Metsähalme 2003 (ari.metsahalme-no.spam-tut.fi)
30 * @author Pekka Jääskeläinen 2010
31 * @note rating: yellow
32 * @note reviewed Jul 13 2004 by vpj, ll, jn, am
33 */
34
35#include <wx/wx.h>
36#include <wx/cmdproc.h>
37#include <stdlib.h>
38#include <limits>
39#include <math.h>
40
41#include "Application.hh"
42#include "ContainerTools.hh"
43#include "MachinePart.hh"
44#include "Port.hh"
45#include "Segment.hh"
46#include "EditPart.hh"
47#include "Figure.hh"
48#include "EditPolicy.hh"
49#include "Request.hh"
50#include "ComponentCommand.hh"
51
52using std::vector;
53using std::set;
54using std::min;
55using std::max;
56
57/**
58 * The Constructor.
59 */
61 parent_(NULL), figure_(NULL), model_(NULL), selectable_(
62 false), selected_(false), garbageCollected_(false) {
63}
64
65/**
66 * The Destructor.
67 *
68 * @note Do not destruct this object elsewhere than in the RootEditPart
69 * destructor! Children "garbage" should be collected before
70 * and EditPart is deleted.
71 */
74 for (unsigned int i = 0; i < editPolicies_.size(); i++) {
75 delete editPolicies_[i];
76 }
77 editPolicies_.clear();
78 delete figure_;
79}
80
81/**
82 * Puts all children and this EditPart to the trashbag.
83 *
84 * @param trashbag The trashbag.
85 */
86void
87EditPart::putGarbage(std::set<EditPart*>& trashbag) {
88 for (unsigned i = 0; i < children_.size(); i++) {
89 children_[i]->putGarbage(trashbag);
90 }
91 trashbag.insert(this);
92 garbageCollected_ = true;
93}
94
95/**
96 * Finds an EditPart whose Figure is located in the given point.
97 *
98 * @param point Point in which to find an EditPart.
99 * @return EditPart whose Figure is located in point or NULL if none found.
100 */
102EditPart::find(wxPoint point) {
103
104 EditPart* found = NULL;
105
106 // first check if a child is in point
107 for (unsigned int i = 0; i < children_.size(); i++) {
108 found = children_[i]->find(point);
109 if (found != NULL) {
110 return found;
111 }
112 }
113
114 // if no children were in located in the point, check self
115#if wxCHECK_VERSION(2, 8, 0)
116 if (selectable_ && figure_->virtualBounds().Contains(point)) {
117#else
118 if (selectable_ && figure_->virtualBounds().Inside(point)) {
119#endif
120 return this;
121 } else {
122 return NULL;
123 }
124}
125
126/**
127 * Finds an EditPart which corresponds given machine component.
128 *
129 * @param model Machine component to find.
130 * @return EditPart of the machine component, or NULL if the component was
131 * not found.
132 */
135
136 if (model == model_) {
137 return this;
138 }
139
140 EditPart* found = NULL;
141
142 // Check if the component is child of this edit part.
143 for (unsigned int i = 0; i < children_.size(); i++) {
144 found = children_[i]->find(model);
145 if (found != NULL) {
146 return found;
147 }
148 }
149
150 return NULL;
151}
152
153/**
154 * Finds an EditPart whose Figure is nearest to the given point or first
155 * found EditPart whose figure contains the given point.
156 *
157 * @param point Point in which to find the nearest EditPart.
158 * @param exclude An EditPart which is not included in the search.
159 * @return EditPart if the search found one. Otherwise returns NULL.
160 */
162EditPart::findNearest(wxPoint point, const EditPart* exclude) {
163 EditPart* found = NULL;
164 EditPart* lastFound = NULL;
165 int lastDist = std::numeric_limits<int>::max();
166
167 for (unsigned int i = 0; i < children_.size(); i++) {
168 found = children_[i]->findNearest(point, exclude);
169 if (found != NULL) {
170 wxRect foundRect = found->figure()->virtualBounds();
171
172#if wxCHECK_VERSION(2, 8, 0)
173 if (foundRect.Contains(point)) {
174#else
175 if (foundRect.Inside(point)) {
176#endif
177 assert(found->selectable());
178 return found;
179 }
180
181 if (distance(point, foundRect) < lastDist) {
182 lastFound = found;
183 lastDist = distance(point, foundRect);
184 }
185 }
186 }
187
188 if (!selectable_ || this == exclude) {
189 return lastFound;
190 }
191
192 // Check if this part's figure is closer to given point or
193 // contains the point.
194#if wxCHECK_VERSION(2, 8, 0)
195 if(figure_->virtualBounds().Contains(point)) {
196#else
197 if (figure_->virtualBounds().Inside(point)) {
198#endif
199 return this;
200 } else {
201 if (distance(point, figure_->virtualBounds()) < lastDist) {
202 return this;
203 } else {
204 return lastFound;
205 }
206 }
207}
208
209/**
210 * Recursively looks for selectable EditParts including self that are in
211 * range around given coordinates.
212 *
213 * @param point Position for the search.
214 * @param radius Search range.
215 * @param found Found EditParts are collected to this.
216 * @return Number of found EditParts.
217 */
218int
219EditPart::findInRange(wxPoint point, float radius,
220 std::vector<EditPart*>& found) {
221 int totalFound = 0;
222
223 for (unsigned int i = 0; i < children_.size(); i++) {
224 totalFound += children_[i]->findInRange(point, radius, found);
225 }
226
227 if (!selectable_) {
228 return totalFound;
229 }
230
231 if (distance(point, figure_->virtualBounds()) < radius) {
232 found.push_back(this);
233 return totalFound + 1;
234 }
235 return totalFound;
236}
237
238/**
239 * Installs a new EditPolicy.
240 *
241 * @param editpolicy The EditPolicy to be installed.
242 * @note Only one EditPolicy per Request type should be installed.
243 * If two or more EditPolicies are able to handle one type of
244 * Request, it is not defined, which EditPolicy will be chosen.
245 */
246void
248 if (editpolicy->host() == NULL) {
249 editpolicy->setHost(this);
250 }
251 editPolicies_.push_back(editpolicy);
252}
253
254/**
255 * Adds an EditPart as a child.
256 *
257 * @param child The new child to be added.
258 */
259void
262 assert(figure_ != NULL);
264 children_.push_back(child);
265 }
266}
267
268/**
269 * Looks for given EditPart recursively.
270 * @param part EditPart to look up.
271 * @return True if tree has given EditPart.
272 */
273bool
275 if (part == this) {
276 return true;
277 }
278
279 for (unsigned int i = 0; i < children_.size(); i++) {
280 if (children_.at(i)->hasEditPartRecursive(part)) {
281 return true;
282 }
283 }
284 return false;
285}
286
287/**
288 * If an EditPolicy supporting the given Request is installed to this
289 * EditPart, a corresponding Command will be returned or NULL if no
290 * EditPolicies supporting the Request is found.
291 *
292 * @param request The Request to be performed.
293 * @return A command corresponding to the given Request, if an EditPolicy
294 * supporting it is installed, NULL otherwise.
295 */
298 vector<EditPolicy*>::const_iterator i = editPolicies_.begin();
299 for (; i != editPolicies_.end(); i++) {
300 ComponentCommand* command = (*i)->getCommand(request);
301 if (command != NULL) {
302 return command;
303 }
304 }
305 return NULL;
306}
307
308/**
309 * Tells whether this EditPart can handle a certain type of Request.
310 *
311 * @param request The Request to be asked if it can be handled.
312 * @return true if an EditPolicy supporting the given Request is
313 * installed to this EditPart, false otherwise.
314 */
315bool
317 vector<EditPolicy*>::const_iterator i = editPolicies_.begin();
318 for (; i != editPolicies_.end(); i++) {
319 if ((*i)->canHandle(request)) {
320 return true;
321 }
322 }
323 return false;
324}
325
326/**
327 * Calculates distance between a point and a rectangle.
328 *
329 * @param p Position.
330 * @param r Rectangle.
331 * @return Distance between point and rectangle. Zero if the point is in
332 * the rectangle.
333 */
334float
335EditPart::distance(wxPoint p, wxRect r) {
336 float xr = 0;
337 float yr = 0;
338 float xp = static_cast<float>(p.x);
339 float yp = static_cast<float>(p.y);
340
341 xr = static_cast<float>(max(min(p.x, r.GetRight()), r.GetLeft()));
342 yr = static_cast<float>(max(min(p.y, r.GetBottom()), r.GetTop()));
343
344 float dist = sqrt((xr - xp) * (xr - xp) + (yr - yp) * (yr - yp));
345
346 return dist;
347}
#define assert(condition)
find Finds info of the inner loops in the false
static bool containsValue(const ContainerType &aContainer, const ElementType &aKey)
int findInRange(wxPoint point, float radius, std::vector< EditPart * > &found)
Definition EditPart.cc:219
EditPart * findNearest(wxPoint point, const EditPart *exclude=NULL)
Definition EditPart.cc:162
std::vector< EditPolicy * > editPolicies_
List of supported EditPolicies.
Definition EditPart.hh:105
TTAMachine::MachinePart * model_
Machine component corresponding to this EditPart.
Definition EditPart.hh:95
static float distance(wxPoint p, wxRect r)
Definition EditPart.cc:335
bool selectable() const
Figure * figure_
Figure of this EditPart.
Definition EditPart.hh:93
void installEditPolicy(EditPolicy *editpolicy)
Definition EditPart.cc:247
std::vector< EditPart * > children_
List of children EditParts.
Definition EditPart.hh:107
bool hasEditPartRecursive(const EditPart *part) const
Definition EditPart.cc:274
Figure * figure() const
bool canHandle(Request *request) const
Definition EditPart.cc:316
ComponentCommand * performRequest(Request *request) const
Definition EditPart.cc:297
bool garbageCollected_
Helper member to prevent improper deletion.
Definition EditPart.hh:102
EditPart * child(unsigned int index) const
void addChild(EditPart *child)
Definition EditPart.cc:260
EditPart * find(wxPoint point)
Definition EditPart.cc:102
TTAMachine::MachinePart * model() const
virtual ~EditPart()
Definition EditPart.cc:72
void putGarbage(std::set< EditPart * > &trashbag)
Definition EditPart.cc:87
bool selectable_
Tells whether the EditPart is selectable or not.
Definition EditPart.hh:97
void setHost(EditPart *host)
EditPart * host() const
virtual wxRect virtualBounds() const
virtual void addChild(Figure *child)