]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxBlackBoxEditionDialog.cxx
Feature #1771 Add licence terms for all files.
[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(520, 640),wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) 
70         {
71                 _model=model;
72                 _parent = parent;
73                 std::string title("BlackBox Editing - ");
74                 title+=_model->getBBTKPackage();
75                 title+=":";
76                 title+=_model->getBBTKType();
77                 title+=":";
78                 title+=_model->getBBTKName();
79                 SetTitle(std2wx(title));
80
81                 constructBlackBoxEditionDialog();
82         }
83
84         //=========================================================================
85
86
87         wxBlackBoxEditionDialog::~wxBlackBoxEditionDialog()
88         {
89
90         }
91
92         //=========================================================================
93
94         bool wxBlackBoxEditionDialog::isValidNameForABox(std::string boxname) {
95
96                 int i=0;
97                 for (i = 0; i < boxname.size() ; i++) {
98                         if ( (isalnum(boxname[i])==0) && (boxname.compare(i, 1, "-") != 0) && ( boxname.compare(i, 1, "_") != 0) ){
99                                 return false;
100                         }
101                 } // for
102  
103                 return true;
104         }
105
106         //=========================================================================
107
108         void wxBlackBoxEditionDialog::constructBlackBoxEditionDialog()
109         {
110                 wxBoxSizer *sizerDialog = new wxBoxSizer(wxVERTICAL);
111
112         wxScrolledWindow *scrollWin = new wxScrolledWindow( this, -1, wxDefaultPosition,  wxSize(200,200), wxVSCROLL);
113
114                 wxString boxtype( crea::std2wx( _model->getBBTKType() ) );
115                 
116                 wxStaticText *textBoxName = new wxStaticText(scrollWin, -1, wxT("Box Name"));
117                 wxStaticText *textBoxType = new wxStaticText(scrollWin, -1, boxtype+wxT(": ") );
118                 wxTextCtrl *valueBoxName  = new wxTextCtrl(scrollWin, -1, _T(""), wxDefaultPosition,wxSize(300,25));
119                 _initBoxName = _model->getBBTKName();
120                 valueBoxName->SetValue(crea::std2wx(_initBoxName));
121                 _boxName = valueBoxName ;
122
123                 wxBoxSizer *boxnameSizer        = new wxBoxSizer(wxHORIZONTAL);
124                 boxnameSizer->Add(textBoxType,0,wxEXPAND,5);
125                 boxnameSizer->Add(valueBoxName,0,wxEXPAND,5);
126                 boxnameSizer->Add( new wxStaticText(scrollWin, -1, wxT(" ")) ,0,wxCENTRE|wxEXPAND,5);
127
128                 
129                 
130                 wxStaticText *text = new wxStaticText(scrollWin, -1, wxT("Input Ports"));
131                 wxFont font(11, wxDEFAULT, wxNORMAL, wxBOLD);
132                 text->SetFont(font);
133                 textBoxName->SetFont(font);
134
135
136                 std::vector<GPortModel*> lstInputs = _model->getInputPorts();
137                 wxFlexGridSizer *sizer = new wxFlexGridSizer(lstInputs.size(),3,5,5);
138                 sizer->AddGrowableCol(0);
139                 sizer->AddGrowableCol(1);
140                 sizer->AddGrowableCol(2);
141                 
142                 for(int i = 0;i<(int)lstInputs.size();i++)
143                 {
144                         sizer->AddGrowableRow(i);
145                         GPortModel              *port       = lstInputs[i];
146                         std::string              type           = port->getBBTKType();
147                         wxStaticText    *lblName        = new wxStaticText(scrollWin, -1, std2wx(port->getBBTKName()),wxDefaultPosition,wxSize(100,25));
148                         wxStaticText    *lblType        = new wxStaticText(scrollWin, -1, std2wx(type),wxDefaultPosition,wxSize(250,25));
149                         wxTextCtrl              *txtValue   = new wxTextCtrl(scrollWin, -1, _T(""),wxDefaultPosition,wxSize(300,25));
150
151                         lblName->SetToolTip(  std2wx(port->getBBTKDescription()) );
152                         txtValue->SetToolTip( std2wx(port->getBBTKDescription()) );
153                         
154                         
155                         if(port->getValue()!="")
156                         {
157                                 std::string text = port->getValue();
158                                 if(text.length()>0)
159                                         addDoubleQuotes(text);
160                                 txtValue->SetValue(crea::std2wx(text));
161                         }
162
163                         if(port->isConnected())
164                         {
165                                 std::string connected("--Port Connected--");
166                                 txtValue->SetValue(crea::std2wx(connected));
167                                 txtValue->SetEditable(false);
168                         }
169
170                         char et = '*';
171                         if(type.find(et)!=-1)
172                         {
173                                 std::string noEditable("--No editable--");
174                                 txtValue->SetValue(crea::std2wx(noEditable));
175                                 txtValue->SetEditable(false);
176                         }
177
178                         _lstNames.push_back(lblName);
179                         _lstValues.push_back(txtValue);
180                         _lstTypes.push_back(lblType);
181
182                         sizer->Add(lblName,1,wxEXPAND,5);
183                         sizer->Add(txtValue,1,wxEXPAND,5);
184                         sizer->Add(lblType,1,wxCENTRE|wxEXPAND,5);
185                 }
186
187
188                 wxBoxSizer *buts        = new wxBoxSizer(wxHORIZONTAL);
189                 wxButton *okButton      = new wxButton(scrollWin, -1, _T("Ok"),wxDefaultPosition, wxSize(70, 30));
190                 wxButton *closeButton   = new wxButton(scrollWin, -1, _T("Close"), wxDefaultPosition, wxSize(70, 30));
191
192                 // connect command event handlers
193                 Connect(okButton->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(wxBlackBoxEditionDialog::onClickOk));
194                 Connect(closeButton->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(wxBlackBoxEditionDialog::onClickClose));
195
196                 buts->Add(okButton,0,wxCENTRE|wxEXPAND,5);
197                 buts->Add(closeButton,0,wxCENTRE|wxEXPAND,5);
198
199                 sizerDialog->AddSpacer(10);
200                 sizerDialog->Add(textBoxName,0,wxALIGN_TOP|wxALIGN_CENTER);
201                 sizerDialog->AddSpacer(10);
202                 sizerDialog->Add(boxnameSizer, wxSizerFlags(0).Align(0).Border(wxLEFT, 100));
203                 sizerDialog->AddSpacer(10);
204                 sizerDialog->Add(text,0,wxALIGN_TOP|wxALIGN_CENTER);
205                 sizerDialog->AddSpacer(15);
206                 sizerDialog->Add(sizer,0,wxALIGN_CENTER| wxEXPAND);
207                 sizerDialog->AddSpacer(15);
208                 sizerDialog->Add(buts,0,wxALIGN_CENTER | wxTOP | wxBOTTOM);
209
210                 scrollWin->SetSizer(sizerDialog);
211                 scrollWin->Centre();
212
213
214         scrollWin->SetVirtualSize(400,400);
215         scrollWin->SetSize(300,300);
216         scrollWin->SetScrollbars(10, 10, 50, 50);
217 //        scrollWin->SetSizer(sizer);
218
219
220                 ShowModal();
221                 Destroy();
222         }
223
224         //=========================================================================
225
226         void wxBlackBoxEditionDialog::onClickOk(wxCommandEvent& event)
227         {
228                 int closeok = 1;
229                 for(int i=0;i<(int)_lstValues.size();i++)
230                 {
231                         std::string text = wx2std(_lstValues[i]->GetValue());
232                         //TOFIX Search a better alternative
233                         if(text!="--No editable--" && text!="--Port Connected--")
234                         {
235                                 //JPRG: When executing, add double quotes if necessary
236                                 if(text.length()>0)
237                                 {
238                                         addDoubleQuotes(text);
239                                 }
240                                 _model->setValueToInputPort(i,text);
241                         }
242                 }
243                 //handle box name
244                 std::string boxname = wx2std(_boxName->GetValue());
245                 if( boxname.compare(_initBoxName) != 0 ){
246                         if (isValidNameForABox(boxname) == true){
247                                 if( _parent->boxNameExists(boxname) == true ){
248                                         closeok=0;
249                                         wxMessageDialog *dial = new wxMessageDialog(NULL,
250                          wxT("The name already exists. Please provide another name"),
251                          wxT("Change name: name already exists"), wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
252                                         dial->ShowModal();
253                                 }else{
254                                         _model->setBBTKName(boxname);
255                                 }
256                         }else{
257                                 closeok=0;
258                                 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);
259                                 dial->ShowModal();
260                         }
261                 }
262  
263                 if( closeok==1 ){
264                         _parent->SaveTempActualDiagram("edit values");
265                         Close(true);
266                 }
267         }
268
269         //=========================================================================
270
271         void wxBlackBoxEditionDialog::onClickClose(wxCommandEvent& event)
272         {
273                 Close(true);
274         }
275
276         //=========================================================================
277
278         //=========================================================================
279         
280         void wxBlackBoxEditionDialog::addDoubleQuotes(std::string &text)
281         {
282                 
283                 //We add the double quotes at the beginning
284                 if(text[0] != '"' )
285                         text = "\"" + text;
286                 
287                 //We add the double quotes at the end
288                 if(text[text.length()-1] != '"')        
289                         text+= "\"";
290                 
291         }
292         
293
294         //=========================================================================
295         void wxBlackBoxEditionDialog::removeDoubleQuotes(std::string &text)
296         {
297                 if(text[0]== '"' )
298                         text.replace(0, 1,"");
299                 if(text[text.length()-1] == '"')        
300                         text.replace(text.length()-1, 1,"");
301                 
302         }
303         
304         
305         
306 }  // EO namespace bbtk
307
308 // EOF
309 /*      // remove * from the name
310                 std::string name = _tabsMgr->GetNameTabPanel();
311                 if(name[name.length() - 1] != '*')
312                         name = name.substr(0, name.length()-2);
313                 _tabsMgr->SetNameTabPanel(name);*/