KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_database_lib_settings.h
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#ifndef KICAD_DIALOG_DATABASE_LIB_SETTINGS_H
22#define KICAD_DIALOG_DATABASE_LIB_SETTINGS_H
23
24#include <atomic>
25#include <map>
26#include <memory>
27#include <set>
28#include <string>
29#include <vector>
30
38
39#include <wx/timer.h>
40
43
44
52{
53public:
60 DIALOG_DATABASE_LIB_SETTINGS( wxWindow* aParent, const wxString& aDblPath, SYMBOL_LIBRARY_ADAPTER* aLibs,
61 const wxString& aDefaultDir = wxEmptyString );
62
64
65 bool TransferDataToWindow() override;
66 bool TransferDataFromWindow() override;
67
69 const wxString& GetSettingsPath() const { return m_path; }
70
71protected:
72 void OnSettingChanged( wxCommandEvent& aEvent ) override;
73 void OnSettingSpinChanged( wxSpinEvent& aEvent ) override;
74 void OnBackendChanged( wxCommandEvent& aEvent ) override;
75 void OnFileTextChanged( wxCommandEvent& aEvent ) override;
76 void OnBrowseFile( wxCommandEvent& aEvent ) override;
77 void OnSourceTypeChanged( wxCommandEvent& aEvent ) override;
78 void OnSourceChanged( wxCommandEvent& aEvent ) override;
79 void OnSourceSpinChanged( wxSpinEvent& aEvent ) override;
80 void OnInsertDriver( wxCommandEvent& aEvent ) override;
81 void OnTestConnection( wxCommandEvent& aEvent ) override;
82 void OnManageDataSources( wxCommandEvent& aEvent ) override;
83
84 void OnLibrarySelected( wxCommandEvent& aEvent ) override;
85 void OnAddLibrary( wxCommandEvent& aEvent ) override;
86 void OnDuplicateLibrary( wxCommandEvent& aEvent ) override;
87 void OnMoveLibraryUp( wxCommandEvent& aEvent ) override;
88 void OnMoveLibraryDown( wxCommandEvent& aEvent ) override;
89 void OnRemoveLibrary( wxCommandEvent& aEvent ) override;
90 void OnLibraryChanged( wxCommandEvent& aEvent ) override;
91 void OnTableChanged( wxCommandEvent& aEvent ) override;
92
93 void OnFieldCellChanged( wxGridEvent& aEvent ) override;
94 void OnAddField( wxCommandEvent& aEvent ) override;
95 void OnMoveFieldUp( wxCommandEvent& aEvent ) override;
96 void OnMoveFieldDown( wxCommandEvent& aEvent ) override;
97 void OnRemoveField( wxCommandEvent& aEvent ) override;
98 void OnAddUnmappedColumns( wxCommandEvent& aEvent ) override;
99
100 void OnDataLabelRightClick( wxGridEvent& aEvent ) override;
101 void OnDataCellSelected( wxGridEvent& aEvent ) override;
102
103 void onSplitterIdle( wxIdleEvent& aEvent );
104
105private:
108 {
109 wxComboBox* combo;
110 std::string& ( *value )( DATABASE_LIB_TABLE& );
112 wxString label;
113 const char* autofillId;
114 std::vector<wxString> terms;
115 };
116
117 void storeSource();
118
120 void loadBackends();
121
124
126
128 bool sourceConfigured() const;
129
131 void sourceEdited();
132
134 void onProviderTimer( wxTimerEvent& aEvent );
135
137 void loadDataSources();
138
140 void updateDsnDriver();
141
143
146 {
149 std::unique_ptr<DATABASE_CONNECTION> conn;
150 std::vector<std::string> tables;
151 bool connected = false;
152
154 std::atomic<bool> started = false;
155
157 ~CONNECT_JOB();
158 };
159
161 void connect();
162
164 void cancelConnect();
165
166 void onConnectFinished( const std::shared_ptr<CONNECT_JOB>& aJob );
167
169 void refreshTables();
170
171 void loadLibraryList();
172 void loadLibrary();
173 void storeLibrary();
174 void moveLibrary( int aDelta );
175 void loadFields();
176 void swapFields( DATABASE_LIB_TABLE& aLib, int aRow, int aOther );
177 void loadDataGrid();
178
180 void updateSymbolExample();
181
183 void updateDataLabels();
184
185 void loadColumnChoices();
186
188 void markUserRole( const COLUMN_ROLE& aRole, const std::string& aValue );
189
191 bool ensureSample( const std::string& aTable );
192
194 void updateTableWarning();
195
199
201 wxArrayString previewIssues( SCH_IO_DATABASE::PREVIEW_ISSUE::FIELD aField ) const;
202
204 bool validateLibraries();
205
207 wxString cwdDir() const;
208
210 bool chooseSavePath();
211
213 void updateAutofillStyle();
214
217
219 std::string settingsSnapshot();
220
221 enum class CLOSE_ACTION
222 {
226 };
227
230
231 void onCancel( wxCommandEvent& aEvent );
232 void onCloseWindow( wxCloseEvent& aEvent );
233
235 void autofillTable();
236
238 void autofillColumns();
239
240 const std::vector<std::string>& columnsFor( const std::string& aTable );
241
243
245 const DATABASE_FIELD_MAPPING* fieldForColumn( const std::string& aColumn );
246
248 void markDirty();
249
251 void showInfo( const wxString& aMessage, int aIcon );
252 void updatePreview();
253
255 void setConnectionStatus( const wxString& aText, bool aError, const wxString& aDetails = wxEmptyString );
256
257 wxString m_path;
258 wxString m_defaultDir;
260 std::unique_ptr<DATABASE_LIB_SETTINGS> m_settings;
261 std::unique_ptr<DATABASE_CONNECTION> m_conn;
262 std::shared_ptr<CONNECT_JOB> m_connectJob;
264 std::vector<COLUMN_ROLE> m_roles;
265
268
271
274
277
280
283
286
288 wxString m_tableError;
289
292
294 std::vector<FIELD_WARNING_ICON*> m_roleIcons;
295
298
300 std::vector<DATABASE_SOURCE_TYPE> m_backendTypes;
301
303 wxString m_dbFile;
304
307
308 bool m_pathEdited = false;
309
312
314 std::string m_autoTable;
315 std::map<wxComboBox*, std::string> m_autoRoles;
316 std::map<std::string, std::string> m_autoFields;
317
319 std::set<wxComboBox*> m_userRoles;
320
322 std::map<std::string, std::string> m_newChoices;
323
324 std::vector<ODBC_BACKEND::DATA_SOURCE> m_dataSources;
325 std::vector<std::string> m_tables;
326 std::map<std::string, std::vector<std::string>> m_columns;
327
329 std::string m_sampleTable;
330 std::vector<std::string> m_sampleColumns;
331 std::vector<DATABASE_CONNECTION::ROW> m_sampleRows;
332 std::map<std::string, size_t> m_sampleRepeats;
333
335 std::string m_cleanSnapshot;
336};
337
338#endif //KICAD_DIALOG_DATABASE_LIB_SETTINGS_H
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.
A warning icon drawn inside the right edge of a field, left of its dropdown button when it has one,...
std::vector< PREVIEW_ISSUE > PREVIEW_ISSUES
An interface to the global shared library manager that is schematic-specific and linked to one projec...
Debounces an action: runs it only after a quiet period following the last trigger.
DATABASE_SOURCE_TYPE
A database library table will be mapped to a sub-library provided by the database library entry in th...
One column mapping on the Libraries page: its combo, its setting and its Data page label.
const char * autofillId
Key of the remembered column choices for this role.
std::vector< wxString > terms
English names to autofill from.
Result of a connection attempt made on a worker thread.
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 ...
DIALOG_DATABASE_LIB_SETTINGS * owner
Cleared on the UI thread when the attempt is abandoned; the worker never reads it.
std::unique_ptr< DATABASE_CONNECTION > conn