base_struct.cpp 14.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.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
 */

25 26
/**
 * @file base_struct.cpp
27
 * @brief Implementation of EDA_ITEM base class for KiCad.
28
 */
plyatov's avatar
plyatov committed
29

30 31 32 33
#include <fctsys.h>
#include <trigo.h>
#include <common.h>
#include <macros.h>
34
#include <kicad_string.h>
35 36 37
#include <wxstruct.h>
#include <class_drawpanel.h>
#include <class_base_screen.h>
38

39 40
#include "../eeschema/dialogs/dialog_schematic_find.h"

41 42 43 44

const wxString traceFindReplace( wxT( "KicadFindReplace" ) );


plyatov's avatar
plyatov committed
45
enum textbox {
46
    ID_TEXTBOX_LIST = 8010
plyatov's avatar
plyatov committed
47 48 49
};


50
EDA_ITEM::EDA_ITEM( EDA_ITEM* parent, KICAD_T idType )
plyatov's avatar
plyatov committed
51
{
52 53
    InitVars();
    m_StructType = idType;
54
    m_Parent     = parent;
plyatov's avatar
plyatov committed
55
}
56

plyatov's avatar
plyatov committed
57

58
EDA_ITEM::EDA_ITEM( KICAD_T idType )
59
{
60 61
    InitVars();
    m_StructType = idType;
plyatov's avatar
plyatov committed
62
}
63

64

65
EDA_ITEM::EDA_ITEM( const EDA_ITEM& base )
66
{
67
    InitVars();
68 69 70
    m_StructType = base.m_StructType;
    m_Parent     = base.m_Parent;
    m_Flags      = base.m_Flags;
71 72 73

    // A copy of an item cannot have the same time stamp as the original item.
    SetTimeStamp( GetNewTimeStamp() );
74 75 76 77
    m_Status     = base.m_Status;
}


78
void EDA_ITEM::InitVars()
79
{
80
    m_StructType = TYPE_NOT_INIT;
81 82 83 84 85 86
    Pnext       = NULL;     // Linked list: Link (next struct)
    Pback       = NULL;     // Linked list: Link (previous struct)
    m_Parent    = NULL;     // Linked list: Link (parent struct)
    m_List      = NULL;     // I am not on any list yet
    m_Image     = NULL;     // Link to an image copy for undelete or abort command
    m_Flags     = 0;        // flags for editions and other
87
    SetTimeStamp( 0 );      // Time stamp used for logical links
88
    m_Status    = 0;
89
    m_forceVisible = false; // true to override the visibility setting of the item.
90 91
}

plyatov's avatar
plyatov committed
92

93
void EDA_ITEM::SetModified()
94
{
95
    SetFlags( IS_CHANGED );
96 97 98 99 100 101 102

    // If this a child object, then the parent modification state also needs to be set.
    if( m_Parent )
        m_Parent->SetModified();
}


Dick Hollenbeck's avatar
Dick Hollenbeck committed
103 104
EDA_ITEM* EDA_ITEM::Clone() const
{
105 106
    wxCHECK_MSG( false, NULL, wxT( "Clone not implemented in derived class " ) + GetClass() +
                 wxT( ".  Bad programmer!" ) );
Dick Hollenbeck's avatar
Dick Hollenbeck committed
107 108 109
}


110 111 112 113
SEARCH_RESULT EDA_ITEM::IterateForward( EDA_ITEM*     listStart,
                                        INSPECTOR*    inspector,
                                        const void*   testData,
                                        const KICAD_T scanTypes[] )
114
{
115
    EDA_ITEM* p = listStart;
116

117 118 119 120 121 122 123 124 125 126 127 128
    for( ; p; p = p->Pnext )
    {
        if( SEARCH_QUIT == p->Visit( inspector, testData, scanTypes ) )
            return SEARCH_QUIT;
    }

    return SEARCH_CONTINUE;
}


// see base_struct.h
// many classes inherit this method, be careful:
129 130
SEARCH_RESULT EDA_ITEM::Visit( INSPECTOR* inspector, const void* testData,
                               const KICAD_T scanTypes[] )
131
{
132
    KICAD_T stype;
133

134
#if 0 && defined(DEBUG)
135
    std::cout << GetClass().mb_str() << ' ';
136
#endif
dickelbeck's avatar
dickelbeck committed
137

138
    for( const KICAD_T* p = scanTypes;  (stype = *p) != EOT;   ++p )
139 140
    {
        // If caller wants to inspect my type
141
        if( stype == Type() )
142 143 144 145 146 147 148 149
        {
            if( SEARCH_QUIT == inspector->Inspect( this, testData ) )
                return SEARCH_QUIT;

            break;
        }
    }

dickelbeck's avatar
dickelbeck committed
150
    return SEARCH_CONTINUE;
151 152
}

153 154 155 156 157 158 159 160 161 162

wxString EDA_ITEM::GetSelectMenuText() const
{
    wxFAIL_MSG( wxT( "GetSelectMenuText() was not overridden for schematic item type " ) +
                GetClass() );

    return wxString( wxT( "Undefined menu text for " ) + GetClass() );
}


163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
bool EDA_ITEM::Matches( const wxString& aText, wxFindReplaceData& aSearchData )
{
    wxString text = aText;
    wxString searchText = aSearchData.GetFindString();

    // Don't match if searching for replaceable item and the item doesn't support text replace.
    if( (aSearchData.GetFlags() & FR_SEARCH_REPLACE) && !IsReplaceable() )
        return false;

    if( aSearchData.GetFlags() & wxFR_WHOLEWORD )
        return aText.IsSameAs( searchText, aSearchData.GetFlags() & wxFR_MATCHCASE );

    if( aSearchData.GetFlags() & FR_MATCH_WILDCARD )
    {
        if( aSearchData.GetFlags() & wxFR_MATCHCASE )
            return text.Matches( searchText );

        return text.MakeUpper().Matches( searchText.MakeUpper() );
    }

    if( aSearchData.GetFlags() & wxFR_MATCHCASE )
        return aText.Find( searchText ) != wxNOT_FOUND;

    return text.MakeUpper().Find( searchText.MakeUpper() ) != wxNOT_FOUND;
}


190 191 192 193 194
bool EDA_ITEM::Replace( wxFindReplaceData& aSearchData, wxString& aText )
{
    wxCHECK_MSG( IsReplaceable(), false,
                 wxT( "Attempt to replace text in <" ) + GetClass() + wxT( "> item." ) );

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    wxString searchString = (aSearchData.GetFlags() & wxFR_MATCHCASE) ? aText.Upper() : aText;

    int result = searchString.Find( (aSearchData.GetFlags() & wxFR_MATCHCASE) ?
                                    aSearchData.GetFindString() :
                                    aSearchData.GetFindString().Upper() );

    if( result == wxNOT_FOUND )
        return false;

    wxString prefix = aText.Left( result );
    wxString suffix;

    if( aSearchData.GetFindString().length() + result < aText.length() )
        suffix = aText.Right( aText.length() - ( aSearchData.GetFindString().length() + result ) );

    wxLogTrace( traceFindReplace, wxT( "Replacing '%s', prefix '%s', replace '%s', suffix '%s'." ),
                GetChars( aText ), GetChars( prefix ), GetChars( aSearchData.GetReplaceString() ),
                GetChars( suffix ) );

    aText = prefix + aSearchData.GetReplaceString() + suffix;

    return true;
217 218 219
}


220 221 222 223 224 225 226 227 228
bool EDA_ITEM::operator<( const EDA_ITEM& aItem ) const
{
    wxFAIL_MSG( wxString::Format( wxT( "Less than operator not defined for item type %s." ),
                                  GetChars( GetClass() ) ) );

    return false;
}


229 230 231 232
EDA_ITEM& EDA_ITEM::operator=( const EDA_ITEM& aItem )
{
    if( &aItem != this )
    {
233
        m_Image = aItem.m_Image;
234
        m_StructType = aItem.m_StructType;
235 236
        m_Parent = aItem.m_Parent;
        m_Flags = aItem.m_Flags;
237
        m_TimeStamp = aItem.m_TimeStamp;
238
        m_Status = aItem.m_Status;
239
        m_forceVisible = aItem.m_forceVisible;
240 241 242 243 244 245
    }

    return *this;
}


246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
const BOX2I EDA_ITEM::ViewBBox() const
{
    // Basic fallback
    return BOX2I( VECTOR2I( GetBoundingBox().GetOrigin() ),
                  VECTOR2I( GetBoundingBox().GetSize() ) );
}


void EDA_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
{
    // Basic fallback
    aCount      = 1;
    aLayers[0]  = 0;
}


262
#if defined(DEBUG)
263

264
// A function that should have been in wxWidgets
265
std::ostream& operator<<( std::ostream& out, const wxSize& size )
266 267 268 269 270
{
    out << " width=\"" << size.GetWidth() << "\" height=\"" << size.GetHeight() << "\"";
    return out;
}

271

272
// A function that should have been in wxWidgets
273
std::ostream& operator<<( std::ostream& out, const wxPoint& pt )
274 275 276 277 278
{
    out << " x=\"" << pt.x << "\" y=\"" << pt.y << "\"";
    return out;
}

279

280
void EDA_ITEM::ShowDummy( std::ostream& os ) const
281
{
282
    // XML output:
dickelbeck's avatar
dickelbeck committed
283
    wxString s = GetClass();
284

285 286 287
    os << '<' << s.Lower().mb_str() << ">"
       << " Need ::Show() override for this class "
       << "</" << s.Lower().mb_str() << ">\n";
288 289
}

dickelbeck's avatar
dickelbeck committed
290

291
std::ostream& EDA_ITEM::NestedSpace( int nestLevel, std::ostream& os )
292
{
293
    for( int i = 0; i<nestLevel; ++i )
294 295 296
        os << "  ";

    // number of spaces here controls indent per nest level
297

298 299 300
    return os;
}

301
#endif
302

303

304
/******************/
305
/* Class EDA_RECT */
306 307
/******************/

308
void EDA_RECT::Normalize()
plyatov's avatar
plyatov committed
309
{
310 311 312 313 314
    if( m_Size.y < 0 )
    {
        m_Size.y = -m_Size.y;
        m_Pos.y -= m_Size.y;
    }
315

316 317 318 319 320
    if( m_Size.x < 0 )
    {
        m_Size.x = -m_Size.x;
        m_Pos.x -= m_Size.x;
    }
plyatov's avatar
plyatov committed
321 322 323
}


324
void EDA_RECT::Move( const wxPoint& aMoveVector )
325 326 327 328
{
    m_Pos += aMoveVector;
}

329

330
bool EDA_RECT::Contains( const wxPoint& aPoint ) const
plyatov's avatar
plyatov committed
331
{
332
    wxPoint rel_pos = aPoint - m_Pos;
333
    wxSize size     = m_Size;
334 335 336

    if( size.x < 0 )
    {
337
        size.x    = -size.x;
338
        rel_pos.x += size.x;
339 340 341 342
    }

    if( size.y < 0 )
    {
343
        size.y    = -size.y;
344
        rel_pos.y += size.y;
345
    }
dickelbeck's avatar
dickelbeck committed
346

347
    return (rel_pos.x >= 0) && (rel_pos.y >= 0) && ( rel_pos.y <= size.y) && ( rel_pos.x <= size.x);
348 349
}

350

351 352 353
/*
 * return true if aRect is inside me (or on boundaries)
 */
354
bool EDA_RECT::Contains( const EDA_RECT& aRect ) const
355
{
356
    return Contains( aRect.GetOrigin() ) && Contains( aRect.GetEnd() );
plyatov's avatar
plyatov committed
357 358
}

359

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
/* Intersects
 * test for a common area between segment and rect.
 * return true if at least a common point is found
 */
bool EDA_RECT::Intersects( const wxPoint& aPoint1, const wxPoint& aPoint2 ) const
{
    wxPoint point2, point4;

    if( Contains( aPoint1 ) || Contains( aPoint2 ) )
        return true;

    point2.x = GetEnd().x;
    point2.y = GetOrigin().y;
    point4.x = GetOrigin().x;
    point4.y = GetEnd().y;

    //Only need to test 3 sides since a straight line cant enter and exit on same side
    if( SegmentIntersectsSegment( aPoint1, aPoint2, GetOrigin() , point2 ) )
        return true;

    if( SegmentIntersectsSegment( aPoint1, aPoint2, point2      , GetEnd() ) )
        return true;

    if( SegmentIntersectsSegment( aPoint1, aPoint2, GetEnd()    , point4 ) )
        return true;

    return false;
}


390 391 392 393
/* Intersects
 * test for a common area between 2 rect.
 * return true if at least a common point is found
 */
394
bool EDA_RECT::Intersects( const EDA_RECT& aRect ) const
dickelbeck's avatar
dickelbeck committed
395 396
{
    // this logic taken from wxWidgets' geometry.cpp file:
dickelbeck's avatar
dickelbeck committed
397
    bool rc;
398 399
    EDA_RECT me(*this);
    EDA_RECT rect(aRect);
400 401 402 403
    me.Normalize();         // ensure size is >= 0
    rect.Normalize();       // ensure size is >= 0

    // calculate the left common area coordinate:
404
    int  left   = std::max( me.m_Pos.x, rect.m_Pos.x );
405
    // calculate the right common area coordinate:
406
    int  right  = std::min( me.m_Pos.x + me.m_Size.x, rect.m_Pos.x + rect.m_Size.x );
407
    // calculate the upper common area coordinate:
408
    int  top    = std::max( me.m_Pos.y, aRect.m_Pos.y );
409
    // calculate the lower common area coordinate:
410
    int  bottom = std::min( me.m_Pos.y + me.m_Size.y, rect.m_Pos.y + rect.m_Size.y );
411 412 413

    // if a common area exists, it must have a positive (null accepted) size
    if( left <= right && top <= bottom )
dickelbeck's avatar
dickelbeck committed
414 415 416 417 418
        rc = true;
    else
        rc = false;

    return rc;
dickelbeck's avatar
dickelbeck committed
419 420 421
}


422
EDA_RECT& EDA_RECT::Inflate( int aDelta )
423 424
{
    Inflate( aDelta, aDelta );
425
    return *this;
426 427
}

428

429
EDA_RECT& EDA_RECT::Inflate( wxCoord dx, wxCoord dy )
plyatov's avatar
plyatov committed
430
{
431
    if( m_Size.x >= 0 )
432
    {
433 434 435 436 437 438 439 440 441 442 443 444
        if( m_Size.x < -2 * dx )
        {
            // Don't allow deflate to eat more width than we have,
            m_Pos.x += m_Size.x / 2;
            m_Size.x = 0;
        }
        else
        {
            // The inflate is valid.
            m_Pos.x  -= dx;
            m_Size.x += 2 * dx;
        }
445
    }
446
    else    // size.x < 0:
447
    {
448 449 450 451 452 453 454 455 456 457 458 459
        if( m_Size.x > -2 * dx )
        {
            // Don't allow deflate to eat more width than we have,
            m_Pos.x -= m_Size.x / 2;
            m_Size.x = 0;
        }
        else
        {
            // The inflate is valid.
            m_Pos.x  += dx;
            m_Size.x -= 2 * dx; // m_Size.x <0: inflate when dx > 0
        }
460 461
    }

462
    if( m_Size.y >= 0 )
463
    {
464 465 466 467 468 469 470 471 472 473 474 475
        if( m_Size.y < -2 * dy )
        {
            // Don't allow deflate to eat more height than we have,
            m_Pos.y += m_Size.y / 2;
            m_Size.y = 0;
        }
        else
        {
            // The inflate is valid.
            m_Pos.y  -= dy;
            m_Size.y += 2 * dy;
        }
476
    }
477
    else    // size.y < 0:
478
    {
479 480 481 482 483 484 485 486 487 488 489 490
        if( m_Size.y > 2 * dy )
        {
            // Don't allow deflate to eat more height than we have,
            m_Pos.y -= m_Size.y / 2;
            m_Size.y = 0;
        }
        else
        {
            // The inflate is valid.
            m_Pos.y  += dy;
            m_Size.y -= 2 * dy; // m_Size.y <0: inflate when dy > 0
        }
491
    }
plyatov's avatar
plyatov committed
492 493 494 495 496

    return *this;
}


497
void EDA_RECT::Merge( const EDA_RECT& aRect )
498 499
{
    Normalize();        // ensure width and height >= 0
500
    EDA_RECT rect = aRect;
501
    rect.Normalize();   // ensure width and height >= 0
502
    wxPoint  end = GetEnd();
503
    wxPoint  rect_end = rect.GetEnd();
504

dickelbeck's avatar
dickelbeck committed
505
    // Change origin and size in order to contain the given rect
506 507 508 509
    m_Pos.x = std::min( m_Pos.x, rect.m_Pos.x );
    m_Pos.y = std::min( m_Pos.y, rect.m_Pos.y );
    end.x   = std::max( end.x, rect_end.x );
    end.y   = std::max( end.y, rect_end.y );
510 511
    SetEnd( end );
}
512

513

514
void EDA_RECT::Merge( const wxPoint& aPoint )
515 516 517 518 519
{
    Normalize();        // ensure width and height >= 0

    wxPoint  end = GetEnd();
    // Change origin and size in order to contain the given rect
520 521 522 523
    m_Pos.x = std::min( m_Pos.x, aPoint.x );
    m_Pos.y = std::min( m_Pos.y, aPoint.y );
    end.x   = std::max( end.x, aPoint.x );
    end.y   = std::max( end.y, aPoint.y );
524 525
    SetEnd( end );
}
526 527


528
double EDA_RECT::GetArea() const
529 530 531
{
    return (double) GetWidth() * (double) GetHeight();
}
532 533 534

/* Calculate the bounding box of this, when rotated
 */
535
const EDA_RECT EDA_RECT::GetBoundingBoxRotated( wxPoint aRotCenter, double aAngle )
536 537 538 539 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
{
    wxPoint corners[4];

    // Build the corners list
    corners[0]   = GetOrigin();
    corners[2]   = GetEnd();
    corners[1].x = corners[0].x;
    corners[1].y = corners[2].y;
    corners[3].x = corners[2].x;
    corners[3].y = corners[0].y;

    // Rotate all corners, to find the bounding box
    for( int ii = 0; ii < 4; ii ++ )
        RotatePoint( &corners[ii], aRotCenter, aAngle );

    // Find the corners bounding box
    wxPoint start = corners[0];
    wxPoint end = corners[0];

    for( int ii = 1; ii < 4; ii ++ )
    {
        start.x = std::min( start.x, corners[ii].x);
        start.y = std::min( start.y, corners[ii].y);
        end.x = std::max( end.x, corners[ii].x);
        end.y = std::max( end.y, corners[ii].y);
    }

    EDA_RECT bbox;
    bbox.SetOrigin( start );
    bbox.SetEnd( end );

    return bbox;
}