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