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