File: footprint_select_widget.cpp

package info (click to toggle)
kicad 5.0.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 234,592 kB
  • sloc: cpp: 505,330; ansic: 57,038; python: 4,886; sh: 879; awk: 294; makefile: 253; xml: 103; perl: 5
file content (269 lines) | stat: -rw-r--r-- 7,262 bytes parent folder | download
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
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <dialog_shim.h>
#include <kiway.h>
#include <kiway_player.h>
#include <project.h>
#include <widgets/footprint_choice.h>
#include <widgets/footprint_select_widget.h>

#include <wx/wupdlock.h>
#include <widgets/progress_reporter.h>

/**
 * Fixed positions for standard items in the list
 */
enum
{
    POS_DEFAULT,
    POS_OTHER,
    POS_SEPARATOR
};


wxDEFINE_EVENT( EVT_FOOTPRINT_SELECTED, wxCommandEvent );


FOOTPRINT_SELECT_WIDGET::FOOTPRINT_SELECT_WIDGET( wxWindow* aParent,
        FOOTPRINT_LIST* aFpList, bool aUpdate,
        int aMaxItems )
        : wxPanel( aParent ),
          m_kiway( nullptr ),
          m_update( aUpdate ),
          m_finished_loading( false ),
          m_max_items( aMaxItems ),
          m_last_item( 0 ),
          m_fp_list( aFpList )
{
    m_zero_filter = true;
    m_sizer = new wxBoxSizer( wxVERTICAL );
    m_fp_sel_ctrl = new FOOTPRINT_CHOICE( this, wxID_ANY );
    m_sizer->Add( m_fp_sel_ctrl, 1, wxEXPAND | wxALL, 5 );

    SetSizer( m_sizer );
    Layout();
    m_sizer->Fit( this );

    m_fp_sel_ctrl->Bind( wxEVT_COMBOBOX, &FOOTPRINT_SELECT_WIDGET::OnComboBox, this );
    m_fp_sel_ctrl->Bind( EVT_INTERACTIVE_CHOICE, &FOOTPRINT_SELECT_WIDGET::OnComboInteractive, this );
}


void FOOTPRINT_SELECT_WIDGET::Load( KIWAY& aKiway, PROJECT& aProject )
{
    m_kiway = &aKiway;

    try
    {
        auto fp_lib_table = aProject.PcbFootprintLibs( aKiway );
        m_fp_list = FOOTPRINT_LIST::GetInstance( aKiway );

        WX_PROGRESS_REPORTER progressReporter( this, _( "Loading Footprint Libraries" ), 2 );
        m_fp_list->ReadFootprintFiles( fp_lib_table, nullptr, &progressReporter );
        FootprintsLoaded();
    }
    catch( ... )
    {
        // no footprint libraries available
    }
}


void FOOTPRINT_SELECT_WIDGET::FootprintsLoaded()
{
    m_fp_filter.SetList( *m_fp_list );

    m_finished_loading = true;

    if( m_update )
        UpdateList();
}


void FOOTPRINT_SELECT_WIDGET::OnComboBox( wxCommandEvent& aEvent )
{
    wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );
    int            sel = m_fp_sel_ctrl->GetSelection();

    switch( sel )
    {
    case wxNOT_FOUND: return;

    case POS_SEPARATOR:
        // User somehow managed to select the separator. This should not be
        // possible, but just in case... deselect it
        m_fp_sel_ctrl->SetSelection( m_last_item );
        break;

    case POS_OTHER:
        // When POS_OTHER is selected, a dialog should be shown. However, we don't want to
        // do this ALL the time, as some times (e.g. when moving around with the arrow keys)
        // it could be very annoying. Therefore showing the picker is done from the custom
        // "interactive select" event on FOOTPRINT_CHOICE, which only fires for more direct
        // choice actions.
        break;

    default:
    {
        wxStringClientData* clientdata =
                static_cast<wxStringClientData*>( m_fp_sel_ctrl->GetClientObject( sel ) );
        wxASSERT( clientdata );

        evt.SetString( clientdata->GetData() );
        wxPostEvent( this, evt );
    }
    }
}


void FOOTPRINT_SELECT_WIDGET::OnComboInteractive( wxCommandEvent& aEvent )
{
    if( aEvent.GetInt() == POS_OTHER && !m_fp_sel_ctrl->IsPopupShown() )
    {
        DoOther();
    }
}


void FOOTPRINT_SELECT_WIDGET::DoOther()
{
    wxCommandEvent evt( EVT_FOOTPRINT_SELECTED );

    wxString fpname = ShowPicker();
    m_other_footprint = fpname;
    UpdateList();
    m_fp_sel_ctrl->SetSelection( POS_OTHER );
    m_last_item = POS_OTHER;

    evt.SetString( m_other_footprint );
    wxPostEvent( this, evt );
}


wxString FOOTPRINT_SELECT_WIDGET::ShowPicker()
{
    wxString     fpname;
    wxWindow*    parent = ::wxGetTopLevelParent( this );
    DIALOG_SHIM* dsparent = dynamic_cast<DIALOG_SHIM*>( parent );

    // Only quasimodal dialogs can launch modal kiface dialogs. Otherwise the
    // event loop goes all silly.
    wxASSERT( !dsparent || dsparent->IsQuasiModal() );

    auto frame = m_kiway->Player( FRAME_PCB_MODULE_VIEWER_MODAL, true );

    if( !frame->ShowModal( &fpname, parent ) )
    {
        fpname = wxEmptyString;
    }

    frame->Destroy();

    return fpname;
}


void FOOTPRINT_SELECT_WIDGET::ClearFilters()
{
    m_fp_filter.ClearFilters();
    m_default_footprint.Clear();
    m_other_footprint.Clear();
    m_zero_filter = false;
}


void FOOTPRINT_SELECT_WIDGET::FilterByPinCount( int aPinCount )
{
    m_fp_filter.FilterByPinCount( aPinCount );
}


void FOOTPRINT_SELECT_WIDGET::FilterByFootprintFilters(
        wxArrayString const& aFilters, bool aZeroFilters )
{
    if( aZeroFilters && aFilters.size() == 0 )
        m_zero_filter = true;
    else
        m_zero_filter = false;

    m_fp_filter.FilterByFootprintFilters( aFilters );
}


void FOOTPRINT_SELECT_WIDGET::SetDefaultFootprint( wxString const& aFp )
{
    m_default_footprint = aFp;
}


bool FOOTPRINT_SELECT_WIDGET::UpdateList()
{
    int n_items = 0;

    if( !m_fp_list || !m_finished_loading )
        return false;

    wxWindowUpdateLocker lock( m_fp_sel_ctrl );
    m_fp_sel_ctrl->Clear();

    // Be careful adding items! "Default" must occupy POS_DEFAULT,
    // "Other" must occupy POS_OTHER, and the separator must occupy POS_SEPARATOR.

    m_fp_sel_ctrl->Append( m_default_footprint.IsEmpty() ?
                                   _( "No default footprint" ) :
                                   "[" + _( "Default" ) + "] " + m_default_footprint,
            new wxStringClientData( m_default_footprint ) );

    m_fp_sel_ctrl->Append( m_other_footprint.IsEmpty() ?
                                   _( "Other..." ) :
                                   "[" + _( "Other..." ) + "] " + m_other_footprint,
            new wxStringClientData( m_other_footprint ) );

    m_fp_sel_ctrl->Append( "", new wxStringClientData( "" ) );

    if( !m_zero_filter )
    {
        for( auto& fpinfo : m_fp_filter )
        {
            wxString display_name( fpinfo.GetNickname() + ":" + fpinfo.GetFootprintName() );

            m_fp_sel_ctrl->Append( display_name, new wxStringClientData( display_name ) );
            ++n_items;

            if( n_items >= m_max_items )
                break;
        }
    }

    SelectDefault();
    return true;
}


void FOOTPRINT_SELECT_WIDGET::SelectDefault()
{
    m_fp_sel_ctrl->SetSelection( POS_DEFAULT );
}


bool FOOTPRINT_SELECT_WIDGET::Enable( bool aEnable )
{
    return m_fp_sel_ctrl->Enable( aEnable );
}