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.

510 lines
16 KiB

13 years ago
  1. /**
  2. * @file class_worksheet_dataitem.h
  3. * @brief description of graphic items and texts to build a title block
  4. */
  5. #ifndef CLASS_WORKSHEET_DATA_ITEM_H
  6. #define CLASS_WORKSHEET_DATA_ITEM_H
  7. #include <math/vector2d.h>
  8. #include <eda_text.h>
  9. #include <class_bitmap_base.h>
  10. class WS_DRAW_ITEM_TEXT; // Forward declaration
  11. #define TB_DEFAULT_TEXTSIZE 1.5 // default worksheet text size in mm
  12. // Text attributes set in m_flags (ORed bits)
  13. #define USE_BOLD 1 // has meaning for texts
  14. #define USE_THICK_LINE 1 // equivalent to bold for lines
  15. #define USE_ITALIC (1<<1) // has meaning for texts
  16. #define USE_ALT_COLOR (1<<2)
  17. #define SELECTED_STATE (1<<3) // When set, use the hight light color to draw item
  18. #define NEW_ITEM (1<<4) // Set for new items which can be deleted
  19. // by an abort command
  20. #define LOCATE_STARTPOINT (1<<5) // Used in locate function:set by locate function
  21. // if the start point is located
  22. #define LOCATE_ENDPOINT (1<<6) // Used in locate function:set by locate function
  23. // if the end point is located
  24. #define PAGE1OPTION (3<<7) // flag to manage items drawn or not drawn only
  25. // on page 1: NONE = item on all pages
  26. #define PAGE1OPTION_NONE (0<<7) // NONE = item on all pages
  27. #define PAGE1OPTION_PAGE1ONLY (1<<7) // = item only on page 1
  28. #define PAGE1OPTION_NOTONPAGE1 (2<<7) // = item on all pages but page 1
  29. // A coordinate is relative to a page corner.
  30. // Any of the 4 corners can be a reference.
  31. // The default is the right bottom corner
  32. enum corner_anchor
  33. {
  34. RB_CORNER, // right bottom corner
  35. RT_CORNER, // right top corner
  36. LB_CORNER, // left bottom corner
  37. LT_CORNER, // left top corner
  38. };
  39. // a coordinate point
  40. // The position is always relative to the corner anchor
  41. // Note the coordinate is from the anchor point
  42. // to the opposite corner.
  43. class POINT_COORD
  44. {
  45. public:
  46. DPOINT m_Pos;
  47. int m_Anchor;
  48. public:
  49. POINT_COORD() { m_Anchor = RB_CORNER; }
  50. POINT_COORD( DPOINT aPos, enum corner_anchor aAnchor = RB_CORNER )
  51. {
  52. m_Pos = aPos;
  53. m_Anchor = aAnchor;
  54. }
  55. };
  56. // Work sheet structure type definitions.
  57. // Basic items are:
  58. // * segment and rect (defined by 2 points)
  59. // * text (defined by a coordinate), the text and its justifications
  60. // * poly polygon defined by a coordinate, and a set of list of corners
  61. // ( because we use it for logos, there are more than one polygon
  62. // in this description
  63. class WORKSHEET_DATAITEM
  64. {
  65. public:
  66. enum WS_ItemType {
  67. WS_TEXT,
  68. WS_SEGMENT,
  69. WS_RECT,
  70. WS_POLYPOLYGON,
  71. WS_BITMAP
  72. };
  73. protected:
  74. WS_ItemType m_type;
  75. int m_flags;
  76. public:
  77. wxString m_Name; // a item name used in page layout
  78. // editor to identify items
  79. wxString m_Info; // a comment, only useful in page
  80. // layout editor
  81. POINT_COORD m_Pos;
  82. POINT_COORD m_End;
  83. double m_LineWidth;
  84. int m_RepeatCount; // repeat count for duplicate items
  85. DPOINT m_IncrementVector; // For duplicate items: move vector
  86. // for position increment
  87. int m_IncrementLabel;
  88. // These variables are static, because these values are common to all
  89. // instances of WORKSHEET_DATAITEM.
  90. // They are default or common values.
  91. static double m_WSunits2Iu; // conversion factor between
  92. // ws units (mils) and draw/plot units
  93. static DPOINT m_RB_Corner; // cordinates of the right bottom corner
  94. // (ws units)
  95. static DPOINT m_LT_Corner; // cordinates of the left top corner
  96. // (ws units)
  97. static double m_DefaultLineWidth; // Default line width,
  98. // when not defined inside a line
  99. // or a rect
  100. static DSIZE m_DefaultTextSize; // Default text size,
  101. // when not defined inside a tbtext
  102. static double m_DefaultTextThickness;// Default text thickness,
  103. // when not defined inside a tbtext
  104. static bool m_SpecialMode; // Used in page layout editor
  105. // When set to true, base texts
  106. // instead of full texts are displayed
  107. static EDA_COLOR_T m_Color; // the default color to draw items
  108. static EDA_COLOR_T m_AltColor; // an alternate color to draw items
  109. static EDA_COLOR_T m_SelectedColor; // the color to draw selected items
  110. // (used in page layout editor
  111. public:
  112. WORKSHEET_DATAITEM( WS_ItemType aType );
  113. virtual ~WORKSHEET_DATAITEM() {}
  114. void SetStart( double aPosx, double aPosy, enum corner_anchor aAnchor = RB_CORNER )
  115. {
  116. m_Pos.m_Pos.x = aPosx;
  117. m_Pos.m_Pos.y = aPosy;
  118. m_Pos.m_Anchor = aAnchor;
  119. }
  120. void SetEnd( double aPosx, double aPosy, enum corner_anchor aAnchor = RB_CORNER )
  121. {
  122. m_End.m_Pos.x = aPosx;
  123. m_End.m_Pos.y = aPosy;
  124. m_End.m_Anchor = aAnchor;
  125. }
  126. // Accessors:
  127. WS_ItemType GetType() const { return m_type; }
  128. int GetFlags() const { return m_flags; }
  129. void SetFlags( int aMask ) { m_flags |= aMask; }
  130. void ClearFlags( int aMask ) { m_flags &= ~aMask; }
  131. /**
  132. * @return true if the item has a end point (segment; rect)
  133. * of false (text, polugon)
  134. */
  135. virtual bool HasEndPoint() { return true; }
  136. /**
  137. * @return 0 if the item has no specific option for page 1
  138. * 1 if the item is only on page 1
  139. * -1 if the item is not on page 1
  140. */
  141. int GetPage1Option();
  142. /**
  143. * Set the option for page 1
  144. * @param aChoice = 0 if the item has no specific option for page 1
  145. * > 0 if the item is only on page 1
  146. * < 0 if the item is not on page 1
  147. */
  148. void SetPage1Option( int aChoice );
  149. // Coordinate handling
  150. const wxPoint GetStartPosUi( int ii = 0 ) const;
  151. const wxPoint GetEndPosUi( int ii = 0 ) const;
  152. const DPOINT GetStartPos( int ii = 0 ) const;
  153. const DPOINT GetEndPos( int ii = 0 ) const;
  154. virtual int GetPenSizeUi()
  155. {
  156. if( m_LineWidth )
  157. return KiROUND( m_LineWidth * m_WSunits2Iu );
  158. else
  159. return KiROUND( m_DefaultLineWidth * m_WSunits2Iu );
  160. }
  161. static int GetMarkerSizeUi()
  162. {
  163. return KiROUND( 0.5 * m_WSunits2Iu );
  164. }
  165. /**
  166. * move item to a new position
  167. * @param aPosition = the new position of item, in mm
  168. */
  169. void MoveTo( DPOINT aPosition );
  170. /**
  171. * move item to a new position
  172. * @param aPosition = the new position of the starting point in graphic units
  173. */
  174. void MoveToUi( wxPoint aPosition );
  175. /**
  176. * move the starting point of the item to a new position
  177. * @param aPosition = the new position of the starting point, in mm
  178. */
  179. void MoveStartPointTo( DPOINT aPosition );
  180. /**
  181. * move the starting point of the item to a new position
  182. * @param aPosition = the new position of item in graphic units
  183. */
  184. void MoveStartPointToUi( wxPoint aPosition );
  185. /**
  186. * move the ending point of the item to a new position
  187. * has meaning only for items defined by 2 points
  188. * (segments and rectangles)
  189. * @param aPosition = the new position of the ending point, in mm
  190. */
  191. void MoveEndPointTo( DPOINT aPosition );
  192. /**
  193. * move the ending point of the item to a new position
  194. * has meaning only for items defined by 2 points
  195. * (segments and rectangles)
  196. * @param aPosition = the new position of the ending point in graphic units
  197. */
  198. void MoveEndPointToUi( wxPoint aPosition );
  199. /**
  200. * @return true if the item is inside the rectangle defined by the
  201. * 4 corners, false otherwise.
  202. */
  203. virtual bool IsInsidePage( int ii ) const;
  204. const wxString GetClassName() const;
  205. /**
  206. * @return true if the selected state on ON
  207. */
  208. bool IsSelected() { return (m_flags & SELECTED_STATE) != 0; }
  209. /**
  210. * Function SetSelected
  211. * Toggles on/off the selected flag (used in edition functions
  212. * @param aState = the flag value
  213. */
  214. void SetSelected( bool aState )
  215. {
  216. if( aState )
  217. m_flags |= SELECTED_STATE;
  218. else
  219. m_flags &= ~SELECTED_STATE;
  220. }
  221. bool UseAltColor() {return m_flags & USE_ALT_COLOR; }
  222. EDA_COLOR_T GetItemColor()
  223. {
  224. if( IsSelected() )
  225. return m_SelectedColor;
  226. if( UseAltColor() )
  227. return m_AltColor;
  228. return m_Color;
  229. }
  230. };
  231. class WORKSHEET_DATAITEM_POLYPOLYGON : public WORKSHEET_DATAITEM
  232. {
  233. public:
  234. double m_Orient; // Orientation in degrees
  235. std::vector<DPOINT> m_Corners; // corner list
  236. private:
  237. std::vector<unsigned> m_polyIndexEnd; // index of the last point of each polygon
  238. DPOINT m_minCoord; // min coord of corners, relative to m_Pos
  239. DPOINT m_maxCoord; // max coord of corners, relative to m_Pos
  240. public:
  241. WORKSHEET_DATAITEM_POLYPOLYGON( );
  242. virtual int GetPenSizeUi()
  243. {
  244. return KiROUND( m_LineWidth * m_WSunits2Iu );
  245. }
  246. /**
  247. * @return false (no end point)
  248. */
  249. virtual bool HasEndPoint() { return false; };
  250. /**
  251. * add a corner in corner list
  252. * @param aCorner: the item to append
  253. */
  254. void AppendCorner( const DPOINT& aCorner )
  255. {
  256. m_Corners.push_back( aCorner );
  257. }
  258. /**
  259. * Closes the current contour, by storing the index of the last corner
  260. * of the current polygon in m_polyIndexEnd.
  261. */
  262. void CloseContour()
  263. {
  264. m_polyIndexEnd.push_back( m_Corners.size() -1 );
  265. }
  266. /**
  267. * @return the count of contours in the poly polygon
  268. */
  269. int GetPolyCount() const { return (int) m_polyIndexEnd.size(); }
  270. /**
  271. * @return the index of the first corner of the contour aCountour
  272. * @param aContour = the index of the contour
  273. */
  274. unsigned GetPolyIndexStart( unsigned aContour) const
  275. {
  276. if( aContour == 0 )
  277. return 0;
  278. else
  279. return m_polyIndexEnd[aContour-1] + 1;
  280. }
  281. /**
  282. * @return the index of the last corner of the contour aCountour
  283. * @param aContour = the index of the contour
  284. */
  285. unsigned GetPolyIndexEnd( unsigned aContour) const
  286. {
  287. return m_polyIndexEnd[aContour];
  288. }
  289. /**
  290. * @return the coordinate (in mm) of the corner aIdx,
  291. * for the repeated item aRepeat
  292. */
  293. const DPOINT GetCornerPosition( unsigned aIdx, int aRepeat = 0 ) const;
  294. /**
  295. * @return the coordinate (in draw/plot units) of the corner aIdx,
  296. * for the repeated item aRepeat
  297. */
  298. const wxPoint GetCornerPositionUi( unsigned aIdx, int aRepeat = 0 ) const;
  299. /**
  300. * calculate the bounding box of the set polygons
  301. */
  302. void SetBoundingBox();
  303. bool IsInsidePage( int ii ) const;
  304. };
  305. class WORKSHEET_DATAITEM_TEXT : public WORKSHEET_DATAITEM
  306. {
  307. public:
  308. wxString m_TextBase; // The basic text, with format symbols
  309. wxString m_FullText; // The expanded text, shown on screen
  310. double m_Orient; // Orientation in degrees
  311. enum EDA_TEXT_HJUSTIFY_T m_Hjustify;
  312. enum EDA_TEXT_VJUSTIFY_T m_Vjustify;
  313. DSIZE m_TextSize;
  314. DSIZE m_BoundingBoxSize; // When not null, this is the max
  315. // size of the full text.
  316. // the text size will be modified
  317. // to keep the full text insite this
  318. // bound.
  319. DSIZE m_ConstrainedTextSize;// Actual text size, if constrained by
  320. // the m_BoundingBoxSize constraint
  321. public:
  322. WORKSHEET_DATAITEM_TEXT( const wxChar* aTextBase );
  323. /**
  324. * @return false (no end point)
  325. */
  326. virtual bool HasEndPoint() { return false; };
  327. virtual int GetPenSizeUi()
  328. {
  329. if( m_LineWidth )
  330. return KiROUND( m_LineWidth * m_WSunits2Iu );
  331. else
  332. return KiROUND( m_DefaultTextThickness * m_WSunits2Iu );
  333. }
  334. /**
  335. * move item to a new position
  336. * @param aPosition = the new position of item
  337. */
  338. void MoveTo( DPOINT aPosition );
  339. /**
  340. * transfert the text justification and orientation
  341. * to aGText
  342. */
  343. void TransfertSetupToGraphicText( WS_DRAW_ITEM_TEXT* aGText );
  344. /**
  345. * Try to build text wihich is an increment of m_TextBase
  346. * has meaning only if m_TextBase is a basic text (one char)
  347. * If the basic char is a digit, build a number
  348. * If the basic char is a letter, use the letter with ascii code
  349. * aIncr + (basic char ascc code)
  350. * @param aIncr = the increment value
  351. * return the incremented label in m_FullText
  352. */
  353. void IncrementLabel( int aIncr );
  354. /**
  355. * Calculates m_ConstrainedTextSize from m_TextSize
  356. * to keep the X size and the full Y size of the text
  357. * smaller than m_BoundingBoxSize
  358. * if m_BoundingBoxSize.x or m_BoundingBoxSize.y > 0
  359. * if m_BoundingBoxSize.x or m_BoundingBoxSize.y == 0
  360. * the corresponding text size is not constrained
  361. */
  362. void SetConstrainedTextSize();
  363. /** Replace the '\''n' sequence by EOL
  364. * and the sequence '\''\' by only one '\'
  365. * inside m_FullText
  366. * @return true if the EOL symbol is found or is inserted (multiline text)
  367. */
  368. bool ReplaceAntiSlashSequence();
  369. /**
  370. * @return true is a bold font should be selected
  371. */
  372. bool IsBold() { return (m_flags & USE_BOLD) != 0; }
  373. /**
  374. * Function SetBold
  375. * Toggles on/off the bold option flag
  376. * @param aState = the bold option value
  377. */
  378. void SetBold( bool aState )
  379. {
  380. if( aState )
  381. m_flags |= USE_BOLD;
  382. else
  383. m_flags &= ~USE_BOLD;
  384. }
  385. /**
  386. * @return true is an italic font should be selected
  387. */
  388. bool IsItalic() const { return (m_flags & USE_ITALIC) != 0; }
  389. /**
  390. * Function SetItalic
  391. * Toggles on/off the italic option flag
  392. * @param aState = the italic option value
  393. */
  394. void SetItalic( bool aState )
  395. {
  396. if( aState )
  397. m_flags |= USE_ITALIC;
  398. else
  399. m_flags &= ~USE_ITALIC;
  400. }
  401. };
  402. class BITMAP_BASE;
  403. class WORKSHEET_DATAITEM_BITMAP : public WORKSHEET_DATAITEM
  404. {
  405. public:
  406. BITMAP_BASE* m_ImageBitmap;
  407. public:
  408. WORKSHEET_DATAITEM_BITMAP(BITMAP_BASE* aImage)
  409. : WORKSHEET_DATAITEM( WS_BITMAP )
  410. {
  411. m_ImageBitmap = aImage;
  412. }
  413. /**
  414. * @return false (no end point)
  415. */
  416. virtual bool HasEndPoint() { return false; }
  417. /**
  418. * @return the PPI of the bitmap
  419. */
  420. int GetPPI() const;
  421. /**
  422. * adjust the PPI of the bitmap
  423. * @param aBitmapPPI = the ned PPI for the bitmap
  424. */
  425. void SetPPI( int aBitmapPPI );
  426. /**
  427. * set the pixel scale factor of the bitmap
  428. * this factor depend on the application internal unit
  429. * and the pixel per inch bitmap factor
  430. * the pixel scale factor is the pixel size to application internal unit
  431. * and should be initialized before printing or drawing the bitmap
  432. */
  433. void SetPixelScaleFactor();
  434. };
  435. #endif // CLASS_WORKSHEET_DATA_ITEM_H