KiCad PCB EDA Suite
ws_painter.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) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
26 #include <pgm_base.h>
27 #include <base_screen.h>
28 #include <eda_draw_frame.h>
29 #include <title_block.h>
30 #include <build_version.h>
34 
35 #include <page_layout/ws_painter.h>
37 
38 #include <wx/app.h>
39 
40 using namespace KIGFX;
41 
42 static const wxString productName = wxT( "KiCad E.D.A. " );
43 
45 {
46  m_backgroundColor = COLOR4D( 1.0, 1.0, 1.0, 1.0 );
49  m_brightenedColor = COLOR4D( 0.0, 1.0, 0.0, 0.9 );
50  m_pageBorderColor = COLOR4D( 0.4, 0.4, 0.4, 1.0 );
51 
52  update();
53 }
54 
55 
57 {
58  for( int layer = SCH_LAYER_ID_START; layer < SCH_LAYER_ID_END; layer ++)
59  m_layerColors[ layer ] = aSettings->GetColor( layer );
60 
61  for( int layer = GAL_LAYER_ID_START; layer < GAL_LAYER_ID_END; layer ++)
62  m_layerColors[ layer ] = aSettings->GetColor( layer );
63 
67 }
68 
69 
70 COLOR4D WS_RENDER_SETTINGS::GetColor( const VIEW_ITEM* aItem, int aLayer ) const
71 {
72  const EDA_ITEM* item = dynamic_cast<const EDA_ITEM*>( aItem );
73 
74  if( item )
75  {
76  // Selection disambiguation
77  if( item->IsBrightened() )
78  return m_brightenedColor;
79 
80  if( item->IsSelected() )
81  return m_selectedColor;
82  }
83 
84  return m_normalColor;
85 }
86 
87 
88 void WS_DRAW_ITEM_LIST::GetTextVars( wxArrayString* aVars )
89 {
90  aVars->push_back( wxT( "KICAD_VERSION" ) );
91  aVars->push_back( wxT( "#" ) );
92  aVars->push_back( wxT( "##" ) );
93  aVars->push_back( wxT( "SHEETNAME" ) );
94  aVars->push_back( wxT( "FILENAME" ) );
95  aVars->push_back( wxT( "PAPER" ) );
96  aVars->push_back( wxT( "LAYER" ) );
97  aVars->push_back( wxT( "ISSUE_DATE" ) );
98  aVars->push_back( wxT( "REVISION" ) );
99  aVars->push_back( wxT( "TITLE" ) );
100  aVars->push_back( wxT( "COMPANY" ) );
101  aVars->push_back( wxT( "COMMENT0" ) );
102  aVars->push_back( wxT( "COMMENT1" ) );
103  aVars->push_back( wxT( "COMMENT2" ) );
104  aVars->push_back( wxT( "COMMENT3" ) );
105  aVars->push_back( wxT( "COMMENT4" ) );
106  aVars->push_back( wxT( "COMMENT5" ) );
107  aVars->push_back( wxT( "COMMENT6" ) );
108  aVars->push_back( wxT( "COMMENT7" ) );
109  aVars->push_back( wxT( "COMMENT8" ) );
110  aVars->push_back( wxT( "COMMENT9" ) );
111 }
112 
113 
114 // returns the full text corresponding to the aTextbase,
115 // after replacing format symbols by the corresponding value
116 wxString WS_DRAW_ITEM_LIST::BuildFullText( const wxString& aTextbase )
117 {
118  std::function<bool( wxString* )> wsResolver =
119  [ this ]( wxString* token ) -> bool
120  {
121  bool tokenUpdated = false;
122 
123  if( token->IsSameAs( wxT( "KICAD_VERSION" ) ) && PgmOrNull() )
124  {
125  // TODO: it'd be nice to get the Python script name/version here for when
126  // PgmOrNull() is null...
127 
128  *token = wxString::Format( wxT( "%s%s %s" ),
129  productName,
130  Pgm().App().GetAppName(),
131  GetBuildVersion() );
132  tokenUpdated = true;
133  }
134  else if( token->IsSameAs( wxT( "#" ) ) )
135  {
136  *token = wxString::Format( wxT( "%s" ), m_pageNumber );
137  tokenUpdated = true;
138  }
139  else if( token->IsSameAs( wxT( "##" ) ) )
140  {
141  *token = wxString::Format( wxT( "%d" ), m_sheetCount );
142  tokenUpdated = true;
143  }
144  else if( token->IsSameAs( wxT( "SHEETNAME" ) ) )
145  {
146  *token = m_sheetFullName;
147  tokenUpdated = true;
148  }
149  else if( token->IsSameAs( wxT( "FILENAME" ) ) )
150  {
151  wxFileName fn( m_fileName );
152  *token = fn.GetFullName();
153  tokenUpdated = true;
154  }
155  else if( token->IsSameAs( wxT( "PAPER" ) ) )
156  {
157  *token = m_paperFormat ? *m_paperFormat : wxString( "" );
158  tokenUpdated = true;
159  }
160  else if( token->IsSameAs( wxT( "LAYER" ) ) )
161  {
162  *token = m_sheetLayer ? *m_sheetLayer : wxString( "" );
163  tokenUpdated = true;
164  }
165  else if( m_titleBlock )
166  {
167  m_titleBlock->TextVarResolver( token, m_project );
168  // no need for tokenUpdated; TextVarResolver() did a full resolve
169  return true;
170  }
171 
172  if( tokenUpdated )
173  {
174  *token = ExpandTextVars( *token, m_project );
175  return true;
176  }
177 
178  return false;
179  };
180 
181  return ExpandTextVars( aTextbase, &wsResolver, nullptr, m_project );
182 }
183 
184 
185 bool KIGFX::WS_PAINTER::Draw( const VIEW_ITEM* aItem, int aLayer )
186 {
187  auto item = dynamic_cast<const EDA_ITEM*>( aItem );
188 
189  if( !item )
190  return false;
191 
192  switch( item->Type() )
193  {
194  case WSG_LINE_T: draw( (WS_DRAW_ITEM_LINE*) item, aLayer ); break;
195  case WSG_POLY_T: draw( (WS_DRAW_ITEM_POLYPOLYGONS*) item, aLayer ); break;
196  case WSG_RECT_T: draw( (WS_DRAW_ITEM_RECT*) item, aLayer ); break;
197  case WSG_TEXT_T: draw( (WS_DRAW_ITEM_TEXT*) item, aLayer ); break;
198  case WSG_BITMAP_T: draw( (WS_DRAW_ITEM_BITMAP*) item, aLayer ); break;
199  case WSG_PAGE_T: draw( (WS_DRAW_ITEM_PAGE*) item, aLayer ); break;
200  default: return false;
201  }
202 
203  return true;
204 }
205 
206 
207 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_LINE* aItem, int aLayer ) const
208 {
209  m_gal->SetIsStroke( true );
210  m_gal->SetIsFill( false );
211  m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
212  m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
213  m_gal->DrawLine( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
214 }
215 
216 
217 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_RECT* aItem, int aLayer ) const
218 {
219  m_gal->SetIsStroke( true );
220  m_gal->SetIsFill( false );
221  m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
222  m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
223  m_gal->DrawRectangle( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
224 }
225 
226 
227 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_POLYPOLYGONS* aItem, int aLayer ) const
228 {
229  m_gal->SetFillColor( m_renderSettings.GetColor( aItem, aLayer ) );
230  m_gal->SetIsFill( true );
231  m_gal->SetIsStroke( false );
232 
234 
235  for( int idx = 0; idx < item->GetPolygons().OutlineCount(); ++idx )
236  {
237  SHAPE_LINE_CHAIN& outline = item->GetPolygons().Outline( idx );
238  m_gal->DrawPolygon( outline );
239  }
240 }
241 
242 
243 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_TEXT* aItem, int aLayer ) const
244 {
245  VECTOR2D position( aItem->GetTextPos().x, aItem->GetTextPos().y );
246  int penWidth = std::max( aItem->GetEffectiveTextPenWidth(),
247  m_renderSettings.GetDefaultPenWidth() );
248 
249  m_gal->Save();
250  m_gal->Translate( position );
251  m_gal->Rotate( -aItem->GetTextAngle() * M_PI / 1800.0 );
252  m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
253  m_gal->SetLineWidth( penWidth );
254  m_gal->SetTextAttributes( aItem );
255  m_gal->SetIsFill( false );
256  m_gal->SetIsStroke( true );
257  m_gal->StrokeText( aItem->GetShownText(), VECTOR2D( 0, 0 ), 0.0 );
258  m_gal->Restore();
259 }
260 
261 
262 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_BITMAP* aItem, int aLayer ) const
263 {
264  m_gal->Save();
265  auto* bitmap = static_cast<WS_DATA_ITEM_BITMAP*>( aItem->GetPeer() );
266 
267  VECTOR2D position = aItem->GetPosition();
268  m_gal->Translate( position );
269 
270  // When the image scale factor is not 1.0, we need to modify the actual scale
271  // as the image scale factor is similar to a local zoom
272  double img_scale = bitmap->m_ImageBitmap->GetScale();
273 
274  if( img_scale != 1.0 )
275  m_gal->Scale( VECTOR2D( img_scale, img_scale ) );
276 
277  m_gal->DrawBitmap( *bitmap->m_ImageBitmap );
278 
279 #if 0 // For bounding box debug purpose only
280  EDA_RECT bbox = aItem->GetBoundingBox();
281  m_gal->SetIsFill( true );
282  m_gal->SetIsStroke( true );
283  m_gal->SetFillColor( COLOR4D( 1, 1, 1, 0.4 ) );
284  m_gal->SetStrokeColor( COLOR4D( 0, 0, 0, 1 ) );
285 
286  if( img_scale != 1.0 )
287  m_gal->Scale( VECTOR2D( 1.0, 1.0 ) );
288 
289  m_gal->DrawRectangle( VECTOR2D( bbox.GetOrigin() ) - position,
290  VECTOR2D( bbox.GetEnd() ) - position );
291 #endif
292 
293  m_gal->Restore();
294 }
295 
296 
297 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_PAGE* aItem, int aLayer ) const
298 {
299  VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
300  VECTOR2D end = VECTOR2D( aItem->GetPageSize().x,
301  aItem->GetPageSize().y );
302 
303  m_gal->SetIsStroke( true );
304 
305  // Use a gray color for the border color
306  m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
307  m_gal->SetIsFill( false );
308  m_gal->DrawRectangle( origin, end );
309 
310  // Draw the corner marker
311  double marker_size = aItem->GetMarkerSize();
312 
313  m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
314  VECTOR2D pos = VECTOR2D( aItem->GetMarkerPos().x, aItem->GetMarkerPos().y );
315 
316  // Draw a circle and a X
317  m_gal->DrawCircle( pos, marker_size );
318  m_gal->DrawLine( VECTOR2D( pos.x - marker_size, pos.y - marker_size),
319  VECTOR2D( pos.x + marker_size, pos.y + marker_size ) );
320  m_gal->DrawLine( VECTOR2D( pos.x + marker_size, pos.y - marker_size),
321  VECTOR2D( pos.x - marker_size, pos.y + marker_size ) );
322 }
323 
324 
325 void KIGFX::WS_PAINTER::DrawBorder( const PAGE_INFO* aPageInfo, int aScaleFactor ) const
326 {
327  VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
328  VECTOR2D end = VECTOR2D( aPageInfo->GetWidthMils() * aScaleFactor,
329  aPageInfo->GetHeightMils() * aScaleFactor );
330 
331  m_gal->SetIsStroke( true );
332  // Use a gray color for the border color
333  m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
334  m_gal->SetIsFill( false );
335  m_gal->DrawRectangle( origin, end );
336 }
const wxPoint & GetStart() const
Definition: ws_draw_item.h:219
WS_DATA_ITEM * GetPeer() const
Definition: ws_draw_item.h:63
const wxPoint & GetEnd() const
Definition: ws_draw_item.h:221
A rectangle with thick segment showing the page limits and a marker showing the coordinate origin.
Definition: ws_draw_item.h:252
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
int OutlineCount() const
Returns the number of outlines in the set
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
bool IsSelected() const
Definition: eda_item.h:171
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:58
const wxPoint & GetEnd() const
Definition: ws_draw_item.h:135
COLOR4D m_layerColors[LAYER_ID_COUNT]
int GetHeightMils() const
Definition: page_info.h:133
SHAPE_POLY_SET & GetPolygons()
Definition: ws_draw_item.h:170
double GetTextAngle() const
Definition: eda_text.h:181
Non filled rectangle with thick segment.
Definition: ws_draw_item.h:205
bool IsBrightened() const
Definition: eda_item.h:174
void LoadColors(const COLOR_SETTINGS *aSettings) override
Definition: ws_painter.cpp:56
Add new GAL layers here.
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:81
wxPoint GetPosition() const override
Definition: ws_draw_item.h:350
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
int GetEffectiveTextPenWidth(int aDefaultWidth=0) const
The EffectiveTextPenWidth uses the text thickness if > 1 or aDefaultWidth.
Definition: eda_text.cpp:157
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:257
const wxPoint GetEnd() const
Definition: eda_rect.h:108
VECTOR2< double > VECTOR2D
Definition: vector2d.h:629
void DrawBorder(const PAGE_INFO *aPageInfo, int aScaleFactor) const
Definition: ws_painter.cpp:325
PGM_BASE * PgmOrNull()
similar to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Definition: cvpcb.cpp:112
wxString GetBuildVersion()
Get the full KiCad version string.
virtual void update()
Precalculates extra colors for layers (e.g.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
const wxPoint GetOrigin() const
Definition: eda_rect.h:106
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:53
const wxPoint & GetStart() const
Definition: ws_draw_item.h:133
void draw(const WS_DRAW_ITEM_LINE *aItem, int aLayer) const
Definition: ws_painter.cpp:207
Definition: color4d.h:59
virtual int GetPenWidth() const
Definition: ws_draw_item.h:70
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
COLOR4D GetColor(int aLayer) const
wxSize GetPageSize() const
Definition: ws_draw_item.h:265
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:378
see class PGM_BASE
wxString BuildFullText(const wxString &aTextbase)
Return the full text corresponding to the aTextbase, after replacing format symbols by the correspond...
Definition: ws_painter.cpp:116
SHAPE_LINE_CHAIN.
Handle the component boundary box.
Definition: eda_rect.h:42
double GetMarkerSize() const
Definition: ws_draw_item.h:268
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:148
virtual COLOR4D GetColor(const VIEW_ITEM *aItem, int aLayer) const override
Returns the color that should be used to draw the specific VIEW_ITEM on the specific layer using curr...
Definition: ws_painter.cpp:70
Color settings are a bit different than most of the settings objects in that there can be more than o...
const wxPoint & GetTextPos() const
Definition: eda_text.h:254
A graphic text.
Definition: ws_draw_item.h:297
BASE_SCREEN class implementation.
virtual bool Draw(const VIEW_ITEM *, int) override
Takes an instance of VIEW_ITEM and passes it to a function that knows how to draw the item.
Definition: ws_painter.cpp:185
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:141
static const wxString productName
Definition: ws_painter.cpp:42
const wxPoint & GetMarkerPos() const
Definition: ws_draw_item.h:266
int GetWidthMils() const
Definition: page_info.h:130
static void GetTextVars(wxArrayString *aVars)
Definition: ws_painter.cpp:88
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98