]> Creatis software - crea.git/blob - lib/creaDevManagerLib/wxCDMCodeFileDescriptionPanel.cpp
#3136 creaFeature New Normal - branch vtk7itk4wx3
[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
84 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
85 #if wxMAJOR_VERSION <= 2
86   this->SetScrollRate(5, 5);
87 #else
88         printf ("EED wxCDMPackageDescriptionPanel::Create    Change 05\n");
89   //...
90 #endif
91
92   return TRUE;
93 }
94
95 void wxCDMCodeFileDescriptionPanel::CreateControls()
96 {
97   wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
98
99   //Links to return
100   wxBoxSizer *linksSizer = new wxBoxSizer(wxHORIZONTAL);
101   std::vector<modelCDMIProjectTreeNode*> parents = this->codeFile->GetParents();
102   for (int i = 0; i < (int)(parents.size()); i++)
103     {
104       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);
105       returnLnk->SetWindowStyle(wxNO_BORDER);
106       returnLnk->SetToolTip(crea::std2wx("Return to " + parents[parents.size()-1-i]->GetName() + "."));
107       linksSizer->Add(returnLnk, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 5);
108       linksSizer->Add(new wxStaticText(this,wxID_ANY, wxT("/")), 0, wxALIGN_CENTER, 0);
109     }
110
111   linksSizer->Add(new wxStaticText(this, wxID_ANY, crea::std2wx(this->codeFile->GetName())), 0, wxALIGN_CENTER, 0);
112
113   sizer->Add(linksSizer, 0, wxALIGN_CENTER | wxALL, 5);
114
115   //Header
116   wxBoxSizer* headerSizer = new wxBoxSizer(wxHORIZONTAL);
117   {
118     //Image
119     headerSizer->Add(new wxStaticBitmap(this, -1, wxBitmap(CFIcon64)),0, wxALIGN_CENTER, 0);
120     wxBoxSizer* textSizer = new wxBoxSizer(wxVERTICAL);
121     //Title
122     textSizer->Add(new wxStaticText(this, -1, _("Code File")),0, wxALIGN_LEFT, 0);
123     //File Name
124     textSizer->Add(new wxStaticText(this, -1, crea::std2wx(this->codeFile->GetName())),0, wxALIGN_LEFT, 0);
125     headerSizer->Add(textSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
126   }
127   sizer->Add(headerSizer, 0, wxALIGN_CENTER);
128
129   //File Properties
130   wxStaticBoxSizer* propertiesBox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Properties"));
131   wxPanel* propertiesPanel = new wxPanel(this);
132   wxBoxSizer* propertiesPanelSizer = new wxBoxSizer(wxVERTICAL);
133   wxFlexGridSizer* propertiesGridSizer = new wxFlexGridSizer(4, 2, 9, 15);
134
135   wxStaticText *pLocation = new wxStaticText(propertiesPanel, -1, wxT("Location"));
136   wxStaticText *pLength = new wxStaticText(propertiesPanel, -1, wxT("File Size"));
137
138   wxStaticText* pLocationtc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(this->codeFile->GetPath()));
139   pLocationtc->SetMaxSize(wxSize(300,-1));
140   int lgth = this->codeFile->GetLength();
141   std::stringstream ss;
142   ss << lgth / 1024;
143   std::string lgths = ss.str() + " KB";
144   wxStaticText* pLengthtc = new wxStaticText(propertiesPanel, wxID_ANY, crea::std2wx(lgths));
145
146   propertiesGridSizer->Add(pLocation, 0, wxALIGN_RIGHT | wxALIGN_TOP);
147   propertiesGridSizer->Add(pLocationtc, 1, wxEXPAND);
148   propertiesGridSizer->Add(pLength, 0, wxALIGN_RIGHT | wxALIGN_TOP);
149   propertiesGridSizer->Add(pLengthtc, 1, wxEXPAND);
150
151   propertiesGridSizer->AddGrowableCol(1,1);
152
153   propertiesPanelSizer -> Add(propertiesGridSizer, 1, wxEXPAND);
154   propertiesPanel->SetSizer(propertiesPanelSizer);
155   propertiesPanelSizer->Fit(propertiesPanel);
156   propertiesBox->Add(propertiesPanel, 1, wxALL|wxEXPAND, 5);
157
158   sizer->Add(propertiesBox, 0, wxEXPAND | wxALL, 10);
159
160
161   //Actions
162   wxStaticBoxSizer* actionsBox = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("&Actions"));
163   wxPanel* actionsPanel = new wxPanel(this);
164   wxBoxSizer* actionsPanelSizer = new wxBoxSizer(wxHORIZONTAL);
165   //actionsGrid Sizer
166   wxFlexGridSizer* actionsGridSizer = new wxFlexGridSizer(2, 2, 9, 15);
167
168   wxButton* editfilebt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FILE, _T("Open File"));
169   editfilebt->SetToolTip(wxT("Open the code file in the default text editor."));
170   actionsGridSizer->Add(editfilebt, 1, wxALL | wxEXPAND, 5);
171   wxButton* openFolderbt = new wxButton(actionsPanel, ID_BUTTON_OPEN_FOLDER, _T("Open Containing Folder"));
172   openFolderbt->SetToolTip(wxT("Open the folder where the code file is located in the file explorer."));
173   actionsGridSizer->Add(openFolderbt, 1, wxALL | wxEXPAND, 5);
174   wxButton* openfilebt = new wxButton(actionsPanel, ID_BUTTON_OPEN_COMMAND, _T("Open With Command"));
175   openfilebt->SetToolTip(wxT("Open the code file with a specific command."));
176   actionsGridSizer->Add(openfilebt, 1, wxALL | wxEXPAND, 5);
177
178   actionsGridSizer->AddGrowableCol(0,1);
179   actionsGridSizer->AddGrowableCol(1,1);
180
181   actionsPanelSizer->Add(actionsGridSizer, 1, wxEXPAND, 0);
182   actionsPanel->SetSizer(actionsPanelSizer);
183   actionsPanelSizer->Fit(actionsPanel);
184   actionsBox->Add(actionsPanel, 1, wxEXPAND);
185   sizer -> Add(actionsBox, 0, wxEXPAND | wxALL, 10);
186
187   //Assign sizer
188   SetSizer(sizer);
189   sizer->SetSizeHints(this);
190 }
191
192 void wxCDMCodeFileDescriptionPanel::OnBtnReturn(wxHyperlinkEvent& event)
193 {
194   std::vector<modelCDMIProjectTreeNode*> parents = this->codeFile->GetParents();
195   std::string parentURL = crea::wx2std(((wxHyperlinkCtrl*)event.GetEventObject())->GetURL());
196   //std::cout << parentURL << std::endl;
197   for (int i = 0; i < (int)(parents.size()); i++)
198     {
199       if (parents[i]->GetPath() == parentURL)
200         {
201           wxCommandEvent* newEvent = new wxCommandEvent(wxEVT_DISPLAY_CHANGED);
202           newEvent->SetClientData(parents[i]);
203           newEvent->SetId(0);
204           wxPostEvent(this->GetParent(), *newEvent);
205         }
206     }
207 }
208
209 void wxCDMCodeFileDescriptionPanel::OnBtnOpenInEditor(wxCommandEvent& event)
210 {
211   std::string* result;
212   if(!this->codeFile->OpenFile(result))
213     wxMessageBox(crea::std2wx(*result),_T("Open Code File - Error!"),wxOK | wxICON_ERROR);
214 }
215
216 void wxCDMCodeFileDescriptionPanel::OnBtnOpenWithCommand(wxCommandEvent& event)
217 {
218   //get command
219   wxString commandEx = wxGetTextFromUser(
220       _T("Enter the command to execute file"),
221       _T("Execute File - creaDevManager"),
222       _T("")
223   );
224   //check name
225   if(commandEx.Len() > 0)
226     {
227       std::string* result;
228       if(!((modelCDMFile*)this->codeFile)->OpenFile(result, crea::wx2std(commandEx)))
229         wxMessageBox(crea::std2wx(*result),_T("Execute Code File - Error!"),wxOK | wxICON_ERROR);
230     }
231 }
232
233 void wxCDMCodeFileDescriptionPanel::OnBtnOpenFolder(wxCommandEvent& event)
234 {
235   std::string* result;
236   if(!this->codeFile->OpenInFileExplorer(result))
237     wxMessageBox(crea::std2wx(*result),_T("Open File in Folder - Error!"),wxOK | wxICON_ERROR);
238 }