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