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_data_item.cpp                   //
15 //                                                       //
16 //          Copyright (C) 2013 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 Hamburg                  //
42 //                Germany                                //
43 //                                                       //
44 //    e-mail:     oconrad@saga-gis.org                   //
45 //                                                       //
46 ///////////////////////////////////////////////////////////
47 
48 //---------------------------------------------------------
49 #include "res_commands.h"
50 #include "res_dialogs.h"
51 
52 #include "saga_frame.h"
53 
54 #include "helper.h"
55 
56 #include "active.h"
57 #include "active_parameters.h"
58 
59 #include "wksp_base_control.h"
60 
61 #include "wksp_data_manager.h"
62 #include "wksp_data_menu_files.h"
63 
64 #include "wksp_map_manager.h"
65 #include "wksp_map.h"
66 #include "wksp_map_layer.h"
67 
68 #include "wksp_data_manager.h"
69 #include "wksp_data_layers.h"
70 #include "wksp_data_item.h"
71 
72 #include "view_histogram.h"
73 #include "view_scatterplot.h"
74 
75 #include "data_source_pgsql.h"
76 
77 
78 ///////////////////////////////////////////////////////////
79 //														 //
80 //														 //
81 //														 //
82 ///////////////////////////////////////////////////////////
83 
84 //---------------------------------------------------------
CWKSP_Data_Item(CSG_Data_Object * pObject)85 CWKSP_Data_Item::CWKSP_Data_Item(CSG_Data_Object *pObject)
86 {
87 	m_pObject	= pObject;
88 
89 	m_bUpdating	= false;
90 }
91 
92 //---------------------------------------------------------
~CWKSP_Data_Item(void)93 CWKSP_Data_Item::~CWKSP_Data_Item(void)
94 {
95 	CSG_Data_Object	*pObject = m_pObject; m_pObject = NULL;
96 
97 	//-----------------------------------------------------
98 	g_pSAGA_Frame->Freeze();
99 
100 	for(int i=m_Views.GetCount()-1; i>=0; i--)
101 	{
102 		((CVIEW_Base *)m_Views[i])->Do_Destroy();
103 	}
104 
105 	g_pSAGA_Frame->Thaw();
106 
107 	//-----------------------------------------------------
108 	if( pObject )
109 	{
110 		MSG_General_Add(wxString::Format("%s: %s...", _TL("Closing"), pObject->Get_Name()), true, true);
111 
112 		g_pData->On_Data_Deletion(pObject);
113 
114 		SG_Get_Data_Manager().Delete(pObject);
115 
116 		MSG_General_Add(_TL("okay"), false, false, SG_UI_MSG_STYLE_SUCCESS);
117 	}
118 }
119 
120 //---------------------------------------------------------
On_Data_Deletion(CSG_Data_Object * pObject)121 bool CWKSP_Data_Item::On_Data_Deletion(CSG_Data_Object *pObject)
122 {
123 	return( m_pObject && m_pObject != pObject ? CWKSP_Base_Item::On_Data_Deletion(pObject) : false );
124 }
125 
126 
127 ///////////////////////////////////////////////////////////
128 //														 //
129 ///////////////////////////////////////////////////////////
130 
131 //---------------------------------------------------------
Add_Metadata2Parameters(const CSG_MetaData & M,CSG_Parameters & P,CSG_Parameter * pParent=NULL)132 void Add_Metadata2Parameters(const CSG_MetaData &M, CSG_Parameters &P, CSG_Parameter *pParent = NULL)
133 {
134 	for(int i=0; i<M.Get_Children_Count(); i++)
135 	{
136 		CSG_String	Properties;
137 
138 		for(int j=0; j<M[i].Get_Property_Count(); j++)
139 		{
140 			if( j > 0 )	Properties	+= "\n";
141 
142 			Properties	+= M[i].Get_Property_Name(j) + ": " + M[i].Get_Property(j);
143 		}
144 
145 		if( M[i].Get_Children_Count() > 0 )
146 		{
147 			Add_Metadata2Parameters(M[i], P, P.Add_Node(pParent, SG_Get_String(P.Get_Count()), M[i].Get_Name(), Properties));
148 		}
149 		else if( M[i].Get_Content().is_Empty() )
150 		{
151 			P.Add_Info_String(pParent, SG_Get_String(P.Get_Count()), M[i].Get_Name(), Properties, Properties);
152 		}
153 		else
154 		{
155 			P.Add_Info_String(pParent, SG_Get_String(P.Get_Count()), M[i].Get_Name(), Properties, M[i].Get_Content());
156 		}
157 	}
158 }
159 
160 //---------------------------------------------------------
On_Command(int Cmd_ID)161 bool CWKSP_Data_Item::On_Command(int Cmd_ID)
162 {
163 	switch( Cmd_ID )
164 	{
165 	default:
166 		return( CWKSP_Base_Item::On_Command(Cmd_ID) );
167 
168 	case ID_CMD_DATA_SAVE:
169 		Save(m_pObject->Get_File_Name());
170 		break;
171 
172 	case ID_CMD_DATA_SAVEAS:
173 		Save();
174 		break;
175 
176 	case ID_CMD_DATA_SAVETODB:
177 		switch( Get_Type() )
178 		{
179 		case WKSP_ITEM_Table :	PGSQL_Save_Table ((CSG_Table  *)m_pObject);	break;
180 		case WKSP_ITEM_Shapes:	PGSQL_Save_Shapes((CSG_Shapes *)m_pObject);	break;
181 		case WKSP_ITEM_Grid  :	PGSQL_Save_Grid  ((CSG_Grid   *)m_pObject);	break;
182 		case WKSP_ITEM_Grids :	PGSQL_Save_Grids ((CSG_Grids  *)m_pObject);	break;
183 
184 		default:	break;
185 		}
186 		break;
187 
188 	case ID_CMD_DATA_RELOAD:
189 		if( m_pObject->Reload() )
190 		{
191 			DataObject_Changed();
192 		}
193 		break;
194 
195 	case ID_CMD_DATA_DEL_FILES:
196 		if( m_pObject->Delete() )
197 		{
198 			g_pActive->Update_Description();
199 		}
200 		break;
201 
202 	case ID_CMD_DATA_METADATA:
203 		if( m_pObject->Get_MetaData().Get_Children_Count() > 0 )
204 		{
205 			CSG_Parameters	P;
206 
207 			Add_Metadata2Parameters(m_pObject->Get_MetaData(), P);
208 
209 			DLG_Parameters(&P, wxString::Format("%s [%s]", _TL("View Metadata"), m_pObject->Get_Name()));
210 		}
211 		break;
212 	}
213 
214 	return( true );
215 }
216 
217 //---------------------------------------------------------
On_Command_UI(wxUpdateUIEvent & event)218 bool CWKSP_Data_Item::On_Command_UI(wxUpdateUIEvent &event)
219 {
220 	switch( event.GetId() )
221 	{
222 	default:
223 		return( CWKSP_Base_Item::On_Command_UI(event) );
224 
225 	case ID_CMD_DATA_SAVE:
226 		event.Enable(m_pObject->is_Modified() && m_pObject->Get_File_Name() && *(m_pObject->Get_File_Name()));
227 		break;
228 
229 	case ID_CMD_DATA_SAVETODB:
230 		event.Enable(PGSQL_has_Connections());
231 		break;
232 
233 	case ID_CMD_DATA_RELOAD:
234 		event.Enable(m_pObject->is_File_Native() && m_pObject->is_Modified());
235 		break;
236 
237 	case ID_CMD_DATA_DEL_FILES:
238 		event.Enable(m_pObject->is_File_Native() && SG_File_Exists(m_pObject->Get_File_Name()) );
239 		break;
240 	}
241 
242 	return( true );
243 }
244 
245 
246 ///////////////////////////////////////////////////////////
247 //														 //
248 ///////////////////////////////////////////////////////////
249 
250 //---------------------------------------------------------
Get_Name(void)251 wxString CWKSP_Data_Item::Get_Name(void)
252 {
253 	wxString	Name("###");
254 
255 	if( m_pObject && *m_pObject->Get_Name() )
256 	{
257 		Name	= m_pObject->Get_Name();
258 	}
259 
260 	if( g_pData->Get_Numbering() < 0 )
261 	{
262 		return( Name );
263 	}
264 
265 	return( wxString::Format("%0*d. %s", g_pData->Get_Numbering(), 1 + Get_ID(), Name.c_str()) );
266 }
267 
268 
269 ///////////////////////////////////////////////////////////
270 //														 //
271 ///////////////////////////////////////////////////////////
272 
273 //---------------------------------------------------------
On_Create_Parameters(void)274 void CWKSP_Data_Item::On_Create_Parameters(void)
275 {
276 	CWKSP_Base_Item::On_Create_Parameters();
277 
278 	//-----------------------------------------------------
279 	// Nodes...
280 
281 	m_Parameters.Add_Node("", "NODE_GENERAL", _TL("General"), _TL(""));
282 
283 
284 	//-----------------------------------------------------
285 	// General...
286 
287 	if( g_pData->Get_Parameter("NAME_BY_FILE")->asBool() && *m_pObject->Get_File_Name() )
288 	{
289 		CSG_String	Name(SG_File_Get_Name(m_pObject->Get_File_Name(), false));
290 
291 		if( !Name.is_Empty() )
292 		{
293 			m_pObject->Set_Name(Name);
294 		}
295 	}
296 
297 	m_Parameters.Add_String("NODE_GENERAL", "OBJECT_NAME"  , _TL("Name"       ), _TL(""), m_pObject->Get_Name());
298 	m_Parameters.Add_String("NODE_GENERAL", "OBJECT_DESC"  , _TL("Description"), _TL(""), m_pObject->Get_Description(), true);
299 	m_Parameters.Add_Range ("NODE_GENERAL", "OBJECT_NODATA", _TL("No Data"    ), _TL(""));
300 }
301 
302 
303 ///////////////////////////////////////////////////////////
304 //														 //
305 ///////////////////////////////////////////////////////////
306 
307 //---------------------------------------------------------
Save(void)308 bool CWKSP_Data_Item::Save(void)
309 {
310 	int	idDlg;
311 
312 	switch( Get_Type() )
313 	{
314 	case WKSP_ITEM_Table     :	idDlg	= ID_DLG_TABLE_SAVE     ;	break;
315 	case WKSP_ITEM_TIN       :	idDlg	= ID_DLG_SHAPES_SAVE    ;	break;
316 	case WKSP_ITEM_Shapes    :	idDlg	= ID_DLG_SHAPES_SAVE    ;	break;
317 	case WKSP_ITEM_PointCloud:	idDlg	= ID_DLG_POINTCLOUD_SAVE;	break;
318 	case WKSP_ITEM_Grid      :	idDlg	= ID_DLG_GRID_SAVE      ;	break;
319 	case WKSP_ITEM_Grids     :	idDlg	= ID_DLG_GRIDS_SAVE     ;	break;
320 	default:	return( false );
321 	}
322 
323 	wxString	FileName	= m_pObject->Get_File_Name() && *m_pObject->Get_File_Name()
324 		? m_pObject->Get_File_Name()
325 		: m_pObject->Get_Name();
326 
327 	if( DLG_Save(FileName, idDlg) && m_pObject->Save(&FileName) )
328 	{
329 		return( true );
330 	}
331 
332 	return( false );
333 }
334 
335 //---------------------------------------------------------
Save(const wxString & File_Name)336 bool CWKSP_Data_Item::Save(const wxString &File_Name)
337 {
338 	if( File_Name.Length() )
339 	{
340 		bool	bResult	= m_pObject->Save(&File_Name);
341 
342 		if( bResult )
343 		{
344 			g_pData->Get_Menu_Files()->Recent_Add(m_pObject->Get_ObjectType(), m_pObject->Get_File_Name());
345 		}
346 
347 		PROCESS_Set_Okay();
348 
349 		return( bResult );
350 	}
351 
352 	return( Save() );
353 }
354 
355 
356 ///////////////////////////////////////////////////////////
357 //														 //
358 ///////////////////////////////////////////////////////////
359 
360 //---------------------------------------------------------
Parameters_Changed(void)361 void CWKSP_Data_Item::Parameters_Changed(void)
362 {
363 	if( !m_bUpdating )
364 	{
365 		m_bUpdating	= true;
366 
367 		On_Parameters_Changed();
368 
369 		CWKSP_Base_Item::Parameters_Changed();
370 
371 		m_bUpdating	= false;
372 
373 		Update_Views(true);
374 	}
375 }
376 
377 //---------------------------------------------------------
On_Parameters_Changed(void)378 void CWKSP_Data_Item::On_Parameters_Changed(void)
379 {
380 	m_pObject->Set_Name       (m_Parameters("OBJECT_NAME")->asString());
381 	m_pObject->Set_Description(m_Parameters("OBJECT_DESC")->asString());
382 	m_pObject->Set_NoData_Value_Range(
383 		m_Parameters("OBJECT_NODATA")->asRange()->Get_Min(),
384 		m_Parameters("OBJECT_NODATA")->asRange()->Get_Max()
385 	);
386 }
387 
388 //---------------------------------------------------------
DataObject_Changed(void)389 bool CWKSP_Data_Item::DataObject_Changed(void)
390 {
391 	m_Parameters.Set_Name(CSG_String::Format("%02d. %s", 1 + Get_ID(), m_pObject->Get_Name()));
392 
393 	m_Parameters.Set_Parameter("OBJECT_NAME"      , m_pObject->Get_Name             ());
394 	m_Parameters.Set_Parameter("OBJECT_DESC"      , m_pObject->Get_Description      ());
395 	m_Parameters.Set_Parameter("OBJECT_NODATA.MIN", m_pObject->Get_NoData_Value(false));
396 	m_Parameters.Set_Parameter("OBJECT_NODATA.MAX", m_pObject->Get_NoData_Value(true ));
397 
398 	//-----------------------------------------------------
399 	On_DataObject_Changed();
400 
401 	g_pActive->Update(this, false);
402 
403 	Parameters_Changed();
404 
405 	return( true );
406 }
407 
408 //---------------------------------------------------------
On_DataObject_Changed(void)409 void CWKSP_Data_Item::On_DataObject_Changed(void)
410 {
411 	// nop
412 }
413 
414 
415 ///////////////////////////////////////////////////////////
416 //														 //
417 ///////////////////////////////////////////////////////////
418 
419 //---------------------------------------------------------
Add_ScatterPlot(void)420 bool CWKSP_Data_Item::Add_ScatterPlot(void)
421 {
422 	new CVIEW_ScatterPlot(this);
423 
424 	return( true );
425 }
426 
427 
428 ///////////////////////////////////////////////////////////
429 //														 //
430 ///////////////////////////////////////////////////////////
431 
432 //---------------------------------------------------------
View_Opened(MDI_ChildFrame * pView)433 bool CWKSP_Data_Item::View_Opened(MDI_ChildFrame *pView)
434 {
435 	if( m_Views.Index(pView) == wxNOT_FOUND )	// only add once
436 	{
437 		m_Views.Add(pView);
438 
439 		return( true );
440 	}
441 
442 	return( false );
443 }
444 
445 //---------------------------------------------------------
View_Closes(MDI_ChildFrame * pView)446 bool CWKSP_Data_Item::View_Closes(MDI_ChildFrame *pView)
447 {
448 	if( m_Views.Index(pView) != wxNOT_FOUND )
449 	{
450 		m_Views.Remove(pView);
451 
452 		return( true );
453 	}
454 
455 	return( false );
456 }
457 
458 //---------------------------------------------------------
Update_Views(bool bAll)459 bool CWKSP_Data_Item::Update_Views(bool bAll)
460 {
461 	if( !m_bUpdating )
462 	{
463 		m_bUpdating	= true;
464 
465 		On_Update_Views(bAll);
466 
467 		if( bAll )
468 		{
469 			for(size_t i=0; i<m_Views.Count(); i++)
470 			{
471 				((CVIEW_Base *)m_Views[i])->Do_Update();
472 			}
473 		}
474 
475 		if( g_pActive->Get_Active_Data_Item() == this )
476 		{
477 			g_pActive->Update_Description();
478 			g_pActive->Update_Attributes();
479 			g_pActive->Update_Info();
480 		}
481 
482 		m_bUpdating	= false;
483 
484 		return( true );
485 	}
486 
487 	return( false );
488 }
489 
490 
491 ///////////////////////////////////////////////////////////
492 //														 //
493 //														 //
494 //														 //
495 ///////////////////////////////////////////////////////////
496 
497 //---------------------------------------------------------
498