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.

382 lines
11 KiB

12 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-2019 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. /**
  25. * @file worksheet_dataitem.h
  26. * @brief description of graphic items and texts to build a title block
  27. */
  28. #ifndef WORKSHEET_DATA_ITEM_H
  29. #define WORKSHEET_DATA_ITEM_H
  30. #include <math/vector2d.h>
  31. #include <eda_text.h>
  32. #include <bitmap_base.h>
  33. #include <view/view.h>
  34. #include "ws_draw_item.h"
  35. class WS_DRAW_ITEM_TEXT; // Forward declaration
  36. #define TB_DEFAULT_TEXTSIZE 1.5 // default worksheet text size in mm
  37. /**
  38. * A coordinate is relative to a page corner.
  39. * Any of the 4 corners can be a reference.
  40. * The default is the right bottom corner
  41. */
  42. enum CORNER_ANCHOR
  43. {
  44. RB_CORNER, // right bottom corner
  45. RT_CORNER, // right top corner
  46. LB_CORNER, // left bottom corner
  47. LT_CORNER, // left top corner
  48. };
  49. enum PAGE_OPTION
  50. {
  51. ALL_PAGES,
  52. FIRST_PAGE_ONLY,
  53. SUBSEQUENT_PAGES
  54. };
  55. /**
  56. * A coordinate point
  57. * The position is always relative to the corner anchor
  58. * Note the coordinate is from the anchor point to the opposite corner.
  59. */
  60. class POINT_COORD
  61. {
  62. public:
  63. DPOINT m_Pos;
  64. int m_Anchor;
  65. public:
  66. POINT_COORD() { m_Anchor = RB_CORNER; }
  67. POINT_COORD( DPOINT aPos, enum CORNER_ANCHOR aAnchor = RB_CORNER )
  68. {
  69. m_Pos = aPos;
  70. m_Anchor = aAnchor;
  71. }
  72. };
  73. /**
  74. * Work sheet structure type definitions.
  75. * Basic items are:
  76. * * segment and rect (defined by 2 points)
  77. * * text (defined by a coordinate), the text and its justifications
  78. * * poly polygon defined by a coordinate, and a set of list of corners
  79. * ( because we use it for logos, there are more than one polygon
  80. * in this description
  81. */
  82. class WS_DATA_ITEM
  83. {
  84. public:
  85. enum WS_ITEM_TYPE {
  86. WS_TEXT,
  87. WS_SEGMENT,
  88. WS_RECT,
  89. WS_POLYPOLYGON,
  90. WS_BITMAP
  91. };
  92. protected:
  93. WS_ITEM_TYPE m_type;
  94. PAGE_OPTION m_pageOption;
  95. std::vector<WS_DRAW_ITEM_BASE*> m_drawItems;
  96. public:
  97. wxString m_Name; // a item name used in page layout
  98. // editor to identify items
  99. wxString m_Info; // a comment, only useful in page
  100. // layout editor
  101. POINT_COORD m_Pos;
  102. POINT_COORD m_End;
  103. double m_LineWidth;
  104. int m_RepeatCount; // repeat count for duplicate items
  105. DPOINT m_IncrementVector; // For duplicate items: move vector
  106. // for position increment
  107. int m_IncrementLabel;
  108. public:
  109. WS_DATA_ITEM( WS_ITEM_TYPE aType );
  110. virtual ~WS_DATA_ITEM();
  111. const std::vector<WS_DRAW_ITEM_BASE*>& GetDrawItems() const { return m_drawItems; }
  112. virtual void SyncDrawItems( WS_DRAW_ITEM_LIST* aCollector, KIGFX::VIEW* aView );
  113. void SetStart( double aPosx, double aPosy, enum CORNER_ANCHOR aAnchor = RB_CORNER )
  114. {
  115. m_Pos.m_Pos.x = aPosx;
  116. m_Pos.m_Pos.y = aPosy;
  117. m_Pos.m_Anchor = aAnchor;
  118. }
  119. void SetEnd( double aPosx, double aPosy, enum CORNER_ANCHOR aAnchor = RB_CORNER )
  120. {
  121. m_End.m_Pos.x = aPosx;
  122. m_End.m_Pos.y = aPosy;
  123. m_End.m_Anchor = aAnchor;
  124. }
  125. // Accessors:
  126. WS_ITEM_TYPE GetType() const { return m_type; }
  127. /**
  128. * @return true if the item has a end point (segment; rect)
  129. * of false (text, polugon)
  130. */
  131. PAGE_OPTION GetPage1Option() const { return m_pageOption; }
  132. void SetPage1Option( PAGE_OPTION aChoice ) { m_pageOption = aChoice; }
  133. // Coordinate handling
  134. const wxPoint GetStartPosUi( int ii = 0 ) const;
  135. const wxPoint GetEndPosUi( int ii = 0 ) const;
  136. const DPOINT GetStartPos( int ii = 0 ) const;
  137. const DPOINT GetEndPos( int ii = 0 ) const;
  138. virtual int GetPenSizeUi();
  139. /**
  140. * move item to a new position
  141. * @param aPosition = the new position of item, in mm
  142. */
  143. void MoveTo( DPOINT aPosition );
  144. /**
  145. * move item to a new position
  146. * @param aPosition = the new position of the starting point in graphic units
  147. */
  148. void MoveToUi( wxPoint aPosition );
  149. /**
  150. * move the starting point of the item to a new position
  151. * @param aPosition = the new position of the starting point, in mm
  152. */
  153. void MoveStartPointTo( DPOINT aPosition );
  154. /**
  155. * move the starting point of the item to a new position
  156. * @param aPosition = the new position of item in graphic units
  157. */
  158. void MoveStartPointToUi( wxPoint aPosition );
  159. /**
  160. * move the ending point of the item to a new position
  161. * has meaning only for items defined by 2 points
  162. * (segments and rectangles)
  163. * @param aPosition = the new position of the ending point, in mm
  164. */
  165. void MoveEndPointTo( DPOINT aPosition );
  166. /**
  167. * move the ending point of the item to a new position
  168. * has meaning only for items defined by 2 points
  169. * (segments and rectangles)
  170. * @param aPosition = the new position of the ending point in graphic units
  171. */
  172. void MoveEndPointToUi( wxPoint aPosition );
  173. /**
  174. * @return true if the item is inside the rectangle defined by the
  175. * 4 corners, false otherwise.
  176. */
  177. virtual bool IsInsidePage( int ii ) const;
  178. const wxString GetClassName() const;
  179. };
  180. class WS_DATA_ITEM_POLYGONS : public WS_DATA_ITEM
  181. {
  182. public:
  183. double m_Orient; // Orientation in degrees
  184. std::vector<DPOINT> m_Corners; // corner list
  185. private:
  186. std::vector<unsigned> m_polyIndexEnd; // index of the last point of each polygon
  187. DPOINT m_minCoord; // min coord of corners, relative to m_Pos
  188. DPOINT m_maxCoord; // max coord of corners, relative to m_Pos
  189. public:
  190. WS_DATA_ITEM_POLYGONS( );
  191. void SyncDrawItems( WS_DRAW_ITEM_LIST* aCollector, KIGFX::VIEW* aView ) override;
  192. virtual int GetPenSizeUi() override;
  193. /**
  194. * add a corner in corner list
  195. * @param aCorner: the item to append
  196. */
  197. void AppendCorner( const DPOINT& aCorner )
  198. {
  199. m_Corners.push_back( aCorner );
  200. }
  201. /**
  202. * Closes the current contour, by storing the index of the last corner
  203. * of the current polygon in m_polyIndexEnd.
  204. */
  205. void CloseContour()
  206. {
  207. m_polyIndexEnd.push_back( m_Corners.size() -1 );
  208. }
  209. /**
  210. * @return the count of contours in the poly polygon
  211. */
  212. int GetPolyCount() const { return (int) m_polyIndexEnd.size(); }
  213. /**
  214. * @return the index of the first corner of the contour aCountour
  215. * @param aContour = the index of the contour
  216. */
  217. unsigned GetPolyIndexStart( unsigned aContour) const
  218. {
  219. if( aContour == 0 )
  220. return 0;
  221. else
  222. return m_polyIndexEnd[aContour-1] + 1;
  223. }
  224. /**
  225. * @return the index of the last corner of the contour aCountour
  226. * @param aContour = the index of the contour
  227. */
  228. unsigned GetPolyIndexEnd( unsigned aContour) const
  229. {
  230. return m_polyIndexEnd[aContour];
  231. }
  232. /**
  233. * @return the coordinate (in mm) of the corner aIdx,
  234. * for the repeated item aRepeat
  235. */
  236. const DPOINT GetCornerPosition( unsigned aIdx, int aRepeat = 0 ) const;
  237. /**
  238. * @return the coordinate (in draw/plot units) of the corner aIdx,
  239. * for the repeated item aRepeat
  240. */
  241. const wxPoint GetCornerPositionUi( unsigned aIdx, int aRepeat = 0 ) const;
  242. /**
  243. * calculate the bounding box of the set polygons
  244. */
  245. void SetBoundingBox();
  246. bool IsInsidePage( int ii ) const override;
  247. };
  248. class WS_DATA_ITEM_TEXT : public WS_DATA_ITEM
  249. {
  250. public:
  251. wxString m_TextBase; // The basic text, with format symbols
  252. wxString m_FullText; // The expanded text, shown on screen
  253. double m_Orient; // Orientation in degrees
  254. EDA_TEXT_HJUSTIFY_T m_Hjustify;
  255. EDA_TEXT_VJUSTIFY_T m_Vjustify;
  256. bool m_Italic;
  257. bool m_Bold;
  258. DSIZE m_TextSize;
  259. DSIZE m_BoundingBoxSize; // When not null, this is the max
  260. // size of the full text.
  261. // the text size will be modified
  262. // to keep the full text insite this
  263. // bound.
  264. DSIZE m_ConstrainedTextSize; // Actual text size, if constrained by
  265. // the m_BoundingBoxSize constraint
  266. public:
  267. WS_DATA_ITEM_TEXT( const wxString& aTextBase );
  268. void SyncDrawItems( WS_DRAW_ITEM_LIST* aCollector, KIGFX::VIEW* aView ) override;
  269. virtual int GetPenSizeUi() override;
  270. /**
  271. * move item to a new position
  272. * @param aPosition = the new position of item
  273. */
  274. void MoveTo( DPOINT aPosition );
  275. /**
  276. * Try to build text wihich is an increment of m_TextBase
  277. * has meaning only if m_TextBase is a basic text (one char)
  278. * If the basic char is a digit, build a number
  279. * If the basic char is a letter, use the letter with ascii code
  280. * aIncr + (basic char ascc code)
  281. * @param aIncr = the increment value
  282. * return the incremented label in m_FullText
  283. */
  284. void IncrementLabel( int aIncr );
  285. /**
  286. * Calculates m_ConstrainedTextSize from m_TextSize
  287. * to keep the X size and the full Y size of the text
  288. * smaller than m_BoundingBoxSize
  289. * if m_BoundingBoxSize.x or m_BoundingBoxSize.y > 0
  290. * if m_BoundingBoxSize.x or m_BoundingBoxSize.y == 0
  291. * the corresponding text size is not constrained
  292. */
  293. void SetConstrainedTextSize();
  294. /** Replace the '\''n' sequence by EOL
  295. * and the sequence '\''\' by only one '\'
  296. * inside m_FullText
  297. * @return true if the EOL symbol is found or is inserted (multiline text)
  298. */
  299. bool ReplaceAntiSlashSequence();
  300. };
  301. class BITMAP_BASE;
  302. class WS_DATA_ITEM_BITMAP : public WS_DATA_ITEM
  303. {
  304. public:
  305. BITMAP_BASE* m_ImageBitmap;
  306. public:
  307. WS_DATA_ITEM_BITMAP(BITMAP_BASE* aImage)
  308. : WS_DATA_ITEM( WS_BITMAP )
  309. {
  310. m_ImageBitmap = aImage;
  311. }
  312. void SyncDrawItems( WS_DRAW_ITEM_LIST* aCollector, KIGFX::VIEW* aView ) override;
  313. int GetPPI() const;
  314. void SetPPI( int aBitmapPPI );
  315. };
  316. #endif // WORKSHEET_DATA_ITEM_H