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