]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMLibDescriptionPanel.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / wxCDMLibDescriptionPanel.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  * wxCDMLibDescriptionPanel.cpp
30  *
31  *  Created on: Nov 27, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "wxCDMLibDescriptionPanel.h"
36
37 #include "wxCDMMainFrame.h"
38
39 #include "wxCDMLibHelpDialog.h"
40
41 #include "creaDevManagerIds.h"
42 #include "images/LbIcon64.xpm"
43
44 BEGIN_EVENT_TABLE(wxCDMLibDescriptionPanel, wxPanel)
45 EVT_HYPERLINK(ID_BUTTON_PREV, wxCDMLibDescriptionPanel::OnBtnReturn)
46 EVT_HYPERLINK(ID_LINK_SELECT_LIBRARY, wxCDMLibDescriptionPanel::OnLnkLibrarySelect)
47 EVT_BUTTON(ID_BUTTON_CREATE_LIBRARY, wxCDMLibDescriptionPanel::OnBtnCreateLibrary)
48 EVT_BUTTON(ID_BUTTON_EDIT_CMAKELISTSFILE, wxCDMLibDescriptionPanel::OnBtnEditCMakeLists)
49 EVT_BUTTON(ID_BUTTON_OPEN_FOLDER, wxCDMLibDescriptionPanel::OnBtnOpenFolder)
50 END_EVENT_TABLE()
51
52 wxCDMLibDescriptionPanel::wxCDMLibDescriptionPanel(
53     wxWindow* parent,
54     modelCDMLib* lib,
55     wxWindowID id,
56     const wxString& caption,
57     const wxPoint& pos,
58     const wxSize& size,
59     long style
60 )
61 {
62   wxCDMLibDescriptionPanel::Create(parent, lib, id, caption, pos, size, style);
63 }
64
65 wxCDMLibDescriptionPanel::~wxCDMLibDescriptionPanel()
66 {
67 }
68
69 bool wxCDMLibDescriptionPanel::Create(
70     wxWindow* parent,
71     modelCDMLib* lib,
72     wxWindowID id,
73     const wxString& caption,
74     const wxPoint& pos,
75     const wxSize& size,
76     long style
77 )
78 {
79   wxPanel::Create(parent, id, pos, size, style);
80   this->lib = lib;
81   CreateControls();
82   return TRUE;
83 }
84
85 void wxCDMLibDescriptionPanel::CreateControls()
86 {
87   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
88
89   //Links to return
90   wxBoxSizer *linksSizer = new wxBoxSizer(wxHORIZONTAL);
91   std::vector<modelCDMIProjectTreeNode*> parents = this->lib->GetParents();
92   for (int i = 0; i < (int)(parents.size()); i++)
93     {
94       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);
95       returnLnk->SetWindowStyle(wxNO_BORDER);
96           returnLnk->SetToolTip(crea::std2wx("Return to " + parents[parents.size()-1-i]->GetName() + "."));
97       linksSizer->Add(returnLnk, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 5);
98       if (i < (int)(parents.size())-1)
99         {
100           linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
101         }
102     }
103   sizer->Add(linksSizer, 0, wxALIGN_CENTER | wxALL, 5);
104
105   //Header
106   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
107   {
108     //Image
109     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(LbIcon64)),0, wxALIGN_CENTER, 0);
110     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
111     //Title
112     textSizer->Add(new wxStaticText(this, -1, _("Library Management")),0, wxALIGN_LEFT, 0);
113     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
114   }
115   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
116
117   //Libraries
118   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("A&vailable Libraries"));
119   propertiesBox->GetStaticBox()->SetToolTip(wxT("Select any of the available libraries to see its details or modify them."));
120   wxPanel* propertiesPanel = new wxPanel(this);
121   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
122
123   std::vector<modelCDMLibrary*> libraries = this->lib->GetLibraries();
124   for (int i = 0; i < (int)(libraries.size()); i++)
125     {
126                 wxHyperlinkCtrl* pLibrarylk = new wxHyperlinkCtrl(propertiesPanel, ID_LINK_SELECT_LIBRARY, crea::std2wx(libraries[i]->GetName().c_str()), crea::std2wx(libraries[i]->GetName().c_str()), wxDefaultPosition, wxDefaultSize, wxHL_DEFAULT_STYLE);
127       pLibrarylk->SetWindowStyle(wxALIGN_LEFT | wxNO_BORDER);
128       std::string tt = "Name: " + libraries[i]->GetName() + "\n";
129       tt += "Location: " + libraries[i]->GetPath();
130       pLibrarylk->SetToolTip(crea::std2wx(tt.c_str()));
131       pLibrarylk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibDescriptionPanel::OnMouseEnter,NULL,this);
132       pLibrarylk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibDescriptionPanel::OnMouseExit,NULL,this);
133       propertiesPanelSizer -> Add(pLibrarylk, 0, wxEXPAND | wxALL, 5);
134     }
135
136   propertiesPanel->SetSizer(propertiesPanelSizer);
137   propertiesPanelSizer->Fit(propertiesPanel);
138   propertiesBox->Add(propertiesPanel, 1, wxALL | wxEXPAND, 5);
139   sizer -> Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
140
141   //Actions
142   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Actions"));
143   actionsBox->GetStaticBox()->SetToolTip(wxT("Create a new library or make any change to the lib's CMakeLists.txt file by selecting the desired action."));
144   wxPanel* actionsPanel = new wxPanel(this);
145   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
146
147   //actionsGrid Sizer
148   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
149
150   wxButton* createLibrarybt = new wxButton(actionsPanel, ID_BUTTON_CREATE_LIBRARY, _T("A. Create Library"));
151   createLibrarybt->SetToolTip(wxT("Create a new library for this project."));
152   actionsGridSizer->Add(createLibrarybt, 1, wxALL | wxEXPAND, 5);
153   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("B. Edit CMakeLists File"));
154   editCMakebt->SetToolTip(wxT("Open the system default text editor to edit the Lib's CMakeLists.txt file."));
155   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibDescriptionPanel::OnCMakeMouseEnter,NULL,this);
156   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibDescriptionPanel::OnCMakeMouseExit,NULL,this);
157   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
158   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("C. Open Libraries Folder"));
159   openFolderbt->SetToolTip(wxT("Open the lib folder in the file explorer."));
160   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
161
162   actionsGridSizer->AddGrowableCol(0,1);
163   actionsGridSizer->AddGrowableCol(1,1);
164
165   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
166
167   actionsPanel->SetSizer(actionsPanelSizer);
168   actionsPanelSizer->Fit(actionsPanel);
169   actionsBox->Add(actionsPanel, 0, wxALL, 5);
170   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
171
172   //Assign sizer
173   SetSizer(sizer);
174   sizer->SetSizeHints(this);
175
176   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
177     {
178       wxCDMLibHelpDialog* helpDialog = new wxCDMLibHelpDialog(this->GetParent(), this->lib, wxID_ANY);
179       helpDialog->Show(true);
180     }
181 }
182
183 void wxCDMLibDescriptionPanel::OnBtnCreateLibrary(wxCommandEvent& event)
184 {
185   //get name
186   wxString libraryName = wxGetTextFromUser(
187       _T("Enter the new library name"),
188       _T("New Library - creaDevManager"),
189       _T("")
190   );
191   //check name
192   if(libraryName.Len() > 0)
193     {
194       std::string* result;
195       //create library
196       modelCDMIProjectTreeNode* library = this->lib->CreateLibrary(crea::wx2std(libraryName),result);
197       //check library created
198       if(library == NULL)
199         {
200           wxMessageBox(crea::std2wx(*result),_T("New Library - Error!"),wxOK | wxICON_ERROR);
201           return;
202         }
203       wxMessageBox(crea::std2wx("Library successfully created."),_T("New Library - Success!"),wxOK | wxICON_INFORMATION);
204
205       //refreshing tree and description
206       //send event instead of calling parent to avoid crashing
207
208       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
209
210       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
211       newEvent->SetClientData(library);
212       wxPostEvent(this->GetParent(), *newEvent);
213     }
214 }
215
216 void wxCDMLibDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
217 {
218   std::string* result;
219   if(!this->lib->OpenCMakeListsFile(result))
220     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
221
222   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
223
224   if(this->lib->GetCMakeLists() != NULL)
225     {
226       newEvent->SetClientData(this->lib->GetCMakeLists());
227       newEvent->SetId(0);
228       wxPostEvent(this->GetParent(), *newEvent);
229     }
230 }
231
232 void wxCDMLibDescriptionPanel::OnLnkLibrarySelect(wxHyperlinkEvent& event)
233 {
234   modelCDMLibrary* theLibrary = NULL;
235   std::vector<modelCDMLibrary*> libraries = this->lib->GetLibraries();
236   for (int i = 0; i < (int)(libraries.size()); i++)
237     {
238       if(libraries[i]->GetName() == crea::wx2std(event.GetURL()))
239         {
240           theLibrary = libraries[i];
241           break;
242         }
243     }
244
245   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
246   newEvent->SetClientData(theLibrary);
247   newEvent->SetId(0);
248   wxPostEvent(this->GetParent(), *newEvent);
249
250   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
251   newEvent1->SetClientData(theLibrary);
252   newEvent1->SetId(0);
253   wxPostEvent(this->GetParent(), *newEvent1);
254
255 }
256
257 void wxCDMLibDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
258 {
259   std::vector<modelCDMIProjectTreeNode*> parents = this->lib->GetParents();
260   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
261   //std::cout << parentURL << std::endl;
262   for (int i = 0; i < (int)(parents.size()); i++)
263     {
264       if (parents[i]->GetPath() == parentURL)
265         {
266           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
267           newEvent->SetClientData(parents[i]);
268           newEvent->SetId(0);
269           wxPostEvent(this->GetParent(), *newEvent);
270         }
271     }
272 }
273
274 void wxCDMLibDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
275 {
276   std::string* result;
277   if(!this->lib->OpenInFileExplorer(result))
278     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
279 }
280
281 void wxCDMLibDescriptionPanel::OnMouseEnter(wxMouseEvent& event)
282 {
283   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
284   std::string LibName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
285   modelCDMLibrary* theLibrary = NULL;
286   std::vector<modelCDMLibrary*> libraries = this->lib->GetLibraries();
287   for (int i = 0; i < (int)(libraries.size()); i++)
288     {
289       if(libraries[i]->GetName() == LibName)
290         {
291           theLibrary = libraries[i];
292           break;
293         }
294     }
295   newEvent->SetClientData(theLibrary);
296   newEvent->SetId(0);
297   wxPostEvent(this->GetParent(), *newEvent);
298   event.Skip();
299 }
300
301 void wxCDMLibDescriptionPanel::OnMouseExit(wxMouseEvent& event)
302 {
303   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
304   std::string LibName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
305   modelCDMLibrary* theLibrary = NULL;
306   std::vector<modelCDMLibrary*> libraries = this->lib->GetLibraries();
307   for (int i = 0; i < (int)(libraries.size()); i++)
308     {
309       if(libraries[i]->GetName() == LibName)
310         {
311           theLibrary = libraries[i];
312           break;
313         }
314     }
315   newEvent->SetClientData(theLibrary);
316   newEvent->SetId(0);
317   wxPostEvent(this->GetParent(), *newEvent);
318   event.Skip();
319 }
320
321 void wxCDMLibDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
322 {
323   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
324
325   if(this->lib->GetCMakeLists() != NULL)
326     {
327       newEvent->SetClientData(this->lib->GetCMakeLists());
328       newEvent->SetId(0);
329       wxPostEvent(this->GetParent(), *newEvent);
330     }
331   event.Skip();
332 }
333
334 void wxCDMLibDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
335 {
336   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
337
338   if(this->lib->GetCMakeLists() != NULL)
339     {
340       newEvent->SetClientData(this->lib->GetCMakeLists());
341       newEvent->SetId(0);
342       wxPostEvent(this->GetParent(), *newEvent);
343     }
344   event.Skip();
345 }