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