OpenASIP  2.0
RFPortDialog.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 RFPortDialog.cc
26  *
27  * Definition of RFPortDialog class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2004 (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <wx/wx.h>
34 #include <wx/statline.h>
35 #include <boost/format.hpp>
36 
37 #include "RegisterFile.hh"
38 #include "RFPortDialog.hh"
39 #include "WxConversion.hh"
40 #include "Conversion.hh"
41 #include "ProDeConstants.hh"
42 #include "Socket.hh"
43 #include "Machine.hh"
44 #include "MachineTester.hh"
45 #include "WarningDialog.hh"
46 #include "UserManualCmd.hh"
47 #include "Port.hh"
48 #include "WidgetTools.hh"
49 #include "GUITextGenerator.hh"
50 #include "ProDeTextGenerator.hh"
51 #include "InformationDialog.hh"
52 
53 using boost::format;
54 using std::string;
55 using namespace TTAMachine;
56 
57 BEGIN_EVENT_TABLE(RFPortDialog, wxDialog)
58  EVT_TEXT(ID_NAME, RFPortDialog::onName)
61  EVT_CHOICE(ID_INPUT_SOCKET, RFPortDialog::onSocketChoice)
62  EVT_CHOICE(ID_OUTPUT_SOCKET, RFPortDialog::onSocketChoice)
64 
65 /**
66  * The Constructor.
67  *
68  * @param parent Parent window of the dialog.
69  * @param port Port to modify.
70  */
72  wxWindow* parent,
73  Port* port):
74  wxDialog(parent, -1, _T(""), wxDefaultPosition),
75  port_(port),
76  name_(_T("")),
77  inputSocketChoice_(NULL),
78  outputSocketChoice_(NULL) {
79 
80  oldInput_ = port_->inputSocket();
81  oldOutput_ = port_->outputSocket();
82  createContents(this, true, true);
83  FindWindow(wxID_OK)->Disable();
84 
85  // set widget texts
86  setTexts();
87 
88  TransferDataToWindow();
89 }
90 
91 /**
92  * The Destructor.
93  */
95 }
96 
97 /**
98  * Sets texts for widgets.
99  */
100 void
104 
105  // Dialog title
106  format fmt = prodeTexts->text(
108  SetTitle(WxConversion::toWxString(fmt.str()));
109 
110  // buttons
111  WidgetTools::setLabel(generator, FindWindow(wxID_OK),
113 
114  WidgetTools::setLabel(generator, FindWindow(wxID_CANCEL),
116 
117  WidgetTools::setLabel(generator, FindWindow(ID_HELP),
119 
120  // widget labels
121  WidgetTools::setLabel(prodeTexts, FindWindow(ID_NAME),
123 
124  WidgetTools::setLabel(prodeTexts, FindWindow(ID_INPUT_SOCKET),
126 
127  WidgetTools::setLabel(prodeTexts, FindWindow(ID_OUTPUT_SOCKET),
129 }
130 
131 /**
132  * Validates input in the controls, and updates the port.
133  */
134 void
135 RFPortDialog::onOK(wxCommandEvent&) {
136 
137  if (!Validate()) {
138  return;
139  }
140 
141  if (!TransferDataFromWindow()) {
142  return;
143  }
144 
145  string trimmedName =
146  WxConversion::toString(name_.Trim(false).Trim(true));
147 
148  // Check the name validity.
149  if (!MachineTester::isValidComponentName(trimmedName)) {
151  format message =
153  InformationDialog warning(
154  this, WxConversion::toWxString(message.str()));
155  warning.ShowModal();
156  return;
157  }
158 
159  // check whether RF already has a port of tht name.
160  if (port_->name() != trimmedName) {
161  Unit* rf = port_->parentUnit();
162  for (int i = 0; i < rf->portCount(); i++) {
163  string name = rf->port(i)->name();
164  if (name == WxConversion::toString(name_)) {
165  ProDeTextGenerator* prodeTexts =
167  format message =
169  format a_port =
171  format rf =
173  format port =
174  prodeTexts->text(ProDeTextGenerator::COMP_PORT);
175  message % trimmedName % a_port.str() % rf.str() % port.str();
176  WarningDialog warning(
177  this, WxConversion::toWxString(message.str()));
178  warning.ShowModal();
179  return;
180  }
181  }
182  }
183 
184  port_->setName(trimmedName);
185  // update parent RFs max write and read parameters
186  RegisterFile* RF = dynamic_cast<RegisterFile*>(port_->parentUnit());
187  assert (RF != NULL);
188  //RF->updateMaxReadsAndWrites();
189  EndModal(wxID_OK);
190 }
191 
192 
193 /**
194  * Resets the original output and input sockets for the port and closes the
195  * dialog.
196  */
197 void
198 RFPortDialog::onCancel(wxCommandEvent&) {
199  port_->detachAllSockets();
200  if (oldInput_ != NULL) {
201  port_->attachSocket(*oldInput_);
202  }
203  if (oldOutput_ != NULL) {
204  port_->attachSocket(*oldOutput_);
205  }
206  EndModal(wxID_CANCEL);
207 }
208 
209 /**
210  * Transfers data from the port object to the dialog widgets.
211  *
212  * @return False, if an error occured in the transfer.
213  */
214 bool
216  name_ = WxConversion::toWxString(port_->name());
217  updateSockets();
218  return wxWindow::TransferDataToWindow();
219 }
220 
221 /**
222  * Updates the port object when user changes input/output socket selection.
223  */
224 void
226  port_->detachAllSockets();
227  Machine::SocketNavigator navigator =
228  port_->parentUnit()->machine()->socketNavigator();
229 
230  // set input socket
231  string inputSocketName =
232  WxConversion::toString(inputSocketChoice_->GetStringSelection());
233  if (inputSocketName != WxConversion::toString(ProDeConstants::NONE)) {
234  port_->attachSocket(*(navigator.item(inputSocketName)));
235  }
236 
237  // set output socket
238  string outputSocketName =
239  WxConversion::toString(outputSocketChoice_->GetStringSelection());
240  if (outputSocketName != WxConversion::toString(ProDeConstants::NONE)) {
241  port_->attachSocket(*(navigator.item(outputSocketName)));
242  }
243  updateSockets();
244 }
245 
246 /**
247  * Updates input and output socket choicers.
248  */
249 void
251 
252  inputSocketChoice_->Clear();
253  inputSocketChoice_->Append(ProDeConstants::NONE);
254  outputSocketChoice_->Clear();
255  outputSocketChoice_->Append(ProDeConstants::NONE);
256 
257  MachineTester tester(*(port_->parentUnit()->machine()));
258 
259  // Add ports to the choicers
260  Machine::SocketNavigator navigator =
261  port_->parentUnit()->machine()->socketNavigator();
262 
263  for (int i = 0; i < navigator.count(); i++) {
264  Socket* socket = navigator.item(i);
265  wxString socketName = WxConversion::toWxString(socket->name());
266 
267  // Add available input sockets.
268  Socket* input = port_->inputSocket();
269  if (input != NULL) {
270  port_->detachSocket(*input);
271  }
272  bool legal = tester.canConnect(*socket, *port_);
273  if (legal && socket->direction() == Socket::INPUT) {
274  inputSocketChoice_->Append(socketName);
275  }
276  if (input != NULL) {
277  port_->attachSocket(*input);
278  }
279 
280  // Add available output sockets.
281  Socket* output = port_->outputSocket();
282  if (output != NULL) {
283  port_->detachSocket(*output);
284  }
285  legal = tester.canConnect(*socket, *port_);
286  if (legal && socket->direction() == Socket::OUTPUT) {
287  outputSocketChoice_->Append(socketName);
288  }
289  if (output != NULL) {
290  port_->attachSocket(*output);
291  }
292  // ignore sockets with unknown direction
293  }
294 
295  // set input socket choice
296  if (port_->inputSocket() == NULL) {
297  inputSocketChoice_->SetStringSelection(ProDeConstants::NONE);
298  } else {
299  wxString socketName =
300  WxConversion::toWxString(port_->inputSocket()->name());
301  inputSocketChoice_->SetStringSelection(socketName);
302  }
303 
304  // set output socket choice
305  if (port_->outputSocket() == NULL) {
306  outputSocketChoice_->SetStringSelection(ProDeConstants::NONE);
307  } else {
308  wxString socketName =
309  WxConversion::toWxString(port_->outputSocket()->name());
310  outputSocketChoice_->SetStringSelection(socketName);
311  }
312 }
313 
314 /**
315  * Validates input in the controls, and updates the ComponentDescriptor.
316  */
317 void
318 RFPortDialog::onName(wxCommandEvent&) {
319  if (!TransferDataFromWindow()) {
320  assert(false);
321  }
322  wxString trimmedName = name_.Trim(false).Trim(true);
323  if (trimmedName == _T("")) {
324  FindWindow(wxID_OK)->Disable();
325  } else {
326  FindWindow(wxID_OK)->Enable();
327  }
328 }
329 
330 /**
331  * Creates contents of the dialog window. Initially generated with
332  * wxDesigner, the code will be cleaned up later.
333  *
334  * @param parent Parent dialog of the contents.
335  * @param call_fit If true, fits sizer in dialog window.
336  * @param set_sizer If true, sets sizer as dialog's sizer.
337  * @return Top level sizer of the contents.
338  */
339 wxSizer*
341  wxWindow *parent, bool call_fit, bool set_sizer) {
342 
343  wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
344  wxFlexGridSizer *item1 = new wxFlexGridSizer( 2, 0, 0 );
345 
346  // name element
347  wxStaticText *item2 =
348  new wxStaticText( parent, -1, wxT("Name:"),
349  wxDefaultPosition, wxDefaultSize, 0 );
350  item1->Add( item2, 0, wxALIGN_RIGHT|wxALL, 5 );
351  wxTextCtrl *item3 =
352  new wxTextCtrl(parent, ID_NAME, wxT(""), wxDefaultPosition,
353  wxSize(160,-1), 0,
354  wxTextValidator(wxFILTER_ASCII, &name_) );
355  item1->Add( item3, 0, wxALIGN_CENTER|wxALL, 5 );
356 
357  // input socket element
358  wxStaticText *item4 =
359  new wxStaticText(parent, -1, wxT("Input Socket:"),
360  wxDefaultPosition, wxDefaultSize, 0);
361  item1->Add( item4, 0, wxALIGN_RIGHT|wxALL, 5 );
362  wxString *strs5 = (wxString*) NULL;
363  inputSocketChoice_ =
364  new wxChoice(parent, ID_INPUT_SOCKET, wxDefaultPosition,
365  wxSize(100,-1), 0, strs5, 0);
366  item1->Add( inputSocketChoice_, 0,
367  wxGROW|wxALL, 5 );
368 
369  // output socket element
370  wxStaticText *item6 =
371  new wxStaticText(parent, -1, wxT("Output Socket:"),
372  wxDefaultPosition, wxDefaultSize, 0);
373  item1->Add( item6, 0, wxALIGN_RIGHT|wxALL, 5 );
374  wxString *strs7 = (wxString*) NULL;
375  outputSocketChoice_ =
376  new wxChoice(parent, ID_OUTPUT_SOCKET, wxDefaultPosition,
377  wxSize(100,-1), 0, strs7, 0);
378  item1->Add( outputSocketChoice_, 0,
379  wxGROW|wxALL, 5 );
380  item0->Add( item1, 0, wxGROW|wxALL, 5 );
381  wxStaticLine *item8 =
382  new wxStaticLine(parent, -1, wxDefaultPosition, wxSize(20,-1),
383  wxLI_HORIZONTAL);
384  item0->Add( item8, 0, wxGROW|wxALL, 5 );
385  wxBoxSizer *item9 = new wxBoxSizer( wxHORIZONTAL );
386 
387  // buttons
388  wxButton *item10 =
389  new wxButton(parent, ID_HELP, wxT("&Help"), wxDefaultPosition,
390  wxDefaultSize, 0);
391  item9->Add( item10, 0, wxALIGN_CENTER|wxALL, 5 );
392 
393  wxButton *item11 =
394  new wxButton(parent, wxID_OK, wxT("&OK"), wxDefaultPosition,
395  wxDefaultSize, 0);
396  item9->Add( item11, 0, wxALIGN_CENTER|wxALL, 5 );
397  wxButton *item12 =
398  new wxButton(parent, wxID_CANCEL, wxT("&Cancel"), wxDefaultPosition,
399  wxDefaultSize, 0);
400  item9->Add( item12, 0, wxALIGN_CENTER|wxALL, 5 );
401  item0->Add( item9, 0, wxALIGN_CENTER|wxALL, 5 );
402 
403  if (set_sizer) {
404  parent->SetAutoLayout( TRUE );
405  parent->SetSizer( item0 );
406  if (call_fit)
407  {
408  item0->Fit( parent );
409  item0->SetSizeHints( parent );
410  }
411  }
412  return item0;
413 }
MachineTester::canConnect
virtual bool canConnect(const TTAMachine::Socket &socket, const TTAMachine::Segment &segment)
Definition: MachineTester.cc:86
WarningDialog
Definition: WarningDialog.hh:42
WxConversion::toWxString
static wxString toWxString(const std::string &source)
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
TTAMachine::Socket::OUTPUT
@ OUTPUT
Data goes from port to bus.
Definition: Socket.hh:60
WidgetTools::setLabel
static void setLabel(Texts::TextGenerator *generator, wxWindow *widget, int textID)
Definition: WidgetTools.cc:92
GUITextGenerator::instance
static GUITextGenerator * instance()
Definition: GUITextGenerator.cc:67
RFPortDialog::onCancel
void onCancel(wxCommandEvent &event)
Definition: RFPortDialog.cc:198
ProDeTextGenerator::TXT_LABEL_INPUT_SOCKET
@ TXT_LABEL_INPUT_SOCKET
Label for input socket selector.
Definition: ProDeTextGenerator.hh:61
GUITextGenerator
Definition: GUITextGenerator.hh:46
GUITextGenerator::TXT_BUTTON_HELP
@ TXT_BUTTON_HELP
Label for help button.
Definition: GUITextGenerator.hh:60
WidgetTools.hh
FindWindow
Definition: FindWindow.hh:49
RFPortDialog::createContents
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
Definition: RFPortDialog.cc:340
TTAMachine::Machine::Navigator::count
int count() const
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
TTAMachine::Socket::direction
Direction direction() const
RFPortDialog
Definition: RFPortDialog.hh:44
RFPortDialog::setTexts
void setTexts()
Definition: RFPortDialog.cc:101
Socket.hh
ProDeTextGenerator.hh
ProDeTextGenerator
Definition: ProDeTextGenerator.hh:49
ProDeTextGenerator::MSG_ERROR_ILLEGAL_NAME
@ MSG_ERROR_ILLEGAL_NAME
Error: Illegal component name.
Definition: ProDeTextGenerator.hh:223
ProDeTextGenerator::MSG_ERROR_SAME_NAME
@ MSG_ERROR_SAME_NAME
Error: Same name exists.
Definition: ProDeTextGenerator.hh:229
assert
#define assert(condition)
Definition: Application.hh:86
ProDeTextGenerator::TXT_RF_PORT_DIALOG_TITLE
@ TXT_RF_PORT_DIALOG_TITLE
Register file port Dialog title.
Definition: ProDeTextGenerator.hh:161
RFPortDialog::onName
void onName(wxCommandEvent &event)
Definition: RFPortDialog.cc:318
Port.hh
GUITextGenerator::TXT_BUTTON_CANCEL
@ TXT_BUTTON_CANCEL
Label for cancel button.
Definition: GUITextGenerator.hh:55
TTAMachine::Unit
Definition: Unit.hh:51
WarningDialog.hh
Conversion.hh
TTAMachine::Port
Definition: Port.hh:54
InformationDialog.hh
ProDeTextGenerator::COMP_A_PORT
@ COMP_A_PORT
Name for port (w/ article).
Definition: ProDeTextGenerator.hh:270
TTAMachine::Unit::port
virtual Port * port(const std::string &name) const
Definition: Unit.cc:116
TTAMachine::Socket
Definition: Socket.hh:53
ProDeTextGenerator::COMP_REGISTER_FILE
@ COMP_REGISTER_FILE
Register file (w/o article).
Definition: ProDeTextGenerator.hh:269
ProDeTextGenerator::TXT_LABEL_NAME
@ TXT_LABEL_NAME
Label for component name widget.
Definition: ProDeTextGenerator.hh:56
RFPortDialog.hh
Machine.hh
MachineTester::isValidComponentName
static bool isValidComponentName(const std::string &name)
Definition: MachineTester.cc:312
TRUE
const string TRUE
Value used for true in attribute and element values.
Definition: GUIOptionsSerializer.cc:65
RFPortDialog::updateSockets
void updateSockets()
Definition: RFPortDialog.cc:250
TTAMachine::Unit::portCount
virtual int portCount() const
Definition: Unit.cc:135
ProDeConstants.hh
GUITextGenerator.hh
MachineTester.hh
ProDeTextGenerator::instance
static ProDeTextGenerator * instance()
Definition: ProDeTextGenerator.cc:382
EVT_BUTTON
EVT_BUTTON(ID_EDIT_ARCH_PORT, FUImplementationDialog::onEditArchitecturePort) EVT_BUTTON(ID_ADD_EXTERNAL_PORT
RegisterFile.hh
TTAMachine::Port::name
virtual std::string name() const
Definition: Port.cc:141
ProDeTextGenerator::TXT_LABEL_OUTPUT_SOCKET
@ TXT_LABEL_OUTPUT_SOCKET
Label for output socket selector.
Definition: ProDeTextGenerator.hh:62
WxConversion.hh
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::RegisterFile
Definition: RegisterFile.hh:47
InformationDialog
Definition: InformationDialog.hh:42
TTAMachine
Definition: Assembler.hh:48
ProDeTextGenerator::COMP_PORT
@ COMP_PORT
Name for port (w/o article).
Definition: ProDeTextGenerator.hh:271
RFPortDialog::onOK
void onOK(wxCommandEvent &event)
Definition: RFPortDialog.cc:135
ProDeConstants::NONE
static const wxString NONE
Constant for "None".
Definition: ProDeConstants.hh:56
MachineTester
Definition: MachineTester.hh:46
WxConversion::toString
static std::string toString(const wxString &source)
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
UserManualCmd.hh
RF
const string RF
Definition: IDFSerializer.cc:68
RFPortDialog::TransferDataToWindow
virtual bool TransferDataToWindow()
Definition: RFPortDialog.cc:215
RFPortDialog::~RFPortDialog
virtual ~RFPortDialog()
Definition: RFPortDialog.cc:94
END_EVENT_TABLE
END_EVENT_TABLE() using namespace IDF
TTAMachine::Socket::INPUT
@ INPUT
Data goes from bus to port.
Definition: Socket.hh:59
GUITextGenerator::TXT_BUTTON_OK
@ TXT_BUTTON_OK
Label for OK button.
Definition: GUITextGenerator.hh:59
RFPortDialog::onSocketChoice
void onSocketChoice(wxCommandEvent &event)
Definition: RFPortDialog.cc:225