OpenASIP  2.0
SelectTool.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 SelectTool.cc
26  *
27  * Definition of SelectTool class.
28  *
29  * @author Veli-Pekka Jääskeläinen (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 #include <wx/event.h>
35 #include <wx/gdicmn.h>
36 
37 #include "SelectTool.hh"
38 #include "Conversion.hh"
39 #include "WxConversion.hh"
40 #include "MDFView.hh"
41 #include "EditPart.hh"
42 #include "Request.hh"
43 #include "ComponentCommand.hh"
44 #include "ModifyComponentCmd.hh"
45 #include "CommandRegistry.hh"
46 #include "ProDeConstants.hh"
47 #include "SelectionFigure.hh"
48 #include "MachineCanvas.hh"
49 #include "ProDe.hh"
50 #include "MainFrame.hh"
51 
52 using std::string;
53 
54 
55 /**
56  * The Constructor.
57  */
59  ChildFrame* frame, MDFView* view):
60  MachineCanvasTool(view->canvas()),
61  frame_(frame),
62  view_(view),
63  active_(false) {
64 
65  figure_ = new SelectionFigure(NULL);
66 }
67 
68 
69 /**
70  * The Destructor.
71  */
73  delete figure_;
74 }
75 
76 
77 /**
78  * Activates the tool.
79  */
80 void
82  active_ = true;
83 }
84 
85 
86 /**
87  * Deactivates the tool.
88  */
89 void
91  active_ = false;
92 }
93 
94 /**
95  * Returns figure of the selection.
96  *
97  * @return Figure of the selection.
98  */
99 Figure*
101  return NULL;
102 }
103 
104 /**
105  * Handles mouse events on the Canvas.
106  *
107  * @param event Mouse event to handle.
108  * @param dc Device context.
109  */
110 void
111 SelectTool::onMouseEvent(wxMouseEvent& event, wxDC& dc) {
112 
113  if (!active_) {
114  return;
115  }
116 
117  // Get event position and translate "raw" coordinates to logical ones.
118  wxPoint position = event.GetPosition();
119  int x = position.x;
120  int y = position.y;
121  long logicalX = dc.DeviceToLogicalX(position.x);
122  long logicalY = dc.DeviceToLogicalY(position.y);
123 
124  // Check if there is an EditPart at the cursor position.
125  EditPart* part = canvas_->findEditPart(logicalX, logicalY);
126 
127  // If an EditPart was found at the cursor position, get name of the
128  // Component related to the EditPart.
129  string status = "";
130  Request* request = new Request(Request::STATUS_REQUEST);
131  if (part != NULL && part->canHandle(request)) {
132  ComponentCommand* command = part->performRequest(request);
133  if (command != NULL) {
134  if(!command->Do())
135  frame_->setStatus(_T(""));
136  }
137  } else {
138  frame_->setStatus(_T(""));
139  }
140 
141  // If left mouse button was released, select the EditPart at cursor
142  // position. (if there is a selectable EditPart)
143  if (event.LeftUp()) {
144  leftClick(part);
145  }
146 
147  if (event.RightDown()) {
148  // select component
149  leftClick(part);
150  // popup context menu
151  popContextMenu(part, x, y);
152  }
153 
154  // left mouse button double click executes ModifyComponentCmd
155  if (event.ButtonDClick(1)) {
156  ModifyComponentCmd* command = new ModifyComponentCmd();
157  command->setParentWindow(frame_);
158  command->setView(view_);
159  command->Do();
160  }
161 
162  wxGetApp().mainFrame()->updateUI();
163 }
164 
165 
166 /**
167  * Selects Component at cursor position, if there is a selectable
168  * EditPart at the coordinates.
169  *
170  * @param part Component to select.
171  */
172 void
174  if (part != NULL && part->selectable()) {
175  canvas_->select(part);
176  figure_->setSelection(part->figure());
177  } else {
179  }
180 }
181 
182 
183 /**
184  * Pops a context menu for the EditPart at the coordinates;
185  *
186  * @param part Context component.
187  * @param x X-coordinate for the menu.
188  * @param y Y-coordinate for the menu.
189  */
190 void
191 SelectTool::popContextMenu(EditPart* part, int x, int y) {
192 
193  wxMenu* contextMenu = NULL;
194 
195  if (part == NULL) {
196  contextMenu = createDefaultMenu();
197 
198  } else {
199 
200  CommandRegistry* registry = wxGetApp().commandRegistry();
201 
202  contextMenu = new wxMenu();
203  contextMenu->Append(ProDeConstants::COMMAND_COPY,
204  _T("&Copy"));
205  contextMenu->Append(ProDeConstants::COMMAND_CUT,
206  _T("Cu&t"));
207  contextMenu->AppendSeparator();
208  contextMenu->Append(ProDeConstants::COMMAND_MODIFY_COMP,
209  _T("&Modify..."));
210  contextMenu->Append(ProDeConstants::COMMAND_DELETE_COMP,
211  _T("&Delete"));
212 
213  contextMenu->Enable(
216 
217  contextMenu->Enable(
220 
221  contextMenu->Enable(
224 
225  contextMenu->Enable(
228  }
229 
230  view_->canvas()->PopupMenu(contextMenu, wxPoint(x,y));
231 }
232 
233 
234 /**
235  * Returns a popup menu to be used as a default context menu on the canvas.
236  */
237 wxMenu*
239 
240  wxMenu* popupMenu = new wxMenu();
241  CommandRegistry* registry = wxGetApp().commandRegistry();
242  popupMenu->Append(ProDeConstants::COMMAND_UNDO, _T("&Undo"));
243  popupMenu->Append(ProDeConstants::COMMAND_REDO, _T("&Redo"));
244  popupMenu->Append(ProDeConstants::COMMAND_PASTE, _T("&Paste"));
245 
246  popupMenu->Enable(ProDeConstants::COMMAND_UNDO,
248  popupMenu->Enable(ProDeConstants::COMMAND_REDO,
250  popupMenu->Enable(ProDeConstants::COMMAND_PASTE,
252 
253 
254  popupMenu->AppendSeparator();
255 
256  wxMenu* addSubMenu = new wxMenu;
257  addSubMenu->Append(ProDeConstants::COMMAND_ADD_FU,
258  _T("&Function Unit..."));
259  addSubMenu->Append(ProDeConstants::COMMAND_ADD_RF,
260  _T("&Register File..."));
261  addSubMenu->Append(ProDeConstants::COMMAND_ADD_BUS,
262  _T("&Transport Bus..."));
263  addSubMenu->Append(ProDeConstants::COMMAND_ADD_SOCKET,
264  _T("&Socket..."));
265  addSubMenu->Append(ProDeConstants::COMMAND_ADD_BRIDGE,
266  _T("&Bridge..."));
267  addSubMenu->Append(ProDeConstants::COMMAND_ADD_IU,
268  _T("&Immediate Unit..."));
269  addSubMenu->Append(ProDeConstants::COMMAND_ADD_GCU,
270  _T("&Global Control Unit..."));
271  addSubMenu->Append(ProDeConstants::COMMAND_ADD_AS,
272  _T("&Address Space..."));
273  popupMenu->Append(ID_ADD_SUBMENU, _T("&Add"), addSubMenu);
274 
276  _T("Edit &Connections"));
277  popupMenu->Append(ProDeConstants::COMMAND_SELECT,
278  _T("&Select"));
279  popupMenu->AppendSeparator();
280  popupMenu->Append(ProDeConstants::COMMAND_VERIFY_MACHINE,
281  _T("&Verify"));
282 
283  return popupMenu;
284 }
MachineCanvas::findEditPart
EditPart * findEditPart(int x, int y)
Definition: MachineCanvas.cc:421
GUICommand::setParentWindow
void setParentWindow(wxWindow *view)
Definition: GUICommand.cc:64
SelectTool::deactivate
virtual void deactivate()
Definition: SelectTool.cc:90
ProDe.hh
ChildFrame::setStatus
void setStatus(const wxString text, int field=0)
Definition: ChildFrame.cc:76
ProDeConstants::COMMAND_UNDO
@ COMMAND_UNDO
Definition: ProDeConstants.hh:433
ProDeConstants::COMMAND_ADD_IU
@ COMMAND_ADD_IU
Definition: ProDeConstants.hh:422
CommandRegistry::isEnabled
bool isEnabled(const std::string command)
Definition: CommandRegistry.cc:210
ProDeConstants::COMMAND_MODIFY_COMP
@ COMMAND_MODIFY_COMP
Definition: ProDeConstants.hh:431
CommandRegistry.hh
ProDeConstants::COMMAND_EDIT_CONNECTIONS
@ COMMAND_EDIT_CONNECTIONS
Definition: ProDeConstants.hh:439
MachineCanvas::clearSelection
void clearSelection()
Definition: MachineCanvas.cc:395
MDFView::canvas
MachineCanvas * canvas() const
Definition: MDFView.cc:229
ProDeConstants::COMMAND_ADD_GCU
@ COMMAND_ADD_GCU
Definition: ProDeConstants.hh:423
ModifyComponentCmd::Do
virtual bool Do()
Definition: ModifyComponentCmd.cc:61
EditPart::performRequest
ComponentCommand * performRequest(Request *request) const
Definition: EditPart.cc:297
ProDeConstants::COMMAND_ADD_FU
@ COMMAND_ADD_FU
Definition: ProDeConstants.hh:417
ProDeConstants::COMMAND_DELETE_COMP
@ COMMAND_DELETE_COMP
Definition: ProDeConstants.hh:430
ModifyComponentCmd
Definition: ModifyComponentCmd.hh:43
ProDeConstants::CMD_NAME_MODIFY_COMP
static const std::string CMD_NAME_MODIFY_COMP
Command name for the "Modify Component" command.
Definition: ProDeConstants.hh:119
ProDeConstants::COMMAND_ADD_AS
@ COMMAND_ADD_AS
Definition: ProDeConstants.hh:424
EditorCommand::setView
void setView(wxView *view)
Definition: EditorCommand.cc:65
ProDeConstants::CMD_NAME_PASTE
static const std::string CMD_NAME_PASTE
Command name for the "Paste" command.
Definition: ProDeConstants.hh:130
ProDeConstants::COMMAND_COPY
@ COMMAND_COPY
Definition: ProDeConstants.hh:436
ChildFrame
Definition: ChildFrame.hh:42
ComponentCommand::Do
virtual bool Do()=0
ProDeConstants::COMMAND_CUT
@ COMMAND_CUT
Definition: ProDeConstants.hh:435
SelectTool::ID_ADD_SUBMENU
@ ID_ADD_SUBMENU
Definition: SelectTool.hh:75
EditPart::selectable
bool selectable() const
Request.hh
SelectTool::~SelectTool
virtual ~SelectTool()
Definition: SelectTool.cc:72
SelectTool::figure_
SelectionFigure * figure_
Selection figure;.
Definition: SelectTool.hh:67
EditPart.hh
ProDeConstants::COMMAND_SELECT
@ COMMAND_SELECT
Definition: ProDeConstants.hh:460
Conversion.hh
Figure
Definition: Figure.hh:50
MachineCanvas.hh
CommandRegistry
Definition: CommandRegistry.hh:47
ProDeConstants::COMMAND_ADD_BUS
@ COMMAND_ADD_BUS
Definition: ProDeConstants.hh:419
MachineCanvasTool
Definition: MachineCanvasTool.hh:49
SelectTool::leftClick
void leftClick(EditPart *part)
Definition: SelectTool.cc:173
EditPart
Definition: EditPart.hh:60
SelectTool::activate
virtual void activate()
Definition: SelectTool.cc:81
ProDeConstants::COMMAND_ADD_RF
@ COMMAND_ADD_RF
Definition: ProDeConstants.hh:418
SelectionFigure
Definition: SelectionFigure.hh:45
ProDeConstants::COMMAND_REDO
@ COMMAND_REDO
Definition: ProDeConstants.hh:434
ProDeConstants::CMD_NAME_COPY
static const std::string CMD_NAME_COPY
Command name for the "Copy" command.
Definition: ProDeConstants.hh:128
SelectTool::frame_
ChildFrame * frame_
Parent frame of the Canvas.
Definition: SelectTool.hh:61
Request
Definition: Request.hh:43
ProDeConstants.hh
ComponentCommand
Definition: ComponentCommand.hh:46
MachineCanvas::select
void select(EditPart *part)
Definition: MachineCanvas.cc:485
SelectTool.hh
ProDeConstants::CMD_NAME_UNDO
static const std::string CMD_NAME_UNDO
Command name for the "Undo" command.
Definition: ProDeConstants.hh:122
MainFrame.hh
EditPart::figure
Figure * figure() const
SelectTool::createDefaultMenu
wxMenu * createDefaultMenu()
Definition: SelectTool.cc:238
MDFView.hh
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
SelectTool::view_
MDFView * view_
View displayed on the Canvas.
Definition: SelectTool.hh:63
SelectTool::onMouseEvent
virtual void onMouseEvent(wxMouseEvent &event, wxDC &dc)
Definition: SelectTool.cc:111
SelectTool::popContextMenu
void popContextMenu(EditPart *part, int x, int y)
Definition: SelectTool.cc:191
ProDeConstants::COMMAND_ADD_SOCKET
@ COMMAND_ADD_SOCKET
Definition: ProDeConstants.hh:420
EditPart::canHandle
bool canHandle(Request *request) const
Definition: EditPart.cc:316
ProDeConstants::CMD_NAME_CUT
static const std::string CMD_NAME_CUT
Command name for the "Cut" command.
Definition: ProDeConstants.hh:126
SelectTool::SelectTool
SelectTool(ChildFrame *frame, MDFView *view)
Definition: SelectTool.cc:58
WxConversion.hh
ModifyComponentCmd.hh
Request::STATUS_REQUEST
@ STATUS_REQUEST
Status request.
Definition: Request.hh:52
SelectTool::figure
virtual Figure * figure()
Definition: SelectTool.cc:100
MachineCanvasTool::canvas_
MachineCanvas * canvas_
Machine canvas where the tool is used.
Definition: MachineCanvasTool.hh:77
SelectTool::active_
bool active_
Tells if the tool is active or not.
Definition: SelectTool.hh:65
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
SelectionFigure::setSelection
void setSelection(Figure *selection)
Definition: SelectionFigure.cc:100
ProDeConstants::COMMAND_ADD_BRIDGE
@ COMMAND_ADD_BRIDGE
Definition: ProDeConstants.hh:421
SelectionFigure.hh
ProDeConstants::COMMAND_VERIFY_MACHINE
@ COMMAND_VERIFY_MACHINE
Definition: ProDeConstants.hh:457
MDFView
Definition: MDFView.hh:59
ComponentCommand.hh