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.

543 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 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. 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 COLOR4D m_Color; // the default color to draw items
  131. static COLOR4D m_AltColor; // an alternate color to draw items
  132. static COLOR4D 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. /** @return the size of markers used in page layout editor to draw
  185. * the anchor points of selected items.
  186. * @param aZoomScale is a scaling factor that can be used to adjust
  187. * the final marker size depending on zoom level
  188. */
  189. static int GetMarkerSizeUi( double aZoomScale = 1.0 )
  190. {
  191. #define MARKER_DRAW_SIZE 0.5 // Is a value choosen for a suitable size on screen
  192. return KiROUND( MARKER_DRAW_SIZE * m_WSunits2Iu * aZoomScale );
  193. }
  194. /**
  195. * move item to a new position
  196. * @param aPosition = the new position of item, in mm
  197. */
  198. void MoveTo( DPOINT aPosition );
  199. /**
  200. * move item to a new position
  201. * @param aPosition = the new position of the starting point in graphic units
  202. */
  203. void MoveToUi( wxPoint aPosition );
  204. /**
  205. * move the starting point of the item to a new position
  206. * @param aPosition = the new position of the starting point, in mm
  207. */
  208. void MoveStartPointTo( DPOINT aPosition );
  209. /**
  210. * move the starting point of the item to a new position
  211. * @param aPosition = the new position of item in graphic units
  212. */
  213. void MoveStartPointToUi( wxPoint aPosition );
  214. /**
  215. * move the ending point of the item to a new position
  216. * has meaning only for items defined by 2 points
  217. * (segments and rectangles)
  218. * @param aPosition = the new position of the ending point, in mm
  219. */
  220. void MoveEndPointTo( DPOINT aPosition );
  221. /**
  222. * move the ending point of the item to a new position
  223. * has meaning only for items defined by 2 points
  224. * (segments and rectangles)
  225. * @param aPosition = the new position of the ending point in graphic units
  226. */
  227. void MoveEndPointToUi( wxPoint aPosition );
  228. /**
  229. * @return true if the item is inside the rectangle defined by the
  230. * 4 corners, false otherwise.
  231. */
  232. virtual bool IsInsidePage( int ii ) const;
  233. const wxString GetClassName() const;
  234. /**
  235. * @return true if the selected state on ON
  236. */
  237. bool IsSelected() { return (m_flags & SELECTED_STATE) != 0; }
  238. /**
  239. * Function SetSelected
  240. * Toggles on/off the selected flag (used in editing functions)
  241. * @param aState = the flag value
  242. */
  243. void SetSelected( bool aState )
  244. {
  245. if( aState )
  246. m_flags |= SELECTED_STATE;
  247. else
  248. m_flags &= ~SELECTED_STATE;
  249. }
  250. bool UseAltColor() {return m_flags & USE_ALT_COLOR; }
  251. COLOR4D GetItemColor()
  252. {
  253. if( IsSelected() )
  254. return m_SelectedColor;
  255. if( UseAltColor() )
  256. return m_AltColor;
  257. return m_Color;
  258. }
  259. };
  260. class WORKSHEET_DATAITEM_POLYPOLYGON : public WORKSHEET_DATAITEM
  261. {
  262. public:
  263. double m_Orient; // Orientation in degrees
  264. std::vector<DPOINT> m_Corners; // corner list
  265. private:
  266. std::vector<unsigned> m_polyIndexEnd; // index of the last point of each polygon
  267. DPOINT m_minCoord; // min coord of corners, relative to m_Pos
  268. DPOINT m_maxCoord; // max coord of corners, relative to m_Pos
  269. public:
  270. WORKSHEET_DATAITEM_POLYPOLYGON( );
  271. virtual int GetPenSizeUi() override
  272. {
  273. return KiROUND( m_LineWidth * m_WSunits2Iu );
  274. }
  275. /**
  276. * @return false (no end point)
  277. */
  278. virtual bool HasEndPoint() override { return false; }
  279. /**
  280. * add a corner in corner list
  281. * @param aCorner: the item to append
  282. */
  283. void AppendCorner( const DPOINT& aCorner )
  284. {
  285. m_Corners.push_back( aCorner );
  286. }
  287. /**
  288. * Closes the current contour, by storing the index of the last corner
  289. * of the current polygon in m_polyIndexEnd.
  290. */
  291. void CloseContour()
  292. {
  293. m_polyIndexEnd.push_back( m_Corners.size() -1 );
  294. }
  295. /**
  296. * @return the count of contours in the poly polygon
  297. */
  298. int GetPolyCount() const { return (int) m_polyIndexEnd.size(); }
  299. /**
  300. * @return the index of the first corner of the contour aCountour
  301. * @param aContour = the index of the contour
  302. */
  303. unsigned GetPolyIndexStart( unsigned aContour) const
  304. {
  305. if( aContour == 0 )
  306. return 0;
  307. else
  308. return m_polyIndexEnd[aContour-1] + 1;
  309. }
  310. /**
  311. * @return the index of the last corner of the contour aCountour
  312. * @param aContour = the index of the contour
  313. */
  314. unsigned GetPolyIndexEnd( unsigned aContour) const
  315. {
  316. return m_polyIndexEnd[aContour];
  317. }
  318. /**
  319. * @return the coordinate (in mm) of the corner aIdx,
  320. * for the repeated item aRepeat
  321. */
  322. const DPOINT GetCornerPosition( unsigned aIdx, int aRepeat = 0 ) const;
  323. /**
  324. * @return the coordinate (in draw/plot units) of the corner aIdx,
  325. * for the repeated item aRepeat
  326. */
  327. const wxPoint GetCornerPositionUi( unsigned aIdx, int aRepeat = 0 ) const;
  328. /**
  329. * calculate the bounding box of the set polygons
  330. */
  331. void SetBoundingBox();
  332. bool IsInsidePage( int ii ) const override;
  333. };
  334. class WORKSHEET_DATAITEM_TEXT : public WORKSHEET_DATAITEM
  335. {
  336. public:
  337. wxString m_TextBase; // The basic text, with format symbols
  338. wxString m_FullText; // The expanded text, shown on screen
  339. double m_Orient; // Orientation in degrees
  340. EDA_TEXT_HJUSTIFY_T m_Hjustify;
  341. EDA_TEXT_VJUSTIFY_T m_Vjustify;
  342. DSIZE m_TextSize;
  343. DSIZE m_BoundingBoxSize; // When not null, this is the max
  344. // size of the full text.
  345. // the text size will be modified
  346. // to keep the full text insite this
  347. // bound.
  348. DSIZE m_ConstrainedTextSize; // Actual text size, if constrained by
  349. // the m_BoundingBoxSize constraint
  350. public:
  351. WORKSHEET_DATAITEM_TEXT( const wxString& aTextBase );
  352. /**
  353. * @return false (no end point)
  354. */
  355. virtual bool HasEndPoint() override { return false; }
  356. virtual int GetPenSizeUi() override
  357. {
  358. if( m_LineWidth )
  359. return KiROUND( m_LineWidth * m_WSunits2Iu );
  360. else
  361. return KiROUND( m_DefaultTextThickness * m_WSunits2Iu );
  362. }
  363. /**
  364. * move item to a new position
  365. * @param aPosition = the new position of item
  366. */
  367. void MoveTo( DPOINT aPosition );
  368. /**
  369. * transfert the text justification and orientation
  370. * to aGText
  371. */
  372. void TransfertSetupToGraphicText( WS_DRAW_ITEM_TEXT* aGText );
  373. /**
  374. * Try to build text wihich is an increment of m_TextBase
  375. * has meaning only if m_TextBase is a basic text (one char)
  376. * If the basic char is a digit, build a number
  377. * If the basic char is a letter, use the letter with ascii code
  378. * aIncr + (basic char ascc code)
  379. * @param aIncr = the increment value
  380. * return the incremented label in m_FullText
  381. */
  382. void IncrementLabel( int aIncr );
  383. /**
  384. * Calculates m_ConstrainedTextSize from m_TextSize
  385. * to keep the X size and the full Y size of the text
  386. * smaller than m_BoundingBoxSize
  387. * if m_BoundingBoxSize.x or m_BoundingBoxSize.y > 0
  388. * if m_BoundingBoxSize.x or m_BoundingBoxSize.y == 0
  389. * the corresponding text size is not constrained
  390. */
  391. void SetConstrainedTextSize();
  392. /** Replace the '\''n' sequence by EOL
  393. * and the sequence '\''\' by only one '\'
  394. * inside m_FullText
  395. * @return true if the EOL symbol is found or is inserted (multiline text)
  396. */
  397. bool ReplaceAntiSlashSequence();
  398. /**
  399. * @return true is a bold font should be selected
  400. */
  401. bool IsBold() { return (m_flags & USE_BOLD) != 0; }
  402. /**
  403. * Function SetBold
  404. * Toggles on/off the bold option flag
  405. * @param aState = the bold option value
  406. */
  407. void SetBold( bool aState )
  408. {
  409. if( aState )
  410. m_flags |= USE_BOLD;
  411. else
  412. m_flags &= ~USE_BOLD;
  413. }
  414. /**
  415. * @return true is an italic font should be selected
  416. */
  417. bool IsItalic() const { return (m_flags & USE_ITALIC) != 0; }
  418. /**
  419. * Function SetItalic
  420. * Toggles on/off the italic option flag
  421. * @param aState = the italic option value
  422. */
  423. void SetItalic( bool aState )
  424. {
  425. if( aState )
  426. m_flags |= USE_ITALIC;
  427. else
  428. m_flags &= ~USE_ITALIC;
  429. }
  430. };
  431. class BITMAP_BASE;
  432. class WORKSHEET_DATAITEM_BITMAP : public WORKSHEET_DATAITEM
  433. {
  434. public:
  435. BITMAP_BASE* m_ImageBitmap;
  436. public:
  437. WORKSHEET_DATAITEM_BITMAP(BITMAP_BASE* aImage)
  438. : WORKSHEET_DATAITEM( WS_BITMAP )
  439. {
  440. m_ImageBitmap = aImage;
  441. }
  442. /**
  443. * @return false (no end point)
  444. */
  445. virtual bool HasEndPoint() override { return false; }
  446. /**
  447. * @return the PPI of the bitmap
  448. */
  449. int GetPPI() const;
  450. /**
  451. * adjust the PPI of the bitmap
  452. * @param aBitmapPPI = the ned PPI for the bitmap
  453. */
  454. void SetPPI( int aBitmapPPI );
  455. /**
  456. * set the pixel scale factor of the bitmap
  457. * this factor depend on the application internal unit
  458. * and the pixel per inch bitmap factor
  459. * the pixel scale factor is the pixel size to application internal unit
  460. * and should be initialized before printing or drawing the bitmap
  461. */
  462. void SetPixelScaleFactor();
  463. };
  464. #endif // WORKSHEET_DATA_ITEM_H