KiCad PCB EDA Suite
worksheet_viewitem.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) 2013-2018 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 
30 #include <worksheet_viewitem.h>
32 #include <worksheet_dataitem.h>
34 #include <painter.h>
36 #include <page_info.h>
37 #include <view/view.h>
38 
39 using namespace KIGFX;
40 
41 WORKSHEET_VIEWITEM::WORKSHEET_VIEWITEM( int aMils2IUscalefactor,
42  const PAGE_INFO* aPageInfo, const TITLE_BLOCK* aTitleBlock ) :
43  EDA_ITEM( NOT_USED ), // this item is never added to a BOARD so it needs no type
44  m_mils2IUscalefactor( aMils2IUscalefactor ),
45  m_titleBlock( aTitleBlock ), m_pageInfo( aPageInfo ), m_sheetNumber( 1 ), m_sheetCount( 1 ) {}
46 
47 
49 {
50  m_pageInfo = aPageInfo;
51 }
52 
53 
55 {
56  m_titleBlock = aTitleBlock;
57 }
58 
59 
61 {
62  BOX2I bbox;
63 
64  if( m_pageInfo != NULL )
65  {
66  bbox.SetOrigin( VECTOR2I( 0, 0 ) );
69  }
70  else
71  {
72  bbox.SetMaximum();
73  }
74 
75  return bbox;
76 }
77 
78 
79 void WORKSHEET_VIEWITEM::ViewDraw( int aLayer, VIEW* aView ) const
80 {
81  auto gal = aView->GetGAL();
82  auto settings = aView->GetPainter()->GetSettings();
83  wxString fileName( m_fileName.c_str(), wxConvUTF8 );
84  wxString sheetName( m_sheetName.c_str(), wxConvUTF8 );
85  WS_DRAW_ITEM_LIST drawList;
86 
87  drawList.SetPenSize( settings->GetWorksheetLineWidth() );
88  // Adjust the scaling factor for worksheet items:
89  // worksheet items coordinates and sizes are stored in mils,
90  // and must be scaled to the same units as the caller
91  drawList.SetMilsToIUfactor( m_mils2IUscalefactor );
92  drawList.SetSheetNumber( m_sheetNumber );
93  drawList.SetSheetCount( m_sheetCount );
94  drawList.SetFileName( fileName );
95  drawList.SetSheetName( sheetName );
96 
97  COLOR4D color = settings->GetColor( this, aLayer );
98  drawList.BuildWorkSheetGraphicList( *m_pageInfo, *m_titleBlock, color, color );
99 
100  // Draw the title block normally even if the view is flipped
101  bool flipped = gal->IsFlippedX();
102 
103  if( flipped )
104  {
105  gal->Save();
106  gal->Translate( VECTOR2D( m_pageInfo->GetWidthMils() * m_mils2IUscalefactor, 0 ) );
107  gal->Scale( VECTOR2D( -1.0, 1.0 ) );
108  }
109 
110  // Draw all the components that make the page layout
111  WS_DRAW_ITEM_BASE* item = drawList.GetFirst();
112  while( item )
113  {
114  switch( item->GetType() )
115  {
117  draw( static_cast<const WS_DRAW_ITEM_LINE*>( item ), gal );
118  break;
119 
121  draw( static_cast<const WS_DRAW_ITEM_RECT*>( item ), gal );
122  break;
123 
125  draw( static_cast<const WS_DRAW_ITEM_POLYGON*>( item ), gal );
126  break;
127 
129  draw( static_cast<const WS_DRAW_ITEM_TEXT*>( item ), gal );
130  break;
131 
133  draw( static_cast<const WS_DRAW_ITEM_BITMAP*>( item ), gal );
134  break;
135  }
136 
137  item = drawList.GetNext();
138  }
139 
140  // Draw gray line that outlines the sheet size
141  if( settings->GetShowPageLimits() )
142  drawBorder( gal );
143 
144  if( flipped )
145  gal->Restore();
146 }
147 
148 
149 void WORKSHEET_VIEWITEM::ViewGetLayers( int aLayers[], int& aCount ) const
150 {
151  aCount = 1;
152  aLayers[0] = LAYER_WORKSHEET;
153 }
154 
155 
156 void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_LINE* aItem, GAL* aGal ) const
157 {
158  aGal->SetIsStroke( true );
159  aGal->SetIsFill( false );
160  aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
161  aGal->SetLineWidth( aItem->GetPenWidth() );
162  aGal->DrawLine( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
163 }
164 
165 
166 void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_RECT* aItem, GAL* aGal ) const
167 {
168  aGal->SetIsStroke( true );
169  aGal->SetIsFill( false );
170  aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
171  aGal->SetLineWidth( aItem->GetPenWidth() );
172  aGal->DrawRectangle( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
173 }
174 
175 
176 void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_POLYGON* aItem, GAL* aGal ) const
177 {
178  std::deque<VECTOR2D> corners;
179  for( wxPoint point : aItem->m_Corners )
180  {
181  corners.push_back( VECTOR2D( point ) );
182  }
183 
184  if( aItem->IsFilled() )
185  {
186  aGal->SetFillColor( COLOR4D( aItem->GetColor() ) );
187  aGal->SetIsFill( true );
188  aGal->SetIsStroke( false );
189  aGal->DrawPolygon( corners );
190  }
191  else
192  {
193  aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
194  aGal->SetIsFill( false );
195  aGal->SetIsStroke( true );
196  aGal->SetLineWidth( aItem->GetPenWidth() );
197  aGal->DrawPolyline( corners );
198  }
199 }
200 
201 
202 void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_TEXT* aItem, GAL* aGal ) const
203 {
204  VECTOR2D position( aItem->GetTextPos().x, aItem->GetTextPos().y );
205 
206  aGal->Save();
207  aGal->Translate( position );
208  aGal->Rotate( -aItem->GetTextAngle() * M_PI / 1800.0 );
209  aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
210  aGal->SetLineWidth( aItem->GetThickness() );
211  aGal->SetTextAttributes( aItem );
212  aGal->StrokeText( aItem->GetShownText(), VECTOR2D( 0, 0 ), 0.0 );
213  aGal->Restore();
214 }
215 
216 
217 void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_BITMAP* aItem, GAL* aGal ) const
218 {
219  aGal->Save();
220  VECTOR2D position = aItem->GetPosition();
221  aGal->Translate( position );
222  WORKSHEET_DATAITEM_BITMAP* parent = static_cast<WORKSHEET_DATAITEM_BITMAP*>( aItem->GetParent() );
223 
224  // When the image scale factor is not 1.0, we need to modify the actual scale
225  // as the image scale factor is similar to a local zoom
226  double img_scale = parent->m_ImageBitmap->GetScale();
227 
228  if( img_scale != 1.0 )
229  aGal->Scale( VECTOR2D( img_scale, img_scale ) );
230 
231  aGal->DrawBitmap( *parent->m_ImageBitmap );
232  aGal->Restore();
233 }
234 
235 
237 {
238  VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
241 
242  aGal->SetIsStroke( true );
243  // Use a gray color for the border color
244  aGal->SetStrokeColor( COLOR4D( 0.4, 0.4, 0.4, 1.0 ) );
245  aGal->SetIsFill( false );
246  aGal->DrawRectangle( origin, end );
247 }
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
virtual void DrawPolyline(const std::deque< VECTOR2D > &aPointList)
Draw a polyline.
std::string m_sheetName
Sheet name displayed in the title block.
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a rectangle.
void SetPenSize(int aPenSize)
Function SetPenSize Set the default pen size to draw/plot lines and texts.
const wxPoint GetPosition() const
const wxPoint & GetTextPos() const
Definition: eda_text.h:237
void SetPageInfo(const PAGE_INFO *aPageInfo)
Function SetPageInfo() Changes the PAGE_INFO object used to draw the worksheet.
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
const BOX2I ViewBBox() const override
Function ViewBBox() returns the bounding box of the item covering all its layers. ...
virtual void SetTextAttributes(const EDA_TEXT *aText)
Loads attributes of the given text (bold/italic/underline/mirrored and so on).
the 3d code uses this value
Definition: typeinfo.h:80
int color
Definition: DXF_plotter.cpp:62
virtual void DrawBitmap(const BITMAP_BASE &aBitmap)
Draw a bitmap image.
void SetTitleBlock(const TITLE_BLOCK *aTitleBlock)
Function SetTitleBlock() Changes the TITLE_BLOCK object used to draw the worksheet.
int m_sheetCount
Sheets count number displayed in the title block.
void ViewGetLayers(int aLayers[], int &aCount) const override
Function ViewGetLayers() Returns the all the layers within the VIEW the object is painted on...
void draw(const WS_DRAW_ITEM_LINE *aItem, GAL *aGal) const
WORKSHEET_VIEWITEM(int aMils2IUscalefactor, const PAGE_INFO *aPageInfo, const TITLE_BLOCK *aTitleBlock)
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
Class TITLE_BLOCK holds the information shown in the lower right corner of a plot, printout, or editing view.
Definition: title_block.h:40
double GetTextAngle() const
Definition: eda_text.h:177
virtual void SetLineWidth(float aLineWidth)
Set the line width.
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:167
virtual void Rotate(double aAngle)
Rotate the context.
virtual wxString GetShownText() const
Returns the string actually shown after processing of the base text.
Definition: eda_text.h:148
Class that handles properties and drawing of worksheet layout.
std::string m_fileName
File name displayed in the title block.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:586
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
void drawBorder(GAL *aGal) const
Draws a border that determines the page size.
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
Definition: page_info.h:54
virtual void StrokeText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draws a vector type text using preloaded Newstroke font.
WORKSHEET_DATAITEM * GetParent() const
void SetMaximum()
Definition: box2.h:71
description of graphic items and texts to build a title block
const wxPoint & GetEnd() const
COLOR4D GetColor() const
void ViewDraw(int aLayer, VIEW *aView) const override
Function ViewDraw() Draws the parts of the object belonging to layer aLayer.
std::vector< wxPoint > m_Corners
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
int GetWidthMils() const
Definition: page_info.h:137
double GetScale() const
Definition: bitmap_base.h:84
virtual void Scale(const VECTOR2D &aScale)
Scale the context.
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:223
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void Restore()
Restore the context.
const PAGE_INFO * m_pageInfo
Worksheet page information.
virtual void DrawPolygon(const std::deque< VECTOR2D > &aPointList)
Draw a polygon.
Board layer functions and definitions.
int m_sheetNumber
Sheet number displayed in the title block.
void SetOrigin(const Vec &pos)
Definition: box2.h:208
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
const TITLE_BLOCK * m_titleBlock
Title block that contains properties of the title block displayed in the worksheet.
classes and function to generate graphics to plt or draw titles blocks and frame references ...
int m_mils2IUscalefactor
the factor between mils (units used in worksheet and internal units) it is the value IU_PER_MILS used...
virtual void Save()
Save the context.
Class VIEW.
Definition: view.h:61
WS_DRAW_TYPE GetType() const
virtual void Translate(const VECTOR2D &aTranslation)
Translate the context.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
int GetHeightMils() const
Definition: page_info.h:140
Class GAL is the abstract interface for drawing on a 2D-surface.
const wxPoint & GetStart() const
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39