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