KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 (C) 2024 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 pinnedLibSelectedCondition =
44 [this]( const SELECTION& aSel )
45 {
46 LIB_TREE* libTree = m_frame->GetLibTree();
47 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
48 return current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && current->m_Pinned;
49 };
50 auto unpinnedLibSelectedCondition =
51 [this](const SELECTION& aSel )
52 {
53 LIB_TREE* libTree = m_frame->GetLibTree();
54 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
55 return current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && !current->m_Pinned;
56 };
57
58 aMenu->AddItem( ACTIONS::pinLibrary, unpinnedLibSelectedCondition, 1 );
59 aMenu->AddItem( ACTIONS::unpinLibrary, pinnedLibSelectedCondition, 1 );
60 aMenu->AddSeparator( 1 );
61
62 aMenu->AddSeparator( 400 );
64}
65
66
68{
69 LIB_TREE* libTree = m_frame->GetLibTree();
70 LIB_ID target = m_frame->GetTargetLibId();
71
72 libTree->Regenerate( true );
73
74 if( target.IsValid() )
75 libTree->CenterLibId( target );
76}
77
78
80{
81 LIB_TREE* libTree = m_frame->GetLibTree();
82 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
83
84 if( current && !current->m_Pinned )
85 {
87 {
88 case KIWAY::FACE_SCH:
91 break;
92 case KIWAY::FACE_PCB:
95 break;
96 default: wxFAIL_MSG( wxT( "Unsupported frame type for library pinning." ) ); break;
97 }
98
99 current->m_Pinned = true;
101 }
102
103 return 0;
104}
105
106
108{
109 LIB_TREE* libTree = m_frame->GetLibTree();
110 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
111
112 if( current && current->m_Pinned )
113 {
115 {
116 case KIWAY::FACE_SCH:
119 break;
120 case KIWAY::FACE_PCB:
123 break;
124 default: wxFAIL_MSG( wxT( "Unsupported frame type for library pinning." ) ); break;
125 }
126
127 current->m_Pinned = false;
129 }
130
131 return 0;
132}
133
134
136{
138 return 0;
139}
140
141
143{
146
148 return 0;
149}
150
151
152class RENAME_DIALOG : public wxTextEntryDialog
153{
154public:
155 RENAME_DIALOG( wxWindow* aParent, const wxString& aTitle, const wxString& aName,
156 std::function<bool( const wxString& newName )> aValidator ) :
157 wxTextEntryDialog( aParent, _( "New name:" ), aTitle, aName ),
158 m_validator( std::move( aValidator ) )
159 { }
160
161protected:
163 {
164 return m_validator( m_textctrl->GetValue().Trim( true ).Trim( false ) );
165 }
166
167private:
168 std::function<bool( const wxString& aNewName )> m_validator;
169};
170
171
172bool LIBRARY_EDITOR_CONTROL::RenameLibrary( const wxString& aTitle, const wxString& aName,
173 std::function<bool( const wxString& aNewName )> aValidator )
174{
175 RENAME_DIALOG dlg( m_frame, aTitle, aName, std::move( aValidator ) );
176
177 return dlg.ShowModal() == wxID_OK;
178}
179
180
182{
188}
static TOOL_ACTION showLibraryTree
Definition: actions.h:154
static TOOL_ACTION pinLibrary
Definition: actions.h:152
static TOOL_ACTION libraryTreeSearch
Definition: actions.h:156
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_SCH
eeschema DSO
Definition: kiway.h:291
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:292
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)
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
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
Model class in the component selector Model-View-Adapter (mediated MVC) architecture.
enum TYPE m_Type
Widget displaying a tree of symbols with optional search text control and description panel....
Definition: lib_tree.h:49
LIB_TREE_NODE * GetCurrentTreeNode() const
Definition: lib_tree.cpp:332
void CenterLibId(const LIB_ID &aLibId)
Ensure that an item is visible (preferably centered).
Definition: lib_tree.cpp:349
void Regenerate(bool aKeepState)
Regenerate the tree.
Definition: lib_tree.cpp:422
@ SYMBOL_LIB
Definition: project.h:190
@ FOOTPRINT_LIB
Definition: project.h:191
void PinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition: project.cpp:177
void UnpinLibrary(const wxString &aLibrary, enum LIB_TYPE_T aLibType)
Definition: project.cpp:214
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:167
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.