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. 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. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  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. virtual COLOR4D GetDefaultColor();
  319. void SetUnit( int aUnit ) { m_Unit = aUnit; }
  320. int GetUnit() const { return m_Unit; }
  321. void SetConvert( int aConvert ) { m_Convert = aConvert; }
  322. int GetConvert() const { return m_Convert; }
  323. void SetFillMode( FILL_T aFillMode ) { m_Fill = aFillMode; }
  324. FILL_T GetFillMode() const { return m_Fill; }
  325. #if defined(DEBUG)
  326. void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
  327. #endif
  328. private:
  329. /**
  330. * Provide the draw object specific comparison called by the == and < operators.
  331. *
  332. * The base object sort order which always proceeds the derived object sort order
  333. * is as follows:
  334. * - Component alternate part (DeMorgan) number.
  335. * - Component part number.
  336. * - KICAD_T enum value.
  337. * - Result of derived classes comparison.
  338. *
  339. * @param aOther A reference to the other #LIB_ITEM to compare the arc against.
  340. * @return An integer value less than 0 if the object is less than \a aOther ojbect,
  341. * zero if the object is equal to \a aOther object, or greater than 0 if the
  342. * object is greater than \a aOther object.
  343. */
  344. virtual int compare( const LIB_ITEM& aOther ) const = 0;
  345. };
  346. #endif // _LIB_ITEM_H_