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