]> 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 "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   //Header
97   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
98   {
99     //Image
100     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(PkIcon64)),0, wxALIGN_CENTER, 0);
101     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
102     //Title
103     textSizer->Add(new wxStaticText(this, -1, _("Package Management")),0, wxALIGN_LEFT, 0);
104
105     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
106   }
107   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
108
109   //Packages
110   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("A&vailable Packages"));
111   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."));
112   wxPanel* propertiesPanel = new wxPanel(this);
113   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
114
115   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
116   for (int i = 0; i < packages.size(); i++)
117     {
118       wxHyperlinkCtrl* pPackagelk = new wxHyperlinkCtrl(propertiesPanel,ID_LINK_SELECT_PACKAGE, crea::std2wx(packages[i]->GetName().c_str()), crea::std2wx(packages[i]->GetName().c_str()));
119       std::string tt = "Author: " + packages[i]->GetAuthors() + "\nDescription: " + packages[i]->GetDescription();
120       pPackagelk->SetToolTip(crea::std2wx(tt));
121       propertiesPanelSizer -> Add(pPackagelk, 0, wxALIGN_LEFT | wxALL, 5);
122       pPackagelk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnMouseEnter,NULL,this);
123       pPackagelk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnMouseExit,NULL,this);
124     }
125
126   propertiesPanel->SetSizer(propertiesPanelSizer);
127   propertiesPanelSizer->Fit(propertiesPanel);
128   propertiesBox->Add(propertiesPanel, 0, wxALL, 5);
129
130   sizer -> Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
131
132   //Actions
133   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
134   wxPanel* actionsPanel = new wxPanel(this);
135   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
136
137   //actionsGrid Sizer
138   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(1, 2, 9, 15);
139
140   wxButton* createPkgbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_PACKAGE, _T("A. Create Package"));
141   createPkgbt->SetToolTip(wxT("Create a new package for this project."));
142   actionsGridSizer->Add(createPkgbt, 1, wxALL | wxEXPAND, 5);
143   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("B. Edit CMakeLists File"));
144   editCMakebt->SetToolTip(wxT("Edit the CMakeLists.txt file of this project."));
145   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnCMakeMouseEnter,NULL,this);
146   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnCMakeMouseExit,NULL,this);
147   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
148
149   actionsGridSizer->AddGrowableCol(0,1);
150   actionsGridSizer->AddGrowableCol(1,1);
151
152   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
153   actionsPanel->SetSizer(actionsPanelSizer);
154   actionsPanelSizer->Fit(actionsPanel);
155   actionsBox->Add(actionsPanel, 1, wxEXPAND);
156   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
157
158   //Assign sizer
159   SetSizer(sizer);
160   sizer->SetSizeHints(this);
161
162   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
163     {
164       wxCDMPackageManagerHelpDialog* helpDialog = new wxCDMPackageManagerHelpDialog(this->GetParent(), this, wxID_ANY);
165       helpDialog->Show(true);
166     }
167 }
168
169 modelCDMProject* wxCDMPackageManagerPanel::GetProject() const
170 {
171   return this->project;
172 }
173
174 void wxCDMPackageManagerPanel::OnBtnReturn(wxCommandEvent& event)
175 {
176   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
177   newEvent->SetInt(project->GetId());
178   newEvent->SetId(0);
179   wxPostEvent(this->GetParent(), *newEvent);
180 }
181
182 void wxCDMPackageManagerPanel::OnLnkPackageSelect(wxHyperlinkEvent& event)
183 {
184   int packageId = 0;
185   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
186   for (int i = 0; i < packages.size(); i++)
187     {
188       if(packages[i]->GetName() == crea::wx2std(event.GetURL()))
189         {
190           packageId = packages[i]->GetId();
191           break;
192         }
193     }
194
195   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
196   newEvent->SetInt(packageId);
197   newEvent->SetId(0);
198   wxPostEvent(this->GetParent(), *newEvent);
199
200   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
201   newEvent1->SetInt(packageId);
202   newEvent1->SetId(0);
203   wxPostEvent(this->GetParent(), *newEvent1);
204
205 }
206
207 void wxCDMPackageManagerPanel::OnBtnCreatePackage(wxCommandEvent& event)
208 {
209   std::string* result;
210
211   wxCDMNewPackageDialog* dialog = new wxCDMNewPackageDialog(this);
212   long userResponse;
213   userResponse = dialog->ShowModal();
214
215   if(userResponse == wxID_FORWARD)
216     {
217       modelCDMIProjectTreeNode* package = this->project->CreatePackage(
218           crea::wx2std(dialog->GetPackageName()),
219           result,
220           crea::wx2std(dialog->GetPackageAuthor()),
221           crea::wx2std(dialog->GetPackageAuthorEmail()),
222           crea::wx2std(dialog->GetPackageDescription())
223       );
224       if(package == NULL)
225         {
226           std::cout << "error creating package: " << *result << std::endl;
227           wxMessageBox(crea::std2wx(*result),_T("New Package - Error!"),wxOK | wxICON_ERROR);
228           event.Skip();
229           return;
230         }
231       wxMessageBox(crea::std2wx("Package successfully created."),_T("New Package - Success!"),wxOK | wxICON_INFORMATION);
232
233       //refreshing tree and description
234       //send event instead of calling parent to avoid crashing
235
236       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
237
238       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
239       newEvent->SetInt(package->GetId());
240       newEvent->SetId(0);
241       wxPostEvent(this->GetParent(), *newEvent);
242       event.Skip();
243     }
244
245   event.Skip();
246 }
247
248 void wxCDMPackageManagerPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
249 {
250   std::string* result;
251   if(!this->project->OpenCMakeListsFile(result))
252     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
253
254   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
255
256   if(this->project->GetCMakeLists() != NULL)
257     {
258       int CMId = this->project->GetCMakeLists()->GetId();
259       newEvent->SetInt(CMId);
260       newEvent->SetId(0);
261       wxPostEvent(this->GetParent(), *newEvent);
262     }
263 }
264
265 void wxCDMPackageManagerPanel::OnMouseEnter(wxMouseEvent& event)
266 {
267   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
268   std::string PkgName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
269   int pkgId = 0;
270   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
271   for (int i = 0; i < packages.size(); i++)
272     {
273       if(packages[i]->GetName() == PkgName)
274         {
275           pkgId = packages[i]->GetId();
276           break;
277         }
278     }
279   newEvent->SetInt(pkgId);
280   newEvent->SetId(0);
281   wxPostEvent(this->GetParent(), *newEvent);
282   event.Skip();
283 }
284
285 void wxCDMPackageManagerPanel::OnMouseExit(wxMouseEvent& event)
286 {
287   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
288   std::string PkgName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
289   int pkgId = 0;
290   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
291   project->GetPackages();
292   for (int i = 0; i < packages.size(); i++)
293     {
294       if(packages[i]->GetName() == PkgName)
295         {
296           pkgId = packages[i]->GetId();
297           break;
298         }
299     }
300   newEvent->SetInt(pkgId);
301   newEvent->SetId(0);
302   wxPostEvent(this->GetParent(), *newEvent);
303   event.Skip();
304 }
305
306 void wxCDMPackageManagerPanel::OnCMakeMouseEnter(wxMouseEvent& event)
307 {
308   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED);
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 void wxCDMPackageManagerPanel::OnCMakeMouseExit(wxMouseEvent& event)
321 {
322   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
323
324   if(this->project->GetCMakeLists() != NULL)
325     {
326       int CMId = this->project->GetCMakeLists()->GetId();
327       newEvent->SetInt(CMId);
328       newEvent->SetId(0);
329       wxPostEvent(this->GetParent(), *newEvent);
330     }
331   event.Skip();
332 }
333
334