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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22// Need to use wxRTTI macros in order for OnCompareItems to work properly
23// See: https://docs.wxwidgets.org/3.1/classwx_tree_ctrl.html#ab90a465793c291ca7aa827a576b7d146
25
26
27int UP_DOWN_TREE::OnCompareItems( const wxTreeItemId& item1, const wxTreeItemId& item2 )
28{
29 if( m_sortMap[item1] < m_sortMap[item2] )
30 return -1;
31 else if( m_sortMap[item1] > m_sortMap[item2] )
32 return 1;
33 else
34 return 0;
35}
36
37
38void UP_DOWN_TREE::prepareForSort( const wxTreeItemId& aItem )
39{
40 // Number all siblings sequentially with even numbers. The item moved will be given
41 // an odd number.
42
43 wxTreeItemId first = aItem;
44
45 while( GetPrevSibling( first ) )
46 first = GetPrevSibling( first );
47
48 int idx = 2;
49 m_sortMap.clear();
50
51 m_sortMap[first] = idx;
52
53 while( GetNextSibling( first ) )
54 {
55 first = GetNextSibling( first );
56 idx += 2;
57 m_sortMap[first] = idx;
58 }
59}
60
61
62void UP_DOWN_TREE::MoveItemUp( const wxTreeItemId& aItem )
63{
64 prepareForSort( aItem );
65 m_sortMap[aItem] = m_sortMap[aItem] - 3;
66 SortChildren( GetItemParent( aItem ) );
67}
68
69
70void UP_DOWN_TREE::MoveItemDown( const wxTreeItemId& aItem )
71{
72 prepareForSort( aItem );
73 m_sortMap[aItem] = m_sortMap[aItem] + 3;
74 SortChildren( GetItemParent( aItem ) );
75}
76
77
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)