OpenASIP  2.0
BlockImplementationFileDialog.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 BlockImplementationFileDialog.cc
26  *
27  * Implementation of BlockImplementationFileDialog class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2006 (vjaaskel-no.spam-cs.tut.fi)
30  * @author Vinogradov Viacheslav(added Verilog generating) 2012
31  * @note rating: red
32  */
33 
34 #include <wx/statline.h>
35 #include <wx/valgen.h>
37 #include "WxConversion.hh"
38 #include "ErrorDialog.hh"
40 #include "FileSystem.hh"
41 #include "HDBManager.hh"
42 #include "HDBEditor.hh"
43 #include "HDBEditorMainFrame.hh"
44 
45 #if wxCHECK_VERSION(3, 0, 0)
46  #define wxOPEN wxFD_OPEN
47  #define wxFILE_MUST_EXIST wxFD_FILE_MUST_EXIST
48 #endif
49 
50 using namespace HDB;
51 
52 BEGIN_EVENT_TABLE(BlockImplementationFileDialog, wxDialog)
56 
57 /**
58  * The Constructor.
59  *
60  * @param parent Parent window of the dialog.
61  * @param id Window identifier for the dialog window.
62  * @param file BlockImplementationFile object to modify.
63  */
65  wxWindow* parent, wxWindowID id, HDB::BlockImplementationFile& file) :
66  wxDialog(parent, id, _T("Parameter")),
67  file_(file) {
68 
69  createContents(this, true, true);
70 
71  path_ = WxConversion::toWxString(file_.pathToFile());
72  format_ = file_.format();
73 
74  FindWindow(ID_PATH)->SetValidator(wxTextValidator(wxFILTER_ASCII, &path_));
75  FindWindow(ID_FORMAT)->SetValidator(wxGenericValidator(&format_));
76 
77  TransferDataToWindow();
78 }
79 
80 
81 /**
82  * The Destructor.
83  */
85 }
86 
87 /**
88  * Event handler for the Browse-button.
89  *
90  * Displays a wxFileDialog for selecting the implementation file.
91  */
92 void
94 
95  const HDBManager& manager = *wxGetApp().mainFrame().hdbManager();
96  std::string hdbPath = manager.fileName();
97  wxString defaultDir = WxConversion::toWxString(
98  FileSystem::directoryOfPath(manager.fileName()).c_str());
99 
100  wxFileDialog dialog(
101  this, _T("Choose a file"), defaultDir, _T(""),
102  _T("VHDL files (*.vhd;*.vhdl)|*.vhd;*.vhdl|Verilog files (*.v)|*.v| All files (*.*)|*.*"),
103  (wxOPEN | wxFILE_MUST_EXIST));
104 
105  wxChoice *hdl_type = (wxChoice *)FindWindow(ID_FORMAT);
106  dialog.SetFilterIndex(hdl_type->GetCurrentSelection());
107 
108  if (dialog.ShowModal() == wxID_OK) {
109 
110  std::string filename = WxConversion::toString(dialog.GetPath());
111  std::string vhdlPath = FileSystem::directoryOfPath(filename);
112  std::vector<std::string> vhdlPaths = Environment::vhdlPaths(hdbPath);
113 
114  for (std::vector<std::string>::iterator it = vhdlPaths.begin();
115  it != vhdlPaths.end(); ++it) {
116  // check if VHDL path is relative to one of the search paths.
117  if (vhdlPath.substr(0, (*it).length()) == *it) {
118 
119  filename = filename.substr((*it).length() + 1);
120  break;
121  }
122  }
123 
124  path_ = WxConversion::toWxString(filename);
125  TransferDataToWindow();
126  }
127 }
128 
129 
130 /**
131  * Event handler for the dialog OK-button.
132  */
133 void
135 
136  TransferDataFromWindow();
137 
138  path_ = path_.Trim(true).Trim(false);
139 
140  if (path_.IsEmpty()) {
141  wxString message = _T("Path field must not be empty.");
142  ErrorDialog dialog(this, message);
143  dialog.ShowModal();
144  return;
145  }
146 
147  // Attempt to find the file in search paths
148  const HDBManager& manager = *wxGetApp().mainFrame().hdbManager();
149 
150  std::string hdbPath = manager.fileName();
151  std::vector<std::string> vhdlPaths =
152  Environment::vhdlPaths(hdbPath);
153  try {
154  std::string filename =
156  vhdlPaths, WxConversion::toString(path_));
157 
158  for (std::vector<std::string>::iterator it = vhdlPaths.begin();
159  it != vhdlPaths.end(); ++it) {
160  if (filename.substr(0, (*it).length()) == *it) {
162  << filename << " in search path " << *it << std::endl;
163  filename = filename.substr((*it).length() + 1);
164  path_ = WxConversion::toWxString(filename);
165  break;
166  }
167  }
168  } catch (FileNotFound f) {
169  wxString message = _T("File not found from the VHDL search paths.");
170  ErrorDialog dialog(this, message);
171  dialog.ShowModal();
172  return;
173  }
174 
175  file_.setPathToFile(WxConversion::toString(path_));
176  file_.setFormat((HDB::BlockImplementationFile::Format)format_);
177 
178  EndModal(wxID_OK);
179 }
180 
181 /**
182  * Creates the dialog contents.
183  */
184 wxSizer*
186  wxWindow *parent, bool call_fit, bool set_sizer) {
187 
188  wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
189 
190  wxFlexGridSizer *item1 = new wxFlexGridSizer( 3, 0, 0 );
191 
192  wxStaticText *item2 = new wxStaticText( parent, ID_LABEL_PATH, wxT("Path:"), wxDefaultPosition, wxDefaultSize, 0 );
193  item1->Add( item2, 0, wxALIGN_RIGHT|wxALL, 5 );
194 
195  wxTextCtrl *item3 = new wxTextCtrl( parent, ID_PATH, wxT(""), wxDefaultPosition, wxSize(200,-1), 0 );
196  item1->Add( item3, 0, wxALIGN_CENTER|wxALL, 5 );
197 
198  wxButton *item4 = new wxButton( parent, ID_BROWSE, wxT("Browse..."), wxDefaultPosition, wxDefaultSize, 0 );
199  item1->Add( item4, 0, wxALIGN_CENTER|wxALL, 5 );
200 
201  wxStaticText *item5 = new wxStaticText( parent, ID_LABEL_FORMAT, wxT("Format:"), wxDefaultPosition, wxDefaultSize, 0 );
202  item1->Add( item5, 0, wxALIGN_RIGHT|wxALL, 5 );
203 
204  wxString strs6[] =
205  {
206  wxT("VHDL"),
207  wxT("Verilog")
208  };
209  wxChoice *item6 = new wxChoice( parent, ID_FORMAT, wxDefaultPosition, wxSize(100,-1), 2, strs6, 0 );
210  item1->Add( item6, 0, wxGROW|wxALL, 5 );
211 
212  item0->Add( item1, 0, wxALIGN_CENTER|wxALL, 5 );
213 
214  wxStaticLine *item7 = new wxStaticLine( parent, ID_LINE, wxDefaultPosition, wxSize(20,-1), wxLI_HORIZONTAL );
215  item0->Add( item7, 0, wxGROW|wxALL, 5 );
216 
217  wxBoxSizer *item8 = new wxBoxSizer( wxHORIZONTAL );
218 
219  wxButton *item9 = new wxButton( parent, wxID_CANCEL, wxT("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
220  item8->Add( item9, 0, wxALL, 5 );
221 
222  wxButton *item10 = new wxButton( parent, wxID_OK, wxT("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
223  item8->Add( item10, 0, wxALIGN_CENTER|wxALL, 5 );
224 
225  item0->Add( item8, 0, wxALIGN_CENTER|wxALL, 5 );
226 
227  if (set_sizer)
228  {
229  parent->SetSizer( item0 );
230  if (call_fit)
231  item0->SetSizeHints( parent );
232  }
233 
234  return item0;
235 }
FileSystem.hh
WxConversion::toWxString
static wxString toWxString(const std::string &source)
FileNotFound
Definition: Exception.hh:224
HDB
Definition: CostDatabase.hh:49
BlockImplementationFileDialog::onBrowse
void onBrowse(wxCommandEvent &event)
Definition: BlockImplementationFileDialog.cc:93
HDBEditor.hh
HDB::BlockImplementationFile::Format
Format
Format of the file.
Definition: BlockImplementationFile.hh:47
BlockImplementationFileDialog::onOK
void onOK(wxCommandEvent &event)
Definition: BlockImplementationFileDialog.cc:134
FindWindow
Definition: FindWindow.hh:49
Application::logStream
static std::ostream & logStream()
Definition: Application.cc:155
HDB::BlockImplementationFile
Definition: BlockImplementationFile.hh:44
ErrorDialog
Definition: ErrorDialog.hh:42
ErrorDialog.hh
HDBEditorMainFrame.hh
BlockImplementationFileDialog::createContents
wxSizer * createContents(wxWindow *parent, bool call_fit, bool set_sizer)
Creates the dialog contents.
Definition: BlockImplementationFileDialog.cc:185
FileSystem::directoryOfPath
static std::string directoryOfPath(const std::string fileName)
Definition: FileSystem.cc:79
HDB::HDBManager
Definition: HDBManager.hh:82
BlockImplementationFileDialog::~BlockImplementationFileDialog
virtual ~BlockImplementationFileDialog()
Definition: BlockImplementationFileDialog.cc:84
HDB::HDBManager::fileName
std::string fileName() const
Definition: HDBManager.cc:612
EVT_BUTTON
EVT_BUTTON(ID_EDIT_ARCH_PORT, FUImplementationDialog::onEditArchitecturePort) EVT_BUTTON(ID_ADD_EXTERNAL_PORT
Environment::vhdlPaths
static std::vector< std::string > vhdlPaths(const std::string &hdbPath)
Definition: Environment.cc:760
BlockImplementationFile.hh
WxConversion.hh
HDB::Parameter
Definition: HDBTypes.hh:46
HDBManager.hh
BlockImplementationFileDialog.hh
FileSystem::findFileInSearchPaths
static std::string findFileInSearchPaths(const std::vector< std::string > &searchPaths, const std::string &file)
Definition: FileSystem.cc:562
WxConversion::toString
static std::string toString(const wxString &source)
BlockImplementationFileDialog
Definition: BlockImplementationFileDialog.hh:44
END_EVENT_TABLE
END_EVENT_TABLE() using namespace IDF