]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxBlackBoxEditionDialog.cxx
Feature #1758
[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)
44                 :wxDialog(parent,wxID_ANY,_T(""), wxDefaultPosition, wxSize(520, 640),wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) 
45         {
46                 _model=model;
47                 _parent = parent;
48                 std::string title("BlackBox Editing - ");
49                 title+=_model->getBBTKPackage();
50                 title+=":";
51                 title+=_model->getBBTKType();
52                 title+=":";
53                 title+=_model->getBBTKName();
54                 SetTitle(std2wx(title));
55
56                 constructBlackBoxEditionDialog();
57         }
58
59         //=========================================================================
60
61
62         wxBlackBoxEditionDialog::~wxBlackBoxEditionDialog()
63         {
64
65         }
66
67         //=========================================================================
68
69         bool wxBlackBoxEditionDialog::isValidNameForABox(std::string boxname) {
70
71                 int i=0;
72                 for (i = 0; i < boxname.size() ; i++) {
73                         if ( (isalnum(boxname[i])==0) && (boxname.compare(i, 1, "-") != 0) && ( boxname.compare(i, 1, "_") != 0) ){
74                                 return false;
75                         }
76                 } // for
77  
78                 return true;
79         }
80
81         //=========================================================================
82
83         void wxBlackBoxEditionDialog::constructBlackBoxEditionDialog()
84         {
85                 wxBoxSizer *sizerDialog = new wxBoxSizer(wxVERTICAL);
86
87         wxScrolledWindow *scrollWin = new wxScrolledWindow( this, -1, wxDefaultPosition,  wxSize(200,200), wxVSCROLL);
88
89                 wxString boxtype( crea::std2wx( _model->getBBTKType() ) );
90                 
91                 wxStaticText *textBoxName = new wxStaticText(scrollWin, -1, wxT("Box Name"));
92                 wxStaticText *textBoxType = new wxStaticText(scrollWin, -1, boxtype+wxT(": ") );
93                 wxTextCtrl *valueBoxName  = new wxTextCtrl(scrollWin, -1, _T(""), wxDefaultPosition,wxSize(300,25));
94                 _initBoxName = _model->getBBTKName();
95                 valueBoxName->SetValue(crea::std2wx(_initBoxName));
96                 _boxName = valueBoxName ;
97
98                 wxBoxSizer *boxnameSizer        = new wxBoxSizer(wxHORIZONTAL);
99                 boxnameSizer->Add(textBoxType,0,wxEXPAND,5);
100                 boxnameSizer->Add(valueBoxName,0,wxEXPAND,5);
101                 boxnameSizer->Add( new wxStaticText(scrollWin, -1, wxT(" ")) ,0,wxCENTRE|wxEXPAND,5);
102
103                 
104                 
105                 wxStaticText *text = new wxStaticText(scrollWin, -1, wxT("Input Ports"));
106                 wxFont font(11, wxDEFAULT, wxNORMAL, wxBOLD);
107                 text->SetFont(font);
108                 textBoxName->SetFont(font);
109
110
111                 std::vector<GPortModel*> lstInputs = _model->getInputPorts();
112                 wxFlexGridSizer *sizer = new wxFlexGridSizer(lstInputs.size(),3,5,5);
113                 sizer->AddGrowableCol(0);
114                 sizer->AddGrowableCol(1);
115                 sizer->AddGrowableCol(2);
116                 
117                 for(int i = 0;i<(int)lstInputs.size();i++)
118                 {
119                         sizer->AddGrowableRow(i);
120                         GPortModel              *port       = lstInputs[i];
121                         std::string              type           = port->getBBTKType();
122                         wxStaticText    *lblName        = new wxStaticText(scrollWin, -1, std2wx(port->getBBTKName()),wxDefaultPosition,wxSize(100,25));
123                         wxStaticText    *lblType        = new wxStaticText(scrollWin, -1, std2wx(type),wxDefaultPosition,wxSize(250,25));
124                         wxTextCtrl              *txtValue   = new wxTextCtrl(scrollWin, -1, _T(""),wxDefaultPosition,wxSize(300,25));
125
126                         lblName->SetToolTip(  std2wx(port->getBBTKDescription()) );
127                         txtValue->SetToolTip( std2wx(port->getBBTKDescription()) );
128                         
129                         
130                         if(port->getValue()!="")
131                         {
132                                 std::string text = port->getValue();
133                                 if(text.length()>0)
134                                         addDoubleQuotes(text);
135                                 txtValue->SetValue(crea::std2wx(text));
136                         }
137
138                         if(port->isConnected())
139                         {
140                                 std::string connected("--Port Connected--");
141                                 txtValue->SetValue(crea::std2wx(connected));
142                                 txtValue->SetEditable(false);
143                         }
144
145                         char et = '*';
146                         if(type.find(et)!=-1)
147                         {
148                                 std::string noEditable("--No editable--");
149                                 txtValue->SetValue(crea::std2wx(noEditable));
150                                 txtValue->SetEditable(false);
151                         }
152
153                         _lstNames.push_back(lblName);
154                         _lstValues.push_back(txtValue);
155                         _lstTypes.push_back(lblType);
156
157                         sizer->Add(lblName,1,wxEXPAND,5);
158                         sizer->Add(txtValue,1,wxEXPAND,5);
159                         sizer->Add(lblType,1,wxCENTRE|wxEXPAND,5);
160                 }
161
162
163                 wxBoxSizer *buts        = new wxBoxSizer(wxHORIZONTAL);
164                 wxButton *okButton      = new wxButton(scrollWin, -1, _T("Ok"),wxDefaultPosition, wxSize(70, 30));
165                 wxButton *closeButton   = new wxButton(scrollWin, -1, _T("Close"), wxDefaultPosition, wxSize(70, 30));
166
167                 // connect command event handlers
168                 Connect(okButton->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(wxBlackBoxEditionDialog::onClickOk));
169                 Connect(closeButton->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(wxBlackBoxEditionDialog::onClickClose));
170
171                 buts->Add(okButton,0,wxCENTRE|wxEXPAND,5);
172                 buts->Add(closeButton,0,wxCENTRE|wxEXPAND,5);
173
174                 sizerDialog->AddSpacer(10);
175                 sizerDialog->Add(textBoxName,0,wxALIGN_TOP|wxALIGN_CENTER);
176                 sizerDialog->AddSpacer(10);
177                 sizerDialog->Add(boxnameSizer, wxSizerFlags(0).Align(0).Border(wxLEFT, 100));
178                 sizerDialog->AddSpacer(10);
179                 sizerDialog->Add(text,0,wxALIGN_TOP|wxALIGN_CENTER);
180                 sizerDialog->AddSpacer(15);
181                 sizerDialog->Add(sizer,0,wxALIGN_CENTER| wxEXPAND);
182                 sizerDialog->AddSpacer(15);
183                 sizerDialog->Add(buts,0,wxALIGN_CENTER | wxTOP | wxBOTTOM);
184
185                 scrollWin->SetSizer(sizerDialog);
186                 scrollWin->Centre();
187
188
189         scrollWin->SetVirtualSize(400,400);
190         scrollWin->SetSize(300,300);
191         scrollWin->SetScrollbars(10, 10, 50, 50);
192 //        scrollWin->SetSizer(sizer);
193
194
195                 ShowModal();
196                 Destroy();
197         }
198
199         //=========================================================================
200
201         void wxBlackBoxEditionDialog::onClickOk(wxCommandEvent& event)
202         {
203                 int closeok = 1;
204                 for(int i=0;i<(int)_lstValues.size();i++)
205                 {
206                         std::string text = wx2std(_lstValues[i]->GetValue());
207                         //TOFIX Search a better alternative
208                         if(text!="--No editable--" && text!="--Port Connected--")
209                         {
210                                 //JPRG: When executing, add double quotes if necessary
211                                 if(text.length()>0)
212                                 {
213                                         addDoubleQuotes(text);
214                                 }
215                                 _model->setValueToInputPort(i,text);
216                         }
217                 }
218                 //handle box name
219                 std::string boxname = wx2std(_boxName->GetValue());
220                 if( boxname.compare(_initBoxName) != 0 ){
221                         if (isValidNameForABox(boxname) == true){
222                                 if( _parent->boxNameExists(boxname) == true ){
223                                         closeok=0;
224                                         wxMessageDialog *dial = new wxMessageDialog(NULL,
225                          wxT("The name already exists. Please provide another name"),
226                          wxT("Change name: name already exists"), wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
227                                         dial->ShowModal();
228                                 }else{
229                                         _model->setBBTKName(boxname);
230                                 }
231                         }else{
232                                 closeok=0;
233                                 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);
234                                 dial->ShowModal();
235                         }
236                 }
237  
238                 if( closeok==1 ){
239                         _parent->SaveTempActualDiagram("edit values");
240                         Close(true);
241                 }
242         }
243
244         //=========================================================================
245
246         void wxBlackBoxEditionDialog::onClickClose(wxCommandEvent& event)
247         {
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);*/