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 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 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
49 && current->m_Pinned;
50 };
51
52 auto unpinnedLibSelectedCondition =
53 [this](const SELECTION& aSel )
54 {
55 LIB_TREE* libTree = m_frame->GetLibTree();
56 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
57 return current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY
58 && !current->m_Pinned;
59 };
60
61 aMenu->AddItem( ACTIONS::pinLibrary, unpinnedLibSelectedCondition, 1 );
62 aMenu->AddItem( ACTIONS::unpinLibrary, pinnedLibSelectedCondition, 1 );
63 aMenu->AddSeparator( 1 );
64
65 aMenu->AddSeparator( 400 );
67}
68
69
71{
72 LIB_TREE* libTree = m_frame->GetLibTree();
73 LIB_ID target = m_frame->GetTargetLibId();
74
75 libTree->Regenerate( true );
76
77 if( target.IsValid() )
78 libTree->CenterLibId( target );
79}
80
81
83{
84 LIB_TREE* libTree = m_frame->GetLibTree();
85 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
86
87 if( current && !current->m_Pinned )
88 {
90 {
91 case KIWAY::FACE_SCH:
94 break;
95
96 case KIWAY::FACE_PCB:
99 break;
100
101 default:
102 wxFAIL_MSG( wxT( "Unsupported frame type for library pinning." ) );
103 break;
104 }
105
106 current->m_Pinned = true;
108 }
109
110 return 0;
111}
112
113
115{
116 LIB_TREE* libTree = m_frame->GetLibTree();
117 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
118
119 if( current && current->m_Pinned )
120 {
122 {
123 case KIWAY::FACE_SCH:
126 break;
127
128 case KIWAY::FACE_PCB:
131 break;
132
133 default:
134 wxFAIL_MSG( wxT( "Unsupported frame type for library pinning." ) );
135 break;
136 }
137
138 current->m_Pinned = false;
140 }
141
142 return 0;
143}
144
145
147{
149 return 0;
150}
151
152
154{
157
159 return 0;
160}
161
162
163class RENAME_DIALOG : public wxTextEntryDialog
164{
165public:
166 RENAME_DIALOG( wxWindow* aParent, const wxString& aTitle, const wxString& aName,
167 std::function<bool( const wxString& newName )> aValidator ) :
168 wxTextEntryDialog( aParent, _( "New name:" ), aTitle, aName ),
169 m_validator( std::move( aValidator ) )
170 { }
171
172protected:
174 {
175 return m_validator( m_textctrl->GetValue().Trim( true ).Trim( false ) );
176 }
177
178private:
179 std::function<bool( const wxString& aNewName )> m_validator;
180};
181
182
183bool LIBRARY_EDITOR_CONTROL::RenameLibrary( const wxString& aTitle, const wxString& aName,
184 std::function<bool( const wxString& aNewName )> aValidator )
185{
186 RENAME_DIALOG dlg( m_frame, aTitle, aName, std::move( aValidator ) );
187
188 return dlg.ShowModal() == wxID_OK;
189}
190
191
193{
199}
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: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)
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: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.