From 8a09a1d3298038540c6dd3b4f9dfdd3ef37de7c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20W=C5=82ostowski?= Date: Mon, 30 Oct 2017 20:17:23 +0100 Subject: [PATCH] refactoring: derive PCB-specific PCB_VIEW from VIEW with the intention of moving PCB_EDIT_FRAME entries used by the tools (e.g. GetActiveLayer) there --- common/draw_panel_gal.cpp | 7 --- include/view/view.h | 20 +++---- include/view/wx_view_controls.h | 2 +- pcbnew/CMakeLists.txt | 1 + pcbnew/basepcbframe.cpp | 8 +-- pcbnew/dialogs/dialog_display_options.cpp | 2 +- pcbnew/pcb_draw_panel_gal.cpp | 23 ++++++-- pcbnew/pcb_draw_panel_gal.h | 4 ++ pcbnew/pcb_painter.cpp | 3 + pcbnew/pcb_view.cpp | 47 +++++++++++++++ pcbnew/pcb_view.h | 63 +++++++++++++++++++++ pcbnew/tools/pcb_tool.cpp | 11 ++++ pcbnew/tools/pcb_tool.h | 7 ++- pcbnew/tools/pcbnew_control.cpp | 69 ++++++++++------------- 14 files changed, 197 insertions(+), 70 deletions(-) create mode 100644 pcbnew/pcb_view.cpp create mode 100644 pcbnew/pcb_view.h diff --git a/common/draw_panel_gal.cpp b/common/draw_panel_gal.cpp index 2639cd1bed..c98cc64125 100644 --- a/common/draw_panel_gal.cpp +++ b/common/draw_panel_gal.cpp @@ -74,9 +74,6 @@ EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS ); EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas - m_view = new KIGFX::VIEW( true ); - m_view->SetGAL( m_gal ); - Connect( wxEVT_SIZE, wxSizeEventHandler( EDA_DRAW_PANEL_GAL::onSize ), NULL, this ); Connect( wxEVT_ENTER_WINDOW, wxEventHandler( EDA_DRAW_PANEL_GAL::onEnter ), NULL, this ); Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( EDA_DRAW_PANEL_GAL::onLostFocus ), NULL, this ); @@ -103,10 +100,6 @@ EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin NULL, m_eventDispatcher ); } - // View controls is the first in the event handler chain, so the Tool Framework operates - // on updated viewport data. - m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this ); - m_pendingRefresh = false; m_drawing = false; m_drawingEnabled = false; diff --git a/include/view/view.h b/include/view/view.h index 0448609022..30bb6df39a 100644 --- a/include/view/view.h +++ b/include/view/view.h @@ -68,7 +68,7 @@ public: */ VIEW( bool aIsDynamic = true ); - ~VIEW(); + virtual ~VIEW(); // nasty hack, invoked by the destructor of VIEW_ITEM to auto-remove the item // from the owning VIEW if there is any. Kicad relies too much on this mechanism. @@ -83,14 +83,14 @@ public: * @param aItem: item to be added. No ownership is given * @param aDrawPriority: priority to draw this item on its layer, lowest first. */ - void Add( VIEW_ITEM* aItem, int aDrawPriority = -1 ); + virtual void Add( VIEW_ITEM* aItem, int aDrawPriority = -1 ); /** * Function Remove() * Removes a VIEW_ITEM from the view. * @param aItem: item to be removed. Caller must dispose the removed item if necessary */ - void Remove( VIEW_ITEM* aItem ); + virtual void Remove( VIEW_ITEM* aItem ); /** @@ -102,7 +102,7 @@ public: * first). * @return Number of found items. */ - int Query( const BOX2I& aRect, std::vector& aResult ) const; + virtual int Query( const BOX2I& aRect, std::vector& aResult ) const; /** * Sets the item visibility. @@ -136,8 +136,8 @@ public: * @param aItem: the item to update. * @param aUpdateFlags: how much the object has changed. */ - void Update( VIEW_ITEM* aItem, int aUpdateFlags ); - void Update( VIEW_ITEM* aItem ); + virtual void Update( VIEW_ITEM* aItem, int aUpdateFlags ); + virtual void Update( VIEW_ITEM* aItem ); /** * Function SetRequired() @@ -476,7 +476,7 @@ public: * @param aLayer: the layer or -1 in case when no particular layer should * be displayed on the top. */ - void SetTopLayer( int aLayer, bool aEnabled = true ); + virtual void SetTopLayer( int aLayer, bool aEnabled = true ); /** * Function EnableTopLayer() @@ -485,9 +485,9 @@ public: * layer set previously with SetTopLayer function. * @param aEnable whether to enable or disable display of the top layer. */ - void EnableTopLayer( bool aEnable ); + virtual void EnableTopLayer( bool aEnable ); - int GetTopLayer() const; + virtual int GetTopLayer() const; /** * Function ClearTopLayers() @@ -513,7 +513,7 @@ public: * Function Redraw() * Immediately redraws the whole view. */ - void Redraw(); + virtual void Redraw(); /** * Function RecacheAllItems() diff --git a/include/view/wx_view_controls.h b/include/view/wx_view_controls.h index f675caef1f..af0cf938bc 100644 --- a/include/view/wx_view_controls.h +++ b/include/view/wx_view_controls.h @@ -47,7 +47,7 @@ class WX_VIEW_CONTROLS : public VIEW_CONTROLS, public wxEvtHandler { public: WX_VIEW_CONTROLS( VIEW* aView, wxScrolledCanvas* aParentPanel ); - ~WX_VIEW_CONTROLS() + virtual ~WX_VIEW_CONTROLS() {} /// Handler functions diff --git a/pcbnew/CMakeLists.txt b/pcbnew/CMakeLists.txt index 844cb65e8f..f9cf76ae25 100644 --- a/pcbnew/CMakeLists.txt +++ b/pcbnew/CMakeLists.txt @@ -264,6 +264,7 @@ set( PCBNEW_CLASS_SRCS pcbplot.cpp pcb_draw_panel_gal.cpp pcb_general_settings.cpp + pcb_view.cpp plot_board_layers.cpp plot_brditems_plotter.cpp print_board_functions.cpp diff --git a/pcbnew/basepcbframe.cpp b/pcbnew/basepcbframe.cpp index 579a956704..c5ac1a0bc5 100644 --- a/pcbnew/basepcbframe.cpp +++ b/pcbnew/basepcbframe.cpp @@ -52,7 +52,7 @@ #include #include #include -#include +#include #include #include #include @@ -450,10 +450,8 @@ void PCB_BASE_FRAME::OnTogglePadDrawMode( wxCommandEvent& aEvent ) if( gal ) { // Apply new display options to the GAL canvas - auto view = gal->GetView(); - auto painter = static_cast ( view->GetPainter() ); - auto settings = static_cast ( painter->GetSettings() ); - settings->LoadDisplayOptions( displ_opts ); + auto view = static_cast( gal->GetView() ); + view->UpdateDisplayOptions( displ_opts ); // Update pads BOARD* board = GetBoard(); diff --git a/pcbnew/dialogs/dialog_display_options.cpp b/pcbnew/dialogs/dialog_display_options.cpp index cca2a073f4..8f0fefca9f 100644 --- a/pcbnew/dialogs/dialog_display_options.cpp +++ b/pcbnew/dialogs/dialog_display_options.cpp @@ -40,7 +40,7 @@ #include #include -#include +#include #include #include diff --git a/pcbnew/pcb_draw_panel_gal.cpp b/pcbnew/pcb_draw_panel_gal.cpp index 57898d2a4f..d79b6c8154 100644 --- a/pcbnew/pcb_draw_panel_gal.cpp +++ b/pcbnew/pcb_draw_panel_gal.cpp @@ -23,7 +23,7 @@ */ #include "pcb_draw_panel_gal.h" -#include +#include #include #include #include @@ -104,12 +104,19 @@ PCB_DRAW_PANEL_GAL::PCB_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin KIGFX::GAL_DISPLAY_OPTIONS& aOptions, GAL_TYPE aGalType ) : EDA_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, aSize, aOptions, aGalType ) { - setDefaultLayerOrder(); - setDefaultLayerDeps(); + m_view = new KIGFX::PCB_VIEW( true ); + m_view->SetGAL( m_gal ); m_painter.reset( new KIGFX::PCB_PAINTER( m_gal ) ); m_view->SetPainter( m_painter.get() ); + setDefaultLayerOrder(); + setDefaultLayerDeps(); + + // View controls is the first in the event handler chain, so the Tool Framework operates + // on updated viewport data. + m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this ); + // Load display options (such as filled/outline display of items). // Can be made only if the parent window is an EDA_DRAW_FRAME (or a derived class) // which is not always the case (namely when it is used from a wxDialog like the pad editor) @@ -117,8 +124,8 @@ EDA_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, aSize, aOptions, aGalTy if( frame ) { - auto displ_opts = (PCB_DISPLAY_OPTIONS*) frame->GetDisplayOptions(); - static_cast( m_view->GetPainter()->GetSettings() )->LoadDisplayOptions( displ_opts ); + auto opts = (PCB_DISPLAY_OPTIONS*) frame->GetDisplayOptions(); + static_cast( m_view )->UpdateDisplayOptions( opts ); } } @@ -442,3 +449,9 @@ void PCB_DRAW_PANEL_GAL::setDefaultLayerDeps() m_view->SetLayerDisplayOnly( LAYER_GRID ); m_view->SetLayerDisplayOnly( LAYER_DRC ); } + + +KIGFX::PCB_VIEW* PCB_DRAW_PANEL_GAL::view() const +{ + return static_cast( m_view ); +} diff --git a/pcbnew/pcb_draw_panel_gal.h b/pcbnew/pcb_draw_panel_gal.h index 1db0825ecc..75a2ac6931 100644 --- a/pcbnew/pcb_draw_panel_gal.h +++ b/pcbnew/pcb_draw_panel_gal.h @@ -32,6 +32,7 @@ namespace KIGFX { class WORKSHEET_VIEWITEM; class RATSNEST_VIEWITEM; + class PCB_VIEW; } class COLORS_DESIGN_SETTINGS; @@ -103,6 +104,9 @@ public: void RedrawRatsnest(); protected: + + KIGFX::PCB_VIEW* view() const; + ///> Reassigns layer order to the initial settings. void setDefaultLayerOrder(); diff --git a/pcbnew/pcb_painter.cpp b/pcbnew/pcb_painter.cpp index f3ee59b94c..9850a993de 100644 --- a/pcbnew/pcb_painter.cpp +++ b/pcbnew/pcb_painter.cpp @@ -63,6 +63,9 @@ PCB_RENDER_SETTINGS::PCB_RENDER_SETTINGS() m_sketchMode[i] = false; } + COLORS_DESIGN_SETTINGS dummyCds( FRAME_PCB ); + ImportLegacyColors( &dummyCds ); + update(); } diff --git a/pcbnew/pcb_view.cpp b/pcbnew/pcb_view.cpp new file mode 100644 index 0000000000..357ecca6cd --- /dev/null +++ b/pcbnew/pcb_view.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +namespace KIGFX +{ + +PCB_VIEW::PCB_VIEW( bool aIsDynamic ) : + VIEW ( aIsDynamic ) +{ + +} + +PCB_VIEW::~PCB_VIEW() +{ + +} + +void PCB_VIEW::Add( VIEW_ITEM* aItem, int aDrawPriority ) +{ + VIEW::Add( aItem, aDrawPriority ); +} + +void PCB_VIEW::Remove( VIEW_ITEM* aItem ) +{ + VIEW::Remove( aItem ); +} + +void PCB_VIEW::Update( VIEW_ITEM* aItem, int aUpdateFlags ) +{ + VIEW::Update( aItem, aUpdateFlags ); +} + +/// @copydoc VIEW::Update() +void PCB_VIEW::Update( VIEW_ITEM* aItem ) +{ + VIEW::Update( aItem ); +} + +void PCB_VIEW::UpdateDisplayOptions( PCB_DISPLAY_OPTIONS* aOptions ) +{ + auto painter = static_cast( GetPainter() ); + auto settings = static_cast( painter->GetSettings() ); + settings->LoadDisplayOptions( aOptions ); +} + +}; diff --git a/pcbnew/pcb_view.h b/pcbnew/pcb_view.h new file mode 100644 index 0000000000..ce7148ddd8 --- /dev/null +++ b/pcbnew/pcb_view.h @@ -0,0 +1,63 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2013-2016 CERN + * @author Tomasz Wlostowski + * + * 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 + */ + +#ifndef __PCB_VIEW_H +#define __PCB_VIEW_H + +#include +#include + +class PCB_DISPLAY_OPTIONS; + +namespace KIGFX { + +class PCB_VIEW : public VIEW +{ +public: + PCB_VIEW( bool aIsDynamic = true ); + virtual ~PCB_VIEW(); + + /// @copydoc VIEW::Add() + virtual void Add( VIEW_ITEM* aItem, int aDrawPriority = -1 ) override; + /// @copydoc VIEW::Remove() + + virtual void Remove( VIEW_ITEM* aItem ) override; + + /// @copydoc VIEW::Update() + virtual void Update( VIEW_ITEM* aItem, int aUpdateFlags ) override; + + /// @copydoc VIEW::Update() + virtual void Update( VIEW_ITEM* aItem ) override; + + void UpdateDisplayOptions( PCB_DISPLAY_OPTIONS* aOptions ); +private: + + PCB_LAYER_ID m_activeLayer; + PCB_LAYER_ID m_routeLayerTop; + PCB_LAYER_ID m_routeLayerBottom; +}; + +}; + +#endif diff --git a/pcbnew/tools/pcb_tool.cpp b/pcbnew/tools/pcb_tool.cpp index 3e64deb100..cc0bf26372 100644 --- a/pcbnew/tools/pcb_tool.cpp +++ b/pcbnew/tools/pcb_tool.cpp @@ -30,6 +30,7 @@ #include #include +#include #include "selection_tool.h" #include "pcb_actions.h" @@ -193,3 +194,13 @@ void PCB_TOOL::doInteractiveItemPlacement( INTERACTIVE_PLACER_BASE* aPlacer, view()->Remove( &preview ); } + +PCB_DISPLAY_OPTIONS* PCB_TOOL::displayOptions() const +{ + return static_cast( frame()->GetDisplayOptions() ); +} + +PCB_DRAW_PANEL_GAL* PCB_TOOL::canvas() const +{ + return static_cast( frame()->GetGalCanvas() ); +} diff --git a/pcbnew/tools/pcb_tool.h b/pcbnew/tools/pcb_tool.h index 7ca005de26..d09781da2a 100644 --- a/pcbnew/tools/pcb_tool.h +++ b/pcbnew/tools/pcb_tool.h @@ -32,6 +32,7 @@ #include #include #include +#include #include @@ -44,6 +45,8 @@ class PCB_TOOL; class PCB_EDIT_FRAME; +class PCB_DISPLAY_OPTIONS; +class PCB_DRAW_PANEL_GAL; struct INTERACTIVE_PLACER_BASE { @@ -120,11 +123,13 @@ protected: const wxString& aCommitMessage, int aOptions = IPO_ROTATE | IPO_FLIP | IPO_REPEAT ); - KIGFX::VIEW* view() const { return getView(); } + KIGFX::PCB_VIEW* view() const { return static_cast( getView() ); } KIGFX::VIEW_CONTROLS* controls() const { return getViewControls(); } PCB_EDIT_FRAME* frame() const { return getEditFrame(); } BOARD* board() const { return getModel(); } MODULE* module() const { return board()->m_Modules; } + PCB_DISPLAY_OPTIONS* displayOptions() const; + PCB_DRAW_PANEL_GAL* canvas() const; bool m_editModules; }; diff --git a/pcbnew/tools/pcbnew_control.cpp b/pcbnew/tools/pcbnew_control.cpp index a0cd7b2a2a..7c51ceceb4 100644 --- a/pcbnew/tools/pcbnew_control.cpp +++ b/pcbnew/tools/pcbnew_control.cpp @@ -256,16 +256,17 @@ void PCBNEW_CONTROL::Reset( RESET_REASON aReason ) } } +template void Flip( T& aValue ) +{ + aValue = !aValue; +} int PCBNEW_CONTROL::TrackDisplayMode( const TOOL_EVENT& aEvent ) { - auto painter = static_cast( getView()->GetPainter() ); - auto settings = painter->GetSettings(); + auto opts = displayOptions(); - // Apply new display options to the GAL canvas - PCB_DISPLAY_OPTIONS* displ_opts = (PCB_DISPLAY_OPTIONS*)m_frame->GetDisplayOptions(); - displ_opts->m_DisplayPcbTrackFill = !displ_opts->m_DisplayPcbTrackFill; - settings->LoadDisplayOptions( displ_opts ); + Flip( opts->m_DisplayPcbTrackFill ); + view()->UpdateDisplayOptions( opts ); for( auto track : board()->Tracks() ) { @@ -273,30 +274,25 @@ int PCBNEW_CONTROL::TrackDisplayMode( const TOOL_EVENT& aEvent ) view()->Update( track, KIGFX::GEOMETRY ); } - m_frame->GetGalCanvas()->Refresh(); + canvas()->Refresh(); return 0; } - int PCBNEW_CONTROL::PadDisplayMode( const TOOL_EVENT& aEvent ) { - auto painter = static_cast( getView()->GetPainter() ); - auto settings = painter->GetSettings(); + auto opts = displayOptions(); - PCB_DISPLAY_OPTIONS* displ_opts = (PCB_DISPLAY_OPTIONS*)m_frame->GetDisplayOptions(); + Flip( opts->m_DisplayPadFill ); + view()->UpdateDisplayOptions( opts ); - // Apply new display options to the GAL canvas - displ_opts->m_DisplayPadFill = !displ_opts->m_DisplayPadFill; - settings->LoadDisplayOptions( displ_opts ); - - for( auto module : board()->Modules() ) + for( auto module : board()->Modules() ) // fixme: move to PCB_VIEW { for( auto pad : module->Pads() ) - getView()->Update( pad, KIGFX::GEOMETRY ); + view()->Update( pad, KIGFX::GEOMETRY ); } - m_frame->GetGalCanvas()->Refresh(); + canvas()->Refresh(); return 0; } @@ -304,21 +300,18 @@ int PCBNEW_CONTROL::PadDisplayMode( const TOOL_EVENT& aEvent ) int PCBNEW_CONTROL::ViaDisplayMode( const TOOL_EVENT& aEvent ) { - auto painter = static_cast( getView()->GetPainter() ); - auto settings = painter->GetSettings(); - PCB_DISPLAY_OPTIONS* displ_opts = (PCB_DISPLAY_OPTIONS*)m_frame->GetDisplayOptions(); + auto opts = displayOptions(); - // Apply new display options to the GAL canvas - displ_opts->m_DisplayViaFill = !displ_opts->m_DisplayViaFill; - settings->LoadDisplayOptions( displ_opts ); + Flip( opts->m_DisplayViaFill ); + view()->UpdateDisplayOptions( opts ); for( auto track : board()->Tracks() ) { if( track->Type() == PCB_TRACE_T || track->Type() == PCB_VIA_T ) - getView()->Update( track, KIGFX::GEOMETRY ); + view()->Update( track, KIGFX::GEOMETRY ); } - m_frame->GetGalCanvas()->Refresh(); + canvas()->Refresh(); return 0; } @@ -326,26 +319,24 @@ int PCBNEW_CONTROL::ViaDisplayMode( const TOOL_EVENT& aEvent ) int PCBNEW_CONTROL::ZoneDisplayMode( const TOOL_EVENT& aEvent ) { - auto painter = static_cast( getView()->GetPainter() ); - auto settings = painter->GetSettings(); - PCB_DISPLAY_OPTIONS* displ_opts = (PCB_DISPLAY_OPTIONS*)m_frame->GetDisplayOptions(); + auto opts = displayOptions(); // Apply new display options to the GAL canvas if( aEvent.IsAction( &PCB_ACTIONS::zoneDisplayEnable ) ) - displ_opts->m_DisplayZonesMode = 0; + opts->m_DisplayZonesMode = 0; else if( aEvent.IsAction( &PCB_ACTIONS::zoneDisplayDisable ) ) - displ_opts->m_DisplayZonesMode = 1; + opts->m_DisplayZonesMode = 1; else if( aEvent.IsAction( &PCB_ACTIONS::zoneDisplayOutlines ) ) - displ_opts->m_DisplayZonesMode = 2; + opts->m_DisplayZonesMode = 2; else assert( false ); - settings->LoadDisplayOptions( displ_opts ); + view()->UpdateDisplayOptions( opts ); for( int i = 0; i < board()->GetAreaCount(); ++i ) view()->Update( board()->GetArea( i ), KIGFX::GEOMETRY ); - m_frame->GetGalCanvas()->Refresh(); + canvas()->Refresh(); return 0; } @@ -353,13 +344,11 @@ int PCBNEW_CONTROL::ZoneDisplayMode( const TOOL_EVENT& aEvent ) int PCBNEW_CONTROL::HighContrastMode( const TOOL_EVENT& aEvent ) { - auto painter = static_cast( getView()->GetPainter() ); - auto settings = painter->GetSettings(); - PCB_DISPLAY_OPTIONS* displ_opts = (PCB_DISPLAY_OPTIONS*)m_frame->GetDisplayOptions(); + auto opts = displayOptions(); - displ_opts->m_ContrastModeDisplay = !displ_opts->m_ContrastModeDisplay; - settings->LoadDisplayOptions( displ_opts ); - m_frame->GetGalCanvas()->SetHighContrastLayer( m_frame->GetActiveLayer() ); + Flip( opts->m_ContrastModeDisplay ); + view()->UpdateDisplayOptions( opts ); + canvas()->SetHighContrastLayer( m_frame->GetActiveLayer() ); return 0; }