1
2 ///////////////////////////////////////////////////////////
3 // //
4 // SAGA //
5 // //
6 // System for Automated Geoscientific Analyses //
7 // //
8 // User Interface //
9 // //
10 // Program: SAGA //
11 // //
12 //-------------------------------------------------------//
13 // //
14 // WKSP_Tool.cpp //
15 // //
16 // Copyright (C) 2005 by Olaf Conrad //
17 // //
18 //-------------------------------------------------------//
19 // //
20 // This file is part of 'SAGA - System for Automated //
21 // Geoscientific Analyses'. SAGA is free software; you //
22 // can redistribute it and/or modify it under the terms //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the //
25 // License, or (at your option) any later version. //
26 // //
27 // SAGA is distributed in the hope that it will be //
28 // useful, but WITHOUT ANY WARRANTY; without even the //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A //
30 // PARTICULAR PURPOSE. See the GNU General Public //
31 // License for more details. //
32 // //
33 // You should have received a copy of the GNU General //
34 // Public License along with this program; if not, see //
35 // <http://www.gnu.org/licenses/>. //
36 // //
37 //-------------------------------------------------------//
38 // //
39 // contact: Olaf Conrad //
40 // Institute of Geography //
41 // University of Goettingen //
42 // Germany //
43 // //
44 // e-mail: oconrad@saga-gis.org //
45 // //
46 ///////////////////////////////////////////////////////////
47
48 //---------------------------------------------------------
49 #include <wx/utils.h>
50 #include <wx/filename.h>
51
52 #include "res_commands.h"
53 #include "res_dialogs.h"
54
55 #include "helper.h"
56
57 #include "active.h"
58 #include "active_parameters.h"
59
60 #include "wksp_data_manager.h"
61 #include "wksp_tool_manager.h"
62 #include "wksp_tool_library.h"
63 #include "wksp_tool.h"
64
65
66 ///////////////////////////////////////////////////////////
67 // //
68 // //
69 // //
70 ///////////////////////////////////////////////////////////
71
72 //---------------------------------------------------------
73 CWKSP_Tool *g_pTool = NULL;
74
75
76 ///////////////////////////////////////////////////////////
77 // //
78 // //
79 // //
80 ///////////////////////////////////////////////////////////
81
82 //---------------------------------------------------------
CWKSP_Tool(CSG_Tool * pTool,const wxString & Menu_Library)83 CWKSP_Tool::CWKSP_Tool(CSG_Tool *pTool, const wxString &Menu_Library)
84 {
85 m_pTool = pTool;
86 m_Menu_ID = -1;
87 m_bCloseGUI = false;
88 }
89
90 //---------------------------------------------------------
~CWKSP_Tool(void)91 CWKSP_Tool::~CWKSP_Tool(void)
92 {
93 if( g_pTool == this )
94 {
95 if( g_pTool->is_Executing() )
96 {
97 PROCESS_Set_Okay(false);
98 }
99
100 if( m_pTool->is_Interactive() )
101 {
102 ((CSG_Tool_Interactive *)m_pTool)->Execute_Finish();
103 }
104 }
105
106 SG_Get_Tool_Library_Manager().Delete_Tool(m_pTool);
107 }
108
109
110 ///////////////////////////////////////////////////////////
111 // //
112 ///////////////////////////////////////////////////////////
113
114 //---------------------------------------------------------
Get_Name(void)115 wxString CWKSP_Tool::Get_Name(void)
116 {
117 return( m_pTool->is_Interactive()
118 ? wxString::Format("%s [%s]", m_pTool->Get_Name().c_str(), _TL("interactive"))
119 : wxString::Format("%s" , m_pTool->Get_Name().c_str())
120 );
121 }
122
123 //---------------------------------------------------------
Get_Description(void)124 wxString CWKSP_Tool::Get_Description(void)
125 {
126 if( !m_pTool->Get_File_Name().is_Empty() )
127 {
128 CSG_String Lib_Name = SG_File_Get_Name(m_pTool->Get_File_Name(), false);
129 CSG_String File_Path = SG_File_Make_Path(SG_File_Get_Path(m_pTool->Get_File_Name()), Lib_Name, "");
130
131 File_Path = SG_File_Make_Path(File_Path, CSG_String::Format("%s_%02d", Lib_Name.c_str(), Get_Index()));
132
133 CSG_String Description;
134 CSG_File Stream;
135
136 if( SG_File_Set_Extension(File_Path, "html") && Stream.Open(File_Path, SG_FILE_R) && Stream.Read(Description, Stream.Length()) )
137 {
138 return( Description.c_str() );
139 }
140
141 if( SG_File_Set_Extension(File_Path, "htm" ) && Stream.Open(File_Path, SG_FILE_R) && Stream.Read(Description, Stream.Length()) )
142 {
143 return( Description.c_str() );
144 }
145 }
146
147 //-----------------------------------------------------
148 wxString Menu(m_pTool->Get_MenuPath(true).c_str()), Description;
149
150 if( g_pTools->Get_Parameter("HELP_SOURCE")->asInt() == 1 )
151 {
152 Description = Get_Online_Tool_Description(((CWKSP_Tool_Library *)Get_Manager())->Get_File_Name(), Get_Tool()->Get_ID().c_str());
153 }
154
155 return( m_pTool->Get_Summary(true, &Menu, &Description).c_str() );
156 }
157
158 //---------------------------------------------------------
Get_Menu(void)159 wxMenu * CWKSP_Tool::Get_Menu(void)
160 {
161 wxMenu *pMenu = new wxMenu(Get_Name());
162
163 pMenu->AppendCheckItem(Get_Menu_ID(), _TL("Execute"), _TL("Execute Tool"));
164
165 pMenu->AppendSeparator();
166 CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_SAVE_SCRIPT);
167 CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_SAVE_TO_CLIPBOARD);
168
169 if( m_pTool->Get_Type() == TOOL_TYPE_Chain )
170 {
171 pMenu->AppendSeparator();
172 CMD_Menu_Add_Item(pMenu, false, ID_CMD_WKSP_ITEM_CLOSE);
173 CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_CHAIN_RELOAD);
174 CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_CHAIN_EDIT);
175 }
176
177 pMenu->AppendSeparator();
178 CMD_Menu_Add_Item(pMenu, false, ID_CMD_TOOL_RELOAD);
179 pMenu->AppendSeparator();
180 CMD_Menu_Add_Item(pMenu, false, ID_CMD_WKSP_ITEM_SEARCH);
181
182 return( pMenu );
183 }
184
185 //---------------------------------------------------------
Get_Parameters(void)186 CSG_Parameters * CWKSP_Tool::Get_Parameters(void)
187 {
188 return( m_pTool->Get_Parameters() );
189 }
190
191
192 ///////////////////////////////////////////////////////////
193 // //
194 ///////////////////////////////////////////////////////////
195
196 //---------------------------------------------------------
On_Command(int Cmd_ID)197 bool CWKSP_Tool::On_Command(int Cmd_ID)
198 {
199 switch( Cmd_ID )
200 {
201 default:
202 return( CWKSP_Base_Item::On_Command(Cmd_ID) );
203
204 case ID_CMD_WKSP_ITEM_RETURN:
205 Execute(true);
206 break;
207
208 case ID_CMD_TOOL_SAVE_SCRIPT:
209 Save_to_Script();
210 break;
211
212 case ID_CMD_TOOL_SAVE_TO_CLIPBOARD:
213 Save_to_Clipboard();
214 break;
215
216 case ID_CMD_TOOL_CHAIN_RELOAD:
217 if( m_pTool->Get_Type() == TOOL_TYPE_Chain
218 && g_pTools->Open(m_pTool->Get_File_Name().c_str())
219 && g_pActive->Get_Active() == this )
220 {
221 g_pActive->Set_Active(NULL);
222 g_pActive->Set_Active(this);
223 }
224 break;
225
226 case ID_CMD_TOOL_CHAIN_EDIT:
227 if( m_pTool->Get_Type() == TOOL_TYPE_Chain )
228 {
229 Open_Application(m_pTool->Get_File_Name().c_str(), "txt");
230 }
231 break;
232 }
233
234 return( true );
235 }
236
237
238 ///////////////////////////////////////////////////////////
239 // //
240 ///////////////////////////////////////////////////////////
241
242 //---------------------------------------------------------
Set_Menu_ID(int Menu_ID)243 void CWKSP_Tool::Set_Menu_ID(int Menu_ID)
244 {
245 m_Menu_ID = Menu_ID;
246 }
247
248
249 ///////////////////////////////////////////////////////////
250 // //
251 ///////////////////////////////////////////////////////////
252
253 //---------------------------------------------------------
is_Interactive(void)254 bool CWKSP_Tool::is_Interactive(void)
255 {
256 return( m_pTool->is_Interactive() );
257 }
258
259 //---------------------------------------------------------
is_Executing(void)260 bool CWKSP_Tool::is_Executing(void)
261 {
262 return( m_pTool->is_Executing() );
263 }
264
265
266 ///////////////////////////////////////////////////////////
267 // //
268 ///////////////////////////////////////////////////////////
269
270 //---------------------------------------------------------
Execute(bool bDialog)271 bool CWKSP_Tool::Execute(bool bDialog)
272 {
273 bool bResult = false;
274
275 //-----------------------------------------------------
276 if( g_pTool )
277 {
278 if( g_pTool == this )
279 {
280 if( g_pTool->is_Executing() )
281 {
282 if( !bDialog || DLG_Message_Confirm(_TL("Shall execution be stopped?"), m_pTool->Get_Name().c_str()) )
283 {
284 bResult = true;
285
286 PROCESS_Set_Okay(false);
287 }
288 }
289 else if( m_pTool->is_Interactive() )
290 {
291 if( !bDialog || DLG_Message_Confirm(_TL("Shall execution be stopped?"), m_pTool->Get_Name().c_str()) )
292 {
293 bResult = ((CSG_Tool_Interactive *)m_pTool)->Execute_Finish();
294
295 g_pTool = NULL;
296 }
297 }
298 }
299 else
300 {
301 DLG_Message_Show(_TL("Can't execute a tool while another runs"), _TL("Tool Execution"));
302 }
303 }
304
305 //-----------------------------------------------------
306 else
307 {
308 g_pTool = this;
309
310 if( m_pTool->On_Before_Execution() && (!bDialog || DLG_Parameters(m_pTool->Get_Parameters(), "", m_pTool->Get_Summary(false).c_str())) )
311 {
312 g_pTools->Set_Recently_Used(this);
313
314 MSG_General_Add_Line(); MSG_Execution_Add_Line();
315
316 STATUSBAR_Set_Text(m_pTool->Get_Name().w_str());
317
318 bResult = m_pTool->Execute(true);
319
320 m_pTool->On_After_Execution();
321
322 g_pActive->Get_Parameters()->Update_Parameters(m_pTool->Get_Parameters(), false);
323
324 if( g_pTools && g_pTools->Do_Beep() )
325 {
326 Do_Beep();
327 }
328
329 SG_UI_ProgressAndMsg_Reset();
330 }
331
332 if( !m_pTool->is_Interactive() || !bResult )
333 {
334 g_pTool = NULL;
335 }
336
337 if( m_bCloseGUI )
338 {
339 m_bCloseGUI = false;
340
341 MDI_Get_Frame()->Close();
342 }
343 }
344
345 //-----------------------------------------------------
346 return( bResult );
347 }
348
349 //---------------------------------------------------------
Execute(CSG_Point ptWorld,TSG_Tool_Interactive_Mode Mode,int Keys)350 bool CWKSP_Tool::Execute(CSG_Point ptWorld, TSG_Tool_Interactive_Mode Mode, int Keys)
351 {
352 if( g_pTool == this && m_pTool->is_Interactive() )
353 {
354 return( ((CSG_Tool_Interactive *)m_pTool)->Execute_Position(ptWorld, Mode, Keys) );
355 }
356
357 return( false );
358 }
359
360 //---------------------------------------------------------
Finish(bool bDialog,bool bCloseGUI)361 bool CWKSP_Tool::Finish(bool bDialog, bool bCloseGUI)
362 {
363 if( Execute(bDialog) )
364 {
365 m_bCloseGUI = bCloseGUI;
366
367 return( true );
368 }
369
370 return( false );
371 }
372
373
374 ///////////////////////////////////////////////////////////
375 // //
376 // //
377 // //
378 ///////////////////////////////////////////////////////////
379
380 //---------------------------------------------------------
381 #include <wx/clipbrd.h>
382
383 //---------------------------------------------------------
Save_to_Clipboard(void)384 void CWKSP_Tool::Save_to_Clipboard(void)
385 {
386 wxArrayString Choices;
387
388 Choices.Add(_TL("Tool Chain" ));
389 Choices.Add(_TL("Tool Chain with Header" ));
390 Choices.Add(_TL("Command Line" ));
391 Choices.Add(_TL("Command Line with Header"));
392 Choices.Add(_TL("Python" ));
393 Choices.Add(_TL("Python with Header" ));
394
395 wxSingleChoiceDialog dlg(MDI_Get_Top_Window(), _TL("Select Format"), _TL("Save to Clipboard"), Choices);
396
397 if( dlg.ShowModal() == wxID_OK )
398 {
399 CSG_String Script;
400
401 switch( dlg.GetSelection() )
402 {
403 case 0: Script = m_pTool->Get_Script(TOOL_SCRIPT_CHAIN , false); break; // Tool Chain
404 case 1: Script = m_pTool->Get_Script(TOOL_SCRIPT_CHAIN , true); break; // Tool Chain with Header
405 #ifdef _SAGA_MSW
406 case 2: Script = m_pTool->Get_Script(TOOL_SCRIPT_CMD_BATCH, false); break; // Command Line
407 case 3: Script = m_pTool->Get_Script(TOOL_SCRIPT_CMD_BATCH, true); break; // Command Line with Header
408 #else
409 case 2: Script = m_pTool->Get_Script(TOOL_SCRIPT_CMD_SHELL, false); break; // Command Line
410 case 3: Script = m_pTool->Get_Script(TOOL_SCRIPT_CMD_SHELL, true); break; // Command Line with Header
411 #endif
412 case 4: Script = m_pTool->Get_Script(TOOL_SCRIPT_PYTHON , false); break; // Python
413 case 5: Script = m_pTool->Get_Script(TOOL_SCRIPT_PYTHON , true); break; // Python with Header
414 }
415
416 if( !Script.is_Empty() && wxTheClipboard->Open() )
417 {
418 wxTheClipboard->SetData(new wxTextDataObject(Script.c_str()));
419
420 wxTheClipboard->Close();
421 }
422 }
423 }
424
425 //---------------------------------------------------------
Save_to_Script(void)426 void CWKSP_Tool::Save_to_Script(void)
427 {
428 wxString FileName;
429
430 if( DLG_Save(FileName, _TL("Create Script Command File"), "DOS Batch Script (*.bat)|*.bat|Bash Script (*.sh)|*.sh|Python Script (*.py)|*.py|SAGA Tool Chain (*.xml)|*.xml") )
431 {
432 CSG_String Script;
433
434 if( SG_File_Cmp_Extension(&FileName, "xml") )
435 {
436 Script = m_pTool->Get_Script(TOOL_SCRIPT_CHAIN , true);
437 }
438
439 if( SG_File_Cmp_Extension(&FileName, "bat") )
440 {
441 Script = m_pTool->Get_Script(TOOL_SCRIPT_CMD_BATCH, true);
442 }
443
444 if( SG_File_Cmp_Extension(&FileName, "sh") )
445 {
446 Script = m_pTool->Get_Script(TOOL_SCRIPT_CMD_SHELL, true);
447 }
448
449 if( SG_File_Cmp_Extension(&FileName, "py") )
450 {
451 Script = m_pTool->Get_Script(TOOL_SCRIPT_PYTHON , true);
452 }
453
454 //-------------------------------------------------
455 CSG_File File;
456
457 if( !Script.is_Empty() && File.Open(&FileName, SG_FILE_W, false) )
458 {
459 File.Write(Script);
460 }
461 }
462 }
463
464
465 ///////////////////////////////////////////////////////////
466 // //
467 // //
468 // //
469 ///////////////////////////////////////////////////////////
470
471 //---------------------------------------------------------
472