]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMAppliDescriptionPanel.cpp
Feature #1711
[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 END_EVENT_TABLE()
53
54 wxCDMAppliDescriptionPanel::wxCDMAppliDescriptionPanel(
55     wxWindow* parent,
56     modelCDMAppli* appli,
57     wxWindowID id,
58     const wxString& caption,
59     const wxPoint& pos,
60     const wxSize& size,
61     long style
62 )
63 {
64   wxCDMAppliDescriptionPanel::Create(parent, appli, id, caption, pos, size, style);
65 }
66
67 wxCDMAppliDescriptionPanel::~wxCDMAppliDescriptionPanel()
68 {
69 }
70
71 bool wxCDMAppliDescriptionPanel::Create(
72     wxWindow* parent,
73     modelCDMAppli* appli,
74     wxWindowID id,
75     const wxString& caption,
76     const wxPoint& pos,
77     const wxSize& size,
78     long style
79 )
80 {
81   wxPanel::Create(parent, id, pos, size, style);
82   this->appli = appli;
83   CreateControls();
84   return TRUE;
85 }
86
87 void wxCDMAppliDescriptionPanel::CreateControls()
88 {
89   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
90
91   //Links to return
92   wxBoxSizer *linksSizer = new wxBoxSizer(wxHORIZONTAL);
93   std::vector<modelCDMIProjectTreeNode*> parents = this->appli->GetParents();
94   for (int i = 0; i < (int)(parents.size()); i++)
95     {
96       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);
97       returnLnk->SetWindowStyle(wxNO_BORDER);
98       returnLnk->SetToolTip(crea::std2wx("Return to " + parents[parents.size()-1-i]->GetName() + "."));
99       linksSizer->Add(returnLnk, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 5);
100       if (i < (int)(parents.size())-1)
101         {
102           linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
103         }
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   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
125
126   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
127   for (int i = 0; i < (int)(applications.size()); i++)
128     {
129       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);
130       pApplicationlk->SetWindowStyle(wxALIGN_LEFT | wxNO_BORDER);
131       std::string tt = "Name: " + applications[i]->GetName() + "\n";
132       tt += "Location: " + applications[i]->GetPath();
133       pApplicationlk->SetToolTip(crea::std2wx(tt.c_str()));
134       pApplicationlk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnMouseEnter,NULL,this);
135       pApplicationlk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnMouseExit,NULL,this);
136       propertiesPanelSizer -> Add(pApplicationlk, 1, wxEXPAND | wxALL, 5);
137     }
138
139   propertiesPanel->SetSizer(propertiesPanelSizer);
140   propertiesPanelSizer->Fit(propertiesPanel);
141   propertiesBox->Add(propertiesPanel, 1, wxEXPAND | wxALL, 5);
142   sizer -> Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
143
144   //Actions
145   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Actions"));
146   actionsBox->GetStaticBox()->SetToolTip(wxT("Create a new application or make any change to the appli's CMakeLists.txt file by selecting the desired action."));
147   wxPanel* actionsPanel = new wxPanel(this);
148   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
149   //actionsGrid Sizer
150   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
151
152   wxButton* createApplicationbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_APPLICATION, _T("A. Create Application"));
153   createApplicationbt->SetToolTip(wxT("Create a new application for this project."));
154   actionsGridSizer->Add(createApplicationbt, 1, wxALL | wxEXPAND, 5);
155   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("B. Edit CMakeLists File"));
156   editCMakebt->SetToolTip(wxT("Open the system default text editor to edit the CMakeLists.txt file."));
157   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnCMakeMouseEnter,NULL,this);
158   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMAppliDescriptionPanel::OnCMakeMouseExit,NULL,this);
159   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
160   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("C. Open Applications Folder"));
161   openFolderbt->SetToolTip(wxT("Open the appli folder in the file explorer."));
162   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
163
164   actionsGridSizer->AddGrowableCol(0,1);
165   actionsGridSizer->AddGrowableCol(1,1);
166
167   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
168   actionsPanel->SetSizer(actionsPanelSizer);
169   actionsPanelSizer->Fit(actionsPanel);
170   actionsBox->Add(actionsPanel, 0, wxALL | wxEXPAND, 5);
171   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
172
173   //Assign sizer
174   SetSizer(sizer);
175   sizer->SetSizeHints(this);
176
177   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
178     {
179       wxCDMAppliHelpDialog* helpDialog = new wxCDMAppliHelpDialog(this->GetParent(), this->appli, wxID_ANY);
180       helpDialog->Show(true);
181     }
182 }
183
184 void wxCDMAppliDescriptionPanel::OnBtnCreateApplication(wxCommandEvent& event)
185 {
186   //get name
187   wxTextEntryDialog* appDlg = new wxTextEntryDialog(
188         this,
189         wxT("Enter the new application name (NO white spaces)"),
190         wxT("New Application - creaDevManager"),
191         wxT(""),
192         wxOK | wxCANCEL
193     );
194
195     if (appDlg->ShowModal() == wxID_OK)
196       {
197         std::string applicationName = crea::wx2std(appDlg->GetValue());
198         //check name
199         if(applicationName.size() > 0)
200           {
201             wxArrayString types;
202             types.Add(wxT("Console Application"));
203             types.Add(wxT("GUI Application (wxWidgets)"));
204             int applicationType = wxGetSingleChoiceIndex(
205                 wxT("Select the application type"),
206                 wxT("New Application - creaDevManager"),
207                 types
208             );
209
210             if (applicationType != -1)
211               {
212                 std::string* result;
213                 //create library
214                 modelCDMIProjectTreeNode* application = this->appli->CreateApplication(applicationName, applicationType ,result);
215                 //check library created
216                 if(application == NULL)
217                   {
218                     wxMessageBox(crea::std2wx(*result),_T("New Application - Error!"),wxOK | wxICON_ERROR);
219                     event.Skip();
220                     return;
221                   }
222                 wxMessageBox(crea::std2wx("Application successfully created."),_T("New Application - Success!"),wxOK | wxICON_INFORMATION);
223
224                 //refreshing tree and description
225                 //send event instead of calling parent to avoid crashing
226
227                 ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
228
229                 wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
230                 newEvent->SetClientData(application);
231                 wxPostEvent(this->GetParent(), *newEvent);
232                 event.Skip();
233               }
234           }
235         else
236           {
237             wxMessageBox(crea::std2wx("Invalid application name, please try again with a valid name."),_T("New Application - Error!"),wxOK | wxICON_INFORMATION);
238           }
239       }
240 }
241
242 void wxCDMAppliDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
243 {
244   std::string* result;
245   if(!this->appli->OpenCMakeListsFile(result))
246     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
247
248   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
249
250   if(this->appli->GetCMakeLists() != NULL)
251     {
252       newEvent->SetClientData(this->appli->GetCMakeLists());
253       newEvent->SetId(0);
254       wxPostEvent(this->GetParent(), *newEvent);
255     }
256 }
257
258 void wxCDMAppliDescriptionPanel::OnLnkApplicationSelect(wxHyperlinkEvent& event)
259 {
260   modelCDMApplication* applicationFound = NULL;
261   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
262   for (int i = 0; i < (int)(applications.size()); i++)
263     {
264       if(applications[i]->GetName() == crea::wx2std(event.GetURL()))
265         {
266           applicationFound = applications[i];
267           break;
268         }
269     }
270   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
271   newEvent1->SetClientData(applicationFound);
272   newEvent1->SetId(0);
273   wxPostEvent(this->GetParent(), *newEvent1);
274
275   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
276   newEvent->SetClientData(applicationFound);
277   newEvent->SetId(0);
278   wxPostEvent(this->GetParent(), *newEvent);
279 }
280
281 void wxCDMAppliDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
282 {
283   std::vector<modelCDMIProjectTreeNode*> parents = this->appli->GetParents();
284   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
285   //std::cout << parentURL << std::endl;
286   for (int i = 0; i < (int)(parents.size()); i++)
287     {
288       if (parents[i]->GetPath() == parentURL)
289         {
290           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
291           newEvent->SetClientData(parents[i]);
292           newEvent->SetId(0);
293           wxPostEvent(this->GetParent(), *newEvent);
294         }
295     }
296 }
297
298 void wxCDMAppliDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
299 {
300   std::string* result;
301   if(!this->appli->OpenInFileExplorer(result))
302     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
303 }
304
305 void wxCDMAppliDescriptionPanel::OnMouseEnter(wxMouseEvent& event)
306 {
307   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
308   std::string AppName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
309   modelCDMApplication* theApp = NULL;
310   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
311   for (int i = 0; i < (int)(applications.size()); i++)
312     {
313       if(applications[i]->GetName() == AppName)
314         {
315           theApp = applications[i];
316           break;
317         }
318     }
319   newEvent->SetClientData(theApp);
320   newEvent->SetId(0);
321   wxPostEvent(this->GetParent(), *newEvent);
322   event.Skip();
323 }
324
325 void wxCDMAppliDescriptionPanel::OnMouseExit(wxMouseEvent& event)
326 {
327   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
328   std::string AppName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
329   modelCDMApplication* theApp = NULL;
330   std::vector<modelCDMApplication*> applications = this->appli->GetApplications();
331   for (int i = 0; i < (int)(applications.size()); i++)
332     {
333       if(applications[i]->GetName() == AppName)
334         {
335           theApp = applications[i];
336           break;
337         }
338     }
339   newEvent->SetClientData(theApp);
340   newEvent->SetId(0);
341   wxPostEvent(this->GetParent(), *newEvent);
342   event.Skip();
343 }
344
345 void wxCDMAppliDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
346 {
347   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
348
349   if(this->appli->GetCMakeLists() != NULL)
350     {
351       newEvent->SetClientData(this->appli->GetCMakeLists());
352       newEvent->SetId(0);
353       wxPostEvent(this->GetParent(), *newEvent);
354     }
355   event.Skip();
356 }
357
358 void wxCDMAppliDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
359 {
360   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
361
362   if(this->appli->GetCMakeLists() != NULL)
363     {
364       newEvent->SetClientData(this->appli->GetCMakeLists());
365       newEvent->SetId(0);
366       wxPostEvent(this->GetParent(), *newEvent);
367     }
368   event.Skip();
369 }