KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_manage_repositories.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 (C) 2021 Andrew Lutsenko, anlutsenko at gmail dot com
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
24#include <grid_tricks.h>
26#include <widgets/wx_grid.h>
28
29
30#define GRID_CELL_MARGIN 4
31
32
34 wxWindow* parent, std::shared_ptr<PLUGIN_CONTENT_MANAGER> pcm ) :
36 m_pcm( pcm )
37{
42
43 // For aesthetic reasons, we must set the size of m_buttonAdd to match the other bitmaps
44 // manually (for instance m_buttonRemove)
45 Layout(); // Needed at least on MSW to compute the actual buttons sizes, after initializing
46 // their bitmaps
47 m_buttonAdd->SetWidthPadding( 4 );
48 m_buttonAdd->SetMinSize( m_buttonRemove->GetSize() );
49
50 m_buttonAdd->Bind( wxEVT_BUTTON, &DIALOG_MANAGE_REPOSITORIES::OnAdd, this );
51
52 wxMenu* addMenu = m_buttonAdd->GetSplitButtonMenu();
53 wxMenuItem* menuItem = addMenu->Append( wxID_ANY, _( "Add Default Repository" ) );
54
55 addMenu->Bind( wxEVT_COMMAND_MENU_SELECTED, &DIALOG_MANAGE_REPOSITORIES::OnAddDefault, this,
56 menuItem->GetId() );
57
58 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
59 {
60 OnAdd( aEvent );
61 } ) );
62
63 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
64 {
65 const wxString& heading = m_grid->GetColLabelValue( col );
66 int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
67
68 // Set the minimal width to the column label size.
69 m_grid->SetColMinimalWidth( col, headingWidth );
70 }
71
72 // fix sizers now widgets are set.
74}
75
76
78{
79 // Delete the GRID_TRICKS.
80 m_grid->PopEventHandler( true );
81}
82
83
85{
86 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
87 {
88 // Set the width to see the full contents
89 m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true ) );
90 }
91}
92
93
94void DIALOG_MANAGE_REPOSITORIES::OnAdd( wxCommandEvent& event )
95{
96 wxTextEntryDialog entry_dialog( this, _( "Fully qualified repository url:" ),
97 _( "Add Repository" ) );
98
99 if( entry_dialog.ShowModal() == wxID_OK )
100 {
101 wxString url = entry_dialog.GetValue();
102 addRepository( url );
103 }
104}
105
106
107int DIALOG_MANAGE_REPOSITORIES::findRow( int aCol, const wxString& aVal )
108{
109 for( int row = 0; row < m_grid->GetNumberRows(); row++ )
110 {
111 if( m_grid->GetCellValue( row, aCol ) == aVal )
112 return row;
113 }
114
115 return -1;
116}
117
118
120{
121 int matching_row;
122
123 if( ( matching_row = findRow( 1, aUrl ) ) >= 0 )
124 {
125 selectRow( matching_row );
126 return;
127 }
128
129 PCM_REPOSITORY repository;
130 WX_PROGRESS_REPORTER reporter( GetParent(), wxT( "" ), 1, PR_CAN_ABORT );
131
132 if( m_pcm->FetchRepository( aUrl, repository, &reporter ) )
133 {
134 wxString name = repository.name;
135 int increment = 1;
136
137 while( findRow( 0, name ) >= 0 )
138 name = wxString::Format( "%s (%d)", repository.name, increment++ );
139
140 m_grid->Freeze();
141
142 m_grid->AppendRows();
143 int row = m_grid->GetNumberRows() - 1;
144
145 m_grid->SetCellValue( row, 0, name );
146 m_grid->SetCellValue( row, 1, aUrl );
147
149 m_grid->Thaw();
150
151 selectRow( row );
152 }
153}
154
155
160
161
163{
164 m_grid->OnDeleteRows(
165 [&]( int row )
166 {
167 m_grid->DeleteRows( row );
169 } );
170}
171
172
174{
175 m_grid->OnMoveRowUp(
176 [&]( int row )
177 {
178 m_grid->SwapRows( row, row - 1 );
179 } );
180}
181
182
184{
185 m_grid->OnMoveRowDown(
186 [&]( int row )
187 {
188 m_grid->SwapRows( row, row + 1 );
189 } );
190}
191
192
194{
195 selectRow( event.GetRow() );
196}
197
198
200{
201 m_grid->ClearSelection();
202 m_grid->SelectRow( aRow );
203}
204
205
206void DIALOG_MANAGE_REPOSITORIES::SetData( const std::vector<std::pair<wxString, wxString>>& aData )
207{
208 m_grid->Freeze();
209 m_grid->ClearRows();
210 m_grid->AppendRows( aData.size() );
211
212 for( size_t i = 0; i < aData.size(); i++ )
213 {
214 m_grid->SetCellValue( i, 0, aData[i].first );
215 m_grid->SetCellValue( i, 1, aData[i].second );
216 }
217
219
220 m_grid->Thaw();
221}
222
223
224std::vector<std::pair<wxString, wxString>> DIALOG_MANAGE_REPOSITORIES::GetData()
225{
226 std::vector<std::pair<wxString, wxString>> result;
227
228 for( int i = 0; i < m_grid->GetNumberRows(); i++ )
229 {
230 result.push_back(
231 std::make_pair( m_grid->GetCellValue( i, 0 ), m_grid->GetCellValue( i, 1 ) ) );
232 }
233
234 return result;
235}
236
237
238void DIALOG_MANAGE_REPOSITORIES::OnSaveClicked( wxCommandEvent& event )
239{
240 EndModal( wxID_SAVE );
241}
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
DIALOG_MANAGE_REPOSITORIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Manage Repositories"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void OnRemoveButtonClicked(wxCommandEvent &event) override
void OnMoveDownButtonClicked(wxCommandEvent &event) override
std::vector< std::pair< wxString, wxString > > GetData()
void addRepository(const wxString &aUrl)
void OnSaveClicked(wxCommandEvent &event) override
std::shared_ptr< PLUGIN_CONTENT_MANAGER > m_pcm
void OnAdd(wxCommandEvent &event)
DIALOG_MANAGE_REPOSITORIES(wxWindow *parent, std::shared_ptr< PLUGIN_CONTENT_MANAGER > aPcm)
Constructor.
int findRow(int aCol, const wxString &aVal)
void SetData(const std::vector< std::pair< wxString, wxString > > &aData)
void OnMoveUpButtonClicked(wxCommandEvent &event) override
void OnAddDefault(wxCommandEvent &event)
void OnGridCellClicked(wxGridEvent &event) override
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:61
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
#define GRID_CELL_MARGIN
#define _(s)
#define PCM_DEFAULT_REPOSITORY_URL
Package installation entry.
Definition pcm_data.h:141
wxString name
Definition pcm_data.h:142
wxString result
Test unit parsing edge cases and error handling.
#define PR_CAN_ABORT