]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMPackageDescriptionPanel.cpp
4411c9eeb5cc9963864c7c8e4090aa35b64cd413
[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
164   std::vector<modelCDMBlackBox*> blackBoxes = this->package->GetSrc()->GetBlackBoxes();
165   for (int i = 0; i < blackBoxes.size(); i++)
166     {
167
168       if(blackBoxes[i] != NULL)
169         {
170
171           wxHyperlinkCtrl* pBBlk = new wxHyperlinkCtrl(BBPanel,ID_LINK_SELECT_BLACKBOX, crea::std2wx(blackBoxes[i]->GetName().c_str()), crea::std2wx(blackBoxes[i]->GetName().c_str()));
172           std::string tt = "Author: " + blackBoxes[i]->GetAuthors() + "\nDescription: " + blackBoxes[i]->GetDescription() + "\nCategories: " + blackBoxes[i]->GetCategories();
173           pBBlk->SetToolTip(crea::std2wx(tt));
174           pBBlk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageDescriptionPanel::OnMouseEnter,NULL,this);
175           pBBlk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageDescriptionPanel::OnMouseExit,NULL,this);
176           BBPanelSizer -> Add(pBBlk, 0, wxALIGN_LEFT | wxALL, 5);
177         }
178     }
179
180   BBPanel->SetSizer(BBPanelSizer);
181   BBPanelSizer->Fit(BBPanel);
182   BBBox->Add(BBPanel, 0, wxALL, 5);
183
184   sizer -> Add(BBBox, 0, wxEXPAND | wxALL, 10);
185
186   //Actions
187   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
188   wxPanel* actionsPanel = new wxPanel(this);
189   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
190
191   wxButton* createBBbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_BLACKBOX, _T("Create Black Box"));
192   createBBbt->SetToolTip(wxT("Create a new black box for the active project inside this package."));
193   actionsPanelSizer->Add(createBBbt, 0, wxALL, 5);
194   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("Edit CMakeLists File"));
195   editCMakebt->SetToolTip(wxT("Open the system default text editor to edit the package's CMakeLists.txt file."));
196   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageDescriptionPanel::OnCMakeMouseEnter,NULL,this);
197   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageDescriptionPanel::OnCMakeMouseExit,NULL,this);
198   actionsPanelSizer->Add(editCMakebt, 0, wxALL, 5);
199   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("Open Package Folder"));
200   openFolderbt->SetToolTip(wxT("Open the package folder in the file explorer."));
201   actionsPanelSizer->Add(openFolderbt, 0, wxALL, 5);
202
203   actionsPanel->SetSizer(actionsPanelSizer);
204   actionsPanelSizer->Fit(actionsPanel);
205   actionsBox->Add(actionsPanel, 0, wxEXPAND);
206   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
207
208   //Assign sizer
209   SetSizer(sizer);
210   sizer->SetSizeHints(this);
211 }
212
213 void wxCDMPackageDescriptionPanel::OnBtnReturn(wxCommandEvent& event)
214 {
215   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
216   newEvent->SetInt(((wxCDMMainFrame*)this->GetParent())->GetModel()->GetProject()->GetId());
217   newEvent->SetId(0);
218   wxPostEvent(this->GetParent(), *newEvent);
219 }
220
221 void wxCDMPackageDescriptionPanel::OnBtnSetAuthor(wxCommandEvent& event)
222 {
223   //get author from user
224   wxTextEntryDialog* authDlg = new wxTextEntryDialog(
225       this,
226       wxT("Enter the new authors name. Separate each author with a '/'."),
227       wxT("Change Package Author - creaDevManager"),
228       crea::std2wx(this->package->GetAuthors()),
229       wxTE_MULTILINE | wxOK | wxCANCEL
230   );
231
232   if (authDlg->ShowModal() == wxID_OK)
233     {
234       std::string authorsStr = crea::wx2std(authDlg->GetValue());
235       //check name
236       if(authorsStr.size() > 0)
237         {
238           std::string* result;
239           if(!this->package->SetAuthors(authorsStr, result))
240             wxMessageBox(crea::std2wx(*result),_T("Change Package Author - Error!"),wxOK | wxICON_ERROR);
241         }
242       this->authortc->SetLabel(crea::std2wx(this->package->GetAuthors()));
243       this->authortc->GetParent()->GetSizer()->RecalcSizes();
244     }
245
246 }
247
248 void wxCDMPackageDescriptionPanel::OnBtnSetVersion(wxCommandEvent& event)
249 {
250   //get version
251   wxString versionWx = wxGetTextFromUser(
252       wxT("Enter the new version name"),
253       wxT("Change Package Version - creaDevManager"),
254       crea::std2wx(this->package->GetVersion())
255   );
256   //check name
257   std::vector<std::string> parts;
258   CDMUtilities::splitter::split(parts, crea::wx2std(versionWx), " .", CDMUtilities::splitter::no_empties);
259   if(parts.size() == 3)
260     {
261       std::string* result;
262       if(!this->package->SetVersion(crea::wx2std(versionWx), result))
263         wxMessageBox(crea::std2wx(*result),_T("Change Package Version - Error!"),wxOK | wxICON_ERROR);
264     }
265   else
266     {
267       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);
268     }
269   this->versiontc->SetLabel(crea::std2wx(this->package->GetVersion()));
270 }
271
272 void wxCDMPackageDescriptionPanel::OnBtnSetDescription(wxCommandEvent& event)
273 {
274   //get description from user
275   wxTextEntryDialog* descDlg = new wxTextEntryDialog(
276       this,
277       wxT("Edit the package description."),
278       wxT("Change Package Description - creaDevManager"),
279       crea::std2wx(this->package->GetDescription()),
280       wxTE_MULTILINE | wxOK | wxCANCEL
281   );
282
283   if (descDlg->ShowModal() == wxID_OK)
284     {
285       std::string descriptionStr = crea::wx2std(descDlg->GetValue());
286       //check desc
287       if(descriptionStr.size() > 0)
288         {
289           std::string* result;
290           if(!this->package->SetDescription(descriptionStr, result))
291             wxMessageBox(crea::std2wx(*result),_T("Change Package Description - Error!"),wxOK | wxICON_ERROR);
292         }
293       this->descriptiontc->SetLabel(crea::std2wx(this->package->GetDescription()));
294       this->descriptiontc->GetParent()->GetSizer()->RecalcSizes();
295     }
296 }
297
298 void wxCDMPackageDescriptionPanel::OnLnkBlackBoxSelect(wxHyperlinkEvent& event)
299 {
300   int bbId = 0;
301   modelCDMBlackBox* bb;
302   std::vector<modelCDMBlackBox*> bbs = this->package->GetSrc()->GetBlackBoxes();
303   for (int i = 0; i < bbs.size(); i++)
304     {
305       if(bbs[i]->GetName() == crea::wx2std(event.GetURL()))
306         {
307           bbId = bbs[i]->GetHeaderFile()->GetId();
308           bb = bbs[i];
309           break;
310         }
311     }
312
313   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
314   newEvent->SetClientData(bb);
315   newEvent->SetId(1);
316   newEvent->SetString(wxT("blackbox"));
317   wxPostEvent(this->GetParent(), *newEvent);
318
319   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
320   newEvent1->SetInt(bbId);
321   newEvent1->SetId(0);
322   wxPostEvent(this->GetParent(), *newEvent1);
323
324 }
325
326 void wxCDMPackageDescriptionPanel::OnBtnCreateBlackBox(wxCommandEvent& event)
327 {
328   //TODO: implement method
329   std::cerr << "Event OnBtnCreateBlackBox not implemented" << std::endl;
330   event.Skip();
331 }
332
333 void wxCDMPackageDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
334 {
335   std::string* result;
336   if(!this->package->OpenCMakeListsFile(result))
337     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
338
339   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
340
341   if(this->package->GetCMakeLists() != NULL)
342     {
343       int CMId = this->package->GetCMakeLists()->GetId();
344       newEvent->SetInt(CMId);
345       newEvent->SetId(0);
346       wxPostEvent(this->GetParent(), *newEvent);
347     }
348
349   event.Skip();
350 }
351
352 void wxCDMPackageDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
353 {
354   std::string* result;
355   if(!this->package->OpenInFileExplorer(result))
356     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
357 }
358
359 void wxCDMPackageDescriptionPanel::OnMouseEnter(wxMouseEvent& event)
360 {
361   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
362   std::string BBName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
363
364   int bbId = 0;
365   std::vector<modelCDMBlackBox*> boxes = this->package->GetSrc()->GetBlackBoxes();
366   for (int i = 0; i < boxes.size(); i++)
367     {
368       if(boxes[i]->GetName() == BBName)
369         {
370           bbId = boxes[i]->GetHeaderFile()->GetId();
371           break;
372         }
373     }
374   newEvent->SetInt(bbId);
375   newEvent->SetId(0);
376   wxPostEvent(this->GetParent(), *newEvent);
377   event.Skip();
378 }
379
380 void wxCDMPackageDescriptionPanel::OnMouseExit(wxMouseEvent& event)
381 {
382   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
383   std::string BBName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
384   int bbId = 0;
385   std::vector<modelCDMBlackBox*> boxes = this->package->GetSrc()->GetBlackBoxes();
386   for (int i = 0; i < boxes.size(); i++)
387     {
388       if(boxes[i]->GetName() == BBName)
389         {
390           bbId = boxes[i]->GetHeaderFile()->GetId();
391           break;
392         }
393     }
394   newEvent->SetInt(bbId);
395   newEvent->SetId(0);
396   wxPostEvent(this->GetParent(), *newEvent);
397   event.Skip();
398 }
399
400 void wxCDMPackageDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
401 {
402   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
403
404   if(this->package->GetCMakeLists() != NULL)
405     {
406       int CMId = this->package->GetCMakeLists()->GetId();
407       newEvent->SetInt(CMId);
408       newEvent->SetId(0);
409       wxPostEvent(this->GetParent(), *newEvent);
410     }
411   event.Skip();
412 }
413
414 void wxCDMPackageDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
415 {
416   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
417
418   if(this->package->GetCMakeLists() != NULL)
419     {
420       int CMId = this->package->GetCMakeLists()->GetId();
421       newEvent->SetInt(CMId);
422       newEvent->SetId(0);
423       wxPostEvent(this->GetParent(), *newEvent);
424     }
425   event.Skip();
426 }
427
428