]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMPackageDescriptionPanel.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / wxCDMPackageDescriptionPanel.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  * wxCDMPackageDescriptionPanel.cpp
30  *
31  *  Created on: Nov 27, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "wxCDMPackageDescriptionPanel.h"
36
37 #include "wxCDMMainFrame.h"
38
39 #include "creaDevManagerIds.h"
40 #include "images/PkIcon64.xpm"
41
42 #include <wx/textdlg.h>
43 #include "CDMUtilities.h"
44
45 BEGIN_EVENT_TABLE(wxCDMPackageDescriptionPanel, wxPanel)
46 EVT_BUTTON(ID_BUTTON_PREV, wxCDMPackageDescriptionPanel::OnBtnReturn)
47 EVT_BUTTON(ID_BUTTON_SET_AUTHOR, wxCDMPackageDescriptionPanel::OnBtnSetAuthor)
48 EVT_BUTTON(ID_BUTTON_SET_VERSION, wxCDMPackageDescriptionPanel::OnBtnSetVersion)
49 EVT_BUTTON(ID_BUTTON_SET_DESCRIPTION, wxCDMPackageDescriptionPanel::OnBtnSetDescription)
50 EVT_HYPERLINK(ID_LINK_SELECT_BLACKBOX, wxCDMPackageDescriptionPanel::OnLnkBlackBoxSelect)
51 EVT_BUTTON(ID_BUTTON_CREATE_BLACKBOX, wxCDMPackageDescriptionPanel::OnBtnCreateBlackBox)
52 EVT_BUTTON(ID_BUTTON_EDIT_CMAKELISTSFILE, wxCDMPackageDescriptionPanel::OnBtnEditCMakeLists)
53 EVT_BUTTON(ID_BUTTON_OPEN_FOLDER, wxCDMPackageDescriptionPanel::OnBtnOpenFolder)
54 END_EVENT_TABLE()
55
56 wxCDMPackageDescriptionPanel::wxCDMPackageDescriptionPanel(
57     wxWindow* parent,
58     modelCDMPackage* package,
59     wxWindowID id,
60     const wxString& caption,
61     const wxPoint& pos,
62     const wxSize& size,
63     long style
64 )
65 {
66   wxCDMPackageDescriptionPanel::Create(parent, package, id, caption, pos, size, style);
67 }
68
69 wxCDMPackageDescriptionPanel::~wxCDMPackageDescriptionPanel()
70 {
71 }
72
73 bool wxCDMPackageDescriptionPanel::Create(
74     wxWindow* parent,
75     modelCDMPackage* package,
76     wxWindowID id,
77     const wxString& caption,
78     const wxPoint& pos,
79     const wxSize& size,
80     long style
81 )
82 {
83   wxPanel::Create(parent, id, pos, size, style);
84   this->package = package;
85   CreateControls();
86   return TRUE;
87 }
88
89 void wxCDMPackageDescriptionPanel::CreateControls()
90 {
91   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
92
93   //Link to return
94   wxButton* returnbt = new wxButton(this, ID_BUTTON_PREV, wxT("Return to project"));
95   returnbt->SetToolTip(wxT("Return to the active project description."));
96   sizer->Add(returnbt, 0, wxALIGN_CENTER | wxALL, 5);
97
98   //Title
99   sizer->Add(new wxStaticText(this, -1, _("Package")),0, wxALIGN_CENTER, 0);
100
101   //Image
102   sizer->Add(new wxStaticBitmap(this, -1, wxBitmap(PkIcon64)),0, wxALIGN_CENTER, 0);
103
104   //Package Name
105   sizer->Add(new wxStaticText(this, -1, crea::std2wx(this->package->GetNamePackage())),0, wxALIGN_CENTER, 0);
106
107   //Package Properties
108   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Properties"));
109   wxPanel* propertiesPanel = new wxPanel(this);
110   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
111   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(4, 2, 9, 15);
112
113   wxStaticText *pAuthor = new wxStaticText(propertiesPanel, -1, wxT("Author"));
114   wxStaticText *pVersion = new wxStaticText(propertiesPanel, -1, wxT("Version"));
115   wxStaticText *pDescription = new wxStaticText(propertiesPanel, -1, wxT("Description"));
116
117   // author
118   wxBoxSizer* pAuthorsz = new wxBoxSizer(wxHORIZONTAL);
119   this->authortc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(this->package->GetAuthors()));
120   wxButton* pAuthorbt = new wxButton(propertiesPanel, ID_BUTTON_SET_AUTHOR, wxT("Change"));
121   pAuthorbt->SetToolTip(wxT("Update the author/s of the package."));
122   pAuthorsz->Add(this->authortc, 1, wxALIGN_CENTER_VERTICAL);
123   pAuthorsz->Add(pAuthorbt, 0, wxALIGN_CENTER | wxLEFT, 10);
124
125   // version
126   wxBoxSizer* pVersionsz = new wxBoxSizer(wxHORIZONTAL);
127   this->versiontc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(this->package->GetVersion()));
128   wxButton* pVersionbt = new wxButton(propertiesPanel, ID_BUTTON_SET_VERSION, wxT("Set"));
129   pVersionbt->SetToolTip(wxT("Update the version of the package."));
130   pVersionsz->Add(this->versiontc, 1, wxALIGN_CENTER_VERTICAL);
131   pVersionsz->Add(pVersionbt, 0, wxALIGN_CENTER | wxLEFT, 10);
132
133   // description
134   wxBoxSizer* pDescriptionsz = new wxBoxSizer(wxHORIZONTAL);
135   this->descriptiontc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(this->package->GetDescription()));
136   wxButton* pDescriptionbt = new wxButton(propertiesPanel, ID_BUTTON_SET_DESCRIPTION, wxT("Change"));
137   pDescriptionbt->SetToolTip(wxT("Update the description of the project."));
138   pDescriptionsz->Add(this->descriptiontc, 1, wxALIGN_CENTER_VERTICAL);
139   pDescriptionsz->Add(pDescriptionbt, 0, wxALIGN_CENTER | wxLEFT, 10);
140
141   propertiesGridSizer->Add(pAuthor, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
142   propertiesGridSizer->Add(pAuthorsz, 1, wxEXPAND);
143   propertiesGridSizer->Add(pVersion, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
144   propertiesGridSizer->Add(pVersionsz, 1, wxEXPAND);
145   propertiesGridSizer->Add(pDescription, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
146   propertiesGridSizer->Add(pDescriptionsz, 1, wxEXPAND);
147
148   propertiesGridSizer->AddGrowableCol(1,1);
149
150   propertiesPanelSizer -> Add(propertiesGridSizer, 1, wxEXPAND);
151   propertiesPanel->SetSizer(propertiesPanelSizer);
152   propertiesPanelSizer->Fit(propertiesPanel);
153   propertiesBox->Add(propertiesPanel, 0, wxEXPAND | wxALL, 5);
154
155   sizer->Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
156
157   //Black Boxes
158   wxStaticBoxSizer* BBBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("A&vailable Black Boxes"));
159   BBBox->GetStaticBox()->SetToolTip(wxT("Select any of the available black boxes to see its details or modify them."));
160   wxPanel* BBPanel = new wxPanel(this);
161   wxBoxSizer* BBPanelSizer = new wxBoxSizer(wxVERTICAL);
162
163   std::vector<modelCDMBlackBox*> blackBoxes = this->package->GetBlackBoxes();
164   for (int i = 0; i < blackBoxes.size(); i++)
165     {
166       wxHyperlinkCtrl* pBBlk = new wxHyperlinkCtrl(BBPanel,ID_LINK_SELECT_BLACKBOX, crea::std2wx(blackBoxes[i]->GetName().c_str()), crea::std2wx(blackBoxes[i]->GetName().c_str()));
167       std::string tt = "Author: " + blackBoxes[i]->GetAuthors() + "\nDescription: " + blackBoxes[i]->GetDescription() + "\nCategories: " + blackBoxes[i]->GetCategories();
168       pBBlk->SetToolTip(crea::std2wx(tt));
169       pBBlk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageDescriptionPanel::OnMouseEnter,NULL,this);
170       pBBlk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageDescriptionPanel::OnMouseExit,NULL,this);
171       BBPanelSizer -> Add(pBBlk, 0, wxALIGN_LEFT | wxALL, 5);
172     }
173
174   BBPanel->SetSizer(BBPanelSizer);
175   BBPanelSizer->Fit(BBPanel);
176   BBBox->Add(BBPanel, 0, wxALL, 5);
177
178   sizer -> Add(BBBox, 0, wxEXPAND | wxALL, 10);
179
180   //Actions
181   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
182   wxPanel* actionsPanel = new wxPanel(this);
183   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
184
185   wxButton* createBBbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_BLACKBOX, _T("Create Black Box"));
186   createBBbt->SetToolTip(wxT("Create a new black box for the active project inside this package."));
187   actionsPanelSizer->Add(createBBbt, 0, wxALL, 5);
188   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("Edit CMakeLists File"));
189   editCMakebt->SetToolTip(wxT("Open the system default text editor to edit the package's CMakeLists.txt file."));
190   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageDescriptionPanel::OnCMakeMouseEnter,NULL,this);
191   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageDescriptionPanel::OnCMakeMouseExit,NULL,this);
192   actionsPanelSizer->Add(editCMakebt, 0, wxALL, 5);
193   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("Open Package Folder"));
194   openFolderbt->SetToolTip(wxT("Open the package folder in the file explorer."));
195   actionsPanelSizer->Add(openFolderbt, 0, wxALL, 5);
196
197   actionsPanel->SetSizer(actionsPanelSizer);
198   actionsPanelSizer->Fit(actionsPanel);
199   actionsBox->Add(actionsPanel, 0, wxEXPAND);
200   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
201
202   //Assign sizer
203   SetSizer(sizer);
204   sizer->SetSizeHints(this);
205 }
206
207 void wxCDMPackageDescriptionPanel::OnBtnReturn(wxCommandEvent& event)
208 {
209   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
210   newEvent->SetInt(((wxCDMMainFrame*)this->GetParent())->GetModel()->GetProject()->GetId());
211   newEvent->SetId(0);
212   wxPostEvent(this->GetParent(), *newEvent);
213 }
214
215 void wxCDMPackageDescriptionPanel::OnBtnSetAuthor(wxCommandEvent& event)
216 {
217   //get author from user
218   wxTextEntryDialog* authDlg = new wxTextEntryDialog(
219       this,
220       wxT("Enter the new authors name. Separate each author with a '/'."),
221       wxT("Change Package Author - creaDevManager"),
222       crea::std2wx(this->package->GetAuthors()),
223       wxTE_MULTILINE | wxOK | wxCANCEL
224   );
225
226   if (authDlg->ShowModal() == wxID_OK)
227     {
228       std::string authorsStr = crea::wx2std(authDlg->GetValue());
229       //check name
230       if(authorsStr.size() > 0)
231         {
232           std::string* result;
233           if(!this->package->SetAuthors(authorsStr, result))
234             wxMessageBox(crea::std2wx(*result),_T("Change Package Author - Error!"),wxOK | wxICON_ERROR);
235         }
236       this->authortc->SetLabel(crea::std2wx(this->package->GetAuthors()));
237       this->authortc->GetParent()->GetSizer()->RecalcSizes();
238     }
239
240 }
241
242 void wxCDMPackageDescriptionPanel::OnBtnSetVersion(wxCommandEvent& event)
243 {
244   //get version
245   wxString versionWx = wxGetTextFromUser(
246       wxT("Enter the new version name"),
247       wxT("Change Package Version - creaDevManager"),
248       crea::std2wx(this->package->GetVersion())
249   );
250   //check name
251   std::vector<std::string> parts;
252   CDMUtilities::splitter::split(parts, crea::wx2std(versionWx), " .", CDMUtilities::splitter::no_empties);
253   if(parts.size() == 3)
254     {
255       std::string* result;
256       if(!this->package->SetVersion(crea::wx2std(versionWx), result))
257         wxMessageBox(crea::std2wx(*result),_T("Change Package Version - Error!"),wxOK | wxICON_ERROR);
258     }
259   else
260     {
261       wxMessageBox(crea::std2wx("The version format is incorrect, please follow the following format:\nX.Y.Z\nX: Major Version\nY: Minor Version\nZ: Build Version"),_T("Set Project Version - Error!"),wxOK | wxICON_ERROR);
262     }
263   this->versiontc->SetLabel(crea::std2wx(this->package->GetVersion()));
264 }
265
266 void wxCDMPackageDescriptionPanel::OnBtnSetDescription(wxCommandEvent& event)
267 {
268   //get author from user
269   wxTextEntryDialog* descDlg = new wxTextEntryDialog(
270       this,
271       wxT("Edit the package description."),
272       wxT("Change Package Description - creaDevManager"),
273       crea::std2wx(this->package->GetDescription()),
274       wxTE_MULTILINE | wxOK | wxCANCEL
275   );
276
277   if (descDlg->ShowModal() == wxID_OK)
278     {
279       std::string descriptionStr = crea::wx2std(descDlg->GetValue());
280       //check name
281       if(descriptionStr.size() > 0)
282         {
283           std::string* result;
284           if(!this->package->SetDescription(descriptionStr, result))
285             wxMessageBox(crea::std2wx(*result),_T("Change Package Description - Error!"),wxOK | wxICON_ERROR);
286         }
287       this->descriptiontc->SetLabel(crea::std2wx(this->package->GetDescription()));
288       this->descriptiontc->GetParent()->GetSizer()->RecalcSizes();
289     }
290 }
291
292 void wxCDMPackageDescriptionPanel::OnLnkBlackBoxSelect(wxHyperlinkEvent& event)
293 {
294   int bbId = 0;
295   std::vector<modelCDMBlackBox*> bbs = this->package->GetBlackBoxes();
296   for (int i = 0; i < bbs.size(); i++)
297     {
298       if(bbs[i]->GetName() == crea::wx2std(event.GetURL()))
299         {
300           bbId = bbs[i]->GetId();
301           break;
302         }
303     }
304
305   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
306   newEvent->SetInt(bbId);
307   newEvent->SetId(0);
308   wxPostEvent(this->GetParent(), *newEvent);
309
310   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
311   newEvent1->SetInt(bbId);
312   newEvent1->SetId(0);
313   wxPostEvent(this->GetParent(), *newEvent1);
314
315 }
316
317 void wxCDMPackageDescriptionPanel::OnBtnCreateBlackBox(wxCommandEvent& event)
318 {
319   //TODO: implement method
320   std::cerr << "Event OnBtnCreateBlackBox not implemented" << std::endl;
321   event.Skip();
322 }
323
324 void wxCDMPackageDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
325 {
326   std::string* result;
327   if(!this->package->OpenCMakeListsFile(result))
328     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
329
330   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
331
332   if(this->package->GetCMakeLists() != NULL)
333     {
334       int CMId = this->package->GetCMakeLists()->GetId();
335       newEvent->SetInt(CMId);
336       newEvent->SetId(0);
337       wxPostEvent(this->GetParent(), *newEvent);
338     }
339
340   event.Skip();
341 }
342
343 void wxCDMPackageDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
344 {
345   std::string* result;
346   if(!this->package->OpenInFileExplorer(result))
347     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
348 }
349
350 void wxCDMPackageDescriptionPanel::OnMouseEnter(wxMouseEvent& event)
351 {
352   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
353   std::string BBName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
354   int bbId = 0;
355   std::vector<modelCDMBlackBox*> boxes = this->package->GetBlackBoxes();
356   for (int i = 0; i < boxes.size(); i++)
357     {
358       if(boxes[i]->GetName() == BBName)
359         {
360           bbId = boxes[i]->GetId();
361           break;
362         }
363     }
364   newEvent->SetInt(bbId);
365   newEvent->SetId(0);
366   wxPostEvent(this->GetParent(), *newEvent);
367   event.Skip();
368 }
369
370 void wxCDMPackageDescriptionPanel::OnMouseExit(wxMouseEvent& event)
371 {
372   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
373   std::string BBName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
374   int bbId = 0;
375   std::vector<modelCDMBlackBox*> boxes = this->package->GetBlackBoxes();
376   for (int i = 0; i < boxes.size(); i++)
377     {
378       if(boxes[i]->GetName() == BBName)
379         {
380           bbId = boxes[i]->GetId();
381           break;
382         }
383     }
384   newEvent->SetInt(bbId);
385   newEvent->SetId(0);
386   wxPostEvent(this->GetParent(), *newEvent);
387   event.Skip();
388 }
389
390 void wxCDMPackageDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
391 {
392   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
393
394   if(this->package->GetCMakeLists() != NULL)
395     {
396       int CMId = this->package->GetCMakeLists()->GetId();
397       newEvent->SetInt(CMId);
398       newEvent->SetId(0);
399       wxPostEvent(this->GetParent(), *newEvent);
400     }
401   event.Skip();
402 }
403
404 void wxCDMPackageDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
405 {
406   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
407
408   if(this->package->GetCMakeLists() != NULL)
409     {
410       int CMId = this->package->GetCMakeLists()->GetId();
411       newEvent->SetInt(CMId);
412       newEvent->SetId(0);
413       wxPostEvent(this->GetParent(), *newEvent);
414     }
415   event.Skip();
416 }
417
418