]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMPackageManagerPanel.cpp
a706c175ca2c5021e182aaee68b75c6298a8bae8
[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 EVT_CHECKBOX(ID_CHECK_INCLUDE_PACKAGE, wxCDMPackageManagerPanel::OnChBPackageChange)
53 END_EVENT_TABLE()
54
55 wxCDMPackageManagerPanel::wxCDMPackageManagerPanel(
56     wxWindow* parent,
57     modelCDMProject* project,
58     wxWindowID id,
59     const wxString& caption,
60     const wxPoint& pos,
61     const wxSize& size,
62     long style
63 )
64 {
65   wxCDMPackageManagerPanel::Create(parent, project, id, caption, pos, size, style);
66 }
67
68 wxCDMPackageManagerPanel::~wxCDMPackageManagerPanel()
69 {
70 }
71
72 bool wxCDMPackageManagerPanel::Create(
73     wxWindow* parent,
74     modelCDMProject* project,
75     wxWindowID id,
76     const wxString& caption,
77     const wxPoint& pos,
78     const wxSize& size,
79     long style
80 )
81 {
82   wxPanel::Create(parent, id, pos, size, style);
83   // this part makes the scrollbars show up
84   this->FitInside(); // ask the sizer about the needed size
85   this->SetScrollRate(5, 5);
86
87   this->project = project;
88   CreateControls();
89   return TRUE;
90 }
91
92 void wxCDMPackageManagerPanel::CreateControls()
93 {
94   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
95
96   //Link to return
97   wxHyperlinkCtrl* returnLnk = new wxHyperlinkCtrl(this, ID_BUTTON_PREV, crea::std2wx(this->project->GetName()), crea::std2wx(this->project->GetPath()), wxDefaultPosition, wxDefaultSize, wxHL_DEFAULT_STYLE);
98   returnLnk->SetWindowStyle(wxNO_BORDER);
99   returnLnk->SetToolTip(wxT("Return to the active project description."));
100   sizer->Add(returnLnk, 0, wxALIGN_CENTER | wxALL, 5);
101
102   //Header
103   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
104   {
105     //Image
106     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(PkIcon64)),0, wxALIGN_CENTER, 0);
107     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
108     //Title
109     textSizer->Add(new wxStaticText(this, -1, _("Package Management")),0, wxALIGN_LEFT, 0);
110
111     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
112   }
113   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
114
115   //Packages
116   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("A&vailable Packages"));
117   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."));
118   wxPanel* propertiesPanel = new wxPanel(this);
119
120   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
121   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(packages.size()+1, 3, 9, 5);
122
123   wxStaticText* ChBTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Include in\nCMake"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
124   wxStaticText* LkTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Package Name"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
125   wxStaticText* HlpTitle = new wxStaticText(propertiesPanel, wxID_ANY, wxT("Help"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE_HORIZONTAL);
126
127   propertiesGridSizer -> Add(ChBTitle, 0, wxEXPAND | wxALL, 5);
128   propertiesGridSizer -> Add(LkTitle,  0, wxEXPAND | wxALL, 5);
129   propertiesGridSizer -> Add(HlpTitle, 0, wxEXPAND | wxALL, 5);
130
131   for (int i = 0; i < (int)(packages.size()); i++)
132     {
133       //checkbox for cmake inclusion
134       wxCheckBox* pPackageChB = new wxCheckBox(propertiesPanel, ID_CHECK_INCLUDE_PACKAGE, wxT(""), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
135       pPackageChB->SetName(crea::std2wx(packages[i]->GetName()));
136       std::string tt = "if this box is checked the the " + packages[i]->GetName() + " package is included in the project compilation.";
137       pPackageChB->SetToolTip(crea::std2wx(tt));
138       pPackageChB->SetValue(this->project->IsPackageIncluded(packages[i]->GetName()));
139       propertiesGridSizer -> Add(pPackageChB, 0, wxEXPAND | wxALIGN_CENTER);
140
141       //link to package with description
142       wxHyperlinkCtrl* pPackagelk = new wxHyperlinkCtrl(propertiesPanel, ID_LINK_SELECT_PACKAGE, crea::std2wx(packages[i]->GetName().c_str()), crea::std2wx(packages[i]->GetName().c_str()), wxDefaultPosition, wxDefaultSize, wxHL_DEFAULT_STYLE);
143       pPackagelk->SetWindowStyle(wxALIGN_LEFT | wxNO_BORDER);
144       tt = "Name: " + packages[i]->GetName() + "\n";
145       tt += "Location: " + packages[i]->GetPath();
146       pPackagelk->SetToolTip(crea::std2wx(tt));
147       pPackagelk->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnMouseEnter,NULL,this);
148       pPackagelk->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnMouseExit,NULL,this);
149       propertiesGridSizer -> Add(pPackagelk, 0, wxEXPAND);
150
151       //help icon
152       wxButton* pPackageHlp = new wxButton(propertiesPanel, wxID_ANY, wxT("?"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
153       propertiesGridSizer -> Add(pPackageHlp, 0, wxEXPAND | wxALIGN_CENTER);
154     }
155
156   propertiesGridSizer->AddGrowableCol(1,1);
157
158   propertiesPanel->SetSizer(propertiesGridSizer);
159   propertiesGridSizer->Fit(propertiesPanel);
160
161
162   propertiesBox->Add(propertiesPanel, 1, wxALL | wxEXPAND, 5);
163
164   sizer -> Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
165
166   //Actions
167   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
168   wxPanel* actionsPanel = new wxPanel(this);
169   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
170
171   //actionsGrid Sizer
172   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(1, 2, 9, 15);
173
174   wxButton* createPkgbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_PACKAGE, _T("A. Create Package"));
175   createPkgbt->SetToolTip(wxT("Create a new package for this project."));
176   actionsGridSizer->Add(createPkgbt, 1, wxALL | wxEXPAND, 5);
177   wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("Edit CMakeLists File"));
178   editCMakebt->SetToolTip(wxT("Edit the CMakeLists.txt file of this project."));
179   editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnCMakeMouseEnter,NULL,this);
180   editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMPackageManagerPanel::OnCMakeMouseExit,NULL,this);
181   actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
182
183   actionsGridSizer->AddGrowableCol(0,1);
184   actionsGridSizer->AddGrowableCol(1,1);
185
186   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
187   actionsPanel->SetSizer(actionsPanelSizer);
188   actionsPanelSizer->Fit(actionsPanel);
189   actionsBox->Add(actionsPanel, 1, wxEXPAND);
190   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
191
192   //Assign sizer
193   SetSizer(sizer);
194   sizer->SetSizeHints(this);
195
196   if (((wxCDMMainFrame*)this->GetParent())->isHelp())
197     {
198       wxCDMPackageManagerHelpDialog* helpDialog = new wxCDMPackageManagerHelpDialog(this->GetParent(), this, wxID_ANY);
199       helpDialog->Show(true);
200     }
201 }
202
203 modelCDMProject* wxCDMPackageManagerPanel::GetProject() const
204 {
205   return this->project;
206 }
207
208 void wxCDMPackageManagerPanel::OnBtnReturn(wxHyperlinkEvent& event)
209 {
210   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
211   newEvent->SetClientData(project);
212   newEvent->SetId(0);
213   wxPostEvent(this->GetParent(), *newEvent);
214 }
215
216 void wxCDMPackageManagerPanel::OnChBPackageChange(wxCommandEvent& event)
217 {
218   this->project->SetPackageInclude(
219       crea::wx2std(((wxCheckBox*)event.GetEventObject())->GetName()),
220       ((wxCheckBox*)event.GetEventObject())->GetValue()
221     );
222 }
223
224 void wxCDMPackageManagerPanel::OnLnkPackageSelect(wxHyperlinkEvent& event)
225 {
226   modelCDMPackage* thePackage = NULL;
227   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
228   for (int i = 0; i < (int)(packages.size()); i++)
229     {
230       if(packages[i]->GetName() == crea::wx2std(event.GetURL()))
231         {
232           thePackage = packages[i];
233           break;
234         }
235     }
236
237   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
238   newEvent->SetClientData(thePackage);
239   newEvent->SetId(0);
240   wxPostEvent(this->GetParent(), *newEvent);
241
242   wxCommandEvent* newEvent1 = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
243   newEvent1->SetClientData(thePackage);
244   newEvent1->SetId(0);
245   wxPostEvent(this->GetParent(), *newEvent1);
246
247 }
248
249 void wxCDMPackageManagerPanel::OnBtnCreatePackage(wxCommandEvent& event)
250 {
251   std::string* result;
252
253   wxCDMNewPackageDialog* dialog = new wxCDMNewPackageDialog(this);
254   long userResponse;
255   userResponse = dialog->ShowModal();
256
257   if(userResponse == wxID_FORWARD)
258     {
259       modelCDMIProjectTreeNode* package = this->project->CreatePackage(
260           crea::wx2std(dialog->GetPackageName()),
261           result,
262           crea::wx2std(dialog->GetPackageAuthor()),
263           crea::wx2std(dialog->GetPackageAuthorEmail()),
264           crea::wx2std(dialog->GetPackageDescription())
265       );
266       if(package == NULL)
267         {
268           std::cout << "error creating package: " << *result << std::endl;
269           wxMessageBox(crea::std2wx(*result),_T("New Package - Error!"),wxOK | wxICON_ERROR);
270           event.Skip();
271           return;
272         }
273       wxMessageBox(crea::std2wx("Package successfully created."),_T("New Package - Success!"),wxOK | wxICON_INFORMATION);
274
275       //refreshing tree and description
276       //send event instead of calling parent to avoid crashing
277
278       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
279
280       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
281       newEvent->SetClientData(package);
282       newEvent->SetId(0);
283       wxPostEvent(this->GetParent(), *newEvent);
284       event.Skip();
285     }
286
287   event.Skip();
288 }
289
290 void wxCDMPackageManagerPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
291 {
292   std::string* result;
293   if(!this->project->OpenCMakeListsFile(result))
294     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
295
296   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
297
298   if(this->project->GetCMakeLists() != NULL)
299     {
300       newEvent->SetClientData(this->project->GetCMakeLists());
301       newEvent->SetId(0);
302       wxPostEvent(this->GetParent(), *newEvent);
303     }
304 }
305
306 void wxCDMPackageManagerPanel::OnMouseEnter(wxMouseEvent& event)
307 {
308   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
309   std::string PkgName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
310   modelCDMPackage* thePackage = NULL;
311   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
312   for (int i = 0; i < (int)(packages.size()); i++)
313     {
314       if(packages[i]->GetName() == PkgName)
315         {
316           thePackage = packages[i];
317           break;
318         }
319     }
320   newEvent->SetClientData(thePackage);
321   newEvent->SetId(0);
322   wxPostEvent(this->GetParent(), *newEvent);
323   event.Skip();
324 }
325
326 void wxCDMPackageManagerPanel::OnMouseExit(wxMouseEvent& event)
327 {
328   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
329   std::string PkgName = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
330   modelCDMPackage* thePackage = NULL;
331   std::vector<modelCDMPackage*> packages = this->project->GetPackages();
332   project->GetPackages();
333   for (int i = 0; i < (int)(packages.size()); i++)
334     {
335       if(packages[i]->GetName() == PkgName)
336         {
337           thePackage = packages[i];
338           break;
339         }
340     }
341   newEvent->SetClientData(thePackage);
342   newEvent->SetId(0);
343   wxPostEvent(this->GetParent(), *newEvent);
344   event.Skip();
345 }
346
347 void wxCDMPackageManagerPanel::OnCMakeMouseEnter(wxMouseEvent& event)
348 {
349   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
350
351   if(this->project->GetCMakeLists() != NULL)
352     {
353       newEvent->SetClientData(this->project->GetCMakeLists());
354       newEvent->SetId(0);
355       wxPostEvent(this->GetParent(), *newEvent);
356     }
357   event.Skip();
358 }
359
360 void wxCDMPackageManagerPanel::OnCMakeMouseExit(wxMouseEvent& event)
361 {
362   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
363
364   if(this->project->GetCMakeLists() != NULL)
365     {
366       newEvent->SetClientData(this->project->GetCMakeLists());
367       newEvent->SetId(0);
368       wxPostEvent(this->GetParent(), *newEvent);
369     }
370   event.Skip();
371 }
372
373