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.

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