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