Schematic hierarchy selection dialog fixes.

Derive the dialog from DIALOG_SHIM and show dialog quasi-modally to prevent
other KiCad main frame window events from being blocked.

Remove redundant storing of parent window pointer.

Verify parent window pointer is actually a SCH_EDIT_FRAME since the dialog
directly accesses parent object methods.

Replace static event table with dynamic event handling.

Fixes lp:1718241

https://bugs.launchpad.net/kicad/+bug/1718241
This commit is contained in:
Wayne Stambaugh 2017-09-20 08:44:52 -04:00
parent a43f3ab1fe
commit 6816b0dc99
1 changed files with 52 additions and 52 deletions

View File

@ -2,8 +2,8 @@
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2004 Jean-Pierre Charras, jp.charras at wanadoo.fr * Copyright (C) 2004 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2008-2016 Wayne Stambaugh <stambaughw@verizon.net> * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright (C) 2004-2016 KiCad Developers, see change_log.txt for contributors. * Copyright (C) 2004-2017 KiCad Developers, see change_log.txt for contributors.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -31,9 +31,10 @@
#include <class_drawpanel.h> #include <class_drawpanel.h>
#include <confirm.h> #include <confirm.h>
#include <id.h> #include <id.h>
#include <schframe.h>
#include <bitmaps.h> #include <bitmaps.h>
#include <dialog_shim.h>
#include <schframe.h>
#include <general.h> #include <general.h>
#include <sch_sheet.h> #include <sch_sheet.h>
#include <sch_sheet_path.h> #include <sch_sheet_path.h>
@ -44,6 +45,7 @@
#include <class_netlist_object.h> #include <class_netlist_object.h>
#include <sch_sheet_path.h> #include <sch_sheet_path.h>
enum enum
{ {
ID_TREECTRL_HIERARCHY = 1600 ID_TREECTRL_HIERARCHY = 1600
@ -53,9 +55,9 @@ enum
class HIERARCHY_NAVIG_DLG; class HIERARCHY_NAVIG_DLG;
/* This class derived from wxTreeItemData stores the SCH_SHEET_PATH of each /**
* sheet in hierarchy in each TreeItem, in its associated data buffer * Store an SCH_SHEET_PATH of each sheet in hierarchy.
*/ */
class TreeItemData : public wxTreeItemData class TreeItemData : public wxTreeItemData
{ {
public: public:
@ -67,7 +69,10 @@ public:
} }
}; };
/* Class to handle hierarchy tree. */
/**
* Handle hierarchy tree control.
*/
class HIERARCHY_TREE : public wxTreeCtrl class HIERARCHY_TREE : public wxTreeCtrl
{ {
private: private:
@ -86,6 +91,7 @@ public:
DECLARE_DYNAMIC_CLASS( HIERARCHY_TREE ) DECLARE_DYNAMIC_CLASS( HIERARCHY_TREE )
}; };
IMPLEMENT_DYNAMIC_CLASS( HIERARCHY_TREE, wxTreeCtrl ) IMPLEMENT_DYNAMIC_CLASS( HIERARCHY_TREE, wxTreeCtrl )
@ -108,10 +114,9 @@ HIERARCHY_TREE::HIERARCHY_TREE( HIERARCHY_NAVIG_DLG* parent ) :
} }
class HIERARCHY_NAVIG_DLG : public wxDialog class HIERARCHY_NAVIG_DLG : public DIALOG_SHIM
{ {
public: public:
SCH_EDIT_FRAME* m_Parent;
HIERARCHY_TREE* m_Tree; HIERARCHY_TREE* m_Tree;
int m_nbsheets; int m_nbsheets;
@ -121,60 +126,65 @@ private:
public: public:
HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos ); HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos );
void BuildSheetsTree( SCH_SHEET_PATH* list, wxTreeItemId* previousmenu );
~HIERARCHY_NAVIG_DLG(); ~HIERARCHY_NAVIG_DLG();
/**
* Create the hierarchical tree of the schematic.
*
* This routine is re-entrant!
*/
void BuildTree( SCH_SHEET_PATH* list, wxTreeItemId* previousmenu );
/**
* Open the selected sheet and display the corresponding screen when a tree item is
* selected.
*/
void OnSelect( wxTreeEvent& event ); void OnSelect( wxTreeEvent& event );
private: private:
void OnQuit( wxCommandEvent& event ); void OnQuit( wxCommandEvent& event );
DECLARE_EVENT_TABLE()
}; };
BEGIN_EVENT_TABLE( HIERARCHY_NAVIG_DLG, wxDialog )
EVT_TREE_ITEM_ACTIVATED( ID_TREECTRL_HIERARCHY, HIERARCHY_NAVIG_DLG::OnSelect )
END_EVENT_TABLE()
void SCH_EDIT_FRAME::InstallHierarchyFrame( wxPoint& pos ) void SCH_EDIT_FRAME::InstallHierarchyFrame( wxPoint& pos )
{ {
HIERARCHY_NAVIG_DLG* treeframe = new HIERARCHY_NAVIG_DLG( this, pos ); HIERARCHY_NAVIG_DLG* treeframe = new HIERARCHY_NAVIG_DLG( this, pos );
treeframe->ShowModal(); treeframe->ShowQuasiModal();
treeframe->Destroy(); treeframe->Destroy();
} }
HIERARCHY_NAVIG_DLG::HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos ) : HIERARCHY_NAVIG_DLG::HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint& aPos ) :
wxDialog( aParent, wxID_ANY, _( "Navigator" ), aPos, wxSize( 110, 50 ), DIALOG_SHIM( aParent, wxID_ANY, _( "Navigator" ), wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ) wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
{ {
wxTreeItemId cellule; wxASSERT( dynamic_cast< SCH_EDIT_FRAME* >( aParent ) );
wxTreeItemId root;
m_Parent = aParent;
m_Tree = new HIERARCHY_TREE( this ); m_Tree = new HIERARCHY_TREE( this );
m_nbsheets = 1; m_nbsheets = 1;
cellule = m_Tree->AddRoot( _( "Root" ), 0, 1 ); root = m_Tree->AddRoot( _( "Root" ), 0, 1 );
m_Tree->SetItemBold( cellule, true ); m_Tree->SetItemBold( root, true );
SCH_SHEET_PATH list; SCH_SHEET_PATH list;
list.push_back( g_RootSheet ); list.push_back( g_RootSheet );
m_Tree->SetItemData( cellule, new TreeItemData( list ) ); m_Tree->SetItemData( root, new TreeItemData( list ) );
if( m_Parent->GetCurrentSheet().Last() == g_RootSheet ) if( dynamic_cast< SCH_EDIT_FRAME* >( aParent )->GetCurrentSheet().Last() == g_RootSheet )
m_Tree->SelectItem( cellule ); //root. m_Tree->SelectItem( root ); //root.
maxposx = 15; maxposx = 15;
BuildSheetsTree( &list, &cellule ); BuildTree( &list, &root );
m_Tree->Expand( cellule ); m_Tree->Expand( root );
wxRect itemrect; wxRect itemrect;
m_Tree->GetBoundingRect( cellule, itemrect ); m_Tree->GetBoundingRect( root, itemrect );
// Set dialog window size to be large enough // Set dialog window size to be large enough
m_TreeSize.x = itemrect.GetWidth() + 20; m_TreeSize.x = itemrect.GetWidth() + 20;
@ -185,11 +195,14 @@ HIERARCHY_NAVIG_DLG::HIERARCHY_NAVIG_DLG( SCH_EDIT_FRAME* aParent, const wxPoint
m_TreeSize.y += 10; m_TreeSize.y += 10;
SetClientSize( m_TreeSize ); SetClientSize( m_TreeSize );
Bind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::OnSelect, this );
} }
HIERARCHY_NAVIG_DLG::~HIERARCHY_NAVIG_DLG() HIERARCHY_NAVIG_DLG::~HIERARCHY_NAVIG_DLG()
{ {
Unbind( wxEVT_TREE_ITEM_ACTIVATED, &HIERARCHY_NAVIG_DLG::OnSelect, this );
} }
@ -200,26 +213,12 @@ void HIERARCHY_NAVIG_DLG::OnQuit( wxCommandEvent& event )
} }
/* Routine to create the hierarchical tree of the schematic void HIERARCHY_NAVIG_DLG::BuildTree( SCH_SHEET_PATH* list, wxTreeItemId* previousmenu )
* This routine is re-entrant!
*/
void HIERARCHY_NAVIG_DLG::BuildSheetsTree( SCH_SHEET_PATH* list, wxTreeItemId* previousmenu )
{ {
wxTreeItemId menu; wxTreeItemId menu;
if( m_nbsheets > NB_MAX_SHEET ) wxCHECK_RET( m_nbsheets < NB_MAX_SHEET, "Maximum number of sheets exceeded." );
{
if( m_nbsheets == (NB_MAX_SHEET + 1) )
{
wxString msg;
msg << wxT( "BuildSheetsTree: Error: nbsheets > " ) << NB_MAX_SHEET;
DisplayError( this, msg );
m_nbsheets++;
}
return;
}
maxposx += m_Tree->GetIndent(); maxposx += m_Tree->GetIndent();
SCH_ITEM* schitem = list->LastDrawList(); SCH_ITEM* schitem = list->LastDrawList();
@ -244,13 +243,13 @@ void HIERARCHY_NAVIG_DLG::BuildSheetsTree( SCH_SHEET_PATH* list, wxTreeItemId*
m_TreeSize.x = std::max( m_TreeSize.x, ll ); m_TreeSize.x = std::max( m_TreeSize.x, ll );
m_TreeSize.y += 1; m_TreeSize.y += 1;
if( *list == m_Parent->GetCurrentSheet() ) if( *list == dynamic_cast< SCH_EDIT_FRAME* >( GetParent() )->GetCurrentSheet() )
{ {
m_Tree->EnsureVisible( menu ); m_Tree->EnsureVisible( menu );
m_Tree->SelectItem( menu ); m_Tree->SelectItem( menu );
} }
BuildSheetsTree( list, &menu ); BuildTree( list, &menu );
m_Tree->Expand( menu ); m_Tree->Expand( menu );
list->pop_back(); list->pop_back();
} }
@ -262,16 +261,17 @@ void HIERARCHY_NAVIG_DLG::BuildSheetsTree( SCH_SHEET_PATH* list, wxTreeItemId*
} }
/* Called on a double-click on a tree item:
* Open the selected sheet, and display the corresponding screen
*/
void HIERARCHY_NAVIG_DLG::OnSelect( wxTreeEvent& event ) void HIERARCHY_NAVIG_DLG::OnSelect( wxTreeEvent& event )
{ {
wxTreeItemId ItemSel = m_Tree->GetSelection(); wxTreeItemId ItemSel = m_Tree->GetSelection();
SCH_EDIT_FRAME* parent = dynamic_cast< SCH_EDIT_FRAME* >( GetParent() );
m_Parent->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath ); wxCHECK2_MSG( parent, Close( true ),
m_Parent->DisplayCurrentSheet(); "Parent window of hierarchy dialog is not SCH_EDIT_FRAME." );
parent->SetCurrentSheet(( (TreeItemData*) m_Tree->GetItemData( ItemSel ) )->m_SheetPath );
parent->DisplayCurrentSheet();
Close( true ); Close( true );
} }