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 <fctsys.h>
27 #include <pgm_base.h>
28 #include <gr_basic.h>
29 #include <common.h>
30 #include <base_screen.h>
31 #include <eda_draw_frame.h>
32 #include <title_block.h>
33 #include <build_version.h>
35 #include <ws_draw_item.h>
37 
38 #include <ws_painter.h>
39 #include <ws_data_item.h>
40 
41 using namespace KIGFX;
42 
43 static const wxString productName = wxT( "KiCad E.D.A. " );
44 
46 {
47  m_backgroundColor = COLOR4D( 1.0, 1.0, 1.0, 1.0 );
50  m_brightenedColor = COLOR4D( 0.0, 1.0, 0.0, 0.9 );
51  m_pageBorderColor = COLOR4D( 0.4, 0.4, 0.4, 1.0 );
52 
53  update();
54 }
55 
56 
58 {
59  for( int layer = SCH_LAYER_ID_START; layer < SCH_LAYER_ID_END; layer ++)
60  m_layerColors[ layer ] = aSettings->GetColor( layer );
61 
62  for( int layer = GAL_LAYER_ID_START; layer < GAL_LAYER_ID_END; layer ++)
63  m_layerColors[ layer ] = aSettings->GetColor( layer );
64 
68 }
69 
70 
71 COLOR4D WS_RENDER_SETTINGS::GetColor( const VIEW_ITEM* aItem, int aLayer ) const
72 {
73  const EDA_ITEM* item = dynamic_cast<const EDA_ITEM*>( aItem );
74 
75  if( item )
76  {
77  // Selection disambiguation
78  if( item->IsBrightened() )
79  return m_brightenedColor;
80 
81  if( item->IsSelected() )
82  return m_selectedColor;
83  }
84 
85  return m_normalColor;
86 }
87 
88 
89 void WS_DRAW_ITEM_LIST::GetTextVars( wxArrayString* aVars )
90 {
91  aVars->push_back( wxT( "KICAD_VERSION" ) );
92  aVars->push_back( wxT( "#" ) );
93  aVars->push_back( wxT( "##" ) );
94  aVars->push_back( wxT( "SHEETNAME" ) );
95  aVars->push_back( wxT( "FILENAME" ) );
96  aVars->push_back( wxT( "PAPER" ) );
97  aVars->push_back( wxT( "LAYER" ) );
98  aVars->push_back( wxT( "ISSUE_DATE" ) );
99  aVars->push_back( wxT( "REVISION" ) );
100  aVars->push_back( wxT( "TITLE" ) );
101  aVars->push_back( wxT( "COMPANY" ) );
102  aVars->push_back( wxT( "COMMENT0" ) );
103  aVars->push_back( wxT( "COMMENT1" ) );
104  aVars->push_back( wxT( "COMMENT2" ) );
105  aVars->push_back( wxT( "COMMENT3" ) );
106  aVars->push_back( wxT( "COMMENT4" ) );
107  aVars->push_back( wxT( "COMMENT5" ) );
108  aVars->push_back( wxT( "COMMENT6" ) );
109  aVars->push_back( wxT( "COMMENT7" ) );
110  aVars->push_back( wxT( "COMMENT8" ) );
111  aVars->push_back( wxT( "COMMENT9" ) );
112 }
113 
114 
115 // returns the full text corresponding to the aTextbase,
116 // after replacing format symbols by the corresponding value
117 wxString WS_DRAW_ITEM_LIST::BuildFullText( const wxString& aTextbase )
118 {
119  std::function<bool( wxString* )> wsResolver =
120  [ this ]( wxString* token ) -> bool
121  {
122  bool tokenUpdated = false;
123 
124  if( token->IsSameAs( wxT( "KICAD_VERSION" ) ) )
125  {
126  *token = wxString::Format( wxT( "%s%s %s" ),
127  productName,
128  Pgm().App().GetAppName(),
129  GetBuildVersion() );
130  tokenUpdated = true;
131  }
132  else if( token->IsSameAs( wxT( "#" ) ) )
133  {
134  *token = wxString::Format( wxT( "%d" ), m_sheetNumber );
135  tokenUpdated = true;
136  }
137  else if( token->IsSameAs( wxT( "##" ) ) )
138  {
139  *token = wxString::Format( wxT( "%d" ), m_sheetCount );
140  tokenUpdated = true;
141  }
142  else if( token->IsSameAs( wxT( "SHEETNAME" ) ) )
143  {
144  *token = m_sheetFullName;
145  tokenUpdated = true;
146  }
147  else if( token->IsSameAs( wxT( "FILENAME" ) ) )
148  {
149  wxFileName fn( m_fileName );
150  *token = fn.GetFullName();
151  tokenUpdated = true;
152  }
153  else if( token->IsSameAs( wxT( "PAPER" ) ) )
154  {
155  *token = m_paperFormat ? *m_paperFormat : wxString( "" );
156  tokenUpdated = true;
157  }
158  else if( token->IsSameAs( wxT( "LAYER" ) ) )
159  {
160  *token = m_sheetLayer ? *m_sheetLayer : wxString( "" );
161  tokenUpdated = true;
162  }
163  else if( token->IsSameAs( wxT( "ISSUE_DATE" ) ) )
164  {
165  *token = m_titleBlock ? m_titleBlock->GetDate() : wxString( "" );
166  tokenUpdated = true;
167  }
168  else if( token->IsSameAs( wxT( "REVISION" ) ) )
169  {
170  *token = m_titleBlock ? m_titleBlock->GetRevision() : wxString( "" );
171  tokenUpdated = true;
172  }
173  else if( token->IsSameAs( wxT( "TITLE" ) ) )
174  {
175  *token = m_titleBlock ? m_titleBlock->GetTitle() : wxString( "" );
176  tokenUpdated = true;
177  }
178  else if( token->IsSameAs( wxT( "COMPANY" ) ) )
179  {
180  *token = m_titleBlock ? m_titleBlock->GetCompany() : wxString( "" );
181  tokenUpdated = true;
182  }
183  else if( token->Left( token->Len() - 1 ).IsSameAs( wxT( "COMMENT" ) ) )
184  {
185  wxChar c = token->Last();
186 
187  switch( c )
188  {
189  case '0':
190  case '1':
191  case '2':
192  case '3':
193  case '4':
194  case '5':
195  case '6':
196  case '7':
197  case '8':
198  case '9':
199  *token = m_titleBlock ? m_titleBlock->GetComment( c - '0' )
200  : wxString( "" );
201  tokenUpdated = true;
202  }
203  }
204 
205  if( tokenUpdated )
206  {
207  *token = ExpandTextVars( *token, nullptr, m_project );
208  return true;
209  }
210 
211  return false;
212  };
213 
214  return ExpandTextVars( aTextbase, &wsResolver, m_project );
215 }
216 
217 
218 bool KIGFX::WS_PAINTER::Draw( const VIEW_ITEM* aItem, int aLayer )
219 {
220  auto item = dynamic_cast<const EDA_ITEM*>( aItem );
221 
222  if( !item )
223  return false;
224 
225  switch( item->Type() )
226  {
227  case WSG_LINE_T: draw( (WS_DRAW_ITEM_LINE*) item, aLayer ); break;
228  case WSG_POLY_T: draw( (WS_DRAW_ITEM_POLYPOLYGONS*) item, aLayer ); break;
229  case WSG_RECT_T: draw( (WS_DRAW_ITEM_RECT*) item, aLayer ); break;
230  case WSG_TEXT_T: draw( (WS_DRAW_ITEM_TEXT*) item, aLayer ); break;
231  case WSG_BITMAP_T: draw( (WS_DRAW_ITEM_BITMAP*) item, aLayer ); break;
232  case WSG_PAGE_T: draw( (WS_DRAW_ITEM_PAGE*) item, aLayer ); break;
233  default: return false;
234  }
235 
236  return true;
237 }
238 
239 
240 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_LINE* aItem, int aLayer ) const
241 {
242  m_gal->SetIsStroke( true );
243  m_gal->SetIsFill( false );
244  m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
245  m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
246  m_gal->DrawLine( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
247 }
248 
249 
250 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_RECT* aItem, int aLayer ) const
251 {
252  m_gal->SetIsStroke( true );
253  m_gal->SetIsFill( false );
254  m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
255  m_gal->SetLineWidth( std::max( aItem->GetPenWidth(), m_renderSettings.GetDefaultPenWidth() ) );
256  m_gal->DrawRectangle( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
257 }
258 
259 
260 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_POLYPOLYGONS* aItem, int aLayer ) const
261 {
262  m_gal->SetFillColor( m_renderSettings.GetColor( aItem, aLayer ) );
263  m_gal->SetIsFill( true );
264  m_gal->SetIsStroke( false );
265 
267 
268  for( int idx = 0; idx < item->GetPolygons().OutlineCount(); ++idx )
269  {
270  SHAPE_LINE_CHAIN& outline = item->GetPolygons().Outline( idx );
271  m_gal->DrawPolygon( outline );
272  }
273 }
274 
275 
276 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_TEXT* aItem, int aLayer ) const
277 {
278  VECTOR2D position( aItem->GetTextPos().x, aItem->GetTextPos().y );
279  int penWidth = std::max( aItem->GetEffectiveTextPenWidth(),
280  m_renderSettings.GetDefaultPenWidth() );
281 
282  m_gal->Save();
283  m_gal->Translate( position );
284  m_gal->Rotate( -aItem->GetTextAngle() * M_PI / 1800.0 );
285  m_gal->SetStrokeColor( m_renderSettings.GetColor( aItem, aLayer ) );
286  m_gal->SetLineWidth( penWidth );
287  m_gal->SetTextAttributes( aItem );
288  m_gal->SetIsFill( false );
289  m_gal->SetIsStroke( true );
290  m_gal->StrokeText( aItem->GetShownText(), VECTOR2D( 0, 0 ), 0.0 );
291  m_gal->Restore();
292 }
293 
294 
295 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_BITMAP* aItem, int aLayer ) const
296 {
297  m_gal->Save();
298  auto* bitmap = static_cast<WS_DATA_ITEM_BITMAP*>( aItem->GetPeer() );
299 
300  VECTOR2D position = aItem->GetPosition();
301  m_gal->Translate( position );
302 
303  // When the image scale factor is not 1.0, we need to modify the actual scale
304  // as the image scale factor is similar to a local zoom
305  double img_scale = bitmap->m_ImageBitmap->GetScale();
306 
307  if( img_scale != 1.0 )
308  m_gal->Scale( VECTOR2D( img_scale, img_scale ) );
309 
310  m_gal->DrawBitmap( *bitmap->m_ImageBitmap );
311 
312 #if 0 // For bounding box debug purpose only
313  EDA_RECT bbox = aItem->GetBoundingBox();
314  m_gal->SetIsFill( true );
315  m_gal->SetIsStroke( true );
316  m_gal->SetFillColor( COLOR4D( 1, 1, 1, 0.4 ) );
317  m_gal->SetStrokeColor( COLOR4D( 0, 0, 0, 1 ) );
318 
319  if( img_scale != 1.0 )
320  m_gal->Scale( VECTOR2D( 1.0, 1.0 ) );
321 
322  m_gal->DrawRectangle( VECTOR2D( bbox.GetOrigin() ) - position,
323  VECTOR2D( bbox.GetEnd() ) - position );
324 #endif
325 
326  m_gal->Restore();
327 }
328 
329 
330 void KIGFX::WS_PAINTER::draw( const WS_DRAW_ITEM_PAGE* aItem, int aLayer ) const
331 {
332  VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
333  VECTOR2D end = VECTOR2D( aItem->GetPageSize().x,
334  aItem->GetPageSize().y );
335 
336  m_gal->SetIsStroke( true );
337 
338  // Use a gray color for the border color
339  m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
340  m_gal->SetIsFill( false );
341  m_gal->DrawRectangle( origin, end );
342 
343  // Draw the corner marker
344  double marker_size = aItem->GetMarkerSize();
345 
346  m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
347  VECTOR2D pos = VECTOR2D( aItem->GetMarkerPos().x, aItem->GetMarkerPos().y );
348 
349  // Draw a cirle and a X
350  m_gal->DrawCircle( pos, marker_size );
351  m_gal->DrawLine( VECTOR2D( pos.x - marker_size, pos.y - marker_size),
352  VECTOR2D( pos.x + marker_size, pos.y + marker_size ) );
353  m_gal->DrawLine( VECTOR2D( pos.x + marker_size, pos.y - marker_size),
354  VECTOR2D( pos.x - marker_size, pos.y + marker_size ) );
355 }
356 
357 
358 void KIGFX::WS_PAINTER::DrawBorder( const PAGE_INFO* aPageInfo, int aScaleFactor ) const
359 {
360  VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
361  VECTOR2D end = VECTOR2D( aPageInfo->GetWidthMils() * aScaleFactor,
362  aPageInfo->GetHeightMils() * aScaleFactor );
363 
364  m_gal->SetIsStroke( true );
365  // Use a gray color for the border color
366  m_gal->SetStrokeColor( m_renderSettings.m_pageBorderColor );
367  m_gal->SetIsFill( false );
368  m_gal->DrawRectangle( origin, end );
369 }
const wxPoint & GetStart() const
Definition: ws_draw_item.h:210
WS_DATA_ITEM * GetPeer() const
Definition: ws_draw_item.h:70
const wxPoint & GetEnd() const
Definition: ws_draw_item.h:212
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns 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: base_struct.h:203
const wxPoint & GetEnd() const
Definition: ws_draw_item.h:132
COLOR4D m_layerColors[LAYER_ID_COUNT]
int GetHeightMils() const
Definition: page_info.h:141
SHAPE_POLY_SET & GetPolygons()
Definition: ws_draw_item.h:174
double GetTextAngle() const
Definition: eda_text.h:174
bool IsBrightened() const
Definition: base_struct.h:205
void LoadColors(const COLOR_SETTINGS *aSettings) override
Definition: ws_painter.cpp:57
Add new GAL layers here.
VIEW_ITEM - is an abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:85
wxPoint GetPosition() const override
Definition: ws_draw_item.h:330
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
Function Brightened Returns a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:259
const wxPoint GetEnd() const
Definition: eda_rect.h:116
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
void DrawBorder(const PAGE_INFO *aPageInfo, int aScaleFactor) const
Definition: ws_painter.cpp:358
wxString GetBuildVersion()
Get the full KiCad version string.
virtual void update()
Function 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:114
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
const wxPoint & GetStart() const
Definition: ws_draw_item.h:130
void draw(const WS_DRAW_ITEM_LINE *aItem, int aLayer) const
Definition: ws_painter.cpp:240
Definition: color4d.h:59
virtual int GetPenWidth() const
Definition: ws_draw_item.h:77
wxString ExpandTextVars(const wxString &aSource, const std::function< bool(wxString *)> *aLocalResolver, const PROJECT *aProject, const std::function< bool(wxString *)> *aFallbackResolver)
Expand '${var-name}' templates in text.
Definition: common.cpp:384
COLOR4D GetColor(int aLayer) const
wxSize GetPageSize() const
Definition: ws_draw_item.h:252
VECTOR2< T > Rotate(double aAngle) const
Function Rotate rotates the vector by a given angle.
Definition: vector2d.h:377
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
wxString BuildFullText(const wxString &aTextbase)
Function BuildFullText returns the full text corresponding to the aTextbase, after replacing format s...
Definition: ws_painter.cpp:117
SHAPE_LINE_CHAIN.
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
double GetMarkerSize() const
Definition: ws_draw_item.h:255
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
The common library.
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:71
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:248
BASE_SCREEN class implementation.
virtual bool Draw(const VIEW_ITEM *, int) override
Function Draw Takes an instance of VIEW_ITEM and passes it to a function that know how to draw the it...
Definition: ws_painter.cpp:218
virtual wxString GetShownText(int aDepth=0) const
Return the string actually shown after processing of the base text.
Definition: eda_text.h:134
static const wxString productName
Definition: ws_painter.cpp:43
const wxPoint & GetMarkerPos() const
Definition: ws_draw_item.h:253
int GetWidthMils() const
Definition: page_info.h:138
static void GetTextVars(wxArrayString *aVars)
Definition: ws_painter.cpp:89
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99