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.

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