KiCad PCB EDA Suite
Loading...
Searching...
No Matches
router_status_view_item.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <gal/color4d.h>
26#include <kiplatform/ui.h>
27#include <layer_ids.h>
28#include <wx/settings.h>
29
32#include "gr_text.h"
33
34using namespace KIGFX;
35
36
38{
39 BOX2I tmp;
40
41 // this is an edit-time artefact; no reason to try and be smart with the bounding box
42 // (besides, we can't tell the text extents without a view to know what the scale is)
43 tmp.SetMaximum();
44 return tmp;
45}
46
48{
49 return { LAYER_UI_START, LAYER_UI_START + 1 };
50}
51
52void ROUTER_STATUS_VIEW_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
53{
54 KIGFX::GAL* gal = aView->GetGAL();
55 bool viewFlipped = gal->IsFlippedX();
56 bool drawingDropShadows = ( aLayer == LAYER_UI_START );
57
58 gal->Save();
59 gal->Scale( { 1., 1. } );
60
64 const KIFONT::METRICS& fontMetrics = KIFONT::METRICS::Default();
65 TEXT_ATTRIBUTES textAttrs;
66 int textWidth;
67
68 textWidth = std::max( GRTextWidth( m_status, font, textDims.GlyphSize, textDims.StrokeWidth,
69 false, false, fontMetrics ),
70 GRTextWidth( m_hint, font, hintDims.GlyphSize, hintDims.StrokeWidth,
71 false, false, fontMetrics ) );
72
73 VECTOR2I margin( KiROUND( textDims.GlyphSize.x * 0.4 ), KiROUND( textDims.GlyphSize.y * 0.6 ) );
74 VECTOR2I size( textWidth + margin.x, KiROUND( textDims.GlyphSize.y * 1.7 ) );
75 VECTOR2I offset( margin.x * 5, -( size.y + margin.y * 5 ) );
76
77 if( !m_hint.IsEmpty() )
78 size.y += KiROUND( hintDims.GlyphSize.y * 1.2 );
79
80 if( drawingDropShadows )
81 {
82 gal->SetIsFill( true );
83 gal->SetIsStroke( true );
84 gal->SetLineWidth( gal->GetScreenWorldMatrix().GetScale().x * 2 );
85 gal->SetStrokeColor( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT ) );
86 KIGFX::COLOR4D bgColor( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
87 gal->SetFillColor( bgColor.WithAlpha( 0.9 ) );
88
89 gal->DrawRectangle( GetPosition() + offset - margin,
90 GetPosition() + offset + size + margin );
91 gal->Restore();
92 return;
93 }
94
95 COLOR4D bg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
96 COLOR4D normal = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT );
98
99 if( viewFlipped )
101 else
102 textAttrs.m_Halign = GR_TEXT_H_ALIGN_LEFT;
103
104 gal->SetIsFill( false );
105 gal->SetIsStroke( true );
106 gal->SetStrokeColor( normal );
107 textAttrs.m_Halign = GR_TEXT_H_ALIGN_LEFT;
108
109 // Prevent text flipping when view is flipped
110 if( gal->IsFlippedX() )
111 {
112 textAttrs.m_Mirrored = true;
114 }
115
116 textAttrs.m_Size = textDims.GlyphSize;
117 textAttrs.m_StrokeWidth = textDims.StrokeWidth;
118
119 VECTOR2I textPos = GetPosition() + offset + margin;
120 font->Draw( gal, m_status, textPos, textAttrs, KIFONT::METRICS::Default() );
121
122 if( !m_hint.IsEmpty() )
123 {
124 textAttrs.m_Size = hintDims.GlyphSize;
125 textAttrs.m_StrokeWidth = hintDims.StrokeWidth;
126
127 textPos.y += KiROUND( textDims.GlyphSize.y * 1.6 );
128 font->Draw( gal, m_hint, textPos, textAttrs, KIFONT::METRICS::Default() );
129 }
130
131 gal->Restore();
132}
133
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
constexpr void SetMaximum()
Definition: box2.h:80
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
static FONT * GetFont(const wxString &aFontName=wxEmptyString, bool aBold=false, bool aItalic=false, const std::vector< wxString > *aEmbeddedFiles=nullptr, bool aForDrawingSheet=false)
Definition: font.cpp:146
void Draw(KIGFX::GAL *aGal, const wxString &aText, const VECTOR2I &aPosition, const VECTOR2I &aCursor, const TEXT_ATTRIBUTES &aAttributes, const METRICS &aFontMetrics) const
Draw a string.
Definition: font.cpp:258
static const METRICS & Default()
Definition: font.cpp:52
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:311
Abstract interface for drawing on a 2D-surface.
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a rectangle.
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
const MATRIX3x3D & GetScreenWorldMatrix() const
Get the screen <-> world transformation matrix.
virtual void Restore()
Restore the context.
virtual void SetLineWidth(float aLineWidth)
Set the line width.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
virtual void Scale(const VECTOR2D &aScale)
Scale the context.
virtual void Save()
Save the context.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:199
VECTOR2< T > GetScale() const
Get the scale components of the matrix.
Definition: matrix3x3.h:295
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
VECTOR2I GetPosition() const override
GR_TEXT_H_ALIGN_T m_Halign
int GRTextWidth(const wxString &aText, KIFONT::FONT *aFont, const VECTOR2I &aSize, int aThickness, bool aBold, bool aItalic, const KIFONT::METRICS &aFontMetrics)
Definition: gr_text.cpp:113
@ LAYER_UI_START
Definition: layer_ids.h:265
TEXT_DIMS GetConstantGlyphHeight(KIGFX::GAL *aGal, int aRelativeSize=0)
Set the GAL glyph height to a constant scaled value, so that it always looks the same on screen.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
@ GR_TEXT_H_ALIGN_RIGHT
@ GR_TEXT_H_ALIGN_LEFT