OpenASIP 2.2
Loading...
Searching...
No Matches
ConnectTool.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 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 ConnectTool.cc
26 *
27 * Definition of ConnectTool class.
28 *
29 * @author Veli-Pekka Jääskeläinen (vjaaskel-no.spam-cs.tut.fi)
30 * @note rating: red
31 */
32
33#include <wx/cursor.h>
34#include <vector>
35
36#include "ConnectTool.hh"
37#include "Request.hh"
38#include "ConnectRequest.hh"
39#include "ComponentCommand.hh"
40#include "MDFView.hh"
43#include "Socket.hh"
44#include "Port.hh"
45#include "Segment.hh"
46#include "MDFDocument.hh"
47#include "ProDeConstants.hh"
48#include "CommandRegistry.hh"
49#include "MachineCanvas.hh"
50
51using std::string;
52using namespace TTAMachine;
53
54/**
55 * The Constructor.
56 */
58 MachineCanvasTool(view->canvas()),
59 frame_(frame),
60 view_(view),
61 active_(false),
62 source_(NULL),
63 target_(NULL),
64 figure_(NULL) {
65
66}
67
68
69/**
70 * The Destructor.
71 */
73 if (figure_ != NULL) {
74 delete figure_;
75 figure_ = NULL;
76 }
77}
78
79
80/**
81 * Activates the tool.
82 */
83void
85 wxCursor connectCursor(wxCURSOR_CROSS);
86 canvas_->SetCursor(connectCursor);
87 active_ = true;
88}
89
90
91/**
92 * Deactivates the tool.
93 */
94void
96 canvas_->SetCursor(wxNullCursor);
97 active_ = false;
98}
99
100
101/**
102 * Handles mouse events on the Canvas.
103 *
104 * @param event Mouse event to handle.
105 * @param dc Device context.
106 */
107void
108ConnectTool::onMouseEvent(wxMouseEvent& event, wxDC& dc) {
109
110 if (!active_) {
111 return;
112 }
113
114 // Get event position and translate "raw" coordinates to logical ones.
115 wxPoint position = event.GetPosition();
116 long logicalX = dc.DeviceToLogicalX(position.x);
117 long logicalY = dc.DeviceToLogicalY(position.y);
118
119 // Check if there is an EditPart directly at the cursor position.
120 EditPart* toSelect = canvas_->findEditPart(logicalX, logicalY);
121 // Check if there is an EditParts near at the cursor position.
122 std::vector<EditPart*> nearbyParts;
123 canvas_->findEditPartsInRange(logicalX, logicalY, 10, nearbyParts);
124
125 EditPart* selection = canvas_->selection();
126
127 vector<EditPart*> sources;
128
129 source_ = NULL;
130 if(toSelect != NULL) {
131 target_ = toSelect;
133
134 // This enables single click feature to edit connection between
135 // socket and bus.
136 if((selection == NULL || selection == target_)
137 && !nearbyParts.empty()) {
138 sources = nearbyParts;
139 } else if(selection != target_) {
140 sources.push_back(selection);
141 }
142
143 if(!sources.empty()) {
144 for(unsigned int i = 0; i < sources.size(); i++) {
145 ConnectRequest* request = new ConnectRequest(sources.at(i));
146 if(target_->canHandle(request)) {
147 source_ = sources.at(i);
148 delete request;
149 break;
150 }
151 delete request;
152 }
153 if(source_ == NULL) {
154 target_ = NULL;
155 }
156 } else {
157 target_ = NULL;
158 source_ = NULL;
159 }
160 }
161
163
164 if (event.LeftUp()) {
165 leftClick(toSelect);
166 }
167
168 if (event.RightUp()) {
169 rightClick(event);
170 }
171}
172
173
174/**
175 * Sends a status request to the given EditPart and executes the returned
176 * command.
177 */
178void
180 string status = "";
182 if (part != NULL && part->canHandle(request)) {
183 ComponentCommand* command = part->performRequest(request);
184 if (command != NULL )
185 {
186 command->Do();
187 }
188 } else {
189 frame_->setStatus(_T(""));
190 }
191}
192
193
194/**
195 * Selects Component at cursor position, if there is a selectable
196 * EditPart at the coordinates.
197 *
198 * @param part Component to select.
199 */
200void
202 EditPart* selection = view_->selection();
203 ConnectRequest* request = new ConnectRequest(source_);
204 if (part != NULL && part->canHandle(request)) {
205 if (selection == NULL && source_ == NULL) {
206 canvas_->select(part);
207 } else if(source_ != NULL) {
208 ComponentCommand* cmd = part->performRequest(request);
209
210 if (cmd != NULL) {
211 Model* model = dynamic_cast<MDFDocument*>(
212 wxGetApp().docManager()->GetCurrentDocument())->getModel();
213 model->pushToStack();
214 if (cmd->Do()) {
215 // conenction was modified
216 model->notifyObservers();
217 target_ = NULL;
218 source_ = NULL;
219 } else {
220 // Modification failed.
221 model->popFromStack();
222 }
223 }
224 delete cmd;
225 }
226 } else {
228 }
229 delete request;
230}
231
232
233/**
234 * Pops a context menu when the right mouse button is clicked on the canvas.
235 *
236 * @param event Mouse event containing the cursor location where to pop the
237 * menu.
238 */
239void
240ConnectTool::rightClick(wxMouseEvent& event) {
241 wxMenu* contextMenu = new wxMenu();
242
243 // Create a context menu.
244 CommandRegistry* registry = wxGetApp().commandRegistry();
245 contextMenu->Append(ProDeConstants::COMMAND_UNDO, _T("&Undo"));
246 contextMenu->Append(ProDeConstants::COMMAND_REDO, _T("&Redo"));
247 contextMenu->Append(ProDeConstants::COMMAND_PASTE, _T("&Paste"));
248 contextMenu->Enable(ProDeConstants::COMMAND_UNDO,
250 contextMenu->Enable(ProDeConstants::COMMAND_REDO,
252 contextMenu->Enable(ProDeConstants::COMMAND_PASTE,
254 contextMenu->AppendSeparator();
255 contextMenu->Append(ProDeConstants::COMMAND_SELECT, _T("&Select Tool"));
256
257 // Pop the menu at cursor position.
258 wxPoint position = event.GetPosition();
259 view_->canvas()->PopupMenu(contextMenu, wxPoint(position.x, position.y));
260}
261
262
263/**
264 * Returns the tool Figure.
265 *
266 * @return Tool figure.
267 */
268Figure*
270
271 // Delete old figure.
272 if (figure_ != NULL) {
273 delete figure_;
274 figure_ = NULL;
275 }
276
278 return NULL;
279 }
280
281 Socket* socket = NULL;
282 Port* port = NULL;
283 Segment* segment = NULL;
284
285 // socket
286 socket = dynamic_cast<Socket*>(source_->model());
287 if (socket == NULL) {
288 socket = dynamic_cast<Socket*>(target_->model());
289 }
290
291 // port
292 port = dynamic_cast<Port*>(source_->model());
293 if (port == NULL) {
294 port = dynamic_cast<Port*>(target_->model());
295 }
296
297 // segment
298 segment = dynamic_cast<Segment*>(source_->model());
299 if (segment == NULL) {
300 segment = dynamic_cast<Segment*>(target_->model());
301 }
302
303 // socket - port connection
304 if (socket != NULL && port != NULL) {
306 if (port->isConnectedTo(*socket)) {
307 figure = new SocketPortConnToolFigure(false);
308 } else {
310 }
311 if (source_->model() == socket) {
312 figure->setSource(target_->figure());
313 figure->setTarget(source_->figure());
314 } else {
315 figure->setSource(source_->figure());
316 figure->setTarget(target_->figure());
317 }
318 figure_ = figure;
319 return figure_;
320 }
321
322 if (socket != NULL && segment != NULL) {
323 // return SocketBusConnToolFigure
325 if (socket->isConnectedTo(*segment)) {
326 figure = new SocketBusConnToolFigure(false);
327 } else {
329 }
330 if (source_->model() == segment) {
331 figure->setSource(target_->figure());
332 figure->setTarget(source_->figure());
333 } else {
334 figure->setSource(source_->figure());
335 figure->setTarget(target_->figure());
336 }
337 figure_ = figure;
338 return figure;
339 }
340 target_ = NULL;
341 return NULL;
342}
find Finds info of the inner loops in the false
void setStatus(const wxString text, int field=0)
Definition ChildFrame.cc:76
bool isEnabled(const std::string command)
virtual bool Do()=0
EditPart * source_
Source EditPart of the connection.
Figure * figure_
Connection figure.
ChildFrame * frame_
Parent frame of the canvas.
virtual void activate()
ConnectTool(ChildFrame *frame, MDFView *view)
virtual Figure * figure()
virtual void onMouseEvent(wxMouseEvent &event, wxDC &dc)
void rightClick(wxMouseEvent &event)
MDFView * view_
View displayed on the Canvas.
virtual void deactivate()
virtual ~ConnectTool()
void updateStatusline(EditPart *part)
void leftClick(EditPart *part)
EditPart * target_
Target EditPart of the connection.
bool active_
Tells if the tool is active or not.
Figure * figure() const
bool canHandle(Request *request) const
Definition EditPart.cc:316
ComponentCommand * performRequest(Request *request) const
Definition EditPart.cc:297
TTAMachine::MachinePart * model() const
MachineCanvas * canvas() const
Definition MDFView.cc:229
EditPart * selection()
Definition MDFView.cc:169
void clearSelection()
Definition MDFView.cc:182
MachineCanvas * canvas_
Machine canvas where the tool is used.
EditPart * selection()
int findEditPartsInRange(int x, int y, int range, std::vector< EditPart * > &found)
void select(EditPart *part)
bool hasEditPart(const EditPart *part) const
EditPart * findEditPart(int x, int y)
void refreshToolFigure()
Definition Model.hh:50
void pushToStack()
Definition Model.cc:167
void notifyObservers(bool modified=true)
Definition Model.cc:152
void popFromStack(bool modified=false)
Definition Model.cc:195
static const std::string CMD_NAME_REDO
Command name for the "Redo" command.
static const std::string CMD_NAME_UNDO
Command name for the "Undo" command.
static const std::string CMD_NAME_PASTE
Command name for the "Paste" command.
@ STATUS_REQUEST
Status request.
Definition Request.hh:52
virtual bool isConnectedTo(const Socket &socket) const
Definition Port.cc:393
bool isConnectedTo(const Bus &bus) const
Definition Socket.cc:331