]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMLibDescriptionPanel.cpp
Feature #1711 CreaDevManager application implementation
[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 EVT_CHECKBOX(ID_CHECK_INCLUDE_LIBRARY, wxCDMLibDescriptionPanel::OnChBLibraryChange)
51 END_EVENT_TABLE()
52
53 wxCDMLibDescriptionPanel::wxCDMLibDescriptionPanel(
54     wxWindow* parent,
55     modelCDMLib* lib,
56     wxWindowID id,
57     const wxString& caption,
58     const wxPoint& pos,
59     const wxSize& size,
60     long style
61 )
62 {
63   wxCDMLibDescriptionPanel::Create(parent, lib, id, caption, pos, size, style);
64 }
65
66 wxCDMLibDescriptionPanel::~wxCDMLibDescriptionPanel()
67 {
68 }
69
70 bool wxCDMLibDescriptionPanel::Create(
71     wxWindow* parent,
72     modelCDMLib* lib,
73     wxWindowID id,
74     const wxString& caption,
75     const wxPoint& pos,
76     const wxSize& size,
77     long style
78 )
79 {
80   wxPanel::Create(parent, id, pos, size, style);
81   this->lib = lib;
82   CreateControls();
83   // this part makes the scrollbars show up
84   this->FitInside(); // ask the sizer about the needed size
85   this->SetScrollRate(5, 5);
86   return TRUE;
87 }
88
89 void wxCDMLibDescriptionPanel::CreateControls()
90 {
91   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
92
93   //Links to return
94   wxBoxSizer *linksSizer = new wxBoxSizer(wxHORIZONTAL);
95   std::vector<modelCDMIProjectTreeNode*> parents = this->lib->GetParents();
96   for (int i = 0; i < (int)(parents.size()); i++)
97     {
98       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);
99       returnLnk->SetWindowStyle(wxNO_BORDER);
100           returnLnk->SetToolTip(crea::std2wx("Return to " + parents[parents.size()-1-i]->GetName() + "."));
101       linksSizer->Add(returnLnk, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 5);
102       linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
103     }
104
105   linksSizer->Add(new wxStaticText(this, wxID_ANY, crea::std2wx(this->lib->GetName())), 0, wxALIGN_CENTER, 0);
106
107   sizer->Add(linksSizer, 0, wxALIGN_CENTER | wxALL, 5);
108
109   //Header
110   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
111   {
112     //Image
113     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(LbIcon64)),0, wxALIGN_CENTER, 0);
114     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
115     //Title
116     textSizer->Add(new wxStaticText(this, -1, _("Library Management")),0, wxALIGN_LEFT, 0);
117     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
118   }
119   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
120
121   //Libraries
122   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("A&vailable Libraries"));
123   propertiesBox->GetStaticBox()->SetToolTip(wxT("Select any of the available libraries to see its details or modify them."));
124   wxPanel* propertiesPanel = new wxPanel(this);
125
126   std::vector<modelCDMLibrary*> libraries = this->lib->GetLibraries();
127   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(libraries.size()+1, 3, 9, 5);
128
129   wxStaticText* ChBTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Include in\nCMake"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
130   wxStaticText* LkTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Library Name"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
131   wxStaticText* HlpTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Help"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
132
133   propertiesGridSizer -> Add(ChBTitle, 0, wxEXPAND | wxALL, 5);
134   propertiesGridSizer -> Add(LkTitle,  0, wxEXPAND | wxALL, 5);
135   propertiesGridSizer -> Add(HlpTitle, 0, wxEXPAND | wxALL, 5);
136
137   for (int i = 0; i < (int)(libraries.size()); i++)
138     {
139       //checkbox for cmake inclusion
140       wxCheckBox* pLibraryChB = new wxCheckBox(propertiesPanel, ID_CHECK_INCLUDE_LIBRARY, wxT(""), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
141       pLibraryChB->SetName(crea::std2wx(libraries[i]->GetName()));
142       std::string tt = "if this box is checked the the " + libraries[i]->GetName() + " library is included in the project compilation.";
143       pLibraryChB->SetToolTip(crea::std2wx(tt));
144       pLibraryChB->SetValue(this->lib->IsLibraryIncluded(libraries[i]->GetName()));
145       propertiesGridSizer -> Add(pLibraryChB, 0, wxEXPAND | wxALIGN_CENTER);
146
147       //link to library with description
148       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);
149       pLibrarylk->SetWindowStyle(wxALIGN_LEFT | wxNO_BORDER);
150       tt = "Name: " + libraries[i]->GetName() + "\n";
151       tt += "Location: " + libraries[i]->GetPath();
152       pLibrarylk->SetToolTip(crea::std2wx(tt));
153       pLibrarylk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibDescriptionPanel::OnMouseEnter,NULL,this);
154       pLibrarylk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibDescriptionPanel::OnMouseExit,NULL,this);
155       propertiesGridSizer -> Add(pLibrarylk, 0, wxEXPAND);
156
157       //help icon
158       wxButton* pLibraryHlp = new wxButton(propertiesPanel, wxID_ANY, wxT("?"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
159       pLibraryHlp->Enable(false);
160       tt = "When this library is included in the CMakeLists file, the\nfollowing line is included in the CMakeList.txt file in the lib\nfolder:\n"
161           "ADD_SUBDIRECTORY(" + libraries[i]->GetName() + ")";
162       pLibraryHlp->SetToolTip(crea::std2wx(tt));
163
164       propertiesGridSizer -> Add(pLibraryHlp, 0, wxEXPAND | wxALIGN_CENTER);
165     }
166
167   propertiesGridSizer->AddGrowableCol(1,1);
168
169   propertiesPanel->SetSizer(propertiesGridSizer);
170   propertiesGridSizer->Fit(propertiesPanel);
171   propertiesBox->Add(propertiesPanel, 1, wxALL | wxEXPAND, 5);
172   sizer -> Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
173
174   //Actions
175   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Actions"));
176   actionsBox->GetStaticBox()->SetToolTip(wxT("Create a new library or make any change to the lib's CMakeLists.txt file by selecting the desired action."));
177   wxPanel* actionsPanel = new wxPanel(this);
178   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
179
180   //actionsGrid Sizer
181   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
182
183   wxButton* createLibrarybt = new wxButton(actionsPanel, ID_BUTTON_CREATE_LIBRARY, _T("A. Create Library"));
184   createLibrarybt->SetToolTip(wxT("Create a new library for this project."));
185   actionsGridSizer->Add(createLibrarybt, 1, wxALL | wxEXPAND, 5);
186   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("Edit CMakeLists File"));
187   editCMakebt->SetToolTip(wxT("Open the system default text editor to edit the Lib's CMakeLists.txt file."));
188   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibDescriptionPanel::OnCMakeMouseEnter,NULL,this);
189   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMLibDescriptionPanel::OnCMakeMouseExit,NULL,this);
190   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
191   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("Open Libraries Folder"));
192   openFolderbt->SetToolTip(wxT("Open the lib folder in the file explorer."));
193   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
194
195   actionsGridSizer->AddGrowableCol(0,1);
196   actionsGridSizer->AddGrowableCol(1,1);
197
198   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
199
200   actionsPanel->SetSizer(actionsPanelSizer);
201   actionsPanelSizer->Fit(actionsPanel);
202   actionsBox->Add(actionsPanel, 0, wxALL, 5);
203   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
204
205   //Assign sizer
206   SetSizer(sizer);
207   sizer->SetSizeHints(this);
208
209   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
210     {
211       wxCDMLibHelpDialog* helpDialog = new wxCDMLibHelpDialog(this->GetParent(), this->lib, wxID_ANY);
212       helpDialog->Show(true);
213     }
214 }
215
216 void wxCDMLibDescriptionPanel::OnBtnCreateLibrary(wxCommandEvent& event)
217 {
218   //get name
219   wxString libraryName = wxGetTextFromUser(
220       _T("Enter the new library name"),
221       _T("New Library - creaDevManager"),
222       _T("")
223   );
224   //check name
225   if(libraryName.Len() > 0)
226     {
227       std::string* result;
228       //create library
229       modelCDMIProjectTreeNode* library = this->lib->CreateLibrary(crea::wx2std(libraryName),result);
230       //check library created
231       if(library == NULL)
232         {
233           wxMessageBox(crea::std2wx(*result),_T("New Library - Error!"),wxOK | wxICON_ERROR);
234           return;
235         }
236       wxMessageBox(crea::std2wx("Library successfully created."),_T("New Library - Success!"),wxOK | wxICON_INFORMATION);
237
238       //refreshing tree and description
239       //send event instead of calling parent to avoid crashing
240
241       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
242
243       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
244       newEvent->SetClientData(library);
245       wxPostEvent(this->GetParent(), *newEvent);
246     }
247 }
248
249 void wxCDMLibDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
250 {
251   std::string* result;
252   if(!this->lib->OpenCMakeListsFile(result))
253     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
254
255   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
256
257   if(this->lib->GetCMakeLists() != NULL)
258     {
259       newEvent->SetClientData(this->lib->GetCMakeLists());
260       newEvent->SetId(0);
261       wxPostEvent(this->GetParent(), *newEvent);
262     }
263 }
264
265 void wxCDMLibDescriptionPanel::OnChBLibraryChange(wxCommandEvent& event)
266 {
267   this->lib->SetLibraryInclude(
268       crea::wx2std(((wxCheckBox*)event.GetEventObject())->GetName()),
269       ((wxCheckBox*)event.GetEventObject())->GetValue()
270     );
271 }
272
273 void wxCDMLibDescriptionPanel::OnLnkLibrarySelect(wxHyperlinkEvent& event)
274 {
275   modelCDMLibrary* theLibrary = NULL;
276   std::vector<modelCDMLibrary*> libraries = this->lib->GetLibraries();
277   for (int i = 0; i < (int)(libraries.size()); i++)
278     {
279       if(libraries[i]->GetName() == crea::wx2std(event.GetURL()))
280         {
281           theLibrary = libraries[i];
282           break;
283         }
284     }
285
286   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
287   newEvent->SetClientData(theLibrary);
288   newEvent->SetId(0);
289   wxPostEvent(this->GetParent(), *newEvent);
290
291   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
292   newEvent1->SetClientData(theLibrary);
293   newEvent1->SetId(0);
294   wxPostEvent(this->GetParent(), *newEvent1);
295
296 }
297
298 void wxCDMLibDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
299 {
300   std::vector<modelCDMIProjectTreeNode*> parents = this->lib->GetParents();
301   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
302   //std::cout << parentURL << std::endl;
303   for (int i = 0; i < (int)(parents.size()); i++)
304     {
305       if (parents[i]->GetPath() == parentURL)
306         {
307           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
308           newEvent->SetClientData(parents[i]);
309           newEvent->SetId(0);
310           wxPostEvent(this->GetParent(), *newEvent);
311         }
312     }
313 }
314
315 void wxCDMLibDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
316 {
317   std::string* result;
318   if(!this->lib->OpenInFileExplorer(result))
319     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
320 }
321
322 void wxCDMLibDescriptionPanel::OnMouseEnter(wxMouseEvent& event)
323 {
324   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
325   std::string LibName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
326   modelCDMLibrary* theLibrary = NULL;
327   std::vector<modelCDMLibrary*> libraries = this->lib->GetLibraries();
328   for (int i = 0; i < (int)(libraries.size()); i++)
329     {
330       if(libraries[i]->GetName() == LibName)
331         {
332           theLibrary = libraries[i];
333           break;
334         }
335     }
336   newEvent->SetClientData(theLibrary);
337   newEvent->SetId(0);
338   wxPostEvent(this->GetParent(), *newEvent);
339   event.Skip();
340 }
341
342 void wxCDMLibDescriptionPanel::OnMouseExit(wxMouseEvent& event)
343 {
344   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
345   std::string LibName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
346   modelCDMLibrary* theLibrary = NULL;
347   std::vector<modelCDMLibrary*> libraries = this->lib->GetLibraries();
348   for (int i = 0; i < (int)(libraries.size()); i++)
349     {
350       if(libraries[i]->GetName() == LibName)
351         {
352           theLibrary = libraries[i];
353           break;
354         }
355     }
356   newEvent->SetClientData(theLibrary);
357   newEvent->SetId(0);
358   wxPostEvent(this->GetParent(), *newEvent);
359   event.Skip();
360 }
361
362 void wxCDMLibDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
363 {
364   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
365
366   if(this->lib->GetCMakeLists() != NULL)
367     {
368       newEvent->SetClientData(this->lib->GetCMakeLists());
369       newEvent->SetId(0);
370       wxPostEvent(this->GetParent(), *newEvent);
371     }
372   event.Skip();
373 }
374
375 void wxCDMLibDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
376 {
377   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
378
379   if(this->lib->GetCMakeLists() != NULL)
380     {
381       newEvent->SetClientData(this->lib->GetCMakeLists());
382       newEvent->SetId(0);
383       wxPostEvent(this->GetParent(), *newEvent);
384     }
385   event.Skip();
386 }