]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/tools/MaracasTools.cpp
Support #1768 CREATIS Licence insertion
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / tools / MaracasTools.cpp
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    INCLUDES WXWIDGETS
28 -----------------------------------------------------------------*/
29 #include "wx/wxprec.h"
30 #ifdef __BORLANDC__
31 #pragma hdrstop
32 #endif
33 #ifndef WX_PRECOMP
34 #include "wx/wx.h"
35 #endif
36 /*-----------------------------------------------------------------*/
37
38 //disable warnings on 255 char debug symbols
39 #pragma warning (disable : 4786) 
40
41
42 #include "osgi/framework/Bundle.h"
43 #include "osgi/framework/BundleContext.h"
44
45 #include "MaracasTools.h"
46 #include "interface/wxWindows/widgets/wxManualSegmentation_MPRWidget.h"
47
48 #include <iostream>
49
50 using namespace std;
51
52 /*----------------------------------------------------
53 Instance. Singleton pattern in this class
54 -----------------------------------------------------*/
55 MaracasTools* MaracasTools::INSTANCE = NULL;
56
57
58 /*----------------------------------------------------
59 GetInstance method
60 -----------------------------------------------------*/
61 MaracasTools* MaracasTools::GetInstance(){
62         if (INSTANCE == NULL){
63                 INSTANCE = new MaracasTools();
64         }
65         return INSTANCE;
66 }
67
68
69 /*----------------------------------------------------
70 Constructor
71 -----------------------------------------------------*/
72 MaracasTools::MaracasTools(){
73         framework = new Framework();
74         if(( logger=freopen("ToolsLogger.txt","w",stdout)) == NULL){
75                 exit(-1);
76         }
77         std::cout << "Framework Creado "<<std::endl;
78         
79         toolsFrame = NULL;
80         optionsFrame = NULL;
81         currentToolPanel = NULL;
82 }
83
84
85 /*----------------------------------------------------
86 It must load all the bundles this class is going to use.
87 -----------------------------------------------------*/
88 wxFrame* MaracasTools::GetToolbox(wxWindow* parent){
89         
90         
91         //1. Install ToolboxBundle
92         //-----------------------------------------------------------------------------------------------
93         std::cout << "Cargando Toolbox..." <<std::endl;
94         Bundle* bundle = framework->InstallBundle("ToolboxBundle");
95         bundle->Start(); 
96         //-----------------------------------------------------------------------------------------------
97
98         
99         
100         
101         //2. Query the OSGi framework for the IToolbox service (should be delivered in 1).
102         //-----------------------------------------------------------------------------------------------
103         BundleContext* ctx = bundle->getBundleContext();
104         const string interfaceName = "ToolboxService";
105         ServiceReference* ref = ctx->GetServiceReference(interfaceName); 
106         if (ref != NULL){
107                 toolbox = (IToolbox*)ctx->GetService(ref);
108                 toolbox->SetClient(this); // ACTIVATE THE CALLBACK: WHEN TOOLBOX NEES TO KNOW ANY PARAMETER
109                                                                   // THE METHOD GetParameter ON THE CLIENT WILL BE CALLED.
110         }
111         //-----------------------------------------------------------------------------------------------
112
113
114         
115
116         //3.Install tools
117         //TODO: Replace for discovery of bundles in the maracas' execute directory.
118         //-----------------------------------------------------------------------------------------------
119         Bundle* bundle1 = framework->InstallBundle("SimpleSegBundle");
120         bundle1->Start();
121
122         Bundle* bundle2 = framework->InstallBundle("FM3DBundle");
123         bundle2->Start();
124         //-----------------------------------------------------------------------------------------------
125
126
127         //4.Build Floating bar
128         //-----------------------------------------------------------------------------------------------
129         //FRAME
130         wxSize _size = wxSize(30,600);
131         toolsFrame = new wxFrame(parent,-1,_T("Maracas Tools"),wxDefaultPosition,_size);//,wxCAPTION | wxMINIMIZE | wxSTAY_ON_TOP | wxFRAME_TOOL_WINDOW | wxRESIZE_BORDER | wxCLOSE_BOX);
132
133         //SIZER
134         wxBoxSizer *sizer= new wxBoxSizer(wxVERTICAL);
135         
136         //BUTTONS
137         wxButton* button = NULL;
138
139
140         int count = toolbox->GetToolsCount();
141         
142                 
143         //Storyboard
144         //4.1. MaracasTools asks IToolbox about registered ITools..
145         //-----------------------------------------------------------------------------------------------
146         int i=0;
147 //      char* name = "";
148         char name[255]="";
149         for(i=0;i<count;i++){
150
151                 string stName = toolbox->GetToolName(i);
152                 sprintf(name,"%s",stName.c_str());
153         
154                 button = new wxButton(toolsFrame,-1,_T(name));
155                 SetIndexForButton(button->GetId());
156
157             sizer->Add( button,1, wxALL, 0);
158                 toolsFrame->Connect(button->GetId(),wxEVT_COMMAND_BUTTON_CLICKED  , (wxObjectEventFunction) &wxToolsHandler::OnSelectToolEvent,NULL,this);
159         }
160         //-----------------------------------------------------------------------------------------------
161         
162         
163
164         //CONFIG FRAME
165         //-----------------------------------------------------------------------------------------------
166         toolsFrame->SetSize(25,600);
167         toolsFrame->SetAutoLayout(true);
168         toolsFrame->SetSizer(sizer);
169         toolsFrame->Layout();
170         
171
172         //BUILD TOOLS OPTIONS FRAME
173         //-----------------------------------------------------------------------------------------------
174         wxSize optSize = wxSize(300,300);
175         optionsFrame = new wxFrame(toolsFrame,-1,_T("Tool"),wxDefaultPosition,optSize);//,wxCAPTION | wxMINIMIZE |  wxSTAY_ON_TOP  | wxRESIZE_BORDER | wxCLOSE_BOX );
176         
177
178         return toolsFrame;
179         
180 }
181
182
183
184
185
186 void MaracasTools::OnSelectToolEvent(wxCommandEvent& event){
187
188
189         int id = event.GetId();
190
191         int idx = GetIndexForButton(id);
192         
193         const string name = toolbox->GetToolName(idx);
194
195         wxSizer* sizer = optionsFrame->GetSizer();
196
197         if (sizer == NULL){
198                 sizer= new wxBoxSizer(wxBOTH);
199                 optionsFrame->SetSizer(sizer);
200         }
201
202
203         if (currentToolPanel != NULL){
204                 //bool t = sizer->Remove(currentToolPanel);
205                 currentToolPanel->Destroy();
206         }
207         
208
209         currentToolPanel = toolbox->GetToolPanel(optionsFrame,idx);
210         sizer->Add(currentToolPanel, 1, wxEXPAND, 0);
211         sizer->Layout();
212         
213         optionsFrame->SetTitle(_T(name.c_str()));
214         optionsFrame->Layout();
215         optionsFrame->Show();
216
217
218 }
219
220
221
222 int MaracasTools::GetIndexForButton(int _wxbuttonid){
223         
224         for(int i=0; i < ids.size(); i++){
225                 if (ids[i] == _wxbuttonid){
226                         return i;
227                 }
228         }
229         return -1; //NOT FOUND.
230 }
231
232
233 void MaracasTools::SetIndexForButton(int _wxbuttonid){
234         ids.push_back(_wxbuttonid);
235         int size = ids.size();
236 }
237
238
239 IParameter* MaracasTools::GetParameter(string name){
240         
241         
242
243         IParameter* parameter = NULL;
244
245         if (name == string("MARACAS_MPR_XYZ")){
246
247                 double* xyz = new double[3];
248                 xyz[0] =widget->GetVtkMPRBaseData()->GetX();
249                 xyz[1] =widget->GetVtkMPRBaseData()->GetY();
250                 xyz[2] =widget->GetVtkMPRBaseData()->GetZ();
251                 parameter = new IParameter(xyz);
252         }
253
254         else if (name == string("MARACAS_MPR_RENDERER")){
255                 vtkRenderer* renderer = widget->GetRenderer();
256                 parameter = new IParameter(renderer);
257         }
258
259         else if (name == string("MARACAS_MPR_IMAGE")){
260                 vtkImageData* image = widget->GetVtkMPRBaseData()->GetImageData();
261                 parameter = new IParameter(image);
262         }
263
264         else if (name == string("MARACAS_MPR_REFRESH")){
265                 widget->Refresh();
266         }
267
268          
269         return parameter;
270 }