]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMPackageManagerPanel.cpp
cd3adf3fa28ef4313a25325d39597fab7f34bf0c
[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_HYPERLINK(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   wxHyperlinkCtrl* returnLnk = new wxHyperlinkCtrl(this, ID_BUTTON_PREV, crea::std2wx(this->project->GetName()), crea::std2wx(this->project->GetPath()));
93   returnLnk->SetToolTip(wxT("Return to the active project description."));
94   sizer->Add(returnLnk, 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       pPackagelk->SetWindowStyle(wxALIGN_LEFT);
120       std::string tt = "Author: " + packages[i]->GetAuthors() + "\nDescription: " + packages[i]->GetDescription();
121       pPackagelk->SetToolTip(crea::std2wx(tt));
122       propertiesPanelSizer -> Add(pPackagelk, 0, wxEXPAND | wxALL, 5);
123       pPackagelk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnMouseEnter,NULL,this);
124       pPackagelk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnMouseExit,NULL,this);
125     }
126
127   propertiesPanel->SetSizer(propertiesPanelSizer);
128   propertiesPanelSizer->Fit(propertiesPanel);
129   propertiesBox->Add(propertiesPanel, 1, wxALL | wxEXPAND, 5);
130
131   sizer -> Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
132
133   //Actions
134   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
135   wxPanel* actionsPanel = new wxPanel(this);
136   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
137
138   //actionsGrid Sizer
139   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(1, 2, 9, 15);
140
141   wxButton* createPkgbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_PACKAGE, _T("A. Create Package"));
142   createPkgbt->SetToolTip(wxT("Create a new package for this project."));
143   actionsGridSizer->Add(createPkgbt, 1, wxALL | wxEXPAND, 5);
144   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("B. Edit CMakeLists File"));
145   editCMakebt->SetToolTip(wxT("Edit the CMakeLists.txt file of this project."));
146   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnCMakeMouseEnter,NULL,this);
147   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnCMakeMouseExit,NULL,this);
148   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
149
150   actionsGridSizer->AddGrowableCol(0,1);
151   actionsGridSizer->AddGrowableCol(1,1);
152
153   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
154   actionsPanel->SetSizer(actionsPanelSizer);
155   actionsPanelSizer->Fit(actionsPanel);
156   actionsBox->Add(actionsPanel, 1, wxEXPAND);
157   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
158
159   //Assign sizer
160   SetSizer(sizer);
161   sizer->SetSizeHints(this);
162
163   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
164     {
165       wxCDMPackageManagerHelpDialog* helpDialog = new wxCDMPackageManagerHelpDialog(this->GetParent(), this, wxID_ANY);
166       helpDialog->Show(true);
167     }
168 }
169
170 modelCDMProject* wxCDMPackageManagerPanel::GetProject() const
171 {
172   return this->project;
173 }
174
175 void wxCDMPackageManagerPanel::OnBtnReturn(wxHyperlinkEvent& event)
176 {
177   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
178   newEvent->SetClientData(project);
179   newEvent->SetId(0);
180   wxPostEvent(this->GetParent(), *newEvent);
181 }
182
183 void wxCDMPackageManagerPanel::OnLnkPackageSelect(wxHyperlinkEvent& event)
184 {
185   modelCDMPackage* thePackage = NULL;
186   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
187   for (int i = 0; i < packages.size(); i++)
188     {
189       if(packages[i]->GetName() == crea::wx2std(event.GetURL()))
190         {
191           thePackage = packages[i];
192           break;
193         }
194     }
195
196   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
197   newEvent->SetClientData(thePackage);
198   newEvent->SetId(0);
199   wxPostEvent(this->GetParent(), *newEvent);
200
201   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
202   newEvent1->SetClientData(thePackage);
203   newEvent1->SetId(0);
204   wxPostEvent(this->GetParent(), *newEvent1);
205
206 }
207
208 void wxCDMPackageManagerPanel::OnBtnCreatePackage(wxCommandEvent& event)
209 {
210   std::string* result;
211
212   wxCDMNewPackageDialog* dialog = new wxCDMNewPackageDialog(this);
213   long userResponse;
214   userResponse = dialog->ShowModal();
215
216   if(userResponse == wxID_FORWARD)
217     {
218       modelCDMIProjectTreeNode* package = this->project->CreatePackage(
219           crea::wx2std(dialog->GetPackageName()),
220           result,
221           crea::wx2std(dialog->GetPackageAuthor()),
222           crea::wx2std(dialog->GetPackageAuthorEmail()),
223           crea::wx2std(dialog->GetPackageDescription())
224       );
225       if(package == NULL)
226         {
227           std::cout << "error creating package: " << *result << std::endl;
228           wxMessageBox(crea::std2wx(*result),_T("New Package - Error!"),wxOK | wxICON_ERROR);
229           event.Skip();
230           return;
231         }
232       wxMessageBox(crea::std2wx("Package successfully created."),_T("New Package - Success!"),wxOK | wxICON_INFORMATION);
233
234       //refreshing tree and description
235       //send event instead of calling parent to avoid crashing
236
237       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
238
239       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
240       newEvent->SetClientData(package);
241       newEvent->SetId(0);
242       wxPostEvent(this->GetParent(), *newEvent);
243       event.Skip();
244     }
245
246   event.Skip();
247 }
248
249 void wxCDMPackageManagerPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
250 {
251   std::string* result;
252   if(!this->project->OpenCMakeListsFile(result))
253     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
254
255   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
256
257   if(this->project->GetCMakeLists() != NULL)
258     {
259       newEvent->SetClientData(this->project->GetCMakeLists());
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_LISTBOX_DOUBLECLICKED);
268   std::string PkgName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
269   modelCDMPackage* thePackage = NULL;
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           thePackage = packages[i];
276           break;
277         }
278     }
279   newEvent->SetClientData(thePackage);
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_LISTBOX_SELECTED);
288   std::string PkgName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
289   modelCDMPackage* thePackage = NULL;
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           thePackage = packages[i];
297           break;
298         }
299     }
300   newEvent->SetClientData(thePackage);
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_LISTBOX_DOUBLECLICKED);
309
310   if(this->project->GetCMakeLists() != NULL)
311     {
312       newEvent->SetClientData(this->project->GetCMakeLists());
313       newEvent->SetId(0);
314       wxPostEvent(this->GetParent(), *newEvent);
315     }
316   event.Skip();
317 }
318
319 void wxCDMPackageManagerPanel::OnCMakeMouseExit(wxMouseEvent& event)
320 {
321   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
322
323   if(this->project->GetCMakeLists() != NULL)
324     {
325       newEvent->SetClientData(this->project->GetCMakeLists());
326       newEvent->SetId(0);
327       wxPostEvent(this->GetParent(), *newEvent);
328     }
329   event.Skip();
330 }
331
332