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