KiCad PCB EDA Suite
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 (C) 1992-2021 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 
22 #include "bitmaps/bitmap_types.h"
23 #include "bitmaps/bitmaps_list.h"
24 #include "grid_tricks.h"
25 #include "widgets/wx_grid.h"
26 
27 
28 #define GRID_CELL_MARGIN 4
29 
30 
32  wxWindow* parent, std::shared_ptr<PLUGIN_CONTENT_MANAGER> pcm ) :
34  m_pcm( pcm )
35 {
36  m_buttonAdd->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
38  m_buttonMoveUp->SetBitmap( KiBitmap( BITMAPS::small_up ) );
40 
41  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
42 
43  for( int col = 0; col < m_grid->GetNumberCols(); col++ )
44  {
45  const wxString& heading = m_grid->GetColLabelValue( col );
46  int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
47 
48  // Set the minimal width to the column label size.
49  m_grid->SetColMinimalWidth( col, headingWidth );
50  }
51 
52  // fix sizers now widgets are set.
54 }
55 
56 
58 {
59  // Delete the GRID_TRICKS.
60  m_grid->PopEventHandler( true );
61 }
62 
63 
65 {
66  for( int col = 0; col < m_grid->GetNumberCols(); col++ )
67  {
68  // Set the width to see the full contents
69  m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true, false ) );
70  }
71 }
72 
73 
75 {
76  wxTextEntryDialog entry_dialog( this,
77  _( "Please enter fully qualified repository url" ),
78  _( "Add repository" ) );
79 
80  if( entry_dialog.ShowModal() == wxID_OK )
81  {
82  PCM_REPOSITORY repository;
83  wxString url = entry_dialog.GetValue();
84 
85  const auto find_row = [&]( const int col, const wxString& val )
86  {
87  for( int row = 0; row < m_grid->GetNumberRows(); row++ )
88  {
89  if( m_grid->GetCellValue( row, col ) == val )
90  return row;
91  }
92 
93  return -1;
94  };
95 
96  int matching_row;
97 
98  if( ( matching_row = find_row( 1, url ) ) >= 0 )
99  {
100  selectRow( matching_row );
101  }
102  else
103  {
104  WX_PROGRESS_REPORTER reporter( GetParent(), wxEmptyString, 1 );
105 
106  if( m_pcm->FetchRepository( url, repository, &reporter ) )
107  {
108  wxString name = repository.name;
109  int increment = 1;
110 
111  while( find_row( 0, name ) >= 0 )
112  name = wxString::Format( wxT( "%s (%d)" ), repository.name, increment++ );
113 
114  m_grid->Freeze();
115 
116  m_grid->AppendRows();
117  int row = m_grid->GetNumberRows() - 1;
118 
119  m_grid->SetCellValue( row, 0, name );
120  m_grid->SetCellValue( row, 1, url );
121 
122  setColumnWidths();
123  m_grid->Thaw();
124 
125  selectRow( row );
126  }
127  }
128  }
129 }
130 
131 
133 {
134  auto selectedRows = m_grid->GetSelectedRows();
135 
136  // If nothing is selected or multiple rows are selected don't do anything.
137  if( selectedRows.size() != 1 )
138  {
139  wxBell();
140  return;
141  }
142 
143  int selectedRow = selectedRows[0];
144  m_grid->DeleteRows( selectedRow );
145  setColumnWidths();
146 
147  if( m_grid->GetNumberRows() > 0 )
148  m_grid->SelectRow( selectedRow == m_grid->GetNumberRows() ? selectedRow - 1 : selectedRow );
149 }
150 
151 
153 {
154  auto selectedRows = m_grid->GetSelectedRows();
155 
156  // If nothing is selected or multiple rows are selected don't do anything.
157  if( selectedRows.size() != 1 )
158  return;
159 
160  int selectedRow = selectedRows[0];
161 
162  // If first row is selected, then it can't go any further up.
163  if( selectedRow == 0 )
164  {
165  wxBell();
166  return;
167  }
168 
169  swapRows( selectedRow, selectedRow - 1 );
170 
171  selectRow( selectedRow - 1 );
172 }
173 
174 
176 {
177  auto selectedRows = m_grid->GetSelectedRows();
178 
179  // If nothing is selected or multiple rows are selected don't do anything.
180  if( selectedRows.size() != 1 )
181  return;
182 
183  int selectedRow = selectedRows[0];
184 
185  // If last row is selected, then it can't go any further down.
186  if( selectedRow + 1 == m_grid->GetNumberRows() )
187  {
188  wxBell();
189  return;
190  }
191 
192  swapRows( selectedRow, selectedRow + 1 );
193 
194  selectRow( selectedRow + 1 );
195 }
196 
197 
198 void DIALOG_MANAGE_REPOSITORIES::swapRows( int aRowA, int aRowB )
199 {
200  m_grid->Freeze();
201 
202  wxString tempStr;
203 
204  for( int column = 0; column < m_grid->GetNumberCols(); column++ )
205  {
206  tempStr = m_grid->GetCellValue( aRowA, column );
207  m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
208  m_grid->SetCellValue( aRowB, column, tempStr );
209  }
210 
211  m_grid->Thaw();
212 }
213 
214 
216 {
217  selectRow( event.GetRow() );
218 }
219 
220 
222 {
223  m_grid->ClearSelection();
224  m_grid->SelectRow( aRow );
225 }
226 
227 
228 void DIALOG_MANAGE_REPOSITORIES::SetData( const std::vector<std::pair<wxString, wxString>>& aData )
229 {
230  m_grid->Freeze();
231 
232  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
233  m_grid->AppendRows( aData.size() );
234 
235  for( size_t i = 0; i < aData.size(); i++ )
236  {
237  m_grid->SetCellValue( i, 0, aData[i].first );
238  m_grid->SetCellValue( i, 1, aData[i].second );
239  }
240 
241  setColumnWidths();
242 
243  m_grid->Thaw();
244 }
245 
246 
247 std::vector<std::pair<wxString, wxString>> DIALOG_MANAGE_REPOSITORIES::GetData()
248 {
249  std::vector<std::pair<wxString, wxString>> result;
250 
251  for( int i = 0; i < m_grid->GetNumberRows(); i++ )
252  {
253  result.push_back( std::make_pair( m_grid->GetCellValue( i, 0 ),
254  m_grid->GetCellValue( i, 1 ) ) );
255  }
256 
257  return result;
258 }
259 
260 
261 void DIALOG_MANAGE_REPOSITORIES::OnSaveClicked( wxCommandEvent& event )
262 {
263  EndModal( wxID_SAVE );
264 }
void SetData(const std::vector< std::pair< wxString, wxString >> &aData)
std::vector< std::pair< wxString, wxString > > GetData()
void OnMoveUpButtonClicked(wxCommandEvent &event) override
#define GRID_CELL_MARGIN
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:55
void swapRows(int aRowA, int aRowB)
std::shared_ptr< PLUGIN_CONTENT_MANAGER > m_pcm
void OnMoveDownButtonClicked(wxCommandEvent &event) override
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=false, bool aKeep=true)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:282
wxString name
Definition: pcm_data.h:121
void OnSaveClicked(wxCommandEvent &event) override
Class DIALOG_MANAGE_REPOSITORIES_BASE.
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
void OnGridCellClicked(wxGridEvent &event) override
DIALOG_MANAGE_REPOSITORIES(wxWindow *parent, std::shared_ptr< PLUGIN_CONTENT_MANAGER > aPcm)
Constructor.
#define _(s)
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void OnAddButtonClicked(wxCommandEvent &event) override
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
const char * name
Definition: DXF_plotter.cpp:56
void OnRemoveButtonClicked(wxCommandEvent &event) override
Package installation entry.
Definition: pcm_data.h:119