]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMPackageManagerPanel.cpp
Feature #1711
[crea.git] / lib / creaDevManagerLib / wxCDMPackageManagerPanel.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  * wxCDMPackageManagerPanel.cpp
30  *
31  *  Created on: Dec 10, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "wxCDMPackageManagerPanel.h"
36
37 #include "wxCDMMainFrame.h"
38 #include "wxCDMNewPackageDialog.h"
39
40 #include "creaDevManagerIds.h"
41 #include "images/PkIcon64.xpm"
42
43 BEGIN_EVENT_TABLE(wxCDMPackageManagerPanel, wxPanel)
44 EVT_BUTTON(ID_BUTTON_PREV, wxCDMPackageManagerPanel::OnBtnReturn)
45 EVT_HYPERLINK(ID_LINK_SELECT_PACKAGE, wxCDMPackageManagerPanel::OnLnkPackageSelect)
46 EVT_BUTTON(ID_BUTTON_CREATE_PACKAGE, wxCDMPackageManagerPanel::OnBtnCreatePackage)
47 EVT_BUTTON(ID_BUTTON_EDIT_CMAKELISTSFILE, wxCDMPackageManagerPanel::OnBtnEditCMakeLists)
48 END_EVENT_TABLE()
49
50 wxCDMPackageManagerPanel::wxCDMPackageManagerPanel(
51     wxWindow* parent,
52     modelCDMProject* project,
53     wxWindowID id,
54     const wxString& caption,
55     const wxPoint& pos,
56     const wxSize& size,
57     long style
58 )
59 {
60   wxCDMPackageManagerPanel::Create(parent, project, id, caption, pos, size, style);
61 }
62
63 wxCDMPackageManagerPanel::~wxCDMPackageManagerPanel()
64 {
65 }
66
67 bool wxCDMPackageManagerPanel::Create(
68     wxWindow* parent,
69     modelCDMProject* project,
70     wxWindowID id,
71     const wxString& caption,
72     const wxPoint& pos,
73     const wxSize& size,
74     long style
75 )
76 {
77   wxPanel::Create(parent, id, pos, size, style);
78   this->project = project;
79   CreateControls();
80   return TRUE;
81 }
82
83 void wxCDMPackageManagerPanel::CreateControls()
84 {
85   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
86
87   //Link to return
88   wxButton* returnbt = new wxButton(this, ID_BUTTON_PREV, wxT("Return to project"));
89   returnbt->SetToolTip(wxT("Return to the active project description."));
90   sizer->Add(returnbt, 0, wxALIGN_CENTER | wxALL, 5);
91
92   //Title
93   sizer->Add(new wxStaticText(this, -1, _("Package Management")),0, wxALIGN_CENTER, 0);
94
95   //Image
96   sizer->Add(new wxStaticBitmap(this, -1, wxBitmap(PkIcon64)),0, wxALIGN_CENTER, 0);
97
98   //Packages
99   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("A&vailable Packages"));
100   propertiesBox->GetStaticBox()->SetToolTip(wxT("Select any of the available packages to see its details or modify them. Remember that black boxes are created inside packages, any of these packages is available."));
101   wxPanel* propertiesPanel = new wxPanel(this);
102   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
103
104   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
105   for (int i = 0; i < packages.size(); i++)
106     {
107       wxHyperlinkCtrl* pPackagelk = new wxHyperlinkCtrl(propertiesPanel,ID_LINK_SELECT_PACKAGE, crea::std2wx(packages[i]->GetName().c_str()), crea::std2wx(packages[i]->GetName().c_str()));
108       std::string tt = "Author: " + packages[i]->GetAuthors() + "\nDescription: " + packages[i]->GetDescription();
109       pPackagelk->SetToolTip(crea::std2wx(tt));
110       propertiesPanelSizer -> Add(pPackagelk, 0, wxALIGN_LEFT | wxALL, 5);
111       pPackagelk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnMouseEnter,NULL,this);
112       pPackagelk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnMouseExit,NULL,this);
113     }
114
115   propertiesPanel->SetSizer(propertiesPanelSizer);
116   propertiesPanelSizer->Fit(propertiesPanel);
117   propertiesBox->Add(propertiesPanel, 0, wxALL, 5);
118
119   sizer -> Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
120
121   //Actions
122   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
123   wxPanel* actionsPanel = new wxPanel(this);
124   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
125
126   wxButton* createPkgbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_PACKAGE, _T("Create Package"));
127   createPkgbt->SetToolTip(wxT("Create a new package for this project."));
128   actionsPanelSizer->Add(createPkgbt, 0, wxALL, 5);
129   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("Edit CMakeLists File"));
130   editCMakebt->SetToolTip(wxT("Edit the CMakeLists.txt file of this project."));
131   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnCMakeMouseEnter,NULL,this);
132   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnCMakeMouseExit,NULL,this);
133   actionsPanelSizer->Add(editCMakebt, 0, wxALL, 5);
134
135   actionsPanel->SetSizer(actionsPanelSizer);
136   actionsPanelSizer->Fit(actionsPanel);
137   actionsBox->Add(actionsPanel, 0, wxEXPAND);
138   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
139
140   //Assign sizer
141   SetSizer(sizer);
142   sizer->SetSizeHints(this);
143 }
144
145 void wxCDMPackageManagerPanel::OnBtnReturn(wxCommandEvent& event)
146 {
147   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
148   newEvent->SetInt(project->GetId());
149   newEvent->SetId(0);
150   wxPostEvent(this->GetParent(), *newEvent);
151 }
152
153 void wxCDMPackageManagerPanel::OnLnkPackageSelect(wxHyperlinkEvent& event)
154 {
155   int packageId = 0;
156   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
157   for (int i = 0; i < packages.size(); i++)
158     {
159       if(packages[i]->GetName() == crea::wx2std(event.GetURL()))
160         {
161           packageId = packages[i]->GetId();
162           break;
163         }
164     }
165
166   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
167   newEvent->SetInt(packageId);
168   newEvent->SetId(0);
169   wxPostEvent(this->GetParent(), *newEvent);
170
171   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
172   newEvent1->SetInt(packageId);
173   newEvent1->SetId(0);
174   wxPostEvent(this->GetParent(), *newEvent1);
175
176 }
177
178 void wxCDMPackageManagerPanel::OnBtnCreatePackage(wxCommandEvent& event)
179 {
180   std::string* result;
181
182   wxCDMNewPackageDialog* dialog = new wxCDMNewPackageDialog(this);
183   long userResponse;
184   userResponse = dialog->ShowModal();
185
186   if(userResponse == wxID_FORWARD)
187     {
188       modelCDMIProjectTreeNode* package = this->project->CreatePackage(
189           crea::wx2std(dialog->GetPackageName()),
190           result,
191           crea::wx2std(dialog->GetPackageAuthor()),
192           crea::wx2std(dialog->GetPackageAuthorEmail()),
193           crea::wx2std(dialog->GetPackageDescription())
194       );
195       if(package == NULL)
196         {
197           std::cout << "error creating package: " << *result << std::endl;
198           wxMessageBox(crea::std2wx(*result),_T("New Package - Error!"),wxOK | wxICON_ERROR);
199           event.Skip();
200           return;
201         }
202       wxMessageBox(crea::std2wx("Package successfully created."),_T("New Package - Success!"),wxOK | wxICON_INFORMATION);
203
204       //refreshing tree and description
205       //send event instead of calling parent to avoid crashing
206
207       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
208
209       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
210       newEvent->SetInt(package->GetId());
211       newEvent->SetId(0);
212       wxPostEvent(this->GetParent(), *newEvent);
213       event.Skip();
214     }
215
216   event.Skip();
217 }
218
219 void wxCDMPackageManagerPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
220 {
221   std::string* result;
222   if(!this->project->OpenCMakeListsFile(result))
223     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
224
225   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
226
227   if(this->project->GetCMakeLists() != NULL)
228     {
229       int CMId = this->project->GetCMakeLists()->GetId();
230       newEvent->SetInt(CMId);
231       newEvent->SetId(0);
232       wxPostEvent(this->GetParent(), *newEvent);
233     }
234 }
235
236 void wxCDMPackageManagerPanel::OnMouseEnter(wxMouseEvent& event)
237 {
238   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
239   std::string PkgName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
240   int pkgId = 0;
241   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
242   for (int i = 0; i < packages.size(); i++)
243     {
244       if(packages[i]->GetName() == PkgName)
245         {
246           pkgId = packages[i]->GetId();
247           break;
248         }
249     }
250   newEvent->SetInt(pkgId);
251   newEvent->SetId(0);
252   wxPostEvent(this->GetParent(), *newEvent);
253   event.Skip();
254 }
255
256 void wxCDMPackageManagerPanel::OnMouseExit(wxMouseEvent& event)
257 {
258   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
259   std::string PkgName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
260   int pkgId = 0;
261   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
262   project->GetPackages();
263   for (int i = 0; i < packages.size(); i++)
264     {
265       if(packages[i]->GetName() == PkgName)
266         {
267           pkgId = packages[i]->GetId();
268           break;
269         }
270     }
271   newEvent->SetInt(pkgId);
272   newEvent->SetId(0);
273   wxPostEvent(this->GetParent(), *newEvent);
274   event.Skip();
275 }
276
277 void wxCDMPackageManagerPanel::OnCMakeMouseEnter(wxMouseEvent& event)
278 {
279   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
280
281   if(this->project->GetCMakeLists() != NULL)
282     {
283       int CMId = this->project->GetCMakeLists()->GetId();
284       newEvent->SetInt(CMId);
285       newEvent->SetId(0);
286       wxPostEvent(this->GetParent(), *newEvent);
287     }
288   event.Skip();
289 }
290
291 void wxCDMPackageManagerPanel::OnCMakeMouseExit(wxMouseEvent& event)
292 {
293   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
294
295   if(this->project->GetCMakeLists() != NULL)
296     {
297       int CMId = this->project->GetCMakeLists()->GetId();
298       newEvent->SetInt(CMId);
299       newEvent->SetId(0);
300       wxPostEvent(this->GetParent(), *newEvent);
301     }
302   event.Skip();
303 }
304
305