]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxBlackBoxEditionDialog.cxx
v1.1.0 Feature 1407
[bbtkGEditor.git] / lib / EditorGraphicBBS / bbsWxGUIEditorGraphic / wxBlackBoxEditionDialog.cxx
1 /*=========================================================================
2 Program:   bbtk
3 Module:    $RCSfile$
4 Language:  C++
5 Date:      $Date$
6 Version:   $Revision$
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and
15 *  abiding by the rules of distribution of free software. You can  use,
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B
17 *  license as circulated by CEA, CNRS and INRIA at the following URL
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability.
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
30
31 /**
32 *  \file
33 *  \brief Class bbtk::wxBlackBoxEditionDialog .
34 */
35
36 #include "wxBlackBoxEditionDialog.h"
37 #include "creaWx.h"
38
39 namespace bbtk
40 {
41         //=========================================================================
42
43         wxBlackBoxEditionDialog::wxBlackBoxEditionDialog(wxGUIEditorGraphicBBS *parent,GBlackBoxModel *model):wxDialog(parent,wxID_ANY,_T(""), wxDefaultPosition, wxSize(520, 640),wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) 
44         {
45                 _model=model;
46                 _parent = parent;
47                 std::string title("BlackBox Editing - ");
48                 title+=_model->getBBTKPackage();
49                 title+=":";
50                 title+=_model->getBBTKType();
51                 title+=":";
52                 title+=_model->getBBTKName();
53                 SetTitle(std2wx(title));
54
55                 constructBlackBoxEditionDialog();
56         }
57
58         //=========================================================================
59
60
61         wxBlackBoxEditionDialog::~wxBlackBoxEditionDialog()
62         {
63
64         }
65
66         //=========================================================================
67
68         bool wxBlackBoxEditionDialog::isValidNameForABox(std::string boxname) {
69
70                 int i=0;
71                 for (i = 0; i < boxname.size() ; i++) {
72                         if ( (isalnum(boxname[i])==0) && (boxname.compare(i, 1, "-") != 0) && ( boxname.compare(i, 1, "_") != 0) ){
73                                 return false;
74                         }
75                 } // for
76  
77                 return true;
78         }
79
80         //=========================================================================
81
82         void wxBlackBoxEditionDialog::constructBlackBoxEditionDialog()
83         {
84                 wxBoxSizer *sizerDialog = new wxBoxSizer(wxVERTICAL);
85
86         wxScrolledWindow *scrollWin = new wxScrolledWindow( this, -1, wxDefaultPosition,  wxSize(200,200), wxVSCROLL);
87
88                 wxString boxtype( crea::std2wx( _model->getBBTKType() ) );
89                 
90                 wxStaticText *textBoxName = new wxStaticText(scrollWin, -1, wxT("Box Name"));
91                 wxStaticText *textBoxType = new wxStaticText(scrollWin, -1, boxtype+wxT(": ") );
92                 wxTextCtrl *valueBoxName  = new wxTextCtrl(scrollWin, -1, _T(""), wxDefaultPosition,wxSize(300,25));
93                 _initBoxName = _model->getBBTKName();
94                 valueBoxName->SetValue(crea::std2wx(_initBoxName));
95                 _boxName = valueBoxName ;
96
97                 wxBoxSizer *boxnameSizer        = new wxBoxSizer(wxHORIZONTAL);
98                 boxnameSizer->Add(textBoxType,0,wxEXPAND,5);
99                 boxnameSizer->Add(valueBoxName,0,wxEXPAND,5);
100                 boxnameSizer->Add( new wxStaticText(scrollWin, -1, wxT(" ")) ,0,wxCENTRE|wxEXPAND,5);
101
102                 
103                 
104                 wxStaticText *text = new wxStaticText(scrollWin, -1, wxT("Input Ports"));
105                 wxFont font(11, wxDEFAULT, wxNORMAL, wxBOLD);
106                 text->SetFont(font);
107                 textBoxName->SetFont(font);
108
109
110                 std::vector<GPortModel*> lstInputs = _model->getInputPorts();
111                 wxFlexGridSizer *sizer = new wxFlexGridSizer(lstInputs.size(),3,5,5);
112                 sizer->AddGrowableCol(0);
113                 sizer->AddGrowableCol(1);
114                 sizer->AddGrowableCol(2);
115                 
116                 for(int i = 0;i<(int)lstInputs.size();i++)
117                 {
118                         sizer->AddGrowableRow(i);
119                         GPortModel              *port       = lstInputs[i];
120                         std::string              type           = port->getBBTKType();
121                         wxStaticText    *lblName        = new wxStaticText(scrollWin, -1, std2wx(port->getBBTKName()),wxDefaultPosition,wxSize(100,25));
122                         wxStaticText    *lblType        = new wxStaticText(scrollWin, -1, std2wx(type),wxDefaultPosition,wxSize(250,25));
123                         wxTextCtrl              *txtValue   = new wxTextCtrl(scrollWin, -1, _T(""),wxDefaultPosition,wxSize(300,25));
124
125                         lblName->SetToolTip(  std2wx(port->getBBTKDescription()) );
126                         txtValue->SetToolTip( std2wx(port->getBBTKDescription()) );
127                         
128                         
129                         if(port->getValue()!="")
130                         {
131                                 std::string text = port->getValue();
132                                 if(text.length()>0)
133                                         addDoubleQuotes(text);
134                                 txtValue->SetValue(crea::std2wx(text));
135                         }
136
137                         if(port->isConnected())
138                         {
139                                 std::string connected("--Port Connected--");
140                                 txtValue->SetValue(crea::std2wx(connected));
141                                 txtValue->SetEditable(false);
142                         }
143
144                         char et = '*';
145                         if(type.find(et)!=-1)
146                         {
147                                 std::string noEditable("--No editable--");
148                                 txtValue->SetValue(crea::std2wx(noEditable));
149                                 txtValue->SetEditable(false);
150                         }
151
152                         _lstNames.push_back(lblName);
153                         _lstValues.push_back(txtValue);
154                         _lstTypes.push_back(lblType);
155
156                         sizer->Add(lblName,1,wxEXPAND,5);
157                         sizer->Add(txtValue,1,wxEXPAND,5);
158                         sizer->Add(lblType,1,wxCENTRE|wxEXPAND,5);
159                 }
160
161
162                 wxBoxSizer *buts        = new wxBoxSizer(wxHORIZONTAL);
163                 wxButton *okButton      = new wxButton(scrollWin, -1, _T("Ok"),wxDefaultPosition, wxSize(70, 30));
164                 wxButton *closeButton   = new wxButton(scrollWin, -1, _T("Close"), wxDefaultPosition, wxSize(70, 30));
165
166                 // connect command event handlers
167                 Connect(okButton->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(wxBlackBoxEditionDialog::onClickOk));
168                 Connect(closeButton->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(wxBlackBoxEditionDialog::onClickClose));
169
170                 buts->Add(okButton,0,wxCENTRE|wxEXPAND,5);
171                 buts->Add(closeButton,0,wxCENTRE|wxEXPAND,5);
172
173                 sizerDialog->AddSpacer(10);
174                 sizerDialog->Add(textBoxName,0,wxALIGN_TOP|wxALIGN_CENTER);
175                 sizerDialog->AddSpacer(10);
176                 sizerDialog->Add(boxnameSizer, wxSizerFlags(0).Align(0).Border(wxLEFT, 100));
177                 sizerDialog->AddSpacer(10);
178                 sizerDialog->Add(text,0,wxALIGN_TOP|wxALIGN_CENTER);
179                 sizerDialog->AddSpacer(15);
180                 sizerDialog->Add(sizer,0,wxALIGN_CENTER| wxEXPAND);
181                 sizerDialog->AddSpacer(15);
182                 sizerDialog->Add(buts,0,wxALIGN_CENTER | wxTOP | wxBOTTOM);
183
184                 scrollWin->SetSizer(sizerDialog);
185                 scrollWin->Centre();
186
187
188         scrollWin->SetVirtualSize(400,400);
189         scrollWin->SetSize(300,300);
190         scrollWin->SetScrollbars(10, 10, 50, 50);
191 //        scrollWin->SetSizer(sizer);
192
193
194                 ShowModal();
195                 Destroy();
196         }
197
198         //=========================================================================
199
200         void wxBlackBoxEditionDialog::onClickOk(wxCommandEvent& event)
201         {
202                 int closeok = 1;
203                 for(int i=0;i<(int)_lstValues.size();i++)
204                 {
205                         std::string text = wx2std(_lstValues[i]->GetValue());
206                         //TOFIX Search a better alternative
207                         if(text!="--No editable--" && text!="--Port Connected--")
208                         {
209                                 //JPRG: When executing, add double quotes if necessary
210                                 if(text.length()>0)
211                                 {
212                                         addDoubleQuotes(text);
213                                 }
214                                 _model->setValueToInputPort(i,text);
215                         }
216                 }
217                 //handle box name
218                 std::string boxname = wx2std(_boxName->GetValue());
219                 if( boxname.compare(_initBoxName) != 0 ){
220                         if (isValidNameForABox(boxname) == true){
221                                 if( _parent->boxNameExists(boxname) == true ){
222                                         closeok=0;
223                                         wxMessageDialog *dial = new wxMessageDialog(NULL,
224                          wxT("The name already exists. Please provide another name"),
225                          wxT("Change name: name already exists"), wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
226                                         dial->ShowModal();
227                                 }else{
228                                         _model->setBBTKName(boxname);
229                                 }
230                         }else{
231                                 closeok=0;
232                                 wxMessageDialog *dial = new wxMessageDialog(NULL,  wxT("Please provide a valide name for your box (no spaces and only leters, digits, \"_\" or \"-\" allowed)"),  wxT("Change name: invalid name"), wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
233                                 dial->ShowModal();
234                         }
235                 }
236  
237                 if( closeok==1 ){
238                         _parent->SaveTempActualDiagram("edit values");
239                         Close(true);
240                 }
241         }
242
243         //=========================================================================
244
245         void wxBlackBoxEditionDialog::onClickClose(wxCommandEvent& event)
246         {
247 printf("EED wxBlackBoxEditionDialog::onClickClose\n");
248                 Close(true);
249         }
250
251         //=========================================================================
252
253         //=========================================================================
254         
255         void wxBlackBoxEditionDialog::addDoubleQuotes(std::string &text)
256         {
257                 
258                 //We add the double quotes at the beginning
259                 if(text[0] != '"' )
260                         text = "\"" + text;
261                 
262                 //We add the double quotes at the end
263                 if(text[text.length()-1] != '"')        
264                         text+= "\"";
265                 
266         }
267         
268
269         //=========================================================================
270         void wxBlackBoxEditionDialog::removeDoubleQuotes(std::string &text)
271         {
272                 if(text[0]== '"' )
273                         text.replace(0, 1,"");
274                 if(text[text.length()-1] == '"')        
275                         text.replace(text.length()-1, 1,"");
276                 
277         }
278         
279         
280         
281 }  // EO namespace bbtk
282
283 // EOF
284 /*      // remove * from the name
285                 std::string name = _tabsMgr->GetNameTabPanel();
286                 if(name[name.length() - 1] != '*')
287                         name = name.substr(0, name.length()-2);
288                 _tabsMgr->SetNameTabPanel(name);*/