30#include <wx/choicdlg.h>
32#include <wx/textdlg.h>
42#include <nlohmann/json.hpp>
47 wxString msg = wxString::Format(
_(
"Design block '%s' already exists in library '%s'." ),
48 newName.GetData(), libname.GetData() );
50 if(
OKOrCancelDialog( aFrame,
_(
"Confirmation" ), msg,
_(
"Overwrite existing design block?" ),
64 if(
Prj().IsNullProject() )
70 wxMessageDialog dlg(
this,
_(
"Add the library to the global library table?" ),
71 _(
"Add To Global Library Table" ), wxYES_NO );
73 if( dlg.ShowModal() != wxID_OK )
80 wxArrayString libTableNames;
81 libTableNames.Add(
_(
"Global" ) );
82 libTableNames.Add(
_(
"Project" ) );
84 wxSingleChoiceDialog dlg(
this,
_(
"Choose the Library Table to add the library to:" ),
85 _(
"Add To Library Table" ), libTableNames );
89 dlg.FindWindow( wxID_CANCEL )->SetLabel(
_(
"Skip" ) );
90 dlg.FindWindow( wxID_OK )->SetLabel(
_(
"Add" ) );
93 if( dlg.ShowModal() != wxID_OK )
96 switch( dlg.GetSelection() )
100 default:
return nullptr;
106 const wxString& aProposedName )
113 const wxString& aProposedName,
116 if( aTable ==
nullptr )
117 return wxEmptyString;
122 wxString initialPath = aProposedName;
124 if( initialPath.IsEmpty() )
127 if( aLibName.IsEmpty() )
135 return wxEmptyString;
144 if( !fn.IsAbsolute() )
146 fn.SetName( aLibName );
147 fn.MakeAbsolute( initialPath );
153 wxString libPath = fn.GetFullPath();
159 bool writable =
false;
164 writable = pi->IsLibraryWritable( libPath );
165 exists = fn.Exists();
176 wxString msg = wxString::Format(
_(
"Library %s is read only." ), libPath );
178 return wxEmptyString;
182 wxString msg = wxString::Format(
_(
"Library %s already exists." ), libPath );
183 KIDIALOG dlg(
this, msg,
_(
"Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
184 dlg.SetOKLabel(
_(
"Overwrite" ) );
188 return wxEmptyString;
190 pi->DeleteLibrary( libPath );
194 pi->CreateLibrary( libPath );
199 return wxEmptyString;
212 if( aTable ==
nullptr )
215 if( aTable ==
nullptr )
216 return wxEmptyString;
220 wxFileName fn( aFilename );
222 if( aFilename.IsEmpty() )
232 wxString libPath = fn.GetFullPath();
233 wxString libName = fn.GetName();
235 if( libName.IsEmpty() )
239 wxString description = wxGetTextFromUser(
_(
"Enter a description for the library:" ),
240 _(
"Library Description" ), wxEmptyString,
this );
254 libName = fn.GetFullName();
262 libName, normalizedPath, type, wxEmptyString, description );
277 LIB_ID libID( libName, wxEmptyString );
291 DisplayError(
this,
_(
"Please select a library to save the design block to." ) );
296 std::vector<SCH_ITEM*> sheets;
299 if( !sheets.empty() )
301 DisplayError(
this,
_(
"Design blocks with nested sheets are not supported." ) );
306 wxFileName fn = wxFileNameFromPath( aSheetPath.
Last()->
GetName() );
311 std::vector<SCH_FIELD>& shFields = aSheetPath.
Last()->
GetFields();
312 nlohmann::ordered_map<wxString, wxString> dbFields;
314 for(
int i = 0; i < (int) shFields.size(); i++ )
319 dbFields[shFields[i].GetCanonicalName()] = shFields[i].GetText();
330 wxString tempFile = wxFileName::CreateTempFileName(
"design_block" );
333 DisplayError(
this,
_(
"Error saving temporary schematic file to create design block." ) );
334 wxRemoveFile( tempFile );
345 if(
Prj().DesignBlockLibs()->DesignBlockExists( libName, newName ) )
357 wxRemoveFile( tempFile );
369 DisplayError(
this,
_(
"Please select a library to save the design block to." ) );
376 if( selection.
Empty() )
378 DisplayError(
this,
_(
"Please select some items to save as a design block." ) );
385 if( selection.
Size() == 1 )
394 DisplayError(
this,
_(
"Design blocks with nested sheets are not supported." ) );
400 wxFileName fn = wxFileNameFromPath(
GetScreen()->GetFileName() );
424 wxString tempFile = wxFileName::CreateTempFileName(
"design_block" );
427 DisplayError(
this,
_(
"Error saving temporary schematic file to create design block." ) );
428 wxRemoveFile( tempFile );
439 if(
Prj().DesignBlockLibs()->DesignBlockExists( libName, newName ) )
451 wxRemoveFile( tempFile );
462 if( aLibName.IsEmpty() )
464 DisplayError(
this,
_(
"Please select a library to delete." ) );
468 if( !
Prj().DesignBlockLibs()->IsDesignBlockLibWritable( aLibName ) )
470 wxString msg = wxString::Format(
_(
"Library '%s' is read only." ), aLibName );
476 wxString msg = wxString::Format(
_(
"Delete design block library '%s' from disk? This will "
477 "delete all design blocks within the library." ),
478 aLibName.GetData() );
480 if( aConfirm && !
IsOK(
this, msg ) )
493 msg.Printf(
_(
"Design block library '%s' deleted" ), aLibName.GetData() );
494 SetStatusText( msg );
510 if( !
Prj().DesignBlockLibs()->IsDesignBlockLibWritable( libname ) )
512 wxString msg = wxString::Format(
_(
"Library '%s' is read only." ), libname );
518 wxString msg = wxString::Format(
_(
"Delete design block '%s' in library '%s' from disk?" ),
519 dbname.GetData(), libname.GetData() );
521 if( aConfirm && !
IsOK(
this, msg ) )
534 msg.Printf(
_(
"Design block '%s' deleted from library '%s'" ), dbname.GetData(),
537 SetStatusText( msg );
553 if( !
Prj().DesignBlockLibs()->IsDesignBlockLibWritable( libname ) )
555 wxString msg = wxString::Format(
_(
"Library '%s' is read only." ), libname );
575 if( originalName != newName )
577 if(
Prj().DesignBlockLibs()->DesignBlockExists( libname, newName ) )
601 wxWindow* aParent,
bool aShowErrorMsg )
603 wxCHECK_MSG( aLibTable,
nullptr, wxS(
"Invalid design block library table." ) );
615 wxString msg = wxString::Format(
_(
"Error loading designblock %s from library '%s'." ),
@ KICAD_SEXP
S-expression KiCad file format.
static const wxString ShowType(DESIGN_BLOCK_FILE_T aFileType)
static DESIGN_BLOCK_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
static DESIGN_BLOCK_IO * FindPlugin(DESIGN_BLOCK_FILE_T aFileType)
Hold a record identifying a library accessed by the appropriate design block library #PLUGIN object i...
void DesignBlockLibDelete(const wxString &aNickname)
static wxString GetGlobalTableFileName()
void DesignBlockDelete(const wxString &aNickname, const wxString &aDesignBlockName)
Delete the aDesignBlockName from the library given by aNickname.
SAVE_T DesignBlockSave(const wxString &aNickname, const DESIGN_BLOCK *aDesignBlock, bool aOverwrite=true)
Write aDesignBlock to an existing library given by aNickname.
DESIGN_BLOCK * DesignBlockLoadWithOptionalNickname(const LIB_ID &aDesignBlockId, bool aKeepUUID=false)
Load a design block having aDesignBlockId with possibly an empty nickname.
static DESIGN_BLOCK_LIB_TABLE & GetGlobalLibTable()
void SelectLibId(const LIB_ID &aLibId)
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
void SetSchematicFile(const wxString &aFile)
void SetLibId(const LIB_ID &aName)
void SetFields(nlohmann::ordered_map< wxString, wxString > &aFields)
const LIB_ID & GetLibId() const
void ShowInfoBarError(const wxString &aErrorMsg, bool aShowCloseButton=false, WX_INFOBAR::MESSAGE_TYPE aType=WX_INFOBAR::MESSAGE_TYPE::GENERIC)
Show the WX_INFOBAR displayed on the top of the canvas with a message and an error icon on the left o...
bool LibraryFileBrowser(bool doOpen, wxFileName &aFilename, const wxString &wildcard, const wxString &ext, bool isDirectory=false, bool aIsGlobal=false, const wxString &aGlobalPath=wxEmptyString)
A base class for most all the KiCad significant classes used in schematics and boards.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
A logical library item identifier and consists of various portions much like a URI.
bool IsValid() const
Check if this LID_ID is valid.
const UTF8 & GetLibItemName() const
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
static wxString GetDefaultUserDesignBlocksPath()
Gets the default path we point users to create projects.
virtual const wxString GetProjectPath() const
Return the full path of the project.
virtual DESIGN_BLOCK_LIB_TABLE * DesignBlockLibs()
Return the table of design block libraries.
Schematic editor (Eeschema) main window.
void SaveSelectionAsDesignBlock(const wxString &aLibraryName)
bool EditDesignBlockProperties(const LIB_ID &aLibId)
wxString createNewDesignBlockLibrary(const wxString &aLibName, const wxString &aProposedName, DESIGN_BLOCK_LIB_TABLE *aTable)
Create a new library in the given table (presumed to be either the global or project library table).
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
void SaveSheetAsDesignBlock(const wxString &aLibraryName, SCH_SHEET_PATH &aSheetPath)
bool DeleteDesignBlockFromLibrary(const LIB_ID &aLibId, bool aConfirm)
SCHEMATIC * m_schematic
The currently loaded schematic.
SCH_SHEET_PATH & GetCurrentSheet() const
bool saveSchematicFile(SCH_SHEET *aSheet, const wxString &aSavePath)
Save aSheet to a schematic file.
DESIGN_BLOCK * GetDesignBlock(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load design block from design block library table.
DESIGN_BLOCK_LIB_TABLE * selectDesignBlockLibTable(bool aOptional=false)
Prompts a user to select global or project library tables.
bool AddDesignBlockLibrary(const wxString &aFilename, DESIGN_BLOCK_LIB_TABLE *aTable)
Add an existing library to either the global or project library table.
wxString CreateNewDesignBlockLibrary(const wxString &aLibName=wxEmptyString, const wxString &aProposedName=wxEmptyString)
If a library name is given, creates a new design block library in the project folder with the given n...
DESIGN_BLOCK_PANE * m_designBlocksPane
void UpdateDesignBlockOptions()
Design block panel options have changed and the panel needs to be refreshed.
bool DeleteDesignBlockLibrary(const wxString &aLibName, bool aConfirm)
Base class for any item which can be embedded within the SCHEMATIC container class,...
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
void GetSheets(std::vector< SCH_ITEM * > *aItems) const
Similar to Items().OfType( SCH_SHEET_T ), but return the sheets in a deterministic order (L-R,...
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_SCREEN * LastScreen()
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
std::vector< SCH_FIELD > & GetFields()
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
bool HasType(KICAD_T aType) const
Checks if there is at least one item of requested kind.
int Size() const
Returns the number of selected parts.
bool Empty() const
Checks if there is anything selected.
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
int OKOrCancelDialog(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aDetailedMessage, const wxString &aOKLabel, const wxString &aCancelLabel, bool *aApplyToAll)
Display a warning dialog with aMessage and returns the user response.
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
DESIGN_BLOCK * SchGetDesignBlock(const LIB_ID &aLibId, DESIGN_BLOCK_LIB_TABLE *aLibTable, wxWindow *aParent, bool aShowErrorMsg)
bool checkOverwrite(SCH_EDIT_FRAME *aFrame, wxString &libname, wxString &newName)
wxString NormalizePath(const wxFileName &aFilePath, const ENV_VAR_MAP *aEnvVars, const wxString &aProjectPath)
Normalize a file path to an environmental variable, if possible.
static const std::string KiCadDesignBlockLibPathExtension
static wxString KiCadDesignBlockLibPathWildcard()
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
This file is part of the common library.
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition of file extensions used in Kicad.