class_worksheet_dataitem.cpp 16.6 KB
Newer Older
1
/**
2
 * @file class_worksheet_dataitem.cpp
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * @brief description of graphic items and texts to build a title block
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 1992-2013 Jean-Pierre Charras <jp.charras at wanadoo.fr>.
 * Copyright (C) 1992-2013 KiCad Developers, see change_log.txt for contributors.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

31 32

/*
33
 * the class WORKSHEET_DATAITEM (and derived) defines
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 * a basic shape of a page layout ( frame references and title block )
 * Basic shapes are line, rect and texts
 * the WORKSHEET_DATAITEM coordinates units is the mm, and are relative to
 * one of 4 page corners.
 *
 * These items cannot be drawn or plot "as this". they should be converted
 * to a "draw list" (WS_DRAW_ITEM_BASE and derived items)

 * The list of these items is stored in a WORKSHEET_LAYOUT instance.
 *
 * When building the draw list:
 * the WORKSHEET_LAYOUT is used to create a WS_DRAW_ITEM_LIST
 *  coordinates are converted to draw/plot coordinates.
 *  texts are expanded if they contain format symbols.
 *  Items with m_RepeatCount > 1 are created m_RepeatCount times
 *
 * the WORKSHEET_LAYOUT is created only once.
 * the WS_DRAW_ITEM_LIST is created each time the page layout is plot/drawn
 *
 * the WORKSHEET_LAYOUT instance is created from a S expression which
 * describes the page layout (can be the default page layout or a custom file).
 */

57 58 59 60 61
#include <fctsys.h>
#include <drawtxt.h>
#include <worksheet.h>
#include <class_title_block.h>
#include <worksheet_shape_builder.h>
62
#include <class_worksheet_dataitem.h>
63

64 65 66 67 68 69 70 71 72 73 74

// Static members of class WORKSHEET_DATAITEM:
double WORKSHEET_DATAITEM::m_WSunits2Iu = 1.0;
DPOINT WORKSHEET_DATAITEM::m_RB_Corner;
DPOINT WORKSHEET_DATAITEM::m_LT_Corner;
double WORKSHEET_DATAITEM::m_DefaultLineWidth = 0.0;
DSIZE  WORKSHEET_DATAITEM::m_DefaultTextSize( TB_DEFAULT_TEXTSIZE, TB_DEFAULT_TEXTSIZE );
double WORKSHEET_DATAITEM::m_DefaultTextThickness = 0.0;
bool WORKSHEET_DATAITEM::m_SpecialMode = false;
EDA_COLOR_T WORKSHEET_DATAITEM::m_Color = RED;              // the default color to draw items
EDA_COLOR_T WORKSHEET_DATAITEM::m_AltColor = RED;           // an alternate color to draw items
75
EDA_COLOR_T WORKSHEET_DATAITEM::m_SelectedColor = BROWN;   // the color to draw selected items
76 77 78

// The constructor:
WORKSHEET_DATAITEM::WORKSHEET_DATAITEM( WS_ItemType aType )
79
{
80 81 82 83 84
    m_type = aType;
    m_flags = 0;
    m_RepeatCount = 1;
    m_IncrementLabel = 1;
    m_LineWidth = 0;
85 86
}

87 88 89 90 91 92 93
// move item to aPosition
// starting point is moved to aPosition
// the Ending point is moved to a position which keeps the item size
// (if both coordinates have the same corner reference)
// MoveToUi and MoveTo takes the graphic position (i.e relative to the left top
// paper corner
void WORKSHEET_DATAITEM::MoveToUi( wxPoint aPosition )
94
{
95 96 97
    DPOINT pos_mm;
    pos_mm.x = aPosition.x / m_WSunits2Iu;
    pos_mm.y = aPosition.y / m_WSunits2Iu;
98

99
    MoveTo( pos_mm );
100 101
}

102
void WORKSHEET_DATAITEM::MoveTo( DPOINT aPosition )
103
{
104 105
    DPOINT vector = aPosition - GetStartPos();
    DPOINT endpos = vector + GetEndPos();
106

107 108
    MoveStartPointTo( aPosition );
    MoveEndPointTo( endpos );
109 110
}

111 112 113 114
/* move the starting point of the item to a new position
 * aPosition = the new position of the starting point, in mm
 */
void WORKSHEET_DATAITEM::MoveStartPointTo( DPOINT aPosition )
115
{
116
    DPOINT position;
117

118 119 120 121 122 123 124 125
    // Calculate the position of the starting point
    // relative to the reference corner
    // aPosition is the position relative to the right top paper corner
    switch( m_Pos.m_Anchor )
    {
        case RB_CORNER:
            position = m_RB_Corner - aPosition;
            break;
126

127 128 129 130
        case RT_CORNER:
            position.x = m_RB_Corner.x - aPosition.x;
            position.y = aPosition.y - m_LT_Corner.y;
            break;
131

132 133 134 135
        case LB_CORNER:
            position.x = aPosition.x - m_LT_Corner.x;
            position.y = m_RB_Corner.y - aPosition.y;
            break;
136

137 138 139 140
        case LT_CORNER:
            position = aPosition - m_LT_Corner;
            break;
    }
141

142
    m_Pos.m_Pos = position;
143 144
}

145 146 147 148
/* move the starting point of the item to a new position
 * aPosition = the new position of the starting point in graphic units
 */
void WORKSHEET_DATAITEM::MoveStartPointToUi( wxPoint aPosition )
149
{
150 151 152
    DPOINT pos_mm;
    pos_mm.x = aPosition.x / m_WSunits2Iu;
    pos_mm.y = aPosition.y / m_WSunits2Iu;
153

154
    MoveStartPointTo( pos_mm );
155 156
}

157 158 159 160 161 162 163
/**
 * move the ending point of the item to a new position
 * has meaning only for items defined by 2 points
 * (segments and rectangles)
 * aPosition = the new position of the ending point, in mm
 */
void WORKSHEET_DATAITEM::MoveEndPointTo( DPOINT aPosition )
164
{
165
    DPOINT position;
166

167 168 169 170
    // Calculate the position of the starting point
    // relative to the reference corner
    // aPosition is the position relative to the right top paper corner
    switch( m_End.m_Anchor )
171
    {
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        case RB_CORNER:
            position = m_RB_Corner - aPosition;
            break;

        case RT_CORNER:
            position.x = m_RB_Corner.x - aPosition.x;
            position.y = aPosition.y - m_LT_Corner.y;
            break;

        case LB_CORNER:
            position.x = aPosition.x - m_LT_Corner.x;
            position.y = m_RB_Corner.y - aPosition.y;
            break;

        case LT_CORNER:
            position = aPosition - m_LT_Corner;
            break;
189 190
    }

191 192
    // Modify m_End only for items having 2 coordinates
    switch( GetType() )
193
    {
194 195 196 197 198 199 200
        case WS_SEGMENT:
        case WS_RECT:
            m_End.m_Pos = position;
            break;

        default:
            break;
201 202 203
    }
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217
/* move the ending point of the item to a new position
 * has meaning only for items defined by 2 points
 * (segments and rectangles)
 * aPosition = the new position of the ending point in graphic units
 */
void WORKSHEET_DATAITEM::MoveEndPointToUi( wxPoint aPosition )
{
    DPOINT pos_mm;
    pos_mm.x = aPosition.x / m_WSunits2Iu;
    pos_mm.y = aPosition.y / m_WSunits2Iu;

    MoveEndPointTo( pos_mm );
}

218
const DPOINT WORKSHEET_DATAITEM::GetStartPos( int ii ) const
219
{
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    DPOINT pos;
    pos.x = m_Pos.m_Pos.x + ( m_IncrementVector.x * ii );
    pos.y = m_Pos.m_Pos.y + ( m_IncrementVector.y * ii );

    switch( m_Pos.m_Anchor )
    {
        case RB_CORNER:      // right bottom corner
            pos = m_RB_Corner - pos;
            break;

        case RT_CORNER:      // right top corner
            pos.x = m_RB_Corner.x - pos.x;
            pos.y = m_LT_Corner.y + pos.y;
            break;

        case LB_CORNER:      // left bottom corner
            pos.x = m_LT_Corner.x + pos.x;
            pos.y = m_RB_Corner.y - pos.y;
            break;

        case LT_CORNER:      // left top corner
            pos = m_LT_Corner + pos;
            break;
    }

    return pos;
}

const wxPoint WORKSHEET_DATAITEM::GetStartPosUi( int ii ) const
249
{
250 251 252 253 254 255
    DPOINT pos = GetStartPos( ii );
    pos = pos * m_WSunits2Iu;
    return wxPoint( int(pos.x), int(pos.y) );
}

const DPOINT WORKSHEET_DATAITEM::GetEndPos( int ii ) const
256
{
257 258 259 260 261 262 263 264
    DPOINT pos;
    pos.x = m_End.m_Pos.x + ( m_IncrementVector.x * ii );
    pos.y = m_End.m_Pos.y + ( m_IncrementVector.y * ii );
    switch( m_End.m_Anchor )
    {
        case RB_CORNER:      // right bottom corner
            pos = m_RB_Corner - pos;
            break;
265

266 267 268 269
        case RT_CORNER:      // right top corner
            pos.x = m_RB_Corner.x - pos.x;
            pos.y = m_LT_Corner.y + pos.y;
            break;
270

271 272 273 274
        case LB_CORNER:      // left bottom corner
            pos.x = m_LT_Corner.x + pos.x;
            pos.y = m_RB_Corner.y - pos.y;
            break;
275

276 277 278 279 280 281 282
        case LT_CORNER:      // left top corner
            pos = m_LT_Corner + pos;
            break;
    }

    return pos;
}
283

284
const wxPoint WORKSHEET_DATAITEM::GetEndPosUi( int ii ) const
285
{
286 287 288 289
    DPOINT pos = GetEndPos( ii );
    pos = pos * m_WSunits2Iu;
    return wxPoint( int(pos.x), int(pos.y) );
}
290 291


292
bool WORKSHEET_DATAITEM::IsInsidePage( int ii ) const
293
{
294
    DPOINT pos = GetStartPos( ii );
295

296 297 298 299
    for( int kk = 0; kk < 1; kk++ )
    {
        if( m_RB_Corner.x < pos.x || m_LT_Corner.x > pos.x )
            return false;
300

301 302
        if( m_RB_Corner.y < pos.y || m_LT_Corner.y > pos.y )
            return false;
303

304 305
        pos = GetEndPos( ii );
    }
306 307 308 309

    return true;
}

310 311 312 313 314 315 316 317 318
const wxString WORKSHEET_DATAITEM::GetClassName() const
{
    wxString name;
    switch( GetType() )
    {
        case WS_TEXT: name = wxT("Text"); break;
        case WS_SEGMENT: name = wxT("Line"); break;
        case WS_RECT: name = wxT("Rect"); break;
        case WS_POLYPOLYGON: name = wxT("Poly"); break;
319
        case WS_BITMAP: name = wxT("Bitmap"); break;
320
    }
321

322 323
    return name;
}
324

325 326 327 328 329
/* return 0 if the item has no specific option for page 1
 * 1  if the item is only on page 1
 * -1  if the item is not on page 1
 */
int WORKSHEET_DATAITEM::GetPage1Option()
330
{
331 332 333 334 335 336 337 338
    if(( m_flags & PAGE1OPTION) == PAGE1OPTION_NOTONPAGE1 )
        return -1;

    if(( m_flags & PAGE1OPTION) == PAGE1OPTION_PAGE1ONLY )
        return 1;

    return 0;
}
339

340 341 342 343 344 345 346 347
/* Set the option for page 1
 * aChoice = 0 if the item has no specific option for page 1
 * > 0  if the item is only on page 1
 * < 0  if the item is not on page 1
 */
void WORKSHEET_DATAITEM::SetPage1Option( int aChoice )
{
    ClearFlags( PAGE1OPTION );
348

349 350
    if( aChoice > 0)
        SetFlags( PAGE1OPTION_PAGE1ONLY );
351

352 353
    else if( aChoice < 0)
        SetFlags( PAGE1OPTION_NOTONPAGE1 );
354

355
}
356 357


358 359 360 361 362
WORKSHEET_DATAITEM_POLYPOLYGON::WORKSHEET_DATAITEM_POLYPOLYGON() :
    WORKSHEET_DATAITEM( WS_POLYPOLYGON )
{
    m_Orient = 0.0;
}
363

364 365 366 367 368 369 370 371 372 373 374 375 376 377
const DPOINT WORKSHEET_DATAITEM_POLYPOLYGON::GetCornerPosition( unsigned aIdx,
                                                         int aRepeat ) const
{
    DPOINT pos = m_Corners[aIdx];

    // Rotation:
    RotatePoint( &pos.x, &pos.y, m_Orient * 10 );
    pos += GetStartPos( aRepeat );
    return pos;
}

void WORKSHEET_DATAITEM_POLYPOLYGON::SetBoundingBox()
{
    if( m_Corners.size() == 0 )
378
    {
379 380 381 382
        m_minCoord.x = m_maxCoord.x = 0.0;
        m_minCoord.y = m_maxCoord.y = 0.0;
        return;
    }
383

384 385 386 387
    DPOINT pos;
    pos = m_Corners[0];
    RotatePoint( &pos.x, &pos.y, m_Orient * 10 );
    m_minCoord = m_maxCoord = pos;
388

389 390 391 392
    for( unsigned ii = 1; ii < m_Corners.size(); ii++ )
    {
        pos = m_Corners[ii];
        RotatePoint( &pos.x, &pos.y, m_Orient * 10 );
393

394 395 396 397 398
        if( m_minCoord.x > pos.x )
            m_minCoord.x = pos.x;

        if( m_minCoord.y > pos.y )
            m_minCoord.y = pos.y;
399

400 401
        if( m_maxCoord.x < pos.x )
            m_maxCoord.x = pos.x;
402

403 404 405 406
        if( m_maxCoord.y < pos.y )
            m_maxCoord.y = pos.y;
    }
}
407

408 409 410 411
bool WORKSHEET_DATAITEM_POLYPOLYGON::IsInsidePage( int ii ) const
{
    DPOINT pos = GetStartPos( ii );
    pos += m_minCoord;  // left top pos of bounding box
412

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    if( m_LT_Corner.x > pos.x || m_LT_Corner.y > pos.y )
        return false;

    pos = GetStartPos( ii );
    pos += m_maxCoord;  // rignt bottom pos of bounding box

    if( m_RB_Corner.x < pos.x || m_RB_Corner.y < pos.y )
        return false;

    return true;
}

const wxPoint WORKSHEET_DATAITEM_POLYPOLYGON::GetCornerPositionUi( unsigned aIdx,
                                                            int aRepeat ) const
{
    DPOINT pos = GetCornerPosition( aIdx, aRepeat );
    pos = pos * m_WSunits2Iu;
    return wxPoint( int(pos.x), int(pos.y) );
}

433
WORKSHEET_DATAITEM_TEXT::WORKSHEET_DATAITEM_TEXT( const wxString& aTextBase ) :
434 435 436 437 438 439 440
    WORKSHEET_DATAITEM( WS_TEXT )
{
    m_TextBase = aTextBase;
    m_IncrementLabel = 1;
    m_Hjustify = GR_TEXT_HJUSTIFY_LEFT;
    m_Vjustify = GR_TEXT_VJUSTIFY_CENTER;
    m_Orient = 0.0;
441
    m_LineWidth = 0.0;      // 0.0 means use default value
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
}

void WORKSHEET_DATAITEM_TEXT::TransfertSetupToGraphicText( WS_DRAW_ITEM_TEXT* aGText )
{
    aGText->SetHorizJustify( m_Hjustify ) ;
    aGText->SetVertJustify( m_Vjustify );
    aGText->SetOrientation( m_Orient * 10 );    // graphic text orient unit = 0.1 degree
}

void WORKSHEET_DATAITEM_TEXT::IncrementLabel( int aIncr )
{
    int last = m_TextBase.Len() -1;

    wxChar lbchar = m_TextBase[last];
    m_FullText = m_TextBase;
    m_FullText.RemoveLast();

    if( lbchar >= '0' &&  lbchar <= '9' )
        // A number is expected:
        m_FullText << (int)( aIncr + lbchar - '0' );
    else
        m_FullText << (wxChar) ( aIncr + lbchar );
}

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 494 495 496 497 498 499 500 501 502
// Replace the '\''n' sequence by EOL
// and the sequence  '\''\' by only one '\' in m_FullText
// if m_FullTextis a multiline text (i;e.contains '\n') return true
bool WORKSHEET_DATAITEM_TEXT::ReplaceAntiSlashSequence()
{
    bool multiline = false;

    for( unsigned ii = 0; ii < m_FullText.Len(); ii++ )
    {
        if( m_FullText[ii] == '\n' )
            multiline = true;

        else if( m_FullText[ii] == '\\' )
        {
            if( ++ii >= m_FullText.Len() )
                break;

            if( m_FullText[ii] == '\\' )
            {
                // a double \\ sequence is replaced by a single \ char
                m_FullText.Remove(ii, 1);
                ii--;
            }
            else if( m_FullText[ii] == 'n' )
            {
                // Replace the "\n" sequence by a EOL char
                multiline = true;
                m_FullText[ii] = '\n';
                m_FullText.Remove(ii-1, 1);
                ii--;
            }
        }
    }

    return multiline;
}

503 504 505 506 507 508 509 510 511 512
void WORKSHEET_DATAITEM_TEXT::SetConstrainedTextSize()
{
    m_ConstrainedTextSize = m_TextSize;

    if( m_ConstrainedTextSize.x == 0  )
        m_ConstrainedTextSize.x = m_DefaultTextSize.x;

    if( m_ConstrainedTextSize.y == 0 )
        m_ConstrainedTextSize.y = m_DefaultTextSize.y;

513
    if( m_BoundingBoxSize.x || m_BoundingBoxSize.y )
514 515
    {
        int linewidth = 0;
516 517
        // to know the X and Y size of the line, we should use
        // EDA_TEXT::GetTextBox()
518 519
        // but this function uses integers
        // So, to avoid truncations with our unit in mm, use microns.
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
        wxSize size_micron;
        size_micron.x = KiROUND( m_ConstrainedTextSize.x * 1000.0 );
        size_micron.y = KiROUND( m_ConstrainedTextSize.y * 1000.0 );
        WS_DRAW_ITEM_TEXT dummy( WS_DRAW_ITEM_TEXT( this, this->m_FullText,
                                               wxPoint(0,0),
                                               size_micron,
                                               linewidth, BLACK,
                                               IsItalic(), IsBold() ) );
        dummy.SetMultilineAllowed( true );
        TransfertSetupToGraphicText( &dummy );
        EDA_RECT rect = dummy.GetTextBox();
        DSIZE size;
        size.x = rect.GetWidth() / 1000.0;
        size.y = rect.GetHeight() / 1000.0;

        if( m_BoundingBoxSize.x && size.x > m_BoundingBoxSize.x )
            m_ConstrainedTextSize.x *= m_BoundingBoxSize.x / size.x;

        if( m_BoundingBoxSize.y &&  size.y > m_BoundingBoxSize.y )
            m_ConstrainedTextSize.y *= m_BoundingBoxSize.y / size.y;
540 541
    }
}
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
/* set the pixel scale factor of the bitmap
 * this factor depend on the application internal unit
 * and the PPI bitmap factor
 * the pixel scale factor should be initialized before drawing the bitmap
 */
void WORKSHEET_DATAITEM_BITMAP::SetPixelScaleFactor()
{
    if( m_ImageBitmap )
    {
        // m_WSunits2Iu is the page layout unit to application internal unit
        // i.e. the mm to to application internal unit
        // however the bitmap definition is always known in pixels per inches
        double scale = m_WSunits2Iu * 25.4 / m_ImageBitmap->GetPPI();
        m_ImageBitmap->SetPixelScaleFactor( scale );
    }
}

/* return the PPI of the bitmap
 */
int WORKSHEET_DATAITEM_BITMAP::GetPPI() const
{
    if( m_ImageBitmap )
        return m_ImageBitmap->GetPPI() / m_ImageBitmap->m_Scale;

    return 300;
}

/*adjust the PPI of the bitmap
 */
void WORKSHEET_DATAITEM_BITMAP::SetPPI( int aBitmapPPI )
{
    if( m_ImageBitmap )
        m_ImageBitmap->m_Scale = (double) m_ImageBitmap->GetPPI() / aBitmapPPI;
}
577