]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMLibraryDescriptionPanel.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / wxCDMLibraryDescriptionPanel.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  * wxCDMLibraryDescriptionPanel.cpp
30  *
31  *  Created on: Nov 27, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "wxCDMLibraryDescriptionPanel.h"
36
37 #include "CDMUtilities.h"
38 #include "wxCDMMainFrame.h"
39
40 #include "wxCDMLibraryHelpDialog.h"
41
42 #include "creaDevManagerIds.h"
43 #include "images/LIcon64.xpm"
44
45 BEGIN_EVENT_TABLE(wxCDMLibraryDescriptionPanel, wxPanel)
46 EVT_BUTTON(ID_BUTTON_PREV, wxCDMLibraryDescriptionPanel::OnBtnReturn)
47 EVT_BUTTON(ID_BUTTON_SET_NAME, wxCDMLibraryDescriptionPanel::OnBtnSetExeName)
48 EVT_BUTTON(ID_BUTTON_CREATE_CLASS, wxCDMLibraryDescriptionPanel::OnBtnCreateClass)
49 EVT_BUTTON(ID_BUTTON_CREATE_FOLDER, wxCDMLibraryDescriptionPanel::OnBtnCreateFolder)
50 EVT_BUTTON(ID_BUTTON_EDIT_CMAKELISTSFILE, wxCDMLibraryDescriptionPanel::OnBtnEditCMakeLists)
51 EVT_BUTTON(ID_BUTTON_OPEN_FOLDER, wxCDMLibraryDescriptionPanel::OnBtnOpenFolder)
52 END_EVENT_TABLE()
53
54 wxCDMLibraryDescriptionPanel::wxCDMLibraryDescriptionPanel(
55     wxWindow* parent,
56     modelCDMLibrary* library,
57     wxWindowID id,
58     const wxString& caption,
59     const wxPoint& pos,
60     const wxSize& size,
61     long style
62 )
63 {
64   wxCDMLibraryDescriptionPanel::Create(parent, library, id, caption, pos, size, style);
65 }
66
67 wxCDMLibraryDescriptionPanel::~wxCDMLibraryDescriptionPanel()
68 {
69 }
70
71 bool wxCDMLibraryDescriptionPanel::Create(
72     wxWindow* parent,
73     modelCDMLibrary* library,
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->library = library;
83   CreateControls();
84   return TRUE;
85 }
86
87 void wxCDMLibraryDescriptionPanel::CreateControls()
88 {
89   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
90
91   //Link to return
92   wxButton* returnbt = new wxButton(this, ID_BUTTON_PREV, wxT("Return to project"));
93   returnbt->SetToolTip(wxT("Return to the active project description."));
94   sizer->Add(returnbt, 0, wxALIGN_CENTER | wxALL, 5);
95   //Header
96   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
97   {
98     //Image
99     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(LIcon64)),0, wxALIGN_CENTER, 0);
100     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
101     //Title
102     textSizer->Add(new wxStaticText(this, -1, _("Library")),0, wxALIGN_LEFT, 0);
103     //Library Name
104     textSizer->Add(new wxStaticText(this, -1, crea::std2wx(this->library->GetName())),0, wxALIGN_LEFT, 0);
105     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
106   }
107   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
108
109   /*//Properties
110   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Properties"));
111   wxPanel* propertiesPanel = new wxPanel(this);
112   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
113
114   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(4, 2, 9, 15);
115
116   wxStaticText *pMainFile = new wxStaticText(propertiesPanel, -1, wxT("Library Name"));
117   wxBoxSizer* pMainFilesz = new wxBoxSizer(wxHORIZONTAL);
118   this->libraryNametc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(this->library->GetNameLibrary()));
119   wxButton* pMainFilebt = new wxButton(propertiesPanel, ID_BUTTON_SET_NAME, wxT("Set"));
120   pMainFilebt->SetToolTip(wxT("Set the name of the library for the project."));
121   pMainFilesz->Add(this->libraryNametc, 0, wxALIGN_CENTER_VERTICAL, 0);
122   pMainFilesz->Add(pMainFilebt, 0, wxALIGN_CENTER | wxLEFT, 10);
123
124   propertiesGridSizer->Add(pMainFile, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
125   propertiesGridSizer->Add(pMainFilesz, 1, wxEXPAND);
126
127   propertiesGridSizer->AddGrowableCol(1,1);
128
129   propertiesPanelSizer->Add(propertiesGridSizer, 0, wxEXPAND);
130   propertiesPanel->SetSizer(propertiesPanelSizer);
131   propertiesPanelSizer->Fit(propertiesPanel);
132   propertiesBox->Add(propertiesPanel, 0, wxEXPAND);
133   sizer->Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
134    */
135   //Actions
136   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
137   wxPanel* actionsPanel = new wxPanel(this);
138   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
139
140   //actionsGrid Sizer
141   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
142
143   wxButton* createClassbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_CLASS, _T("A. Create Class"));
144   createClassbt->SetToolTip(wxT("Create a new class for this library."));
145   actionsGridSizer->Add(createClassbt, 1, wxALL | wxEXPAND, 5);
146   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("B. Edit CMakeLists File"));
147   editCMakebt->SetToolTip(wxT("Edit the CMakeLists.txt of this library in the default text editor."));
148   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibraryDescriptionPanel::OnCMakeMouseEnter,NULL,this);
149   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibraryDescriptionPanel::OnCMakeMouseExit,NULL,this);
150   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
151   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("C. Open Library Folder"));
152   openFolderbt->SetToolTip(wxT("Open the library folder in the file explorer."));
153   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
154   wxButton* createFolderbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_FOLDER, _T("Create Folder (Optional)"));
155   createFolderbt->SetToolTip(wxT("Create a new folder for this library."));
156   actionsGridSizer->Add(createFolderbt, 1, wxALL | wxEXPAND, 5);
157
158   actionsGridSizer->AddGrowableCol(0,1);
159   actionsGridSizer->AddGrowableCol(1,1);
160
161   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
162   actionsPanel->SetSizer(actionsPanelSizer);
163   actionsPanelSizer->Fit(actionsPanel);
164   actionsBox->Add(actionsPanel, 1, wxEXPAND);
165   sizer -> Add(actionsBox, 0, wxALL | wxEXPAND, 10);
166
167   //Assign sizer
168   SetSizer(sizer);
169   sizer->SetSizeHints(this);
170
171   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
172     {
173       wxCDMLibraryHelpDialog* helpDialog = new wxCDMLibraryHelpDialog(this->GetParent(), this->library, wxID_ANY);
174       helpDialog->Show(true);
175     }
176 }
177
178 void wxCDMLibraryDescriptionPanel::OnBtnReturn(wxCommandEvent& event)
179 {
180   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
181   newEvent->SetInt(((wxCDMMainFrame*)this->GetParent())->GetModel()->GetProject()->GetId());
182   newEvent->SetId(0);
183   wxPostEvent(this->GetParent(), *newEvent);
184 }
185
186 void wxCDMLibraryDescriptionPanel::OnBtnSetExeName(wxCommandEvent& event)
187 {
188   //get name
189   wxString versionWx = wxGetTextFromUser(
190       wxT("Enter the new executable name"),
191       wxT("Change Library Name - creaDevManager"),
192       crea::std2wx(this->library->GetNameLibrary())
193   );
194   //check name
195   std::vector<std::string> parts;
196   CDMUtilities::splitter::split(parts, crea::wx2std(versionWx), " .", CDMUtilities::splitter::no_empties);
197   if(parts.size() > 0)
198     {
199       std::string* result;
200       if(!this->library->SetNameLibrary(crea::wx2std(versionWx), result))
201         wxMessageBox(crea::std2wx(*result),_T("Change Library Name - Error!"),wxOK | wxICON_ERROR);
202     }
203   else
204     {
205       wxMessageBox(crea::std2wx("No name specified"),_T("Set Library Name - Error!"),wxOK | wxICON_ERROR);
206     }
207   this->libraryNametc->SetLabel(crea::std2wx(this->library->GetNameLibrary()));
208 }
209
210 void wxCDMLibraryDescriptionPanel::OnBtnCreateClass(wxCommandEvent& event)
211 {
212   //get class name from user
213   wxTextEntryDialog* newClassDlg = new wxTextEntryDialog(
214       this,
215       wxT("Please enter the new class name."),
216       wxT("New Class - creaDevManager"),
217       wxT(""),
218       wxOK | wxCANCEL
219   );
220
221   if (newClassDlg->ShowModal() == wxID_OK)
222     {
223       std::string className = crea::wx2std(newClassDlg->GetValue());
224       //check class name
225       if(className.size() > 0)
226         {
227           if(!this->library->CreateClass(className))
228             wxMessageBox(crea::std2wx("Something has gone wrong with the creation of the class."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
229
230           ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
231
232           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
233           newEvent->SetId(0);
234           newEvent->SetInt(library->GetId());
235           wxPostEvent(this->GetParent(), *newEvent);
236
237           wxMessageBox(crea::std2wx("The class has been created successfully."),_T("New Class - Success"),wxOK | wxICON_INFORMATION);
238         }
239       else
240         {
241           wxMessageBox(crea::std2wx("The new class name cannot be empty."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
242         }
243     }
244 }
245
246 void wxCDMLibraryDescriptionPanel::OnBtnCreateFolder(wxCommandEvent& event)
247 {
248   //get name
249   wxString folderName = wxGetTextFromUser(
250       wxT("Enter the name of the new folder:"),
251       wxT("Create Folder - creaDevManager")
252   );
253   //check name
254   std::vector<std::string> parts;
255   CDMUtilities::splitter::split(parts, crea::wx2std(folderName), " /\\\"", CDMUtilities::splitter::no_empties);
256   if(parts.size() > 0)
257     {
258       std::string* result;
259       modelCDMFolder* folderC = this->library->CreateFolder(crea::wx2std(folderName), result);
260       if(folderC == NULL)
261         {
262           wxMessageBox(crea::std2wx(*result),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
263           return;
264         }
265       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
266       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
267       newEvent->SetInt(folderC->GetId());
268       wxPostEvent(this->GetParent(), *newEvent);
269       wxMessageBox(crea::std2wx("The folder was successfully created"),_T("Create Folder - Success"),wxOK | wxICON_INFORMATION);
270     }
271   else
272     {
273       wxMessageBox(crea::std2wx("No name specified"),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
274     }
275 }
276
277 void wxCDMLibraryDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
278 {
279   std::string* result;
280   if(!this->library->OpenCMakeListsFile(result))
281     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
282
283   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
284
285   if(this->library->GetCMakeLists() != NULL)
286     {
287       int CMId = this->library->GetCMakeLists()->GetId();
288       newEvent->SetInt(CMId);
289       newEvent->SetId(0);
290       wxPostEvent(this->GetParent(), *newEvent);
291     }
292
293   event.Skip();
294 }
295
296 void wxCDMLibraryDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
297 {
298   std::string* result;
299   if(!this->library->OpenInFileExplorer(result))
300     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
301 }
302
303 void wxCDMLibraryDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
304 {
305   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
306
307   if(this->library->GetCMakeLists() != NULL)
308     {
309       int CMId = this->library->GetCMakeLists()->GetId();
310       newEvent->SetInt(CMId);
311       newEvent->SetId(0);
312       wxPostEvent(this->GetParent(), *newEvent);
313     }
314   event.Skip();
315 }
316
317 void wxCDMLibraryDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
318 {
319   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
320
321   if(this->library->GetCMakeLists() != NULL)
322     {
323       int CMId = this->library->GetCMakeLists()->GetId();
324       newEvent->SetInt(CMId);
325       newEvent->SetId(0);
326       wxPostEvent(this->GetParent(), *newEvent);
327     }
328   event.Skip();
329 }