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