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