OpenASIP  2.0
SRPortDialog.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 SRPortDialog.cc
26  *
27  * Definition of SRPortDialog class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2005 (vjaaskel-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2021
31  * @note rating: red
32  */
33 
34 #include <string>
35 #include <wx/wx.h>
36 #include <wx/statline.h>
37 #include <wx/spinctrl.h>
38 #include <wx/valgen.h>
39 #include <boost/format.hpp>
40 
41 #include "Application.hh"
42 #include "SpecialRegisterPort.hh"
43 #include "SRPortDialog.hh"
44 #include "WxConversion.hh"
45 #include "Conversion.hh"
46 #include "ModelConstants.hh"
47 #include "ProDeConstants.hh"
48 #include "ProDeTextGenerator.hh"
49 #include "Socket.hh"
50 #include "Machine.hh"
51 #include "MachineTester.hh"
52 #include "WarningDialog.hh"
53 #include "UserManualCmd.hh"
54 #include "InformationDialog.hh"
55 #include "GUITextGenerator.hh"
56 #include "WidgetTools.hh"
57 #include "ControlUnit.hh"
58 
59 using boost::format;
60 using std::string;
61 using namespace TTAMachine;
62 
63 BEGIN_EVENT_TABLE(SRPortDialog, wxDialog)
64  EVT_TEXT(ID_NAME, SRPortDialog::onName)
67  EVT_CHOICE(ID_INPUT_SOCKET, SRPortDialog::onSocketChoice)
68  EVT_CHOICE(ID_OUTPUT_SOCKET, SRPortDialog::onSocketChoice)
70 
71 
72 /**
73  * The Constructor.
74  *
75  * @param parent Parent window of the dialog.
76  * @param port Port to modify.
77  */
79  wxWindow* parent,
80  SpecialRegisterPort* port):
81  wxDialog(parent, -1, _T(""), wxDefaultPosition),
82  port_(port),
83  name_(_T("")),
85  inputSocketChoice_(NULL),
86  outputSocketChoice_(NULL) {
87 
88  oldInput_ = port_->inputSocket();
89  oldOutput_ = port_->outputSocket();
90 
91  createContents(this, true, true);
92 
93  inputSocketChoice_ =
94  dynamic_cast<wxChoice*>(FindWindow(ID_INPUT_SOCKET));
95  outputSocketChoice_ =
96  dynamic_cast<wxChoice*>(FindWindow(ID_OUTPUT_SOCKET));
97 
98  FindWindow(ID_NAME)->SetValidator(wxTextValidator(wxFILTER_ASCII, &name_));
99  FindWindow(ID_WIDTH)->SetValidator(wxGenericValidator(&width_));
100 
101  FindWindow(wxID_OK)->Disable();
102 
103  // set texts to widgets
104  setTexts();
105 
106  TransferDataToWindow();
107 }
108 
109 /**
110  * The Destructor.
111  */
113 }
114 
115 
116 /**
117  * Sets texts for widgets.
118  */
119 void
123 
124  // Dialog title
125  format fmt = prodeTexts->text(
127  SetTitle(WxConversion::toWxString(fmt.str()));
128 
129  // buttons
130  WidgetTools::setLabel(generator, FindWindow(wxID_OK),
132 
133  WidgetTools::setLabel(generator, FindWindow(wxID_CANCEL),
135 
136  WidgetTools::setLabel(generator, FindWindow(ID_HELP),
138 
139  // widget labels
140  WidgetTools::setLabel(prodeTexts, FindWindow(ID_LABEL_NAME),
142 
143  WidgetTools::setLabel(prodeTexts, FindWindow(ID_LABEL_WIDTH),
145 
146  WidgetTools::setLabel(prodeTexts, FindWindow(ID_LABEL_INPUT_SOCKET),
148 
149  WidgetTools::setLabel(prodeTexts, FindWindow(ID_LABEL_OUTPUT_SOCKET),
151 
152 }
153 
154 
155 /**
156  * Transfers data from the port object to the dialog widgets.
157  *
158  * @return False, if an error occured in the transfer.
159 */
160 bool
162 
163  name_ = WxConversion::toWxString(port_->name());
164  width_ = port_->width();
165  updateSockets();
166 
167  // wxWidgets GTK1 version seems to bug with spincontrol and
168  // checkbox validators. The widget value has to be set manually.
169  dynamic_cast<wxSpinCtrl*>(FindWindow(ID_WIDTH))->SetValue(width_);
170 
171  return wxWindow::TransferDataToWindow();
172 }
173 
174 
175 /**
176  * Updates input and output socket choicers.
177  */
178 void
180 
181  inputSocketChoice_->Clear();
182  inputSocketChoice_->Append(ProDeConstants::NONE);
183  outputSocketChoice_->Clear();
184  outputSocketChoice_->Append(ProDeConstants::NONE);
185 
186  MachineTester tester(*(port_->parentUnit()->machine()));
187 
188  // Add ports to the choicers
189  Machine::SocketNavigator navigator =
190  port_->parentUnit()->machine()->socketNavigator();
191 
192  for (int i = 0; i < navigator.count(); i++) {
193  Socket* socket = navigator.item(i);
194  wxString socketName = WxConversion::toWxString(socket->name());
195 
196  // Add available input sockets.
197  Socket* input = port_->inputSocket();
198  if (input != NULL) {
199  port_->detachSocket(*input);
200  }
201  bool legal = tester.canConnect(*socket, *port_);
202  if (legal && socket->direction() == Socket::INPUT) {
203  inputSocketChoice_->Append(socketName);
204  }
205  if (input != NULL) {
206  port_->attachSocket(*input);
207  }
208 
209  // Add available output sockets.
210  Socket* output = port_->outputSocket();
211  if (output != NULL) {
212  port_->detachSocket(*output);
213  }
214  legal = tester.canConnect(*socket, *port_);
215  if (legal && socket->direction() == Socket::OUTPUT) {
216  outputSocketChoice_->Append(socketName);
217  }
218  if (output != NULL) {
219  port_->attachSocket(*output);
220  }
221  // ignore sockets with unknown direction
222  }
223 
224  // set input socket choice
225  if (port_->inputSocket() == NULL) {
226  inputSocketChoice_->SetStringSelection(ProDeConstants::NONE);
227  } else {
228  wxString socketName =
229  WxConversion::toWxString(port_->inputSocket()->name());
230  inputSocketChoice_->SetStringSelection(socketName);
231  }
232 
233  // set output socket choice
234  if (port_->outputSocket() == NULL) {
235  outputSocketChoice_->SetStringSelection(ProDeConstants::NONE);
236  } else {
237  wxString socketName =
238  WxConversion::toWxString(port_->outputSocket()->name());
239  outputSocketChoice_->SetStringSelection(socketName);
240  }
241 }
242 
243 
244 /**
245  * Resets the original input and output sockets for the port and closes
246  * the dialog.
247  */
248 void
249 SRPortDialog::onCancel(wxCommandEvent&) {
250  port_->detachAllSockets();
251  if (oldInput_ != NULL) {
252  port_->attachSocket(*oldInput_);
253  }
254  if (oldOutput_ != NULL) {
255  port_->attachSocket(*oldOutput_);
256  }
257  EndModal(wxID_CANCEL);
258 }
259 
260 
261 /**
262  * Validates input in the controls, and updates the port object.
263  */
264 void
265 SRPortDialog::onOK(wxCommandEvent&) {
266 
267  if (!Validate()) {
268  return;
269  }
270 
271  if (!TransferDataFromWindow()) {
272  return;
273  }
274 
275  // check whether FU already has a port of tht name.
276 
277  string trimmedName =
278  WxConversion::toString(name_.Trim(false).Trim(true));
279 
280  // Check the name validity.
281  if (!MachineTester::isValidComponentName(trimmedName)) {
283  format message =
285  InformationDialog warning(
286  this, WxConversion::toWxString(message.str()));
287  warning.ShowModal();
288  return;
289  }
290 
291  if (port_->name() != trimmedName) {
292 
293  // TODO: Remove dyanmic_cast and assert when MOM return type is
294  // fixed.
295  ControlUnit* gcu = dynamic_cast<ControlUnit*>(port_->parentUnit());
296  assert(gcu != NULL);
297 
298  for (int i = 0; i < gcu->portCount(); i++) {
299  string name = gcu->port(i)->name();
300  if (name == WxConversion::toString(name_)) {
301  ProDeTextGenerator* prodeTexts =
303  format message =
305  format a_port =
307  format machine =
309  format port =
310  prodeTexts->text(ProDeTextGenerator::COMP_PORT);
311  message % trimmedName % a_port.str() % machine.str() %
312  port.str();
313  WarningDialog warning(
314  this, WxConversion::toWxString(message.str()));
315  warning.ShowModal();
316  return;
317  }
318  }
319  }
320 
321  // update attributes
322  port_->setName(trimmedName);
323  port_->setWidth(width_);
324 
325  EndModal(wxID_OK);
326 }
327 
328 /**
329  * Updates the port object when user changes input/output socket selection.
330  */
331 void
333  port_->detachAllSockets();
334  Machine::SocketNavigator navigator =
335  port_->parentUnit()->machine()->socketNavigator();
336 
337  // set input socket
338  string inputSocketName =
339  WxConversion::toString(inputSocketChoice_->GetStringSelection());
340  if (inputSocketName != WxConversion::toString(ProDeConstants::NONE)) {
341  port_->attachSocket(*(navigator.item(inputSocketName)));
342  }
343 
344  // set output socket
345  string outputSocketName =
346  WxConversion::toString(outputSocketChoice_->GetStringSelection());
347  if (outputSocketName != WxConversion::toString(ProDeConstants::NONE)) {
348  port_->attachSocket(*(navigator.item(outputSocketName)));
349  }
350  updateSockets();
351 }
352 
353 
354 /**
355  * Disables OK-button if the name field is empty.
356  */
357 void
358 SRPortDialog::onName(wxCommandEvent&) {
359  if (!TransferDataFromWindow()) {
360  assert(false);
361  }
362  wxString trimmedName = name_.Trim(false).Trim(true);
363  if (trimmedName == _T("")) {
364  FindWindow(wxID_OK)->Disable();
365  } else {
366  FindWindow(wxID_OK)->Enable();
367  }
368 }
369 
370 
371 /**
372  * Creates contents of the dialog window. Initially generated with
373  * wxDesigner, the code will be cleaned up later.
374  *
375  * @param parent Parent dialog of the contents.
376  * @param call_fit If true, fits sizer in dialog window.
377  * @param set_sizer If true, sets sizer as dialog's sizer.
378  * @return Top level sizer of the contents.
379  */
380 wxSizer*
382  wxWindow *parent, bool call_fit, bool set_sizer) {
383 
384  wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
385 
386  wxFlexGridSizer *item1 = new wxFlexGridSizer( 2, 0, 0 );
387 
388  wxStaticText *item2 = new wxStaticText( parent, ID_LABEL_NAME, wxT("Name:"), wxDefaultPosition, wxDefaultSize, 0 );
389  item1->Add( item2, 0, wxALIGN_RIGHT|wxALL, 5 );
390 
391  wxTextCtrl *item3 = new wxTextCtrl( parent, ID_NAME, wxT(""), wxDefaultPosition, wxSize(200,-1), 0 );
392  item1->Add( item3, 0, wxALIGN_CENTER|wxALL, 5 );
393 
394  wxStaticText *item4 = new wxStaticText( parent, ID_LABEL_WIDTH, wxT("Width:"), wxDefaultPosition, wxDefaultSize, 0 );
395  item1->Add( item4, 0, wxALIGN_RIGHT|wxALL, 5 );
396 
397  wxSpinCtrl *item5 = new wxSpinCtrl( parent, ID_WIDTH, wxT("1"), wxDefaultPosition, wxSize(-1,-1), 0, 1, 10000, 1 );
398  item1->Add( item5, 0, wxALIGN_CENTER|wxALL, 5 );
399 
400  wxStaticText *item6 = new wxStaticText( parent, ID_LABEL_INPUT_SOCKET, wxT("Input Socket:"), wxDefaultPosition, wxDefaultSize, 0 );
401  item1->Add( item6, 0, wxALIGN_RIGHT|wxALL, 5 );
402 
403  wxString strs7[] =
404  {
405  wxT("NONE")
406  };
407  wxChoice *item7 = new wxChoice( parent, ID_INPUT_SOCKET, wxDefaultPosition, wxSize(200,-1), 1, strs7, 0 );
408  item1->Add( item7, 0, wxALIGN_CENTER|wxALL, 5 );
409 
410  wxStaticText *item8 = new wxStaticText( parent, ID_LABEL_OUTPUT_SOCKET, wxT("Output Socket:"), wxDefaultPosition, wxDefaultSize, 0 );
411  item1->Add( item8, 0, wxALIGN_RIGHT|wxALL, 5 );
412 
413  wxString strs9[] =
414  {
415  wxT("NONE")
416  };
417  wxChoice *item9 = new wxChoice( parent, ID_OUTPUT_SOCKET, wxDefaultPosition, wxSize(200,-1), 1, strs9, 0 );
418  item1->Add( item9, 0, wxALIGN_CENTER|wxALL, 5 );
419 
420  item0->Add( item1, 0, wxALIGN_CENTER|wxALL, 5 );
421 
422  wxStaticLine *item10 = new wxStaticLine( parent, ID_LINE, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
423  item0->Add( item10, 0, wxGROW|wxALL, 5 );
424 
425  wxBoxSizer *item11 = new wxBoxSizer( wxHORIZONTAL );
426 
427  wxButton *item12 = new wxButton( parent, ID_HELP, wxT("&Help"), wxDefaultPosition, wxDefaultSize, 0 );
428  item11->Add( item12, 0, wxALIGN_CENTER|wxALL, 5 );
429 
430  wxButton *item13 = new wxButton( parent, wxID_OK, wxT("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
431  item11->Add( item13, 0, wxALIGN_CENTER|wxALL, 5 );
432 
433  wxButton *item14 = new wxButton( parent, wxID_CANCEL, wxT("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
434  item11->Add( item14, 0, wxALIGN_CENTER|wxALL, 5 );
435 
436  item0->Add( item11, 0, wxALIGN_CENTER|wxALL, 5 );
437 
438  if (set_sizer)
439  {
440  parent->SetSizer( item0 );
441  if (call_fit)
442  item0->SetSizeHints( parent );
443  }
444 
445  return item0;
446 }
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
SRPortDialog::onSocketChoice
void onSocketChoice(wxCommandEvent &event)
Definition: SRPortDialog.cc:332
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
DEFAULT_WIDTH
const int DEFAULT_WIDTH
Default window width.
Definition: GUIOptionsSerializer.cc:98
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
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
ModelConstants
Definition: ModelConstants.hh:40
WidgetTools.hh
TTAMachine::FunctionUnit::port
virtual BaseFUPort * port(const std::string &name) const
Definition: FunctionUnit.cc:145
FindWindow
Definition: FindWindow.hh:49
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
SRPortDialog::onCancel
void onCancel(wxCommandEvent &event)
Definition: SRPortDialog.cc:249
SRPortDialog::~SRPortDialog
~SRPortDialog()
Definition: SRPortDialog.cc:112
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
ProDeTextGenerator::COMP_FUNCTION_UNIT
@ COMP_FUNCTION_UNIT
Name for FU (w/o article).
Definition: ProDeTextGenerator.hh:263
SRPortDialog::updateSockets
void updateSockets()
Definition: SRPortDialog.cc:179
assert
#define assert(condition)
Definition: Application.hh:86
GUITextGenerator::TXT_BUTTON_CANCEL
@ TXT_BUTTON_CANCEL
Label for cancel button.
Definition: GUITextGenerator.hh:55
TTAMachine::SpecialRegisterPort
Definition: SpecialRegisterPort.hh:48
WarningDialog.hh
TTAMachine::ControlUnit
Definition: ControlUnit.hh:50
Conversion.hh
InformationDialog.hh
ProDeTextGenerator::COMP_A_PORT
@ COMP_A_PORT
Name for port (w/ article).
Definition: ProDeTextGenerator.hh:270
Application.hh
SRPortDialog
Definition: SRPortDialog.hh:44
TTAMachine::Socket
Definition: Socket.hh:53
ProDeTextGenerator::TXT_LABEL_NAME
@ TXT_LABEL_NAME
Label for component name widget.
Definition: ProDeTextGenerator.hh:56
SRPortDialog::onOK
void onOK(wxCommandEvent &event)
Definition: SRPortDialog.cc:265
ModelConstants.hh
Machine.hh
MachineTester::isValidComponentName
static bool isValidComponentName(const std::string &name)
Definition: MachineTester.cc:312
SRPortDialog::setTexts
void setTexts()
Definition: SRPortDialog.cc:120
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
SRPortDialog::createContents
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
Definition: SRPortDialog.cc:381
EVT_BUTTON
EVT_BUTTON(ID_EDIT_ARCH_PORT, FUImplementationDialog::onEditArchitecturePort) EVT_BUTTON(ID_ADD_EXTERNAL_PORT
SRPortDialog::TransferDataToWindow
virtual bool TransferDataToWindow()
Definition: SRPortDialog.cc:161
SRPortDialog.hh
ProDeTextGenerator::TXT_SR_PORT_DIALOG_TITLE
@ TXT_SR_PORT_DIALOG_TITLE
Special reg. port dialog title.
Definition: ProDeTextGenerator.hh:193
TTAMachine::Port::name
virtual std::string name() const
Definition: Port.cc:141
ControlUnit.hh
SpecialRegisterPort.hh
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
InformationDialog
Definition: InformationDialog.hh:42
SRPortDialog::onName
void onName(wxCommandEvent &event)
Definition: SRPortDialog.cc:358
TTAMachine
Definition: Assembler.hh:48
ProDeTextGenerator::COMP_PORT
@ COMP_PORT
Name for port (w/o article).
Definition: ProDeTextGenerator.hh:271
ProDeConstants::NONE
static const wxString NONE
Constant for "None".
Definition: ProDeConstants.hh:56
MachineTester
Definition: MachineTester.hh:46
ProDeTextGenerator::TXT_LABEL_WIDTH
@ TXT_LABEL_WIDTH
Label for bit width widget.
Definition: ProDeTextGenerator.hh:57
WxConversion::toString
static std::string toString(const wxString &source)
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
UserManualCmd.hh
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