]> 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_PREV, wxCDMApplicationDescriptionPanel::OnBtnReturn)
47 EVT_BUTTON(ID_BUTTON_SET_NAME, wxCDMApplicationDescriptionPanel::OnBtnSetExeName)
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   //Link to return
93   wxButton* returnbt = new wxButton(this, ID_BUTTON_PREV, wxT("Return to project"));
94   returnbt->SetToolTip(wxT("Return to the active project description."));
95   sizer->Add(returnbt, 0, wxALIGN_CENTER | wxALL, 5);
96
97   //Header
98   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
99   {
100     //Image
101     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(AIcon64)),0, wxALIGN_CENTER, 0);
102     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
103     //Title
104     textSizer->Add(new wxStaticText(this, -1, _("Application")),0, wxALIGN_LEFT, 0);
105     //Application Name
106     textSizer->Add(new wxStaticText(this, -1, crea::std2wx(this->application->GetName())),0, wxALIGN_LEFT, 0);
107     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
108   }
109   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
110
111   /*  //Properties
112   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Properties"));
113   wxPanel* propertiesPanel = new wxPanel(this);
114   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
115
116   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(4, 2, 9, 15);
117
118   wxStaticText *pMainFile = new wxStaticText(propertiesPanel, -1, wxT("Executable Name"));
119   wxBoxSizer* pMainFilesz = new wxBoxSizer(wxHORIZONTAL);
120   this->executableNametc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(this->application->GetExecutableName()));
121   wxButton* pMainFilebt = new wxButton(propertiesPanel, ID_BUTTON_SET_NAME, wxT("Set"));
122   pMainFilebt->SetToolTip(wxT("Set the name of the executable file for the application."));
123   pMainFilesz->Add(this->executableNametc, 0, wxALIGN_CENTER_VERTICAL, 0);
124   pMainFilesz->Add(pMainFilebt, 0, wxALIGN_CENTER | wxLEFT, 10);
125
126   propertiesGridSizer->Add(pMainFile, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
127   propertiesGridSizer->Add(pMainFilesz, 1, wxEXPAND);
128
129   propertiesGridSizer->AddGrowableCol(1,1);
130
131   propertiesPanelSizer->Add(propertiesGridSizer, 0, wxEXPAND);
132   propertiesPanel->SetSizer(propertiesPanelSizer);
133   propertiesPanelSizer->Fit(propertiesPanel);
134   propertiesBox->Add(propertiesPanel, 0, wxEXPAND);
135   sizer->Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
136    */
137   //Actions
138   //actions StaticBoxSizer
139   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Actions"));
140   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."));
141   //actions Panel
142   wxPanel* actionsPanel = new wxPanel(this);
143   //actions Sizer
144   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
145   //actionsGrid Sizer
146   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(3, 2, 9, 15);
147
148   wxButton* createClassbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_CLASS, _T("Create Class (Optional)"));
149   createClassbt->SetToolTip(wxT("Create a new Class (.h and .cxx files)."));
150   wxButton* createFolderbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_FOLDER, _T("Create Folder (Optional)"));
151   createFolderbt->SetToolTip(wxT("Create a new Folder inside the application folder."));
152   wxButton* openMainbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_CXX, _T("A. Open Main File (Optional)"));
153   openMainbt->SetToolTip(wxT("Open the main file in the application folder with the default code editor."));
154   openMainbt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMApplicationDescriptionPanel::OnMainMouseEnter,NULL,this);
155   openMainbt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMApplicationDescriptionPanel::OnMainMouseExit,NULL,this);
156   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("B. Edit CMakeLists File"));
157   editCMakebt->SetToolTip(wxT("Edit the CMakeLists.txt file inside this application."));
158   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMApplicationDescriptionPanel::OnCMakeMouseEnter,NULL,this);
159   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMApplicationDescriptionPanel::OnCMakeMouseExit,NULL,this);
160   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("C. Open Application Folder"));
161   openFolderbt->SetToolTip(wxT("Open the application folder in the file explorer."));
162
163
164   actionsGridSizer->Add(openMainbt, 1, wxALL | wxEXPAND, 5);
165   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
166   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
167   actionsGridSizer->Add(createClassbt, 1, wxALL | wxEXPAND, 5);
168   actionsGridSizer->Add(createFolderbt, 1, wxALL | wxEXPAND, 5);
169
170   //SetPanel sizer and box
171   actionsGridSizer->AddGrowableCol(0,1);
172   actionsGridSizer->AddGrowableCol(1,1);
173
174   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
175   actionsPanel->SetSizer(actionsPanelSizer);
176   actionsPanelSizer->Fit(actionsPanel);
177   actionsBox->Add(actionsPanel, 1, wxALL | wxEXPAND, 5);
178   sizer->Add(actionsBox, 0, wxEXPAND | wxALL, 10);
179
180   //Assign sizer
181   SetSizer(sizer);
182   sizer->SetSizeHints(this);
183
184   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
185     {
186       wxCDMApplicationHelpDialog* helpDialog = new wxCDMApplicationHelpDialog(this->GetParent(), this->application, wxID_ANY);
187       helpDialog->Show(true);
188     }
189 }
190
191 void wxCDMApplicationDescriptionPanel::OnBtnReturn(wxCommandEvent& event)
192 {
193   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
194   newEvent->SetInt(((wxCDMMainFrame*)this->GetParent())->GetModel()->GetProject()->GetId());
195   newEvent->SetId(0);
196   wxPostEvent(this->GetParent(), *newEvent);
197 }
198
199 void wxCDMApplicationDescriptionPanel::OnBtnSetExeName(wxCommandEvent& event)
200 {
201   //get name
202   wxString versionWx = wxGetTextFromUser(
203       wxT("Enter the new executable name"),
204       wxT("Change Application Executable Name - creaDevManager"),
205       crea::std2wx(this->application->GetExecutableName())
206   );
207   //check name
208   std::vector<std::string> parts;
209   CDMUtilities::splitter::split(parts, crea::wx2std(versionWx), " .", CDMUtilities::splitter::no_empties);
210   if(parts.size() > 0)
211     {
212       std::string* result;
213       if(!this->application->SetExecutableName(crea::wx2std(versionWx), result))
214         wxMessageBox(crea::std2wx(*result),_T("Change Application Executable Name - Error!"),wxOK | wxICON_ERROR);
215     }
216   else
217     {
218       wxMessageBox(crea::std2wx("No name specified"),_T("Set Application Executable Name - Error!"),wxOK | wxICON_ERROR);
219     }
220   this->executableNametc->SetLabel(crea::std2wx(this->application->GetExecutableName()));
221 }
222
223 void wxCDMApplicationDescriptionPanel::OnBtnCreateClass(wxCommandEvent& event)
224 {
225   //get class name from user
226   wxTextEntryDialog* newClassDlg = new wxTextEntryDialog(
227       this,
228       wxT("Please enter the new class name."),
229       wxT("New Class - creaDevManager"),
230       wxT(""),
231       wxOK | wxCANCEL
232   );
233
234   if (newClassDlg->ShowModal() == wxID_OK)
235     {
236       std::string className = crea::wx2std(newClassDlg->GetValue());
237       //check class name
238       if(className.size() > 0)
239         {
240           if(!this->application->CreateClass(className))
241             wxMessageBox(crea::std2wx("Something has gone wrong with the creation of the class."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
242
243           ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
244
245           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
246           newEvent->SetId(0);
247           newEvent->SetInt(this->application->GetId());
248           wxPostEvent(this->GetParent(), *newEvent);
249
250           wxMessageBox(crea::std2wx("The class has been created successfully."),_T("New Class - Success"),wxOK | wxICON_INFORMATION);
251         }
252       else
253         {
254           wxMessageBox(crea::std2wx("The new class name cannot be empty."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
255         }
256     }
257 }
258
259 void wxCDMApplicationDescriptionPanel::OnBtnCreateFolder(wxCommandEvent& event)
260 {
261   //get name
262   wxString folderName = wxGetTextFromUser(
263       wxT("Enter the name of the new folder:"),
264       wxT("Create Folder - creaDevManager")
265   );
266   //check name
267   std::vector<std::string> parts;
268   CDMUtilities::splitter::split(parts, crea::wx2std(folderName), " /\\\"", CDMUtilities::splitter::no_empties);
269   if(parts.size() > 0)
270     {
271       std::string* result;
272       modelCDMFolder* folderC = this->application->CreateFolder(crea::wx2std(folderName), result);
273       if(folderC == NULL)
274         {
275           wxMessageBox(crea::std2wx(*result),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
276           return;
277         }
278       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
279       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
280       newEvent->SetInt(folderC->GetId());
281       wxPostEvent(this->GetParent(), *newEvent);
282       wxMessageBox(crea::std2wx("The folder was successfully created"),_T("Create Folder - Success"),wxOK | wxICON_INFORMATION);
283     }
284   else
285     {
286       wxMessageBox(crea::std2wx("No name specified"),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
287     }
288 }
289
290 void wxCDMApplicationDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
291 {
292   std::string* result;
293   if(!this->application->OpenCMakeListsFile(result))
294     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
295
296   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
297
298   if(this->application->GetCMakeLists() != NULL)
299     {
300       int CMId = this->application->GetCMakeLists()->GetId();
301       newEvent->SetInt(CMId);
302       newEvent->SetId(0);
303       wxPostEvent(this->GetParent(), *newEvent);
304     }
305
306   event.Skip();
307 }
308
309 void wxCDMApplicationDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
310 {
311   std::string* result;
312   if(!this->application->OpenInFileExplorer(result))
313     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
314 }
315
316 void wxCDMApplicationDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
317 {
318   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
319
320   if(this->application->GetCMakeLists() != NULL)
321     {
322       int CMId = this->application->GetCMakeLists()->GetId();
323       newEvent->SetInt(CMId);
324       newEvent->SetId(0);
325       wxPostEvent(this->GetParent(), *newEvent);
326     }
327   event.Skip();
328 }
329
330 void wxCDMApplicationDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
331 {
332   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
333
334   if(this->application->GetCMakeLists() != NULL)
335     {
336       int CMId = this->application->GetCMakeLists()->GetId();
337       newEvent->SetInt(CMId);
338       newEvent->SetId(0);
339       wxPostEvent(this->GetParent(), *newEvent);
340     }
341   event.Skip();
342 }
343
344 void wxCDMApplicationDescriptionPanel::OnBtnOpenMain(wxCommandEvent& event)
345 {
346   if (this->application->GetMainFile() != NULL)
347     {
348       if (CDMUtilities::openTextEditor(this->application->GetMainFile()->GetPath()))
349       {
350         wxMessageBox(crea::std2wx("The main file couldn't be opened."),_T("Open Main File - Error!"),wxOK | wxICON_ERROR);
351       }
352
353       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
354
355       int MId = this->application->GetMainFile()->GetId();
356       newEvent->SetInt(MId);
357       newEvent->SetId(0);
358       wxPostEvent(this->GetParent(), *newEvent);
359
360       event.Skip();
361     }
362   else
363     {
364       wxMessageBox(crea::std2wx("There is no main file or it couldn't be detected."),_T("Open Main File - Error!"),wxOK | wxICON_ERROR);
365     }
366 }
367
368 void wxCDMApplicationDescriptionPanel::OnMainMouseEnter(wxMouseEvent& event)
369 {
370   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
371
372   if(this->application->GetMainFile() != NULL)
373     {
374       int MId = this->application->GetMainFile()->GetId();
375       newEvent->SetInt(MId);
376       newEvent->SetId(0);
377       wxPostEvent(this->GetParent(), *newEvent);
378     }
379   event.Skip();
380 }
381
382 void wxCDMApplicationDescriptionPanel::OnMainMouseExit(wxMouseEvent& event)
383 {
384   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
385
386   if(this->application->GetMainFile() != NULL)
387     {
388       int MId = this->application->GetMainFile()->GetId();
389       newEvent->SetInt(MId);
390       newEvent->SetId(0);
391       wxPostEvent(this->GetParent(), *newEvent);
392     }
393   event.Skip();
394 }