]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMFolderDescriptionPanel.cpp
Feature #1711 CreaDevManager application implementation
[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->folder = folder;
80   CreateControls();
81   // this part makes the scrollbars show up
82   this->FitInside(); // ask the sizer about the needed size
83   this->SetScrollRate(5, 5);
84   return TRUE;
85 }
86
87 void wxCDMFolderDescriptionPanel::CreateControls()
88 {
89   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
90
91   //Links to return
92   wxBoxSizer *linksSizer = new wxBoxSizer(wxHORIZONTAL);
93   std::vector<modelCDMIProjectTreeNode*> parents = this->folder->GetParents();
94   for (int i = 0; i < (int)(parents.size()); i++)
95     {
96       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);
97       returnLnk->SetWindowStyle(wxNO_BORDER);
98       returnLnk->SetToolTip(crea::std2wx("Return to " + parents[parents.size()-1-i]->GetName() + "."));
99       linksSizer->Add(returnLnk, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 5);
100       linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
101     }
102
103   linksSizer->Add(new wxStaticText(this, wxID_ANY, crea::std2wx(this->folder->GetName())), 0, wxALIGN_CENTER, 0);
104
105   sizer->Add(linksSizer, 0, wxALIGN_CENTER | wxALL, 5);
106
107   //Header
108   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
109   {
110     //Image
111     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(FdIcon64)),0, wxALIGN_CENTER, 0);
112     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
113     //Title
114     textSizer->Add(new wxStaticText(this, -1, _("Folder")),0, wxALIGN_LEFT, 0);
115     //Folder Name
116     textSizer->Add(new wxStaticText(this, -1, crea::std2wx(this->folder->GetName())),0, wxALIGN_LEFT, 0);
117     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
118   }
119   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
120
121   //Actions
122   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
123   wxPanel* actionsPanel = new wxPanel(this);
124   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
125   //actionsGrid Sizer
126   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
127
128   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("Open in File Explorer"));
129   openFolderbt->SetToolTip(wxT("Open this folder in the file explorer."));
130   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
131
132   if(this->folder->HasCMakeLists())
133     {
134       wxButton* editCMakebt = new wxButton(actionsPanel, ID_BUTTON_EDIT_CMAKELISTSFILE, _T("Edit CMakeLists File"));
135       editCMakebt->SetToolTip(wxT("Open the CMakeLists.txt file in this folder with the default text editor."));
136       editCMakebt->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMFolderDescriptionPanel::OnCMakeMouseEnter,NULL,this);
137       editCMakebt->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(wxEventFunction)(wxMouseEventFunction)&wxCDMFolderDescriptionPanel::OnCMakeMouseExit,NULL,this);
138       actionsGridSizer->Add(editCMakebt, 1, wxALL | wxEXPAND, 5);
139     }
140
141   wxButton* createClassbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_CLASS, _T("Create Class"));
142   createClassbt->SetToolTip(wxT("Create a new class (.h and .cpp) inside this folder."));
143   actionsGridSizer->Add(createClassbt, 1, wxALL | wxEXPAND, 5);
144
145   wxButton* createFolderbt = new wxButton(actionsPanel, ID_BUTTON_CREATE_FOLDER, _T("Create Folder"));
146   createFolderbt->SetToolTip(wxT("Create a new folder inside this folder."));
147   actionsGridSizer->Add(createFolderbt, 1, wxALL | wxEXPAND, 5);
148
149   actionsGridSizer->AddGrowableCol(0,1);
150   actionsGridSizer->AddGrowableCol(1,1);
151
152   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
153   actionsPanel->SetSizer(actionsPanelSizer);
154   actionsPanelSizer->Fit(actionsPanel);
155   actionsBox->Add(actionsPanel, 1, wxEXPAND);
156   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
157
158   //Assign sizer
159   SetSizer(sizer);
160   sizer->SetSizeHints(this);
161 }
162
163 void wxCDMFolderDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
164 {
165   std::vector<modelCDMIProjectTreeNode*> parents = this->folder->GetParents();
166   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
167   //std::cout << parentURL << std::endl;
168   for (int i = 0; i < (int)(parents.size()); i++)
169     {
170       if (parents[i]->GetPath() == parentURL)
171         {
172           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
173           newEvent->SetClientData(parents[i]);
174           newEvent->SetId(0);
175           wxPostEvent(this->GetParent(), *newEvent);
176         }
177     }
178 }
179
180 void wxCDMFolderDescriptionPanel::OnBtnOpenInExplorer(wxCommandEvent& event)
181 {
182   std::string* result;
183   if(!this->folder->OpenInFileExplorer(result))
184     wxMessageBox(crea::std2wx(*result),_T("Open Folder - Error!"),wxOK | wxICON_ERROR);
185 }
186
187 void wxCDMFolderDescriptionPanel::OnBtnEditCMakeLists(wxCommandEvent& event)
188 {
189   std::string* result;
190   if(!this->folder->OpenCMakeListsFile(result))
191     wxMessageBox(crea::std2wx(*result),_T("Open CMakeLists File - Error!"),wxOK | wxICON_ERROR);
192
193   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
194
195   if(this->folder->GetCMakeLists() != NULL)
196     {
197       newEvent->SetClientData(this->folder->GetCMakeLists());
198       newEvent->SetId(0);
199       wxPostEvent(this->GetParent(), *newEvent);
200     }
201 }
202
203 void wxCDMFolderDescriptionPanel::OnBtnCreateClass(wxCommandEvent& event)
204 {
205   //get class name from user
206   wxTextEntryDialog* newClassDlg = new wxTextEntryDialog(
207       this,
208       wxT("Please enter the new class name."),
209       wxT("New Class - creaDevManager"),
210       wxT(""),
211       wxOK | wxCANCEL
212   );
213
214   if (newClassDlg->ShowModal() == wxID_OK)
215     {
216       std::string className = crea::wx2std(newClassDlg->GetValue());
217       //check class name
218       if(className.size() > 0)
219         {
220           if(!this->folder->CreateClass(className))
221             wxMessageBox(crea::std2wx("Something has gone wrong with the creation of the class."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
222
223           ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
224
225           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
226           newEvent->SetId(0);
227           newEvent->SetClientData(folder);
228           wxPostEvent(this->GetParent(), *newEvent);
229
230           wxMessageBox(crea::std2wx("The class has been created successfully."),_T("New Class - Success"),wxOK | wxICON_INFORMATION);
231         }
232       else
233         {
234           wxMessageBox(crea::std2wx("The new class name cannot be empty."),_T("New Class - Error!"),wxOK | wxICON_ERROR);
235         }
236     }
237 }
238
239 void wxCDMFolderDescriptionPanel::OnBtnCreateFolder(wxCommandEvent& event)
240 {
241   //get name
242   wxString folderName = wxGetTextFromUser(
243       wxT("Enter the name of the new folder:"),
244       wxT("Create Folder - creaDevManager")
245   );
246   //check name
247   std::vector<std::string> parts;
248   CDMUtilities::splitter::split(parts, crea::wx2std(folderName), " /\\\"", CDMUtilities::splitter::no_empties);
249   if(parts.size() > 0)
250     {
251       std::string* result;
252       modelCDMFolder* folderC = this->folder->CreateFolder(crea::wx2std(folderName), result);
253       if(folderC == NULL)
254         {
255           wxMessageBox(crea::std2wx(*result),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
256           return;
257         }
258       ((wxCDMMainFrame*)this->GetParent())->RefreshProject();
259       wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
260       newEvent->SetClientData(folderC);
261       wxPostEvent(this->GetParent(), *newEvent);
262       wxMessageBox(crea::std2wx("The folder was successfully created"),_T("Create Folder - Success"),wxOK | wxICON_INFORMATION);
263     }
264   else
265     {
266       wxMessageBox(crea::std2wx("No name specified"),_T("Create Folder - Error!"),wxOK | wxICON_ERROR);
267     }
268 }
269
270 void wxCDMFolderDescriptionPanel::OnCMakeMouseEnter(wxMouseEvent& event)
271 {
272   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED);
273
274   if(this->folder->GetCMakeLists() != NULL)
275     {
276       newEvent->SetClientData(this->folder->GetCMakeLists());
277       newEvent->SetId(0);
278       wxPostEvent(this->GetParent(), *newEvent);
279     }
280   event.Skip();
281 }
282
283 void wxCDMFolderDescriptionPanel::OnCMakeMouseExit(wxMouseEvent& event)
284 {
285   wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_COMMAND_LISTBOX_SELECTED);
286
287   if(this->folder->GetCMakeLists() != NULL)
288     {
289       newEvent->SetClientData(this->folder->GetCMakeLists());
290       newEvent->SetId(0);
291       wxPostEvent(this->GetParent(), *newEvent);
292     }
293   event.Skip();
294 }