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.

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