summaryrefslogtreecommitdiff
path: root/pcbnew/collectors.h
blob: b2d5c9dc0a59bc306cbe680ed45c91718b7ad384 (plain)
1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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
390
391
392
393
394
395
396
397
398
399
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2007-2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
 * Copyright (C) 2004-2007 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
 */

/**
 * @file collectors.h
 */

#ifndef COLLECTORS_H
#define COLLECTORS_H


/*  This module contains a number of COLLECTOR implementations which are used
    to augment the functionality of class PCB_EDIT_FRAME.
*/


#include <class_collector.h>
#include <layers_id_colors_and_visibility.h>              // LAYER_COUNT, layer defs


class BOARD_ITEM;


/**
 * Class COLLECTORS_GUIDE
 * is an abstract base class whose derivatives may be passed to a GENERAL_COLLECTOR,
 * telling GENERAL_COLLECTOR what should be collected (aside from HitTest()ing
 * and KICAD_T scanTypes[], information which are provided to the GENERAL_COLLECTOR
 * through attributes or arguments separately).
 * <p>
 * A justification for this class is to keep the structural storage details of
 * the program's "global preferences" or "configuration options" out of
 * GENERAL_COLLECTOR::Inspect().  This class carries all the necessary details
 * in with it to the Inspect() call. The constructors or other functions of
 * this class's derivatives are then the only place where knowledge of the
 * specific structure of the global preference storage is needed.  Thus,
 * GENERAL_COLLECTOR::Inspect() can be kept as simple as possible, and insulated
 * from changes in global preference storage (and even then it is
 * not simple enough).
 * <p>
 * This class introduces the notion of layer locking.
 */
class COLLECTORS_GUIDE
{

public:
    virtual     ~COLLECTORS_GUIDE() {}

    /**
     * Function IsLayerLocked
     * @return bool - true if the given layer is locked, else false.
     */
    virtual     bool IsLayerLocked( LAYER_ID layer ) const = 0;

    /**
     * Function IsLayerVisible
     * @return bool - true if the given layer is visible, else false.
     */
    virtual     bool IsLayerVisible( LAYER_ID layer ) const = 0;

    /**
     * Function IgnoreLockedLayers
     * @return bool - true if should ignore locked layers, else false.
     */
    virtual     bool IgnoreLockedLayers() const = 0;

    /**
     * Function IgnoredNonVisibleLayers
     * @return bool - true if should ignore non-visible layers, else false.
     */
    virtual     bool IgnoreNonVisibleLayers() const = 0;

    /**
     * Function GetPreferredLayer
     * @return int - the preferred layer for HitTest()ing.
     */
    virtual     LAYER_ID GetPreferredLayer() const = 0;

    /**
     * Function IgnorePreferredLayer
     * provides wildcard behavior regarding the preferred layer.
     * @return bool - true if should ignore preferred layer, else false.
     */
    virtual     bool IgnorePreferredLayer() const = 0;

    /**
     * Function IgnoreLockedItems
     * @return bool - true if should ignore locked items, else false.
     */
    virtual     bool IgnoreLockedItems() const = 0;

    /**
     * Function IncludeSecondary
     * determines if the secondary criteria, or 2nd choice items should be
     * included.
     * @return bool - true if should include, else false.
     */
    virtual     bool IncludeSecondary() const = 0;

    /**
     * Function IgnoreMTextsMarkedNoShow
     * @return bool - true if MTexts marked as "no show" should be ignored.
     */
    virtual     bool IgnoreMTextsMarkedNoShow() const = 0;

    /**
     * Function IgnoreZones
     * @return bool - true if should ignore zones.
    virtual     bool IgnoreZones() const = 0;
     can simply omit from scanTypes[] PCB_ZONE_T */

    /**
     * Function IgnoreMTextsOnBack
     * @return bool - true if should ignore MTexts on back layers
     */
    virtual     bool IgnoreMTextsOnBack() const = 0;

    /**
     * Function IgnoreMTextsOnFront
     * @return bool - true if should ignore MTexts on front layers.
     */
    virtual     bool IgnoreMTextsOnFront() const = 0;

    /**
     * Function IgnoreModulesOnBack
     * @return bool - true if should ignore MODULEs on Back Side.
     */
    virtual     bool IgnoreModulesOnBack() const = 0;

    /**
     * Function IgnoreModulesOnFront
     * @return bool - ture if should ignore MODULEs on Front Side.
     */
    virtual     bool IgnoreModulesOnFront() const = 0;

    /**
     * Function IgnorePadsOnBack
     * @return bool - true if should ignore Pads on Back Side.
     */
    virtual     bool IgnorePadsOnBack() const = 0;

    /**
     * Function IgnorePadsOnFront
     * @return bool - ture if should ignore PADSs on Front Side.
     */
    virtual     bool IgnorePadsOnFront() const = 0;

    /**
     * Function IgnorePads
     * @return bool - true if should ignore PADSs on Front side and Back side.
     */
    virtual     bool IgnorePads() const
    {
        return IgnorePadsOnFront() && IgnorePadsOnBack();
    }

    /**
     * Function IgnoreModulesVals
     * @return bool - true if should ignore modules values.
     */
    virtual     bool IgnoreModulesVals() const = 0;

    /**
     * Function IgnoreModulesRefs
     * @return bool - true if should ignore module references.
     */
    virtual     bool IgnoreModulesRefs() const = 0;

    /**
     * Function UseHitTesting
     * @return bool - true if Inspect() should use BOARD_ITEM::HitTest()
     *             or false if Inspect() should use BOARD_ITEM::BoundsTest().
    virtual     bool UseHitTesting() const = 0;
     */
};



/**
 * Class GENERAL_COLLECTOR
 * is intended for use when the right click button is pressed, or when the
 * plain "arrow" tool is in effect.  This class can be used by window classes
 * such as PCB_EDIT_FRAME.
 *
 * Philosophy: this class knows nothing of the context in which a BOARD is used
 * and that means it knows nothing about which layers are visible or current,
 * but can handle those concerns by the SetPreferredLayer() function and the
 * SetLayerSet() function.
 */
class GENERAL_COLLECTOR : public COLLECTOR
{
protected:
    /**
     * A place to hold collected objects which don't match precisely the search
     * criteria, but would be acceptable if nothing else is found.
     * "2nd" choice, which will be appended to the end of COLLECTOR's prime
     * "list" at the end of the search.
     */
    std::vector<BOARD_ITEM*>    m_List2nd;


    /**
     * Determines which items are to be collected by Inspect()
     */
    const COLLECTORS_GUIDE*     m_Guide;


    /**
     * The number of items that were originally in the primary list before the
     * m_List2nd was concatenated onto the end of it.
     */
    int                         m_PrimaryLength;


public:

    /**
     * A scan list for all editable board items, like PcbGeneralLocateAndDisplay()
     */
    static const KICAD_T AllBoardItems[];


    /**
     * A scan list for all editable board items, except zones
     */
    static const KICAD_T AllButZones[];

    /**
     * A scan list for zones outlines only
     */
    static const KICAD_T Zones[];


    /**
     * A scan list for all primary board items, omitting items which are subordinate to
     * a MODULE, such as D_PAD and TEXTEMODULE.
    static const KICAD_T PrimaryItems[];
     */


    /**
     * A scan list for only MODULEs
     */
    static const KICAD_T Modules[];


    /**
     * A scan list for PADs or MODULEs
     */
    static const KICAD_T PadsOrModules[];

    /**
     * A scan list for PADs, TRACKs, VIAs, or ZONEs
     */
    static const KICAD_T PadsTracksOrZones[];


    /**
     * A scan list for MODULEs and their items (for Modedit)
     */
    static const KICAD_T ModulesAndTheirItems[];


    /**
     * A scan list for primary module items.
     */
    static const KICAD_T ModuleItems[];


    /**
     * A scan list for only TRACKS
     */
    static const KICAD_T Tracks[];


    /**
     * Constructor GENERALCOLLECTOR
     */
    GENERAL_COLLECTOR()
    {
        m_Guide = NULL;
        m_PrimaryLength = 0;
        SetScanTypes( AllBoardItems );
    }

    void Empty2nd()
    {
        m_List2nd.clear();
    }

    void Append2nd( BOARD_ITEM* item )
    {
        m_List2nd.push_back( item );
    }


    /**
     * Function SetGuide
     * records which COLLECTORS_GUIDE to use.
     * @param aGuide Which guide to use in the collection.
     */
    void SetGuide( const COLLECTORS_GUIDE* aGuide ) { m_Guide = aGuide; }


    /**
     * Function operator[int]
     * overloads COLLECTOR::operator[](int) to return a BOARD_ITEM* instead of
     * an EDA_ITEM* type.
     * @param ndx The index into the list.
     * @return BOARD_ITEM* - or something derived from it, or NULL.
     */
    BOARD_ITEM* operator[]( int ndx ) const
    {
        if( (unsigned)ndx < (unsigned)GetCount() )
            return (BOARD_ITEM*) m_List[ ndx ];
        return NULL;
    }


    /**
     * Function GetPrimaryCount
     * @return int - The number if items which met the primary search criteria
     */
    int GetPrimaryCount() { return m_PrimaryLength; }


    /**
     * Function Inspect
     * is the examining function within the INSPECTOR which is passed to the
     * Iterate function.
     *
     * @param testItem An EDA_ITEM to examine.
     * @param testData is not used in this class.
     * @return SEARCH_RESULT - SEARCH_QUIT if the Iterator is to stop the scan,
     *   else SCAN_CONTINUE;
     */
    SEARCH_RESULT Inspect( EDA_ITEM* testItem, const void* testData );


    /**
     * Function Collect
     * scans a BOARD_ITEM using this class's Inspector method, which does the collection.
     * @param aItem A BOARD_ITEM to scan, may be a BOARD or MODULE, or whatever.
     * @param aScanList A list of KICAD_Ts with a terminating EOT, that specs
     *  what is to be collected and the priority order of the resultant
     *  collection in "m_List".
     * @param aRefPos A wxPoint to use in hit-testing.
     * @param aGuide The COLLECTORS_GUIDE to use in collecting items.
     */
    void Collect( BOARD_ITEM* aItem, const KICAD_T aScanList[],
                 const wxPoint& aRefPos, const COLLECTORS_GUIDE& aGuide );
};


/**
 * Class GENERAL_COLLECTORS_GUIDE
 * is a general implementation of a COLLECTORS_GUIDE.  One of its constructors is
 * entitled to grab information from the program's global preferences.
 */
class GENERAL_COLLECTORS_GUIDE : public COLLECTORS_GUIDE
{
private:
    // the storage architecture here is not important, since this is only
    // a carrier object and its functions are what is used, and data only indirectly.

    LAYER_ID m_PreferredLayer;
    bool    m_IgnorePreferredLayer;

    LSET    m_LayerLocked;                  ///< bit-mapped layer locked bits
    bool    m_IgnoreLockedLayers;

    LSET    m_LayerVisible;                 ///< bit-mapped layer visible bits
    bool    m_IgnoreNonVisibleLayers;

    bool    m_IgnoreLockedItems;
    bool    m_IncludeSecondary;

    bool    m_IgnoreMTextsMarkedNoShow;
    bool    m_IgnoreMTextsOnBack;
    bool    m_IgnoreMTextsOnFront;
    bool    m_IgnoreModulesOnBack;
    bool    m_IgnoreModulesOnFront;
    bool    m_IgnorePadsOnFront;
    bool    m_IgnorePadsOnBack;
    bool    m_IgnoreModulesVals;
    bool    m_IgnoreModulesRefs;

public:

    /**
     * Constructor GENERAL_COLLECTORS_GUIDE
     * grabs stuff from global preferences and uses reasonable defaults.
     * Add more constructors as needed.
     * @param aVisibleLayerMask = current visible layers (bit mask)
     * @param aPreferredLayer = the layer to search first
     */
    GENERAL_COLLECTORS_GUIDE( LSET aVisibleLayerMask, LAYER_ID aPreferredLayer )
    {
        m_PreferredLayer            = aPreferredLayer;
        m_IgnorePreferredLayer      = false;
        m_LayerVisible              = aVisibleLayerMask;
        m_IgnoreLockedLayers        = true;
        m_IgnoreNonVisibleLayers    = true;
        m_IgnoreLockedItems         = false;

#if defined(USE_MATCH_LAYER)
        m_IncludeSecondary          = false;
#else
        m_IncludeSecondary          = true;
#endif

        m_IgnoreMTextsMarkedNoShow  = true; // g_ModuleTextNOVColor;
        m_IgnoreMTextsOnBack        = true;
        m_IgnoreMTextsOnFront       = false;
        m_IgnoreModulesOnBack       = true; // !Show_Modules_Cmp;
        m_IgnoreModulesOnFront      = false;

        m_IgnorePadsOnFront         = false;
        m_IgnorePadsOnBack          = false;

        m_IgnoreModulesVals         = false;
        m_IgnoreModulesRefs         = false;
    }


    /**
     * Function IsLayerLocked
     * @return bool - true if the given layer is locked, else false.
     */
    bool IsLayerLocked( LAYER_ID aLayerId ) const
    {
        return m_LayerLocked[aLayerId];
    }

    void SetLayerLocked( LAYER_ID aLayerId, bool isLocked )
    {
        m_LayerLocked.set( aLayerId, isLocked );
    }

    /**
     * Function IsLayerVisible
     * @return bool - true if the given layer is visible, else false.
     */
    bool IsLayerVisible( LAYER_ID aLayerId ) const
    {
        return m_LayerVisible[aLayerId];
    }
    void SetLayerVisible( LAYER_ID aLayerId, bool isVisible )
    {
        m_LayerVisible.set( aLayerId, isVisible );
    }
    void SetLayerVisibleBits( LSET aLayerBits ) { m_LayerVisible = aLayerBits; }

    /**
     * Function IgnoreLockedLayers
     * @return bool - true if should ignore locked layers, else false.
     */
    bool IgnoreLockedLayers() const                 { return m_IgnoreLockedLayers; }
    void SetIgnoreLockedLayers( bool ignore )       { m_IgnoreLockedLayers = ignore; }


    /**
     * Function IgnoredNonVisibleLayers
     * @return bool - true if should ignore non-visible layers, else false.
     */
    bool IgnoreNonVisibleLayers() const             { return m_IgnoreNonVisibleLayers; }
    void SetIgnoreNonVisibleLayers( bool ignore )   { m_IgnoreLockedLayers = ignore; }


    /**
     * Function GetPreferredLayer
     * @return int - the preferred layer for HitTest()ing.
     */
    LAYER_ID GetPreferredLayer() const             { return m_PreferredLayer; }
    void SetPreferredLayer( LAYER_ID aLayer )      { m_PreferredLayer = aLayer; }


    /**
     * Function IgnorePreferredLayer
     * provides wildcard behavior regarding the preferred layer.
     * @return bool - true if should ignore preferred layer, else false.
     */
    bool IgnorePreferredLayer() const               { return  m_IgnorePreferredLayer; }
    void SetIgnorePreferredLayer( bool ignore )     { m_IgnorePreferredLayer = ignore; }


    /**
     * Function IgnoreLockedItems
     * @return bool - true if should ignore locked items, else false.
     */
    bool IgnoreLockedItems() const                  { return m_IgnoreLockedItems; }
    void SetIgnoreLockedItems( bool ignore )        { m_IgnoreLockedItems = ignore; }


    /**
     * Function IncludeSecondary
     * determines if the secondary criteria, or 2nd choice items should be
     * included.
     * @return bool - true if should include, else false.
     */
    bool IncludeSecondary() const  { return m_IncludeSecondary; }
    void SetIncludeSecondary( bool include ) { m_IncludeSecondary = include; }


    /**
     * Function IgnoreMTextsMarkedNoShow
     * @return bool - true if MTexts marked as "no show" should be ignored.
     */
    bool IgnoreMTextsMarkedNoShow() const { return m_IgnoreMTextsMarkedNoShow; }
    void SetIgnoreMTextsMarkedNoShow( bool ignore ) { m_IgnoreMTextsMarkedNoShow = ignore; }

    /**
     * Function IgnoreMTextsOnCu
     * @return bool - true if should ignore MTexts on back layers
     */
    bool IgnoreMTextsOnBack() const { return m_IgnoreMTextsOnBack; }
    void SetIgnoreMTextsOnBack( bool ignore ) { m_IgnoreMTextsOnBack = ignore; }

    /**
     * Function IgnoreMTextsOnFront
     * @return bool - true if should ignore MTexts on front layers
     */
    bool IgnoreMTextsOnFront() const { return m_IgnoreMTextsOnFront; }
    void SetIgnoreMTextsOnFront( bool ignore ) { m_IgnoreMTextsOnFront = ignore; }

    /**
     * Function IgnoreModulesOnBack
     * @return bool - true if should ignore MODULEs on the back side
     */
    bool IgnoreModulesOnBack() const { return m_IgnoreModulesOnBack; }
    void SetIgnoreModulesOnBack( bool ignore ) { m_IgnoreModulesOnBack = ignore; }

    /**
     * Function IgnoreModulesOnFront
     * @return bool - true if should ignore MODULEs on component layer.
     */
    bool IgnoreModulesOnFront() const { return m_IgnoreModulesOnFront; }
    void SetIgnoreModulesOnFront( bool ignore ) { m_IgnoreModulesOnFront = ignore; }

    /**
     * Function IgnorePadsOnBack
     * @return bool - true if should ignore Pads on Back Side.
     */
    bool IgnorePadsOnBack() const { return m_IgnorePadsOnBack; }
    void SetIgnorePadsOnBack(bool ignore) { m_IgnorePadsOnBack = ignore; }

    /**
     * Function IgnorePadsOnFront
     * @return bool - true if should ignore PADSs on Front Side.
     */
    bool IgnorePadsOnFront() const { return m_IgnorePadsOnFront; }
    void SetIgnorePadsOnFront(bool ignore) { m_IgnorePadsOnFront = ignore; }

    /**
     * Function IgnoreModulesVals
     * @return bool - true if should ignore modules values.
     */
    bool IgnoreModulesVals() const { return m_IgnoreModulesVals; }
    void SetIgnoreModulesVals(bool ignore) { m_IgnoreModulesVals = ignore; }

    /**
     * Function IgnoreModulesRefs
     * @return bool - true if should ignore modules references.
     */
    bool IgnoreModulesRefs() const { return m_IgnoreModulesRefs; }
    void SetIgnoreModulesRefs(bool ignore) { m_IgnoreModulesRefs = ignore; }
};


/**
 * Class PCB_TYPE_COLLECTOR
 * merely gathers up all BOARD_ITEMs of a given set of KICAD_T type(s).
 * @see class COLLECTOR
 */
class PCB_TYPE_COLLECTOR : public COLLECTOR
{

public:

    /**
     * Function operator[int]
     * overloads COLLECTOR::operator[](int) to return a BOARD_ITEM* instead of
     * an EDA_ITEM* type.
     * @param ndx The index into the list.
     * @return BOARD_ITEM* - or something derived from it, or NULL.
     */
    BOARD_ITEM* operator[]( int ndx ) const
    {
        if( (unsigned)ndx < (unsigned)GetCount() )
            return (BOARD_ITEM*) m_List[ ndx ];
        return NULL;
    }


    /**
     * Function Inspect
     * is the examining function within the INSPECTOR which is passed to the
     * Iterate function.
     *
     * @param testItem An EDA_ITEM to examine.
     * @param testData is not used in this class.
     * @return SEARCH_RESULT - SEARCH_QUIT if the Iterator is to stop the scan,
     *   else SCAN_CONTINUE;
     */
    SEARCH_RESULT Inspect( EDA_ITEM* testItem, const void* testData );


    /**
     * Function Collect
     * scans a BOARD_ITEM using this class's Inspector method, which does
     * the collection.
     * @param aBoard The BOARD_ITEM to scan.
     * @param aScanList The KICAD_Ts to gather up.
     */
    void Collect( BOARD_ITEM* aBoard, const KICAD_T aScanList[] );
};

#endif // COLLECTORS_H