KiCad PCB EDA Suite
Loading...
Searching...
No Matches
design_block_tree_model_adapter.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <pgm_base.h>
21#include <kiface_base.h>
22#include <eda_base_frame.h>
23#include <core/kicad_algo.h>
26#include <wx/log.h>
27#include <wx/tokenzr.h>
29#include <string_utils.h>
30#include <eda_pattern_match.h>
31#include <design_block.h>
34
35wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>
38 TOOL_INTERACTIVE* aContextMenuTool )
39{
40 auto* adapter = new DESIGN_BLOCK_TREE_MODEL_ADAPTER( aParent, aLibs, aSettings, aContextMenuTool );
41 return wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>( adapter );
42}
43
44
47 TOOL_INTERACTIVE* aContextMenuTool ) :
48 LIB_TREE_MODEL_ADAPTER( aParent, wxT( "pinned_design_block_libs" ),
49 Kiface().KifaceSettings()->m_DesignBlockChooserPanel.tree ),
50 m_libs( aLibs ),
51 m_frame( aParent ),
52 m_contextMenuTool( aContextMenuTool )
53{
54}
55
56
58{
60 PROJECT_FILE& project = aParent->Prj().GetProjectFile();
62
63 for( const LIBRARY_TABLE_ROW* row : manager.Rows( LIBRARY_TABLE_TYPE::DESIGN_BLOCK ) )
64 {
65 if( row->Hidden() )
66 continue;
67
68 wxString libName = row->Nickname();
69
70 bool pinned = alg::contains( cfg->m_Session.pinned_design_block_libs, libName )
71 || alg::contains( project.m_PinnedDesignBlockLibs, libName );
72
73 DoAddLibrary( libName, row->Description(), getDesignBlocks( aParent, libName ), pinned, true );
74 }
75
76 m_tree.AssignIntrinsicRanks( m_shownColumns );
77}
78
79
84
85
87 const wxString& aLibName )
88{
89 std::vector<LIB_TREE_ITEM*> libList;
90
91 DESIGN_BLOCK_LIBRARY_ADAPTER* libs = m_frame->Prj().DesignBlockLibs();
92
93 std::vector<DESIGN_BLOCK*> blocks = libs->GetDesignBlocks( aLibName );
94
95 for( DESIGN_BLOCK* block : blocks )
96 {
97 LIB_ID id = block->GetLIB_ID();
98 id.SetLibNickname( aLibName );
99 block->SetLibId( id );
100 libList.emplace_back( block );
101 }
102
103 return libList;
104}
105
106
107wxString DESIGN_BLOCK_TREE_MODEL_ADAPTER::GenerateInfo( LIB_ID const& aLibId, int aUnit )
108{
109 static const wxString DescriptionFormat = wxT(
110 "<b>__NAME__</b>"
111 "__DESC__"
112 "__KEY__"
113 "<hr><table border=0>"
114 "__FIELDS__"
115 "</table>" );
116
117 static const wxString DescFormat = wxS( "<br>%s" );
118 static const wxString KeywordsFormat = wxS( "<br>" ) + _( "Keywords" ) + wxS( ": %s" );
119
120 static const wxString FieldFormat = wxT(
121 "<tr>"
122 " <td><b>__FIELD_NAME__</b></td>"
123 " <td>__FIELD_VALUE__</td>"
124 "</tr>" );
125
126
127 if( !aLibId.IsValid() )
128 return wxEmptyString;
129
130 const DESIGN_BLOCK* db = nullptr;
131
132 try
133 {
134 db = m_libs->GetEnumeratedDesignBlock( aLibId.GetLibNickname(), aLibId.GetLibItemName() );
135 }
136 catch( const IO_ERROR& ioe )
137 {
138 wxLogError( _( "Error loading design block %s from library '%s'." ) + wxS( "\n%s" ),
139 aLibId.GetLibItemName().wx_str(), aLibId.GetLibNickname().wx_str(), ioe.What() );
140
141 return wxEmptyString;
142 }
143
144 wxString html = DescriptionFormat;
145
146 if( db )
147 {
148 wxString name = aLibId.GetLibItemName();
149 wxString desc = db->GetLibDescription();
150 wxString keywords = db->GetKeywords();
151
152 html.Replace( "__NAME__", EscapeHTML( name ) );
153
154 wxString esc_desc = EscapeHTML( UnescapeString( desc ) );
155
156 // Add line breaks
157 esc_desc.Replace( wxS( "\n" ), wxS( "<br>" ) );
158
159 // Add links
160 esc_desc = LinkifyHTML( esc_desc );
161
162 if( esc_desc.IsEmpty() )
163 html.Replace( "__DESC__", wxEmptyString );
164 else
165 html.Replace( "__DESC__", wxString::Format( DescFormat, esc_desc ) );
166
167 if( keywords.IsEmpty() )
168 html.Replace( "__KEY__", wxEmptyString );
169 else
170 html.Replace( "__KEY__", wxString::Format( KeywordsFormat, EscapeHTML( keywords ) ) );
171
172 wxString fieldTable;
173
174 for( const auto& [key, value] : db->GetFields() )
175 {
176 wxString fieldRow = FieldFormat;
177 fieldRow.Replace( wxS( "__FIELD_NAME__" ), EscapeHTML( key ) );
178 fieldRow.Replace( wxS( "__FIELD_VALUE__" ), EscapeHTML( value ) );
179 fieldTable += fieldRow;
180 }
181
182 html.Replace( "__FIELDS__", fieldTable );
183
184 // Design blocks (unlike symbols and footprints) are not cached. We own the pointer.
185 delete db;
186 }
187 else
188 {
189 html.Printf( _( "Error loading design block %s from library '%s'." ) + wxS( "\n" ),
190 aLibId.GetLibItemName().wx_str(), aLibId.GetLibNickname().wx_str() );
191 }
192
193 return html;
194}
195
196
const char * name
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
std::vector< DESIGN_BLOCK * > GetDesignBlocks(const wxString &aNickname)
std::vector< LIB_TREE_ITEM * > getDesignBlocks(EDA_BASE_FRAME *aParent, const wxString &aLibName)
wxString GenerateInfo(LIB_ID const &aLibId, int aUnit) override
static wxObjectDataPtr< LIB_TREE_MODEL_ADAPTER > Create(EDA_BASE_FRAME *aParent, DESIGN_BLOCK_LIBRARY_ADAPTER *aLibs, APP_SETTINGS_BASE::LIB_TREE &aSettings, TOOL_INTERACTIVE *aContextMenuTool)
Factory function: create a model adapter in a reference-counting container.
DESIGN_BLOCK_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, DESIGN_BLOCK_LIBRARY_ADAPTER *aLibs, APP_SETTINGS_BASE::LIB_TREE &aSettings, TOOL_INTERACTIVE *aContextMenuTool)
Constructor; takes a set of libraries to be included in the search.
const wxString & GetKeywords() const
const wxString & GetLibDescription() const
const nlohmann::ordered_map< wxString, wxString > & GetFields() const
The base frame for deriving all KiCad main window classes.
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()
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Returns a flattened list of libraries of the given type.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:172
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:100
const UTF8 & GetLibItemName() const
Definition lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:87
LIB_TREE_MODEL_ADAPTER(EDA_BASE_FRAME *aParent, const wxString &aPinnedKey, APP_SETTINGS_BASE::LIB_TREE &aSettingsStruct)
Create the adapter.
std::vector< wxString > m_shownColumns
LIB_TREE_NODE_LIBRARY & DoAddLibrary(const wxString &aNodeName, const wxString &aDesc, const std::vector< LIB_TREE_ITEM * > &aItemList, bool pinned, bool presorted)
Add the given list of symbols by alias.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:537
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:130
The backing store for a PROJECT, in JSON format.
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:205
wxString wx_str() const
Definition utf8.cpp:45
#define _(s)
Base window classes and related definitions.
Abstract pattern-matching tool and implementations.
static const wxString KeywordsFormat
static const wxString DescriptionFormat
static const wxString DescFormat
static const wxString FieldFormat
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:100
PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:946
see class PGM_BASE
wxString EscapeHTML(const wxString &aString)
Return a new wxString escaped for embedding in HTML.
wxString UnescapeString(const wxString &aSource)
wxString LinkifyHTML(wxString aStr)
Wraps links in HTML tags.
std::vector< wxString > pinned_design_block_libs