2022-09-14 02:59:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2023-12-18 21:09:13 +00:00
|
|
|
* Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2022-09-14 02:59:57 +00:00
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <widgets/search_pane_tab.h>
|
|
|
|
#include <widgets/search_pane.h>
|
|
|
|
#include <kiway.h>
|
2022-09-21 03:58:49 +00:00
|
|
|
#include <vector>
|
2023-08-09 17:30:58 +00:00
|
|
|
#include <string_utils.h>
|
2024-01-30 01:35:04 +00:00
|
|
|
#include <wx/clipbrd.h>
|
2022-09-14 02:59:57 +00:00
|
|
|
|
|
|
|
SEARCH_PANE_LISTVIEW::SEARCH_PANE_LISTVIEW( SEARCH_HANDLER* handler, wxWindow* parent,
|
|
|
|
wxWindowID winid, const wxPoint& pos,
|
|
|
|
const wxSize& size ) :
|
|
|
|
wxListView( parent, winid, pos, size, wxLC_REPORT | wxLC_VIRTUAL ),
|
2023-08-09 17:30:58 +00:00
|
|
|
m_handler( handler ),
|
|
|
|
m_sortCol( -1 ),
|
2023-11-09 14:30:23 +00:00
|
|
|
m_sortAscending( true ),
|
|
|
|
m_selectionDirty( false )
|
2022-09-14 02:59:57 +00:00
|
|
|
{
|
|
|
|
SetItemCount( 0 );
|
|
|
|
|
|
|
|
RefreshColumnNames();
|
|
|
|
|
2022-09-21 03:38:06 +00:00
|
|
|
Bind( wxEVT_LIST_ITEM_SELECTED, &SEARCH_PANE_LISTVIEW::OnItemSelected, this );
|
2023-01-25 16:09:23 +00:00
|
|
|
Bind( wxEVT_LIST_ITEM_ACTIVATED, &SEARCH_PANE_LISTVIEW::OnItemActivated, this );
|
2022-09-25 14:40:08 +00:00
|
|
|
Bind( wxEVT_LIST_ITEM_FOCUSED, &SEARCH_PANE_LISTVIEW::OnItemSelected, this );
|
|
|
|
Bind( wxEVT_LIST_ITEM_DESELECTED, &SEARCH_PANE_LISTVIEW::OnItemDeselected, this );
|
2023-08-09 17:30:58 +00:00
|
|
|
Bind( wxEVT_LIST_COL_CLICK, &SEARCH_PANE_LISTVIEW::OnColClicked, this );
|
2023-11-09 14:30:23 +00:00
|
|
|
Bind( wxEVT_UPDATE_UI, &SEARCH_PANE_LISTVIEW::OnUpdateUI, this );
|
2024-01-30 01:35:04 +00:00
|
|
|
Bind( wxEVT_CHAR, &SEARCH_PANE_LISTVIEW::OnChar, this );
|
2022-09-27 23:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SEARCH_PANE_LISTVIEW::~SEARCH_PANE_LISTVIEW()
|
|
|
|
{
|
|
|
|
Unbind( wxEVT_LIST_ITEM_SELECTED, &SEARCH_PANE_LISTVIEW::OnItemSelected, this );
|
2023-01-25 16:09:23 +00:00
|
|
|
Unbind( wxEVT_LIST_ITEM_ACTIVATED, &SEARCH_PANE_LISTVIEW::OnItemActivated, this );
|
2022-09-27 23:16:29 +00:00
|
|
|
Unbind( wxEVT_LIST_ITEM_FOCUSED, &SEARCH_PANE_LISTVIEW::OnItemSelected, this );
|
|
|
|
Unbind( wxEVT_LIST_ITEM_DESELECTED, &SEARCH_PANE_LISTVIEW::OnItemDeselected, this );
|
2024-01-30 01:35:04 +00:00
|
|
|
Unbind( wxEVT_LIST_COL_CLICK, &SEARCH_PANE_LISTVIEW::OnColClicked, this );
|
2023-11-09 14:30:23 +00:00
|
|
|
Unbind( wxEVT_UPDATE_UI, &SEARCH_PANE_LISTVIEW::OnUpdateUI, this );
|
2024-01-30 01:35:04 +00:00
|
|
|
Unbind( wxEVT_CHAR, &SEARCH_PANE_LISTVIEW::OnChar, this );
|
2022-09-21 03:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-21 07:04:31 +00:00
|
|
|
void SEARCH_PANE_LISTVIEW::GetSelectRowsList( std::vector<long>& aSelectedList )
|
2022-09-21 03:58:49 +00:00
|
|
|
{
|
2022-09-21 07:04:31 +00:00
|
|
|
long idx = GetFirstSelected();
|
2022-09-21 17:34:54 +00:00
|
|
|
|
|
|
|
if( idx < 0 ) // Nothing selected
|
|
|
|
return;
|
|
|
|
|
2022-09-21 07:04:31 +00:00
|
|
|
aSelectedList.emplace_back( idx );
|
2022-09-21 03:58:49 +00:00
|
|
|
|
|
|
|
idx = GetNextSelected( idx );
|
2022-12-10 23:37:23 +00:00
|
|
|
|
|
|
|
while( idx >= 0 )
|
2022-09-21 03:58:49 +00:00
|
|
|
{
|
2022-09-21 07:04:31 +00:00
|
|
|
aSelectedList.emplace_back( idx );
|
2022-09-21 03:58:49 +00:00
|
|
|
idx = GetNextSelected( idx );
|
|
|
|
}
|
2022-09-14 02:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-25 16:09:23 +00:00
|
|
|
void SEARCH_PANE_LISTVIEW::OnItemActivated( wxListEvent& aEvent )
|
|
|
|
{
|
2024-02-14 18:15:49 +00:00
|
|
|
long item = aEvent.GetIndex();
|
|
|
|
|
2023-01-25 16:09:23 +00:00
|
|
|
CallAfter(
|
2024-02-14 18:15:49 +00:00
|
|
|
[this, item]()
|
2023-01-25 16:09:23 +00:00
|
|
|
{
|
2024-02-14 18:15:49 +00:00
|
|
|
m_handler->ActivateItem( item );
|
2023-01-25 16:09:23 +00:00
|
|
|
} );
|
|
|
|
|
2023-11-09 14:30:23 +00:00
|
|
|
m_selectionDirty = true;
|
2023-01-25 16:09:23 +00:00
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-21 03:38:06 +00:00
|
|
|
void SEARCH_PANE_LISTVIEW::OnItemSelected( wxListEvent& aEvent )
|
2022-09-14 02:59:57 +00:00
|
|
|
{
|
2023-11-09 14:30:23 +00:00
|
|
|
m_selectionDirty = true;
|
2022-09-25 14:40:08 +00:00
|
|
|
aEvent.Skip();
|
2022-09-21 03:58:49 +00:00
|
|
|
}
|
2022-09-14 02:59:57 +00:00
|
|
|
|
2022-09-21 03:58:49 +00:00
|
|
|
|
|
|
|
void SEARCH_PANE_LISTVIEW::OnItemDeselected( wxListEvent& aEvent )
|
|
|
|
{
|
2023-11-09 14:30:23 +00:00
|
|
|
m_selectionDirty = true;
|
2022-12-10 23:37:23 +00:00
|
|
|
aEvent.Skip();
|
2022-09-14 02:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-09 14:30:23 +00:00
|
|
|
void SEARCH_PANE_LISTVIEW::OnUpdateUI( wxUpdateUIEvent& aEvent )
|
|
|
|
{
|
|
|
|
if( m_selectionDirty )
|
|
|
|
{
|
|
|
|
m_selectionDirty = false;
|
|
|
|
|
|
|
|
std::vector<long> list;
|
|
|
|
GetSelectRowsList( list );
|
|
|
|
m_handler->SelectItems( list );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-09 17:30:58 +00:00
|
|
|
void SEARCH_PANE_LISTVIEW::OnColClicked( wxListEvent& aEvent )
|
|
|
|
{
|
|
|
|
if( aEvent.GetColumn() == m_sortCol )
|
|
|
|
{
|
|
|
|
m_sortAscending = !m_sortAscending;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_sortAscending = true;
|
|
|
|
m_sortCol = aEvent.GetColumn();
|
|
|
|
}
|
|
|
|
|
2023-08-10 00:09:14 +00:00
|
|
|
ShowSortIndicator( m_sortCol, m_sortAscending );
|
|
|
|
|
2023-08-09 17:30:58 +00:00
|
|
|
Sort();
|
|
|
|
Refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-30 01:35:04 +00:00
|
|
|
void SEARCH_PANE_LISTVIEW::OnChar( wxKeyEvent& aEvent )
|
|
|
|
{
|
|
|
|
if( aEvent.GetKeyCode() == WXK_CONTROL_A )
|
|
|
|
{
|
|
|
|
// Select All
|
|
|
|
for( int row = 0; row < GetItemCount(); row++ )
|
|
|
|
SetItemState( row, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
|
|
|
|
}
|
|
|
|
else if( aEvent.GetKeyCode() == WXK_CONTROL_C )
|
|
|
|
{
|
|
|
|
// Copy to clipboard the selected rows
|
|
|
|
if( wxTheClipboard->Open() )
|
|
|
|
{
|
|
|
|
wxString txt;
|
|
|
|
for( int row = 0; row < GetItemCount(); row++ )
|
|
|
|
{
|
|
|
|
if( GetItemState( row, wxLIST_STATE_SELECTED ) == wxLIST_STATE_SELECTED )
|
|
|
|
{
|
|
|
|
for( int col = 0; col < GetColumnCount(); col++ )
|
|
|
|
{
|
|
|
|
if( GetColumnWidth( col ) > 0 )
|
|
|
|
{
|
|
|
|
txt += GetItemText( row, col );
|
|
|
|
if( row <= GetItemCount() - 1 )
|
|
|
|
txt += wxT( "\t" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
txt += wxT( "\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTheClipboard->SetData( new wxTextDataObject( txt ) );
|
|
|
|
wxTheClipboard->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-09 17:30:58 +00:00
|
|
|
void SEARCH_PANE_LISTVIEW::Sort()
|
|
|
|
{
|
|
|
|
m_handler->Sort( m_sortCol, m_sortAscending );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-14 02:59:57 +00:00
|
|
|
void SEARCH_PANE_LISTVIEW::RefreshColumnNames()
|
|
|
|
{
|
|
|
|
Freeze();
|
|
|
|
DeleteAllColumns();
|
|
|
|
|
2023-10-01 21:45:12 +00:00
|
|
|
std::vector<std::tuple<wxString, int, wxListColumnFormat>> columns = m_handler->GetColumns();
|
2022-12-10 23:37:23 +00:00
|
|
|
|
2023-10-01 21:45:12 +00:00
|
|
|
for( auto& [ columnName, colProportion, colAlign ] : columns )
|
|
|
|
AppendColumn( wxGetTranslation( columnName ), colAlign );
|
2022-12-10 23:37:23 +00:00
|
|
|
|
2023-10-01 21:45:12 +00:00
|
|
|
int widthUnit = GetClientSize().GetWidth() / 10;
|
2022-12-10 23:37:23 +00:00
|
|
|
|
|
|
|
for( int ii = 0; ii < (int) columns.size(); ++ii )
|
2023-10-01 21:45:12 +00:00
|
|
|
SetColumnWidth( ii, widthUnit * std::get<1>( columns[ ii ] ) );
|
2022-09-14 02:59:57 +00:00
|
|
|
|
|
|
|
Thaw();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString SEARCH_PANE_LISTVIEW::OnGetItemText( long item, long column ) const
|
|
|
|
{
|
2022-12-10 23:37:23 +00:00
|
|
|
return m_handler->GetResultCell( (int) item, (int) column );
|
2022-09-14 02:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SEARCH_PANE_TAB::SEARCH_PANE_TAB( SEARCH_HANDLER* handler, wxWindow* parent, wxWindowID aId,
|
|
|
|
const wxPoint& aLocation, const wxSize& aSize ) :
|
|
|
|
wxPanel( parent, aId, aLocation, aSize ),
|
|
|
|
m_handler( handler )
|
|
|
|
{
|
|
|
|
wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
|
|
|
|
|
|
|
|
m_listView = new SEARCH_PANE_LISTVIEW( handler, this );
|
|
|
|
sizer->Add( m_listView, 5, wxRIGHT | wxBOTTOM | wxEXPAND, 1 );
|
|
|
|
|
|
|
|
SetSizer( sizer );
|
|
|
|
|
|
|
|
Layout();
|
|
|
|
sizer->Fit( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SEARCH_PANE_TAB::Search( wxString& query )
|
|
|
|
{
|
|
|
|
int results = m_handler->Search( query );
|
|
|
|
m_listView->SetItemCount( results );
|
2023-08-09 17:30:58 +00:00
|
|
|
m_listView->Sort();
|
2022-09-14 02:59:57 +00:00
|
|
|
m_listView->Refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-21 03:38:06 +00:00
|
|
|
void SEARCH_PANE_TAB::Clear()
|
|
|
|
{
|
|
|
|
m_listView->SetItemCount( 0 );
|
|
|
|
m_listView->Refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-14 02:59:57 +00:00
|
|
|
void SEARCH_PANE_TAB::RefreshColumnNames()
|
|
|
|
{
|
|
|
|
m_listView->RefreshColumnNames();
|
2023-01-25 16:09:23 +00:00
|
|
|
}
|