KiCad PCB EDA Suite
Loading...
Searching...
No Matches
up_down_tree.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26// Need to use wxRTTI macros in order for OnCompareItems to work properly
27// See: https://docs.wxwidgets.org/3.1/classwx_tree_ctrl.html#ab90a465793c291ca7aa827a576b7d146
29
30
31int UP_DOWN_TREE::OnCompareItems( const wxTreeItemId& item1, const wxTreeItemId& item2 )
32{
33 if( m_sortMap[item1] < m_sortMap[item2] )
34 return -1;
35 else if( m_sortMap[item1] > m_sortMap[item2] )
36 return 1;
37 else
38 return 0;
39}
40
41
42void UP_DOWN_TREE::prepareForSort( const wxTreeItemId& aItem )
43{
44 // Number all siblings sequentially with even numbers. The item moved will be given
45 // an odd number.
46
47 wxTreeItemId first = aItem;
48
49 while( GetPrevSibling( first ) )
50 first = GetPrevSibling( first );
51
52 int idx = 2;
53 m_sortMap.clear();
54
55 m_sortMap[first] = idx;
56
57 while( GetNextSibling( first ) )
58 {
59 first = GetNextSibling( first );
60 idx += 2;
61 m_sortMap[first] = idx;
62 }
63}
64
65
66void UP_DOWN_TREE::MoveItemUp( const wxTreeItemId& aItem )
67{
68 prepareForSort( aItem );
69 m_sortMap[aItem] = m_sortMap[aItem] - 3;
70 SortChildren( GetItemParent( aItem ) );
71}
72
73
74void UP_DOWN_TREE::MoveItemDown( const wxTreeItemId& aItem )
75{
76 prepareForSort( aItem );
77 m_sortMap[aItem] = m_sortMap[aItem] + 3;
78 SortChildren( GetItemParent( aItem ) );
79}
80
81
void MoveItemUp(const wxTreeItemId &aItem)
int OnCompareItems(const wxTreeItemId &item1, const wxTreeItemId &item2) override
void MoveItemDown(const wxTreeItemId &aItem)
void prepareForSort(const wxTreeItemId &aItem)
std::map< wxTreeItemId, int > m_sortMap
wxIMPLEMENT_ABSTRACT_CLASS(UP_DOWN_TREE, wxTreeCtrl)