KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_database_lib_settings.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) 2023 Jon Evans <[email protected]>
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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <algorithm>
22#include <set>
23
24#include <wx/app.h>
25#include <wx/display.h>
26#include <wx/filedlg.h>
27#include <wx/filefn.h>
28#include <wx/filename.h>
29#include <wx/menu.h>
30#include <wx/regex.h>
31#include <wx/settings.h>
32#include <wx/stdpaths.h>
33#include <wx/thread.h>
34#include <wx/utils.h>
35
36#include <bitmaps.h>
37#include <confirm.h>
38#include <core/kicad_algo.h>
42#include <eda_draw_frame.h>
43#include <eeschema_settings.h>
44#include <grid_tricks.h>
45#include <i18n_utility.h>
46#include <kiplatform/ui.h>
47#include <lib_symbol.h>
48#include <pgm_base.h>
49#include <reporter.h>
51#include <thread_pool.h>
52#include <scoped_set_reset.h>
56#include <string_utils.h>
60#include <widgets/wx_grid.h>
61#include <widgets/wx_infobar.h>
63
64#ifdef _WIN32
66#endif
67
68
70static constexpr size_t PREVIEW_ROW_LIMIT = 200;
71
73static constexpr int PREVIEW_MIN_WIDTH_DIP = 450;
74
76static constexpr int PREVIEW_DEBOUNCE_MS = 750;
77
79static constexpr int PROVIDER_POLL_MS = 250;
80static constexpr int PROVIDER_POLL_LIMIT = 80;
81
83static constexpr int CONNECT_DEBOUNCE_MS = 500;
84
85
92
93
109
110
112static void relayoutPage( wxScrolledWindow* aPage )
113{
114 aPage->Layout();
115 aPage->FitInside();
116}
117
118
120template <typename MAP>
121static bool isAutofilled( const MAP& aAutofilled, const typename MAP::key_type& aKey, const std::string& aValue )
122{
123 auto it = aAutofilled.find( aKey );
124
125 return it != aAutofilled.end() && it->second == aValue;
126}
127
128
138
139
140static wxString displayName( const DATABASE_LIB_TABLE& aTable )
141{
142 return aTable.name.empty() ? _( "(no nickname)" ) : From_UTF8( aTable.name );
143}
144
145
146static wxString describeFailure( DB_FAILURE aFailure, const DATABASE_SOURCE& aSource )
147{
148 using FAILURE = DB_FAILURE;
149
150 switch( aFailure )
151 {
152 case FAILURE::LOGIN:
153 if( aSource.type == DATABASE_SOURCE_TYPE::ACCESS )
154 return _( "The database password is missing or wrong." );
155
156 return _( "The server rejected the user name or password." );
157
158 case FAILURE::UNREACHABLE:
159 if( aSource.type != DATABASE_SOURCE_TYPE::ODBC )
160 return _( "Could not reach the database file; check that its drive or share is available." );
161
162 return _( "Could not reach the database server; check that it is running." );
163
164 case FAILURE::TIMEOUT:
165 if( aSource.type != DATABASE_SOURCE_TYPE::ODBC )
166 {
167 const int seconds = DatabaseFileWaitSeconds( aSource.timeout );
168
169 return wxString::Format( wxPLURAL( "The database file '%s' did not respond within %d second.",
170 "The database file '%s' did not respond within %d seconds.",
171 static_cast<unsigned>( seconds ) ),
172 wxFileName( From_UTF8( aSource.path ) ).GetFullName(), seconds );
173 }
174
175 if( aSource.timeout <= 0 )
176 return _( "The server did not answer in time." );
177
178 return wxString::Format( wxPLURAL( "The server did not answer within %d second.",
179 "The server did not answer within %d seconds.",
180 static_cast<unsigned>( aSource.timeout ) ),
181 aSource.timeout );
182
183 case FAILURE::NO_DSN:
184 if( !aSource.connection_string.empty() )
185 return _( "The connection string names a data source or driver that is not configured." );
186
187 return wxString::Format( _( "There is no ODBC data source named '%s'." ), From_UTF8( aSource.dsn ) );
188
189 case FAILURE::NO_DRIVER:
190 return _( "The ODBC driver for this data source is not installed." );
191
192 case FAILURE::FILE_NOT_FOUND: return _( "The database file was not found." );
193
194 case FAILURE::FILE_IN_USE: return _( "The database file is locked by another program." );
195
196 case FAILURE::PROVIDER_MISSING: return _( "The Microsoft Access Database Engine is not installed." );
197
198 case FAILURE::NOT_SUPPORTED_ON_PLATFORM: return _( "Microsoft Access databases can only be read on Windows." );
199
200 case FAILURE::INVALID_TYPE:
201 return wxString::Format( _( "This KiCad does not know the database type '%s'." ),
202 From_UTF8( aSource.type_name ) );
203
204 case FAILURE::NETWORK_WAL:
205 return _( "A database in write-ahead log mode cannot be read safely from a network drive." );
206
207 case FAILURE::UNKNOWN: break;
208 }
209
210 return _( "Could not connect to the database." );
211}
212
213
216{
217 const char* name;
218 std::vector<wxString> terms;
220};
221
222
223static const std::vector<AUTOFILL_FIELD>& autofillFields()
224{
225 static const std::vector<AUTOFILL_FIELD> fields = {
226 { "Value", { _HKI( "Value" ) }, true },
227 { "Datasheet", { _HKI( "Datasheet" ), _HKI( "data sheet" ) }, false }
228 };
229
230 return fields;
231}
232
233
234static std::string fieldChoiceId( const char* aFieldName )
235{
236 return std::string( "field." ) + aFieldName;
237}
238
239
240static DB_AUTOFILL::ROLE autofillRole( const std::string& aChoiceId, const std::vector<wxString>& aTerms )
241{
243
244 for( const wxString& term : aTerms )
245 {
246 role.terms.push_back( term );
247
248 if( wxString translated = wxGetTranslation( term ); translated != term )
249 role.terms.push_back( translated );
250 }
251
253 {
254 if( auto it = cfg->m_DatabaseColumnChoices.find( aChoiceId ); it != cfg->m_DatabaseColumnChoices.end() )
255 role.remembered = it->second;
256 }
257
258 return role;
259}
260
261
262static DATABASE_FIELD_MAPPING* findField( DATABASE_LIB_TABLE& aLib, const char* aName )
263{
264 for( DATABASE_FIELD_MAPPING& field : aLib.fields )
265 {
266 if( wxString::FromUTF8( field.name ).IsSameAs( aName, false ) )
267 return &field;
268 }
269
270 return nullptr;
271}
272
273
274DIALOG_DATABASE_LIB_SETTINGS::DIALOG_DATABASE_LIB_SETTINGS( wxWindow* aParent, const wxString& aDblPath,
276 const wxString& aDefaultDir ) :
278 m_defaultDir( aDefaultDir ),
279 m_libs( aLibs ),
280 m_preview( nullptr ),
281 m_currentLibrary( -1 ),
282 m_loading( false ),
283 m_unnamed( aDblPath.IsEmpty() ),
284 m_flagMissing( false ),
285 m_canSave( true ),
286 m_previewDebounce( [this]()
287 {
289 },
291 m_connectDebounce( [this]()
292 {
293 storeSource();
294 connect();
295 },
297{
298 m_providerTimer.SetOwner( this );
299 Bind( wxEVT_TIMER, &DIALOG_DATABASE_LIB_SETTINGS::onProviderTimer, this, m_providerTimer.GetId() );
300
301 // Search terms are English; autofill also tries their translations
302 m_roles = {
303 { m_cbKeyCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.key_col; }, false, _( "key" ),
304 "key", { _HKI( "id" ), _HKI( "key" ) } },
305 { m_cbSymbolsCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.symbols_col; }, false,
306 _( "symbols" ), "symbols", { _HKI( "symbol" ), _HKI( "sym" ) } },
307 { m_cbFootprintsCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.footprints_col; }, false,
308 _( "footprints" ), "footprints", { _HKI( "footprint" ), _HKI( "fp" ) } },
309 { m_cbPinsCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.pins_col; }, true, _( "pin map" ),
310 "pins", { _HKI( "pin map" ), _HKI( "pins" ) } },
311 { m_cbDescriptionCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.properties.description; },
312 true, _( "description" ), "description", { _HKI( "description" ), _HKI( "desc" ) } },
313 { m_cbKeywordsCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.properties.keywords; }, true,
314 _( "keywords" ), "keywords", { _HKI( "keywords" ), _HKI( "tags" ) } },
315 { m_cbFpFiltersCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.properties.footprint_filters; },
316 true, _( "footprint filters" ), "footprint_filters",
317 { _HKI( "footprint filters" ), _HKI( "fp filters" ) } },
318 { m_cbExclSimCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.properties.exclude_from_sim; },
319 true, _( "exclude from simulation" ), "exclude_from_sim",
320 { _HKI( "exclude from simulation" ), _HKI( "exclude from sim" ) } },
321 { m_cbExclBomCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.properties.exclude_from_bom; },
322 true, _( "exclude from BOM" ), "exclude_from_bom", { _HKI( "exclude from BOM" ) } },
323 { m_cbExclBoardCol, []( DATABASE_LIB_TABLE& t ) -> std::string& { return t.properties.exclude_from_board; },
324 true, _( "exclude from board" ), "exclude_from_board", { _HKI( "exclude from board" ) } }
325 };
326
327 wxFileName fn;
328 bool isNew = true;
329 bool loaded = true;
330
331 if( m_unnamed )
332 {
333 // The file is chosen on OK, so there is nothing to load yet
334 m_settings = std::make_unique<DATABASE_LIB_SETTINGS>( std::string() );
335 m_settings->SetReadOnly( true );
336 m_settings->ResetToDefaults();
337 }
338 else
339 {
340 // The settings file always carries the extension, whatever the library table row says
341 fn.Assign( aDblPath );
343 m_path = fn.GetFullPath();
344
345 isNew = !wxFileExists( m_path );
346
347 m_settings = LoadDatabaseLibSettingsReadOnly( m_path, &loaded );
348 m_canSave = loaded && !m_settings->IsFutureFormat();
349 }
350
351 EDA_BASE_FRAME* frame = nullptr;
352
353 for( wxWindow* window = aParent; window && !frame; window = window->GetParent() )
354 frame = dynamic_cast<EDA_BASE_FRAME*>( window );
355
357 EDA_DRAW_FRAME* drawFrame = dynamic_cast<EDA_DRAW_FRAME*>( frame );
358
359 if( drawFrame && drawFrame->GetCanvas() )
360 canvasType = drawFrame->GetCanvas()->GetBackend();
361 else if( COMMON_SETTINGS* cfg = Pgm().GetCommonSettings() )
362 canvasType = EDA_DRAW_PANEL_GAL::ResolveStoredCanvasType( cfg->m_Graphics.canvas_type );
363
364 m_preview = new DB_LIB_PREVIEW_PANEL( m_previewPanel, frame, HasKiway() ? &Kiway() : nullptr, canvasType );
365 m_previewSizer->Add( m_preview, 1, wxEXPAND | wxALL, 5 );
366
367 m_btnBrowse->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
368 m_btnAddLibrary->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
369 m_btnDuplicateLibrary->SetBitmap( KiBitmapBundle( BITMAPS::copy ) );
370 m_btnMoveLibraryUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
371 m_btnMoveLibraryDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
372 m_btnRemoveLibrary->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
373 m_btnAddField->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
374 m_btnMoveFieldUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
375 m_btnMoveFieldDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
376 m_btnRemoveField->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
377
379 {
380 wxGridCellAttr* attr = new wxGridCellAttr;
381 attr->SetRenderer( new wxGridCellBoolRenderer() );
382 attr->SetEditor( new wxGridCellBoolEditor() );
383 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
384 m_fieldsGrid->SetColAttr( col, attr );
385 }
386
387 m_fieldsGrid->PushEventHandler( new GRID_TRICKS( m_fieldsGrid ) );
388 m_dataGrid->PushEventHandler( new GRID_TRICKS( m_dataGrid ) );
389
390 // The button turns into Stop while connecting and must not shift the status beside it
391 m_btnTest->SetMinSize( m_btnTest->GetBestSize() );
392
393 // Lets the label shrink below its text so long messages ellipsize instead of widening the page
394 m_stConnectionStatus->SetMinSize( wxSize( FromDIP( 40 ), -1 ) );
395 m_stDsnDriver->SetMinSize( wxSize( FromDIP( 40 ), -1 ) );
396 m_stSymbolExample->SetMinSize( wxSize( FromDIP( 40 ), -1 ) );
397
398 if( m_unnamed && m_defaultDir.IsEmpty() )
399 m_defaultDir = wxStandardPaths::Get().GetDocumentsDir();
400
401 m_stCwdHelp->SetLabel( m_unnamed ? wxString::Format( _( "${CWD} is replaced by %s (the folder the file will "
402 "be saved in)" ),
403 m_defaultDir )
404 : wxString::Format( _( "${CWD} is replaced by %s" ), fn.GetPath() ) );
405
406 // The hint sits in the narrow library list column
407 m_stNoLibraries->Wrap( FromDIP( 160 ) );
408
409 if( !loaded )
410 {
411 m_infoBar->ShowMessage( _( "This file could not be read and is shown read-only." ), wxICON_ERROR );
412 }
413 else if( m_settings->IsFutureFormat() )
414 {
415 m_infoBar->ShowMessage( _( "This file was written by a newer version of KiCad and is shown read-only." ),
416 wxICON_WARNING );
417 }
418 else if( isNew )
419 {
420 m_infoBar->ShowMessage( _( "Choose a data source and click Test Connection, then add a library on the "
421 "Libraries page." ),
422 wxICON_INFORMATION );
423 }
424
425 // Every control mirrors the file being edited, so restoring values from a previous session
426 // would silently overwrite it
427 OptOut( this );
428
429 // Child windows of the combo boxes' parents, which destroy them
430 m_tableIcon = new FIELD_WARNING_ICON( m_cbTable );
431
432 for( const COLUMN_ROLE& role : m_roles )
433 {
434 m_roleIcons.push_back( new FIELD_WARNING_ICON( role.combo ) );
435
436 role.combo->Bind( wxEVT_KILL_FOCUS,
437 [this]( wxFocusEvent& aEvent )
438 {
439 aEvent.Skip();
441 } );
442 }
443
444 // Bound after GRID_TRICKS so the mapping menu runs first; a left click opens it as well
445 m_dataGrid->Bind( wxEVT_GRID_LABEL_RIGHT_CLICK, &DIALOG_DATABASE_LIB_SETTINGS::OnDataLabelRightClick, this );
446 m_dataGrid->Bind( wxEVT_GRID_LABEL_LEFT_CLICK, &DIALOG_DATABASE_LIB_SETTINGS::OnDataLabelRightClick, this );
447
448 m_fieldsGrid->GetGridColLabelWindow()->Bind( wxEVT_MOTION,
449 [this]( wxMouseEvent& aEvent )
450 {
451 const wxString tips[] = {
452 _( "Database column the field reads its value from" ),
453 _( "Name of the symbol field" ),
454 _( "Show the field on the schematic when the symbol is placed" ),
455 _( "Show the field as a column in the symbol chooser" ),
456 _( "Show the field name before its value" ),
457 _( "Take position and style from the symbol's own field of the same name" )
458 };
459
460 int x = m_fieldsGrid->CalcUnscrolledPosition( aEvent.GetPosition() ).x;
461 int col = m_fieldsGrid->XToCol( x );
462
463 bool validCol = col >= 0 && col < static_cast<int>( std::size( tips ) );
464
465 m_fieldsGrid->GetGridColLabelWindow()->SetToolTip( validCol ? tips[col] : wxString() );
466 aEvent.Skip();
467 } );
468
469 // Escape arrives as the Cancel button too
470 Bind( wxEVT_BUTTON, &DIALOG_DATABASE_LIB_SETTINGS::onCancel, this, wxID_CANCEL );
471 Bind( wxEVT_CLOSE_WINDOW, &DIALOG_DATABASE_LIB_SETTINGS::onCloseWindow, this );
472
473 SetTitle( m_unnamed ? _( "New Database Library" )
474 : wxString::Format( _( "Database Library Settings (%s)" ), fn.GetFullName() ) );
475
476 SetupStandardButtons();
477
478 if( !m_canSave )
479 m_sdbSizerOK->Disable();
480
481 // The pages scroll, so their full height must not make the dialog taller than the screen
482 wxSize fullSize = GetBestSize();
483
484 // wxFormBuilder's Fit() leaves each page as tall as its content, which GTK will not shrink
485 for( wxScrolledWindow* page : { m_connectionPage, m_librariesPage } )
486 {
487 page->SetMinSize( wxSize( -1, FromDIP( 200 ) ) );
488 page->SetSize( wxSize( page->GetSize().x, FromDIP( 200 ) ) );
489 }
490
491 finishDialogSettings();
492
493 // DIALOG_SHIM opens at this size, clamped to the monitor the dialog lands on
494 m_initialSize.IncTo( fullSize );
495 m_connectionPage->FitInside();
496 m_librariesPage->FitInside();
497
498 // The base class restores a fixed sash position on the first idle event, which cuts the pages off wherever
499 // the platform's fonts and controls need more room
500 m_splitter->Disconnect( wxEVT_IDLE, wxIdleEventHandler( DIALOG_DATABASE_LIB_SETTINGS_BASE::m_splitterOnIdle ),
501 nullptr, this );
502 m_splitter->Bind( wxEVT_IDLE, &DIALOG_DATABASE_LIB_SETTINGS::onSplitterIdle, this );
503}
504
505
508static void releaseConnection( std::unique_ptr<DATABASE_CONNECTION> aConn )
509{
510 if( !aConn )
511 return;
512
513 // Once the app is gone the pool may be deleted too
514 if( !wxTheApp || !wxIsMainThread() )
515 {
516 aConn.reset();
517 return;
518 }
519
520 GetKiCadThreadPool().detach_task(
521 [holder = std::shared_ptr<DATABASE_CONNECTION>( std::move( aConn ) )]() mutable
522 {
523 holder.reset();
524 } );
525}
526
527
529{
530 // A job purged from the pool's queue is destroyed under its lock and never opened anything
531 if( !started )
532 {
533 conn.reset();
534 return;
535 }
536
537 releaseConnection( std::move( conn ) );
538}
539
540
542{
543 m_providerTimer.Stop();
544
546 m_previewDebounce.Cancel();
547 m_connectDebounce.Cancel();
548 releaseConnection( std::move( m_conn ) );
549
550 m_fieldsGrid->PopEventHandler( true );
551 m_dataGrid->PopEventHandler( true );
552}
553
554
556{
557 SCOPED_SET_RESET<bool> loading( m_loading, true );
558
559 DATABASE_SOURCE& source = m_settings->m_Source;
560 bool useConnectionString = !source.connection_string.empty();
561 bool passwordInFile = !source.password.empty();
562
564
566 m_keyringLogin.password.clear();
567
568 if( !passwordInFile && GetDatabasePassword( source, m_keyringLogin.password ) )
569 source.password = m_keyringLogin.password;
570
571 m_txtName->ChangeValue( From_UTF8( m_settings->m_Name ) );
572 m_txtDescription->ChangeValue( From_UTF8( m_settings->m_Description ) );
573
574 m_rbDSN->SetValue( !useConnectionString );
575 m_rbConnectionString->SetValue( useConnectionString );
576
577 loadBackends();
578
579 m_dbFile = From_UTF8( source.Expanded( cwdDir() ).path );
580 m_pathEdited = false;
581 m_txtDbFile->ChangeValue( m_dbFile );
582 m_txtFilePassword->ChangeValue( source.type == DATABASE_SOURCE_TYPE::ACCESS ? From_UTF8( source.password )
583 : wxString() );
584
585 for( const std::string& driver : ODBC_BACKEND::ListDrivers() )
586 m_choiceDriver->Append( From_UTF8( driver ) );
587
588 m_cbDSN->ChangeValue( From_UTF8( source.dsn ) );
590 m_txtUser->ChangeValue( From_UTF8( source.username ) );
591 m_txtPassword->ChangeValue( source.type == DATABASE_SOURCE_TYPE::ODBC ? From_UTF8( source.password ) : wxString() );
592 m_cbPasswordInFile->SetValue( passwordInFile );
593 m_txtConnectionString->ChangeValue( From_UTF8( source.connection_string ) );
594 m_spinTimeout->SetValue( source.timeout );
595
596 m_spinCacheSize->SetValue( m_settings->m_Cache.max_size );
597 m_spinCacheAge->SetValue( m_settings->m_Cache.max_age );
598 m_cbGloballyUniqueKeys->SetValue( m_settings->m_GloballyUniqueKeys );
599
601
602 m_currentLibrary = m_settings->m_Tables.empty() ? -1 : 0;
604 loadLibrary();
605
607
608 // Connect once the dialog is visible; a slow server should not delay it appearing
609 CallAfter( [this]()
610 {
611 connect();
612 } );
613
614 return true;
615}
616
617
619{
620 m_connectDebounce.Cancel();
621
622 // Ctrl+Enter reaches here even with OK disabled
623 if( !m_canSave || !m_fieldsGrid->CommitPendingChanges() )
624 return false;
625
626 storeSource();
627 storeLibrary();
628
629 if( !validateLibraries() )
630 return false;
631
632 // Asked last so that cancelling it leaves the keyring and the dialog as they were
633 if( m_unnamed && m_path.IsEmpty() && !chooseSavePath() )
634 return false;
635
636 DATABASE_SOURCE& source = m_settings->m_Source;
637 std::string password = source.password;
638 bool useKeyring = source.type == DATABASE_SOURCE_TYPE::ODBC && source.connection_string.empty()
639 && !m_cbPasswordInFile->GetValue();
640
641 if( useKeyring )
642 {
643 bool sameLogin = source.dsn == m_keyringLogin.dsn && source.username == m_keyringLogin.username;
644
645 if( !password.empty() && ( !sameLogin || password != m_keyringLogin.password ) )
646 {
647 if( !StoreDatabasePassword( source, password ) )
648 {
649 DisplayError( this, _( "The password could not be saved in the system keyring, which needs a "
650 "user name and a running keyring service.\n\n"
651 "Check 'Save password in the library file' to keep it there instead." ) );
652 return false;
653 }
654 }
655 else if( password.empty() && sameLogin && !m_keyringLogin.password.empty() )
656 {
657 // A failure here is usually an entry that is already gone
658 StoreDatabasePassword( source, password );
659 }
660
662 source.password.clear();
663 }
664
665 // A file chosen in the save dialog is replaced, however much it already holds
666 m_settings->SetReadOnly( false );
667 bool saved = m_settings->SaveToFile( wxEmptyString, m_unnamed || !wxFileExists( m_path ) );
668 m_settings->SetReadOnly( true );
669
670 // The dialog stays open on failure and keeps connecting with the entered password
671 source.password = password;
672
673 // An unchanged file is not rewritten, which is not a failure
674 if( !saved && m_settings->LastSaveFailed() )
675 {
676 DisplayError( this, wxString::Format( _( "Could not save '%s'." ), m_path ) );
677
678 // The next OK asks for a location again, starting in the same folder
679 if( m_unnamed )
680 {
682 m_path.Clear();
683 }
684
685 return false;
686 }
687
688 m_unnamed = false;
689
691 {
692 for( const auto& [id, column] : m_newChoices )
693 DB_AUTOFILL::RememberChoice( cfg->m_DatabaseColumnChoices[id], column );
694 }
695
696 return true;
697}
698
699
701{
702 std::set<std::string> nicknames;
703
704 for( size_t ii = 0; ii < m_settings->m_Tables.size(); ++ii )
705 {
706 const DATABASE_LIB_TABLE& table = m_settings->m_Tables[ii];
707 wxString msg;
708 wxWindow* focus = nullptr;
709 wxString name = From_UTF8( table.name );
710
711 if( table.table.empty() && table.key_col.empty() )
712 {
713 msg = wxString::Format( _( "Library '%s' needs a table and a key column." ), name );
714 focus = m_cbTable;
715 }
716 else if( table.table.empty() )
717 {
718 msg = wxString::Format( _( "Library '%s' needs a table." ), name );
719 focus = m_cbTable;
720 }
721 else if( table.key_col.empty() )
722 {
723 msg = wxString::Format( _( "Library '%s' needs a key column." ), name );
724 focus = m_cbKeyCol;
725 }
726 else if( !nicknames.insert( table.name ).second )
727 {
728 msg = wxString::Format( _( "More than one library is named '%s'." ), name );
729 focus = m_txtLibName;
730 }
731
732 if( !focus )
733 continue;
734
735 m_flagMissing = true;
736 m_notebook->SetSelection( 1 );
737
738 // Reloading would drop the autofill state of a library the user is already looking at
739 if( m_currentLibrary != static_cast<int>( ii ) )
740 {
741 m_currentLibrary = static_cast<int>( ii );
743 loadLibrary();
744 }
745 else
746 {
748 loadDataGrid();
749 }
750
751 showInfo( msg, wxICON_ERROR );
752 focus->SetFocus();
753 return false;
754 }
755
756 return true;
757}
758
759
761{
762 return m_path.IsEmpty() ? m_defaultDir : wxFileName( m_path ).GetPath();
763}
764
765
767{
768 wxString ext = From_UTF8( FILEEXT::DatabaseLibraryFileExtension.c_str() );
769 wxString filter = _( "KiCad database library files" )
771
772 wxString dir = m_defaultDir;
773 wxString name = DefaultDatabaseLibFileName( From_UTF8( m_settings->m_Name ) );
774
775 wxFileDialog dlg( this, _( "Save Database Library" ), dir, name + wxS( "." ) + ext, filter,
776 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
777
779
780 if( dlg.ShowModal() != wxID_OK )
781 return false;
782
783 wxFileName fn( dlg.GetPath() );
785
786 // The file dialog only confirmed the name as typed, not the one with the extension added
787 if( fn.GetFullPath() != dlg.GetPath() && fn.FileExists()
788 && !IsOK( this, wxString::Format( _( "Overwrite '%s'?" ), fn.GetFullPath() ) ) )
789 {
790 return false;
791 }
792
793 wxString oldBase = cwdDir();
794 m_path = fn.GetFullPath();
795
796 m_settings->SetFilename( m_path );
797 m_settings->Internals()->SetFromString( "meta.filename", std::string( m_settings->GetFullFilename().ToUTF8() ) );
798 m_stCwdHelp->SetLabel( wxString::Format( _( "${CWD} is replaced by %s" ), cwdDir() ) );
799
800 // The picked file stays absolute so its stored form follows the save folder
801 storeSource();
802
803 // The connection was tested against another folder than the one it will resolve in once saved
804 const DATABASE_SOURCE& stored = m_settings->m_Source;
805
806 if( oldBase != cwdDir()
807 && ( stored.connection_string.find( "${CWD}" ) != std::string::npos
808 || stored.path.find( "${CWD}" ) != std::string::npos ) )
809 {
810 connect();
811 showInfo( _( "The data source uses ${CWD} and was tested again in the folder you chose. "
812 "Check the result, then click OK to save." ),
813 wxICON_INFORMATION );
814 return false;
815 }
816
817 return true;
818}
819
820
822{
823 storeSource();
824 storeLibrary();
825
826 return m_settings->FormatAsString() + ( m_cbPasswordInFile->GetValue() ? "1" : "0" );
827}
828
829
831{
832 // A grid edit in progress only reaches the settings once committed
833 if( !m_fieldsGrid->CommitPendingChanges() )
834 return CLOSE_ACTION::STAY;
835
838
839 bool saved = false;
840 wxString prompt = m_unnamed ? _( "Save the new database library before closing?" )
841 : wxString::Format( _( "Save changes to '%s' before closing?" ),
842 wxFileName( m_path ).GetFullName() );
843
844 if( !HandleUnsavedChanges( this, prompt,
845 [&]() -> bool
846 {
847 saved = TransferDataFromWindow();
848 return saved;
849 } ) )
850 {
851 return CLOSE_ACTION::STAY;
852 }
853
854 if( saved )
855 {
856 EndDialogShim( wxID_OK );
857 return CLOSE_ACTION::SAVED;
858 }
859
861}
862
863
864void DIALOG_DATABASE_LIB_SETTINGS::onCancel( wxCommandEvent& aEvent )
865{
866 m_connectDebounce.Cancel();
867
869 aEvent.Skip();
870}
871
872
874{
875 m_connectDebounce.Cancel();
876
877 if( !aEvent.CanVeto() )
878 {
879 aEvent.Skip();
880 return;
881 }
882
883 // The default close handler sends a Cancel, which would ask a second time
885 EndDialogShim( wxID_CANCEL );
886 else
887 aEvent.Veto();
888}
889
890
892{
893 if( m_loading )
894 return;
895
896 m_settings->m_Name = TO_UTF8( m_txtName->GetValue() );
897 m_settings->m_Description = TO_UTF8( m_txtDescription->GetValue() );
898 m_settings->m_Cache.max_size = m_spinCacheSize->GetValue();
899 m_settings->m_Cache.max_age = m_spinCacheAge->GetValue();
900 m_settings->m_GloballyUniqueKeys = m_cbGloballyUniqueKeys->GetValue();
901
903 markDirty();
904}
905
906
908{
909 OnSettingChanged( aEvent );
910}
911
912
914{
915 // Clearing the list raises text events that would store an empty data source
916 SCOPED_SET_RESET<bool> loading( m_loading, true );
917
918 wxString current = m_cbDSN->GetValue();
919
921 m_cbDSN->Clear();
922
924 m_cbDSN->Append( From_UTF8( source.name ) );
925
926 m_cbDSN->ChangeValue( current );
928}
929
930
932{
933 std::string name = TO_UTF8( m_cbDSN->GetValue() );
934 wxString text;
935
936 if( !name.empty() )
937 {
938 text = _( "Not configured on this computer" );
939
941 {
942 if( source.name == name )
943 text = wxString::Format( _( "Driver: %s" ), From_UTF8( source.driver ) );
944 }
945 }
946
947 m_stDsnDriver->SetLabel( text );
948 m_stDsnDriver->SetToolTip( text );
949
950 // SetLabel() resizes the control to its text; layout shrinks it back so the end ellipsizes
952}
953
954
956{
957#ifdef __WXMSW__
958 // Waits so that the list can show what was added
959 wxExecute( wxS( "odbcad32.exe" ), wxEXEC_SYNC );
961#endif
962}
963
964
965static wxString accessLabel( ACCESS_PROVIDER aProvider )
966{
967 return aProvider == ACCESS_PROVIDER::MISSING ? _( "Access (unavailable)" ) : _( "Access" );
968}
969
970
972{
973 const DATABASE_SOURCE& source = m_settings->m_Source;
974
976 m_choiceBackend->Clear();
977 m_choiceBackend->Append( _( "ODBC" ) );
978 m_choiceBackend->Append( _( "SQLite" ) );
979
980 const ACCESS_PROVIDER provider = accessProvider();
981
982 if( provider == ACCESS_PROVIDER::PENDING )
984
985 // A file naming Access keeps its entry, marked when this computer cannot read it, so opening it never changes it
987 {
989 m_choiceBackend->Append( accessLabel( provider ) );
990 }
991
993 {
995 m_choiceBackend->Append( wxString::Format( _( "Unknown (%s)" ), From_UTF8( source.type_name ) ) );
996 }
997
998 selectBackend( source.type );
999}
1000
1001
1003{
1004 const int selection = m_choiceBackend->GetSelection();
1005
1006 if( selection < 0 || selection >= static_cast<int>( m_backendTypes.size() ) )
1008
1009 return m_backendTypes[selection];
1010}
1011
1012
1014{
1015 auto it = std::find( m_backendTypes.begin(), m_backendTypes.end(), aType );
1016
1017 m_choiceBackend->SetSelection( it == m_backendTypes.end() ? 0 : static_cast<int>( it - m_backendTypes.begin() ) );
1018}
1019
1020
1022{
1023 const ACCESS_PROVIDER provider = accessProvider();
1024
1026 return;
1027
1028 m_providerTimer.Stop();
1029
1030 // A check that never finished counts as unavailable
1031 const ACCESS_PROVIDER shown =
1033 const DATABASE_SOURCE_TYPE current = selectedBackend();
1034 auto access = std::find( m_backendTypes.begin(), m_backendTypes.end(), DATABASE_SOURCE_TYPE::ACCESS );
1035
1036 if( access != m_backendTypes.end() )
1037 {
1038 m_choiceBackend->SetString( access - m_backendTypes.begin(), accessLabel( shown ) );
1039 }
1040 else if( shown == ACCESS_PROVIDER::AVAILABLE )
1041 {
1042 // An unknown type always comes last
1044 m_choiceBackend->Insert( accessLabel( shown ), 2 );
1045 }
1046
1047 selectBackend( current );
1048}
1049
1050
1052{
1053 const DATABASE_SOURCE& source = m_settings->m_Source;
1054
1055 switch( source.type )
1056 {
1057 case DATABASE_SOURCE_TYPE::ODBC: return !source.dsn.empty() || !source.connection_string.empty();
1059 case DATABASE_SOURCE_TYPE::ACCESS: return !source.path.empty();
1060 case DATABASE_SOURCE_TYPE::INVALID: return true;
1061 }
1062
1063 return false;
1064}
1065
1066
1068{
1070 const bool odbc = type == DATABASE_SOURCE_TYPE::ODBC;
1071 const bool access = type == DATABASE_SOURCE_TYPE::ACCESS;
1072 const bool file = access || type == DATABASE_SOURCE_TYPE::SQLITE;
1073 const bool useDSN = m_rbDSN->GetValue();
1074 wxSizer* sbSource = m_rbDSN->GetContainingSizer();
1075 wxSizer* fgBackend = m_choiceBackend->GetContainingSizer();
1076
1077 sbSource->Show( m_rbDSN, odbc );
1078 sbSource->Show( m_rbConnectionString, odbc );
1079 sbSource->Show( m_cbDSN->GetContainingSizer(), odbc && useDSN );
1080 sbSource->Show( m_cbPasswordInFile, odbc && useDSN );
1081 sbSource->Show( m_txtConnectionString, odbc && !useDSN );
1082 sbSource->Show( m_choiceDriver->GetContainingSizer(), odbc && !useDSN );
1083 sbSource->Show( m_stCwdHelp, odbc && !useDSN );
1084
1085 fgBackend->Show( m_fileLabel, file );
1086 fgBackend->Show( m_txtDbFile->GetContainingSizer(), file );
1087 fgBackend->Show( m_filePasswordLabel, access );
1088 fgBackend->Show( m_txtFilePassword, access );
1089
1090 // Showing the data source rows shows every control in them
1091#ifdef __WXMSW__
1092 m_btnManageDsn->Show( odbc && useDSN );
1093#else
1094 m_btnManageDsn->Hide();
1095#endif
1096
1098}
1099
1100
1102{
1104 sourceEdited();
1105}
1106
1107
1109{
1110 if( m_loading )
1111 return;
1112
1114 m_pathEdited = true;
1115 sourceEdited();
1116
1117 // Typing reports every keystroke, and a file on an unreachable share must not stall this thread
1118 m_connectDebounce.Restart();
1119 setConnectionStatus( _( "The file is tried when typing pauses." ), false );
1120}
1121
1122
1124{
1125 wxString dir = wxPathOnly( m_dbFile );
1126
1127 if( dir.IsEmpty() )
1128 dir = cwdDir();
1129
1130 wxFileDialog dlg( this, _( "Select the database file" ), dir, wxEmptyString,
1131 _( "SQLite databases (*.sqlite;*.sqlite3;*.db)|*.sqlite;*.sqlite3;*.db|"
1132 "Access databases (*.accdb;*.mdb)|*.accdb;*.mdb|All files|*" ),
1133 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
1134
1136
1137 if( dlg.ShowModal() != wxID_OK )
1138 return;
1139
1140 m_txtDbFile->ChangeValue( dlg.GetPath() );
1141 m_dbFile = DatabasePathFromInput( dlg.GetPath(), cwdDir() );
1142 m_pathEdited = true;
1143 sourceEdited();
1144 m_connectDebounce.Cancel();
1145 storeSource();
1146 connect();
1147}
1148
1149
1151{
1153 OnSourceChanged( aEvent );
1154}
1155
1156
1158{
1159 sourceEdited();
1160}
1161
1162
1164{
1165 if( m_loading )
1166 return;
1167
1168 m_connectDebounce.Cancel();
1170 storeSource();
1171 cancelConnect();
1172 markDirty();
1173
1174 // Connecting on every keystroke would hammer the server, so it waits for an explicit test
1175 setConnectionStatus( _( "Data source changed; click Test Connection to apply it." ), false );
1176}
1177
1178
1180{
1181 OnSourceChanged( aEvent );
1182}
1183
1184
1186{
1187 wxString driver = wxString::Format( wxS( "Driver={%s};" ), m_choiceDriver->GetStringSelection() );
1188 wxString text = m_txtConnectionString->GetValue();
1189 wxRegEx existing( wxS( "Driver=\\{[^}]*\\};?" ), wxRE_ICASE | wxRE_ADVANCED );
1190
1191 if( !existing.Replace( &text, driver, 1 ) )
1192 text = driver + text;
1193
1194 m_txtConnectionString->SetValue( text );
1195}
1196
1197
1199{
1200 if( m_connectJob )
1201 {
1202 cancelConnect();
1203 setConnectionStatus( _( "Connection attempt stopped." ), false );
1204 refreshTables();
1205 markDirty();
1206 return;
1207 }
1208
1209 m_connectDebounce.Cancel();
1210 storeSource();
1211 connect();
1212}
1213
1214
1216{
1217 DATABASE_SOURCE& source = m_settings->m_Source;
1219
1220 source.timeout = m_spinTimeout->GetValue();
1221
1222 // An unknown type is restored as the file has it, whatever was chosen in between
1223 if( type == DATABASE_SOURCE_TYPE::INVALID )
1224 {
1226 source.timeout = m_spinTimeout->GetValue();
1227 return;
1228 }
1229
1230 if( source.type != type )
1231 {
1232 source.type = type;
1233 source.type_name.clear();
1234 source.path.clear();
1235 }
1236
1237 if( type == DATABASE_SOURCE_TYPE::ODBC )
1238 {
1239 if( m_rbConnectionString->GetValue() )
1240 {
1241 source.connection_string = TO_UTF8( m_txtConnectionString->GetValue() );
1242 source.dsn.clear();
1243 source.username.clear();
1244 source.password.clear();
1245 }
1246 else
1247 {
1248 source.connection_string.clear();
1249 source.dsn = TO_UTF8( m_cbDSN->GetValue() );
1250 source.username = TO_UTF8( m_txtUser->GetValue() );
1251 source.password = TO_UTF8( m_txtPassword->GetValue() );
1252 }
1253
1254 return;
1255 }
1256
1257 source.connection_string.clear();
1258 source.dsn.clear();
1259 source.username.clear();
1261
1262 // The keyring is keyed by DSN login, so Access keeps its password in the file
1263 source.password = type == DATABASE_SOURCE_TYPE::ACCESS ? TO_UTF8( m_txtFilePassword->GetValue() ) : std::string();
1264}
1265
1266
1268{
1269 cancelConnect();
1270
1271 releaseConnection( std::move( m_conn ) );
1272 m_tables.clear();
1273 m_columns.clear();
1274 m_sampleTable.clear();
1275 m_sampleColumns.clear();
1276 m_sampleRows.clear();
1277 m_sampleRepeats.clear();
1278 loadDataGrid();
1279 markDirty();
1280
1281 const DATABASE_SOURCE& source = m_settings->m_Source;
1282
1283 if( !sourceConfigured() )
1284 {
1285 setConnectionStatus( source.type == DATABASE_SOURCE_TYPE::ODBC ? _( "No data source configured." )
1286 : _( "No database file chosen." ),
1287 false );
1288 refreshTables();
1289 return;
1290 }
1291
1292 auto job = std::make_shared<CONNECT_JOB>();
1293 job->owner = this;
1295 job->conn = std::move( created.connection );
1296
1297 if( !job->conn )
1298 {
1299 setConnectionStatus( describeFailure( created.failure, source ), true, From_UTF8( created.error ) );
1300 refreshTables();
1301 return;
1302 }
1303
1304 m_connectJob = job;
1305
1306 const std::string sourceName = m_settings->ExpandedSource( cwdDir() ).Describe();
1307
1308 setConnectionStatus( sourceName.empty() ? _( "Connecting..." )
1309 : wxString::Format( _( "Connecting to %s..." ), From_UTF8( sourceName ) ),
1310 false );
1311 m_btnTest->SetLabel( _( "Stop" ) );
1312 refreshTables();
1313
1314 // A slow or unreachable server would otherwise freeze the dialog for the whole login timeout
1315 GetKiCadThreadPool().detach_task(
1316 [job]()
1317 {
1318 job->started = true;
1319 job->connected = job->conn->Connect();
1320
1321 if( job->connected )
1322 job->conn->GetTables( job->tables );
1323
1324 // A callback still pending at exit is destroyed after the pool, so it must not own the job
1325 if( wxTheApp )
1326 {
1327 wxTheApp->CallAfter(
1328 [weak = std::weak_ptr<CONNECT_JOB>( job )]()
1329 {
1330 std::shared_ptr<CONNECT_JOB> finished = weak.lock();
1331
1332 if( finished && finished->owner )
1333 finished->owner->onConnectFinished( finished );
1334 } );
1335 }
1336 } );
1337}
1338
1339
1341{
1342 if( !m_connectJob )
1343 return;
1344
1345 m_connectJob->owner = nullptr;
1346 m_connectJob.reset();
1347 m_btnTest->SetLabel( _( "Test Connection" ) );
1348}
1349
1350
1351void DIALOG_DATABASE_LIB_SETTINGS::onConnectFinished( const std::shared_ptr<CONNECT_JOB>& aJob )
1352{
1353 if( aJob != m_connectJob )
1354 return;
1355
1356 m_connectJob.reset();
1357 m_btnTest->SetLabel( _( "Test Connection" ) );
1358
1359 if( aJob->connected )
1360 {
1361 m_conn = std::move( aJob->conn );
1362 m_tables = std::move( aJob->tables );
1363 setConnectionStatus( wxString::Format( wxPLURAL( "Connected; %zu table or view found.",
1364 "Connected; %zu tables and views found.",
1365 static_cast<unsigned>( m_tables.size() ) ),
1366 m_tables.size() ),
1367 false );
1368 }
1369 else
1370 {
1371 setConnectionStatus( describeFailure( aJob->conn->GetLastFailure(), m_settings->m_Source ), true,
1372 From_UTF8( aJob->conn->GetLastError() ) );
1373 }
1374
1375 refreshTables();
1376 autofillTable();
1377 loadDataGrid();
1378 markDirty();
1379}
1380
1381
1383{
1384 SCOPED_SET_RESET<bool> loading( m_loading, true );
1385
1386 wxArrayString tables;
1387
1388 for( const std::string& name : m_tables )
1389 tables.Add( From_UTF8( name ) );
1390
1391 wxString table = m_cbTable->GetValue();
1392 m_cbTable->Set( tables );
1393 m_cbTable->ChangeValue( table );
1395}
1396
1397
1398void DIALOG_DATABASE_LIB_SETTINGS::setConnectionStatus( const wxString& aText, bool aError,
1399 const wxString& aDetails )
1400{
1401 m_stConnectionStatus->SetLabel( aText );
1402 m_stConnectionStatus->SetToolTip( aDetails.IsEmpty() ? aText : aText + wxS( "\n\n" ) + aDetails );
1403 m_stConnectionStatus->SetForegroundColour( aError ? *wxRED : wxNullColour );
1404
1405 // SetLabel() resizes the control to its text; layout shrinks it back so the end ellipsizes
1407}
1408
1409
1411{
1412 if( m_currentLibrary < 0 || m_currentLibrary >= static_cast<int>( m_settings->m_Tables.size() ) )
1413 return nullptr;
1414
1415 return &m_settings->m_Tables[m_currentLibrary];
1416}
1417
1418
1419const std::vector<std::string>& DIALOG_DATABASE_LIB_SETTINGS::columnsFor( const std::string& aTable )
1420{
1421 static const std::vector<std::string> empty;
1422
1423 if( !m_conn || aTable.empty() )
1424 return empty;
1425
1426 auto it = m_columns.find( aTable );
1427
1428 if( it == m_columns.end() )
1429 {
1430 it = m_columns.emplace( aTable, std::vector<std::string>() ).first;
1431 m_conn->GetColumns( aTable, it->second );
1432 }
1433
1434 return it->second;
1435}
1436
1437
1439{
1440 wxArrayString names;
1441
1442 for( const DATABASE_LIB_TABLE& table : m_settings->m_Tables )
1443 names.Add( displayName( table ) );
1444
1445 m_libList->Set( names );
1446
1447 if( m_currentLibrary >= 0 )
1448 m_libList->SetSelection( m_currentLibrary );
1449
1451}
1452
1453
1455{
1456 bool empty = m_settings->m_Tables.empty();
1457
1458 if( m_stNoLibraries->IsShown() == empty && m_btnAddFirstLibrary->IsShown() == empty )
1459 return;
1460
1461 m_stNoLibraries->Show( empty );
1462 m_btnAddFirstLibrary->Show( empty );
1464}
1465
1466
1468{
1470
1471 m_autoTable.clear();
1472 m_autoRoles.clear();
1473 m_autoFields.clear();
1474 m_userRoles.clear();
1475
1476 {
1477 SCOPED_SET_RESET<bool> loading( m_loading, true );
1478
1479 for( wxWindow* control : std::initializer_list<wxWindow*>{ m_txtLibName, m_cbTable, m_fieldsGrid,
1485 {
1486 control->Enable( lib != nullptr );
1487 }
1488
1489 for( const COLUMN_ROLE& role : m_roles )
1490 role.combo->Enable( lib != nullptr );
1491
1492 m_txtLibName->ChangeValue( lib ? From_UTF8( lib->name ) : wxString() );
1493 m_cbTable->ChangeValue( lib ? From_UTF8( lib->table ) : wxString() );
1494
1496 loadFields();
1497 }
1498
1500
1501 // Another library may sample the same table with different column roles
1502 loadDataGrid();
1503 markDirty();
1504}
1505
1506
1508{
1511 DATABASE_LIB_TABLE& shown = lib ? *lib : empty;
1512
1513 wxArrayString columns;
1514
1515 for( const std::string& column : columnsFor( shown.table ) )
1516 columns.Add( From_UTF8( column ) );
1517
1518 // Optional mappings offer a blank entry to clear them
1519 wxArrayString optionalColumns = columns;
1520 optionalColumns.Insert( wxEmptyString, 0 );
1521
1522 SCOPED_SET_RESET<bool> loading( m_loading, true );
1523
1524 for( const COLUMN_ROLE& role : m_roles )
1525 {
1526 role.combo->Set( role.optional ? optionalColumns : columns );
1527 role.combo->ChangeValue( From_UTF8( role.value( shown ) ) );
1528 }
1529
1530 wxGridCellAttr* attr = new wxGridCellAttr;
1531 attr->SetEditor( new GRID_CELL_COMBOBOX( columns ) );
1532 m_fieldsGrid->SetColAttr( FIELD_COL_COLUMN, attr );
1533
1537}
1538
1539
1540void DIALOG_DATABASE_LIB_SETTINGS::markUserRole( const COLUMN_ROLE& aRole, const std::string& aValue )
1541{
1542 m_userRoles.insert( aRole.combo );
1543 m_autoRoles.erase( aRole.combo );
1544 m_newChoices[aRole.autofillId] = aValue;
1545}
1546
1547
1549{
1550 for( const COLUMN_ROLE& role : m_roles )
1551 {
1552 std::string value = TO_UTF8( role.combo->GetValue() );
1553 bool autofilled = !value.empty() && isAutofilled( m_autoRoles, role.combo, value );
1554
1555 wxFont font = role.combo->GetFont();
1556 wxFontStyle style = autofilled ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL;
1557
1558 // A new font on a combo that is being typed in makes Windows expand the typed character to a matching entry
1559 // and select it, so the next key replaces it. The style follows when focus leaves.
1560 wxWindow* focus = wxWindow::FindFocus();
1561 bool typing = focus && ( focus == role.combo || focus->GetParent() == role.combo );
1562
1563 if( font.GetStyle() != style && !typing )
1564 {
1565 font.SetStyle( style );
1566 role.combo->SetFont( font );
1567 }
1568 }
1569}
1570
1571
1573{
1575
1576 if( !lib )
1577 return;
1578
1579 lib->name = TO_UTF8( m_txtLibName->GetValue() );
1580
1581 // The loader strips '/' because it separates the nickname from the symbol name
1582 std::erase( lib->name, '/' );
1583
1584 lib->table = TO_UTF8( m_cbTable->GetValue() );
1585
1586 for( const COLUMN_ROLE& role : m_roles )
1587 role.value( *lib ) = TO_UTF8( role.combo->GetValue() );
1588}
1589
1590
1592{
1593 if( !m_fieldsGrid->CommitPendingChanges() )
1594 return;
1595
1596 storeLibrary();
1597 m_currentLibrary = m_libList->GetSelection();
1598 loadLibrary();
1599}
1600
1601
1603{
1604 storeLibrary();
1605 m_flagMissing = false;
1606 m_settings->m_Tables.emplace_back();
1607 m_currentLibrary = static_cast<int>( m_settings->m_Tables.size() ) - 1;
1609 loadLibrary();
1610 m_txtLibName->SetFocus();
1611}
1612
1613
1615{
1616 storeLibrary();
1617
1618 if( DATABASE_LIB_TABLE* lib = currentLibrary() )
1619 {
1620 DATABASE_LIB_TABLE copy = *lib;
1621 copy.name += "-copy";
1622 m_settings->m_Tables.insert( m_settings->m_Tables.begin() + m_currentLibrary + 1, std::move( copy ) );
1625 loadLibrary();
1626 }
1627}
1628
1629
1631{
1632 storeLibrary();
1633
1634 int target = m_currentLibrary + aDelta;
1635
1636 if( m_currentLibrary < 0 || target < 0 || target >= static_cast<int>( m_settings->m_Tables.size() ) )
1637 {
1638 wxBell();
1639 return;
1640 }
1641
1642 std::swap( m_settings->m_Tables[m_currentLibrary], m_settings->m_Tables[target] );
1643 m_currentLibrary = target;
1645}
1646
1647
1649{
1650 moveLibrary( -1 );
1651}
1652
1653
1655{
1656 moveLibrary( 1 );
1657}
1658
1659
1661{
1662 if( !currentLibrary() )
1663 return;
1664
1665 m_settings->m_Tables.erase( m_settings->m_Tables.begin() + m_currentLibrary );
1666 m_currentLibrary = std::min( m_currentLibrary, static_cast<int>( m_settings->m_Tables.size() ) - 1 );
1668 loadLibrary();
1669}
1670
1671
1673{
1674 if( m_loading )
1675 return;
1676
1677 storeLibrary();
1678
1679 if( DATABASE_LIB_TABLE* lib = currentLibrary() )
1680 m_libList->SetString( m_currentLibrary, displayName( *lib ) );
1681
1682 for( const COLUMN_ROLE& role : m_roles )
1683 {
1684 if( aEvent.GetEventObject() == role.combo )
1685 {
1686 markUserRole( role, TO_UTF8( role.combo->GetValue() ) );
1687 }
1688 }
1689
1690 if( aEvent.GetEventObject() == m_txtLibName )
1691 autofillTable();
1692
1697 markDirty();
1698}
1699
1700
1702{
1703 if( m_loading )
1704 return;
1705
1706 storeLibrary();
1707 m_autoTable.clear();
1708
1710
1711 // Typing a partial name would otherwise query the catalog for every keystroke
1712 if( lib && ( m_tables.empty() || alg::contains( m_tables, lib->table ) ) )
1713 {
1716 }
1717
1720 markDirty();
1721}
1722
1723
1725{
1727
1728 if( !lib || m_tables.empty() || ( !lib->table.empty() && lib->table != m_autoTable ) )
1729 return;
1730
1731 std::string table = DB_AUTOFILL::MatchTable( m_tables, lib->name );
1732
1733 if( table.empty() || table == lib->table )
1734 return;
1735
1736 lib->table = table;
1738
1739 {
1740 SCOPED_SET_RESET<bool> loading( m_loading, true );
1741 m_cbTable->ChangeValue( From_UTF8( table ) );
1742 }
1743
1744 // Reloads the column choices and, by sampling the table, the data grid
1746}
1747
1748
1750{
1752
1753 if( !lib || !alg::contains( m_tables, lib->table ) )
1754 return;
1755
1756 // The sample shows which columns repeat, and a repeating column cannot be the key
1757 ensureSample( lib->table );
1758
1759 // Mappings the user made keep their columns, which are then not offered to other roles
1760 std::set<std::string> taken;
1761 std::vector<const COLUMN_ROLE*> roles;
1762 std::vector<const AUTOFILL_FIELD*> fields;
1763 std::vector<DB_AUTOFILL::ROLE> requests;
1764
1765 for( const COLUMN_ROLE& role : m_roles )
1766 {
1767 const std::string& value = role.value( *lib );
1768
1769 if( m_userRoles.contains( role.combo )
1770 || ( !value.empty() && !isAutofilled( m_autoRoles, role.combo, value ) ) )
1771 {
1772 taken.insert( value );
1773 continue;
1774 }
1775
1776 roles.push_back( &role );
1777 requests.push_back( autofillRole( role.autofillId, role.terms ) );
1778
1779 if( role.combo == m_cbKeyCol && m_sampleTable == lib->table )
1780 {
1781 for( const auto& [column, repeats] : m_sampleRepeats )
1782 {
1783 if( repeats > 0 )
1784 requests.back().excluded.insert( column );
1785 }
1786 }
1787 }
1788
1789 for( const AUTOFILL_FIELD& def : autofillFields() )
1790 {
1791 const DATABASE_FIELD_MAPPING* field = findField( *lib, def.name );
1792
1793 if( field && !isAutofilled( m_autoFields, def.name, field->column ) )
1794 continue;
1795
1796 fields.push_back( &def );
1797 requests.push_back( autofillRole( fieldChoiceId( def.name ), def.terms ) );
1798 }
1799
1800 for( const DATABASE_FIELD_MAPPING& field : lib->fields )
1801 {
1802 if( !isAutofilled( m_autoFields, field.name, field.column ) )
1803 taken.insert( field.column );
1804 }
1805
1806 std::vector<std::string> columns;
1807
1808 for( const std::string& column : columnsFor( lib->table ) )
1809 {
1810 if( !taken.contains( column ) )
1811 columns.push_back( column );
1812 }
1813
1814 std::vector<std::string> chosen = DB_AUTOFILL::AssignColumns( columns, requests );
1815
1816 for( size_t ii = 0; ii < roles.size(); ++ii )
1817 {
1818 roles[ii]->value( *lib ) = chosen[ii];
1819 m_autoRoles[roles[ii]->combo] = chosen[ii];
1820 }
1821
1822 for( size_t ii = 0; ii < fields.size(); ++ii )
1823 {
1824 const AUTOFILL_FIELD& def = *fields[ii];
1825 const std::string& column = chosen[roles.size() + ii];
1826 DATABASE_FIELD_MAPPING* field = findField( *lib, def.name );
1827
1828 if( column.empty() )
1829 {
1830 if( field )
1831 lib->fields.erase( lib->fields.begin() + ( field - lib->fields.data() ) );
1832
1833 m_autoFields.erase( def.name );
1834 }
1835 else
1836 {
1837 if( field )
1838 field->column = column;
1839 else
1840 lib->fields.emplace_back( column, def.name, def.visibleOnAdd, def.visibleOnAdd, false, false );
1841
1842 m_autoFields[def.name] = column;
1843 }
1844 }
1845
1847 loadFields();
1849}
1850
1851
1853{
1855
1856 m_fieldsGrid->ClearRows();
1857
1858 if( !lib )
1859 return;
1860
1861 m_fieldsGrid->AppendRows( static_cast<int>( lib->fields.size() ) );
1862
1863 auto boolCell =
1864 []( bool aValue )
1865 {
1866 return aValue ? wxS( "1" ) : wxS( "" );
1867 };
1868
1869 for( int row = 0; row < static_cast<int>( lib->fields.size() ); ++row )
1870 {
1871 const DATABASE_FIELD_MAPPING& field = lib->fields[row];
1872
1873 m_fieldsGrid->SetCellValue( row, FIELD_COL_COLUMN, From_UTF8( field.column ) );
1874 m_fieldsGrid->SetCellValue( row, FIELD_COL_NAME, From_UTF8( field.name ) );
1875 m_fieldsGrid->SetCellValue( row, FIELD_COL_VISIBLE_ON_ADD, boolCell( field.visible_on_add ) );
1876 m_fieldsGrid->SetCellValue( row, FIELD_COL_VISIBLE_IN_CHOOSER, boolCell( field.visible_in_chooser ) );
1877 m_fieldsGrid->SetCellValue( row, FIELD_COL_SHOW_NAME, boolCell( field.show_name ) );
1878 m_fieldsGrid->SetCellValue( row, FIELD_COL_INHERIT, boolCell( field.inherit_properties ) );
1879 }
1880
1881 // Counts the header too, since empty rows give no width and the labels would overlap
1882 for( int col = 0; col < m_fieldsGrid->GetNumberCols(); ++col )
1883 m_fieldsGrid->SetColSize( col, m_fieldsGrid->GetVisibleWidth( col ) );
1884}
1885
1886
1888{
1890 int row = aEvent.GetRow();
1891
1892 if( !lib || row < 0 || row >= static_cast<int>( lib->fields.size() ) )
1893 return;
1894
1895 // Edit in place so each mapping keeps the raw JSON it was loaded from
1896 DATABASE_FIELD_MAPPING& field = lib->fields[row];
1897 wxString value = m_fieldsGrid->GetCellValue( row, aEvent.GetCol() );
1898 bool checked = value == wxS( "1" );
1899
1900 switch( aEvent.GetCol() )
1901 {
1902 case FIELD_COL_COLUMN: field.column = TO_UTF8( value ); break;
1903 case FIELD_COL_NAME: field.SetName( TO_UTF8( value ) ); break;
1904 case FIELD_COL_VISIBLE_ON_ADD: field.visible_on_add = checked; break;
1905 case FIELD_COL_VISIBLE_IN_CHOOSER: field.visible_in_chooser = checked; break;
1906 case FIELD_COL_SHOW_NAME: field.show_name = checked; break;
1907 case FIELD_COL_INHERIT: field.inherit_properties = checked; break;
1908 default: break;
1909 }
1910
1911 if( aEvent.GetCol() == FIELD_COL_COLUMN || aEvent.GetCol() == FIELD_COL_NAME )
1912 {
1913 for( const AUTOFILL_FIELD& def : autofillFields() )
1914 {
1915 if( wxString::FromUTF8( field.name ).IsSameAs( def.name, false ) )
1916 {
1917 m_autoFields.erase( def.name );
1918 m_newChoices[fieldChoiceId( def.name )] = field.column;
1919 }
1920 }
1921
1923 }
1924
1925 markDirty();
1926}
1927
1928
1929void DIALOG_DATABASE_LIB_SETTINGS::OnAddField( wxCommandEvent& aEvent )
1930{
1932
1933 if( !lib )
1934 return;
1935
1936 m_fieldsGrid->OnAddRow(
1937 [&]() -> std::pair<int, int>
1938 {
1939 lib->fields.emplace_back();
1940 m_fieldsGrid->AppendRows( 1 );
1941 return { m_fieldsGrid->GetNumberRows() - 1, FIELD_COL_COLUMN };
1942 } );
1943}
1944
1945
1947{
1948 if( DATABASE_LIB_TABLE* lib = currentLibrary() )
1949 m_fieldsGrid->OnMoveRowUp( [&]( int row ) { swapFields( *lib, row, row - 1 ); } );
1950}
1951
1952
1954{
1955 if( DATABASE_LIB_TABLE* lib = currentLibrary() )
1956 m_fieldsGrid->OnMoveRowDown( [&]( int row ) { swapFields( *lib, row, row + 1 ); } );
1957}
1958
1959
1961{
1962 std::swap( aLib.fields[aRow], aLib.fields[aOther] );
1963 m_fieldsGrid->SwapRows( aRow, aOther );
1964 markDirty();
1965}
1966
1967
1969{
1970 if( DATABASE_LIB_TABLE* lib = currentLibrary() )
1971 {
1972 m_fieldsGrid->OnDeleteRows(
1973 [&]( int row )
1974 {
1975 lib->fields.erase( lib->fields.begin() + row );
1976 m_fieldsGrid->DeleteRows( row, 1 );
1977 } );
1978
1980 markDirty();
1981 }
1982}
1983
1984
1986{
1988
1989 if( !lib || !m_fieldsGrid->CommitPendingChanges() )
1990 return;
1991
1992 std::set<std::string> mapped;
1993
1994 for( const COLUMN_ROLE& role : m_roles )
1995 mapped.insert( role.value( *lib ) );
1996
1997 for( const DATABASE_FIELD_MAPPING& field : lib->fields )
1998 mapped.insert( field.column );
1999
2000 for( const std::string& column : columnsFor( lib->table ) )
2001 {
2002 if( !mapped.contains( column ) )
2003 lib->fields.emplace_back( column, column, false, false, false, false );
2004 }
2005
2006 loadFields();
2008 markDirty();
2009}
2010
2011
2013{
2015
2016 if( !lib )
2017 return nullptr;
2018
2019 for( const DATABASE_FIELD_MAPPING& field : lib->fields )
2020 {
2021 if( field.column == aColumn )
2022 return &field;
2023 }
2024
2025 return nullptr;
2026}
2027
2028
2030{
2032
2033 // Rebuilding moves the grid cursor, which must not select a symbol in the preview
2034 SCOPED_SET_RESET<bool> loading( m_loading, true );
2035
2036 m_dataGrid->BeginBatch();
2037 m_dataGrid->ClearRows();
2038
2039 if( m_dataGrid->GetNumberCols() > 0 )
2040 m_dataGrid->DeleteCols( 0, m_dataGrid->GetNumberCols() );
2041
2042 if( lib && !m_sampleTable.empty() && m_sampleTable == lib->table )
2043 {
2044 m_dataGrid->AppendCols( static_cast<int>( m_sampleColumns.size() ) );
2045 m_dataGrid->AppendRows( static_cast<int>( m_sampleRows.size() ) );
2046
2047 for( int col = 0; col < static_cast<int>( m_sampleColumns.size() ); ++col )
2048 {
2049 for( int row = 0; row < static_cast<int>( m_sampleRows.size() ); ++row )
2050 {
2051 auto it = m_sampleRows[row].find( m_sampleColumns[col] );
2052
2053 if( it == m_sampleRows[row].end() )
2054 continue;
2055
2056 if( const std::string* value = std::any_cast<std::string>( &it->second ) )
2057 m_dataGrid->SetCellValue( row, col, From_UTF8( *value ) );
2058 }
2059 }
2060
2061 // Labels first, so the sizing accounts for the role lines under each column name
2063 m_dataGrid->AutoSizeColumns( false );
2064 }
2065 else
2066 {
2068 }
2069
2070 m_dataGrid->EndBatch();
2071}
2072
2073
2075{
2077 wxString text;
2078
2079 if( lib )
2080 {
2081 wxString symbol;
2082
2083 if( m_sampleTable == lib->table && !m_sampleRows.empty() )
2084 symbol = SCH_IO_DATABASE::SymbolNameForRow( *lib, m_settings->m_GloballyUniqueKeys, m_sampleRows[0] );
2085
2086 if( symbol.IsEmpty() )
2087 {
2088 wxString key = _( "<key>" );
2089
2090 if( m_settings->m_GloballyUniqueKeys || lib->name.empty() )
2091 symbol = key;
2092 else
2093 symbol = From_UTF8( lib->name ) + wxS( "/" ) + key;
2094 }
2095
2096 text = wxString::Format( _( "Symbols appear as %s" ), symbol );
2097 }
2098
2099 m_stSymbolExample->SetLabel( text );
2100 m_stSymbolExample->SetToolTip( text );
2101
2102 // SetLabel() resizes the control to its text; layout shrinks it back so the end ellipsizes
2104}
2105
2106
2108{
2110
2112
2113 if( !lib )
2114 {
2115 m_stDataStatus->SetLabel( _( "Choose a library on the Libraries page to see its table." ) );
2116 return;
2117 }
2118
2119 if( m_sampleTable.empty() || m_sampleTable != lib->table )
2120 {
2121 m_stDataStatus->SetLabel( m_conn ? _( "No rows loaded." ) : _( "Not connected." ) );
2122 return;
2123 }
2124
2125 std::map<std::string, wxString> roles;
2126
2127 for( const COLUMN_ROLE& role : m_roles )
2128 {
2129 const std::string& column = role.value( *lib );
2130
2131 if( !column.empty() )
2132 roles.emplace( column, role.label );
2133 }
2134
2135 for( int col = 0; col < m_dataGrid->GetNumberCols(); ++col )
2136 {
2137 const std::string& column = m_sampleColumns[col];
2138 wxString label = From_UTF8( column );
2139
2140 if( auto role = roles.find( column ); role != roles.end() )
2141 label += wxS( "\n" ) + role->second;
2142 else if( const DATABASE_FIELD_MAPPING* field = fieldForColumn( column ) )
2143 label += wxS( "\n" ) + wxString::Format( _( "field '%s'" ), field->name_wx );
2144
2145 m_dataGrid->SetColLabelValue( col, label );
2146 }
2147
2148 const unsigned sampled = static_cast<unsigned>( m_sampleRows.size() );
2149 wxString rows;
2150
2151 if( m_sampleRows.size() < PREVIEW_ROW_LIMIT )
2152 rows = wxString::Format( wxPLURAL( "%zu row", "%zu rows", sampled ), m_sampleRows.size() );
2153 else
2154 rows = wxString::Format( wxPLURAL( "first %zu row", "first %zu rows", sampled ), m_sampleRows.size() );
2155
2156 m_stDataStatus->SetLabel( wxString::Format( _( "Library '%s', table '%s', %s. Click a column header to map it." ),
2157 displayName( *lib ), From_UTF8( lib->table ), rows ) );
2158}
2159
2160
2162{
2164 int col = aEvent.GetCol();
2165
2166 if( !lib || m_sampleTable != lib->table || col < 0 || col >= static_cast<int>( m_sampleColumns.size() )
2167 || !m_fieldsGrid->CommitPendingChanges() )
2168 {
2169 return;
2170 }
2171
2172 // A copy, since the preview timer can replace the sample while the popup menu runs its event loop
2173 const std::string column = m_sampleColumns[col];
2174 bool mapped = fieldForColumn( column ) != nullptr;
2175 wxMenu menu;
2176 int fieldId = wxWindow::NewControlId();
2177
2178 menu.Append( fieldId, mapped ? _( "Remove Field Mapping" ) : _( "Map as Field" ) );
2179 menu.AppendSeparator();
2180
2181 std::map<int, const COLUMN_ROLE*> roleIds;
2182
2183 for( const COLUMN_ROLE& role : m_roles )
2184 {
2185 int id = wxWindow::NewControlId();
2186 roleIds[id] = &role;
2187 menu.AppendCheckItem( id, wxString::Format( _( "Use for %s" ), role.label ) )
2188 ->Check( role.value( *lib ) == column );
2189 }
2190
2191 int choice = GetPopupMenuSelectionFromUser( menu );
2192
2193 if( auto it = roleIds.find( choice ); it != roleIds.end() )
2194 {
2195 const COLUMN_ROLE& role = *it->second;
2196
2197 role.value( *lib ) = role.value( *lib ) == column ? std::string() : column;
2198 markUserRole( role, role.value( *lib ) );
2201 markDirty();
2202 return;
2203 }
2204
2205 if( choice != fieldId )
2206 return;
2207
2208 if( mapped )
2209 {
2210 std::erase_if( lib->fields,
2211 [&]( const DATABASE_FIELD_MAPPING& aField )
2212 {
2213 return aField.column == column;
2214 } );
2215 }
2216 else
2217 {
2218 lib->fields.emplace_back( column, column, false, false, false, false );
2219 }
2220
2221 loadFields();
2223 markDirty();
2224}
2225
2226
2228{
2230 int row = aEvent.GetRow();
2231
2232 if( !m_loading && lib && row >= 0 && row < static_cast<int>( m_sampleRows.size() ) )
2233 {
2234 m_preview->SelectSymbol( SCH_IO_DATABASE::SymbolNameForRow( *lib, m_settings->m_GloballyUniqueKeys,
2235 m_sampleRows[row] ) );
2236 }
2237
2238 aEvent.Skip();
2239}
2240
2241
2242void DIALOG_DATABASE_LIB_SETTINGS::showInfo( const wxString& aMessage, int aIcon )
2243{
2244 m_infoBar->RemoveAllButtons();
2245 m_infoBar->AddCloseButton();
2246 m_infoBar->ShowMessage( aMessage, aIcon );
2247}
2248
2249
2251{
2252 m_splitter->Unbind( wxEVT_IDLE, &DIALOG_DATABASE_LIB_SETTINGS::onSplitterIdle, this );
2253
2254 // What the pages need in the pane around them. A scrolled window accepts being narrower than its content, so
2255 // its own size says nothing, and the sizer's minimum does.
2256 int needed = 0;
2257
2258 for( wxScrolledWindow* page : { m_connectionPage, m_librariesPage } )
2259 {
2260 int overhead = m_editPanel->GetSize().x - page->GetClientSize().x;
2261
2262#ifndef __WXOSX__
2263 // Classic scrollbars take room from the page once the page grows taller than its pane
2264 if( !page->HasScrollbar( wxVERTICAL ) )
2265 overhead += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X, page );
2266#endif
2267
2268 needed = std::max( needed, page->GetSizer()->CalcMin().x + overhead );
2269 }
2270
2271 // The preview is only useful with room for its tree columns and symbol, so the dialog grows to give it that
2272 const int previewWidth = FromDIP( PREVIEW_MIN_WIDTH_DIP );
2273 const int deficit = needed + m_splitter->GetSashSize() + previewWidth - m_splitter->GetClientSize().x;
2274
2275 if( deficit > 0 )
2276 {
2277 wxRect display = wxDisplay( this ).GetClientArea();
2278 wxSize size = GetSize();
2279
2280 SetSize( std::min( size.x + deficit, display.width ), size.y );
2281 }
2282
2283 // Resizing then only changes the preview, so the pages keep the width they need
2284 m_splitter->SetSashGravity( 0.0 );
2285 m_splitter->SetSashPosition( std::clamp( needed, m_splitter->GetMinimumPaneSize(),
2286 std::max( m_splitter->GetClientSize().x - previewWidth, 0 ) ) );
2287
2288 // Narrower than this the pages are cut off, and where scrollbars are overlays nothing says they scroll
2289 wxSize minSize = GetMinSize();
2290
2291 minSize.x = std::max( minSize.x, GetSize().x - m_splitter->GetClientSize().x + needed + m_splitter->GetSashSize()
2292 + m_splitter->GetMinimumPaneSize() );
2293 SetMinSize( minSize );
2294}
2295
2296
2301
2302
2303bool DIALOG_DATABASE_LIB_SETTINGS::ensureSample( const std::string& aTable )
2304{
2305 if( !m_conn || aTable.empty() )
2306 return false;
2307
2308 if( m_sampleTable == aTable )
2309 return true;
2310
2311 wxBusyCursor busy;
2312
2313 // Only a successful sample is cached, so an error stays visible until the table reads
2314 m_sampleTable.clear();
2315 m_sampleRepeats.clear();
2316
2317 bool ok = m_conn->SelectSample( aTable, PREVIEW_ROW_LIMIT, m_sampleColumns, m_sampleRows );
2318
2319 if( ok )
2320 {
2321 m_sampleTable = aTable;
2323 }
2324
2325 loadDataGrid();
2326 return ok;
2327}
2328
2329
2331{
2333
2334 wxArrayString warnings;
2335
2336 if( m_flagMissing && lib && lib->table.empty() )
2337 warnings.Add( _( "Choose a table." ) );
2338 else if( !m_tableError.IsEmpty() )
2339 warnings.Add( m_tableError );
2340
2341 m_tableIcon->SetWarnings( warnings );
2342}
2343
2344
2346{
2348
2350
2351 // A table name still being typed must not query the catalog for its columns
2352 const std::vector<std::string>* columns =
2353 lib && alg::contains( m_tables, lib->table ) ? &columnsFor( lib->table ) : nullptr;
2354
2355 // The sample counts rows with the same key, which is only meaningful for the table it was read from
2356 size_t repeats = 0;
2357
2358 if( lib && m_sampleTable == lib->table )
2359 {
2360 if( auto it = m_sampleRepeats.find( lib->key_col ); it != m_sampleRepeats.end() )
2361 repeats = it->second;
2362 }
2363
2364 for( size_t ii = 0; ii < m_roles.size(); ++ii )
2365 {
2366 const COLUMN_ROLE& role = m_roles[ii];
2367 const bool isKey = role.combo == m_cbKeyCol;
2368 wxArrayString warnings;
2369
2370 if( lib )
2371 {
2372 const std::string& value = role.value( *lib );
2373
2374 if( value.empty() )
2375 {
2376 if( isKey && m_flagMissing )
2377 warnings.Add( _( "Choose a key column." ) );
2378 }
2379 else if( columns && !columns->empty() && !alg::contains( *columns, value ) )
2380 {
2381 warnings.Add( wxString::Format( _( "Column '%s' is not in table '%s'." ), From_UTF8( value ),
2382 From_UTF8( lib->table ) ) );
2383 }
2384
2385 if( isKey && repeats > 0 )
2386 {
2387 warnings.Add( wxString::Format(
2388 wxPLURAL( "'%s' repeats in %zu of the first %zu row, and parts that share a key hide each "
2389 "other. Choose a column with a unique value per part.",
2390 "'%s' repeats in %zu of the first %zu rows, and parts that share a key hide each "
2391 "other. Choose a column with a unique value per part.",
2392 static_cast<unsigned>( m_sampleRows.size() ) ),
2393 From_UTF8( lib->key_col ), repeats, m_sampleRows.size() ) );
2394 }
2395 }
2396
2397 // The repeat warning already covers every key the preview found repeated
2398 if( isKey )
2399 {
2400 for( const wxString& message : previewIssues( FIELD::KEY ) )
2401 warnings.Add( message );
2402
2403 if( repeats == 0 )
2404 {
2405 for( const wxString& message : previewIssues( FIELD::REPEATED_KEY ) )
2406 warnings.Add( message );
2407 }
2408 }
2409 else if( role.combo == m_cbSymbolsCol )
2410 {
2411 for( const wxString& message : previewIssues( FIELD::SYMBOLS ) )
2412 warnings.Add( message );
2413 }
2414 else if( role.combo == m_cbPinsCol )
2415 {
2416 for( const wxString& message : previewIssues( FIELD::PIN_MAP ) )
2417 warnings.Add( message );
2418 }
2419
2420 m_roleIcons[ii]->SetWarnings( warnings );
2421 }
2422}
2423
2424
2426{
2427 wxArrayString messages;
2428
2430 {
2431 if( issue.field == aField )
2432 messages.Add( issue.message );
2433 }
2434
2435 return messages;
2436}
2437
2438
2440{
2442
2443 auto clear =
2444 [&]( const wxString& aStatus, const wxString& aDetails = wxEmptyString )
2445 {
2446 m_tableError = aDetails.IsEmpty() ? wxString() : aStatus + wxS( "\n" ) + aDetails;
2447 m_previewIssues.clear();
2450 m_preview->SetSymbols( wxEmptyString, {} );
2451 m_preview->SetStatus( aStatus );
2452 };
2453
2454 if( !lib )
2455 return clear( _( "Add a library on the Libraries page to preview it." ) );
2456
2457 if( m_connectJob )
2458 return clear( _( "Connecting..." ) );
2459
2460 if( !m_conn )
2461 return clear( _( "Not connected to a database." ) );
2462
2463 if( lib->table.empty() )
2464 return clear( _( "Choose a table." ) );
2465
2466 if( !ensureSample( lib->table ) )
2467 {
2468 return clear( wxString::Format( _( "Could not read table '%s'." ), From_UTF8( lib->table ) ),
2469 From_UTF8( m_conn->GetLastError() ) );
2470 }
2471
2472 m_tableError.Clear();
2474
2475 m_previewIssues.clear();
2476 std::vector<std::unique_ptr<LIB_SYMBOL>> symbols = SCH_IO_DATABASE::BuildPreviewSymbols(
2477 m_libs, *lib, m_settings->m_GloballyUniqueKeys, m_sampleRows, &m_previewIssues );
2478
2479 size_t count = symbols.size();
2480 wxString status;
2481
2482 if( lib->key_col.empty() )
2483 status = _( "Choose a key column." );
2484 else if( !m_sampleRows.empty() && count == 0 && !alg::contains( m_sampleColumns, lib->key_col ) )
2485 status = wxString::Format( _( "Key column '%s' is not in table '%s'." ), From_UTF8( lib->key_col ),
2486 From_UTF8( lib->table ) );
2487 else if( m_sampleRows.size() < PREVIEW_ROW_LIMIT )
2488 status = wxString::Format( wxPLURAL( "%zu symbol.", "%zu symbols.", static_cast<unsigned>( count ) ), count );
2489 else
2490 status = wxString::Format( wxPLURAL( "%zu symbol from the first %zu rows.",
2491 "%zu symbols from the first %zu rows.", static_cast<unsigned>( count ) ),
2492 count, m_sampleRows.size() );
2493
2495
2496 m_preview->SetSymbols( displayName( *lib ), std::move( symbols ) );
2497 m_preview->SetStatus( status );
2498}
const char * name
static std::string describeFailure(const std::string &aPath, const CALL_RESULT &aResult, const std::string &aPassword)
Composes the message for a failed call, naming the file.
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
static PROVIDER_STATE ProviderState()
Tells whether the ACE OLE DB provider is installed and loads.
@ BUSY
The check could not start because the engine lock stayed out of reach.
@ PENDING
The check has not finished, so nothing is known yet.
Shows symbols that are not in any library table the way the symbol chooser does: a searchable library...
DIALOG_DATABASE_LIB_SETTINGS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Database Library Settings"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(1200, 760), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void updateTableWarning()
Marks the table field when it is required and missing or could not be read.
void onConnectFinished(const std::shared_ptr< CONNECT_JOB > &aJob)
bool m_loading
Set while controls are filled from the settings, so their change events are ignored.
int m_currentLibrary
Index into m_settings->m_Tables of the library shown on the Libraries page, or -1.
void updateLibraryListHints()
Shows the empty state of the library list only while it is empty.
std::set< wxComboBox * > m_userRoles
Role combos the user set for the current library, which autofill leaves alone.
std::map< std::string, std::string > m_newChoices
Columns the user chose this session, keyed like EESCHEMA_SETTINGS::m_DatabaseColumnChoices.
std::map< std::string, size_t > m_sampleRepeats
void OnSourceSpinChanged(wxSpinEvent &aEvent) override
wxArrayString previewIssues(SCH_IO_DATABASE::PREVIEW_ISSUE::FIELD aField) const
void markUserRole(const COLUMN_ROLE &aRole, const std::string &aValue)
Records that the user chose aValue for aRole, so autofill leaves it alone.
void loadBackends()
Fills the backend choice with what this computer offers, plus the one the file already names.
void autofillColumns()
Maps columns of the chosen table to roles and fields the user has not set.
bool m_canSave
False when the file is unreadable or from a newer KiCad, since saving would lose its contents.
void OnBrowseFile(wxCommandEvent &aEvent) override
void markDirty()
Restarts the debounce before the preview is rebuilt.
void updateDsnDriver()
Shows which driver the chosen data source uses.
void loadDataSources()
Fills the data source list from the ODBC driver manager, keeping what is typed.
void autofillTable()
Picks the table named like the library while the user has not chosen one.
bool ensureSample(const std::string &aTable)
Reads aTable's first rows unless they are already loaded; false if they could not be read.
DATABASE_SOURCE m_loadedSource
The source as loaded, which an unknown type is restored from and source.path is kept from until a fil...
std::vector< DATABASE_CONNECTION::ROW > m_sampleRows
DATABASE_SOURCE m_keyringLogin
DSN login and keyring password at load, so an untouched login never rewrites the keyring.
wxString m_dbFile
The database file as picked, which stays absolute so the stored path can follow the save location.
void updateAutofillStyle()
Italicizes the role combos that still hold an autofilled value.
@ SAVED
The user chose to save, which ended the dialog with wxID_OK.
@ DISCARD
Nothing to keep, or the user chose to discard it.
bool m_unnamed
True until a new library has been given a file.
void OnSourceChanged(wxCommandEvent &aEvent) override
void OnDataLabelRightClick(wxGridEvent &aEvent) override
void OnMoveFieldUp(wxCommandEvent &aEvent) override
void OnAddField(wxCommandEvent &aEvent) override
DIALOG_DATABASE_LIB_SETTINGS(wxWindow *aParent, const wxString &aDblPath, SYMBOL_LIBRARY_ADAPTER *aLibs, const wxString &aDefaultDir=wxEmptyString)
WX_DEBOUNCED_ACTION m_previewDebounce
Coalesces edits into one preview refresh.
void onProviderTimer(wxTimerEvent &aEvent)
Offers Access once the provider check, which does not block, has found the engine.
std::vector< std::string > m_sampleColumns
void refreshTables()
Refreshes the table list and everything built on it after the connection changes.
void OnAddLibrary(wxCommandEvent &aEvent) override
void OnAddUnmappedColumns(wxCommandEvent &aEvent) override
void updateSymbolExample()
Shows how the current library's symbols will be named.
void updateDataLabels()
Refreshes only the Data page column labels, which follow mapping edits.
void OnMoveLibraryDown(wxCommandEvent &aEvent) override
bool m_flagMissing
Set once OK found a missing table or key, which is then flagged inline.
void connect()
Starts connecting with the current settings; the table list refreshes when it finishes.
void OnBackendChanged(wxCommandEvent &aEvent) override
std::map< std::string, std::vector< std::string > > m_columns
void OnInsertDriver(wxCommandEvent &aEvent) override
void OnLibraryChanged(wxCommandEvent &aEvent) override
DATABASE_SOURCE_TYPE selectedBackend() const
std::string m_cleanSnapshot
settingsSnapshot() when the dialog opened, to tell whether closing loses anything
std::vector< ODBC_BACKEND::DATA_SOURCE > m_dataSources
WX_DEBOUNCED_ACTION m_connectDebounce
Connects once typing in the file field pauses.
void swapFields(DATABASE_LIB_TABLE &aLib, int aRow, int aOther)
void OnFileTextChanged(wxCommandEvent &aEvent) override
wxString m_tableError
Why the table could not be read, shown on the table warning icon.
void OnMoveLibraryUp(wxCommandEvent &aEvent) override
bool chooseSavePath()
Asks where to save a library that has no file yet; false if the user cancelled.
const DATABASE_FIELD_MAPPING * fieldForColumn(const std::string &aColumn)
std::map< std::string, std::string > m_autoFields
void OnTestConnection(wxCommandEvent &aEvent) override
std::vector< FIELD_WARNING_ICON * > m_roleIcons
One per entry of m_roles.
SCH_IO_DATABASE::PREVIEW_ISSUES m_previewIssues
Problems found while building the preview symbols, shown on the icons of the fields they came from.
void sourceEdited()
Follows an edit of the data source by dropping the connection that no longer matches it.
void selectBackend(DATABASE_SOURCE_TYPE aType)
void updateColumnWarnings()
Marks each column field for a missing key, a column the table does not have, keys that repeat in the ...
void OnMoveFieldDown(wxCommandEvent &aEvent) override
std::map< wxComboBox *, std::string > m_autoRoles
CLOSE_ACTION confirmClose()
Asks what to do with unsaved edits before the dialog closes without OK.
void OnSourceTypeChanged(wxCommandEvent &aEvent) override
std::unique_ptr< DATABASE_CONNECTION > m_conn
void OnRemoveLibrary(wxCommandEvent &aEvent) override
void showInfo(const wxString &aMessage, int aIcon)
Replaces the info bar message, which can be dismissed.
void OnTableChanged(wxCommandEvent &aEvent) override
void OnManageDataSources(wxCommandEvent &aEvent) override
std::string m_autoTable
Autofilled values for the current library, which a later autofill may replace.
void OnSettingChanged(wxCommandEvent &aEvent) override
std::shared_ptr< CONNECT_JOB > m_connectJob
const std::vector< std::string > & columnsFor(const std::string &aTable)
bool validateLibraries()
Shows the first problem that stops the settings from being saved.
void OnRemoveField(wxCommandEvent &aEvent) override
std::string m_sampleTable
Rows of m_sampleTable shared by the Data page and the preview.
void OnDuplicateLibrary(wxCommandEvent &aEvent) override
void OnLibrarySelected(wxCommandEvent &aEvent) override
void OnFieldCellChanged(wxGridEvent &aEvent) override
void cancelConnect()
Abandons a connection attempt in progress, whose result is then discarded.
std::vector< DATABASE_SOURCE_TYPE > m_backendTypes
The type each entry of the backend choice stands for.
std::unique_ptr< DATABASE_LIB_SETTINGS > m_settings
void setConnectionStatus(const wxString &aText, bool aError, const wxString &aDetails=wxEmptyString)
Shows a one-line status; the full driver report goes in the tooltip.
void OnDataCellSelected(wxGridEvent &aEvent) override
void OnSettingSpinChanged(wxSpinEvent &aEvent) override
std::string settingsSnapshot()
Every setting as the file would hold it, plus where the password is kept.
void EndDialogShim(int aReturnCode)
A mode agnostic way to close a dialog.
The base frame for deriving all KiCad main window classes.
The base class for create windows for drawing purpose.
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
GAL_TYPE GetBackend() const
Return the type of backend currently used by GAL canvas.
@ GAL_TYPE_OPENGL
OpenGL implementation.
static GAL_TYPE ResolveStoredCanvasType(int aStoredCanvasType)
Convert a stored canvas type setting into a backend that can actually be used.
A warning icon drawn inside the right edge of a field, left of its dropdown button when it has one,...
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:61
static std::vector< std::string > ListDrivers()
static std::vector< DATA_SOURCE > ListDataSourceDetails()
static wxString SymbolNameForRow(const DATABASE_LIB_TABLE &aTable, bool aGloballyUniqueKeys, const DATABASE_CONNECTION::ROW &aRow, std::string *aRawKey=nullptr)
static std::vector< std::unique_ptr< LIB_SYMBOL > > BuildPreviewSymbols(SYMBOL_LIBRARY_ADAPTER *aLibs, const DATABASE_LIB_TABLE &aTable, bool aGloballyUniqueKeys, const std::vector< DATABASE_CONNECTION::ROW > &aRows, PREVIEW_ISSUES *aIssues)
Builds symbols from already-fetched rows exactly as the library would name and load them,...
static CONNECTION_RESULT CreateConnection(const DATABASE_LIB_SETTINGS &aSettings, bool aConnectNow=true, const wxString &aBaseDir=wxEmptyString)
Opens a connection described by aSettings, expanding ${CWD} in a connection string to the directory h...
RAII class that sets an value at construction and resets it to the original value at destruction.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:274
bool HandleUnsavedChanges(wxWindow *aParent, const wxString &aMessage, const std::function< bool()> &aSaveFunction)
Display a dialog with Save, Cancel and Discard Changes buttons.
Definition confirm.cpp:146
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
DB_FAILURE
Why a connection attempt failed, as far as the backend's report tells.
wxString DefaultDatabaseLibFileName(const wxString &aLibName)
std::unique_ptr< DATABASE_LIB_SETTINGS > LoadDatabaseLibSettingsReadOnly(const wxString &aPath, bool *aLoaded)
Loads a database library settings file without allowing the load itself (e.g.
std::string DatabasePathForStorage(const wxString &aPath, const wxString &aBaseDir)
wxString DatabasePathFromInput(const wxString &aInput, const wxString &aBaseDir)
bool StoreDatabasePassword(const DATABASE_SOURCE &aSource, const std::string &aPassword)
Stores the password for the source's DSN login in the system keyring, or removes the entry when the p...
bool GetDatabasePassword(const DATABASE_SOURCE &aSource, std::string &aPassword, const DATABASE_SECRET_LOOKUP &aLookup)
DSN passwords that are not saved in the (often shared) library file live in the system keyring,...
DATABASE_SOURCE_TYPE
int DatabaseFileWaitSeconds(int aTimeoutSeconds)
static constexpr int CONNECT_DEBOUNCE_MS
Pause in typing a database file path before the connection is tried.
static DB_AUTOFILL::ROLE autofillRole(const std::string &aChoiceId, const std::vector< wxString > &aTerms)
static wxString describeFailure(DB_FAILURE aFailure, const DATABASE_SOURCE &aSource)
static DATABASE_FIELD_MAPPING * findField(DATABASE_LIB_TABLE &aLib, const char *aName)
static constexpr int PROVIDER_POLL_MS
How often the Access provider check is asked whether it has finished, and for how long.
static bool isAutofilled(const MAP &aAutofilled, const typename MAP::key_type &aKey, const std::string &aValue)
True if autofill wrote aValue for aKey and the user has not changed it since.
static ACCESS_PROVIDER accessProvider()
Whether this computer can read Access files, which never blocks.
static constexpr int PREVIEW_DEBOUNCE_MS
Delay after the last edit before the preview is rebuilt.
static void relayoutPage(wxScrolledWindow *aPage)
Re-lays out a scrolled page and refits its scroll area after its contents change size.
static constexpr int PREVIEW_MIN_WIDTH_DIP
Narrowest the preview next to the settings can be before its tree columns are cut off.
static wxString displayName(const DATABASE_LIB_TABLE &aTable)
static constexpr int PROVIDER_POLL_LIMIT
static constexpr size_t PREVIEW_ROW_LIMIT
Rows read for the Data page and preview; enough to judge a mapping without loading a whole table.
static std::string fieldChoiceId(const char *aFieldName)
static void releaseConnection(std::unique_ptr< DATABASE_CONNECTION > aConn)
A connection closes with a bounded wait that a dead share can use up, so the UI thread hands it to th...
static const std::vector< AUTOFILL_FIELD > & autofillFields()
static wxString accessLabel(ACCESS_PROVIDER aProvider)
static bool empty(const wxTextEntryBase *aCtrl)
const int minSize
Push and Shove router track width and via size dialog.
#define _(s)
static const std::string DatabaseLibraryFileExtension
Some functions to handle hotkeys in KiCad.
std::string source
std::string message
std::vector< std::string > AssignColumns(const std::vector< std::string > &aColumns, const std::vector< ROLE > &aRoles)
Chooses a column for each role, best scores first, so no column serves two roles.
void RememberChoice(std::vector< std::string > &aHistory, const std::string &aColumn)
Moves aColumn to the front of a role's history, dropping repeats and the oldest entries.
std::string MatchTable(const std::vector< std::string > &aTables, const std::string &aLibraryName)
std::map< std::string, size_t > RepeatCounts(const std::vector< std::map< std::string, std::any > > &aRows)
Counts, per column, the rows whose value already appeared in an earlier row, with NULL counted as emp...
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:741
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
#define _HKI(x)
Definition page_info.cpp:40
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
T * GetAppSettings(const char *aFilename)
KIWAY Kiway(KFCTL_STANDALONE)
wxString From_UTF8(const char *cstring)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
A symbol field that autofill maps from a like-named column.
std::vector< wxString > terms
bool visible_in_chooser
Whether the column is shown by default in the chooser.
std::string column
Database column name.
std::string name
KiCad field name.
void SetName(const std::string &aName)
bool inherit_properties
Whether or not to inherit properties from symbol field.
bool visible_on_add
Whether to show the field when placing the symbol.
bool show_name
Whether or not to show the field name as well as its value.
A database library table will be mapped to a sub-library provided by the database library entry in th...
std::string key_col
Unique key column name (will form part of the LIB_ID)
std::string name
KiCad library nickname (will form part of the LIB_ID)
std::vector< DATABASE_FIELD_MAPPING > fields
std::string table
Database table to pull content from.
std::string type_name
Raw source.type as written, kept for the error when the type is INVALID.
std::string path
Database file of a SQLITE or ACCESS source.
std::string connection_string
DATABASE_SOURCE_TYPE type
One mapping to fill, such as the key column or the Value field.
std::vector< std::string > remembered
Columns the user chose before, most recent first.
std::vector< wxString > terms
Words to look for, in English and translated.
One column mapping on the Libraries page: its combo, its setting and its Data page label.
std::string &(* value)(DATABASE_LIB_TABLE &)
const char * autofillId
Key of the remembered column choices for this role.
std::atomic< bool > started
Set by the worker before it opens anything, so a job that never ran can be destroyed anywhere.
~CONNECT_JOB()
Lets the connection go on a pool thread, so a stalled close never blocks the UI thread while the app ...
std::unique_ptr< DATABASE_CONNECTION > conn
A data source configured in the ODBC driver manager.
A connection built from settings, or the reason none could be built.
std::unique_ptr< DATABASE_CONNECTION > connection
Null when no backend exists for the source type.
DB_FAILURE failure
Why connection is null.
std::string error
The message naming the problem when connection is null.
A problem found while previewing, tagged with the setting that caused it.
const std::string table
std::vector< DATABASE_CONNECTION::ROW > rows
netlist clear()
std::string path
VECTOR2I end
PCB_VIA::VIA_PARAMETER_ERROR::FIELD FIELD
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
wxString AddFileExtListToFilter(const std::vector< std::string > &aExts)
Build the wildcard extension file dialog wildcard filter to add to the base message dialog.
Definition of file extensions used in Kicad.