]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMApplicationDescriptionPanel.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / wxCDMApplicationDescriptionPanel.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  * wxCDMApplicationDescriptionPanel.cpp
30  *
31  *  Created on: Nov 27, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "wxCDMApplicationDescriptionPanel.h"
36
37 #include "CDMUtilities.h"
38 #include "wxCDMMainFrame.h"
39
40 #include "wxCDMApplicationHelpDialog.h"
41
42 #include "creaDevManagerIds.h"
43 #include "images/AIcon64.xpm"
44
45 BEGIN_EVENT_TABLE(wxCDMApplicationDescriptionPanel, wxPanel)
46 EVT_BUTTON(ID_BUTTON_SET_NAME, wxCDMApplicationDescriptionPanel::OnBtnSetExeName)
47 EVT_HYPERLINK(ID_BUTTON_PREV, wxCDMApplicationDescriptionPanel::OnBtnReturn)
48 EVT_BUTTON(ID_BUTTON_CREATE_CLASS, wxCDMApplicationDescriptionPanel::OnBtnCreateClass)
49 EVT_BUTTON(ID_BUTTON_CREATE_FOLDER, wxCDMApplicationDescriptionPanel::OnBtnCreateFolder)
50 EVT_BUTTON(ID_BUTTON_EDIT_CMAKELISTSFILE, wxCDMApplicationDescriptionPanel::OnBtnEditCMakeLists)
51 EVT_BUTTON(ID_BUTTON_OPEN_FOLDER, wxCDMApplicationDescriptionPanel::OnBtnOpenFolder)
52 EVT_BUTTON(ID_BUTTON_OPEN_CXX, wxCDMApplicationDescriptionPanel::OnBtnOpenMain)
53 END_EVENT_TABLE()
54
55 wxCDMApplicationDescriptionPanel::wxCDMApplicationDescriptionPanel(
56     wxWindow* parent,
57     modelCDMApplication* application,
58     wxWindowID id,
59     const wxString& caption,
60     const wxPoint& pos,
61     const wxSize& size,
62     long style
63 )
64 {
65   wxCDMApplicationDescriptionPanel::Create(parent, application, id, caption, pos, size, style);
66 }
67
68 wxCDMApplicationDescriptionPanel::~wxCDMApplicationDescriptionPanel()
69 {
70 }
71
72 bool wxCDMApplicationDescriptionPanel::Create(
73     wxWindow* parent,
74     modelCDMApplication* application,
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->application = application;
84   CreateControls();
85   return TRUE;
86 }
87
88 void wxCDMApplicationDescriptionPanel::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->application->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       if (i < (int)(parents.size())-1)
102         {
103           linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
104         }
105     }
106   sizer->Add(linksSizer, 0, wxALIGN_CENTER | wxALL, 5);
107
108   //Header
109   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
110   {
111     //Image
112     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(AIcon64)),0, wxALIGN_CENTER, 0);
113     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
114     //Title
115     textSizer->Add(new wxStaticText(this, -1, _("Application")),0, wxALIGN_LEFT, 0);
116     //Application Name
117     textSizer->Add(new wxStaticText(this, -1, crea::std2wx(this->application->GetName())),0, wxALIGN_LEFT, 0);
118     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
119   }
120   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
121
122   /*  //Properties
123   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Properties"));
124   wxPanel* propertiesPanel = new wxPanel(this);
125   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
126
127   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(4, 2, 9, 15);
128
129   wxStaticText *pMainFile = new wxStaticText(propertiesPanel, -1, wxT("Executable Name"));
130   wxBoxSizer* pMainFilesz = new wxBoxSizer(wxHORIZONTAL);
131   this->executableNametc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(this->application->GetExecutableName()));
132   wxButton* pMainFilebt = new wxButton(propertiesPanel, ID_BUTTON_SET_NAME, wxT("Set"));
133   pMainFilebt->SetToolTip(wxT("Set the name of the executable file for the application."));
134   pMainFilesz->Add(this->executableNametc, 0, wxALIGN_CENTER_VERTICAL, 0);
135   pMainFilesz->Add(pMainFilebt, 0, wxALIGN_CENTER | wxLEFT, 10);
136
137   propertiesGridSizer->Add(pMainFile, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
138   propertiesGridSizer->Add(pMainFilesz, 1, wxEXPAND);
139
140   propertiesGridSizer->AddGrowableCol(1,1);
141
142   propertiesPanelSizer->Add(propertiesGridSizer, 0, wxEXPAND);
143   propertiesPanel->SetSizer(propertiesPanelSizer);
144   propertiesPanelSizer->Fit(propertiesPanel);
145   propertiesBox->Add(propertiesPanel, 0, wxEXPAND);
146   sizer->Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
147    */
148   //Actions
149   //actions StaticBoxSizer
150   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Actions"));
151   actionsBox->GetStaticBox()->SetToolTip(wxT("Create classes or edit them for this application, as well as editing the application's CMakeLists.txt file by selecting the desired action."));
152   //actions Panel
153   wxPanel* actionsPanel = new wxPanel(this);
154   //actions Sizer
155   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
156   //actionsGrid Sizer
157   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(3, 2, 9, 15);
158
159   wxButton* createClassbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_CLASS, _T("Create Class (Optional)"));
160   createClassbt->SetToolTip(wxT("Create a new Class (.h and .cxx files)."));
161   wxButton* createFolderbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_FOLDER, _T("Create Folder (Optional)"));
162   createFolderbt->SetToolTip(wxT("Create a new Folder inside the application folder."));
163   wxButton* openMainbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_CXX, _T("A. Open Main File"));
164   openMainbt->SetToolTip(wxT("Open the main file in the application folder with the default code editor."));
165   openMainbt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMApplicationDescriptionPanel::OnMainMouseEnter,NULL,this);
166   openMainbt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMApplicationDescriptionPanel::OnMainMouseExit,NULL,this);
167   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("B. Edit CMakeLists File"));
168   editCMakebt->SetToolTip(wxT("Edit the CMakeLists.txt file inside this application."));
169   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMApplicationDescriptionPanel::OnCMakeMouseEnter,NULL,this);
170   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMApplicationDescriptionPanel::OnCMakeMouseExit,NULL,this);
171   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("C. Open Application Folder"));
172   openFolderbt->SetToolTip(wxT("Open the application folder in the file explorer."));
173
174
175   actionsGridSizer->Add(openMainbt, 1, wxALL | wxEXPAND, 5);
176   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
177   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
178   actionsGridSizer->Add(createClassbt, 1, wxALL | wxEXPAND, 5);
179   actionsGridSizer->Add(createFolderbt, 1, wxALL | wxEXPAND, 5);
180
181   //SetPanel sizer and box
182   actionsGridSizer->AddGrowableCol(0,1);
183   actionsGridSizer->AddGrowableCol(1,1);
184
185   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
186   actionsPanel->SetSizer(actionsPanelSizer);
187   actionsPanelSizer->Fit(actionsPanel);
188   actionsBox->Add(actionsPanel, 1, wxALL | wxEXPAND, 5);
189   sizer->Add(actionsBox, 0, wxEXPAND | wxALL, 10);
190
191   //Assign sizer
192   SetSizer(sizer);
193   sizer->SetSizeHints(this);
194
195   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
196     {
197       wxCDMApplicationHelpDialog* helpDialog = new wxCDMApplicationHelpDialog(this->GetParent(), this->application, wxID_ANY);
198       helpDialog->Show(true);
199     }
200 }
201
202 void wxCDMApplicationDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
203 {
204   std::vector<modelCDMIProjectTreeNode*> parents = this->application->GetParents();
205   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
206   //std::cout << parentURL << std::endl;
207   for (int i = 0; i < (int)(parents.size()); i++)
208     {
209       if (parents[i]->GetPath() == parentURL)
210         {
211           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
212           newEvent->SetClientData(parents[i]);
213           newEvent->SetId(0);
214           wxPostEvent(this->GetParent(), *newEvent);
215         }
216     }
217
218 }
219
220 void wxCDMApplicationDescriptionPanel::OnBtnSetExeName(wxCommandEvent& event)
221 {
222   //get name
223   wxString versionWx = wxGetTextFromUser(
224       wxT("Enter the new executable name"),
225       wxT("Change Application Executable Name - creaDevManager"),
226       crea::std2wx(this->application->GetExecutableName())
227   );
228   //check name
229   std::vector<std::string> parts;
230   CDMUtilities::splitter::split(parts, crea::wx2std(versionWx), " .", CDMUtilities::splitter::no_empties);
231   if(parts.size() > 0)
232     {
233       std::string* result;
234       if(!this->application->SetExecutableName(crea::wx2std(versionWx), result))
235         wxMessageBox(crea::std2wx(*result),_T("Change Application Executable Name - Error!"),wxOK | wxICON_ERROR);
236     }
237   else
238     {
239       wxMessageBox(crea::std2wx("No name specified"),_T("Set Application Executable Name - Error!"),wxOK | wxICON_ERROR);
240     }
241   this->executableNametc->SetLabel(crea::std2wx(this->application->GetExecutableName()));
242 }
243
244 void wxCDMApplicationDescriptionPanel::OnBtnCreateClass(wxCommandEvent& event)
245 {
246   //get class name from user
247   wxTextEntryDialog* newClassDlg = new wxTextEntryDialog(
248       this,
249       wxT("Please enter the new class name."),
250       wxT("New Class - creaDevManager"),
251       wxT(""),
252       wxOK | wxCANCEL
253   );
254
255   if (newClassDlg->ShowModal() == wxID_OK)
256     {
257       std::string className = crea::wx2std(newClassDlg->GetValue());
258       //check class name
259       if(className.size() > 0)
260         {
261           if(!this->application->CreateClass(className))
262             wxMessageBox(crea::std2wx("Something has gone wrong with the creation of the class."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
263
264           ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
265
266           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
267           newEvent->SetId(0);
268           newEvent->SetClientData(this->application);
269           wxPostEvent(this->GetParent(), *newEvent);
270
271           wxMessageBox(crea::std2wx("The class has been created successfully."),_T("New Class - Success"),wxOK | wxICON_INFORMATION);
272         }
273       else
274         {
275           wxMessageBox(crea::std2wx("The new class name cannot be empty."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
276         }
277     }
278 }
279
280 void wxCDMApplicationDescriptionPanel::OnBtnCreateFolder(wxCommandEvent& event)
281 {
282   //get name
283   wxString folderName = wxGetTextFromUser(
284       wxT("Enter the name of the new folder:"),
285       wxT("Create Folder - creaDevManager")
286   );
287   //check name
288   std::vector<std::string> parts;
289   CDMUtilities::splitter::split(parts, crea::wx2std(folderName), " /\\\"", CDMUtilities::splitter::no_empties);
290   if(parts.size() > 0)
291     {
292       std::string* result;
293       modelCDMFolder* folderC = this->application->CreateFolder(crea::wx2std(folderName), result);
294       if(folderC == NULL)
295         {
296           wxMessageBox(crea::std2wx(*result),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
297           return;
298         }
299       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
300       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
301       newEvent->SetClientData(folderC);
302       wxPostEvent(this->GetParent(), *newEvent);
303       wxMessageBox(crea::std2wx("The folder was successfully created"),_T("Create Folder - Success"),wxOK | wxICON_INFORMATION);
304     }
305   else
306     {
307       wxMessageBox(crea::std2wx("No name specified"),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
308     }
309 }
310
311 void wxCDMApplicationDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
312 {
313   std::string* result;
314   if(!this->application->OpenCMakeListsFile(result))
315     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
316
317   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
318
319   if(this->application->GetCMakeLists() != NULL)
320     {
321       newEvent->SetClientData(this->application->GetCMakeLists());
322       newEvent->SetId(0);
323       wxPostEvent(this->GetParent(), *newEvent);
324     }
325
326   event.Skip();
327 }
328
329 void wxCDMApplicationDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
330 {
331   std::string* result;
332   if(!this->application->OpenInFileExplorer(result))
333     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
334 }
335
336 void wxCDMApplicationDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
337 {
338   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
339
340   if(this->application->GetCMakeLists() != NULL)
341     {
342       newEvent->SetClientData(this->application->GetCMakeLists());
343       newEvent->SetId(0);
344       wxPostEvent(this->GetParent(), *newEvent);
345     }
346   event.Skip();
347 }
348
349 void wxCDMApplicationDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
350 {
351   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
352
353   if(this->application->GetCMakeLists() != NULL)
354     {
355       newEvent->SetClientData(this->application->GetCMakeLists());
356       newEvent->SetId(0);
357       wxPostEvent(this->GetParent(), *newEvent);
358     }
359   event.Skip();
360 }
361
362 void wxCDMApplicationDescriptionPanel::OnBtnOpenMain(wxCommandEvent& event)
363 {
364   if (this->application->GetMainFile() != NULL)
365     {
366       if (CDMUtilities::openTextEditor(this->application->GetMainFile()->GetPath()))
367       {
368         wxMessageBox(crea::std2wx("The main file couldn't be opened."),_T("Open Main File - Error!"),wxOK | wxICON_ERROR);
369       }
370
371       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
372
373       newEvent->SetClientData(this->application->GetMainFile());
374       newEvent->SetId(0);
375       wxPostEvent(this->GetParent(), *newEvent);
376
377       event.Skip();
378     }
379   else
380     {
381       wxMessageBox(crea::std2wx("There is no main file or it couldn't be detected."),_T("Open Main File - Error!"),wxOK | wxICON_ERROR);
382     }
383 }
384
385 void wxCDMApplicationDescriptionPanel::OnMainMouseEnter(wxMouseEvent& event)
386 {
387   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
388
389   if(this->application->GetMainFile() != NULL)
390     {
391       newEvent->SetClientData(this->application->GetMainFile());
392       newEvent->SetId(0);
393       wxPostEvent(this->GetParent(), *newEvent);
394     }
395   event.Skip();
396 }
397
398 void wxCDMApplicationDescriptionPanel::OnMainMouseExit(wxMouseEvent& event)
399 {
400   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
401
402   if(this->application->GetMainFile() != NULL)
403     {
404       newEvent->SetClientData(this->application->GetMainFile());
405       newEvent->SetId(0);
406       wxPostEvent(this->GetParent(), *newEvent);
407     }
408   event.Skip();
409 }