You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

454 lines
15 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file class_libentry.h
  27. * @brief Class LIB_ITEM definition.
  28. */
  29. #ifndef _LIB_ITEM_H_
  30. #define _LIB_ITEM_H_
  31. #include "base_struct.h"
  32. #include "transform.h"
  33. #include <boost/ptr_container/ptr_vector.hpp>
  34. class LINE_READER;
  35. class OUTPUTFORMATTER;
  36. class LIB_COMPONENT;
  37. class PLOTTER;
  38. class LIB_ITEM;
  39. class LIB_PIN;
  40. extern const int fill_tab[];
  41. #define MINIMUM_SELECTION_DISTANCE 2 // Minimum selection distance in internal units
  42. /**
  43. * Helper for defining a list of library draw object pointers. The Boost
  44. * pointer containers are responsible for deleting object pointers placed
  45. * in them. If you access a object pointer from the list, do not delete
  46. * it directly.
  47. */
  48. typedef boost::ptr_vector< LIB_ITEM > LIB_ITEMS;
  49. /**
  50. * Helper for defining a list of pin object pointers. The list does not
  51. * use a Boost pointer class so the object pointers do not accidentally get
  52. * deleted when the container is deleted.
  53. */
  54. typedef std::vector< LIB_PIN* > LIB_PINS;
  55. /****************************************************************************/
  56. /* Classes for handle the body items of a component: pins add graphic items */
  57. /****************************************************************************/
  58. /**
  59. * Base class for drawable items used in library components.
  60. * (graphic shapes, texts, fields, pins)
  61. */
  62. class LIB_ITEM : public EDA_ITEM
  63. {
  64. /**
  65. * Draws the item.
  66. */
  67. virtual void drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  68. const wxPoint& aOffset, int aColor,
  69. int aDrawMode, void* aData, const TRANSFORM& aTransform ) = 0;
  70. /**
  71. * Draw any editing specific graphics when the item is being edited.
  72. *
  73. * @param aClipBox - Clip box of the current device context.
  74. * @param aDC - The device context to draw on.
  75. * @param aColor - The index of the color to draw.
  76. */
  77. virtual void drawEditGraphics( EDA_RECT* aClipBox, wxDC* aDC, int aColor ) {}
  78. /**
  79. * Calculates the attributes of an item at \a aPosition when it is being edited.
  80. *
  81. * This method gets called by the Draw() method when the item is being edited. This
  82. * probably should be a pure virtual method but bezier curves are not yet editable in
  83. * the component library editor. Therefore, the default method does nothing.
  84. *
  85. * @param aPosition - The current mouse position in drawing coordinates.
  86. */
  87. virtual void calcEdit( const wxPoint& aPosition ) {}
  88. bool m_eraseLastDrawItem; ///< Used when editing a new draw item to prevent drawing
  89. ///< artifacts.
  90. friend class LIB_COMPONENT;
  91. protected:
  92. /**
  93. * Unit identification for multiple parts per package. Set to 0 if the
  94. * item is common to all units.
  95. */
  96. int m_Unit;
  97. /**
  98. * Shape identification for alternate body styles. Set 0 if the item
  99. * is common to all body styles. This is commonly referred to as
  100. * DeMorgan style and this is typically how it is used in KiCad.
  101. */
  102. int m_Convert;
  103. /**
  104. * The body fill type. This has meaning only for some items. For a list of
  105. * fill types see #FILL_T.
  106. */
  107. FILL_T m_Fill;
  108. wxString m_typeName; ///< Name of object displayed in the message panel.
  109. wxPoint m_initialPos; ///< Temporary position when moving an existing item.
  110. wxPoint m_initialCursorPos; ///< Initial cursor position at the beginning of a move.
  111. public:
  112. LIB_ITEM( KICAD_T aType,
  113. LIB_COMPONENT* aComponent = NULL,
  114. int aUnit = 0,
  115. int aConvert = 0,
  116. FILL_T aFillType = NO_FILL );
  117. LIB_ITEM( const LIB_ITEM& aItem );
  118. virtual ~LIB_ITEM() { }
  119. wxString GetTypeName() { return m_typeName; }
  120. /**
  121. * Begin an editing a component library draw item in \a aEditMode at \a aPosition.
  122. *
  123. * This is used to start an editing action such as resize or move a draw object.
  124. * It typically would be called on a left click when a draw tool is selected in
  125. * the component library editor and one of the graphics tools is selected. It
  126. * allows the draw item to maintain it's own internal state while it is being
  127. * edited. Call AbortEdit() to quit the editing mode.
  128. *
  129. * @param aEditMode - The editing mode being performed. See base_struct.h for a list
  130. * of mode flags.
  131. * @param aPosition - The position in drawing coordinates where the editing mode was
  132. * started. This may or may not be required depending on the item
  133. * being edited and the edit mode.
  134. */
  135. virtual void BeginEdit( int aEditMode, const wxPoint aPosition = wxPoint( 0, 0 ) ) {}
  136. /**
  137. * Continue an edit in progress at \a aPosition.
  138. *
  139. * This is used to perform the next action while editing a draw item. This would be
  140. * called for each additional left click when the mouse is captured while the item
  141. * is being edited.
  142. *
  143. * @param aPosition - The position of the mouse left click in drawing coordinates.
  144. * @return True if additional mouse clicks are required to complete the edit in progress.
  145. */
  146. virtual bool ContinueEdit( const wxPoint aPosition ) { return false; }
  147. /**
  148. * End an object editing action.
  149. *
  150. * This is used to end or abort an edit action in progress initiated by BeginEdit().
  151. *
  152. * @param aPosition - The position of the last edit event in drawing coordinates.
  153. * @param aAbort - Set to true to abort the current edit in progress.
  154. */
  155. virtual void EndEdit( const wxPoint& aPosition, bool aAbort = false ) { m_Flags = 0; }
  156. /**
  157. * Draw an item
  158. *
  159. * @param aPanel - DrawPanel to use (can be null) mainly used for clipping
  160. * purposes
  161. * @param aDC - Device Context (can be null)
  162. * @param aOffset - offset to draw
  163. * @param aColor - -1 to use the normal body item color, or use this color
  164. * if >= 0
  165. * @param aDrawMode - GR_OR, GR_XOR, ...
  166. * @param aData - value or pointer used to pass others parameters,
  167. * depending on body items. used for some items to force
  168. * to force no fill mode ( has meaning only for items what
  169. * can be filled ). used in printing or moving objects mode
  170. * or to pass reference to the lib component for pins
  171. * @param aTransform - Transform Matrix (rotation, mirror ..)
  172. */
  173. virtual void Draw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint &aOffset, int aColor,
  174. int aDrawMode, void* aData, const TRANSFORM& aTransform );
  175. /**
  176. * @return the size of the "pen" that be used to draw or plot this item
  177. */
  178. virtual int GetPenSize() const = 0;
  179. /**
  180. * Function Save
  181. * writes draw item object to \a aFormatter in component library "*.lib" format.
  182. *
  183. * @param aFormatter A referenct to an #OUTPUTFORMATTER object to write the
  184. * component library item to.
  185. * @return True if success writing else false.
  186. */
  187. virtual bool Save( OUTPUTFORMATTER& aFormatter ) = 0;
  188. virtual bool Load( LINE_READER& aLine, wxString& aErrorMsg ) = 0;
  189. LIB_COMPONENT* GetParent()
  190. {
  191. return (LIB_COMPONENT *)m_Parent;
  192. }
  193. /**
  194. * Tests if the given point is within the bounds of this object.
  195. *
  196. * Derived classes should override this function.
  197. *
  198. * @param aPosition - The coordinates to test.
  199. * @return - true if a hit, else false
  200. */
  201. virtual bool HitTest( const wxPoint& aPosition )
  202. {
  203. return false;
  204. }
  205. /**
  206. * @param aPosition - a wxPoint to test
  207. * @param aThreshold - max distance to this object (usually the half
  208. * thickness of a line)
  209. * if < 0, it will be automatically set to half
  210. * pen size when locating lines or arcs
  211. * and set to 0 for other items
  212. * @param aTransform - the transform matrix
  213. * @return - true if the point \a aPosition is near this object
  214. */
  215. virtual bool HitTest( wxPoint aPosition, int aThreshold, const TRANSFORM& aTransform ) = 0;
  216. /**
  217. * @return the boundary box for this, in library coordinates
  218. */
  219. virtual EDA_RECT GetBoundingBox() const { return EDA_ITEM::GetBoundingBox(); }
  220. /**
  221. * Function DisplayInfo
  222. * displays basic info (type, part and convert) about the current item
  223. * in message panel.
  224. * <p>
  225. * This base function is used to display the information common to the
  226. * all library items. Call the base class from the derived class or the
  227. * common information will not be updated in the message panel.
  228. * </p>
  229. * @param aFrame A pointer to EDA_DRAW_FRAME window where the message panel resides.
  230. */
  231. virtual void DisplayInfo( EDA_DRAW_FRAME* aFrame );
  232. /**
  233. * Test LIB_ITEM objects for equivalence.
  234. *
  235. * @param aOther - Object to test against.
  236. * @return - True if object is identical to this object.
  237. */
  238. bool operator==( const LIB_ITEM& aOther ) const;
  239. bool operator==( const LIB_ITEM* aOther ) const
  240. {
  241. return *this == *aOther;
  242. }
  243. /**
  244. * Test if another draw item is less than this draw object.
  245. *
  246. * @param aOther - Draw item to compare against.
  247. * @return - True if object is less than this object.
  248. */
  249. bool operator<( const LIB_ITEM& aOther) const;
  250. /**
  251. * Set drawing object offset from the current position.
  252. *
  253. * @param aOffset - Coordinates to offset position.
  254. */
  255. void SetOffset( const wxPoint& aOffset ) { DoOffset( aOffset ); }
  256. /**
  257. * Test if any part of the draw object is inside rectangle bounds.
  258. *
  259. * This is used for block selection. The real work is done by the
  260. * DoTestInside method for each derived object type.
  261. *
  262. * @param aRect - Rectangle to check against.
  263. * @return - True if object is inside rectangle.
  264. */
  265. bool Inside( EDA_RECT& aRect ) const { return DoTestInside( aRect ); }
  266. /**
  267. * Move a draw object to a new \a aPosition.
  268. *
  269. * The real work is done by the DoMove method for each derived object type.
  270. *
  271. * @param aPosition - Position to move draw item to.
  272. */
  273. void Move( const wxPoint& aPosition ) { DoMove( aPosition ); }
  274. /**
  275. * Return the current draw object start position.
  276. */
  277. wxPoint GetPosition() const { return DoGetPosition(); }
  278. void SetPosition( const wxPoint& aPosition ) { DoMove( aPosition ); }
  279. /**
  280. * Mirror the draw object along the horizontal (X) axis about a point.
  281. *
  282. * @param aCenter - Point to mirror around.
  283. */
  284. void MirrorHorizontal( const wxPoint& aCenter )
  285. {
  286. DoMirrorHorizontal( aCenter );
  287. }
  288. /**
  289. * Mirror the draw object along the MirrorVertical (Y) axis about a point.
  290. *
  291. * @param aCenter - Point to mirror around.
  292. */
  293. void MirrorVertical( const wxPoint& aCenter )
  294. {
  295. DoMirrorVertical( aCenter );
  296. }
  297. /**
  298. * Rotate about a point.
  299. *
  300. * @param aCenter - Point to rotate around.
  301. */
  302. void Rotate( const wxPoint& aCenter )
  303. {
  304. DoRotate( aCenter );
  305. }
  306. /**
  307. * Rotate the draw item.
  308. */
  309. virtual void Rotate() {}
  310. /**
  311. * Plot the draw item using the plot object.
  312. *
  313. * @param aPlotter - The plot object to plot to.
  314. * @param aOffset - Plot offset position.
  315. * @param aFill - Flag to indicate whether or not the object is filled.
  316. * @param aTransform - The plot transform.
  317. */
  318. void Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill, const TRANSFORM& aTransform )
  319. {
  320. DoPlot( aPlotter, aOffset, aFill, aTransform );
  321. }
  322. /**
  323. * Return the width of the draw item.
  324. *
  325. * @return Width of draw object.
  326. */
  327. int GetWidth() const { return DoGetWidth(); }
  328. void SetWidth( int aWidth ) { DoSetWidth( aWidth ); }
  329. /**
  330. * Check if draw object can be filled.
  331. *
  332. * The default setting is false. If the derived object support filling,
  333. * set the m_isFillable member to true.
  334. *
  335. * @return - True if draw object can be fill. Default is false.
  336. */
  337. bool IsFillable() const { return m_isFillable; }
  338. /**
  339. * Return the draw item editing mode status.
  340. *
  341. * @return - True if the item is being edited.
  342. */
  343. bool InEditMode() const { return ( m_Flags & ( IS_NEW | IS_MOVED | IS_RESIZED ) ) != 0; }
  344. void SetEraseLastDrawItem( bool aErase = true ) { m_eraseLastDrawItem = aErase; }
  345. virtual int GetDefaultColor();
  346. void SetUnit( int aUnit ) { m_Unit = aUnit; }
  347. int GetUnit() const { return m_Unit; }
  348. void SetConvert( int aConvert ) { m_Convert = aConvert; }
  349. int GetConvert() const { return m_Convert; }
  350. void SetFillMode( FILL_T aFillMode ) { m_Fill = aFillMode; }
  351. FILL_T GetFillMode() const { return m_Fill; }
  352. #if defined(DEBUG)
  353. void Show( int nestLevel, std::ostream& os ) const { ShowDummy( os ); } // override
  354. #endif
  355. protected:
  356. /**
  357. * Provide the draw object specific comparison.
  358. *
  359. * This is called by the == and < operators.
  360. *
  361. * The sort order is as follows:
  362. * - Component alternate part (DeMorgan) number.
  363. * - Component part number.
  364. * - KICAD_T enum value.
  365. * - Result of derived classes comparison.
  366. */
  367. virtual int DoCompare( const LIB_ITEM& aOther ) const = 0;
  368. virtual void DoOffset( const wxPoint& aOffset ) = 0;
  369. virtual bool DoTestInside( EDA_RECT& aRect ) const = 0;
  370. virtual void DoMove( const wxPoint& aPosition ) = 0;
  371. virtual wxPoint DoGetPosition() const = 0;
  372. virtual void DoMirrorHorizontal( const wxPoint& aCenter ) = 0;
  373. virtual void DoMirrorVertical( const wxPoint& aCenter ) = 0;
  374. virtual void DoRotate( const wxPoint& aCenter, bool aRotateCCW = true ) = 0;
  375. virtual void DoPlot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  376. const TRANSFORM& aTransform ) = 0;
  377. virtual int DoGetWidth() const = 0;
  378. virtual void DoSetWidth( int aWidth ) = 0;
  379. /** Flag to indicate if draw item is fillable. Default is false. */
  380. bool m_isFillable;
  381. };
  382. #endif // _LIB_ITEM_H_