OpenASIP  2.0
FUGuardDialog.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 FUGuardDialog.cc
26  *
27  * Implementation of FUGuardDialog class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2004 (vjaaskel-no.spam-cs.tut.fi)
30  */
31 
32 #include <wx/wx.h>
33 #include <wx/statline.h>
34 #include <wx/valgen.h>
35 #include <boost/format.hpp>
36 
37 #include "FUGuardDialog.hh"
38 #include "Guard.hh"
39 #include "WxConversion.hh"
40 #include "Machine.hh"
41 #include "FunctionUnit.hh"
42 #include "FUPort.hh"
43 #include "InformationDialog.hh"
44 #include "WidgetTools.hh"
45 #include "GUITextGenerator.hh"
46 #include "ProDeTextGenerator.hh"
47 #include "ProDeConstants.hh"
48 
49 using std::string;
50 using boost::format;
51 using namespace TTAMachine;
52 
53 BEGIN_EVENT_TABLE(FUGuardDialog, wxDialog)
54  EVT_CHOICE(ID_FU_NAME, FUGuardDialog::onFUChoice)
58 
59 /**
60  * The Constructor.
61  *
62  * @param parent Parent window of the dialog.
63  * @param guard Port guard to edit.
64  */
66  wxWindow* parent,
67  Bus* bus,
68  PortGuard* guard) :
69  wxDialog(parent, -1, _T(""), wxDefaultPosition),
70  inverted_(false),
71  newInverted_(false),
72  port_(NULL),
73  bus_(bus),
74  adding_(false) {
75 
76 
77  if (guard == NULL) {
78  // adding a new guard
79  adding_ = true;
80 
82  bus_->machine()->functionUnitNavigator();
83 
84  for (int i = 0; i < navigator.count(); i++) {
85  if (navigator.item(i)->operationPortCount() > 0) {
86  port_ = navigator.item(i)->operationPort(0);
87  break;
88  }
89  }
90 
91  assert(port_ != NULL);
92 
93  } else {
94  // editing an old guard
95  port_ = guard->port();
96  inverted_ = guard->isInverted();
97  newInverted_ = inverted_;
98  bus_ = guard->parentBus();
99 
100  // The guard is temporarily deleted to simplify legality checks.
101  delete guard;
102  guard = NULL;
103  }
104 
105  createContents(this, true, true);
106 
107  // set pointers to the dialog widgets
108  nameChoice_ = dynamic_cast<wxChoice*>(FindWindow(ID_FU_NAME));
109  portChoice_ = dynamic_cast<wxChoice*>(FindWindow(ID_FU_PORT));
110  invertedBox_ = dynamic_cast<wxCheckBox*>(FindWindow(ID_INVERTED));
111 
112  invertedBox_->SetValidator(wxGenericValidator(&newInverted_));
113 
114  // set widget texts.
115  setTexts();
116 }
117 
118 
119 /**
120  * The Destructor.
121  */
123 }
124 
125 
126 /**
127  * Sets texts for widgets.
128  */
129 void
133 
134  // Dialog title
135  format fmt = prodeTexts->text(
137  SetTitle(WxConversion::toWxString(fmt.str()));
138 
139  // buttons
140  WidgetTools::setLabel(generator, FindWindow(wxID_OK),
142 
143  WidgetTools::setLabel(generator, FindWindow(wxID_CANCEL),
145 
146  WidgetTools::setLabel(generator, FindWindow(ID_HELP),
148 
149  // widget labels
150  WidgetTools::setLabel(prodeTexts, FindWindow(ID_LABEL_NAME),
152 
153  WidgetTools::setLabel(prodeTexts, FindWindow(ID_LABEL_PORT),
155 
156  WidgetTools::setLabel(prodeTexts, FindWindow(ID_INVERTED),
158 }
159 
160 
161 /**
162  * Transfers data from the guard object to the dialog widgets.
163  *
164  * @return true, if the transfer was succesful, false otherwise.
165  */
166 bool
168  nameChoice_->Clear();
169  portChoice_->Clear();
170 
172  bus_->machine()->functionUnitNavigator();
173 
174  // add function units and set selection
175  for (int i = 0; i < navigator.count(); i++) {
176  wxString name = WxConversion::toWxString(navigator.item(i)->name());
177  nameChoice_->Append(name);
178  }
179  nameChoice_->SetStringSelection(
180  WxConversion::toWxString(port_->parentUnit()->name()));
181 
182  // add function unit ports and set selection
183  for (int i = 0; i < port_->parentUnit()->portCount(); i++) {
184 
185  // port must be output port
186  if (port_->parentUnit()->port(i)->outputSocket() == NULL) {
187  continue;
188  }
189 
190  wxString name =
191  WxConversion::toWxString(port_->parentUnit()->port(i)->name());
192 
193  portChoice_->Append(name);
194  }
195  portChoice_->SetStringSelection(WxConversion::toWxString(port_->name()));
196 
197  // set inverted checkbox
198  invertedBox_->SetValue(newInverted_);
199 
200  return wxDialog::TransferDataToWindow();
201 }
202 
203 
204 /**
205  * Updates the port choice when the function unit selection is changed.
206  */
207 void
208 FUGuardDialog::onFUChoice(wxCommandEvent&) {
209 
210  wxString selection = portChoice_->GetStringSelection();
211 
212  portChoice_->Clear();
213 
214  FunctionUnit* fu = selectedFU();
215  for (int i = 0; i < fu->portCount(); i++) {
216  wxString name = WxConversion::toWxString(fu->port(i)->name());
217  portChoice_->Append(name);
218  }
219 
220  if (portChoice_->FindString(selection) >= 0) {
221  portChoice_->SetStringSelection(selection);
222  } else {
223  portChoice_->SetSelection(0);
224  }
225 }
226 
227 
228 /**
229  * Returns a pointer to the selected function unit.
230  *
231  * @return Pointer to the selected function unit.
232  */
235  string name = WxConversion::toString(nameChoice_->GetStringSelection());
237  bus_->machine()->functionUnitNavigator();
238  FunctionUnit* fu = navigator.item(name);
239  return fu;
240 }
241 
242 
243 /**
244  * Returns a pointer to the selected function unit port.
245  *
246  * @return Pointer to the selected function unit port.
247  */
248 FUPort*
250  string name = WxConversion::toString(portChoice_->GetStringSelection());
251 
252  if (name == "") {
253  return NULL;
254  }
255 
256  FUPort* port = selectedFU()->operationPort(name);
257  return port;
258 }
259 
260 /**
261  * Updates the guard object when the OK-button is pressed.
262  *
263  * Closes the dialog.
264  */
265 void
266 FUGuardDialog::onOK(wxCommandEvent&) {
267  FUPort* port = selectedPort();
268  if (port == NULL) {
270  format message = prodeTexts->text(ProDeTextGenerator::MSG_ERROR);
271  InformationDialog dialog(
273  message.str() + "Select a port.\n"));
274  dialog.ShowModal();
275  return;
276  }
277 
278  TransferDataFromWindow();
279 
280  try {
281  new PortGuard(newInverted_, *port, *bus_);
282  } catch (ComponentAlreadyExists& e) {
284  format message =
286  InformationDialog dialog(
287  this, WxConversion::toWxString(message.str()));
288  dialog.ShowModal();
289  return;
290  }
291  EndModal(wxID_OK);
292 }
293 
294 
295 /**
296  * Cancels the dialog effects by creating the original port guard.
297  *
298  * Closes the Dialog.
299  */
300 void
301 FUGuardDialog::onCancel(wxCommandEvent&) {
302  if (adding_ == false) {
303  new PortGuard(inverted_, *port_, *bus_);
304  }
305  EndModal(wxID_CANCEL);
306 }
307 
308 
309 /**
310  * Creates contents of the dialog window.
311  *
312  * Code generated with wxDesigner.
313  *
314  * @param parent Parent dialog of the contents.
315  * @param call_fit If true, fits sizer in dialog window.
316  * @param set_sizer If true, sets sizer as dialog's sizer.
317  * @return Top level sizer of the contents.
318  */
319 wxSizer*
321  wxWindow *parent, bool call_fit, bool set_sizer) {
322 
323  wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
324 
325  wxGridSizer *item1 = new wxGridSizer( 2, 0, 0 );
326 
327  wxStaticText *item2 = new wxStaticText( parent, ID_LABEL_NAME, wxT("Function Unit Name:"), wxDefaultPosition, wxDefaultSize, 0 );
328  item1->Add( item2, 0, wxALL, 5 );
329 
330  wxString *strs3 = (wxString*) NULL;
331  wxChoice *item3 = new wxChoice( parent, ID_FU_NAME, wxDefaultPosition, wxSize(100,-1), 0, strs3, 0 );
332  item1->Add( item3, 0, wxGROW|wxALL, 5 );
333 
334  wxStaticText *item4 = new wxStaticText( parent, ID_LABEL_PORT, wxT("Port Name:"), wxDefaultPosition, wxDefaultSize, 0 );
335  item1->Add( item4, 0, wxALL, 5 );
336 
337  wxString *strs5 = (wxString*) NULL;
338  wxChoice *item5 = new wxChoice( parent, ID_FU_PORT, wxDefaultPosition, wxSize(100,-1), 0, strs5, 0 );
339  item1->Add( item5, 0, wxGROW|wxALL, 5 );
340 
341  item0->Add( item1, 0, wxALIGN_CENTER|wxALL, 5 );
342 
343  wxCheckBox *item6 = new wxCheckBox( parent, ID_INVERTED, wxT("Inverted"), wxDefaultPosition, wxDefaultSize, 0 );
344  item0->Add( item6, 0, wxALL, 5 );
345 
346  wxStaticLine *item7 = new wxStaticLine( parent, ID_LINE, wxDefaultPosition, wxSize(20,-1), wxLI_HORIZONTAL );
347  item0->Add( item7, 0, wxGROW|wxALL, 5 );
348 
349  wxBoxSizer *item8 = new wxBoxSizer( wxHORIZONTAL );
350 
351  wxButton *item9 = new wxButton( parent, ID_HELP, wxT("&Help"), wxDefaultPosition, wxDefaultSize, 0 );
352  item8->Add( item9, 0, wxALIGN_CENTER|wxALL, 5 );
353 
354  wxButton *item10 = new wxButton( parent, wxID_OK, wxT("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
355  item8->Add( item10, 0, wxALIGN_CENTER|wxALL, 5 );
356 
357  wxButton *item11 = new wxButton( parent, wxID_CANCEL, wxT("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
358  item8->Add( item11, 0, wxALIGN_CENTER|wxALL, 5 );
359 
360  item0->Add( item8, 0, wxALIGN_CENTER|wxALL, 5 );
361 
362  if (set_sizer)
363  {
364  parent->SetSizer( item0 );
365  if (call_fit)
366  item0->SetSizeHints( parent );
367  }
368 
369  return item0;
370 }
FUGuardDialog::createContents
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
Definition: FUGuardDialog.cc:320
WxConversion::toWxString
static wxString toWxString(const std::string &source)
FUGuardDialog::selectedFU
TTAMachine::FunctionUnit * selectedFU() const
Definition: FUGuardDialog.cc:234
WidgetTools::setLabel
static void setLabel(Texts::TextGenerator *generator, wxWindow *widget, int textID)
Definition: WidgetTools.cc:92
TTAMachine::Bus
Definition: Bus.hh:53
GUITextGenerator::instance
static GUITextGenerator * instance()
Definition: GUITextGenerator.cc:67
GUITextGenerator
Definition: GUITextGenerator.hh:46
GUITextGenerator::TXT_BUTTON_HELP
@ TXT_BUTTON_HELP
Label for help button.
Definition: GUITextGenerator.hh:60
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
ProDeTextGenerator.hh
ProDeTextGenerator
Definition: ProDeTextGenerator.hh:49
ProDeTextGenerator::MSG_ERROR_GUARD_EXISTS
@ MSG_ERROR_GUARD_EXISTS
Error: Equal guard exists.
Definition: ProDeTextGenerator.hh:242
assert
#define assert(condition)
Definition: Application.hh:86
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
TTAMachine::FUPort
Definition: FUPort.hh:46
GUITextGenerator::TXT_BUTTON_CANCEL
@ TXT_BUTTON_CANCEL
Label for cancel button.
Definition: GUITextGenerator.hh:55
InformationDialog.hh
FUGuardDialog.hh
ProDeTextGenerator::TXT_FU_GUARD_DIALOG_TITLE
@ TXT_FU_GUARD_DIALOG_TITLE
Function unit guard dialog title.
Definition: ProDeTextGenerator.hh:197
Guard.hh
FUGuardDialog
Definition: FUGuardDialog.hh:47
Machine.hh
TTAMachine::Unit::portCount
virtual int portCount() const
Definition: Unit.cc:135
FUGuardDialog::setTexts
void setTexts()
Definition: FUGuardDialog.cc:130
ProDeConstants.hh
GUITextGenerator.hh
FUGuardDialog::onFUChoice
void onFUChoice(wxCommandEvent &event)
Definition: FUGuardDialog.cc:208
FUGuardDialog::TransferDataToWindow
virtual bool TransferDataToWindow()
Definition: FUGuardDialog.cc:167
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
ProDeTextGenerator::MSG_ERROR
@ MSG_ERROR
Text 'Error' and new line.
Definition: ProDeTextGenerator.hh:222
FUGuardDialog::onCancel
void onCancel(wxCommandEvent &event)
Definition: FUGuardDialog.cc:301
ProDeTextGenerator::TXT_LABEL_INVERTED
@ TXT_LABEL_INVERTED
Label for inverted checkbox.
Definition: ProDeTextGenerator.hh:85
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
FUGuardDialog::~FUGuardDialog
virtual ~FUGuardDialog()
Definition: FUGuardDialog.cc:122
TTAMachine::Port::name
virtual std::string name() const
Definition: Port.cc:141
FUPort.hh
ComponentAlreadyExists
Definition: Exception.hh:510
FUGuardDialog::selectedPort
TTAMachine::FUPort * selectedPort() const
Definition: FUGuardDialog.cc:249
WxConversion.hh
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::PortGuard
Definition: Guard.hh:99
InformationDialog
Definition: InformationDialog.hh:42
TTAMachine
Definition: Assembler.hh:48
ProDeTextGenerator::TXT_LABEL_PORT_NAME
@ TXT_LABEL_PORT_NAME
Label for port name.
Definition: ProDeTextGenerator.hh:84
ProDeTextGenerator::TXT_LABEL_FU_NAME
@ TXT_LABEL_FU_NAME
Label for function unit name.
Definition: ProDeTextGenerator.hh:83
WxConversion::toString
static std::string toString(const wxString &source)
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
FUGuardDialog::onOK
void onOK(wxCommandEvent &event)
Definition: FUGuardDialog.cc:266
END_EVENT_TABLE
END_EVENT_TABLE() using namespace IDF
FunctionUnit.hh
GUITextGenerator::TXT_BUTTON_OK
@ TXT_BUTTON_OK
Label for OK button.
Definition: GUITextGenerator.hh:59