KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
library_editor_control.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 under the terms of
7 * the GNU General Public License as published by the Free Software Foundation; either version 3
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program;
15 * if not, you may find one at http://www.gnu.org/licenses/
16 */
17
18#include <tool/tool_manager.h>
19#include <tool/actions.h>
20#include <eda_draw_frame.h>
21#include <widgets/lib_tree.h>
22#include <project.h>
23#include <kiway.h>
24#include "wx/generic/textdlgg.h"
26
27
29 TOOL_INTERACTIVE( "common.LibraryEditorControl" ),
30 m_frame( nullptr )
31{
32}
33
34
36{
37 m_frame = getEditFrame<EDA_DRAW_FRAME>();
38}
39
40
42{
43 auto checkPinnedStatus = [this]( bool aPin )
44 {
45 bool result = true;
46 LIB_TREE* libTree = m_frame->GetLibTree();
47 if( libTree )
48 {
49 std::vector<LIB_TREE_NODE*> selection;
50 libTree->GetSelectedTreeNodes( selection );
51
52 for( const LIB_TREE_NODE* lib : selection )
53 {
54 if( lib && lib->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && lib->m_Pinned != aPin )
55 {
56 result = false;
57 break;
58 }
59 }
60 }
61 else
62 {
63 result = false;
64 }
65
66 return result;
67 };
68
69 auto pinnedLibSelectedCondition = [checkPinnedStatus]( const SELECTION& aSel )
70 {
71 return checkPinnedStatus( true );
72 };
73
74 auto unpinnedLibSelectedCondition = [checkPinnedStatus]( const SELECTION& aSel )
75 {
76 return checkPinnedStatus( false );
77 };
78
79 aMenu->AddItem( ACTIONS::pinLibrary, unpinnedLibSelectedCondition, 1 );
80 aMenu->AddItem( ACTIONS::unpinLibrary, pinnedLibSelectedCondition, 1 );
81 aMenu->AddSeparator( 1 );
82
83 aMenu->AddSeparator( 400 );
85}
86
87
89{
90 LIB_TREE* libTree = m_frame->GetLibTree();
91 LIB_ID target = m_frame->GetTargetLibId();
92
93 libTree->Regenerate( true );
94
95 if( target.IsValid() )
96 libTree->CenterLibId( target );
97}
98
100{
101 LIB_TREE* libTree = m_frame->GetLibTree();
102 if( libTree )
103 {
104 std::vector<LIB_TREE_NODE*> selection;
105 libTree->GetSelectedTreeNodes( selection );
106
107 for( LIB_TREE_NODE* lib : selection )
108 {
109 if( lib && lib->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && lib->m_Pinned != aPin )
110 {
111 const KIWAY::FACE_T kifaceType = KIWAY::KifaceType( m_frame->GetFrameType() );
112
113 if( kifaceType == KIWAY::FACE_SCH || kifaceType == KIWAY::FACE_PCB )
114 {
115 if( aPin )
116 m_frame->Prj().PinLibrary( lib->m_LibId.GetLibNickname(),
117 kifaceType == KIWAY::FACE_SCH
120 else
121 m_frame->Prj().UnpinLibrary( lib->m_LibId.GetLibNickname(),
122 kifaceType == KIWAY::FACE_SCH
125
126 lib->m_Pinned = aPin;
127 }
128 else
129 {
130 wxFAIL_MSG( wxT( "Unsupported frame type for library pinning." ) );
131 }
132 }
133 }
134
136 }
137}
138
140{
142
143 return 0;
144}
145
146
148{
150
151 return 0;
152}
153
154
156{
158 return 0;
159}
160
161
163{
166
168 return 0;
169}
170
171
172class RENAME_DIALOG : public wxTextEntryDialog
173{
174public:
175 RENAME_DIALOG( wxWindow* aParent, const wxString& aTitle, const wxString& aName,
176 std::function<bool( const wxString& newName )> aValidator ) :
177 wxTextEntryDialog( aParent, _( "New name:" ), aTitle, aName ),
178 m_validator( std::move( aValidator ) )
179 { }
180
181protected:
183 {
184 return m_validator( m_textctrl->GetValue().Trim( true ).Trim( false ) );
185 }
186
187private:
188 std::function<bool( const wxString& aNewName )> m_validator;
189};
190
191
192bool LIBRARY_EDITOR_CONTROL::RenameLibrary( const wxString& aTitle, const wxString& aName,
193 std::function<bool( const wxString& aNewName )> aValidator )
194{
195 RENAME_DIALOG dlg( m_frame, aTitle, aName, std::move( aValidator ) );
196
197 return dlg.ShowModal() == wxID_OK;
198}
199
200
202{
208}
static TOOL_ACTION showLibraryTree
Definition: actions.h:154
static TOOL_ACTION pinLibrary
Definition: actions.h:152
static TOOL_ACTION libraryTreeSearch
Definition: actions.h:160
static TOOL_ACTION hideLibraryTree
Definition: actions.h:155
static TOOL_ACTION unpinLibrary
Definition: actions.h:153
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Add a menu entry to run a TOOL_ACTION on selected items.
void AddSeparator(int aOrder=ANY_ORDER)
Add a separator to the menu.
FRAME_T GetFrameType() const
virtual void FocusLibraryTreeInput()
virtual void ToggleLibraryTree()
virtual bool IsLibraryTreeShown() const
virtual LIB_TREE * GetLibTree() const
virtual LIB_ID GetTargetLibId() const
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
static FACE_T KifaceType(FRAME_T aFrameType)
A simple mapping function which returns the FACE_T which is known to implement aFrameType.
Definition: kiway.cpp:344
FACE_T
Known KIFACE implementations.
Definition: kiway.h:291
@ FACE_SCH
eeschema DSO
Definition: kiway.h:292
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:293
int UnpinLibrary(const TOOL_EVENT &aEvent)
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
int ToggleLibraryTree(const TOOL_EVENT &aEvent)
bool RenameLibrary(const wxString &aTitle, const wxString &aName, std::function< bool(const wxString &aNewName)> aValidator)
void changeSelectedPinStatus(const bool aPin)
int PinLibrary(const TOOL_EVENT &aEvent)
int LibraryTreeSearch(const TOOL_EVENT &aEvent)
void setTransitions() override
Set up handlers for various events.
void AddContextMenuItems(CONDITIONAL_MENU *aMenu)
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
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
Widget displaying a tree of symbols with optional search text control and description panel.
Definition: lib_tree.h:49
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:368
int GetSelectedTreeNodes(std::vector< LIB_TREE_NODE * > &aSelection) const
Retrieve a list of pointers to selected tree nodes for trees that allow multi-selection.
Definition: lib_tree.cpp:348
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:441
@ SYMBOL_LIB
Definition: project.h:193
@ FOOTPRINT_LIB
Definition: project.h:194
void PinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition: project.cpp:188
void UnpinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition: project.cpp:225
std::function< bool(const wxString &aNewName)> m_validator
bool TransferDataFromWindow() override
RENAME_DIALOG(wxWindow *aParent, const wxString &aTitle, const wxString &aName, std::function< bool(const wxString &newName)> aValidator)
static bool ShowAlways(const SELECTION &aSelection)
The default condition function (always returns true).
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:168
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
#define _(s)
STL namespace.