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 "wx/generic/textdlgg.h"
25
26
28 TOOL_INTERACTIVE( "common.LibraryEditorControl" ),
29 m_frame( nullptr )
30{
31}
32
33
35{
36 m_frame = getEditFrame<EDA_DRAW_FRAME>();
37}
38
39
41{
42 auto pinnedLibSelectedCondition =
43 [this]( const SELECTION& aSel )
44 {
45 LIB_TREE* libTree = m_frame->GetLibTree();
46 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
47 return current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && current->m_Pinned;
48 };
49 auto unpinnedLibSelectedCondition =
50 [this](const SELECTION& aSel )
51 {
52 LIB_TREE* libTree = m_frame->GetLibTree();
53 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
54 return current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY && !current->m_Pinned;
55 };
56
57 aMenu->AddItem( ACTIONS::pinLibrary, unpinnedLibSelectedCondition, 1 );
58 aMenu->AddItem( ACTIONS::unpinLibrary, pinnedLibSelectedCondition, 1 );
59 aMenu->AddSeparator( 1 );
60
61 aMenu->AddSeparator( 400 );
63}
64
65
67{
68 LIB_TREE* libTree = m_frame->GetLibTree();
69 LIB_ID target = m_frame->GetTargetLibId();
70
71 libTree->Regenerate( true );
72
73 if( target.IsValid() )
74 libTree->CenterLibId( target );
75}
76
77
79{
80 LIB_TREE* libTree = m_frame->GetLibTree();
81 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
82
83 if( current && !current->m_Pinned )
84 {
85 m_frame->Prj().PinLibrary( current->m_LibId.GetLibNickname(), false );
86 current->m_Pinned = true;
88 }
89
90 return 0;
91}
92
93
95{
96 LIB_TREE* libTree = m_frame->GetLibTree();
97 LIB_TREE_NODE* current = libTree ? libTree->GetCurrentTreeNode() : nullptr;
98
99 if( current && current->m_Pinned )
100 {
101 m_frame->Prj().UnpinLibrary( current->m_LibId.GetLibNickname(), false );
102 current->m_Pinned = false;
104 }
105
106 return 0;
107}
108
109
111{
113 return 0;
114}
115
116
118{
121
123 return 0;
124}
125
126
127class RENAME_DIALOG : public wxTextEntryDialog
128{
129public:
130 RENAME_DIALOG( wxWindow* aParent, const wxString& aTitle, const wxString& aName,
131 std::function<bool( const wxString& newName )> aValidator ) :
132 wxTextEntryDialog( aParent, _( "New name:" ), aTitle, aName ),
133 m_validator( std::move( aValidator ) )
134 { }
135
136protected:
138 {
139 return m_validator( m_textctrl->GetValue().Trim( true ).Trim( false ) );
140 }
141
142private:
143 std::function<bool( const wxString& aNewName )> m_validator;
144};
145
146
147bool LIBRARY_EDITOR_CONTROL::RenameLibrary( const wxString& aTitle, const wxString& aName,
148 std::function<bool( const wxString& aNewName )> aValidator )
149{
150 RENAME_DIALOG dlg( m_frame, aTitle, aName, std::move( aValidator ) );
151
152 return dlg.ShowModal() == wxID_OK;
153}
154
155
157{
163}
static TOOL_ACTION showLibraryTree
Definition: actions.h:143
static TOOL_ACTION pinLibrary
Definition: actions.h:141
static TOOL_ACTION libraryTreeSearch
Definition: actions.h:145
static TOOL_ACTION hideLibraryTree
Definition: actions.h:144
static TOOL_ACTION unpinLibrary
Definition: actions.h:142
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.
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.
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
void UnpinLibrary(const wxString &aLibrary, bool isSymbolLibrary)
Definition: project.cpp:192
void PinLibrary(const wxString &aLibrary, bool isSymbolLibrary)
Definition: project.cpp:171
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.