worksheet_shape_builder.h 20.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
/**
 * @file worksheet_shape_builder.h
 * @brief classes and function to generate graphics to plt or draw titles blocks
 * and frame references
 */

#ifndef  WORKSHEET_SHAPE_BUILDER_H
#define  WORKSHEET_SHAPE_BUILDER_H

Maciej Suminski's avatar
Maciej Suminski committed
10
#include <math/vector2d.h>
11
#include <eda_text.h>
12 13
#include <eda_text.h>
#include <class_bitmap_base.h>
14

15 16
class WORKSHEET_DATAITEM;        // Forward declaration
class TITLE_BLOCK;
17 18 19

#define TB_DEFAULT_TEXTSIZE             1.5  // default worksheet text size in mm

20 21 22 23 24 25 26
/*
 * Helper classes to handle basic graphic items used to raw/plot
 * title blocks and frame references
 * segments
 * rect
 * polygons (for logos)
 * graphic texts
27 28
 * bitmaps, also for logos, but they cannot be plot by SVG, GERBER
 * and HPGL plotters (In this case, only the bounding box is plotted)
29 30 31 32 33
 */
class WS_DRAW_ITEM_BASE     // This basic class, not directly usable.
{
public:
    enum WS_DRAW_TYPE {
34
        wsg_line, wsg_rect, wsg_poly, wsg_text, wsg_bitmap
35
    };
36 37
    int m_Flags;                    // temporary flgs used in page layout editor
                                    // to locate the item;
38

39 40 41
protected:
    WS_DRAW_TYPE    m_type; // wsg_line, wsg_rect, wsg_poly, wsg_text
    EDA_COLOR_T     m_color;
42 43 44
    WORKSHEET_DATAITEM*  m_parent;  // an unique identifier, used as link
                                    // to the parent WORKSHEET_DATAITEM item,
                                    // in page layout editor
45

46 47
    WS_DRAW_ITEM_BASE( WORKSHEET_DATAITEM*  aParent,
                       WS_DRAW_TYPE aType, EDA_COLOR_T aColor )
48 49 50
    {
        m_type  = aType;
        m_color = aColor;
51 52
        m_parent = aParent;
        m_Flags = 0;
53 54 55 56 57 58
    }

public:
    virtual ~WS_DRAW_ITEM_BASE() {}

    // Accessors:
59 60
    EDA_COLOR_T GetColor() const { return m_color; }
    WS_DRAW_TYPE GetType() const { return m_type; };
61 62 63

    WORKSHEET_DATAITEM* GetParent() { return m_parent; }

64 65 66 67
    /** The function to draw a WS_DRAW_ITEM
     */
    virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC ) = 0;

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    /**
     * Abstract function: should exist for derived items
     * return true if the point aPosition is on the item
     */
    virtual bool HitTest( const wxPoint& aPosition) = 0;

    /**
     * Abstract function: should exist for derived items
     * return true if the point aPosition is near the starting point of this item,
     * for items defined by 2 points (segments, rect)
     * or the position of the item, for items having only one point
     * (texts or polygons)
     * the maxi dist is WORKSHEET_DATAITEM::GetMarkerSizeUi()/2
     */
    virtual bool HitTestStartPoint( const wxPoint& aPosition) = 0;

    /**
     * return true if the point aPosition is near the ending point of this item
     * This is avirtual function which should be overriden for items defien by
     * 2 points
     * the maxi dist is WORKSHEET_DATAITEM::GetMarkerSizeUi()/2
     */
    virtual bool HitTestEndPoint( const wxPoint& aPosition)
    {
        return false;
    }
94 95 96 97 98 99 100 101
};

// This class draws a thick segment
class WS_DRAW_ITEM_LINE : public WS_DRAW_ITEM_BASE
{
    wxPoint m_start;    // start point of line/rect
    wxPoint m_end;      // end point
    int     m_penWidth;
102

103
public:
104 105
    WS_DRAW_ITEM_LINE( WORKSHEET_DATAITEM* aParent,
                       wxPoint aStart, wxPoint aEnd,
106
                       int aPenWidth, EDA_COLOR_T aColor ) :
107
        WS_DRAW_ITEM_BASE( aParent, wsg_line, aColor )
108 109 110 111 112 113 114
    {
        m_start     = aStart;
        m_end       = aEnd;
        m_penWidth  = aPenWidth;
    }

    // Accessors:
115 116 117
    int GetPenWidth() const { return m_penWidth; }
    const wxPoint&  GetStart() const { return m_start; }
    const wxPoint&  GetEnd() const { return m_end; }
118

119 120 121 122
    /** The function to draw a WS_DRAW_ITEM_LINE
     */
    virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    /**
     * Virtual function
     * return true if the point aPosition is on the line
     */
    virtual bool HitTest( const wxPoint& aPosition);

    /**
     * return true if the point aPosition is on the starting point of this item.
     */
    virtual bool HitTestStartPoint( const wxPoint& aPosition);

    /**
     * return true if the point aPosition is on the ending point of this item
     * This is avirtual function which should be overriden for items defien by
     * 2 points
     */
    virtual bool HitTestEndPoint( const wxPoint& aPosition);
140 141 142 143 144
};

// This class draws a polygon
class WS_DRAW_ITEM_POLYGON : public WS_DRAW_ITEM_BASE
{
145 146 147
    wxPoint m_pos;      // position of reference point, from the
                        // WORKSHEET_DATAITEM_POLYPOLYGON parent
                        // (used only in page layout editor to draw anchors)
148
    int m_penWidth;
149 150
    bool m_fill;

151 152
public:
    std::vector <wxPoint> m_Corners;
153

154
public:
155 156 157
    WS_DRAW_ITEM_POLYGON( WORKSHEET_DATAITEM* aParent, wxPoint aPos,
                          bool aFill, int aPenWidth, EDA_COLOR_T aColor ) :
        WS_DRAW_ITEM_BASE( aParent, wsg_poly, aColor )
158 159
    {
        m_penWidth = aPenWidth;
160
        m_fill = aFill;
161
        m_pos = aPos;
162 163 164
    }

    // Accessors:
165 166 167
    int GetPenWidth() const { return m_penWidth; }
    bool IsFilled() const { return m_fill; }
    const wxPoint& GetPosition() const { return m_pos; }
168

169 170 171 172
    /** The function to draw a WS_DRAW_ITEM_POLYGON
     */
    virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );

173 174 175 176 177 178 179 180 181 182
    /**
     * Virtual function
     * return true if the point aPosition is inside one polygon
     */
    virtual bool HitTest( const wxPoint& aPosition);

    /**
     * return true if the point aPosition is on the starting point of this item.
     */
    virtual bool HitTestStartPoint( const wxPoint& aPosition);
183 184 185 186 187 188
};

// This class draws a not filled rectangle with thick segment
class WS_DRAW_ITEM_RECT : public WS_DRAW_ITEM_LINE
{
public:
189 190
    WS_DRAW_ITEM_RECT( WORKSHEET_DATAITEM* aParent,
                       wxPoint aStart, wxPoint aEnd,
191
                       int aPenWidth, EDA_COLOR_T aColor ) :
192
        WS_DRAW_ITEM_LINE( aParent, aStart, aEnd, aPenWidth, aColor )
193 194 195
    {
        m_type = wsg_rect;
    }
196

197 198 199 200
    /** The function to draw a WS_DRAW_ITEM_RECT
     */
    virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    /**
     * Virtual function
     * return true if the point aPosition is on one edge of the rectangle
     */
    virtual bool HitTest( const wxPoint& aPosition);

    /**
     * return true if the point aPosition is on the starting point of this item.
     */
    virtual bool HitTestStartPoint( const wxPoint& aPosition);

    /**
     * return true if the point aPosition is on the ending point of this item
     * This is avirtual function which should be overriden for items defien by
     * 2 points
     */
    virtual bool HitTestEndPoint( const wxPoint& aPosition);
218 219 220 221 222 223 224 225
};

// This class draws a graphic text.
// it is derived from an EDA_TEXT, so it handle all caracteristics
// of this graphic text (justification, rotation ... )
class WS_DRAW_ITEM_TEXT : public WS_DRAW_ITEM_BASE, public EDA_TEXT
{
public:
226 227
    WS_DRAW_ITEM_TEXT( WORKSHEET_DATAITEM* aParent,
                       wxString& aText, wxPoint aPos, wxSize aSize,
228
                       int aPenWidth, EDA_COLOR_T aColor,
229 230 231 232 233
                       bool aItalic = false, bool aBold = false );

    /** The function to draw a WS_DRAW_ITEM_TEXT
     */
    virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );
234 235 236

    // Accessors:
    int GetPenWidth() { return GetThickness(); }
237 238 239 240 241 242 243 244 245 246 247

    /**
     * Virtual function
     * return true if the point aPosition is on the text
     */
    virtual bool HitTest( const wxPoint& aPosition);

    /**
     * return true if the point aPosition is on the starting point of this item.
     */
    virtual bool HitTestStartPoint( const wxPoint& aPosition);
248 249
};

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
// This class draws a bitmap.
class WS_DRAW_ITEM_BITMAP : public WS_DRAW_ITEM_BASE
{
    wxPoint m_pos;                  // position of reference point

public:
    WS_DRAW_ITEM_BITMAP( WORKSHEET_DATAITEM* aParent, wxPoint aPos )
        :WS_DRAW_ITEM_BASE( aParent, wsg_bitmap, UNSPECIFIED_COLOR )
    {
        m_pos = aPos;
    }

    WS_DRAW_ITEM_BITMAP()
        :WS_DRAW_ITEM_BASE( NULL, wsg_bitmap, UNSPECIFIED_COLOR )
    {
    }

    ~WS_DRAW_ITEM_BITMAP() {}

    /** The function to draw a WS_DRAW_ITEM_BITMAP
     */
    virtual void DrawWsItem( EDA_RECT* aClipBox, wxDC* aDC );

    /**
     * Virtual function
     * return true if the point aPosition is on bitmap
     */
    virtual bool HitTest( const wxPoint& aPosition);

    /**
     * return true if the point aPosition is on the reference point of this item.
     */
    virtual bool HitTestStartPoint( const wxPoint& aPosition);

    const wxPoint& GetPosition() { return m_pos; }
};

287
/*
288 289 290 291
 * this class stores the list of graphic items:
 * rect, lines, polygons and texts to draw/plot
 * the title block and frame references, and parameters to
 * draw/plot them
292 293 294
 */
class WS_DRAW_ITEM_LIST
{
295
protected:
296 297 298 299 300 301 302
    std::vector <WS_DRAW_ITEM_BASE*> m_graphicList;     // Items to draw/plot
    unsigned m_idx;             // for GetFirst, GetNext functions
    wxPoint  m_LTmargin;        // The left top margin in mils of the page layout.
    wxPoint  m_RBmargin;        // The right bottom margin in mils of the page layout.
    wxSize   m_pageSize;        // the page size in mils
    double   m_milsToIu;        // the scalar to convert pages units ( mils)
                                // to draw/plot units.
303 304
    int      m_penSize;         // The default line width for drawings.
                                // used when an item has a pen size = 0
305 306 307
    int      m_sheetNumber;     // the value of the sheet number, for basic inscriptions
    int      m_sheetCount;      // the value of the number of sheets, in schematic
                                // for basic inscriptions, in schematic
308 309
    const TITLE_BLOCK* m_titleBlock;    // for basic inscriptions
    const wxString* m_paperFormat;      // for basic inscriptions
310
    wxString        m_fileName;         // for basic inscriptions
311 312
    const wxString* m_sheetFullName;    // for basic inscriptions

313 314 315 316 317

public:
    WS_DRAW_ITEM_LIST()
    {
        m_idx = 0;
318 319
        m_milsToIu = 1.0;
        m_penSize = 1;
320 321
        m_sheetNumber = 1;
        m_sheetCount = 1;
322 323 324
        m_titleBlock = NULL;
        m_paperFormat = NULL;
        m_sheetFullName = NULL;
325 326 327 328 329 330 331 332
    }

    ~WS_DRAW_ITEM_LIST()
    {
        for( unsigned ii = 0; ii < m_graphicList.size(); ii++ )
            delete m_graphicList[ii];
    }

333 334 335 336 337 338
    /**
     * Set the filename to draw/plot
     * @param aFileName = the text to display by the "filename" format
     */
    void SetFileName( const wxString & aFileName )
    {
339
        m_fileName = aFileName;
340 341 342 343 344 345 346 347 348 349 350 351 352
    }

    /**
     * Set the sheet name to draw/plot
     * @param aSheetName = the text to draw/plot by the "sheetname" format
     */
    void SetSheetName( const wxString & aSheetName )
    {
        m_sheetFullName = &aSheetName;
    }

    /** Function SetPenSize
     * Set the default pen size to draw/plot lines and texts
353 354 355 356 357 358 359
     * @param aPenSize the thickness of lines
     */
    void SetPenSize( int aPenSize )
    {
        m_penSize = aPenSize;
    }

360
    /** Function SetMilsToIUfactor
361 362 363 364 365 366 367 368
     * Set the scalar to convert pages units ( mils) to draw/plot units
     * @param aScale the conversion factor
     */
    void SetMilsToIUfactor( double aScale )
    {
        m_milsToIu = aScale;
    }

369
    /** Function SetPageSize
370 371 372 373 374 375 376 377
     * Set the size of the page layout
     * @param aPageSize size (in mils) of the page layout.
     */
    void SetPageSize( const wxSize& aPageSize )
    {
        m_pageSize = aPageSize;
    }

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    /**
     * Function SetSheetNumber
     * Set the value of the sheet number, for basic inscriptions
     * @param aSheetNumber the number to display.
     */
    void SetSheetNumber( int aSheetNumber )
    {
        m_sheetNumber = aSheetNumber;
    }

    /**
     * Function SetSheetCount
     * Set the value of the count of sheets, for basic inscriptions
     * @param aSheetCount the number of esheets to display.
     */
    void SetSheetCount( int aSheetCount )
    {
        m_sheetCount = aSheetCount;
    }

398
    /** Function SetMargins
399
     * Set the left top margin and the right bottom margin
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
     * of the page layout
     * @param aLTmargin The left top margin of the page layout.
     * @param aRBmargin The right bottom margin of the page layout.
     */
    void SetMargins( const wxPoint& aLTmargin, const wxPoint& aRBmargin )
    {
        m_LTmargin = aLTmargin;
        m_RBmargin = aRBmargin;
    }

    void Append( WS_DRAW_ITEM_BASE* aItem )
    {
        m_graphicList.push_back( aItem );
    }

    WS_DRAW_ITEM_BASE* GetFirst()
    {
        m_idx = 0;

        if( m_graphicList.size() )
            return m_graphicList[0];
        else
            return NULL;
    }

    WS_DRAW_ITEM_BASE* GetNext()
    {
        m_idx++;

        if( m_graphicList.size() > m_idx )
            return m_graphicList[m_idx];
        else
            return NULL;
    }

435
    /**
436
     * Draws the item list created by BuildWorkSheetGraphicList
437 438 439 440 441
     * @param aClipBox = the clipping rect, or NULL if no clipping
     * @param aDC = the current Device Context
     */
    void Draw( EDA_RECT* aClipBox, wxDC* aDC );

442 443 444 445
    /**
     * Function BuildWorkSheetGraphicList is a core function for
     * drawing or plotting the page layout with
     * the frame and the basic inscriptions.
446
     * It populates the list of basic graphic items to draw or plot.
447
     * currently lines, rect, polygons and texts
448 449 450 451 452 453 454 455
     * before calling this function, some parameters should be initialized:
     * by calling:
     *   SetPenSize( aPenWidth );
     *   SetMilsToIUfactor( aScalar );
     *   SetSheetNumber( aSheetNumber );
     *   SetSheetCount( aSheetCount );
     *   SetFileName( aFileName );
     *   SetSheetName( aFullSheetName );
456
     *
457
     * @param aPageInfo The PAGE_INFO, for page size, margins...
458
     * @param aTitleBlock The sheet title block, for basic inscriptions.
459 460
     * @param aColor The color for drawing.
     * @param aAltColor The color for items which need to be "hightlighted".
461
     */
462
    void BuildWorkSheetGraphicList( const PAGE_INFO& aPageInfo,
463
                                    const TITLE_BLOCK& aTitleBlock,
464
                                    EDA_COLOR_T aColor, EDA_COLOR_T aAltColor );
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    /**
     * Function BuildFullText
     * returns the full text corresponding to the aTextbase,
     * after replacing format symbols by the corresponding value
     *
     * Basic texts in Ki_WorkSheetData struct use format notation
     * like "Title %T" to identify at run time the full text
     * to display.
     * Currently format identifier is % followed by a letter or 2 letters
     *
     * %% = replaced by %
     * %K = Kicad version
     * %Z = paper format name (A4, USLetter)
     * %Y = company name
     * %D = date
     * %R = revision
     * %S = sheet number
     * %N = number of sheets
     * %Cx = comment (x = 0 to 9 to identify the comment)
     * %F = filename
     * %P = sheet path or sheet full name
     * %T = title
     * Other fields like Developer, Verifier, Approver could use %Cx
     * and are seen as comments for format
     *
     * @param aTextbase = the text with format symbols
     * @return the text, after replacing the format symbols by the actual value
     */
    wxString BuildFullText( const wxString& aTextbase );
494 495 496 497 498 499 500

    /**
     * Locate graphic items in m_graphicList at location aPosition
     * @param aList = the list of items found
     * @param aPosition the position (in user units) to locate items
     */
    void Locate(std::vector <WS_DRAW_ITEM_BASE*>& aList, const wxPoint& aPosition);
501 502 503
};


504
/**
505
 * WORKSHEET_LAYOUT handles the graphic items list to draw/plot
506 507 508 509 510
 * the title block and other items (page references ...
 */
class WORKSHEET_LAYOUT
{
    std::vector <WORKSHEET_DATAITEM*> m_list;
511 512 513 514
    bool m_allowVoidList;   // If false, the default page layout
                            // will be loaded the first time
                            // WS_DRAW_ITEM_LIST::BuildWorkSheetGraphicList
                            // is run (useful mainly for page layout editor)
515 516 517 518
    double m_leftMargin;    // the left page margin in mm
    double m_rightMargin;   // the right page margin in mm
    double m_topMargin;     // the top page margin in mm
    double m_bottomMargin;  // the bottom page margin in mm
519 520

public:
521
    WORKSHEET_LAYOUT();
522 523
    ~WORKSHEET_LAYOUT() {ClearList(); }

524 525 526 527 528
    /**
     * static function: returns the instance of WORKSHEET_LAYOUT
     * used in the application
     */
    static WORKSHEET_LAYOUT& GetTheInstance()
529
    {
530 531
        extern WORKSHEET_LAYOUT wksTheInstance;
        return wksTheInstance;
532 533
    }

534 535 536 537 538 539 540 541 542 543 544
    // Accessors:
    double GetLeftMargin() { return m_leftMargin; }
    double GetRightMargin() { return m_rightMargin; }
    double GetTopMargin() { return m_topMargin; }
    double GetBottomMargin() { return m_bottomMargin; }

    void SetLeftMargin( double aMargin );
    void SetRightMargin( double aMargin );
    void SetTopMargin( double aMargin );
    void SetBottomMargin( double aMargin );

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    /**
     * In Kicad applications, a page layout description is needed
     * So if the list is empty, a default description is loaded,
     * the first time a page layout is drawn.
     * However, in page layout editor, an empty list is acceptable.
     * AllowVoidList allows or not the empty list
     */
    void AllowVoidList( bool Allow ) { m_allowVoidList = Allow; }

    /**
     * @return true if an empty list is allowed
     * (mainly allowed for page layout editor).
     */
    bool VoidListAllowed() { return m_allowVoidList; }

    /**
     * erase the list of items
     */
    void ClearList();

    /**
566
     * Save the description in a file
567 568 569 570
     * @param aFullFileName the filename of the file to created
     */
    void Save( const wxString& aFullFileName );

571 572 573 574 575 576
    /**
     * Save the description in a buffer
     * @param aOutputString = a wxString to store the S expr string
     */
    void SaveInString( wxString& aOutputString );

577 578 579 580 581 582 583 584
    /**
     * Add an item to the list of items
     */
    void Append( WORKSHEET_DATAITEM* aItem )
    {
        m_list.push_back( aItem );
    }

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
    /**
     *Insert an item to the list of items at position aIdx
     */
    void Insert( WORKSHEET_DATAITEM* aItem, unsigned aIdx );

    /**
     *Remove the item to the list of items at position aIdx
     */
    bool  Remove( unsigned aIdx );

    /**
     *Remove the item to the list of items at position aIdx
     */
    bool  Remove( WORKSHEET_DATAITEM* aItem );

    /**
     * @return the index of aItem, or -1 if does not exist
     */
    int GetItemIndex( WORKSHEET_DATAITEM* aItem ) const;

605 606 607
    /**
     * @return the item from its index aIdx, or NULL if does not exist
     */
608
    WORKSHEET_DATAITEM* GetItem( unsigned aIdx ) const;
609 610 611 612 613 614 615 616 617 618

    /**
     * @return the item count
     */
    unsigned GetCount() const { return m_list.size(); }

    /**
     * Fills the list with the default layout shape
     */
    void SetDefaultLayout();
619 620

    /**
621
     * Populates the list with a custom layout, or
622
     * the default layout, if no custom layout available
623 624 625 626 627
     * @param aFullFileName = the custom page layout description file.
     * if empty, loads the file defined by KICAD_WKSFILE
     * and if its is not defined, uses the default internal description
     * @param Append = if true: do not delete old layout, and load only
       aFullFileName.
628
     */
629 630
    void SetPageLayout( const wxString& aFullFileName = wxEmptyString,
                        bool Append = false );
631

632 633 634
    /**
     * Populates the list from a S expr description stored in a string
     * @param aPageLayout = the S expr string
635 636
     * @param Append Do not delete old layout if true and append \a aPageLayout
     *               the existing one.
637 638 639
     */
    void SetPageLayout( const char* aPageLayout, bool Append = false );

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    /**
     * @return a short filename  from a full filename:
     * if the path is the current path, or if the path
     * is the same as kicad.pro (in template), returns the shortname
     * else do nothing and returns a full filename
     */
    static const wxString MakeShortFileName( const wxString& aFullFileName );

    /**
     * @return a full filename from a short filename,
     * if the short filename path is void
     * In this case the path is the same as kicad.pro (in template)
     * else return the short filename (which have an absolute os relative path
      */
    static const wxString MakeFullFileName( const wxString& aShortFileName );
655 656
};

657
#endif      // WORKSHEET_SHAPE_BUILDER_H