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.

334 lines
12 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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-2020 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. #ifndef _LIB_ITEM_H_
  26. #define _LIB_ITEM_H_
  27. #include <base_struct.h>
  28. #include <eda_rect.h>
  29. #include <transform.h>
  30. #include <gr_basic.h>
  31. #include <render_settings.h>
  32. class LINE_READER;
  33. class OUTPUTFORMATTER;
  34. class LIB_PART;
  35. class PLOTTER;
  36. class LIB_PIN;
  37. class MSG_PANEL_ITEM;
  38. using KIGFX::RENDER_SETTINGS;
  39. extern const int fill_tab[];
  40. #define MINIMUM_SELECTION_DISTANCE 2 // Minimum selection distance in internal units
  41. /**
  42. * Helper for defining a list of pin object pointers. The list does not
  43. * use a Boost pointer class so the object pointers do not accidentally get
  44. * deleted when the container is deleted.
  45. */
  46. typedef std::vector< LIB_PIN* > LIB_PINS;
  47. /**
  48. * The base class for drawable items used by schematic library components.
  49. */
  50. class LIB_ITEM : public EDA_ITEM
  51. {
  52. /**
  53. * Print the item to \a aDC.
  54. *
  55. * @param aOffset A reference to a wxPoint object containing the offset where to draw
  56. * from the object's current position.
  57. * @param aData A pointer to any object specific data required to perform the draw.
  58. * @param aTransform A reference to a #TRANSFORM object containing drawing transform.
  59. */
  60. virtual void print( RENDER_SETTINGS* aSettings, const wxPoint& aOffset, void* aData,
  61. const TRANSFORM& aTransform ) = 0;
  62. friend class LIB_PART;
  63. protected:
  64. /**
  65. * Unit identification for multiple parts per package. Set to 0 if the item is common
  66. * to all units.
  67. */
  68. int m_Unit;
  69. /**
  70. * Shape identification for alternate body styles. Set 0 if the item is common to all
  71. * body styles. This is typially used for representing DeMorgan variants in KiCad.
  72. */
  73. int m_Convert;
  74. /**
  75. * The body fill type. This has meaning only for some items. For a list of fill types
  76. * see #FILL_T.
  77. */
  78. FILL_T m_Fill;
  79. bool m_isFillable;
  80. public:
  81. LIB_ITEM( KICAD_T aType, LIB_PART* aComponent = NULL, int aUnit = 0, int aConvert = 0,
  82. FILL_T aFillType = NO_FILL );
  83. // Do not create a copy constructor. The one generated by the compiler is adequate.
  84. virtual ~LIB_ITEM() { }
  85. // Define the enums for basic
  86. enum LIB_CONVERT : int { BASE = 1, DEMORGAN = 2 };
  87. /**
  88. * The list of flags used by the #compare function.
  89. *
  90. * - NORMAL This compares everthing between two #LIB_ITEM objects.
  91. * - UNIT This compare flag ignores unit and convert and pin number information when
  92. * comparing #LIB_ITEM objects for unit comparison.
  93. */
  94. enum COMPARE_FLAGS : int { NORMAL = 0x00, UNIT = 0x01 };
  95. /**
  96. * Provide a user-consumable name of the object type. Perform localization when
  97. * called so that run-time language selection works.
  98. */
  99. virtual wxString GetTypeName() = 0;
  100. /**
  101. * Begin drawing a component library draw item at \a aPosition.
  102. *
  103. * It typically would be called on a left click when a draw tool is selected in
  104. * the component library editor and one of the graphics tools is selected.
  105. *
  106. * @param aPosition The position in drawing coordinates where the drawing was started.
  107. * May or may not be required depending on the item being drawn.
  108. */
  109. virtual void BeginEdit( const wxPoint aPosition ) {}
  110. /**
  111. * Continue an edit in progress at \a aPosition.
  112. *
  113. * This is used to perform the next action while drawing an item. This would be
  114. * called for each additional left click when the mouse is captured while the item
  115. * is being drawn.
  116. *
  117. * @param aPosition The position of the mouse left click in drawing coordinates.
  118. * @return True if additional mouse clicks are required to complete the edit in progress.
  119. */
  120. virtual bool ContinueEdit( const wxPoint aPosition ) { return false; }
  121. /**
  122. * End an object editing action.
  123. *
  124. * This is used to end or abort an edit action in progress initiated by BeginEdit().
  125. */
  126. virtual void EndEdit() {}
  127. /**
  128. * Calculates the attributes of an item at \a aPosition when it is being edited.
  129. *
  130. * This method gets called by the Draw() method when the item is being edited. This
  131. * probably should be a pure virtual method but bezier curves are not yet editable in
  132. * the component library editor. Therefore, the default method does nothing.
  133. *
  134. * @param aPosition The current mouse position in drawing coordinates.
  135. */
  136. virtual void CalcEdit( const wxPoint& aPosition ) {}
  137. /**
  138. * Draw an item
  139. *
  140. * @param aDC Device Context (can be null)
  141. * @param aOffset Offset to draw
  142. * @param aData Value or pointer used to pass others parameters, depending on body items.
  143. * Used for some items to force to force no fill mode ( has meaning only for
  144. * items what can be filled ). used in printing or moving objects mode or to
  145. * pass reference to the lib component for pins.
  146. * @param aTransform Transform Matrix (rotation, mirror ..)
  147. */
  148. virtual void Print( RENDER_SETTINGS* aSettings, const wxPoint &aOffset, void* aData,
  149. const TRANSFORM& aTransform );
  150. virtual int GetPenWidth() const = 0;
  151. LIB_PART* GetParent() const
  152. {
  153. return (LIB_PART*) m_Parent;
  154. }
  155. void ViewGetLayers( int aLayers[], int& aCount ) const override;
  156. bool HitTest( const wxPoint& aPosition, int aAccuracy = 0 ) const override
  157. {
  158. // This is just here to prevent annoying compiler warnings about hidden overloaded
  159. // virtual functions
  160. return EDA_ITEM::HitTest( aPosition, aAccuracy );
  161. }
  162. bool HitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const override;
  163. /**
  164. * @return the boundary box for this, in library coordinates
  165. */
  166. const EDA_RECT GetBoundingBox() const override { return EDA_ITEM::GetBoundingBox(); }
  167. /**
  168. * Display basic info (type, part and convert) about the current item in message panel.
  169. * <p>
  170. * This base function is used to display the information common to the
  171. * all library items. Call the base class from the derived class or the
  172. * common information will not be updated in the message panel.
  173. * </p>
  174. * @param aList is the list to populate.
  175. */
  176. void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
  177. /**
  178. * Test LIB_ITEM objects for equivalence.
  179. *
  180. * @param aOther Object to test against.
  181. * @return True if object is identical to this object.
  182. */
  183. bool operator==( const LIB_ITEM& aOther ) const;
  184. bool operator==( const LIB_ITEM* aOther ) const
  185. {
  186. return *this == *aOther;
  187. }
  188. /**
  189. * Test if another draw item is less than this draw object.
  190. *
  191. * @param aOther - Draw item to compare against.
  192. * @return - True if object is less than this object.
  193. */
  194. bool operator<( const LIB_ITEM& aOther) const;
  195. /**
  196. * Set the drawing object by \a aOffset from the current position.
  197. *
  198. * @param aOffset Coordinates to offset the item position.
  199. */
  200. virtual void Offset( const wxPoint& aOffset ) = 0;
  201. /**
  202. * Move a draw object to \a aPosition.
  203. *
  204. * @param aPosition Position to move draw item to.
  205. */
  206. virtual void MoveTo( const wxPoint& aPosition ) = 0;
  207. void SetPosition( const wxPoint& aPosition ) { MoveTo( aPosition ); }
  208. /**
  209. * Mirror the draw object along the horizontal (X) axis about \a aCenter point.
  210. *
  211. * @param aCenter Point to mirror around.
  212. */
  213. virtual void MirrorHorizontal( const wxPoint& aCenter ) = 0;
  214. /**
  215. * Mirror the draw object along the MirrorVertical (Y) axis about \a aCenter point.
  216. *
  217. * @param aCenter Point to mirror around.
  218. */
  219. virtual void MirrorVertical( const wxPoint& aCenter ) = 0;
  220. /**
  221. * Rotate the object about \a aCenter point.
  222. *
  223. * @param aCenter Point to rotate around.
  224. * @param aRotateCCW True to rotate counter clockwise. False to rotate clockwise.
  225. */
  226. virtual void Rotate( const wxPoint& aCenter, bool aRotateCCW = true ) = 0;
  227. /**
  228. * Plot the draw item using the plot object.
  229. *
  230. * @param aPlotter The plot object to plot to.
  231. * @param aOffset Plot offset position.
  232. * @param aFill Flag to indicate whether or not the object is filled.
  233. * @param aTransform The plot transform.
  234. */
  235. virtual void Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
  236. const TRANSFORM& aTransform ) = 0;
  237. virtual int GetWidth() const = 0;
  238. virtual void SetWidth( int aWidth ) = 0;
  239. /**
  240. * Check if draw object can be filled.
  241. *
  242. * The default setting is false. If the derived object support filling, set the
  243. * m_isFillable member to true.
  244. */
  245. bool IsFillable() const { return m_isFillable; }
  246. void SetUnit( int aUnit ) { m_Unit = aUnit; }
  247. int GetUnit() const { return m_Unit; }
  248. void SetConvert( int aConvert ) { m_Convert = aConvert; }
  249. int GetConvert() const { return m_Convert; }
  250. void SetFillMode( FILL_T aFillMode ) { m_Fill = aFillMode; }
  251. FILL_T GetFillMode() const { return m_Fill; }
  252. #if defined(DEBUG)
  253. void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
  254. #endif
  255. protected:
  256. /**
  257. * Provide the draw object specific comparison called by the == and < operators.
  258. *
  259. * The base object sort order which always proceeds the derived object sort order
  260. * is as follows:
  261. * - Component alternate part (DeMorgan) number.
  262. * - Component part number.
  263. * - KICAD_T enum value.
  264. * - Result of derived classes comparison.
  265. *
  266. * @note Make sure you call down to #LIB_ITEM::compare before doing any derived object
  267. * comparisons or you will break the sorting using the symbol library file format.
  268. *
  269. * @param aOther A reference to the other #LIB_ITEM to compare the arc against.
  270. * @param aCompareFlags The flags used to perform the comparison.
  271. *
  272. * @return An integer value less than 0 if the object is less than \a aOther ojbect,
  273. * zero if the object is equal to \a aOther object, or greater than 0 if the
  274. * object is greater than \a aOther object.
  275. */
  276. virtual int compare( const LIB_ITEM& aOther,
  277. LIB_ITEM::COMPARE_FLAGS aCompareFlags = LIB_ITEM::COMPARE_FLAGS::NORMAL ) const;
  278. };
  279. #endif // _LIB_ITEM_H_