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.

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