]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMAppliDescriptionPanel.cpp
710d1eefdc1bc344fcd192bcb89a75d7095ec16b
[crea.git] / lib / creaDevManagerLib / wxCDMAppliDescriptionPanel.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sant�)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and
11 #  abiding by the rules of distribution of free software. You can  use,
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B
13 #  license as circulated by CEA, CNRS and INRIA at the following URL
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability.
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26  */
27
28 /*
29  * wxCDMAppliDescriptionPanel.cpp
30  *
31  *  Created on: Nov 27, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "wxCDMAppliDescriptionPanel.h"
36
37
38
39 #include "wxCDMMainFrame.h"
40
41 #include "wxCDMAppliHelpDialog.h"
42
43 #include "creaDevManagerIds.h"
44 #include "images/ApIcon64.xpm"
45
46 BEGIN_EVENT_TABLE(wxCDMAppliDescriptionPanel, wxPanel)
47 EVT_HYPERLINK(ID_BUTTON_PREV, wxCDMAppliDescriptionPanel::OnBtnReturn)
48 EVT_HYPERLINK(ID_LINK_SELECT_APPLICATION, wxCDMAppliDescriptionPanel::OnLnkApplicationSelect)
49 EVT_BUTTON(ID_BUTTON_CREATE_APPLICATION, wxCDMAppliDescriptionPanel::OnBtnCreateApplication)
50 EVT_BUTTON(ID_BUTTON_EDIT_CMAKELISTSFILE, wxCDMAppliDescriptionPanel::OnBtnEditCMakeLists)
51 EVT_BUTTON(ID_BUTTON_OPEN_FOLDER, wxCDMAppliDescriptionPanel::OnBtnOpenFolder)
52 EVT_CHECKBOX(ID_CHECK_INCLUDE_APPLICATION, wxCDMAppliDescriptionPanel::OnChBApplicationChange)
53 END_EVENT_TABLE()
54
55 wxCDMAppliDescriptionPanel::wxCDMAppliDescriptionPanel(
56     wxWindow* parent,
57     modelCDMAppli* appli,
58     wxWindowID id,
59     const wxString& caption,
60     const wxPoint& pos,
61     const wxSize& size,
62     long style
63 )
64 {
65   wxCDMAppliDescriptionPanel::Create(parent, appli, id, caption, pos, size, style);
66 }
67
68 wxCDMAppliDescriptionPanel::~wxCDMAppliDescriptionPanel()
69 {
70 }
71
72 bool wxCDMAppliDescriptionPanel::Create(
73     wxWindow* parent,
74     modelCDMAppli* appli,
75     wxWindowID id,
76     const wxString& caption,
77     const wxPoint& pos,
78     const wxSize& size,
79     long style
80 )
81 {
82   wxPanel::Create(parent, id, pos, size, style);
83
84   // this part makes the scrollbars show up
85   this->FitInside(); // ask the sizer about the needed size
86   this->SetScrollRate(5, 5);
87
88   this->appli = appli;
89   CreateControls();
90   return TRUE;
91 }
92
93 void wxCDMAppliDescriptionPanel::CreateControls()
94 {
95   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
96
97   //Links to return
98   wxBoxSizer *linksSizer = new wxBoxSizer(wxHORIZONTAL);
99   std::vector<modelCDMIProjectTreeNode*> parents = this->appli->GetParents();
100   for (int i = 0; i < (int)(parents.size()); i++)
101     {
102       wxHyperlinkCtrl* returnLnk = new wxHyperlinkCtrl(this, ID_BUTTON_PREV, crea::std2wx(parents[parents.size()-1-i]->GetName()), crea::std2wx(parents[parents.size()-1-i]->GetPath()), wxDefaultPosition, wxDefaultSize, wxHL_DEFAULT_STYLE);
103       returnLnk->SetWindowStyle(wxNO_BORDER);
104       returnLnk->SetToolTip(crea::std2wx("Return to " + parents[parents.size()-1-i]->GetName() + "."));
105       linksSizer->Add(returnLnk, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 5);
106       linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
107     }
108   linksSizer->Add(new wxStaticText(this, wxID_ANY, crea::std2wx(this->appli->GetName())), 0, wxALIGN_CENTER, 0);
109
110   sizer->Add(linksSizer, 0, wxALIGN_CENTER | wxALL, 5);
111
112   //Header
113   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
114   {
115     //Image
116     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(ApIcon64)),0, wxALIGN_CENTER, 0);
117     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
118     //Title
119     textSizer->Add(new wxStaticText(this, -1, _("Application Management")),0, wxALIGN_LEFT, 0);
120
121     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
122   }
123   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
124
125   //Applications
126   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("A&vailable Applications"));
127   propertiesBox->GetStaticBox()->SetToolTip(wxT("Select any of the available applications to see its details or the modify them."));
128   wxPanel* propertiesPanel = new wxPanel(this);
129
130   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
131   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(applications.size()+1, 3, 9, 5);
132
133   wxStaticText* ChBTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Include in\nCMake"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
134   wxStaticText* LkTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Application Name"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
135   wxStaticText* HlpTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Help"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
136
137   propertiesGridSizer -> Add(ChBTitle, 0, wxEXPAND | wxALL, 5);
138   propertiesGridSizer -> Add(LkTitle,  0, wxEXPAND | wxALL, 5);
139   propertiesGridSizer -> Add(HlpTitle, 0, wxEXPAND | wxALL, 5);
140
141   for (int i = 0; i < (int)(applications.size()); i++)
142     {
143       //checkbox for cmake inclusion
144       wxCheckBox* pApplicationChB = new wxCheckBox(propertiesPanel, ID_CHECK_INCLUDE_APPLICATION, wxT(""), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
145       pApplicationChB->SetName(crea::std2wx(applications[i]->GetName()));
146       std::string tt = "if this box is checked the the " + applications[i]->GetName() + " application is included in the project compilation.";
147       pApplicationChB->SetToolTip(crea::std2wx(tt));
148       pApplicationChB->SetValue(this->appli->IsApplicationIncluded(applications[i]->GetName()));
149       propertiesGridSizer -> Add(pApplicationChB, 0, wxEXPAND | wxALIGN_CENTER);
150
151       //link to library with description
152       wxHyperlinkCtrl* pApplicationlk = new wxHyperlinkCtrl(propertiesPanel, ID_LINK_SELECT_APPLICATION, crea::std2wx(applications[i]->GetName().c_str()), crea::std2wx(applications[i]->GetName().c_str()), wxDefaultPosition, wxDefaultSize, wxHL_DEFAULT_STYLE);
153       pApplicationlk->SetWindowStyle(wxALIGN_LEFT | wxNO_BORDER);
154       tt = "Name: " + applications[i]->GetName() + "\n";
155       tt += "Location: " + applications[i]->GetPath();
156       pApplicationlk->SetToolTip(crea::std2wx(tt));
157       pApplicationlk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnMouseEnter,NULL,this);
158       pApplicationlk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnMouseExit,NULL,this);
159       propertiesGridSizer -> Add(pApplicationlk, 0, wxEXPAND);
160
161       //help icon
162       wxButton* pApplicationHlp = new wxButton(propertiesPanel, wxID_ANY, wxT("?"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
163       propertiesGridSizer -> Add(pApplicationHlp, 0, wxEXPAND | wxALIGN_CENTER);
164     }
165
166   propertiesGridSizer->AddGrowableCol(1,1);
167
168   propertiesPanel->SetSizer(propertiesGridSizer);
169   propertiesGridSizer->Fit(propertiesPanel);
170
171   propertiesBox->Add(propertiesPanel, 1, wxEXPAND | wxALL, 5);
172   sizer -> Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
173
174   //Actions
175   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Actions"));
176   actionsBox->GetStaticBox()->SetToolTip(wxT("Create a new application or make any change to the appli's CMakeLists.txt file by selecting the desired action."));
177   wxPanel* actionsPanel = new wxPanel(this);
178   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
179   //actionsGrid Sizer
180   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
181
182   wxButton* createApplicationbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_APPLICATION, _T("A. Create Application"));
183   createApplicationbt->SetToolTip(wxT("Create a new application for this project."));
184   actionsGridSizer->Add(createApplicationbt, 1, wxALL | wxEXPAND, 5);
185   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("Edit CMakeLists File"));
186   editCMakebt->SetToolTip(wxT("Open the system default text editor to edit the CMakeLists.txt file."));
187   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnCMakeMouseEnter,NULL,this);
188   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnCMakeMouseExit,NULL,this);
189   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
190   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("Open Applications Folder"));
191   openFolderbt->SetToolTip(wxT("Open the appli folder in the file explorer."));
192   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
193
194   actionsGridSizer->AddGrowableCol(0,1);
195   actionsGridSizer->AddGrowableCol(1,1);
196
197   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
198   actionsPanel->SetSizer(actionsPanelSizer);
199   actionsPanelSizer->Fit(actionsPanel);
200   actionsBox->Add(actionsPanel, 0, wxALL | wxEXPAND, 5);
201   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
202
203   //Assign sizer
204   SetSizer(sizer);
205   sizer->SetSizeHints(this);
206
207   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
208     {
209       wxCDMAppliHelpDialog* helpDialog = new wxCDMAppliHelpDialog(this->GetParent(), this->appli, wxID_ANY);
210       helpDialog->Show(true);
211     }
212 }
213
214 void wxCDMAppliDescriptionPanel::OnBtnCreateApplication(wxCommandEvent& event)
215 {
216   //get name
217   wxTextEntryDialog* appDlg = new wxTextEntryDialog(
218         this,
219         wxT("Enter the new application name (NO white spaces)"),
220         wxT("New Application - creaDevManager"),
221         wxT(""),
222         wxOK | wxCANCEL
223     );
224
225     if (appDlg->ShowModal() == wxID_OK)
226       {
227         std::string applicationName = crea::wx2std(appDlg->GetValue());
228         //check name
229         if(applicationName.size() > 0)
230           {
231             wxArrayString types;
232             types.Add(wxT("Console Application"));
233             types.Add(wxT("GUI Application (wxWidgets)"));
234             int applicationType = wxGetSingleChoiceIndex(
235                 wxT("Select the application type"),
236                 wxT("New Application - creaDevManager"),
237                 types
238             );
239
240             if (applicationType != -1)
241               {
242                 std::string* result;
243                 //create library
244                 modelCDMIProjectTreeNode* application = this->appli->CreateApplication(applicationName, applicationType ,result);
245                 //check library created
246                 if(application == NULL)
247                   {
248                     wxMessageBox(crea::std2wx(*result),_T("New Application - Error!"),wxOK | wxICON_ERROR);
249                     event.Skip();
250                     return;
251                   }
252                 wxMessageBox(crea::std2wx("Application successfully created."),_T("New Application - Success!"),wxOK | wxICON_INFORMATION);
253
254                 //refreshing tree and description
255                 //send event instead of calling parent to avoid crashing
256
257                 ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
258
259                 wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
260                 newEvent->SetClientData(application);
261                 wxPostEvent(this->GetParent(), *newEvent);
262                 event.Skip();
263               }
264           }
265         else
266           {
267             wxMessageBox(crea::std2wx("Invalid application name, please try again with a valid name."),_T("New Application - Error!"),wxOK | wxICON_INFORMATION);
268           }
269       }
270 }
271
272 void wxCDMAppliDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
273 {
274   std::string* result;
275   if(!this->appli->OpenCMakeListsFile(result))
276     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
277
278   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
279
280   if(this->appli->GetCMakeLists() != NULL)
281     {
282       newEvent->SetClientData(this->appli->GetCMakeLists());
283       newEvent->SetId(0);
284       wxPostEvent(this->GetParent(), *newEvent);
285     }
286 }
287
288 void wxCDMAppliDescriptionPanel::OnChBApplicationChange(wxCommandEvent& event)
289 {
290   this->appli->SetApplicationInclude(
291       crea::wx2std(((wxCheckBox*)event.GetEventObject())->GetName()),
292       ((wxCheckBox*)event.GetEventObject())->GetValue()
293     );
294 }
295
296 void wxCDMAppliDescriptionPanel::OnLnkApplicationSelect(wxHyperlinkEvent& event)
297 {
298   modelCDMApplication* applicationFound = NULL;
299   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
300   for (int i = 0; i < (int)(applications.size()); i++)
301     {
302       if(applications[i]->GetName() == crea::wx2std(event.GetURL()))
303         {
304           applicationFound = applications[i];
305           break;
306         }
307     }
308   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
309   newEvent1->SetClientData(applicationFound);
310   newEvent1->SetId(0);
311   wxPostEvent(this->GetParent(), *newEvent1);
312
313   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
314   newEvent->SetClientData(applicationFound);
315   newEvent->SetId(0);
316   wxPostEvent(this->GetParent(), *newEvent);
317 }
318
319 void wxCDMAppliDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
320 {
321   std::vector<modelCDMIProjectTreeNode*> parents = this->appli->GetParents();
322   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
323   //std::cout << parentURL << std::endl;
324   for (int i = 0; i < (int)(parents.size()); i++)
325     {
326       if (parents[i]->GetPath() == parentURL)
327         {
328           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
329           newEvent->SetClientData(parents[i]);
330           newEvent->SetId(0);
331           wxPostEvent(this->GetParent(), *newEvent);
332         }
333     }
334 }
335
336 void wxCDMAppliDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
337 {
338   std::string* result;
339   if(!this->appli->OpenInFileExplorer(result))
340     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
341 }
342
343 void wxCDMAppliDescriptionPanel::OnMouseEnter(wxMouseEvent& event)
344 {
345   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
346   std::string AppName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
347   modelCDMApplication* theApp = NULL;
348   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
349   for (int i = 0; i < (int)(applications.size()); i++)
350     {
351       if(applications[i]->GetName() == AppName)
352         {
353           theApp = applications[i];
354           break;
355         }
356     }
357   newEvent->SetClientData(theApp);
358   newEvent->SetId(0);
359   wxPostEvent(this->GetParent(), *newEvent);
360   event.Skip();
361 }
362
363 void wxCDMAppliDescriptionPanel::OnMouseExit(wxMouseEvent& event)
364 {
365   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
366   std::string AppName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
367   modelCDMApplication* theApp = NULL;
368   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
369   for (int i = 0; i < (int)(applications.size()); i++)
370     {
371       if(applications[i]->GetName() == AppName)
372         {
373           theApp = applications[i];
374           break;
375         }
376     }
377   newEvent->SetClientData(theApp);
378   newEvent->SetId(0);
379   wxPostEvent(this->GetParent(), *newEvent);
380   event.Skip();
381 }
382
383 void wxCDMAppliDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
384 {
385   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
386
387   if(this->appli->GetCMakeLists() != NULL)
388     {
389       newEvent->SetClientData(this->appli->GetCMakeLists());
390       newEvent->SetId(0);
391       wxPostEvent(this->GetParent(), *newEvent);
392     }
393   event.Skip();
394 }
395
396 void wxCDMAppliDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
397 {
398   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
399
400   if(this->appli->GetCMakeLists() != NULL)
401     {
402       newEvent->SetClientData(this->appli->GetCMakeLists());
403       newEvent->SetId(0);
404       wxPostEvent(this->GetParent(), *newEvent);
405     }
406   event.Skip();
407 }