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.

658 lines
20 KiB

  1. /**
  2. * @file worksheet_shape_builder.h
  3. * @brief classes and function to generate graphics to plt or draw titles blocks
  4. * and frame references
  5. */
  6. #ifndef WORKSHEET_SHAPE_BUILDER_H
  7. #define WORKSHEET_SHAPE_BUILDER_H
  8. #include <vector2d.h>
  9. #include <eda_text.h>
  10. #include <eda_text.h>
  11. #include <class_bitmap_base.h>
  12. class WORKSHEET_DATAITEM; // Forward declaration
  13. class TITLE_BLOCK;
  14. #define TB_DEFAULT_TEXTSIZE 1.5 // default worksheet text size in mm
  15. /*
  16. * Helper classes to handle basic graphic items used to raw/plot
  17. * title blocks and frame references
  18. * segments
  19. * rect
  20. * polygons (for logos)
  21. * graphic texts
  22. * bitmaps, also for logos, but they cannot be plot by SVG, GERBER
  23. * and HPGL plotters (In this case, only the bounding box is plotted)
  24. */
  25. class WS_DRAW_ITEM_BASE // This basic class, not directly usable.
  26. {
  27. public:
  28. enum WS_DRAW_TYPE {
  29. wsg_line, wsg_rect, wsg_poly, wsg_text, wsg_bitmap
  30. };
  31. int m_Flags; // temporary flgs used in page layout editor
  32. // to locate the item;
  33. protected:
  34. WS_DRAW_TYPE m_type; // wsg_line, wsg_rect, wsg_poly, wsg_text
  35. EDA_COLOR_T m_color;
  36. WORKSHEET_DATAITEM* m_parent; // an unique identifier, used as link
  37. // to the parent WORKSHEET_DATAITEM item,
  38. // in page layout editor
  39. protected:
  40. WS_DRAW_ITEM_BASE( WORKSHEET_DATAITEM* aParent,
  41. WS_DRAW_TYPE aType, EDA_COLOR_T aColor )
  42. {
  43. m_type = aType;
  44. m_color = aColor;
  45. m_parent = aParent;
  46. m_Flags = 0;
  47. }
  48. public:
  49. virtual ~WS_DRAW_ITEM_BASE() {}
  50. // Accessors:
  51. EDA_COLOR_T GetColor() { return m_color; }
  52. WS_DRAW_TYPE GetType() { return m_type; };
  53. WORKSHEET_DATAITEM* GetParent() { return m_parent; }
  54. /** The function to draw a WS_DRAW_ITEM
  55. */
  56. virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC ) = 0;
  57. /**
  58. * Abstract function: should exist for derived items
  59. * return true if the point aPosition is on the item
  60. */
  61. virtual bool HitTest( const wxPoint& aPosition) = 0;
  62. /**
  63. * Abstract function: should exist for derived items
  64. * return true if the point aPosition is near the starting point of this item,
  65. * for items defined by 2 points (segments, rect)
  66. * or the position of the item, for items having only one point
  67. * (texts or polygons)
  68. * the maxi dist is WORKSHEET_DATAITEM::GetMarkerSizeUi()/2
  69. */
  70. virtual bool HitTestStartPoint( const wxPoint& aPosition) = 0;
  71. /**
  72. * return true if the point aPosition is near the ending point of this item
  73. * This is avirtual function which should be overriden for items defien by
  74. * 2 points
  75. * the maxi dist is WORKSHEET_DATAITEM::GetMarkerSizeUi()/2
  76. */
  77. virtual bool HitTestEndPoint( const wxPoint& aPosition)
  78. {
  79. return false;
  80. }
  81. };
  82. // This class draws a thick segment
  83. class WS_DRAW_ITEM_LINE : public WS_DRAW_ITEM_BASE
  84. {
  85. wxPoint m_start; // start point of line/rect
  86. wxPoint m_end; // end point
  87. int m_penWidth;
  88. public:
  89. WS_DRAW_ITEM_LINE( WORKSHEET_DATAITEM* aParent,
  90. wxPoint aStart, wxPoint aEnd,
  91. int aPenWidth, EDA_COLOR_T aColor ) :
  92. WS_DRAW_ITEM_BASE( aParent, wsg_line, aColor )
  93. {
  94. m_start = aStart;
  95. m_end = aEnd;
  96. m_penWidth = aPenWidth;
  97. }
  98. // Accessors:
  99. int GetPenWidth() { return m_penWidth; }
  100. const wxPoint& GetStart() { return m_start; }
  101. const wxPoint& GetEnd() { return m_end; }
  102. /** The function to draw a WS_DRAW_ITEM_LINE
  103. */
  104. virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );
  105. /**
  106. * Virtual function
  107. * return true if the point aPosition is on the line
  108. */
  109. virtual bool HitTest( const wxPoint& aPosition);
  110. /**
  111. * return true if the point aPosition is on the starting point of this item.
  112. */
  113. virtual bool HitTestStartPoint( const wxPoint& aPosition);
  114. /**
  115. * return true if the point aPosition is on the ending point of this item
  116. * This is avirtual function which should be overriden for items defien by
  117. * 2 points
  118. */
  119. virtual bool HitTestEndPoint( const wxPoint& aPosition);
  120. };
  121. // This class draws a polygon
  122. class WS_DRAW_ITEM_POLYGON : public WS_DRAW_ITEM_BASE
  123. {
  124. wxPoint m_pos; // position of reference point, from the
  125. // WORKSHEET_DATAITEM_POLYPOLYGON parent
  126. // (used only in page layout editor to draw anchors)
  127. int m_penWidth;
  128. bool m_fill;
  129. public:
  130. std::vector <wxPoint> m_Corners;
  131. public:
  132. WS_DRAW_ITEM_POLYGON( WORKSHEET_DATAITEM* aParent, wxPoint aPos,
  133. bool aFill, int aPenWidth, EDA_COLOR_T aColor ) :
  134. WS_DRAW_ITEM_BASE( aParent, wsg_poly, aColor )
  135. {
  136. m_penWidth = aPenWidth;
  137. m_fill = aFill;
  138. m_pos = aPos;
  139. }
  140. // Accessors:
  141. int GetPenWidth() { return m_penWidth; }
  142. bool IsFilled() { return m_fill; }
  143. const wxPoint& GetPosition() { return m_pos; }
  144. /** The function to draw a WS_DRAW_ITEM_POLYGON
  145. */
  146. virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );
  147. /**
  148. * Virtual function
  149. * return true if the point aPosition is inside one polygon
  150. */
  151. virtual bool HitTest( const wxPoint& aPosition);
  152. /**
  153. * return true if the point aPosition is on the starting point of this item.
  154. */
  155. virtual bool HitTestStartPoint( const wxPoint& aPosition);
  156. };
  157. // This class draws a not filled rectangle with thick segment
  158. class WS_DRAW_ITEM_RECT : public WS_DRAW_ITEM_LINE
  159. {
  160. public:
  161. WS_DRAW_ITEM_RECT( WORKSHEET_DATAITEM* aParent,
  162. wxPoint aStart, wxPoint aEnd,
  163. int aPenWidth, EDA_COLOR_T aColor ) :
  164. WS_DRAW_ITEM_LINE( aParent, aStart, aEnd, aPenWidth, aColor )
  165. {
  166. m_type = wsg_rect;
  167. }
  168. /** The function to draw a WS_DRAW_ITEM_RECT
  169. */
  170. virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );
  171. /**
  172. * Virtual function
  173. * return true if the point aPosition is on one edge of the rectangle
  174. */
  175. virtual bool HitTest( const wxPoint& aPosition);
  176. /**
  177. * return true if the point aPosition is on the starting point of this item.
  178. */
  179. virtual bool HitTestStartPoint( const wxPoint& aPosition);
  180. /**
  181. * return true if the point aPosition is on the ending point of this item
  182. * This is avirtual function which should be overriden for items defien by
  183. * 2 points
  184. */
  185. virtual bool HitTestEndPoint( const wxPoint& aPosition);
  186. };
  187. // This class draws a graphic text.
  188. // it is derived from an EDA_TEXT, so it handle all caracteristics
  189. // of this graphic text (justification, rotation ... )
  190. class WS_DRAW_ITEM_TEXT : public WS_DRAW_ITEM_BASE, public EDA_TEXT
  191. {
  192. public:
  193. WS_DRAW_ITEM_TEXT( WORKSHEET_DATAITEM* aParent,
  194. wxString& aText, wxPoint aPos, wxSize aSize,
  195. int aPenWidth, EDA_COLOR_T aColor,
  196. bool aItalic = false, bool aBold = false );
  197. /** The function to draw a WS_DRAW_ITEM_TEXT
  198. */
  199. virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );
  200. // Accessors:
  201. int GetPenWidth() { return GetThickness(); }
  202. /**
  203. * Virtual function
  204. * return true if the point aPosition is on the text
  205. */
  206. virtual bool HitTest( const wxPoint& aPosition);
  207. /**
  208. * return true if the point aPosition is on the starting point of this item.
  209. */
  210. virtual bool HitTestStartPoint( const wxPoint& aPosition);
  211. };
  212. // This class draws a bitmap.
  213. class WS_DRAW_ITEM_BITMAP : public WS_DRAW_ITEM_BASE
  214. {
  215. wxPoint m_pos; // position of reference point
  216. public:
  217. WS_DRAW_ITEM_BITMAP( WORKSHEET_DATAITEM* aParent, wxPoint aPos )
  218. :WS_DRAW_ITEM_BASE( aParent, wsg_bitmap, UNSPECIFIED_COLOR )
  219. {
  220. m_pos = aPos;
  221. }
  222. WS_DRAW_ITEM_BITMAP()
  223. :WS_DRAW_ITEM_BASE( NULL, wsg_bitmap, UNSPECIFIED_COLOR )
  224. {
  225. }
  226. ~WS_DRAW_ITEM_BITMAP() {}
  227. /** The function to draw a WS_DRAW_ITEM_BITMAP
  228. */
  229. virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );
  230. /**
  231. * Virtual function
  232. * return true if the point aPosition is on bitmap
  233. */
  234. virtual bool HitTest( const wxPoint& aPosition);
  235. /**
  236. * return true if the point aPosition is on the reference point of this item.
  237. */
  238. virtual bool HitTestStartPoint( const wxPoint& aPosition);
  239. const wxPoint& GetPosition() { return m_pos; }
  240. };
  241. /*
  242. * this class stores the list of graphic items:
  243. * rect, lines, polygons and texts to draw/plot
  244. * the title block and frame references, and parameters to
  245. * draw/plot them
  246. */
  247. class WS_DRAW_ITEM_LIST
  248. {
  249. protected:
  250. std::vector <WS_DRAW_ITEM_BASE*> m_graphicList; // Items to draw/plot
  251. unsigned m_idx; // for GetFirst, GetNext functions
  252. wxPoint m_LTmargin; // The left top margin in mils of the page layout.
  253. wxPoint m_RBmargin; // The right bottom margin in mils of the page layout.
  254. wxSize m_pageSize; // the page size in mils
  255. double m_milsToIu; // the scalar to convert pages units ( mils)
  256. // to draw/plot units.
  257. int m_penSize; // The default line width for drawings.
  258. // used when an item has a pen size = 0
  259. int m_sheetNumber; // the value of the sheet number, for basic inscriptions
  260. int m_sheetCount; // the value of the number of sheets, in schematic
  261. // for basic inscriptions, in schematic
  262. const TITLE_BLOCK* m_titleBlock; // for basic inscriptions
  263. const wxString* m_paperFormat; // for basic inscriptions
  264. wxString m_fileName; // for basic inscriptions
  265. const wxString* m_sheetFullName; // for basic inscriptions
  266. public:
  267. WS_DRAW_ITEM_LIST()
  268. {
  269. m_idx = 0;
  270. m_milsToIu = 1.0;
  271. m_penSize = 1;
  272. m_sheetNumber = 1;
  273. m_sheetCount = 1;
  274. m_titleBlock = NULL;
  275. m_paperFormat = NULL;
  276. m_sheetFullName = NULL;
  277. }
  278. ~WS_DRAW_ITEM_LIST()
  279. {
  280. for( unsigned ii = 0; ii < m_graphicList.size(); ii++ )
  281. delete m_graphicList[ii];
  282. }
  283. /**
  284. * Set the filename to draw/plot
  285. * @param aFileName = the text to display by the "filename" format
  286. */
  287. void SetFileName( const wxString & aFileName )
  288. {
  289. m_fileName = aFileName;
  290. }
  291. /**
  292. * Set the sheet name to draw/plot
  293. * @param aSheetName = the text to draw/plot by the "sheetname" format
  294. */
  295. void SetSheetName( const wxString & aSheetName )
  296. {
  297. m_sheetFullName = &aSheetName;
  298. }
  299. /** Function SetPenSize
  300. * Set the default pen size to draw/plot lines and texts
  301. * @param aPenSize the thickness of lines
  302. */
  303. void SetPenSize( int aPenSize )
  304. {
  305. m_penSize = aPenSize;
  306. }
  307. /** Function SetMilsToIUfactor
  308. * Set the scalar to convert pages units ( mils) to draw/plot units
  309. * @param aScale the conversion factor
  310. */
  311. void SetMilsToIUfactor( double aScale )
  312. {
  313. m_milsToIu = aScale;
  314. }
  315. /** Function SetPageSize
  316. * Set the size of the page layout
  317. * @param aPageSize size (in mils) of the page layout.
  318. */
  319. void SetPageSize( const wxSize& aPageSize )
  320. {
  321. m_pageSize = aPageSize;
  322. }
  323. /**
  324. * Function SetSheetNumber
  325. * Set the value of the sheet number, for basic inscriptions
  326. * @param aSheetNumber the number to display.
  327. */
  328. void SetSheetNumber( int aSheetNumber )
  329. {
  330. m_sheetNumber = aSheetNumber;
  331. }
  332. /**
  333. * Function SetSheetCount
  334. * Set the value of the count of sheets, for basic inscriptions
  335. * @param aSheetCount the number of esheets to display.
  336. */
  337. void SetSheetCount( int aSheetCount )
  338. {
  339. m_sheetCount = aSheetCount;
  340. }
  341. /** Function SetMargins
  342. * Set the left top margin and the right bottom margin
  343. * of the page layout
  344. * @param aLTmargin The left top margin of the page layout.
  345. * @param aRBmargin The right bottom margin of the page layout.
  346. */
  347. void SetMargins( const wxPoint& aLTmargin, const wxPoint& aRBmargin )
  348. {
  349. m_LTmargin = aLTmargin;
  350. m_RBmargin = aRBmargin;
  351. }
  352. void Append( WS_DRAW_ITEM_BASE* aItem )
  353. {
  354. m_graphicList.push_back( aItem );
  355. }
  356. WS_DRAW_ITEM_BASE* GetFirst()
  357. {
  358. m_idx = 0;
  359. if( m_graphicList.size() )
  360. return m_graphicList[0];
  361. else
  362. return NULL;
  363. }
  364. WS_DRAW_ITEM_BASE* GetNext()
  365. {
  366. m_idx++;
  367. if( m_graphicList.size() > m_idx )
  368. return m_graphicList[m_idx];
  369. else
  370. return NULL;
  371. }
  372. /**
  373. * Draws the item list created by BuildWorkSheetGraphicList
  374. * @param aClipBox = the clipping rect, or NULL if no clipping
  375. * @param aDC = the current Device Context
  376. */
  377. void Draw( EDA_RECT* aClipBox, wxDC* aDC );
  378. /**
  379. * Function BuildWorkSheetGraphicList is a core function for
  380. * drawing or plotting the page layout with
  381. * the frame and the basic inscriptions.
  382. * It populates the list of basic graphic items to draw or plot.
  383. * currently lines, rect, polygons and texts
  384. * before calling this function, some parameters should be initialized:
  385. * by calling:
  386. * SetPenSize( aPenWidth );
  387. * SetMilsToIUfactor( aScalar );
  388. * SetSheetNumber( aSheetNumber );
  389. * SetSheetCount( aSheetCount );
  390. * SetFileName( aFileName );
  391. * SetSheetName( aFullSheetName );
  392. *
  393. * @param aPageInfo The PAGE_INFO, for page size, margins...
  394. * @param aTitleBlock The sheet title block, for basic inscriptions.
  395. * @param aColor The color for drawing.
  396. * @param aAltColor The color for items which need to be "hightlighted".
  397. */
  398. void BuildWorkSheetGraphicList( const PAGE_INFO& aPageInfo,
  399. const TITLE_BLOCK& aTitleBlock,
  400. EDA_COLOR_T aColor, EDA_COLOR_T aAltColor );
  401. /**
  402. * Function BuildFullText
  403. * returns the full text corresponding to the aTextbase,
  404. * after replacing format symbols by the corresponding value
  405. *
  406. * Basic texts in Ki_WorkSheetData struct use format notation
  407. * like "Title %T" to identify at run time the full text
  408. * to display.
  409. * Currently format identifier is % followed by a letter or 2 letters
  410. *
  411. * %% = replaced by %
  412. * %K = Kicad version
  413. * %Z = paper format name (A4, USLetter)
  414. * %Y = company name
  415. * %D = date
  416. * %R = revision
  417. * %S = sheet number
  418. * %N = number of sheets
  419. * %Cx = comment (x = 0 to 9 to identify the comment)
  420. * %F = filename
  421. * %P = sheet path or sheet full name
  422. * %T = title
  423. * Other fields like Developer, Verifier, Approver could use %Cx
  424. * and are seen as comments for format
  425. *
  426. * @param aTextbase = the text with format symbols
  427. * @return the text, after replacing the format symbols by the actual value
  428. */
  429. wxString BuildFullText( const wxString& aTextbase );
  430. /**
  431. * Locate graphic items in m_graphicList at location aPosition
  432. * @param aList = the list of items found
  433. * @param aPosition the position (in user units) to locate items
  434. */
  435. void Locate(std::vector <WS_DRAW_ITEM_BASE*>& aList, const wxPoint& aPosition);
  436. };
  437. /**
  438. * WORKSHEET_LAYOUT handles the graphic items list to draw/plot
  439. * the title block and other items (page references ...
  440. */
  441. class WORKSHEET_LAYOUT
  442. {
  443. std::vector <WORKSHEET_DATAITEM*> m_list;
  444. bool m_allowVoidList; // If false, the default page layout
  445. // will be loaded the first time
  446. // WS_DRAW_ITEM_LIST::BuildWorkSheetGraphicList
  447. // is run (useful mainly for page layout editor)
  448. double m_leftMargin; // the left page margin in mm
  449. double m_rightMargin; // the right page margin in mm
  450. double m_topMargin; // the top page margin in mm
  451. double m_bottomMargin; // the bottom page margin in mm
  452. public:
  453. WORKSHEET_LAYOUT();
  454. ~WORKSHEET_LAYOUT() {ClearList(); }
  455. /**
  456. * static function: returns the instance of WORKSHEET_LAYOUT
  457. * used in the application
  458. */
  459. static WORKSHEET_LAYOUT& GetTheInstance()
  460. {
  461. extern WORKSHEET_LAYOUT wksTheInstance;
  462. return wksTheInstance;
  463. }
  464. // Accessors:
  465. double GetLeftMargin() { return m_leftMargin; }
  466. double GetRightMargin() { return m_rightMargin; }
  467. double GetTopMargin() { return m_topMargin; }
  468. double GetBottomMargin() { return m_bottomMargin; }
  469. void SetLeftMargin( double aMargin );
  470. void SetRightMargin( double aMargin );
  471. void SetTopMargin( double aMargin );
  472. void SetBottomMargin( double aMargin );
  473. /**
  474. * In Kicad applications, a page layout description is needed
  475. * So if the list is empty, a default description is loaded,
  476. * the first time a page layout is drawn.
  477. * However, in page layout editor, an empty list is acceptable.
  478. * AllowVoidList allows or not the empty list
  479. */
  480. void AllowVoidList( bool Allow ) { m_allowVoidList = Allow; }
  481. /**
  482. * @return true if an empty list is allowed
  483. * (mainly allowed for page layout editor).
  484. */
  485. bool VoidListAllowed() { return m_allowVoidList; }
  486. /**
  487. * erase the list of items
  488. */
  489. void ClearList();
  490. /**
  491. * Save the description in a file
  492. * @param aFullFileName the filename of the file to created
  493. */
  494. void Save( const wxString& aFullFileName );
  495. /**
  496. * Save the description in a buffer
  497. * @param aOutputString = a wxString to store the S expr string
  498. */
  499. void SaveInString( wxString& aOutputString );
  500. /**
  501. * Add an item to the list of items
  502. */
  503. void Append( WORKSHEET_DATAITEM* aItem )
  504. {
  505. m_list.push_back( aItem );
  506. }
  507. /**
  508. *Insert an item to the list of items at position aIdx
  509. */
  510. void Insert( WORKSHEET_DATAITEM* aItem, unsigned aIdx );
  511. /**
  512. *Remove the item to the list of items at position aIdx
  513. */
  514. bool Remove( unsigned aIdx );
  515. /**
  516. *Remove the item to the list of items at position aIdx
  517. */
  518. bool Remove( WORKSHEET_DATAITEM* aItem );
  519. /**
  520. * @return the index of aItem, or -1 if does not exist
  521. */
  522. int GetItemIndex( WORKSHEET_DATAITEM* aItem ) const;
  523. /**
  524. * @return the item from its index aIdx, or NULL if does not exist
  525. */
  526. WORKSHEET_DATAITEM* GetItem( unsigned aIdx ) const;
  527. /**
  528. * @return the item count
  529. */
  530. unsigned GetCount() const { return m_list.size(); }
  531. /**
  532. * Fills the list with the default layout shape
  533. */
  534. void SetDefaultLayout();
  535. /**
  536. * Populates the list with a custom layout, or
  537. * the default layout, if no custom layout available
  538. * @param aFullFileName = the custom page layout description file.
  539. * if empty, loads the file defined by KICAD_WKSFILE
  540. * and if its is not defined, uses the default internal description
  541. * @param Append = if true: do not delete old layout, and load only
  542. aFullFileName.
  543. */
  544. void SetPageLayout( const wxString& aFullFileName = wxEmptyString,
  545. bool Append = false );
  546. /**
  547. * Populates the list from a S expr description stored in a string
  548. * @param aPageLayout = the S expr string
  549. * @param Append Do not delete old layout if true and append \a aPageLayout
  550. * the existing one.
  551. */
  552. void SetPageLayout( const char* aPageLayout, bool Append = false );
  553. /**
  554. * @return a short filename from a full filename:
  555. * if the path is the current path, or if the path
  556. * is the same as kicad.pro (in template), returns the shortname
  557. * else do nothing and returns a full filename
  558. */
  559. static const wxString MakeShortFileName( const wxString& aFullFileName );
  560. /**
  561. * @return a full filename from a short filename,
  562. * if the short filename path is void
  563. * In this case the path is the same as kicad.pro (in template)
  564. * else return the short filename (which have an absolute os relative path
  565. */
  566. static const wxString MakeFullFileName( const wxString& aShortFileName );
  567. };
  568. #endif // WORKSHEET_SHAPE_BUILDER_H