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 <map>
25#include <memory>
26#include <set>
27#include <string>
28#include <vector>
29
36
39
40
48{
49public:
56 DIALOG_DATABASE_LIB_SETTINGS( wxWindow* aParent, const wxString& aDblPath, SYMBOL_LIBRARY_ADAPTER* aLibs,
57 const wxString& aDefaultDir = wxEmptyString );
58
60
61 bool TransferDataToWindow() override;
62 bool TransferDataFromWindow() override;
63
65 const wxString& GetSettingsPath() const { return m_path; }
66
67protected:
68 void OnSettingChanged( wxCommandEvent& aEvent ) override;
69 void OnSettingSpinChanged( wxSpinEvent& aEvent ) override;
70 void OnSourceTypeChanged( wxCommandEvent& aEvent ) override;
71 void OnSourceChanged( wxCommandEvent& aEvent ) override;
72 void OnSourceSpinChanged( wxSpinEvent& aEvent ) override;
73 void OnInsertDriver( wxCommandEvent& aEvent ) override;
74 void OnTestConnection( wxCommandEvent& aEvent ) override;
75 void OnManageDataSources( wxCommandEvent& aEvent ) override;
76
77 void OnLibrarySelected( wxCommandEvent& aEvent ) override;
78 void OnAddLibrary( wxCommandEvent& aEvent ) override;
79 void OnDuplicateLibrary( wxCommandEvent& aEvent ) override;
80 void OnMoveLibraryUp( wxCommandEvent& aEvent ) override;
81 void OnMoveLibraryDown( wxCommandEvent& aEvent ) override;
82 void OnRemoveLibrary( wxCommandEvent& aEvent ) override;
83 void OnLibraryChanged( wxCommandEvent& aEvent ) override;
84 void OnTableChanged( wxCommandEvent& aEvent ) override;
85
86 void OnFieldCellChanged( wxGridEvent& aEvent ) override;
87 void OnAddField( wxCommandEvent& aEvent ) override;
88 void OnMoveFieldUp( wxCommandEvent& aEvent ) override;
89 void OnMoveFieldDown( wxCommandEvent& aEvent ) override;
90 void OnRemoveField( wxCommandEvent& aEvent ) override;
91 void OnAddUnmappedColumns( wxCommandEvent& aEvent ) override;
92
93 void OnDataLabelRightClick( wxGridEvent& aEvent ) override;
94 void OnDataCellSelected( wxGridEvent& aEvent ) override;
95
96 void onSplitterIdle( wxIdleEvent& aEvent );
97
98private:
101 {
102 wxComboBox* combo;
103 std::string& ( *value )( DATABASE_LIB_TABLE& );
105 wxString label;
106 const char* autofillId;
107 std::vector<wxString> terms;
108 };
109
110 void storeSource();
111
113 void loadDataSources();
114
116 void updateDsnDriver();
117
119
122 {
125 std::unique_ptr<DATABASE_CONNECTION> conn;
126 std::vector<std::string> tables;
127 bool connected = false;
128 };
129
131 void connect();
132
134 void cancelConnect();
135
136 void onConnectFinished( const std::shared_ptr<CONNECT_JOB>& aJob );
137
139 void refreshTables();
140
141 void loadLibraryList();
142 void loadLibrary();
143 void storeLibrary();
144 void moveLibrary( int aDelta );
145 void loadFields();
146 void swapFields( DATABASE_LIB_TABLE& aLib, int aRow, int aOther );
147 void loadDataGrid();
148
150 void updateSymbolExample();
151
153 void updateDataLabels();
154
155 void loadColumnChoices();
156
158 void markUserRole( const COLUMN_ROLE& aRole, const std::string& aValue );
159
161 bool ensureSample( const std::string& aTable );
162
164 void updateTableWarning();
165
169
171 wxArrayString previewIssues( SCH_IO_DATABASE::PREVIEW_ISSUE::FIELD aField ) const;
172
174 bool validateLibraries();
175
177 wxString cwdDir() const;
178
180 bool chooseSavePath();
181
183 void updateAutofillStyle();
184
187
189 std::string settingsSnapshot();
190
191 enum class CLOSE_ACTION
192 {
196 };
197
200
201 void onCancel( wxCommandEvent& aEvent );
202 void onCloseWindow( wxCloseEvent& aEvent );
203
205 void autofillTable();
206
208 void autofillColumns();
209
210 const std::vector<std::string>& columnsFor( const std::string& aTable );
211
213
215 const DATABASE_FIELD_MAPPING* fieldForColumn( const std::string& aColumn );
216
218 void markDirty();
219
221 void showInfo( const wxString& aMessage, int aIcon );
222 void updatePreview();
223
225 void setConnectionStatus( const wxString& aText, bool aError, const wxString& aDetails = wxEmptyString );
226
227 wxString m_path;
228 wxString m_defaultDir;
230 std::unique_ptr<DATABASE_LIB_SETTINGS> m_settings;
231 std::unique_ptr<DATABASE_CONNECTION> m_conn;
232 std::shared_ptr<CONNECT_JOB> m_connectJob;
234 std::vector<COLUMN_ROLE> m_roles;
235
238
241
244
247
250
253
255 wxString m_tableError;
256
259
261 std::vector<FIELD_WARNING_ICON*> m_roleIcons;
262
265
267 std::string m_autoTable;
268 std::map<wxComboBox*, std::string> m_autoRoles;
269 std::map<std::string, std::string> m_autoFields;
270
272 std::set<wxComboBox*> m_userRoles;
273
275 std::map<std::string, std::string> m_newChoices;
276
277 std::vector<DATABASE_CONNECTION::DATA_SOURCE> m_dataSources;
278 std::vector<std::string> m_tables;
279 std::map<std::string, std::vector<std::string>> m_columns;
280
282 std::string m_sampleTable;
283 std::vector<std::string> m_sampleColumns;
284 std::vector<DATABASE_CONNECTION::ROW> m_sampleRows;
285 std::map<std::string, size_t> m_sampleRepeats;
286
288 std::string m_cleanSnapshot;
289};
290
291#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 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 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.
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.
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.
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.
std::map< std::string, std::vector< std::string > > m_columns
void OnInsertDriver(wxCommandEvent &aEvent) override
void OnLibraryChanged(wxCommandEvent &aEvent) override
std::string m_cleanSnapshot
settingsSnapshot() when the dialog opened, to tell whether closing loses anything
void swapFields(DATABASE_LIB_TABLE &aLib, int aRow, int aOther)
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 updateColumnWarnings()
Marks each column field for a missing key, a column the table does not have, keys that repeat in the ...
std::vector< DATABASE_CONNECTION::DATA_SOURCE > m_dataSources
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::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.
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.
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