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.

448 lines
16 KiB

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