]> Creatis software - bbtkGEditor.git/blob - lib/EditorGraphicBBS/bbsWxGUIEditorGraphic/wxBlackBoxEditionDialog.cxx
Feature #1380
[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                                 txtValue->SetValue(crea::std2wx(port->getValue()));
118                         }
119
120                         if(port->isConnected())
121                         {
122                                 std::string connected("--Port Connected--");
123                                 txtValue->SetValue(crea::std2wx(connected));
124                                 txtValue->SetEditable(false);
125                         }
126
127                         char et = '*';
128                         if(type.find(et)!=-1)
129                         {
130                                 std::string noEditable("--No editable--");
131                                 txtValue->SetValue(crea::std2wx(noEditable));
132                                 txtValue->SetEditable(false);
133                         }
134
135                         _lstNames.push_back(lblName);
136                         _lstValues.push_back(txtValue);
137                         _lstTypes.push_back(lblType);
138
139                         sizer->Add(lblName,1,wxEXPAND,5);
140                         sizer->Add(txtValue,1,wxEXPAND,5);
141                         sizer->Add(lblType,1,wxCENTRE|wxEXPAND,5);
142                 }
143
144
145                 wxBoxSizer *buts        = new wxBoxSizer(wxHORIZONTAL);
146                 wxButton *okButton      = new wxButton(scrollWin, -1, _T("Ok"),wxDefaultPosition, wxSize(70, 30));
147                 wxButton *closeButton   = new wxButton(scrollWin, -1, _T("Close"), wxDefaultPosition, wxSize(70, 30));
148
149                 // connect command event handlers
150                 Connect(okButton->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(wxBlackBoxEditionDialog::onClickOk));
151                 Connect(closeButton->GetId(),wxEVT_COMMAND_BUTTON_CLICKED,wxCommandEventHandler(wxBlackBoxEditionDialog::onClickClose));
152
153                 buts->Add(okButton,0,wxCENTRE|wxEXPAND,5);
154                 buts->Add(closeButton,0,wxCENTRE|wxEXPAND,5);
155
156                 sizerDialog->AddSpacer(10);
157                 sizerDialog->Add(textBoxName,0,wxALIGN_TOP|wxALIGN_CENTER);
158                 sizerDialog->AddSpacer(10);
159                 sizerDialog->Add(valueBoxName, wxSizerFlags(0).Align(0).Border(wxLEFT, 100));
160                 sizerDialog->AddSpacer(10);
161                 sizerDialog->Add(text,0,wxALIGN_TOP|wxALIGN_CENTER);
162                 sizerDialog->AddSpacer(15);
163                 sizerDialog->Add(sizer,0,wxALIGN_CENTER| wxEXPAND);
164                 sizerDialog->AddSpacer(15);
165                 sizerDialog->Add(buts,0,wxALIGN_CENTER | wxTOP | wxBOTTOM);
166
167                 scrollWin->SetSizer(sizerDialog);
168                 scrollWin->Centre();
169
170
171         scrollWin->SetVirtualSize(400,400);
172         scrollWin->SetSize(300,300);
173         scrollWin->SetScrollbars(10, 10, 50, 50);
174 //        scrollWin->SetSizer(sizer);
175
176
177                 ShowModal();
178                 Destroy();
179         }
180
181         //=========================================================================
182
183         void wxBlackBoxEditionDialog::onClickOk(wxCommandEvent& event)
184         {
185                 int closeok = 1;
186                 for(int i=0;i<(int)_lstValues.size();i++)
187                 {
188                         std::string text = wx2std(_lstValues[i]->GetValue());
189                         //TOFIX Search a better alternative
190                         if(text!="--No editable--" && text!="--Port Connected--")
191                         {
192                                 _model->setValueToInputPort(i,text);
193                         }
194                 }
195                 //handle box name
196                 std::string boxname = wx2std(_boxName->GetValue());
197                 if( boxname.compare(_initBoxName) != 0 ){
198                         if (isValidNameForABox(boxname) == true){
199                                 if( _parent->boxNameExists(boxname) == true ){
200                                         closeok=0;
201                                         wxMessageDialog *dial = new wxMessageDialog(NULL,
202                          wxT("The name already exists. Please provide another name"),
203                          wxT("Change name: name already exists"), wxOK | wxICON_EXCLAMATION | wxSTAY_ON_TOP);
204                                         dial->ShowModal();
205                                 }else{
206                                         _model->setBBTKName(boxname);
207                                 }
208                         }else{
209                                 closeok=0;
210                                 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);
211                                 dial->ShowModal();
212                         }
213                 }
214  
215                 if( closeok==1 ){
216                         _parent->SaveTempActualDiagram("edit values");
217                         Close(true);
218                 }
219         }
220
221         //=========================================================================
222
223         void wxBlackBoxEditionDialog::onClickClose(wxCommandEvent& event)
224         {
225 printf("EED wxBlackBoxEditionDialog::onClickClose\n");
226                 Close(true);
227         }
228
229         //=========================================================================
230
231 }  // EO namespace bbtk
232
233 // EOF
234 /*      // remove * from the name
235                 std::string name = _tabsMgr->GetNameTabPanel();
236                 if(name[name.length() - 1] != '*')
237                         name = name.substr(0, name.length()-2);
238                 _tabsMgr->SetNameTabPanel(name);*/