Commit 60b0a4e0 authored by Maciej Suminski's avatar Maciej Suminski
Browse files

Alternative approach to handling events and commands by TOOL_DISPATCHER.

Event handlers are (dis)connected depending on the active view.
TOOL_DISPATCHER inherits from wxEvtHandler, so now it receives events directly instead of being fed by external handlers.
parent 3fd26b6d
Loading
Loading
Loading
Loading
+52 −33
Original line number Original line Diff line number Diff line
@@ -41,6 +41,8 @@
#include <tool/tool_dispatcher.h>
#include <tool/tool_dispatcher.h>
#include <tool/tool_manager.h>
#include <tool/tool_manager.h>


#include <boost/foreach.hpp>

#ifdef __WXDEBUG__
#ifdef __WXDEBUG__
#include <profile.h>
#include <profile.h>
#endif /* __WXDEBUG__ */
#endif /* __WXDEBUG__ */
@@ -50,6 +52,7 @@ EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin
                                        GalType aGalType ) :
                                        GalType aGalType ) :
    wxWindow( aParentWindow, aWindowId, aPosition, aSize )
    wxWindow( aParentWindow, aWindowId, aPosition, aSize )
{
{
    m_parent     = aParentWindow;
    m_gal        = NULL;
    m_gal        = NULL;
    m_backend    = GAL_TYPE_NONE;
    m_backend    = GAL_TYPE_NONE;
    m_view       = NULL;
    m_view       = NULL;
@@ -68,23 +71,7 @@ EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin
    m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this );
    m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this );


    Connect( wxEVT_SIZE, wxSizeEventHandler( EDA_DRAW_PANEL_GAL::onSize ), NULL, this );
    Connect( wxEVT_SIZE, wxSizeEventHandler( EDA_DRAW_PANEL_GAL::onSize ), NULL, this );

    /* Generic events for the Tool Dispatcher */
    Connect( wxEVT_MOTION,          wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_LEFT_UP,         wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_LEFT_DOWN,       wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_LEFT_DCLICK,     wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_RIGHT_UP,        wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_RIGHT_DOWN,      wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_RIGHT_DCLICK,    wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_MIDDLE_UP,       wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_MIDDLE_DOWN,     wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_MIDDLE_DCLICK,   wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_MOUSEWHEEL,      wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_CHAR,            wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
    Connect( wxEVT_ENTER_WINDOW, wxEventHandler( EDA_DRAW_PANEL_GAL::onEnter ), NULL, this );
    Connect( wxEVT_ENTER_WINDOW, wxEventHandler( EDA_DRAW_PANEL_GAL::onEnter ), NULL, this );
    Connect( KIGFX::WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE,
             wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );


    // Set up timer that prevents too frequent redraw commands
    // Set up timer that prevents too frequent redraw commands
    m_refreshTimer.SetOwner( this );
    m_refreshTimer.SetOwner( this );
@@ -180,6 +167,53 @@ void EDA_DRAW_PANEL_GAL::Refresh( bool aEraseBackground, const wxRect* aRect )
}
}




void EDA_DRAW_PANEL_GAL::SetEventDispatcher( TOOL_DISPATCHER* aEventDispatcher )
{
    m_eventDispatcher = aEventDispatcher;

    const wxEventType events[] =
    {
        wxEVT_LEFT_UP, wxEVT_LEFT_DOWN, wxEVT_LEFT_DCLICK,
        wxEVT_RIGHT_UP, wxEVT_RIGHT_DOWN, wxEVT_RIGHT_DCLICK,
        wxEVT_MIDDLE_UP, wxEVT_MIDDLE_DOWN, wxEVT_MIDDLE_DCLICK,
        wxEVT_MOTION, wxEVT_MOUSEWHEEL, wxEVT_CHAR, KIGFX::WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE
    };

    const wxEventType commands[] =
    {
#if wxCHECK_VERSION( 3, 0, 0 )
        wxEVT_TOOL
#else
        wxEVT_COMMAND_MENU_SELECTED, wxEVT_COMMAND_TOOL_CLICKED
#endif
    };

    if( m_eventDispatcher )
    {
        BOOST_FOREACH( wxEventType eventType, events )
            Connect( eventType, wxEventHandler( TOOL_DISPATCHER::DispatchWxEvent ),
                     NULL, m_eventDispatcher );

        BOOST_FOREACH( wxEventType eventType, commands )
            m_parent->Connect( eventType,
                               wxCommandEventHandler( TOOL_DISPATCHER::DispatchWxCommand ),
                               NULL, m_eventDispatcher );
    }
    else
    {
        // While loops are used to be sure, that we are removing all event handlers
        BOOST_FOREACH( wxEventType eventType, events )
            while( Disconnect( eventType, wxEventHandler( TOOL_DISPATCHER::DispatchWxEvent ),
                     NULL, m_eventDispatcher ) );

        BOOST_FOREACH( wxEventType eventType, commands )
            while( m_parent->Disconnect( eventType,
                               wxCommandEventHandler( TOOL_DISPATCHER::DispatchWxCommand ),
                               NULL, m_eventDispatcher ) );
    }
}


void EDA_DRAW_PANEL_GAL::StartDrawing()
void EDA_DRAW_PANEL_GAL::StartDrawing()
{
{
    m_pendingRefresh = false;
    m_pendingRefresh = false;
@@ -258,21 +292,6 @@ void EDA_DRAW_PANEL_GAL::SwitchBackend( GalType aGalType )
}
}




void EDA_DRAW_PANEL_GAL::onEvent( wxEvent& aEvent )
{
    if( !m_eventDispatcher )
    {
        aEvent.Skip();
    }
    else
    {
        m_eventDispatcher->DispatchWxEvent( aEvent );
    }

    Refresh();
}


void EDA_DRAW_PANEL_GAL::onEnter( wxEvent& aEvent )
void EDA_DRAW_PANEL_GAL::onEnter( wxEvent& aEvent )
{
{
    // Getting focus is necessary in order to receive key events properly
    // Getting focus is necessary in order to receive key events properly
+2 −0
Original line number Original line Diff line number Diff line
@@ -281,6 +281,8 @@ void TOOL_DISPATCHER::DispatchWxEvent( wxEvent& aEvent )
    if( evt )
    if( evt )
        m_toolMgr->ProcessEvent( *evt );
        m_toolMgr->ProcessEvent( *evt );


    static_cast<PCB_BASE_FRAME*>( m_toolMgr->GetEditFrame() )->GetGalCanvas()->Refresh();

    // pass the event to the GUI, it might still be interested in it
    // pass the event to the GUI, it might still be interested in it
    aEvent.Skip();
    aEvent.Skip();
}
}
+7 −6
Original line number Original line Diff line number Diff line
@@ -114,11 +114,10 @@ public:
     * Function SetEventDispatcher()
     * Function SetEventDispatcher()
     * Sets a dispatcher that processes events and forwards them to tools.
     * Sets a dispatcher that processes events and forwards them to tools.
     * @param aEventDispatcher is the object that will be used for dispatching events.
     * @param aEventDispatcher is the object that will be used for dispatching events.
     * DRAW_PANEL_GAL does not take over the ownership. Passing NULL disconnects all event
     * handlers from the DRAW_PANEL_GAL and parent frame.
     */
     */
    void SetEventDispatcher( TOOL_DISPATCHER* aEventDispatcher )
    void SetEventDispatcher( TOOL_DISPATCHER* aEventDispatcher );
    {
        m_eventDispatcher = aEventDispatcher;
    }


    /**
    /**
     * Function StartDrawing()
     * Function StartDrawing()
@@ -148,12 +147,14 @@ public:
protected:
protected:
    void onPaint( wxPaintEvent& WXUNUSED( aEvent ) );
    void onPaint( wxPaintEvent& WXUNUSED( aEvent ) );
    void onSize( wxSizeEvent& aEvent );
    void onSize( wxSizeEvent& aEvent );
    void onEvent( wxEvent& aEvent );
    void onEnter( wxEvent& aEvent );
    void onEnter( wxEvent& aEvent );
    void onRefreshTimer( wxTimerEvent& aEvent );
    void onRefreshTimer( wxTimerEvent& aEvent );


    static const int MinRefreshPeriod = 17;             ///< 60 FPS.
    static const int MinRefreshPeriod = 17;             ///< 60 FPS.


    /// Pointer to the parent window
    wxWindow*               m_parent;

    /// Last timestamp when the panel was refreshed
    /// Last timestamp when the panel was refreshed
    wxLongLong               m_lastRefresh;
    wxLongLong               m_lastRefresh;


+2 −2
Original line number Original line Diff line number Diff line
@@ -26,7 +26,7 @@
#define __TOOL_DISPATCHER_H
#define __TOOL_DISPATCHER_H


#include <vector>
#include <vector>

#include <wx/event.h>
#include <tool/tool_event.h>
#include <tool/tool_event.h>


class TOOL_MANAGER;
class TOOL_MANAGER;
@@ -47,7 +47,7 @@ class VIEW;
 * - issues TOOL_EVENTS to the tool manager
 * - issues TOOL_EVENTS to the tool manager
 */
 */


class TOOL_DISPATCHER
class TOOL_DISPATCHER : public wxEvtHandler
{
{
public:
public:
    /**
    /**
+1 −1
Original line number Original line Diff line number Diff line
@@ -120,9 +120,9 @@ protected:
    bool              m_useCmpFileForFpNames;   ///< is true, use the .cmp file from CvPcb, else use the netlist
    bool              m_useCmpFileForFpNames;   ///< is true, use the .cmp file from CvPcb, else use the netlist
                                                // to know the footprint name of components.
                                                // to know the footprint name of components.


    // Functions that handle the Tool Framework (de)initalization
    void setupTools();
    void setupTools();
    void destroyTools();
    void destroyTools();
    void onGenericCommand( wxCommandEvent& aEvent );


    // we'll use lower case function names for private member functions.
    // we'll use lower case function names for private member functions.
    void createPopUpMenuForZones( ZONE_CONTAINER* edge_zone, wxMenu* aPopMenu );
    void createPopUpMenuForZones( ZONE_CONTAINER* edge_zone, wxMenu* aPopMenu );
Loading