]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMAppliDescriptionPanel.cpp
Feature #1711 CreaDevManager application implementation
[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   this->appli = appli;
84   CreateControls();
85   return TRUE;
86 }
87
88 void wxCDMAppliDescriptionPanel::CreateControls()
89 {
90   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
91
92   //Links to return
93   wxBoxSizer *linksSizer = new wxBoxSizer(wxHORIZONTAL);
94   std::vector<modelCDMIProjectTreeNode*> parents = this->appli->GetParents();
95   for (int i = 0; i < (int)(parents.size()); i++)
96     {
97       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);
98       returnLnk->SetWindowStyle(wxNO_BORDER);
99       returnLnk->SetToolTip(crea::std2wx("Return to " + parents[parents.size()-1-i]->GetName() + "."));
100       linksSizer->Add(returnLnk, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 5);
101       linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
102     }
103   linksSizer->Add(new wxStaticText(this, wxID_ANY, crea::std2wx(this->appli->GetName())), 0, wxALIGN_CENTER, 0);
104
105   sizer->Add(linksSizer, 0, wxALIGN_CENTER | wxALL, 5);
106
107   //Header
108   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
109   {
110     //Image
111     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(ApIcon64)),0, wxALIGN_CENTER, 0);
112     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
113     //Title
114     textSizer->Add(new wxStaticText(this, -1, _("Application Management")),0, wxALIGN_LEFT, 0);
115
116     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
117   }
118   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
119
120   //Applications
121   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("A&vailable Applications"));
122   propertiesBox->GetStaticBox()->SetToolTip(wxT("Select any of the available applications to see its details or the modify them."));
123   wxPanel* propertiesPanel = new wxPanel(this);
124
125   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
126   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(applications.size()+1, 3, 9, 5);
127
128   wxStaticText* ChBTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Include in\nCMake"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
129   wxStaticText* LkTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Application Name"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
130   wxStaticText* HlpTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Help"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
131
132   propertiesGridSizer -> Add(ChBTitle, 0, wxEXPAND | wxALL, 5);
133   propertiesGridSizer -> Add(LkTitle,  0, wxEXPAND | wxALL, 5);
134   propertiesGridSizer -> Add(HlpTitle, 0, wxEXPAND | wxALL, 5);
135
136   for (int i = 0; i < (int)(applications.size()); i++)
137     {
138       //checkbox for cmake inclusion
139       wxCheckBox* pApplicationChB = new wxCheckBox(propertiesPanel, ID_CHECK_INCLUDE_APPLICATION, wxT(""), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
140       pApplicationChB->SetName(crea::std2wx(applications[i]->GetName()));
141       std::string tt = "if this box is checked the the " + applications[i]->GetName() + " application is included in the project compilation.";
142       pApplicationChB->SetToolTip(crea::std2wx(tt));
143       pApplicationChB->SetValue(this->appli->IsApplicationIncluded(applications[i]->GetName()));
144       propertiesGridSizer -> Add(pApplicationChB, 0, wxEXPAND | wxALIGN_CENTER);
145
146       //link to library with description
147       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);
148       pApplicationlk->SetWindowStyle(wxALIGN_LEFT | wxNO_BORDER);
149       tt = "Name: " + applications[i]->GetName() + "\n";
150       tt += "Location: " + applications[i]->GetPath();
151       pApplicationlk->SetToolTip(crea::std2wx(tt));
152       pApplicationlk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnMouseEnter,NULL,this);
153       pApplicationlk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnMouseExit,NULL,this);
154       propertiesGridSizer -> Add(pApplicationlk, 0, wxEXPAND);
155
156       //help icon
157       wxButton* pApplicationHlp = new wxButton(propertiesPanel, wxID_ANY, wxT("?"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
158       propertiesGridSizer -> Add(pApplicationHlp, 0, wxEXPAND | wxALIGN_CENTER);
159     }
160
161   propertiesGridSizer->AddGrowableCol(1,1);
162
163   propertiesPanel->SetSizer(propertiesGridSizer);
164   propertiesGridSizer->Fit(propertiesPanel);
165
166   propertiesBox->Add(propertiesPanel, 1, wxEXPAND | wxALL, 5);
167   sizer -> Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
168
169   //Actions
170   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Actions"));
171   actionsBox->GetStaticBox()->SetToolTip(wxT("Create a new application or make any change to the appli's CMakeLists.txt file by selecting the desired action."));
172   wxPanel* actionsPanel = new wxPanel(this);
173   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
174   //actionsGrid Sizer
175   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
176
177   wxButton* createApplicationbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_APPLICATION, _T("A. Create Application"));
178   createApplicationbt->SetToolTip(wxT("Create a new application for this project."));
179   actionsGridSizer->Add(createApplicationbt, 1, wxALL | wxEXPAND, 5);
180   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("B. Edit CMakeLists File"));
181   editCMakebt->SetToolTip(wxT("Open the system default text editor to edit the CMakeLists.txt file."));
182   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnCMakeMouseEnter,NULL,this);
183   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnCMakeMouseExit,NULL,this);
184   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
185   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("C. Open Applications Folder"));
186   openFolderbt->SetToolTip(wxT("Open the appli folder in the file explorer."));
187   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
188
189   actionsGridSizer->AddGrowableCol(0,1);
190   actionsGridSizer->AddGrowableCol(1,1);
191
192   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
193   actionsPanel->SetSizer(actionsPanelSizer);
194   actionsPanelSizer->Fit(actionsPanel);
195   actionsBox->Add(actionsPanel, 0, wxALL | wxEXPAND, 5);
196   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
197
198   //Assign sizer
199   SetSizer(sizer);
200   sizer->SetSizeHints(this);
201
202   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
203     {
204       wxCDMAppliHelpDialog* helpDialog = new wxCDMAppliHelpDialog(this->GetParent(), this->appli, wxID_ANY);
205       helpDialog->Show(true);
206     }
207 }
208
209 void wxCDMAppliDescriptionPanel::OnBtnCreateApplication(wxCommandEvent& event)
210 {
211   //get name
212   wxTextEntryDialog* appDlg = new wxTextEntryDialog(
213         this,
214         wxT("Enter the new application name (NO white spaces)"),
215         wxT("New Application - creaDevManager"),
216         wxT(""),
217         wxOK | wxCANCEL
218     );
219
220     if (appDlg->ShowModal() == wxID_OK)
221       {
222         std::string applicationName = crea::wx2std(appDlg->GetValue());
223         //check name
224         if(applicationName.size() > 0)
225           {
226             wxArrayString types;
227             types.Add(wxT("Console Application"));
228             types.Add(wxT("GUI Application (wxWidgets)"));
229             int applicationType = wxGetSingleChoiceIndex(
230                 wxT("Select the application type"),
231                 wxT("New Application - creaDevManager"),
232                 types
233             );
234
235             if (applicationType != -1)
236               {
237                 std::string* result;
238                 //create library
239                 modelCDMIProjectTreeNode* application = this->appli->CreateApplication(applicationName, applicationType ,result);
240                 //check library created
241                 if(application == NULL)
242                   {
243                     wxMessageBox(crea::std2wx(*result),_T("New Application - Error!"),wxOK | wxICON_ERROR);
244                     event.Skip();
245                     return;
246                   }
247                 wxMessageBox(crea::std2wx("Application successfully created."),_T("New Application - Success!"),wxOK | wxICON_INFORMATION);
248
249                 //refreshing tree and description
250                 //send event instead of calling parent to avoid crashing
251
252                 ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
253
254                 wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
255                 newEvent->SetClientData(application);
256                 wxPostEvent(this->GetParent(), *newEvent);
257                 event.Skip();
258               }
259           }
260         else
261           {
262             wxMessageBox(crea::std2wx("Invalid application name, please try again with a valid name."),_T("New Application - Error!"),wxOK | wxICON_INFORMATION);
263           }
264       }
265 }
266
267 void wxCDMAppliDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
268 {
269   std::string* result;
270   if(!this->appli->OpenCMakeListsFile(result))
271     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
272
273   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
274
275   if(this->appli->GetCMakeLists() != NULL)
276     {
277       newEvent->SetClientData(this->appli->GetCMakeLists());
278       newEvent->SetId(0);
279       wxPostEvent(this->GetParent(), *newEvent);
280     }
281 }
282
283 void wxCDMAppliDescriptionPanel::OnChBApplicationChange(wxCommandEvent& event)
284 {
285   this->appli->SetApplicationInclude(
286       crea::wx2std(((wxCheckBox*)event.GetEventObject())->GetName()),
287       ((wxCheckBox*)event.GetEventObject())->GetValue()
288     );
289 }
290
291 void wxCDMAppliDescriptionPanel::OnLnkApplicationSelect(wxHyperlinkEvent& event)
292 {
293   modelCDMApplication* applicationFound = NULL;
294   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
295   for (int i = 0; i < (int)(applications.size()); i++)
296     {
297       if(applications[i]->GetName() == crea::wx2std(event.GetURL()))
298         {
299           applicationFound = applications[i];
300           break;
301         }
302     }
303   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
304   newEvent1->SetClientData(applicationFound);
305   newEvent1->SetId(0);
306   wxPostEvent(this->GetParent(), *newEvent1);
307
308   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
309   newEvent->SetClientData(applicationFound);
310   newEvent->SetId(0);
311   wxPostEvent(this->GetParent(), *newEvent);
312 }
313
314 void wxCDMAppliDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
315 {
316   std::vector<modelCDMIProjectTreeNode*> parents = this->appli->GetParents();
317   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
318   //std::cout << parentURL << std::endl;
319   for (int i = 0; i < (int)(parents.size()); i++)
320     {
321       if (parents[i]->GetPath() == parentURL)
322         {
323           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
324           newEvent->SetClientData(parents[i]);
325           newEvent->SetId(0);
326           wxPostEvent(this->GetParent(), *newEvent);
327         }
328     }
329 }
330
331 void wxCDMAppliDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
332 {
333   std::string* result;
334   if(!this->appli->OpenInFileExplorer(result))
335     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
336 }
337
338 void wxCDMAppliDescriptionPanel::OnMouseEnter(wxMouseEvent& event)
339 {
340   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
341   std::string AppName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
342   modelCDMApplication* theApp = NULL;
343   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
344   for (int i = 0; i < (int)(applications.size()); i++)
345     {
346       if(applications[i]->GetName() == AppName)
347         {
348           theApp = applications[i];
349           break;
350         }
351     }
352   newEvent->SetClientData(theApp);
353   newEvent->SetId(0);
354   wxPostEvent(this->GetParent(), *newEvent);
355   event.Skip();
356 }
357
358 void wxCDMAppliDescriptionPanel::OnMouseExit(wxMouseEvent& event)
359 {
360   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
361   std::string AppName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
362   modelCDMApplication* theApp = NULL;
363   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
364   for (int i = 0; i < (int)(applications.size()); i++)
365     {
366       if(applications[i]->GetName() == AppName)
367         {
368           theApp = applications[i];
369           break;
370         }
371     }
372   newEvent->SetClientData(theApp);
373   newEvent->SetId(0);
374   wxPostEvent(this->GetParent(), *newEvent);
375   event.Skip();
376 }
377
378 void wxCDMAppliDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
379 {
380   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
381
382   if(this->appli->GetCMakeLists() != NULL)
383     {
384       newEvent->SetClientData(this->appli->GetCMakeLists());
385       newEvent->SetId(0);
386       wxPostEvent(this->GetParent(), *newEvent);
387     }
388   event.Skip();
389 }
390
391 void wxCDMAppliDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
392 {
393   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
394
395   if(this->appli->GetCMakeLists() != NULL)
396     {
397       newEvent->SetClientData(this->appli->GetCMakeLists());
398       newEvent->SetId(0);
399       wxPostEvent(this->GetParent(), *newEvent);
400     }
401   event.Skip();
402 }