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.

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