]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMCodeFileDescriptionPanel.cpp
Feature #1711 CreaDevManager application implementation
[crea.git] / lib / creaDevManagerLib / wxCDMCodeFileDescriptionPanel.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  * wxCDMCodeFileDescriptionPanel.cpp
30  *
31  *  Created on: Jun 24, 2013
32  *      Author: Daniel Felipe Gonzalez Obando
33  */
34
35 #include "wxCDMCodeFileDescriptionPanel.h"
36
37 #include<sstream>
38
39 #include "wxCDMMainFrame.h"
40
41 #include "creaDevManagerIds.h"
42 #include "images/CFIcon64.xpm"
43
44 BEGIN_EVENT_TABLE(wxCDMCodeFileDescriptionPanel, wxPanel)
45 EVT_HYPERLINK(ID_BUTTON_PREV, wxCDMCodeFileDescriptionPanel::OnBtnReturn)
46 EVT_BUTTON(ID_BUTTON_OPEN_FILE, wxCDMCodeFileDescriptionPanel::OnBtnOpenInEditor)
47 EVT_BUTTON(ID_BUTTON_OPEN_COMMAND, wxCDMCodeFileDescriptionPanel::OnBtnOpenWithCommand)
48 EVT_BUTTON(ID_BUTTON_OPEN_FOLDER, wxCDMCodeFileDescriptionPanel::OnBtnOpenFolder)
49 END_EVENT_TABLE()
50
51 wxCDMCodeFileDescriptionPanel::wxCDMCodeFileDescriptionPanel(
52     wxWindow* parent,
53     modelCDMCodeFile* codefile,
54     wxWindowID id,
55     const wxString& caption,
56     const wxPoint& pos,
57     const wxSize& size,
58     long style
59 )
60 {
61   wxCDMCodeFileDescriptionPanel::Create(parent, codefile, id, caption, pos, size, style);
62 }
63
64 wxCDMCodeFileDescriptionPanel::~wxCDMCodeFileDescriptionPanel()
65 {
66 }
67
68 bool wxCDMCodeFileDescriptionPanel::Create(
69     wxWindow* parent,
70     modelCDMCodeFile* codefile,
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->codeFile = codefile;
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 wxCDMCodeFileDescriptionPanel::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->codeFile->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->codeFile->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(CFIcon64)),0, wxALIGN_CENTER, 0);
112     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
113     //Title
114     textSizer->Add(new wxStaticText(this, -1, _("Code File")),0, wxALIGN_LEFT, 0);
115     //File Name
116     textSizer->Add(new wxStaticText(this, -1, crea::std2wx(this->codeFile->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   //File Properties
122   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Properties"));
123   wxPanel* propertiesPanel = new wxPanel(this);
124   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
125   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(4, 2, 9, 15);
126
127   wxStaticText *pLocation = new wxStaticText(propertiesPanel, -1, wxT("Location"));
128   wxStaticText *pLength = new wxStaticText(propertiesPanel, -1, wxT("File Size"));
129
130   wxStaticText* pLocationtc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(this->codeFile->GetPath()));
131   pLocationtc->SetMaxSize(wxSize(300,-1));
132   int lgth = this->codeFile->GetLength();
133   std::stringstream ss;
134   ss << lgth / 1024;
135   std::string lgths = ss.str() + " KB";
136   wxStaticText* pLengthtc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(lgths));
137
138   propertiesGridSizer->Add(pLocation, 0, wxALIGN_RIGHT | wxALIGN_TOP);
139   propertiesGridSizer->Add(pLocationtc, 1, wxEXPAND);
140   propertiesGridSizer->Add(pLength, 0, wxALIGN_RIGHT | wxALIGN_TOP);
141   propertiesGridSizer->Add(pLengthtc, 1, wxEXPAND);
142
143   propertiesGridSizer->AddGrowableCol(1,1);
144
145   propertiesPanelSizer -> Add(propertiesGridSizer, 1, wxEXPAND);
146   propertiesPanel->SetSizer(propertiesPanelSizer);
147   propertiesPanelSizer->Fit(propertiesPanel);
148   propertiesBox->Add(propertiesPanel, 1, wxALL|wxEXPAND, 5);
149
150   sizer->Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
151
152
153   //Actions
154   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
155   wxPanel* actionsPanel = new wxPanel(this);
156   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
157   //actionsGrid Sizer
158   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
159
160   wxButton* editfilebt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FILE, _T("Open File"));
161   editfilebt->SetToolTip(wxT("Open the code file in the default text editor."));
162   actionsGridSizer->Add(editfilebt, 1, wxALL | wxEXPAND, 5);
163   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("Open Containing Folder"));
164   openFolderbt->SetToolTip(wxT("Open the folder where the code file is located in the file explorer."));
165   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
166   wxButton* openfilebt = new wxButton(actionsPanel, ID_BUTTON_OPEN_COMMAND, _T("Open With Command"));
167   openfilebt->SetToolTip(wxT("Open the code file with a specific command."));
168   actionsGridSizer->Add(openfilebt, 1, wxALL | wxEXPAND, 5);
169
170   actionsGridSizer->AddGrowableCol(0,1);
171   actionsGridSizer->AddGrowableCol(1,1);
172
173   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
174   actionsPanel->SetSizer(actionsPanelSizer);
175   actionsPanelSizer->Fit(actionsPanel);
176   actionsBox->Add(actionsPanel, 1, wxEXPAND);
177   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
178
179   //Assign sizer
180   SetSizer(sizer);
181   sizer->SetSizeHints(this);
182 }
183
184 void wxCDMCodeFileDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
185 {
186   std::vector<modelCDMIProjectTreeNode*> parents = this->codeFile->GetParents();
187   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
188   //std::cout << parentURL << std::endl;
189   for (int i = 0; i < (int)(parents.size()); i++)
190     {
191       if (parents[i]->GetPath() == parentURL)
192         {
193           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
194           newEvent->SetClientData(parents[i]);
195           newEvent->SetId(0);
196           wxPostEvent(this->GetParent(), *newEvent);
197         }
198     }
199 }
200
201 void wxCDMCodeFileDescriptionPanel::OnBtnOpenInEditor(wxCommandEvent& event)
202 {
203   std::string* result;
204   if(!this->codeFile->OpenFile(result))
205     wxMessageBox(crea::std2wx(*result),_T("Open Code File - Error!"),wxOK | wxICON_ERROR);
206 }
207
208 void wxCDMCodeFileDescriptionPanel::OnBtnOpenWithCommand(wxCommandEvent& event)
209 {
210   //get command
211   wxString commandEx = wxGetTextFromUser(
212       _T("Enter the command to execute file"),
213       _T("Execute File - creaDevManager"),
214       _T("")
215   );
216   //check name
217   if(commandEx.Len() > 0)
218     {
219       std::string* result;
220       if(!((modelCDMFile*)this->codeFile)->OpenFile(result, crea::wx2std(commandEx)))
221         wxMessageBox(crea::std2wx(*result),_T("Execute Code File - Error!"),wxOK | wxICON_ERROR);
222     }
223 }
224
225 void wxCDMCodeFileDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
226 {
227   std::string* result;
228   if(!this->codeFile->OpenInFileExplorer(result))
229     wxMessageBox(crea::std2wx(*result),_T("Open File in Folder - Error!"),wxOK | wxICON_ERROR);
230 }