]> 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_HYPERLINK(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   //Links to return
92   wxBoxSizer *linksSizer = new wxBoxSizer(wxHORIZONTAL);
93   std::vector<modelCDMIProjectTreeNode*> parents = this->library->GetParents();
94   for (int i = 0; i < (int)(parents.size()); i++)
95     {
96       wxHyperlinkCtrl* returnLnk = new wxHyperlinkCtrl(this, ID_BUTTON_PREV, crea::std2wx(parents[parents.size()-1-i]->GetName()), crea::std2wx(parents[parents.size()-1-i]->GetPath()));
97       returnLnk->SetToolTip(crea::std2wx("Return to " + parents[parents.size()-1-i]->GetName() + "."));
98       linksSizer->Add(returnLnk, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 5);
99       if (i < (int)(parents.size())-1)
100         {
101           linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
102         }
103     }
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(LIcon64)),0, wxALIGN_CENTER, 0);
111     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
112     //Title
113     textSizer->Add(new wxStaticText(this, -1, _("Library")),0, wxALIGN_LEFT, 0);
114     //Library Name
115     textSizer->Add(new wxStaticText(this, -1, crea::std2wx(this->library->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("Library Name"));
128   wxBoxSizer* pMainFilesz = new wxBoxSizer(wxHORIZONTAL);
129   this->libraryNametc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(this->library->GetNameLibrary()));
130   wxButton* pMainFilebt = new wxButton(propertiesPanel, ID_BUTTON_SET_NAME, wxT("Set"));
131   pMainFilebt->SetToolTip(wxT("Set the name of the library for the project."));
132   pMainFilesz->Add(this->libraryNametc, 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   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
148   wxPanel* actionsPanel = new wxPanel(this);
149   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
150
151   //actionsGrid Sizer
152   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
153
154   wxButton* createClassbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_CLASS, _T("A. Create Class"));
155   createClassbt->SetToolTip(wxT("Create a new class for this library."));
156   actionsGridSizer->Add(createClassbt, 1, wxALL | wxEXPAND, 5);
157   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("B. Edit CMakeLists File"));
158   editCMakebt->SetToolTip(wxT("Edit the CMakeLists.txt of this library in the default text editor."));
159   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibraryDescriptionPanel::OnCMakeMouseEnter,NULL,this);
160   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibraryDescriptionPanel::OnCMakeMouseExit,NULL,this);
161   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
162   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("C. Open Library Folder"));
163   openFolderbt->SetToolTip(wxT("Open the library folder in the file explorer."));
164   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
165   wxButton* createFolderbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_FOLDER, _T("Create Folder (Optional)"));
166   createFolderbt->SetToolTip(wxT("Create a new folder for this library."));
167   actionsGridSizer->Add(createFolderbt, 1, wxALL | wxEXPAND, 5);
168
169   actionsGridSizer->AddGrowableCol(0,1);
170   actionsGridSizer->AddGrowableCol(1,1);
171
172   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
173   actionsPanel->SetSizer(actionsPanelSizer);
174   actionsPanelSizer->Fit(actionsPanel);
175   actionsBox->Add(actionsPanel, 1, wxEXPAND);
176   sizer -> Add(actionsBox, 0, wxALL | wxEXPAND, 10);
177
178   //Assign sizer
179   SetSizer(sizer);
180   sizer->SetSizeHints(this);
181
182   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
183     {
184       wxCDMLibraryHelpDialog* helpDialog = new wxCDMLibraryHelpDialog(this->GetParent(), this->library, wxID_ANY);
185       helpDialog->Show(true);
186     }
187 }
188
189 void wxCDMLibraryDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
190 {
191   std::vector<modelCDMIProjectTreeNode*> parents = this->library->GetParents();
192     std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
193     //std::cout << parentURL << std::endl;
194     for (int i = 0; i < (int)(parents.size()); i++)
195       {
196         if (parents[i]->GetPath() == parentURL)
197           {
198             wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
199             newEvent->SetClientData(parents[i]);
200             newEvent->SetId(0);
201             wxPostEvent(this->GetParent(), *newEvent);
202           }
203       }
204 }
205
206 void wxCDMLibraryDescriptionPanel::OnBtnSetExeName(wxCommandEvent& event)
207 {
208   //get name
209   wxString versionWx = wxGetTextFromUser(
210       wxT("Enter the new executable name"),
211       wxT("Change Library Name - creaDevManager"),
212       crea::std2wx(this->library->GetNameLibrary())
213   );
214   //check name
215   std::vector<std::string> parts;
216   CDMUtilities::splitter::split(parts, crea::wx2std(versionWx), " .", CDMUtilities::splitter::no_empties);
217   if(parts.size() > 0)
218     {
219       std::string* result;
220       if(!this->library->SetNameLibrary(crea::wx2std(versionWx), result))
221         wxMessageBox(crea::std2wx(*result),_T("Change Library Name - Error!"),wxOK | wxICON_ERROR);
222     }
223   else
224     {
225       wxMessageBox(crea::std2wx("No name specified"),_T("Set Library Name - Error!"),wxOK | wxICON_ERROR);
226     }
227   this->libraryNametc->SetLabel(crea::std2wx(this->library->GetNameLibrary()));
228 }
229
230 void wxCDMLibraryDescriptionPanel::OnBtnCreateClass(wxCommandEvent& event)
231 {
232   //get class name from user
233   wxTextEntryDialog* newClassDlg = new wxTextEntryDialog(
234       this,
235       wxT("Please enter the new class name."),
236       wxT("New Class - creaDevManager"),
237       wxT(""),
238       wxOK | wxCANCEL
239   );
240
241   if (newClassDlg->ShowModal() == wxID_OK)
242     {
243       std::string className = crea::wx2std(newClassDlg->GetValue());
244       //check class name
245       if(className.size() > 0)
246         {
247           if(!this->library->CreateClass(className))
248             wxMessageBox(crea::std2wx("Something has gone wrong with the creation of the class."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
249
250           ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
251
252           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
253           newEvent->SetId(0);
254           newEvent->SetClientData(this->library);
255           wxPostEvent(this->GetParent(), *newEvent);
256
257           wxMessageBox(crea::std2wx("The class has been created successfully."),_T("New Class - Success"),wxOK | wxICON_INFORMATION);
258         }
259       else
260         {
261           wxMessageBox(crea::std2wx("The new class name cannot be empty."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
262         }
263     }
264 }
265
266 void wxCDMLibraryDescriptionPanel::OnBtnCreateFolder(wxCommandEvent& event)
267 {
268   //get name
269   wxString folderName = wxGetTextFromUser(
270       wxT("Enter the name of the new folder:"),
271       wxT("Create Folder - creaDevManager")
272   );
273   //check name
274   std::vector<std::string> parts;
275   CDMUtilities::splitter::split(parts, crea::wx2std(folderName), " /\\\"", CDMUtilities::splitter::no_empties);
276   if(parts.size() > 0)
277     {
278       std::string* result;
279       modelCDMFolder* folderC = this->library->CreateFolder(crea::wx2std(folderName), result);
280       if(folderC == NULL)
281         {
282           wxMessageBox(crea::std2wx(*result),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
283           return;
284         }
285       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
286       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
287       newEvent->SetClientData(folderC);
288       wxPostEvent(this->GetParent(), *newEvent);
289       wxMessageBox(crea::std2wx("The folder was successfully created"),_T("Create Folder - Success"),wxOK | wxICON_INFORMATION);
290     }
291   else
292     {
293       wxMessageBox(crea::std2wx("No name specified"),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
294     }
295 }
296
297 void wxCDMLibraryDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
298 {
299   std::string* result;
300   if(!this->library->OpenCMakeListsFile(result))
301     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
302
303   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
304
305   if(this->library->GetCMakeLists() != NULL)
306     {
307       newEvent->SetClientData(this->library->GetCMakeLists());
308       newEvent->SetId(0);
309       wxPostEvent(this->GetParent(), *newEvent);
310     }
311
312   event.Skip();
313 }
314
315 void wxCDMLibraryDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
316 {
317   std::string* result;
318   if(!this->library->OpenInFileExplorer(result))
319     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
320 }
321
322 void wxCDMLibraryDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
323 {
324   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
325
326   if(this->library->GetCMakeLists() != NULL)
327     {
328       newEvent->SetClientData(this->library->GetCMakeLists());
329       newEvent->SetId(0);
330       wxPostEvent(this->GetParent(), *newEvent);
331     }
332   event.Skip();
333 }
334
335 void wxCDMLibraryDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
336 {
337   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
338
339   if(this->library->GetCMakeLists() != NULL)
340     {
341       newEvent->SetClientData(this->library->GetCMakeLists());
342       newEvent->SetId(0);
343       wxPostEvent(this->GetParent(), *newEvent);
344     }
345   event.Skip();
346 }