KiCad PCB EDA Suite
Loading...
Searching...
No Matches
design_block_info.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
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/*
21 * Functions to read design block libraries and fill m_design_blocks by available design blocks
22 * names and their documentation (comments and keywords).
23 */
24
25#include <design_block_info.h>
26#include <fp_lib_table.h>
28#include <string_utils.h>
29#include <kiface_ids.h>
30#include <kiway.h>
31#include <lib_id.h>
32#include <thread>
33#include <utility>
34#include <wx/tokenzr.h>
35#include <kiface_base.h>
36
37DESIGN_BLOCK_INFO* DESIGN_BLOCK_LIST::GetDesignBlockInfo( const wxString& aLibNickname,
38 const wxString& aDesignBlockName )
39{
40 if( aDesignBlockName.IsEmpty() )
41 return nullptr;
42
43 for( std::unique_ptr<DESIGN_BLOCK_INFO>& db : m_list )
44 {
45 if( aLibNickname == db->GetLibNickname() && aDesignBlockName == db->GetDesignBlockName() )
46 return db.get();
47 }
48
49 return nullptr;
50}
51
52
53DESIGN_BLOCK_INFO* DESIGN_BLOCK_LIST::GetDesignBlockInfo( const wxString& aDesignBlockName )
54{
55 if( aDesignBlockName.IsEmpty() )
56 return nullptr;
57
58 LIB_ID dbid;
59
60 wxCHECK_MSG( dbid.Parse( aDesignBlockName ) < 0, nullptr,
61 wxString::Format( wxT( "'%s' is not a valid LIB_ID." ), aDesignBlockName ) );
62
63 return GetDesignBlockInfo( dbid.GetLibNickname(), dbid.GetLibItemName() );
64}
65
66
67std::vector<SEARCH_TERM> DESIGN_BLOCK_INFO::GetSearchTerms()
68{
69 std::vector<SEARCH_TERM> terms;
70
71 terms.emplace_back( SEARCH_TERM( GetLibNickname(), 4 ) );
72 terms.emplace_back( SEARCH_TERM( GetName(), 8 ) );
73 terms.emplace_back( SEARCH_TERM( GetLIB_ID().Format(), 16 ) );
74
75 wxStringTokenizer keywordTokenizer( GetKeywords(), " \t\r\n", wxTOKEN_STRTOK );
76
77 while( keywordTokenizer.HasMoreTokens() )
78 terms.emplace_back( SEARCH_TERM( keywordTokenizer.GetNextToken(), 4 ) );
79
80 // Also include keywords as one long string, just in case
81 terms.emplace_back( SEARCH_TERM( GetKeywords(), 1 ) );
82 terms.emplace_back( SEARCH_TERM( GetDesc(), 1 ) );
83
84 return terms;
85}
86
87
88bool DESIGN_BLOCK_INFO::InLibrary( const wxString& aLibrary ) const
89{
90 return aLibrary == m_nickname;
91}
92
93
94bool operator<( const DESIGN_BLOCK_INFO& lhs, const DESIGN_BLOCK_INFO& rhs )
95{
96 int retv = StrNumCmp( lhs.m_nickname, rhs.m_nickname, false );
97
98 if( retv != 0 )
99 return retv < 0;
100
101 // Technically design block names are not case sensitive because the file name is used
102 // as the design block name. On windows this would be problematic because windows does
103 // not support case sensitive file names by default. This should not cause any issues
104 // and allow for a future change to use the name defined in the design block file.
105 return StrNumCmp( lhs.m_dbname, rhs.m_dbname, false ) < 0;
106}
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:48
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:83
bool operator<(const DESIGN_BLOCK_INFO &lhs, const DESIGN_BLOCK_INFO &rhs)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition ptree.cpp:194
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
A structure for storing weighted search terms.