Add a simple item class for graphical overlays

This introduces SIMPLE_OVERLAT_ITEM, which is an abstract EDA_ITEM,
designed for use as an overlay for assisting interactive tools.

The item is drawn only on the GP_OVERLAY layer, and sets the fill,
stroke and line properties before calling a virtual function to draw the
shape, which will depend on the implementaiton of the derived class.

The motivation here is to simplify and unify basic overlays used when
in interactive tools. It is not designed to be the base class of all
possible overlays - more complex ones would be more clearly represented
as their own derivative of EDA_ITEM.

Applications of this class can include: zoom/select rectangles, zone
polygon previews, geometric shape helpers, and so on.
This commit is contained in:
John Beard 2017-03-10 14:33:00 +01:00 committed by Maciej Suminski
parent 9636086f52
commit 5e84e953f3
3 changed files with 211 additions and 0 deletions

View File

@ -183,12 +183,17 @@ set( COMMON_PAGE_LAYOUT_SRCS
page_layout/page_layout_reader.cpp
)
set( COMMON_PREVIEW_ITEMS_SRCS
preview_items/simple_overlay_item.cpp
)
set( COMMON_SRCS
${LIB_KICAD_SRCS}
${COMMON_ABOUT_DLG_SRCS}
${COMMON_DLG_SRCS}
${COMMON_WIDGET_SRCS}
${COMMON_PAGE_LAYOUT_SRCS}
${COMMON_PREVIEW_ITEMS_SRCS}
base_struct.cpp
basicframe.cpp
bezier_curves.cpp

View File

@ -0,0 +1,70 @@
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2017 Kicad Developers, see change_log.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 <preview_items/simple_overlay_item.h>
#include <gal/graphics_abstraction_layer.h>
#include <view/view.h>
#include <layers_id_colors_and_visibility.h>
using namespace KIGFX::PREVIEW;
SIMPLE_OVERLAY_ITEM::SIMPLE_OVERLAY_ITEM():
EDA_ITEM( NOT_USED ), // this item is never added to a BOARD so it needs no type.
m_fillColor( WHITE ),
m_strokeColor( WHITE ),
m_lineWidth( 1.0 )
{
}
void SIMPLE_OVERLAY_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
{
auto& gal = *aView->GetGAL();
setupGal( gal );
drawPreviewShape( gal );
}
void SIMPLE_OVERLAY_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
{
static const int SelectionLayer = ITEM_GAL_LAYER( GP_OVERLAY );
aLayers[0] = SelectionLayer;
aCount = 1;
}
void SIMPLE_OVERLAY_ITEM::setupGal( KIGFX::GAL& aGal ) const
{
// default impl: set up the GAL options we have - the
// overriding class can add to this if needed
aGal.SetLineWidth( m_lineWidth );
aGal.SetStrokeColor( m_strokeColor );
aGal.SetFillColor( m_fillColor );
aGal.SetIsStroke( true );
aGal.SetIsFill( true );
}

View File

@ -0,0 +1,136 @@
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2017 Kicad Developers, see change_log.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
*/
#ifndef PREVIEW_SIMPLE_OUTLINE_ITEM__H_
#define PREVIEW_SIMPLE_OUTLINE_ITEM__H_
#include <base_struct.h>
#include <gal/color4d.h>
namespace KIGFX
{
class VIEW;
class GAL;
namespace PREVIEW
{
/**
* SIMPLE_OVERLAY_ITEM is class that represents a visual area drawn on
* a canvas, used to temporarily demarcate an area or show something
* on an overlay. An example could be the drag select lasso box.
*
* This class is pretty generic in terms of what the area looks like.
* It provides a fill, stroke and width, which is probably sufficient
* for most simple previews, but the inheritor has freedom to override
* this.
*
* If this doesn't suit a particular preview, it may mean you should
* implement your own EDA_ITEM derivative rather than inheriting this.
*/
class SIMPLE_OVERLAY_ITEM : public EDA_ITEM
{
public:
SIMPLE_OVERLAY_ITEM();
/**
* Sets the overlay layer only. You can override this if
* you have more layers to draw on.
*/
void ViewGetLayers( int aLayers[], int& aCount ) const override;
/**
* Draws the preview - this is done by calling the two functions:
* setupGal() and drawPreviewShape(). If you need more than this,
* or direct access to the VIEW, you probably should make a new
* class.
*/
void ViewDraw( int aLayer, KIGFX::VIEW* aView ) const override final;
#if defined(DEBUG)
void Show( int x, std::ostream& st ) const override
{
}
#endif
/**
* Get class name
* @return string "SIMPLE_OVERLAY_ITEM"
*/
virtual wxString GetClass() const override
{
return "SIMPLE_OVERLAY_ITEM";
}
///> Set the stroke colour to set before drawing preview
void SetStrokeColor( const COLOR4D& aNewColor )
{
m_strokeColor = aNewColor;
}
///> Set the fill colour to set before drawing preview
void SetFillColor( const COLOR4D& aNewColor )
{
m_fillColor = aNewColor;
}
///> Set the line width to set before drawing preview
void SetLineWidth( double aNewWidth )
{
m_lineWidth = aNewWidth;
}
private:
/**
* Set up the GAL canvas - this provides a default implementation,
* that sets fill, stroke and width.
* If that's not suitable, you can set more options in
* updatePreviewShape(), but you might find that defining a new
* EDA_ITEM derivative is easier for heavily customised cases.
*/
void setupGal( KIGFX::GAL& aGal ) const;
/**
* Draw the preview onto the given GAL. setupGal() will be called
* before this function.
*
* Subclasses should implement this in terms of their own graphical
* data.
*/
virtual void drawPreviewShape( KIGFX::GAL& aGal ) const = 0;
COLOR4D m_fillColor;
COLOR4D m_strokeColor;
double m_lineWidth;
};
} // PREVIEW
} // KIGFX
#endif // PREVIEW_SIMPLE_OUTLINE_ITEM__H_