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.

374 lines
11 KiB

12 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013-2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef DS_DATA_ITEM_H
  25. #define DS_DATA_ITEM_H
  26. #include <math/vector2d.h>
  27. #include <eda_text.h>
  28. #include <bitmap_base.h>
  29. #include "drawing_sheet/ds_draw_item.h"
  30. class DS_DRAW_ITEM_TEXT; // Forward declaration
  31. #define TB_DEFAULT_TEXTSIZE 1.5 // default drawing sheet text size in mm
  32. namespace KIGFX
  33. {
  34. class VIEW;
  35. }
  36. /**
  37. * A coordinate is relative to a page corner.
  38. *
  39. * Any of the 4 corners can be a reference. The default is the right bottom corner.
  40. */
  41. enum CORNER_ANCHOR
  42. {
  43. RB_CORNER, // right bottom corner
  44. RT_CORNER, // right top corner
  45. LB_CORNER, // left bottom corner
  46. LT_CORNER, // left top corner
  47. };
  48. enum PAGE_OPTION
  49. {
  50. ALL_PAGES,
  51. FIRST_PAGE_ONLY,
  52. SUBSEQUENT_PAGES
  53. };
  54. /**
  55. * A coordinate point.
  56. *
  57. * The position is always relative to the corner anchor.
  58. *
  59. * @note The coordinate is from the anchor point to the opposite corner.
  60. */
  61. class POINT_COORD
  62. {
  63. public:
  64. POINT_COORD() { m_Anchor = RB_CORNER; }
  65. POINT_COORD( const VECTOR2D& aPos, enum CORNER_ANCHOR aAnchor = RB_CORNER )
  66. {
  67. m_Pos = aPos;
  68. m_Anchor = aAnchor;
  69. }
  70. VECTOR2D m_Pos;
  71. int m_Anchor;
  72. };
  73. /**
  74. * Drawing sheet structure type definitions.
  75. *
  76. * Basic items are:
  77. * * segment and rect (defined by 2 points)
  78. * * text (defined by a coordinate), the text and its justifications
  79. * * poly polygon defined by a coordinate, and a set of list of corners
  80. * ( because we use it for logos, there are more than one polygon
  81. * in this description
  82. */
  83. class DS_DATA_ITEM
  84. {
  85. public:
  86. enum DS_ITEM_TYPE {
  87. DS_TEXT,
  88. DS_SEGMENT,
  89. DS_RECT,
  90. DS_POLYPOLYGON,
  91. DS_BITMAP
  92. };
  93. DS_DATA_ITEM( DS_ITEM_TYPE aType );
  94. virtual ~DS_DATA_ITEM();
  95. const std::vector<DS_DRAW_ITEM_BASE*>& GetDrawItems() const { return m_drawItems; }
  96. virtual void SyncDrawItems( DS_DRAW_ITEM_LIST* aCollector, KIGFX::VIEW* aView );
  97. void SetStart( double aPosx, double aPosy, enum CORNER_ANCHOR aAnchor = RB_CORNER )
  98. {
  99. m_Pos.m_Pos.x = aPosx;
  100. m_Pos.m_Pos.y = aPosy;
  101. m_Pos.m_Anchor = aAnchor;
  102. }
  103. void SetEnd( double aPosx, double aPosy, enum CORNER_ANCHOR aAnchor = RB_CORNER )
  104. {
  105. m_End.m_Pos.x = aPosx;
  106. m_End.m_Pos.y = aPosy;
  107. m_End.m_Anchor = aAnchor;
  108. }
  109. DS_ITEM_TYPE GetType() const { return m_type; }
  110. /**
  111. * @return true if the item has a end point (segment; rect) of false (text, polygon).
  112. */
  113. PAGE_OPTION GetPage1Option() const { return m_pageOption; }
  114. void SetPage1Option( PAGE_OPTION aChoice ) { m_pageOption = aChoice; }
  115. // Coordinate handling
  116. const VECTOR2I GetStartPosIU( int ii = 0 ) const;
  117. const VECTOR2I GetEndPosIU( int ii = 0 ) const;
  118. const VECTOR2D GetStartPos( int ii = 0 ) const;
  119. const VECTOR2D GetEndPos( int ii = 0 ) const;
  120. virtual int GetPenSizeIU();
  121. /**
  122. * Move item to a new position.
  123. *
  124. * @param aPosition the new position of item, in mm.
  125. */
  126. void MoveTo( const VECTOR2D& aPosition );
  127. /**
  128. * Move item to a new position.
  129. *
  130. * @param aPosition the new position of the starting point in graphic units.
  131. */
  132. void MoveToIU( const VECTOR2I& aPosition );
  133. /**
  134. * Move the starting point of the item to a new position.
  135. *
  136. * @param aPosition the new position of the starting point, in mm.
  137. */
  138. void MoveStartPointTo( const VECTOR2D& aPosition );
  139. /**
  140. * Move the starting point of the item to a new position.
  141. *
  142. * @param aPosition is the new position of item in graphic units.
  143. */
  144. void MoveStartPointToIU( const VECTOR2I& aPosition );
  145. /**
  146. * Move the ending point of the item to a new position.
  147. *
  148. * This has meaning only for items defined by 2 points (segments and rectangles).
  149. *
  150. * @param aPosition is the new position of the ending point, in mm.
  151. */
  152. void MoveEndPointTo( const VECTOR2D& aPosition );
  153. /**
  154. * Move the ending point of the item to a new position.
  155. *
  156. * This has meaning only for items defined by 2 points (segments and rectangles).
  157. *
  158. * @param aPosition is the new position of the ending point in graphic units
  159. */
  160. void MoveEndPointToIU( const VECTOR2I& aPosition );
  161. /**
  162. * @return true if the item is inside the rectangle defined by the 4 corners, false otherwise.
  163. */
  164. virtual bool IsInsidePage( int ii ) const;
  165. const wxString GetClassName() const;
  166. wxString m_Name; // a name used in drawing sheet editor to identify items
  167. wxString m_Info; // a comment, only useful in drawing sheet editor
  168. POINT_COORD m_Pos;
  169. POINT_COORD m_End;
  170. double m_LineWidth;
  171. int m_RepeatCount; // repeat count for duplicate items
  172. VECTOR2D m_IncrementVector; // for duplicate items: move vector for position increment
  173. int m_IncrementLabel;
  174. protected:
  175. DS_ITEM_TYPE m_type;
  176. PAGE_OPTION m_pageOption;
  177. std::vector<DS_DRAW_ITEM_BASE*> m_drawItems;
  178. };
  179. class DS_DATA_ITEM_POLYGONS : public DS_DATA_ITEM
  180. {
  181. public:
  182. DS_DATA_ITEM_POLYGONS( );
  183. void SyncDrawItems( DS_DRAW_ITEM_LIST* aCollector, KIGFX::VIEW* aView ) override;
  184. virtual int GetPenSizeIU() override;
  185. /**
  186. * Add a corner in corner list.
  187. *
  188. * @param aCorner is the item to append.
  189. */
  190. void AppendCorner( const VECTOR2D& aCorner )
  191. {
  192. m_Corners.push_back( aCorner );
  193. }
  194. /**
  195. * Close the current contour, by storing the index of the last corner of the current
  196. * polygon in m_polyIndexEnd.
  197. */
  198. void CloseContour()
  199. {
  200. m_polyIndexEnd.push_back( m_Corners.size() -1 );
  201. }
  202. /**
  203. * @return the count of contours in the poly polygon.
  204. */
  205. int GetPolyCount() const { return (int) m_polyIndexEnd.size(); }
  206. /**
  207. * @param aContour is the index of the contour.
  208. * @return the index of the first corner of the contour \a aCountour.
  209. */
  210. unsigned GetPolyIndexStart( unsigned aContour ) const
  211. {
  212. if( aContour == 0 )
  213. return 0;
  214. else
  215. return m_polyIndexEnd[aContour-1] + 1;
  216. }
  217. /**
  218. * @param aContour is the index of the contour.
  219. * @return the index of the last corner of the contour \a aCountour.
  220. */
  221. unsigned GetPolyIndexEnd( unsigned aContour ) const
  222. {
  223. return m_polyIndexEnd[aContour];
  224. }
  225. /**
  226. * @return the coordinate (in mm) of the corner \a aIdx and the repeated item \a aRepeat
  227. */
  228. const VECTOR2D GetCornerPosition( unsigned aIdx, int aRepeat = 0 ) const;
  229. /**
  230. * @return the coordinate (in draw/plot units) of the corner \a aIdx and the repeated
  231. * item \a aRepeat
  232. */
  233. const VECTOR2I GetCornerPositionIU( unsigned aIdx, int aRepeat = 0 ) const;
  234. /**
  235. * Calculate the bounding box of the set polygons.
  236. */
  237. void SetBoundingBox();
  238. bool IsInsidePage( int ii ) const override;
  239. EDA_ANGLE m_Orient; // Orientation
  240. std::vector<VECTOR2D> m_Corners; // corner list
  241. private:
  242. std::vector<unsigned> m_polyIndexEnd; // index of the last point of each polygon
  243. VECTOR2D m_minCoord; // min coord of corners, relative to m_Pos
  244. VECTOR2D m_maxCoord; // max coord of corners, relative to m_Pos
  245. };
  246. class DS_DATA_ITEM_TEXT : public DS_DATA_ITEM
  247. {
  248. public:
  249. DS_DATA_ITEM_TEXT( const wxString& aTextBase );
  250. void SyncDrawItems( DS_DRAW_ITEM_LIST* aCollector, KIGFX::VIEW* aView ) override;
  251. virtual int GetPenSizeIU() override;
  252. /**
  253. * Try to build text which is an increment of m_TextBase
  254. * has meaning only if m_TextBase is a basic text (one char)
  255. * If the basic char is a digit, build a number
  256. * If the basic char is a letter, use the letter with ASCII code
  257. * aIncr + (basic char ascc code)
  258. * @param aIncr = the increment value
  259. * return the incremented label in m_FullText
  260. */
  261. void IncrementLabel( int aIncr );
  262. /**
  263. * Calculate m_ConstrainedTextSize from m_TextSize
  264. * to keep the X size and the full Y size of the text
  265. * smaller than m_BoundingBoxSize
  266. * if m_BoundingBoxSize.x or m_BoundingBoxSize.y > 0
  267. * if m_BoundingBoxSize.x or m_BoundingBoxSize.y == 0
  268. * the corresponding text size is not constrained
  269. */
  270. void SetConstrainedTextSize();
  271. /**
  272. * Replace the '\''n' sequence by EOL and the sequence '\''\' by only one '\'
  273. * inside m_FullText
  274. * @return true if the EOL symbol is found or is inserted (multiline text).
  275. */
  276. bool ReplaceAntiSlashSequence();
  277. public:
  278. wxString m_TextBase; // The basic text, with format symbols
  279. wxString m_FullText; // The expanded text, shown on screen
  280. double m_Orient; // Orientation in degrees
  281. GR_TEXT_H_ALIGN_T m_Hjustify;
  282. GR_TEXT_V_ALIGN_T m_Vjustify;
  283. bool m_Italic;
  284. bool m_Bold;
  285. KIFONT::FONT* m_Font;
  286. VECTOR2D m_TextSize;
  287. KIGFX::COLOR4D m_TextColor;
  288. VECTOR2D m_BoundingBoxSize; // When not null, this is the max size of the
  289. // full text. The text size will be modified
  290. // to keep the full text inside this bound.
  291. VECTOR2D m_ConstrainedTextSize; // Actual text size, if constrained by
  292. // the m_BoundingBoxSize constraint
  293. };
  294. class BITMAP_BASE;
  295. class DS_DATA_ITEM_BITMAP : public DS_DATA_ITEM
  296. {
  297. public:
  298. DS_DATA_ITEM_BITMAP( BITMAP_BASE* aImage ) :
  299. DS_DATA_ITEM( DS_BITMAP )
  300. {
  301. m_ImageBitmap = aImage;
  302. }
  303. void SyncDrawItems( DS_DRAW_ITEM_LIST* aCollector, KIGFX::VIEW* aView ) override;
  304. int GetPPI() const;
  305. void SetPPI( int aBitmapPPI );
  306. public:
  307. BITMAP_BASE* m_ImageBitmap;
  308. };
  309. #endif // DS_DATA_ITEM_H