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