File: selection.cpp

package info (click to toggle)
kicad 9.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 770,320 kB
  • sloc: cpp: 961,692; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (290 lines) | stat: -rw-r--r-- 8,131 bytes parent folder | download | duplicates (3)
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
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013-2017 CERN
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * 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
 */

#include <algorithm>
#include <eda_item.h>
#include <tool/selection.h>


bool SELECTION::operator==( const SELECTION& aOther ) const
{
    return ( m_items == aOther.m_items
             && m_itemsOrders == aOther.m_itemsOrders
             && m_isHover == aOther.m_isHover
             && m_lastAddedItem == aOther.m_lastAddedItem
             && m_orderCounter == aOther.m_orderCounter );
}


void SELECTION::Add( EDA_ITEM* aItem )
{
    // We're not sorting here; this is just a time-optimized way to do an
    // inclusion check.  std::lower_bound will return the first i >= aItem
    // and the second i > aItem check rules out i == aItem.
    ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );

    if( i == m_items.end() || *i > aItem )
    {
        m_itemsOrders.insert( m_itemsOrders.begin() + std::distance( m_items.begin(), i ),
                              m_orderCounter );
        m_items.insert( i, aItem );
        m_orderCounter++;
        m_lastAddedItem = aItem;
    }
}


void SELECTION::Remove( EDA_ITEM* aItem )
{
    ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );

    if( !( i == m_items.end() || *i > aItem ) )
    {
        m_itemsOrders.erase( m_itemsOrders.begin() + std::distance( m_items.begin(), i ) );
        m_items.erase( i );

        if( aItem == m_lastAddedItem )
            m_lastAddedItem = nullptr;
    }
}


KIGFX::VIEW_ITEM* SELECTION::GetItem( unsigned int aIdx ) const
{
    if( aIdx < m_items.size() )
        return m_items[aIdx];

    return nullptr;
}


bool SELECTION::Contains( EDA_ITEM* aItem ) const
{
    CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );

    return !( i == m_items.end() || *i > aItem );
}


VECTOR2I SELECTION::GetCenter() const
{
    static const std::vector<KICAD_T> textTypes = { SCH_TEXT_T, SCH_LABEL_LOCATE_ANY_T };
    bool                              hasOnlyText = true;

    // If the selection contains only texts calculate the center as the mean of all positions
    // instead of using the center of the total bounding box. Otherwise rotating the selection will
    // also translate it.
    for( EDA_ITEM* item : m_items )
    {
        if( !item->IsType( textTypes ) )
        {
            hasOnlyText = false;
            break;
        }
    }

    BOX2I bbox;

    if( hasOnlyText )
    {
        VECTOR2I center( 0, 0 );

        for( EDA_ITEM* item : m_items )
            center += item->GetPosition();

        center = center / static_cast<int>( m_items.size() );
        return static_cast<VECTOR2I>( center );
    }

    for( EDA_ITEM* item : m_items )
    {
        if( !item->IsType( { SCH_TEXT_T, SCH_LABEL_LOCATE_ANY_T } ) )
            bbox.Merge( item->GetBoundingBox() );
    }

    return static_cast<VECTOR2I>( bbox.GetCenter() );
}


BOX2I SELECTION::GetBoundingBox() const
{
    BOX2I bbox;

    for( EDA_ITEM* item : m_items )
        bbox.Merge( item->GetBoundingBox() );

    return bbox;
}


bool SELECTION::HasType( KICAD_T aType ) const
{
    for( const EDA_ITEM* item : m_items )
    {
        if( item->IsType( { aType } ) )
            return true;
    }

    return false;
}


size_t SELECTION::CountType( KICAD_T aType ) const
{
    size_t count = 0;

    for( const EDA_ITEM* item : m_items )
    {
        if( item->IsType( { aType } ) )
            count++;
    }

    return count;
}


VECTOR2I SELECTION::GetReferencePoint() const
{
    if( m_referencePoint )
        return *m_referencePoint;
    else
        return GetBoundingBox().Centre();
}


void SELECTION::SetReferencePoint( const VECTOR2I& aP )
{
    m_referencePoint = aP;
}


void SELECTION::ClearReferencePoint()
{
    m_referencePoint = std::nullopt;
}


const std::vector<KIGFX::VIEW_ITEM*> SELECTION::updateDrawList() const
{
    std::vector<VIEW_ITEM*> items;

    for( EDA_ITEM* item : m_items )
        items.push_back( item );

    return items;
}


bool SELECTION::AreAllItemsIdentical() const
{
    return std::all_of( m_items.begin() + 1, m_items.end(),
            [&]( const EDA_ITEM* r )
            {
                return r->Type() == m_items.front()->Type();
            } );
}


bool SELECTION::OnlyContains( std::vector<KICAD_T> aList ) const
{
    return std::all_of( m_items.begin(), m_items.end(),
            [&]( const EDA_ITEM* r )
            {
                return r->IsType( aList );
            } );
}


std::vector<EDA_ITEM*> SELECTION::GetItemsSortedByTypeAndXY( bool leftBeforeRight,
                                                             bool topBeforeBottom ) const
{
    std::vector<EDA_ITEM*> sorted_items = std::vector<EDA_ITEM*>( m_items.begin(), m_items.end() );

    std::sort( sorted_items.begin(), sorted_items.end(),
               [&]( EDA_ITEM* a, EDA_ITEM* b )
               {
                   if( a->Type() == b->Type() )
                   {
                       const VECTOR2I aPos = a->GetSortPosition();
                       const VECTOR2I bPos = b->GetSortPosition();

                       if( aPos.x == bPos.x )
                       {
                           // Ensure deterministic sort
                           if( aPos.y == bPos.y )
                               return a->m_Uuid < b->m_Uuid;

                           if( topBeforeBottom )
                               return aPos.y < bPos.y;
                           else
                               return aPos.y > bPos.y;
                       }
                       else if( leftBeforeRight )
                       {
                           return aPos.x < bPos.x;
                       }
                       else
                       {
                           return aPos.x > bPos.x;
                       }
                   }
                   else
                   {
                       return a->Type() < b->Type();
                   }
               } );
    return sorted_items;
}


std::vector<EDA_ITEM*> SELECTION::GetItemsSortedBySelectionOrder() const
{
    using pairedIterators = std::pair<decltype( m_items.begin() ),
                                      decltype( m_itemsOrders.begin() )>;

    // Create a vector of all {selection item, selection order} iterator pairs
    std::vector<pairedIterators> pairs;
    auto                         item = m_items.begin();
    auto                         order = m_itemsOrders.begin();

    for( ; item != m_items.end(); ++item, ++order )
        pairs.emplace_back( make_pair( item, order ) );

    // Sort the pairs by the selection order
    std::sort( pairs.begin(), pairs.end(),
               []( pairedIterators const& a, pairedIterators const& b )
               {
                   return *a.second < *b.second;
               } );

    // Make a vector of just the sortedItems
    std::vector<EDA_ITEM*> sortedItems;

    for( pairedIterators sortedItem : pairs )
        sortedItems.emplace_back( *sortedItem.first );

    return sortedItems;
}