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.

344 lines
12 KiB

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