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