]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/CutModule/interface/CutModelMainPanel.cxx
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / maracasVisuLib / src / CutModule / interface / CutModelMainPanel.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 /*=========================================================================
27
28 Program:   wxMaracas
29 Module:    $RCSfile: CutModelMainPanel.cxx,v $
30 Language:  C++
31 Date:      $Date: 2012/11/15 14:16:20 $
32 Version:   $Revision: 1.14 $
33
34 Copyright: (c) 2002, 2003
35 License:
36
37 This software is distributed WITHOUT ANY WARRANTY; without even
38 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
39 PURPOSE.  See the above copyright notice for more information.
40
41 =========================================================================*/
42
43
44
45 // EOF - wxMaracasMPR.cxx
46
47 #include "CutModelMainPanel.h"
48 #include "HistogramDialog.h"
49 #include "creaSystem.h"
50
51 #include <wx/colordlg.h>
52 #include <wx/bmpbuttn.h>
53
54 #include "Add.xpm"
55 #include "Undo.xpm"
56 #include "Redo.xpm"
57 #include "OkAll.xpm"
58
59
60
61 CutModelMainPanel* CutModelMainPanel::instance=NULL;
62
63 CutModelMainPanel::CutModelMainPanel( wxWindow* parent, std::string path)
64 : wxPanel(parent, -1, wxDefaultPosition, wxDefaultSize){
65
66         cutmanager=NULL;
67         _isCheck=false;
68         _isFirstTime=true;
69         initialize(path);
70
71 }
72 CutModelMainPanel::~CutModelMainPanel( ){
73         viewpanels.clear();
74         delete cutmanager;
75 }
76
77 void CutModelMainPanel::initialize(std::string path){
78         cutmanager = new CutModelManager(path);
79         _panelid = 0;
80         _wxauimanager = new wxAuiManager(this);
81
82         wxAuiPaneInfo paneinfo;
83
84         //RaC 5-02-2010 Add tabs
85         _notebook = new wxAuiNotebook(this,wxID_ANY,wxDefaultPosition,wxDefaultSize,wxAUI_NB_TOP |wxAUI_NB_TAB_MOVE | wxAUI_NB_SCROLL_BUTTONS);
86
87         _notebook->AddPage(new ToolBarCutModel(_notebook),_T("Cutter"),true);
88         //_wxauimanager->AddPane(new ToolBarCutModel(this),paneinfo.ToolbarPane().Top());
89         _wxauimanager->AddPane(_notebook,paneinfo.Center().CloseButton(false));
90         addPolygonCutterTab();
91         _notebook->Update();
92
93         _wxauimanager->Update();
94 }
95
96 CutModelMainPanel* CutModelMainPanel::getInstance(wxWindow* parent, std::string path){
97         if(instance==NULL){
98                 if(parent == NULL){
99                         parent = new wxFrame();
100                 }
101                 instance = new CutModelMainPanel(parent, path);
102         }
103         return instance;
104 }
105
106 /*CutModelMainPanel* CutModelMainPanel::getInstance(){
107 return instance;
108 }*/
109
110
111 void CutModelMainPanel::setImageData(vtkImageData* img){
112         try{
113                 checkInvariant();
114                 cutmanager->setImageData(img);
115         }catch(CutModelException e){
116                 showErrorDialog(e.getCause());
117         }
118 }
119
120 void CutModelMainPanel::setInteractor(vtkRenderWindowInteractor* interactor){
121         try{
122                 checkInvariant();
123                 cutmanager->setInteractor(interactor);
124         }catch(CutModelException e){
125                 showErrorDialog(e.getCause());
126         }
127 }
128
129 void CutModelMainPanel::setRenderer(vtkRenderer* renderer){
130         try{
131                 checkInvariant();
132                 cutmanager->setRenderer(renderer);
133         }catch(CutModelException e){
134                 showErrorDialog(e.getCause());
135         }
136 }
137
138 void CutModelMainPanel::checkInvariant()throw (CutModelException){
139         if(cutmanager==NULL){
140                 throw CutModelException("The manager is not initialize");
141         }
142 }
143
144 void CutModelMainPanel::showErrorDialog(std::string str){
145         wxMessageDialog* diag = new wxMessageDialog(this, wxString(str.c_str(),wxConvUTF8 ), wxString(str.c_str(),wxConvUTF8 ), wxICON_ERROR);
146         diag->ShowModal();
147 }
148
149 void CutModelMainPanel::RemoveActor(int id){
150         try{
151                 checkInvariant();
152
153                 if(!this->IsBeingDeleted()){
154                         int i,j;
155                         for(i = 0; i < (int)viewpanels.size()-1;i++){
156                                 CutModelView* view = viewpanels[i];
157                                 if(view->getId()==id){
158                                         for(j = i; j < (int)viewpanels.size()-1;j++){
159                                                 viewpanels[j]=viewpanels[j+1];
160                                         }
161                                         i = viewpanels.size();
162                                 }
163                         }
164                         viewpanels.pop_back();
165                         cutmanager->RemoveActor(id);
166                 }
167
168         }catch(CutModelException e){
169                 showErrorDialog(e.getCause());
170         }
171 }
172
173 //RaC
174 void CutModelMainPanel::onCheckChanged(){
175
176         if(_isFirstTime==true){
177                 _isFirstTime=false;
178                 cutmanager->InitializePolygonInteractorStyle();
179         }
180
181         if(_isCheck==false)
182         {
183                 _isCheck=true;
184                 //Draw Polygon
185
186                 cutmanager->ParallelProjectionOn();
187                 btnExecutePolygonCut->Enable(false);
188                 _notebook->GetPage(0)->Enable(false);
189                 _radioinsideout->Enable(false);
190                 lblMessagePolygon->SetLabel(_T("Drawing polygon..."));
191         }
192         else
193         {
194                 _isCheck=false;
195                 //Finish Drawing
196                 cout<<"Finish Drawing"<<endl;
197                 cutmanager->ParallelProjectionOff();
198                 btnExecutePolygonCut->Enable(true);
199                 _notebook->GetPage(0)->Enable(true);
200                 _radioinsideout->Enable(true);
201                 lblMessagePolygon->SetLabel(_T("Contour saved! Click on Execute Cut"));
202         }
203
204         cutmanager->UpdatePolygon(_isCheck);
205
206 }
207
208
209 //RaC
210 void CutModelMainPanel::onExecuteCutPolygon()
211 {
212         //Cuts Polygon
213         cutmanager->ExecuteCutPolygon(_radioinsideout->GetSelection());
214         lblMessagePolygon->SetLabel(_T("No contour drawed"));
215 }
216
217
218 void CutModelMainPanel::onAddCutModel(){
219         try{
220                 checkInvariant();
221
222                 int id = addNewViewPanel();
223                 cutmanager->onAddCutModel(id, getModelView(id));
224
225                 _panelid++;
226
227                 ShowCurrentPanel(id);
228
229         }catch(CutModelException e){
230                 showErrorDialog(e.getCause());
231         }
232 }
233
234 void CutModelMainPanel::ShowCurrentPanel(int id)
235 {
236         int i;
237         for(i = 0; i < (int) viewpanels.size();i++){
238                 if(viewpanels[i]->getId()==id){
239                         _wxauimanager->GetPane(viewpanels[i]).Show(true);
240                         cutmanager->RefreshActor(id);
241                 }else{
242                         _wxauimanager->GetPane(viewpanels[i]).Show(false);
243                 }
244         }
245         _wxauimanager->Update();
246 }
247 int CutModelMainPanel::addNewViewPanel()throw( CutModelException){
248
249         CutModelView* viewpanel = new CutModelView(this,cutmanager->getImageRange());
250
251         wxAuiPaneInfo paneinfo0;
252         _wxauimanager->AddPane(viewpanel, paneinfo0.DefaultPane().DestroyOnClose().Centre().Bottom());
253
254         viewpanel->setId(_panelid);
255         viewpanels.push_back(viewpanel);
256
257         return viewpanel->getId();
258
259
260 }
261 CutModelView* CutModelMainPanel::getModelView(int id)throw( CutModelException)
262 {
263         CutModelView* current = NULL;
264         int i;
265         for(i = 0; i < (int)viewpanels.size();i++)
266         {
267                 if(viewpanels[i]->getId()==id)
268                 {
269                         current = viewpanels[i];
270                 }
271         }
272         if(current == NULL)
273         {
274                 std::string s = "Id not found";
275                 throw CutModelException(s);
276         }
277         return current;
278 }
279
280 void CutModelMainPanel::onUndo()
281         {
282         try{
283                 checkInvariant();
284                 /*int result = */ cutmanager->Undo(); // result unused// JPR
285
286         }catch(CutModelException e){
287                 showErrorDialog(e.getCause());
288         }
289 }
290
291 void CutModelMainPanel::onRedo(){
292         try{
293                 checkInvariant();
294                 /*int result = */ cutmanager->Redo();
295
296         }catch(CutModelException e){
297                 showErrorDialog(e.getCause());
298         }
299 }
300
301 void CutModelMainPanel::changeOpacity(int id,int opacity){
302         try{
303                 checkInvariant();
304                 cutmanager->changeOpacity(id, opacity);
305
306         }catch(CutModelException e){
307                 showErrorDialog(e.getCause());
308         }
309 }
310
311 void CutModelMainPanel::ShowViewBox(int id,bool check){
312         try{
313                 checkInvariant();
314                 cutmanager->ShowViewBox(id, check);
315
316         }catch(CutModelException e){
317                 showErrorDialog(e.getCause());
318         }
319 }
320
321 void CutModelMainPanel::ShowPopUpMenu(int id){
322         showErrorDialog("test");
323 }
324
325 void CutModelMainPanel::changeColor(int id,double r,double g,double b){
326
327         try{
328                 checkInvariant();
329                 cutmanager->changeColor(id, r, g, b);
330
331         }catch(CutModelException e){
332                 showErrorDialog(e.getCause());
333         }
334 }
335
336 void CutModelMainPanel::ChangeShape(int id,int selection){
337         try{
338                 checkInvariant();
339
340                 cutmanager->ChangeShape(id, selection);
341
342         }catch(CutModelException e){
343                 showErrorDialog(e.getCause());
344         }
345 }
346 void CutModelMainPanel::updateActorDirection(int id){
347         try{
348                 checkInvariant();
349
350                 cutmanager->updateActorDirection(id);
351
352         }catch(CutModelException e){
353                 showErrorDialog(e.getCause());
354         }
355 }
356
357 void CutModelMainPanel::ExecuteCut(int id, double* range, bool isinside){
358         try{
359                 checkInvariant();
360
361                 cutmanager->ExecuteCut(id, range, isinside);
362         }catch(CutModelException e){
363                 showErrorDialog(e.getCause());
364         }
365 }
366
367 void CutModelMainPanel::ExecuteAll(){
368         int i;
369         for(i = 0; i < (int)viewpanels.size(); i++){
370                 viewpanels[i]->ExecuteCut();
371         }
372 }
373
374 vtkImageData* CutModelMainPanel::GetResultImage(){
375         try{
376                 checkInvariant();
377                 return cutmanager->GetResultImage();
378         }catch(CutModelException e){
379                 showErrorDialog(e.getCause());
380         }
381         return NULL;
382 }
383
384 void CutModelMainPanel::ShowStatistics(int id){
385
386         checkInvariant();
387         HistogramDialog *histo = new HistogramDialog(this, _T("Statistics"));
388
389         histo->initializeHistogram(cutmanager->GetResultImage());
390
391         histo->ShowModal();
392
393         delete histo;
394 }
395
396 void CutModelMainPanel::SaveCutModelData(std::string filename){
397         cutmanager->SaveCutModelData(filename);
398 }
399
400 void CutModelMainPanel::LoadCutModelData(std::string filename){
401         cutmanager->LoadCutModelData(filename);
402 }
403
404 void CutModelMainPanel::SetType(int type){
405         _type = type;
406 }
407
408 int CutModelMainPanel::GetType(){
409         return _type;
410 }
411
412 void CutModelMainPanel::addPolygonCutterTab(){
413         wxPanel *panel = new wxPanel(_notebook);
414         panel->SetAutoLayout(true);
415         wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
416         panel->SetSizer(sizer);
417         sizer->AddSpacer(20);
418
419         wxCheckBox *item = new wxCheckBox(panel,10,_T("Draw Polygon"),wxDefaultPosition,wxDefaultSize,0,wxDefaultValidator,_T("") );
420         item->SetValue(false);
421         Connect(item->GetId(), wxEVT_COMMAND_CHECKBOX_CLICKED,(wxObjectEventFunction)(&PolygonCutterEventHandlerCutModel::onCheckChanged));
422         sizer->Add(item, 0, wxALIGN_LEFT);
423
424         lblMessagePolygon = new wxStaticText(panel,30,_T("No contour drawed"),wxDefaultPosition,wxDefaultSize,0,_T("") );
425         sizer->Add(lblMessagePolygon, 0, wxALIGN_LEFT);
426
427         wxString choices0[2];
428         choices0[0] = _T("inside");
429         choices0[1] = _T("outside");
430         _radioinsideout = new  wxRadioBox(panel,-1,_T(""), wxDefaultPosition, wxDefaultSize,2,choices0);
431         sizer->Add(_radioinsideout, wxALIGN_LEFT);
432
433         btnExecutePolygonCut = new wxButton(panel,20,_T("Execute Cut"),wxDefaultPosition,wxDefaultSize,0,wxDefaultValidator,_T("") );
434         Connect(btnExecutePolygonCut->GetId(), wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)(&PolygonCutterEventHandlerCutModel::onExecuteCutPolygon));
435         sizer->Add(btnExecutePolygonCut, 0, wxALIGN_LEFT);
436
437         _notebook->AddPage(panel, _T("Polygon Cutter"), false);
438 }
439
440 void PolygonCutterEventHandlerCutModel::onCheckChanged(wxCommandEvent& event){
441         CutModelMainPanel::getInstance()->onCheckChanged();
442 }
443
444 void PolygonCutterEventHandlerCutModel::onExecuteCutPolygon(wxCommandEvent& event){
445         CutModelMainPanel::getInstance()->onExecuteCutPolygon();
446 }
447
448 /**
449 **
450 **/
451 ToolBarCutModel::ToolBarCutModel(wxWindow * parent)
452 : wxToolBar(parent, -1, wxDefaultPosition, wxDefaultSize)
453 {
454         wxBitmap bitmap10(Add_xpm);
455         this->AddTool(10, wxString(_T("Add")),bitmap10);
456
457         this->AddSeparator();
458
459         wxBitmap bitmap20(Undo_xpm);
460         this->AddTool(20, wxString(_T("Undo")),bitmap20);
461
462         wxBitmap bitmap30(Redo_xpm);
463         this->AddTool(30, wxString(_T("Redo")),bitmap30);
464
465         this->AddSeparator();
466
467         wxBitmap bitmap40(OkAll_xpm);
468         this->AddTool(40, wxString(_T("Ok All")),bitmap40);
469
470         this->Realize();
471
472         _evthand = new ToolBarEventHandlerCutModel();
473         this->SetEventHandler(_evthand);
474 }
475
476 ToolBarCutModel::~ToolBarCutModel(void){
477 }
478
479 ToolBarEventHandlerCutModel::ToolBarEventHandlerCutModel()
480 : wxEvtHandler(){
481 }
482 ToolBarEventHandlerCutModel::~ToolBarEventHandlerCutModel(){
483 }
484
485 void ToolBarEventHandlerCutModel::onAdd(wxCommandEvent& event){
486         CutModelMainPanel::getInstance()->onAddCutModel();
487 }
488
489 void ToolBarEventHandlerCutModel::onUndo(wxCommandEvent& event){
490         CutModelMainPanel::getInstance()->onUndo();
491 }
492
493 void ToolBarEventHandlerCutModel::onRedo(wxCommandEvent& event){
494         CutModelMainPanel::getInstance()->onRedo();
495 }
496
497 void ToolBarEventHandlerCutModel::onExecuteAll(wxCommandEvent& event){
498         CutModelMainPanel::getInstance()->ExecuteAll();
499 }
500
501 BEGIN_EVENT_TABLE(ToolBarEventHandlerCutModel, wxEvtHandler)
502         EVT_MENU(10, ToolBarEventHandlerCutModel::onAdd)
503         EVT_MENU(20, ToolBarEventHandlerCutModel::onUndo)
504         EVT_MENU(30, ToolBarEventHandlerCutModel::onRedo)
505         EVT_MENU(40, ToolBarEventHandlerCutModel::onExecuteAll)
506 END_EVENT_TABLE()