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