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