]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMFolderDescriptionPanel.cpp
7f95533059e6dc0f3d4d49cb191b552851b85d8d
[crea.git] / lib / creaDevManagerLib / wxCDMFolderDescriptionPanel.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  * wxCDMFolderDescriptionPanel.cpp
30  *
31  *  Created on: Nov 27, 2012
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "wxCDMFolderDescriptionPanel.h"
36
37 #include "wxCDMMainFrame.h"
38 #include "CDMUtilities.h"
39
40 #include "creaDevManagerIds.h"
41 #include "images/FdIcon64.xpm"
42
43 BEGIN_EVENT_TABLE(wxCDMFolderDescriptionPanel, wxPanel)
44 EVT_HYPERLINK(ID_BUTTON_PREV, wxCDMFolderDescriptionPanel::OnBtnReturn)
45 EVT_BUTTON(ID_BUTTON_OPEN_FOLDER, wxCDMFolderDescriptionPanel::OnBtnOpenInExplorer)
46 EVT_BUTTON(ID_BUTTON_EDIT_CMAKELISTSFILE, wxCDMFolderDescriptionPanel::OnBtnEditCMakeLists)
47 EVT_BUTTON(ID_BUTTON_CREATE_CLASS, wxCDMFolderDescriptionPanel::OnBtnCreateClass)
48 EVT_BUTTON(ID_BUTTON_CREATE_FOLDER, wxCDMFolderDescriptionPanel::OnBtnCreateFolder)
49 END_EVENT_TABLE()
50
51 wxCDMFolderDescriptionPanel::wxCDMFolderDescriptionPanel(
52     wxWindow* parent,
53     modelCDMFolder* folder,
54     wxWindowID id,
55     const wxString& caption,
56     const wxPoint& pos,
57     const wxSize& size,
58     long style
59 )
60 {
61   wxCDMFolderDescriptionPanel::Create(parent, folder, id, caption, pos, size, style);
62 }
63
64 wxCDMFolderDescriptionPanel::~wxCDMFolderDescriptionPanel()
65 {
66 }
67
68 bool wxCDMFolderDescriptionPanel::Create(
69     wxWindow* parent,
70     modelCDMFolder* folder,
71     wxWindowID id,
72     const wxString& caption,
73     const wxPoint& pos,
74     const wxSize& size,
75     long style
76 )
77 {
78   wxPanel::Create(parent, id, pos, size, style);
79   // this part makes the scrollbars show up
80   this->FitInside(); // ask the sizer about the needed size
81   this->SetScrollRate(5, 5);
82
83   this->folder = folder;
84   CreateControls();
85   return TRUE;
86 }
87
88 void wxCDMFolderDescriptionPanel::CreateControls()
89 {
90   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
91
92   //Links to return
93   wxBoxSizer *linksSizer = new wxBoxSizer(wxHORIZONTAL);
94   std::vector<modelCDMIProjectTreeNode*> parents = this->folder->GetParents();
95   for (int i = 0; i < (int)(parents.size()); i++)
96     {
97       wxHyperlinkCtrl* returnLnk = new wxHyperlinkCtrl(this, ID_BUTTON_PREV, crea::std2wx(parents[parents.size()-1-i]->GetName()), crea::std2wx(parents[parents.size()-1-i]->GetPath()), wxDefaultPosition, wxDefaultSize, wxHL_DEFAULT_STYLE);
98       returnLnk->SetWindowStyle(wxNO_BORDER);
99       returnLnk->SetToolTip(crea::std2wx("Return to " + parents[parents.size()-1-i]->GetName() + "."));
100       linksSizer->Add(returnLnk, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 5);
101       linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
102     }
103
104   linksSizer->Add(new wxStaticText(this, wxID_ANY, crea::std2wx(this->folder->GetName())), 0, wxALIGN_CENTER, 0);
105
106   sizer->Add(linksSizer, 0, wxALIGN_CENTER | wxALL, 5);
107
108   //Header
109   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
110   {
111     //Image
112     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(FdIcon64)),0, wxALIGN_CENTER, 0);
113     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
114     //Title
115     textSizer->Add(new wxStaticText(this, -1, _("Folder")),0, wxALIGN_LEFT, 0);
116     //Folder Name
117     textSizer->Add(new wxStaticText(this, -1, crea::std2wx(this->folder->GetName())),0, wxALIGN_LEFT, 0);
118     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
119   }
120   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
121
122   //Actions
123   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
124   wxPanel* actionsPanel = new wxPanel(this);
125   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
126   //actionsGrid Sizer
127   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
128
129   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("Open in File Explorer"));
130   openFolderbt->SetToolTip(wxT("Open this folder in the file explorer."));
131   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
132
133   if(this->folder->HasCMakeLists())
134     {
135       wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("Edit CMakeLists File"));
136       editCMakebt->SetToolTip(wxT("Open the CMakeLists.txt file in this folder with the default text editor."));
137       editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMFolderDescriptionPanel::OnCMakeMouseEnter,NULL,this);
138       editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMFolderDescriptionPanel::OnCMakeMouseExit,NULL,this);
139       actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
140     }
141
142   wxButton* createClassbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_CLASS, _T("Create Class"));
143   createClassbt->SetToolTip(wxT("Create a new class (.h and .cpp) inside this folder."));
144   actionsGridSizer->Add(createClassbt, 1, wxALL | wxEXPAND, 5);
145
146   wxButton* createFolderbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_FOLDER, _T("Create Folder"));
147   createFolderbt->SetToolTip(wxT("Create a new folder inside this folder."));
148   actionsGridSizer->Add(createFolderbt, 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
164 void wxCDMFolderDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
165 {
166   std::vector<modelCDMIProjectTreeNode*> parents = this->folder->GetParents();
167   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
168   //std::cout << parentURL << std::endl;
169   for (int i = 0; i < (int)(parents.size()); i++)
170     {
171       if (parents[i]->GetPath() == parentURL)
172         {
173           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
174           newEvent->SetClientData(parents[i]);
175           newEvent->SetId(0);
176           wxPostEvent(this->GetParent(), *newEvent);
177         }
178     }
179 }
180
181 void wxCDMFolderDescriptionPanel::OnBtnOpenInExplorer(wxCommandEvent& event)
182 {
183   std::string* result;
184   if(!this->folder->OpenInFileExplorer(result))
185     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
186 }
187
188 void wxCDMFolderDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
189 {
190   std::string* result;
191   if(!this->folder->OpenCMakeListsFile(result))
192     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
193
194   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
195
196   if(this->folder->GetCMakeLists() != NULL)
197     {
198       newEvent->SetClientData(this->folder->GetCMakeLists());
199       newEvent->SetId(0);
200       wxPostEvent(this->GetParent(), *newEvent);
201     }
202 }
203
204 void wxCDMFolderDescriptionPanel::OnBtnCreateClass(wxCommandEvent& event)
205 {
206   //get class name from user
207   wxTextEntryDialog* newClassDlg = new wxTextEntryDialog(
208       this,
209       wxT("Please enter the new class name."),
210       wxT("New Class - creaDevManager"),
211       wxT(""),
212       wxOK | wxCANCEL
213   );
214
215   if (newClassDlg->ShowModal() == wxID_OK)
216     {
217       std::string className = crea::wx2std(newClassDlg->GetValue());
218       //check class name
219       if(className.size() > 0)
220         {
221           if(!this->folder->CreateClass(className))
222             wxMessageBox(crea::std2wx("Something has gone wrong with the creation of the class."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
223
224           ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
225
226           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
227           newEvent->SetId(0);
228           newEvent->SetClientData(folder);
229           wxPostEvent(this->GetParent(), *newEvent);
230
231           wxMessageBox(crea::std2wx("The class has been created successfully."),_T("New Class - Success"),wxOK | wxICON_INFORMATION);
232         }
233       else
234         {
235           wxMessageBox(crea::std2wx("The new class name cannot be empty."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
236         }
237     }
238 }
239
240 void wxCDMFolderDescriptionPanel::OnBtnCreateFolder(wxCommandEvent& event)
241 {
242   //get name
243   wxString folderName = wxGetTextFromUser(
244       wxT("Enter the name of the new folder:"),
245       wxT("Create Folder - creaDevManager")
246   );
247   //check name
248   std::vector<std::string> parts;
249   CDMUtilities::splitter::split(parts, crea::wx2std(folderName), " /\\\"", CDMUtilities::splitter::no_empties);
250   if(parts.size() > 0)
251     {
252       std::string* result;
253       modelCDMFolder* folderC = this->folder->CreateFolder(crea::wx2std(folderName), result);
254       if(folderC == NULL)
255         {
256           wxMessageBox(crea::std2wx(*result),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
257           return;
258         }
259       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
260       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
261       newEvent->SetClientData(folderC);
262       wxPostEvent(this->GetParent(), *newEvent);
263       wxMessageBox(crea::std2wx("The folder was successfully created"),_T("Create Folder - Success"),wxOK | wxICON_INFORMATION);
264     }
265   else
266     {
267       wxMessageBox(crea::std2wx("No name specified"),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
268     }
269 }
270
271 void wxCDMFolderDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
272 {
273   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
274
275   if(this->folder->GetCMakeLists() != NULL)
276     {
277       newEvent->SetClientData(this->folder->GetCMakeLists());
278       newEvent->SetId(0);
279       wxPostEvent(this->GetParent(), *newEvent);
280     }
281   event.Skip();
282 }
283
284 void wxCDMFolderDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
285 {
286   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
287
288   if(this->folder->GetCMakeLists() != NULL)
289     {
290       newEvent->SetClientData(this->folder->GetCMakeLists());
291       newEvent->SetId(0);
292       wxPostEvent(this->GetParent(), *newEvent);
293     }
294   event.Skip();
295 }