DiagramEntity.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 55k
Category:

Graph program

Development Platform:

Visual C++

  1. /* ==========================================================================
  2. Class : CDiagramEntity
  3. Author : Johan Rosengren, Abstrakt Mekanik AB
  4. Date : 2004-03-29
  5. Purpose : "CDiagramEntity" is the base class for all objects that can 
  6. be drawn and managed by "CDiagramEditor".
  7. Description : "CDiagramEntity" is derived from "CObject", to allow 
  8. instances to be stored in "CObArrays".
  9. Usage : Classes should be derived from "CDiagramEntity". "Clone" 
  10. must be overridden, returning a copy of the this 
  11. pointer.
  12. Normally, "Draw" should also be overridden. 
  13. The class supports basic saving to a text file. If this 
  14. is desired, "SetType" must be called from the derived 
  15. class ctor with a string uniquely identifying the class. 
  16. "FromString" and "GetString" must be overridden if other 
  17. properties than the default are to be saved. Loading can 
  18. be accomplished by creating a static "factory"-function 
  19. returning an instance of the class if "FromString" returns 
  20. "TRUE" for a given line from a data file. See "CreateFromString" 
  21. in this class for a model implementation.
  22. Minimum- and maximum sizes for an instance of the 
  23. derived object can be set in the class ctor by calling
  24. "SetConstraints". A 0-constraint means that the object 
  25. can't be turned "inside out", while -1 means no 
  26. constraints.
  27. Popup menus for the derived classes can be created by 
  28. overriding "ShowPopup". Command ids for the menu items 
  29. must be in the range "CMD_START" to "CMD_END" inclusively, 
  30. and if a menu alternative is selected, it will be 
  31. returned to the class instance through "DoMessage" - 
  32. which must of course also be overriddden.
  33. Each derived class can also have a property dialog. The 
  34. dialog class must be derived from "CDiagramPropertyDlg". 
  35. The derived "CDiagramEntity" class must have a class 
  36. member instance of the desired "CDiagramPropertyDlg"-
  37. derived class, and call "SetAttributeDialog" in the "ctor". 
  38. Transport of data to and from the object is made in the 
  39. "CDiagramPropertyDlg"-derived class (see 
  40. CDiagramPropertyDlg.cpp)
  41. The number, position and types of the selection rects 
  42. can be modified by overriding "GetHitCode" and 
  43. "DrawSelectionMarkers", see CDiagramLine.cpp for an 
  44. example.
  45. "BodyInRect" can be overridden to allow non-rect hit 
  46. testing, see CDiagramLine.cpp for an example.
  47. "GetCursor" can be overridden to display other cursors 
  48. than the default ones.
  49.    ========================================================================
  50. Changes : 10/4 2004 Changed accessors for m_type to public.
  51. 24/4 2004 Added colon as a replace-character for 
  52. saving
  53. 30/4 2004 Added redraw parent to the property dialog
  54.    ========================================================================
  55. 23/6 2004 Added \newline as a replace-character when 
  56. saving/loading (Unruled Boy).
  57. 26/6 2004 Added group handling (Unruled Boy).
  58. 27/6 2004 Added help functions for saving and loading
  59.    ========================================================================
  60. 19/8 2004 Setting m_parent to NULL in the ctor (Marc G)
  61.    ========================================================================
  62. 23/1 2005 Made SetParent/GetParent public.
  63.    ========================================================================*/
  64. #include "stdafx.h"
  65. #include "DiagramEntity.h"
  66. #include "DiagramEntityContainer.h"
  67. #include "Tokenizer.h"
  68. #include "..//UnitConversion.h"
  69. #ifdef _DEBUG
  70. #define new DEBUG_NEW
  71. #undef THIS_FILE
  72. static char THIS_FILE[] = __FILE__;
  73. #endif
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CDiagramEntity
  76. CDiagramEntity::CDiagramEntity()
  77. /* ============================================================
  78. Function : CDiagramEntity::CDiagramEntity
  79. Description : Constructor
  80. Access : Public
  81. Return : void
  82. Parameters : none
  83. Usage :
  84.    ============================================================*/
  85. {
  86. SetParent( NULL );
  87. SetAttributeDialog( NULL, 0 );
  88. Clear();
  89. SetType( _T( "basic" ) );
  90. SetGroup( 0 );
  91. }
  92. CDiagramEntity::~CDiagramEntity()
  93. /* ============================================================
  94. Function : CDiagramEntity::~CDiagramEntity
  95. Description : Destructor
  96. Access : Public
  97. Return : void
  98. Parameters : none
  99. Usage :
  100.    ============================================================*/
  101. {
  102. }
  103. void CDiagramEntity::Clear()
  104. /* ============================================================
  105. Function : CDiagramEntity::Clear
  106. Description : Zero all properties of this object.
  107. Access : Protected
  108. Return : void
  109. Parameters : none
  110. Usage : Call to initialize the object.
  111.    ============================================================*/
  112. {
  113. SetRect( 0.0, 0.0, 0.0, 0.0 );
  114. SetMarkerSize( CSize( 8, 8 ) );
  115. SetConstraints( CSize( 1, 1 ), CSize( -1, -1 ) );
  116. Select( FALSE );
  117. SetParent( NULL );
  118. SetName( _T( "" ) );
  119. }
  120. CDiagramEntity* CDiagramEntity::Clone()
  121. /* ============================================================
  122. Function : CDiagramEntity::Clone
  123. Description : Clone this object to a new object.
  124. Access : Public
  125. Return : CDiagramEntity* - The new object.
  126. Parameters : none
  127. Usage : Call to create a clone of the object. The 
  128. caller will have to delete the object.
  129.    ============================================================*/
  130. {
  131. CDiagramEntity* obj = new CDiagramEntity;
  132. obj->Copy( this );
  133. return obj;
  134. }
  135. void CDiagramEntity::Copy( CDiagramEntity* obj )
  136. /* ============================================================
  137. Function : CDiagramEntity::Copy
  138. Description : Copy the information in "obj" to this object.
  139. Access : Public
  140. Return : void
  141. Parameters : CDiagramEntity* obj - The object to copy 
  142. from.
  143. Usage : Copies basic information. from "obj" to this.
  144. "GetType" can be used to check for the correct 
  145. object type in overridden versions.
  146.    ============================================================*/
  147. {
  148. Clear();
  149. SetMarkerSize( obj->GetMarkerSize() );
  150. SetConstraints( obj->GetMinimumSize(), obj->GetMaximumSize() );
  151. Select( obj->IsSelected() );
  152. SetParent( obj->GetParent() );
  153. SetType( obj->GetType() );
  154. SetTitle( obj->GetTitle() );
  155. SetName( obj->GetName() );
  156. SetRect( obj->GetLeft(), obj->GetTop(), obj->GetRight(), obj->GetBottom() );
  157. }
  158. BOOL CDiagramEntity::FromString( const CString& str )
  159. /* ============================================================
  160. Function : CDiagramEntity::FromString
  161. Description : Sets the values for an object from "str". 
  162. Access : Public
  163. Return : BOOL - "TRUE" if "str" 
  164. represents an 
  165. object of this 
  166. type.
  167. Parameters : const CString& str - Possible text 
  168. format 
  169. representation.
  170. Usage : Can be called to fill an existing object 
  171. with information from a string created with 
  172. "GetString".
  173.    ============================================================*/
  174. {
  175. BOOL result = FALSE;
  176. CString data( str );
  177. CString header = GetHeaderFromString( data );
  178. if( header == GetType() )
  179. if( GetDefaultFromString( data ) )
  180. result = TRUE;
  181. return result;
  182. }
  183. CString CDiagramEntity::GetHeaderFromString( CString& str )
  184. /* ============================================================
  185. Function : CDiagramEntity::GetHeaderFromString
  186. Description : Gets the header from "str".
  187. Access : Protected
  188. Return : CString - The type of "str".
  189. Parameters : CString& str - "CString" to get type from.
  190. Usage : Call as a part of loading the object. "str" 
  191. will have the type removed after the call.
  192.    ============================================================*/
  193. {
  194. CTokenizer main( str, _T( ":" ) );
  195. CString header;
  196. CString data;
  197. if( main.GetSize() == 2 )
  198. {
  199. main.GetAt( 0, header );
  200. main.GetAt( 1, data );
  201. header.TrimLeft();
  202. header.TrimRight();
  203. data.TrimLeft();
  204. data.TrimRight();
  205. str = data;
  206. }
  207. return header;
  208. }
  209. BOOL CDiagramEntity::GetDefaultFromString( CString& str )
  210. /* ============================================================
  211. Function : CDiagramEntity::GetDefaultFromString
  212. Description : Gets the default properties from "str"
  213. Access : Protected
  214. Return : BOOL - "TRUE" if the default 
  215. properties could be loaded ok.
  216. Parameters : CString& str - "CString" to get the 
  217. default properties from.
  218. Usage : Call as a part of loading the object from 
  219. disk. The default object properties will 
  220. be stripped from "str" and the object 
  221. properties set from the data.
  222.    ============================================================*/
  223. {
  224. BOOL result = FALSE;
  225. CString data( str );
  226. if( data[ data.GetLength() -1 ] == _TCHAR( ';' ) )
  227. data = data.Left( data.GetLength() - 1 ); // Strip the ';'
  228. CTokenizer tok( data ); 
  229. int size = tok.GetSize();
  230. if( size >= 7 )
  231. {
  232. double left;
  233. double top;
  234. double right;
  235. double bottom;
  236. CString title;
  237. CString name;
  238. int group;
  239. int count = 0;
  240. tok.GetAt( count++, left );
  241. tok.GetAt( count++, top );
  242. tok.GetAt( count++, right );
  243. tok.GetAt( count++, bottom );
  244. tok.GetAt( count++, title );
  245. tok.GetAt( count++, name );
  246. tok.GetAt( count++, group );
  247. SetRect( left, top, right, bottom );
  248. title.Replace( _T( "\colon" ), _T( ":" ) );
  249. title.Replace( _T( "\semicolon" ), _T( ";" ) );
  250. title.Replace( _T( "\comma" ), _T( "," ) );
  251. title.Replace( _T( "\newline" ), _T( "rn" ) );
  252. name.Replace( _T( "\colon" ), _T( ":" ) );
  253. name.Replace( _T( "\semicolon" ), _T( ";" ) );
  254. name.Replace( _T( "\comma" ), _T( "," ) );
  255. name.Replace( _T( "\newline" ), _T( "rn" ) );
  256. SetTitle( title );
  257. SetName( name );
  258. SetGroup( group );
  259. // Rebuild rest of string
  260. str = _T( "" );
  261. for( int t = count ; t < size ; t++ )
  262. {
  263. tok.GetAt( t, data );
  264. str += data;
  265. if( t < size - 1 )
  266. str += _T( "," );
  267. }
  268. result = TRUE;
  269. }
  270. return result;
  271. }
  272. BOOL CDiagramEntity::LoadFromString( CString& data )
  273. /* ============================================================
  274. Function : CDiagramEntity::LoadFromString
  275. Description : Loads the object from "data".
  276. Access : Public
  277. Return : BOOL - "TRUE" if "str" is a 
  278. well-formed object prefix.
  279. Parameters : CString& data - String to load from
  280. Usage : Call to load the first part of an object 
  281. from string.
  282.    ============================================================*/
  283. {
  284. BOOL result = FALSE;
  285. CString header = GetHeaderFromString( data );
  286. if( header == GetType() )
  287. if( GetDefaultFromString( data ) )
  288. result = TRUE;
  289. return result;
  290. }
  291. CDiagramEntity* CDiagramEntity::CreateFromString( const CString& str )
  292. /* ============================================================
  293. Function : CDiagramEntity::CreateFromString
  294. Description : Static factory function that creates and 
  295. returns an instance of this class if "str" 
  296. is a valid representation.
  297. Access : Public
  298. Return : CDiagramEntity* - The object, or "NULL" 
  299. if "str" is not a 
  300. representation of 
  301. this type.
  302. Parameters : const CString& str - The string to create 
  303. from.
  304. Usage : Can be used as a factory for text file loads. 
  305. Each object type should have its own 
  306. version - the default one is a model 
  307. implementation.
  308.    ============================================================*/
  309. {
  310. CDiagramEntity* obj = new CDiagramEntity;
  311. if(!obj->FromString( str ) )
  312. {
  313. delete obj;
  314. obj = NULL;
  315. }
  316. return obj;
  317. }
  318. void  CDiagramEntity::ReadStringFromArchive(CArchive& ar, CString& str)
  319. {
  320. int str_sz = 0;
  321. ar.Read(&str_sz,sizeof(int));
  322. if (str_sz<=0)
  323. return;
  324. ar.Read(str.GetBufferSetLength(str_sz), str_sz);
  325. }
  326. void   CDiagramEntity::WriteStringToArchive(CArchive& ar, CString& str)
  327. {
  328. int str_sz = str.GetLength();
  329. ar.Write(&str_sz, sizeof(int));
  330. if (str_sz<=0)
  331. return;
  332. ar.Write(str, str_sz);
  333. }
  334. void  CDiagramEntity::Serialize(CArchive& ar)
  335. {
  336. typedef struct
  337. {
  338. int a_f;
  339. int b_f;
  340. int c_f;
  341. int d_f;
  342. } OBJ_RESERVE_FIELDS;
  343. OBJ_RESERVE_FIELDS obj_reserve;
  344. memset(&obj_reserve,0,sizeof(OBJ_RESERVE_FIELDS));
  345. CRect rect = GetRect();
  346. // Saving and loading to/from a text file
  347. if (ar.IsStoring())
  348. {
  349. // 杨躔囗