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