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.

537 lines
17 KiB

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