2017-04-10 14:22:41 +00:00
|
|
|
/* -*- c++ -*-
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
|
|
|
|
* Copyright (C) 2017 Chris Pavlina <pavlina.chris@gmail.com>
|
|
|
|
* Copyright (C) 2014-2017 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "component_tree.h"
|
|
|
|
#include <generate_alias_info.h>
|
|
|
|
#include <wxdataviewctrl_helpers.h>
|
|
|
|
|
|
|
|
#include <wx/artprov.h>
|
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/statbmp.h>
|
|
|
|
#include <wx/html/htmlwin.h>
|
|
|
|
|
2017-09-15 14:17:44 +00:00
|
|
|
#include <symbol_lib_table.h>
|
|
|
|
|
|
|
|
|
|
|
|
COMPONENT_TREE::COMPONENT_TREE( wxWindow* aParent, SYMBOL_LIB_TABLE* aSymLibTable,
|
2017-10-28 18:28:14 +00:00
|
|
|
CMP_TREE_MODEL_ADAPTER_BASE::PTR& aAdapter, WIDGETS aWidgets )
|
2017-09-15 14:17:44 +00:00
|
|
|
: wxPanel( aParent ),
|
|
|
|
m_sym_lib_table( aSymLibTable ),
|
|
|
|
m_adapter( aAdapter ),
|
|
|
|
m_query_ctrl( nullptr ),
|
2017-11-21 13:54:30 +00:00
|
|
|
m_details_ctrl( nullptr ),
|
|
|
|
m_filtering( false )
|
2017-04-10 14:22:41 +00:00
|
|
|
{
|
2017-10-28 16:59:49 +00:00
|
|
|
// create space for context menu pointers, INVALID is the max value
|
|
|
|
m_menus.resize( CMP_TREE_NODE::TYPE::INVALID + 1 );
|
|
|
|
|
2017-04-10 14:22:41 +00:00
|
|
|
auto sizer = new wxBoxSizer( wxVERTICAL );
|
|
|
|
|
2017-04-10 14:43:25 +00:00
|
|
|
// Search text control
|
|
|
|
if( aWidgets & SEARCH )
|
|
|
|
{
|
|
|
|
auto search_sizer = new wxBoxSizer( wxHORIZONTAL );
|
2017-04-10 14:22:41 +00:00
|
|
|
|
2017-09-15 14:17:44 +00:00
|
|
|
m_query_ctrl = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
|
|
|
|
wxDefaultSize, wxTE_PROCESS_ENTER );
|
2017-04-10 14:22:41 +00:00
|
|
|
|
|
|
|
// Additional visual cue for GTK, which hides the placeholder text on focus
|
|
|
|
#ifdef __WXGTK__
|
2017-04-10 14:43:25 +00:00
|
|
|
search_sizer->Add( new wxStaticBitmap( this, wxID_ANY,
|
|
|
|
wxArtProvider::GetBitmap( wxART_FIND, wxART_FRAME_ICON ) ),
|
|
|
|
0, wxALIGN_CENTER | wxALL, 5 );
|
2017-04-10 14:22:41 +00:00
|
|
|
#endif
|
|
|
|
|
2017-07-08 12:22:20 +00:00
|
|
|
search_sizer->Add( m_query_ctrl, 1, wxALL | wxEXPAND, 5 );
|
2017-04-10 14:43:25 +00:00
|
|
|
sizer->Add( search_sizer, 0, wxEXPAND, 5 );
|
|
|
|
|
|
|
|
m_query_ctrl->Bind( wxEVT_TEXT, &COMPONENT_TREE::onQueryText, this );
|
|
|
|
m_query_ctrl->Bind( wxEVT_TEXT_ENTER, &COMPONENT_TREE::onQueryEnter, this );
|
|
|
|
m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &COMPONENT_TREE::onQueryCharHook, this );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Component tree
|
|
|
|
m_tree_ctrl =
|
|
|
|
new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxDV_SINGLE );
|
|
|
|
m_adapter->AttachTo( m_tree_ctrl );
|
2017-04-10 14:22:41 +00:00
|
|
|
|
|
|
|
sizer->Add( m_tree_ctrl, 1, wxALL | wxEXPAND, 5 );
|
2017-04-10 14:43:25 +00:00
|
|
|
|
|
|
|
// Description panel
|
|
|
|
if( aWidgets & DETAILS )
|
|
|
|
{
|
|
|
|
m_details_ctrl = new wxHtmlWindow( this, wxID_ANY, wxDefaultPosition, wxSize( 320, 240 ),
|
|
|
|
wxHW_SCROLLBAR_AUTO | wxSUNKEN_BORDER );
|
|
|
|
|
|
|
|
sizer->Add( m_details_ctrl, 1, wxALL | wxEXPAND, 5 );
|
|
|
|
m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &COMPONENT_TREE::onDetailsLink, this );
|
|
|
|
}
|
2017-04-10 14:22:41 +00:00
|
|
|
|
|
|
|
SetSizer( sizer );
|
|
|
|
|
|
|
|
m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &COMPONENT_TREE::onTreeActivate, this );
|
|
|
|
m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &COMPONENT_TREE::onTreeSelect, this );
|
2017-10-28 16:59:49 +00:00
|
|
|
m_tree_ctrl->Bind( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, &COMPONENT_TREE::onContextMenu, this );
|
2017-04-10 14:22:41 +00:00
|
|
|
|
2017-06-25 21:13:39 +00:00
|
|
|
Bind( COMPONENT_PRESELECTED, &COMPONENT_TREE::onPreselect, this );
|
|
|
|
|
2017-08-19 06:39:55 +00:00
|
|
|
// If wxTextCtrl::SetHint() is called before binding wxEVT_TEXT, the event
|
|
|
|
// handler will intermittently fire.
|
|
|
|
if( m_query_ctrl )
|
|
|
|
{
|
|
|
|
m_query_ctrl->SetHint( _( "Search" ) );
|
|
|
|
m_query_ctrl->SetFocus();
|
|
|
|
m_query_ctrl->SetValue( wxEmptyString );
|
|
|
|
}
|
|
|
|
|
|
|
|
// There may be a part preselected in the model. Make sure it is displayed.
|
|
|
|
postPreselectEvent();
|
|
|
|
|
2017-04-10 14:22:41 +00:00
|
|
|
Layout();
|
|
|
|
sizer->Fit( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-15 14:17:44 +00:00
|
|
|
LIB_ID COMPONENT_TREE::GetSelectedLibId( int* aUnit ) const
|
2017-04-10 14:22:41 +00:00
|
|
|
{
|
|
|
|
auto sel = m_tree_ctrl->GetSelection();
|
|
|
|
|
2017-09-15 14:17:44 +00:00
|
|
|
if( !sel )
|
|
|
|
{
|
|
|
|
LIB_ID emptyId;
|
|
|
|
|
|
|
|
return emptyId;
|
|
|
|
}
|
|
|
|
|
2017-04-10 14:22:41 +00:00
|
|
|
if( aUnit )
|
|
|
|
*aUnit = m_adapter->GetUnitFor( sel );
|
|
|
|
|
|
|
|
return m_adapter->GetAliasFor( sel );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-14 11:03:19 +00:00
|
|
|
void COMPONENT_TREE::SelectLibId( const LIB_ID& aLibId )
|
|
|
|
{
|
|
|
|
selectIfValid( m_adapter->FindItem( aLibId ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-30 11:31:36 +00:00
|
|
|
void COMPONENT_TREE::Regenerate()
|
|
|
|
{
|
|
|
|
STATE current;
|
|
|
|
|
|
|
|
// Store the state
|
|
|
|
if( !m_filtering )
|
|
|
|
m_unfilteredState = getState();
|
|
|
|
else
|
|
|
|
current = getState();
|
|
|
|
|
2017-12-13 09:33:04 +00:00
|
|
|
wxString filter = m_query_ctrl->GetValue();
|
|
|
|
m_adapter->UpdateSearchString( filter );
|
|
|
|
m_filtering = !filter.IsEmpty();
|
2017-11-30 11:31:36 +00:00
|
|
|
postPreselectEvent();
|
|
|
|
|
|
|
|
// Restore the state
|
|
|
|
setState( m_filtering ? current : m_unfilteredState );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-20 11:25:12 +00:00
|
|
|
void COMPONENT_TREE::toggleExpand( const wxDataViewItem& aTreeId )
|
|
|
|
{
|
|
|
|
if( !aTreeId.IsOk() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( m_tree_ctrl->IsExpanded( aTreeId ) )
|
|
|
|
m_tree_ctrl->Collapse( aTreeId );
|
|
|
|
else
|
|
|
|
m_tree_ctrl->Expand( aTreeId );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-10 14:22:41 +00:00
|
|
|
void COMPONENT_TREE::selectIfValid( const wxDataViewItem& aTreeId )
|
|
|
|
{
|
|
|
|
if( aTreeId.IsOk() )
|
|
|
|
{
|
|
|
|
m_tree_ctrl->EnsureVisible( aTreeId );
|
|
|
|
m_tree_ctrl->Select( aTreeId );
|
2017-06-25 21:13:39 +00:00
|
|
|
postPreselectEvent();
|
2017-04-10 14:22:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-25 21:13:39 +00:00
|
|
|
void COMPONENT_TREE::postPreselectEvent()
|
2017-04-10 14:22:41 +00:00
|
|
|
{
|
2017-06-25 21:13:39 +00:00
|
|
|
wxCommandEvent event( COMPONENT_PRESELECTED );
|
|
|
|
wxPostEvent( this, event );
|
2017-04-10 14:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-25 21:13:39 +00:00
|
|
|
void COMPONENT_TREE::postSelectEvent()
|
2017-04-10 14:22:41 +00:00
|
|
|
{
|
2017-06-25 21:13:39 +00:00
|
|
|
wxCommandEvent event( COMPONENT_SELECTED );
|
|
|
|
wxPostEvent( this, event );
|
2017-04-10 14:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-30 11:31:36 +00:00
|
|
|
COMPONENT_TREE::STATE COMPONENT_TREE::getState() const
|
2017-04-10 14:22:41 +00:00
|
|
|
{
|
2017-11-30 11:31:36 +00:00
|
|
|
STATE state;
|
|
|
|
wxDataViewItemArray items;
|
|
|
|
m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
|
|
|
|
|
|
|
|
for( const auto& item : items )
|
2017-11-21 13:54:30 +00:00
|
|
|
{
|
2017-11-30 11:31:36 +00:00
|
|
|
if( m_tree_ctrl->IsExpanded( item ) )
|
|
|
|
state.expanded.push_back( item );
|
2017-11-21 13:54:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 10:22:24 +00:00
|
|
|
state.selection = GetSelectedLibId();
|
2017-04-10 14:22:41 +00:00
|
|
|
|
2017-11-30 11:31:36 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPONENT_TREE::setState( const STATE& aState )
|
|
|
|
{
|
2017-12-20 11:05:25 +00:00
|
|
|
m_tree_ctrl->Freeze();
|
2017-11-30 11:31:36 +00:00
|
|
|
|
|
|
|
for( const auto& item : aState.expanded )
|
|
|
|
m_tree_ctrl->Expand( item );
|
|
|
|
|
2017-12-20 11:05:25 +00:00
|
|
|
// wxDataViewCtrl cannot be frozen when a selection
|
|
|
|
// command is issued, otherwise it selects a random item (Windows)
|
|
|
|
m_tree_ctrl->Thaw();
|
|
|
|
|
2017-12-11 10:22:24 +00:00
|
|
|
if( aState.selection.IsValid() )
|
|
|
|
SelectLibId( aState.selection );
|
2017-11-30 11:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPONENT_TREE::onQueryText( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
Regenerate();
|
2017-11-21 13:54:30 +00:00
|
|
|
|
2017-04-10 14:22:41 +00:00
|
|
|
// Required to avoid interaction with SetHint()
|
|
|
|
// See documentation for wxTextEntry::SetHint
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPONENT_TREE::onQueryEnter( wxCommandEvent& aEvent )
|
|
|
|
{
|
2017-09-15 14:17:44 +00:00
|
|
|
if( GetSelectedLibId().IsValid() )
|
2017-06-25 21:13:39 +00:00
|
|
|
postSelectEvent();
|
2017-04-10 14:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPONENT_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
|
|
|
|
{
|
|
|
|
auto const sel = m_tree_ctrl->GetSelection();
|
2017-12-20 11:25:12 +00:00
|
|
|
auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : CMP_TREE_NODE::INVALID;
|
2017-04-10 14:22:41 +00:00
|
|
|
|
|
|
|
switch( aKeyStroke.GetKeyCode() )
|
|
|
|
{
|
2017-12-20 11:23:05 +00:00
|
|
|
case WXK_UP:
|
|
|
|
selectIfValid( GetPrevItem( *m_tree_ctrl, sel ) );
|
|
|
|
break;
|
2017-04-10 14:22:41 +00:00
|
|
|
|
2017-12-20 11:23:05 +00:00
|
|
|
case WXK_DOWN:
|
|
|
|
selectIfValid( GetNextItem( *m_tree_ctrl, sel ) );
|
|
|
|
break;
|
2017-04-10 14:22:41 +00:00
|
|
|
|
2017-12-20 11:25:12 +00:00
|
|
|
case WXK_RETURN:
|
|
|
|
if( type == CMP_TREE_NODE::LIB )
|
|
|
|
{
|
|
|
|
toggleExpand( sel );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through, so the selected component will be treated as the selected one */
|
|
|
|
|
2017-04-10 14:22:41 +00:00
|
|
|
default:
|
|
|
|
aKeyStroke.Skip(); // Any other key: pass on to search box directly.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPONENT_TREE::onTreeSelect( wxDataViewEvent& aEvent )
|
2017-06-25 21:13:39 +00:00
|
|
|
{
|
|
|
|
postPreselectEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPONENT_TREE::onTreeActivate( wxDataViewEvent& aEvent )
|
|
|
|
{
|
2017-09-15 14:17:44 +00:00
|
|
|
if( !GetSelectedLibId().IsValid() )
|
2017-06-25 21:13:39 +00:00
|
|
|
{
|
|
|
|
// Expand library/part units subtree
|
2017-12-20 11:25:12 +00:00
|
|
|
toggleExpand( m_tree_ctrl->GetSelection() );
|
2017-06-25 21:13:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
postSelectEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPONENT_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
|
|
|
|
{
|
|
|
|
const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
|
|
|
|
::wxLaunchDefaultBrowser( info.GetHref() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COMPONENT_TREE::onPreselect( wxCommandEvent& aEvent )
|
2017-04-10 14:22:41 +00:00
|
|
|
{
|
2017-04-10 14:43:25 +00:00
|
|
|
if( m_details_ctrl )
|
|
|
|
{
|
|
|
|
int unit = 0;
|
2017-09-15 14:17:44 +00:00
|
|
|
LIB_ID id = GetSelectedLibId( &unit );
|
2017-04-10 14:22:41 +00:00
|
|
|
|
2017-09-15 14:17:44 +00:00
|
|
|
if( id.IsValid() )
|
|
|
|
m_details_ctrl->SetPage( GenerateAliasInfo( m_sym_lib_table, id, unit ) );
|
2017-04-10 14:43:25 +00:00
|
|
|
else
|
|
|
|
m_details_ctrl->SetPage( wxEmptyString );
|
|
|
|
}
|
2017-04-10 14:22:41 +00:00
|
|
|
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-28 16:59:49 +00:00
|
|
|
void COMPONENT_TREE::onContextMenu( wxDataViewEvent& aEvent )
|
|
|
|
{
|
|
|
|
auto const sel = m_tree_ctrl->GetSelection();
|
|
|
|
auto type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : CMP_TREE_NODE::INVALID;
|
|
|
|
|
|
|
|
if( m_menus[type] )
|
|
|
|
{
|
|
|
|
m_menuActive = true;
|
|
|
|
PopupMenu( m_menus[type].get() );
|
|
|
|
m_menuActive = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-25 21:13:39 +00:00
|
|
|
wxDEFINE_EVENT( COMPONENT_PRESELECTED, wxCommandEvent );
|
|
|
|
wxDEFINE_EVENT( COMPONENT_SELECTED, wxCommandEvent );
|