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