KiCad PCB EDA Suite
Loading...
Searching...
No Matches
field_warning_icon.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
20
22
23#include <algorithm>
24#include <utility>
25#include <vector>
26
27#include <bitmaps.h>
28#include <i18n_utility.h>
29#include <wx/choice.h>
30#include <wx/combo.h>
31#include <wx/combobox.h>
32#include <wx/settings.h>
33#include <wx/spinctrl.h>
34
35
36FIELD_WARNING_ICON::FIELD_WARNING_ICON( wxWindow* aField, BITMAPS aIcon, int aSlot ) :
37 wxGenericStaticBitmap( aField->GetParent(), wxID_ANY, KiBitmapBundle( aIcon ) ),
38 m_field( aField ),
39 m_slot( aSlot )
40{
41 Hide();
42
43 // Without a background of its own the icon shows the page behind the field, or on some ports the text under it
44 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) );
45
46 m_field->Bind( wxEVT_SIZE, &FIELD_WARNING_ICON::onFieldChanged, this );
47 m_field->Bind( wxEVT_MOVE, &FIELD_WARNING_ICON::onFieldChanged, this );
48 m_field->Bind( wxEVT_DESTROY, &FIELD_WARNING_ICON::onFieldDestroyed, this );
49}
50
51
53{
54 // The parent destroys the field first, since it was created first
55 if( m_field )
56 {
57 m_field->Unbind( wxEVT_SIZE, &FIELD_WARNING_ICON::onFieldChanged, this );
58 m_field->Unbind( wxEVT_MOVE, &FIELD_WARNING_ICON::onFieldChanged, this );
59 m_field->Unbind( wxEVT_DESTROY, &FIELD_WARNING_ICON::onFieldDestroyed, this );
60 }
61}
62
63
64void FIELD_WARNING_ICON::SetWarnings( const wxArrayString& aMessages )
65{
66 // Identical messages collapse into one line with a count, in the order they first appear
67 std::vector<std::pair<wxString, size_t>> lines;
68
69 for( const wxString& message : aMessages )
70 {
71 auto it = std::find_if( lines.begin(), lines.end(),
72 [&]( const std::pair<wxString, size_t>& aLine )
73 {
74 return aLine.first == message;
75 } );
76
77 if( it != lines.end() )
78 it->second++;
79 else
80 lines.emplace_back( message, 1 );
81 }
82
83 wxString text;
84
85 for( size_t ii = 0; ii < lines.size() && ii < MAX_LINES; ++ii )
86 {
87 if( ii > 0 )
88 text += wxS( "\n" );
89
90 text += lines[ii].first;
91
92 if( lines[ii].second > 1 )
93 text += wxString::Format( _( " (%zu times)" ), lines[ii].second );
94 }
95
96 if( lines.size() > MAX_LINES )
97 text += wxString::Format( _( "\n... and %zu more" ), lines.size() - MAX_LINES );
98
99 SetToolTip( text );
100 Show( !text.IsEmpty() );
101
102 Reposition();
103}
104
105
106void FIELD_WARNING_ICON::SetWarning( const wxString& aMessage )
107{
108 wxArrayString messages;
109
110 if( !aMessage.IsEmpty() )
111 messages.Add( aMessage );
112
113 SetWarnings( messages );
114}
115
116
118{
119 // A tooltip of its own, however empty, keeps the field's help from showing through the icon
120 SetToolTip( wxS( " " ) );
121 Show( true );
122 Reposition();
123}
124
125
127{
128 if( !m_field )
129 return;
130
131 const wxSize fieldSize = m_field->GetSize();
132 int buttonWidth = 0;
133
134 // A dropdown button, or a spin control's arrows, is about as wide as the field is tall, except on Windows
135 // where it has the width of a scrollbar
136 if( dynamic_cast<wxComboBox*>( m_field ) || dynamic_cast<wxChoice*>( m_field )
137 || dynamic_cast<wxComboCtrl*>( m_field ) || dynamic_cast<wxSpinCtrl*>( m_field ) )
138 {
139#ifdef __WXMSW__
140 buttonWidth = wxSystemSettings::GetMetric( wxSYS_VSCROLL_X, m_field );
141#else
142 buttonWidth = fieldSize.y;
143#endif
144 }
145
146 const wxSize iconSize = GetSize();
147 const int gap = m_field->FromDIP( 3 );
148
149 const int slotOffset = m_slot * ( iconSize.x + gap );
150
151 SetPosition(
152 m_field->GetPosition()
153 + wxPoint( fieldSize.x - buttonWidth - iconSize.x - gap - slotOffset, ( fieldSize.y - iconSize.y ) / 2 ) );
154 Raise();
155}
156
157
159{
160 aEvent.Skip();
161
162 // The field's new geometry is only final once the sizer is done with it
163 CallAfter(
164 [this]()
165 {
166 Reposition();
167 } );
168}
169
170
171void FIELD_WARNING_ICON::onFieldDestroyed( wxWindowDestroyEvent& aEvent )
172{
173 // Only the field's own destruction counts, not a child's that bubbles up
174 if( aEvent.GetEventObject() == m_field )
175 m_field = nullptr;
176
177 aEvent.Skip();
178}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
BITMAPS
A list of all bitmap identifiers.
FIELD_WARNING_ICON(wxWindow *aField, BITMAPS aIcon=BITMAPS::small_warning, int aSlot=0)
static constexpr size_t MAX_LINES
The most warnings listed in the tooltip, with a count of the rest.
wxWindow * m_field
Null once the field has been destroyed.
void ShowAsBadge()
Shows the icon with no tooltip of its own, for one that opens something on click or hover.
void onFieldChanged(wxEvent &aEvent)
void Reposition()
Moves the icon back over the field after the field has moved or changed size.
void SetWarnings(const wxArrayString &aMessages)
Lists aMessages in the tooltip, once each with a count when repeated, and shows the icon,...
void SetWarning(const wxString &aMessage)
Shows aMessage as the tooltip, or hides the icon when it is empty.
void onFieldDestroyed(wxWindowDestroyEvent &aEvent)
#define _(s)
Some functions to handle hotkeys in KiCad.
std::string message