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