2017-03-09 09:09:13 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-01-13 12:29:46 +00:00
|
|
|
* Copyright (C) 2017-2022 Kicad Developers, see change_log.txt for contributors.
|
2017-03-09 09:09:13 +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 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/ruler_item.h>
|
|
|
|
#include <preview_items/preview_utils.h>
|
|
|
|
#include <gal/graphics_abstraction_layer.h>
|
2021-07-29 09:47:43 +00:00
|
|
|
#include <layer_ids.h>
|
2019-08-31 14:35:33 +00:00
|
|
|
#include <painter.h>
|
2017-03-09 09:09:13 +00:00
|
|
|
#include <view/view.h>
|
2020-10-14 03:37:48 +00:00
|
|
|
#include <trigo.h>
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
using namespace KIGFX::PREVIEW;
|
|
|
|
|
|
|
|
static const double maxTickDensity = 10.0; // min pixels between tick marks
|
|
|
|
static const double midTickLengthFactor = 1.5;
|
|
|
|
static const double majorTickLengthFactor = 2.5;
|
|
|
|
|
2020-10-02 12:47:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It would be nice to know why Cairo seems to have an opposite layer order from GAL, but
|
|
|
|
* only when drawing RULER_ITEMs (the TWO_POINT_ASSISTANT and ARC_ASSISTANT are immune from
|
|
|
|
* this issue).
|
|
|
|
*
|
|
|
|
* Until then, this egregious hack...
|
|
|
|
*/
|
|
|
|
static int getShadowLayer( KIGFX::GAL* aGal )
|
|
|
|
{
|
|
|
|
if( aGal->IsCairoEngine() )
|
|
|
|
return LAYER_SELECT_OVERLAY;
|
|
|
|
else
|
|
|
|
return LAYER_GP_OVERLAY;
|
|
|
|
}
|
2020-09-30 19:02:30 +00:00
|
|
|
|
|
|
|
|
2020-10-02 12:47:23 +00:00
|
|
|
static void drawCursorStrings( KIGFX::VIEW* aView, const VECTOR2D& aCursor,
|
|
|
|
const VECTOR2D& aRulerVec, EDA_UNITS aUnits,
|
2021-11-23 20:51:37 +00:00
|
|
|
bool aDrawingDropShadows, bool aFlipX, bool aFlipY )
|
2017-03-09 09:09:13 +00:00
|
|
|
{
|
|
|
|
// draw the cursor labels
|
|
|
|
std::vector<wxString> cursorStrings;
|
|
|
|
|
2021-11-23 20:51:37 +00:00
|
|
|
VECTOR2D temp = aRulerVec;
|
|
|
|
|
|
|
|
if( aFlipX )
|
|
|
|
temp.x = -temp.x;
|
|
|
|
|
|
|
|
if( aFlipY )
|
|
|
|
temp.y = -temp.y;
|
|
|
|
|
|
|
|
cursorStrings.push_back( DimensionLabel( "x", temp.x, aUnits ) );
|
|
|
|
cursorStrings.push_back( DimensionLabel( "y", temp.y, aUnits ) );
|
2018-08-25 11:59:21 +00:00
|
|
|
|
2018-02-02 15:56:09 +00:00
|
|
|
cursorStrings.push_back( DimensionLabel( "r", aRulerVec.EuclideanNorm(), aUnits ) );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
double degs = RAD2DECIDEG( -aRulerVec.Angle() );
|
2020-09-24 02:02:21 +00:00
|
|
|
cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), degs,
|
|
|
|
EDA_UNITS::DEGREES ) );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
2021-11-23 20:51:37 +00:00
|
|
|
temp = aRulerVec;
|
2020-10-02 12:47:23 +00:00
|
|
|
DrawTextNextToCursor( aView, aCursor, -temp, cursorStrings, aDrawingDropShadows );
|
2020-09-30 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-02 12:47:23 +00:00
|
|
|
static double getTickLineWidth( const TEXT_DIMS& textDims, bool aDrawingDropShadows )
|
2020-09-30 19:02:30 +00:00
|
|
|
{
|
2020-10-01 10:48:50 +00:00
|
|
|
double width = textDims.StrokeWidth * 0.8;
|
|
|
|
|
2020-10-02 12:47:23 +00:00
|
|
|
if( aDrawingDropShadows )
|
2020-10-01 10:48:50 +00:00
|
|
|
width += textDims.ShadowWidth;
|
2020-09-30 19:02:30 +00:00
|
|
|
|
2020-10-01 10:48:50 +00:00
|
|
|
return width;
|
2017-03-09 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Description of a "tick format" for a scale factor - how many ticks there are
|
|
|
|
* between medium/major ticks and how each scale relates to the last one
|
|
|
|
*/
|
|
|
|
struct TICK_FORMAT
|
|
|
|
{
|
2021-01-25 12:42:36 +00:00
|
|
|
double divisionBase; ///< multiple from the last scale
|
|
|
|
int majorStep; ///< ticks between major ticks
|
|
|
|
int midStep; ///< ticks between medium ticks (0 if no medium ticks)
|
2017-03-09 09:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-12-20 14:11:39 +00:00
|
|
|
static TICK_FORMAT getTickFormatForScale( double aScale, double& aTickSpace, EDA_UNITS aUnits )
|
2017-03-09 09:09:13 +00:00
|
|
|
{
|
|
|
|
// simple 1/2/5 scales per decade
|
|
|
|
static std::vector<TICK_FORMAT> tickFormats =
|
|
|
|
{
|
|
|
|
{ 2, 10, 5 }, // |....:....|
|
|
|
|
{ 2, 5, 0 }, // |....|
|
|
|
|
{ 2.5, 2, 0 }, // |.|.|
|
|
|
|
};
|
|
|
|
|
|
|
|
// could start at a set number of MM, but that's not available in common
|
|
|
|
aTickSpace = 1;
|
|
|
|
|
2020-12-01 01:20:04 +00:00
|
|
|
// Convert to a round (mod-10) number of mils for imperial units
|
|
|
|
if( EDA_UNIT_UTILS::IsImperialUnit( aUnits ) )
|
2017-03-09 09:09:13 +00:00
|
|
|
aTickSpace *= 2.54;
|
|
|
|
|
|
|
|
int tickFormat = 0;
|
|
|
|
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
const auto pixelSpace = aTickSpace * aScale;
|
|
|
|
|
|
|
|
if( pixelSpace >= maxTickDensity )
|
|
|
|
break;
|
|
|
|
|
|
|
|
tickFormat = ( tickFormat + 1 ) % tickFormats.size();
|
|
|
|
aTickSpace *= tickFormats[tickFormat].divisionBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tickFormats[tickFormat];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw labelled ticks on a line. Ticks are spaced according to a
|
2021-04-05 13:24:57 +00:00
|
|
|
* maximum density. Minor ticks are not labelled.
|
2017-03-09 09:09:13 +00:00
|
|
|
*
|
|
|
|
* @param aGal the GAL to draw on
|
|
|
|
* @param aOrigin start of line to draw ticks on
|
|
|
|
* @param aLine line vector
|
|
|
|
* @param aMinorTickLen length of minor ticks in IU
|
|
|
|
*/
|
2020-12-02 00:27:44 +00:00
|
|
|
void drawTicksAlongLine( KIGFX::VIEW* aView, const VECTOR2D& aOrigin, const VECTOR2D& aLine,
|
2020-10-02 12:47:23 +00:00
|
|
|
double aMinorTickLen, EDA_UNITS aUnits, bool aDrawingDropShadows )
|
2017-03-09 09:09:13 +00:00
|
|
|
{
|
2022-01-11 13:47:21 +00:00
|
|
|
KIGFX::GAL* gal = aView->GetGAL();
|
|
|
|
KIFONT::FONT* font = KIFONT::FONT::GetFont();
|
|
|
|
VECTOR2D tickLine = aLine.Rotate( -M_PI_2 );
|
|
|
|
double tickSpace;
|
|
|
|
TICK_FORMAT tickFormat = getTickFormatForScale( gal->GetWorldScale(), tickSpace, aUnits );
|
|
|
|
double majorTickLen = aMinorTickLen * ( majorTickLengthFactor + 1 );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
// number of ticks in whole ruler
|
2022-01-11 13:47:21 +00:00
|
|
|
int numTicks = (int) std::ceil( aLine.EuclideanNorm() / tickSpace );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
// work out which way up the tick labels go
|
2022-01-11 13:47:21 +00:00
|
|
|
TEXT_DIMS labelDims = GetConstantGlyphHeight( gal, -1 );
|
|
|
|
double labelAngle = -tickLine.Angle();
|
|
|
|
VECTOR2I labelOffset = tickLine.Resize( majorTickLen );
|
2020-09-30 19:02:30 +00:00
|
|
|
|
2020-10-02 12:47:23 +00:00
|
|
|
if( aDrawingDropShadows )
|
2020-09-30 19:02:30 +00:00
|
|
|
{
|
2022-01-11 13:47:21 +00:00
|
|
|
labelOffset = { labelDims.ShadowWidth, labelDims.ShadowWidth };
|
|
|
|
labelDims.StrokeWidth += 2 * labelDims.ShadowWidth;
|
2020-09-30 19:02:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:44:58 +00:00
|
|
|
if( aView->IsMirroredX() )
|
|
|
|
labelOffset = -labelOffset;
|
2022-01-11 13:47:21 +00:00
|
|
|
|
|
|
|
TEXT_ATTRIBUTES labelAttrs;
|
|
|
|
labelAttrs.m_Size = labelDims.GlyphSize;
|
|
|
|
labelAttrs.m_StrokeWidth = labelDims.StrokeWidth;
|
2020-12-17 17:44:58 +00:00
|
|
|
|
2017-03-09 09:09:13 +00:00
|
|
|
if( aLine.Angle() > 0 )
|
|
|
|
{
|
2022-01-11 13:47:21 +00:00
|
|
|
labelAttrs.m_Halign = GR_TEXT_H_ALIGN_LEFT;
|
2022-01-13 12:29:46 +00:00
|
|
|
labelAttrs.m_Angle = EDA_ANGLE( labelAngle, RADIANS_T );
|
2017-03-09 09:09:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-11 13:47:21 +00:00
|
|
|
labelAttrs.m_Halign = GR_TEXT_H_ALIGN_RIGHT;
|
2022-01-13 12:29:46 +00:00
|
|
|
labelAttrs.m_Angle = EDA_ANGLE( labelAngle + M_PI, RADIANS_T );
|
2017-03-09 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:27:44 +00:00
|
|
|
BOX2D viewportD = aView->GetViewport();
|
|
|
|
BOX2I viewport( VECTOR2I( viewportD.GetPosition() ), VECTOR2I( viewportD.GetSize() ) );
|
|
|
|
|
|
|
|
viewport.Inflate( majorTickLen * 2 ); // Doesn't have to be accurate, just big enough not
|
|
|
|
// to exclude anything that should be partially drawn
|
|
|
|
|
2017-03-09 09:09:13 +00:00
|
|
|
for( int i = 0; i < numTicks; ++i )
|
|
|
|
{
|
2020-11-07 21:21:56 +00:00
|
|
|
const VECTOR2D tickPos = aOrigin + aLine.Resize( tickSpace * i );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
2020-12-02 00:27:44 +00:00
|
|
|
if( !viewport.Contains( tickPos ) )
|
|
|
|
continue;
|
|
|
|
|
2017-03-09 09:09:13 +00:00
|
|
|
double length = aMinorTickLen;
|
2022-01-11 13:47:21 +00:00
|
|
|
bool drawLabel = false;
|
2017-03-09 09:09:13 +00:00
|
|
|
|
2022-01-11 13:47:21 +00:00
|
|
|
if( i % tickFormat.majorStep == 0 )
|
2017-03-09 09:09:13 +00:00
|
|
|
{
|
|
|
|
drawLabel = true;
|
|
|
|
length *= majorTickLengthFactor;
|
|
|
|
}
|
2022-01-11 13:47:21 +00:00
|
|
|
else if( tickFormat.midStep && i % tickFormat.midStep == 0 )
|
2017-03-09 09:09:13 +00:00
|
|
|
{
|
|
|
|
drawLabel = true;
|
|
|
|
length *= midTickLengthFactor;
|
|
|
|
}
|
|
|
|
|
2022-01-11 13:47:21 +00:00
|
|
|
gal->SetLineWidth( labelAttrs.m_StrokeWidth / 2 );
|
2020-12-02 00:27:44 +00:00
|
|
|
gal->DrawLine( tickPos, tickPos + tickLine.Resize( length ) );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
if( drawLabel )
|
|
|
|
{
|
2020-10-01 10:48:50 +00:00
|
|
|
wxString label = DimensionLabel( "", tickSpace * i, aUnits, false );
|
2022-01-11 13:47:21 +00:00
|
|
|
font->Draw( gal, label, tickPos + labelOffset, labelAttrs );
|
2017-03-09 09:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw simple ticks on the back of a line such that the line is
|
|
|
|
* divided into n parts.
|
|
|
|
*
|
|
|
|
* @param aGal the GAL to draw on
|
|
|
|
* @param aOrigin start of line to draw ticks on
|
|
|
|
* @param aLine line vector
|
|
|
|
* @param aTickLen length of ticks in IU
|
|
|
|
* @param aNumDivisions number of parts to divide the line into
|
|
|
|
*/
|
2020-12-02 00:27:44 +00:00
|
|
|
void drawBacksideTicks( KIGFX::VIEW* aView, const VECTOR2D& aOrigin, const VECTOR2D& aLine,
|
2020-10-02 12:47:23 +00:00
|
|
|
double aTickLen, int aNumDivisions, bool aDrawingDropShadows )
|
2017-03-09 09:09:13 +00:00
|
|
|
{
|
2020-12-02 00:27:44 +00:00
|
|
|
KIGFX::GAL* gal = aView->GetGAL();
|
2020-10-01 10:48:50 +00:00
|
|
|
const double backTickSpace = aLine.EuclideanNorm() / aNumDivisions;
|
2020-09-30 19:02:30 +00:00
|
|
|
const VECTOR2D backTickVec = aLine.Rotate( M_PI_2 ).Resize( aTickLen );
|
2022-01-11 13:47:21 +00:00
|
|
|
TEXT_DIMS textDims = GetConstantGlyphHeight( gal, -1 );
|
2020-12-02 00:27:44 +00:00
|
|
|
|
|
|
|
BOX2D viewportD = aView->GetViewport();
|
|
|
|
BOX2I viewport( VECTOR2I( viewportD.GetPosition() ), VECTOR2I( viewportD.GetSize() ) );
|
|
|
|
|
|
|
|
viewport.Inflate( aTickLen * 4 ); // Doesn't have to be accurate, just big enough not to
|
|
|
|
// exclude anything that should be partially drawn
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < aNumDivisions + 1; ++i )
|
|
|
|
{
|
2020-09-30 19:02:30 +00:00
|
|
|
const VECTOR2D backTickPos = aOrigin + aLine.Resize( backTickSpace * i );
|
2020-12-02 00:27:44 +00:00
|
|
|
|
|
|
|
if( !viewport.Contains( backTickPos ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
gal->SetLineWidth( getTickLineWidth( textDims, aDrawingDropShadows ) );
|
|
|
|
gal->DrawLine( backTickPos, backTickPos + backTickVec );
|
2017-03-09 09:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-23 20:51:37 +00:00
|
|
|
RULER_ITEM::RULER_ITEM( const TWO_POINT_GEOMETRY_MANAGER& aGeomMgr, EDA_UNITS userUnits,
|
|
|
|
bool aFlipX, bool aFlipY )
|
2019-12-20 14:11:39 +00:00
|
|
|
: EDA_ITEM( NOT_USED ), // Never added to anything - just a preview
|
|
|
|
m_geomMgr( aGeomMgr ),
|
2021-11-23 20:51:37 +00:00
|
|
|
m_userUnits( userUnits ),
|
|
|
|
m_flipX( aFlipX ),
|
|
|
|
m_flipY( aFlipY )
|
2019-12-20 14:11:39 +00:00
|
|
|
{
|
|
|
|
}
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
const BOX2I RULER_ITEM::ViewBBox() const
|
|
|
|
{
|
|
|
|
BOX2I tmp;
|
|
|
|
|
2020-10-01 16:52:01 +00:00
|
|
|
if( m_geomMgr.GetOrigin() == m_geomMgr.GetEnd() )
|
|
|
|
return tmp;
|
|
|
|
|
|
|
|
// this is an edit-time artefact; no reason to try and be smart with the bounding box
|
|
|
|
// (besides, we can't tell the text extents without a view to know what the scale is)
|
|
|
|
tmp.SetMaximum();
|
2017-03-09 09:09:13 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RULER_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
|
|
|
|
{
|
2020-10-02 12:47:23 +00:00
|
|
|
aLayers[0] = LAYER_SELECT_OVERLAY;
|
|
|
|
aLayers[1] = LAYER_GP_OVERLAY;
|
2020-09-30 19:02:30 +00:00
|
|
|
aCount = 2;
|
2017-03-09 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RULER_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
|
|
|
|
{
|
2020-10-01 10:48:50 +00:00
|
|
|
KIGFX::GAL* gal = aView->GetGAL();
|
2020-09-30 19:02:30 +00:00
|
|
|
RENDER_SETTINGS* rs = aView->GetPainter()->GetSettings();
|
2020-10-02 12:47:23 +00:00
|
|
|
bool drawingDropShadows = ( aLayer == getShadowLayer( gal ) );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
2020-09-30 19:02:30 +00:00
|
|
|
gal->PushDepth();
|
|
|
|
gal->SetLayerDepth( gal->GetMinDepth() );
|
2020-09-24 02:02:21 +00:00
|
|
|
|
2019-02-15 14:35:31 +00:00
|
|
|
VECTOR2D origin = m_geomMgr.GetOrigin();
|
|
|
|
VECTOR2D end = m_geomMgr.GetEnd();
|
2017-03-16 14:37:39 +00:00
|
|
|
|
2020-09-30 19:02:30 +00:00
|
|
|
gal->SetIsStroke( true );
|
|
|
|
gal->SetIsFill( false );
|
|
|
|
|
|
|
|
gal->SetTextMirrored( false );
|
|
|
|
gal->SetStrokeColor( rs->GetLayerColor( LAYER_AUX_ITEMS ) );
|
2017-09-12 02:49:05 +00:00
|
|
|
|
2020-10-02 12:47:23 +00:00
|
|
|
if( drawingDropShadows )
|
2020-10-01 10:48:50 +00:00
|
|
|
gal->SetStrokeColor( GetShadowColor( gal->GetStrokeColor() ) );
|
2017-03-30 07:44:38 +00:00
|
|
|
|
2020-09-30 19:02:30 +00:00
|
|
|
gal->ResetTextAttributes();
|
2022-01-11 13:47:21 +00:00
|
|
|
TEXT_DIMS textDims = GetConstantGlyphHeight( gal );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
// draw the main line from the origin to cursor
|
2020-10-02 12:47:23 +00:00
|
|
|
gal->SetLineWidth( getTickLineWidth( textDims, drawingDropShadows ) );
|
2020-09-30 19:02:30 +00:00
|
|
|
gal->DrawLine( origin, end );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
2017-03-16 14:37:39 +00:00
|
|
|
VECTOR2D rulerVec( end - origin );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
2021-11-23 20:51:37 +00:00
|
|
|
drawCursorStrings( aView, end, rulerVec, m_userUnits, drawingDropShadows, m_flipX, m_flipY );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
// basic tick size
|
2020-09-30 19:02:30 +00:00
|
|
|
const double minorTickLen = 5.0 / gal->GetWorldScale();
|
2020-10-02 12:47:23 +00:00
|
|
|
const double majorTickLen = minorTickLen * majorTickLengthFactor;
|
2017-03-09 09:09:13 +00:00
|
|
|
|
2020-12-02 00:27:44 +00:00
|
|
|
drawTicksAlongLine( aView, origin, rulerVec, minorTickLen, m_userUnits, drawingDropShadows );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
2020-12-02 00:27:44 +00:00
|
|
|
drawBacksideTicks( aView, origin, rulerVec, majorTickLen, 2, drawingDropShadows );
|
2017-03-09 09:09:13 +00:00
|
|
|
|
|
|
|
// draw the back of the origin "crosshair"
|
2020-09-30 19:02:30 +00:00
|
|
|
gal->DrawLine( origin, origin + rulerVec.Resize( -minorTickLen * midTickLengthFactor ) );
|
|
|
|
gal->PopDepth();
|
2017-03-09 09:09:13 +00:00
|
|
|
}
|