KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_constraints.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#include <algorithm>
23#include <ranges>
24
25#include <wx/listctrl.h>
26
27#include <board.h>
28#include <pcb_base_edit_frame.h>
29#include <tool/tool_manager.h>
34
35
37 PANEL_CONSTRAINTS_BASE( aFrame ),
38 m_frame( aFrame )
39{
41
42 // Item-cell clicks highlight a single member; the base only wires item-activated/buttons.
43 m_list->Bind( wxEVT_LEFT_DOWN, &PANEL_CONSTRAINTS::onLeftDown, this );
44
46}
47
48
50{
51 BOARD* board = m_frame->GetBoard();
52
54}
55
56
58{
59 m_rows.clear();
60
61 PopulateConstraintList( m_list, m_frame->GetBoard(), m_frame, aDiag,
62 [&]( long aRow, PCB_CONSTRAINT* aConstraint )
63 {
64 m_rows.push_back( aConstraint->m_Uuid );
65 } );
66}
67
68
69void PANEL_CONSTRAINTS::SelectConstraint( const KIID& aConstraint )
70{
71 auto it = std::ranges::find( m_rows, aConstraint );
72
73 if( it == m_rows.end() )
74 return;
75
76 long row = static_cast<long>( std::distance( m_rows.begin(), it ) );
77 m_list->SetItemState( row, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED,
78 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED );
79 m_list->EnsureVisible( row );
80}
81
82
84{
85 return m_frame->GetToolManager()->GetTool<CONSTRAINT_EDIT_TOOL>();
86}
87
88
89const KIID& PANEL_CONSTRAINTS::rowConstraint( long aRow ) const
90{
91 if( aRow < 0 || aRow >= static_cast<long>( m_rows.size() ) )
92 return niluuid;
93
94 return m_rows[aRow];
95}
96
97
98long PANEL_CONSTRAINTS::columnAt( long aRow, const wxPoint& aPos ) const
99{
100 for( int col = 0; col < m_list->GetColumnCount(); ++col )
101 {
102 wxRect rect;
103
104 if( m_list->GetSubItemRect( aRow, col, rect ) && aPos.x >= rect.GetLeft()
105 && aPos.x <= rect.GetRight() )
106 {
107 return col;
108 }
109 }
110
111 return -1;
112}
113
114
115void PANEL_CONSTRAINTS::onLeftDown( wxMouseEvent& aEvent )
116{
117 int flags = 0;
118 long row = m_list->HitTest( aEvent.GetPosition(), flags );
119
120 if( row >= 0 && row < static_cast<long>( m_rows.size() ) && constraintTool() )
121 {
122 // Clicking an item cell highlights just that member; any other cell highlights all.
123 long column = columnAt( row, aEvent.GetPosition() );
124 int memberIndex = ( column == CONSTRAINT_COL_ITEM_1 || column == CONSTRAINT_COL_ITEM_2 )
125 ? static_cast<int>( column )
126 : -1;
127 constraintTool()->HighlightConstraintMembers( m_rows[row], memberIndex );
128 }
129
130 aEvent.Skip();
131}
132
133
134void PANEL_CONSTRAINTS::onRowActivated( wxListEvent& aEvent )
135{
136 // Editing the value is a board mutation, so hand it to the tool; it commits and refreshes us.
138 tool->EditConstraintById( rowConstraint( aEvent.GetIndex() ) );
139}
140
141
142void PANEL_CONSTRAINTS::onDelete( wxCommandEvent& aEvent )
143{
144 long row = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
145
147 tool->RemoveConstraintById( rowConstraint( row ) );
148
149 // Select the last row so pressing Delete again removes the next constraint.
150 if( long count = m_list->GetItemCount(); count > 0 )
151 {
152 m_list->SetItemState( count - 1, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED,
153 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED );
154 m_list->EnsureVisible( count - 1 );
155 }
156}
157
158
159void PANEL_CONSTRAINTS::onRefresh( wxCommandEvent& aEvent )
160{
161 RefreshList();
162}
BOARD_CONSTRAINT_DIAGNOSTICS DiagnoseBoardConstraints(BOARD *aBoard)
Diagnose every constraint cluster on the board (validate only – geometry is not changed) and return t...
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
Interactive authoring of geometric constraints (issue #2329).
void HighlightConstraintMembers(const KIID &aId, int aMemberIndex)
Definition kiid.h:44
PANEL_CONSTRAINTS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
PCB_BASE_EDIT_FRAME * m_frame
void RefreshList()
Rebuild the rows from the board's current constraints (solving the board diagnosis here).
long columnAt(long aRow, const wxPoint &aPos) const
The list column under aPos within row aRow, or -1.
std::vector< KIID > m_rows
void onRefresh(wxCommandEvent &aEvent) override
void onDelete(wxCommandEvent &aEvent) override
void onLeftDown(wxMouseEvent &aEvent)
const KIID & rowConstraint(long aRow) const
The KIID for a row, or niluuid if out of range.
void onRowActivated(wxListEvent &aEvent) override
void SelectConstraint(const KIID &aConstraint)
Select and reveal the row for aConstraint (e.g. when its badge is clicked on canvas).
PANEL_CONSTRAINTS(PCB_BASE_EDIT_FRAME *aFrame)
CONSTRAINT_EDIT_TOOL * constraintTool() const
The owning constraint tool, which performs all board mutation and selection; the panel only displays ...
Common, abstract interface for edit frames.
A geometric constraint between board items (issue #2329).
void PopulateConstraintList(wxListCtrl *aList, BOARD *aBoard, UNITS_PROVIDER *aUnits, const BOARD_CONSTRAINT_DIAGNOSTICS &aDiag, const std::function< void(long, PCB_CONSTRAINT *)> &aOnRow)
Fill aList with one row per constraint on aBoard (board-owned then footprint-owned),...
void AddConstraintListColumns(wxListCtrl *aList)
Add the four constraint columns (Item 1 / Item 2 / Constraint / State) to a report-mode list.
@ CONSTRAINT_COL_ITEM_1
@ CONSTRAINT_COL_ITEM_2
KIID niluuid(0)
Board-wide diagnostics for the constraint overlay and info bar.