OpenASIP  2.0
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 
51 using std::string;
52 using 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  */
83 void
85  wxCursor connectCursor(wxCURSOR_CROSS);
86  canvas_->SetCursor(connectCursor);
87  active_ = true;
88 }
89 
90 
91 /**
92  * Deactivates the tool.
93  */
94 void
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  */
107 void
108 ConnectTool::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  */
178 void
180  string status = "";
181  Request* request = new Request(Request::STATUS_REQUEST);
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  */
200 void
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  */
239 void
240 ConnectTool::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  */
268 Figure*
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 {
309  figure = new SocketPortConnToolFigure(true);
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 {
328  figure = new SocketBusConnToolFigure(true);
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 }
ConnectTool::view_
MDFView * view_
View displayed on the Canvas.
Definition: ConnectTool.hh:63
MachineCanvas::findEditPart
EditPart * findEditPart(int x, int y)
Definition: MachineCanvas.cc:421
ChildFrame::setStatus
void setStatus(const wxString text, int field=0)
Definition: ChildFrame.cc:76
ProDeConstants::COMMAND_UNDO
@ COMMAND_UNDO
Definition: ProDeConstants.hh:433
CommandRegistry::isEnabled
bool isEnabled(const std::string command)
Definition: CommandRegistry.cc:210
CommandRegistry.hh
MDFDocument.hh
ConnectTool::activate
virtual void activate()
Definition: ConnectTool.cc:84
MDFView::canvas
MachineCanvas * canvas() const
Definition: MDFView.cc:229
ConnectTool::ConnectTool
ConnectTool(ChildFrame *frame, MDFView *view)
Definition: ConnectTool.cc:57
TTAMachine::Segment
Definition: Segment.hh:54
EditPart::performRequest
ComponentCommand * performRequest(Request *request) const
Definition: EditPart.cc:297
ConnectTool::leftClick
void leftClick(EditPart *part)
Definition: ConnectTool.cc:201
Socket.hh
ProDeConstants::CMD_NAME_PASTE
static const std::string CMD_NAME_PASTE
Command name for the "Paste" command.
Definition: ProDeConstants.hh:130
Model::pushToStack
void pushToStack()
Definition: Model.cc:167
ChildFrame
Definition: ChildFrame.hh:42
ConnectTool::deactivate
virtual void deactivate()
Definition: ConnectTool.cc:95
ComponentCommand::Do
virtual bool Do()=0
Model::notifyObservers
void notifyObservers(bool modified=true)
Definition: Model.cc:152
ConnectTool::frame_
ChildFrame * frame_
Parent frame of the canvas.
Definition: ConnectTool.hh:61
MachineCanvas::refreshToolFigure
void refreshToolFigure()
Definition: MachineCanvas.cc:164
MachineCanvas::selection
EditPart * selection()
Definition: MachineCanvas.cc:409
Port.hh
Segment.hh
ConnectTool::~ConnectTool
virtual ~ConnectTool()
Definition: ConnectTool.cc:72
Request.hh
ConnectRequest
Definition: ConnectRequest.hh:43
SocketPortConnToolFigure
Definition: SocketPortConnToolFigure.hh:45
ProDeConstants::COMMAND_SELECT
@ COMMAND_SELECT
Definition: ProDeConstants.hh:460
TTAMachine::Port
Definition: Port.hh:54
MDFView::clearSelection
void clearSelection()
Definition: MDFView.cc:182
Figure
Definition: Figure.hh:50
MachineCanvas.hh
CommandRegistry
Definition: CommandRegistry.hh:47
TTAMachine::Socket
Definition: Socket.hh:53
MachineCanvasTool
Definition: MachineCanvasTool.hh:49
ConnectTool::onMouseEvent
virtual void onMouseEvent(wxMouseEvent &event, wxDC &dc)
Definition: ConnectTool.cc:108
EditPart
Definition: EditPart.hh:60
ConnectTool::source_
EditPart * source_
Source EditPart of the connection.
Definition: ConnectTool.hh:67
Model::popFromStack
void popFromStack(bool modified=false)
Definition: Model.cc:195
ProDeConstants::COMMAND_REDO
@ COMMAND_REDO
Definition: ProDeConstants.hh:434
ConnectTool::target_
EditPart * target_
Target EditPart of the connection.
Definition: ConnectTool.hh:69
TTAMachine::Socket::isConnectedTo
bool isConnectedTo(const Bus &bus) const
Definition: Socket.cc:331
SocketPortConnToolFigure.hh
EditPart::model
TTAMachine::MachinePart * model() const
Request
Definition: Request.hh:43
MDFDocument
Definition: MDFDocument.hh:51
ProDeConstants.hh
ComponentCommand
Definition: ComponentCommand.hh:46
ConnectRequest.hh
MDFView::selection
EditPart * selection()
Definition: MDFView.cc:169
MachineCanvas::select
void select(EditPart *part)
Definition: MachineCanvas.cc:485
ProDeConstants::CMD_NAME_UNDO
static const std::string CMD_NAME_UNDO
Command name for the "Undo" command.
Definition: ProDeConstants.hh:122
EditPart::figure
Figure * figure() const
MDFView.hh
Model
Definition: Model.hh:50
ConnectTool.hh
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
MachineCanvas::findEditPartsInRange
int findEditPartsInRange(int x, int y, int range, std::vector< EditPart * > &found)
Definition: MachineCanvas.cc:454
ConnectTool::figure_
Figure * figure_
Connection figure.
Definition: ConnectTool.hh:71
EditPart::canHandle
bool canHandle(Request *request) const
Definition: EditPart.cc:316
SocketBusConnToolFigure.hh
SocketBusConnToolFigure
Definition: SocketBusConnToolFigure.hh:45
ConnectTool::active_
bool active_
Tells if the tool is active or not.
Definition: ConnectTool.hh:65
Request::STATUS_REQUEST
@ STATUS_REQUEST
Status request.
Definition: Request.hh:52
TTAMachine::Port::isConnectedTo
virtual bool isConnectedTo(const Socket &socket) const
Definition: Port.cc:393
ConnectTool::rightClick
void rightClick(wxMouseEvent &event)
Definition: ConnectTool.cc:240
MachineCanvasTool::canvas_
MachineCanvas * canvas_
Machine canvas where the tool is used.
Definition: MachineCanvasTool.hh:77
TTAMachine
Definition: Assembler.hh:48
MachineCanvas::hasEditPart
bool hasEditPart(const EditPart *part) const
Definition: MachineCanvas.cc:470
ProDeConstants::COMMAND_PASTE
@ COMMAND_PASTE
Definition: ProDeConstants.hh:437
ProDeConstants::CMD_NAME_REDO
static const std::string CMD_NAME_REDO
Command name for the "Redo" command.
Definition: ProDeConstants.hh:124
ConnectTool::figure
virtual Figure * figure()
Definition: ConnectTool.cc:269
ConnectTool::updateStatusline
void updateStatusline(EditPart *part)
Definition: ConnectTool.cc:179
MDFView
Definition: MDFView.hh:59
ComponentCommand.hh