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 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>
33 #include <painter.h>
35 #include <class_page_info.h>
36 #include <view/view.h>
37 
38 using namespace KIGFX;
39 
40 WORKSHEET_VIEWITEM::WORKSHEET_VIEWITEM( const PAGE_INFO* aPageInfo, const TITLE_BLOCK* aTitleBlock ) :
41  EDA_ITEM( NOT_USED ), // this item is never added to a BOARD so it needs no type
42  m_titleBlock( aTitleBlock ), m_pageInfo( aPageInfo ), m_sheetNumber( 1 ), m_sheetCount( 1 ) {}
43 
44 
46 {
47  m_pageInfo = aPageInfo;
48 }
49 
50 
52 {
53  m_titleBlock = aTitleBlock;
54 }
55 
56 
58 {
59  BOX2I bbox;
60 
61  if( m_pageInfo != NULL )
62  {
63  bbox.SetOrigin( VECTOR2I( 0, 0 ) );
64  bbox.SetEnd( VECTOR2I( m_pageInfo->GetWidthMils() * 25400,
65  m_pageInfo->GetHeightMils() * 25400 ) );
66  }
67  else
68  {
69  bbox.SetMaximum();
70  }
71 
72  return bbox;
73 }
74 
75 
76 void WORKSHEET_VIEWITEM::ViewDraw( int aLayer, VIEW* aView ) const
77 {
78  auto gal = aView->GetGAL();
79  auto settings = aView->GetPainter()->GetSettings();
80  wxString fileName( m_fileName.c_str(), wxConvUTF8 );
81  wxString sheetName( m_sheetName.c_str(), wxConvUTF8 );
82  WS_DRAW_ITEM_LIST drawList;
83 
84  drawList.SetPenSize( settings->GetWorksheetLineWidth() );
85  // Sorry, but I don't get this multi #ifdef from include/convert_to_biu.h, so here goes a magic
86  // number. IU_PER_MILS should be 25400 (as in a different compilation unit), but somehow
87  // it equals 1 in this case..
88  drawList.SetMilsToIUfactor( 25400 /* IU_PER_MILS */ );
89  drawList.SetSheetNumber( m_sheetNumber );
90  drawList.SetSheetCount( m_sheetCount );
91  drawList.SetFileName( fileName );
92  drawList.SetSheetName( sheetName );
93 
94  COLOR4D color = settings->GetColor( this, aLayer );
95  drawList.BuildWorkSheetGraphicList( *m_pageInfo, *m_titleBlock, color, color );
96 
97  // Draw all the components that make the page layout
98  WS_DRAW_ITEM_BASE* item = drawList.GetFirst();
99  while( item )
100  {
101  switch( item->GetType() )
102  {
104  draw( static_cast<const WS_DRAW_ITEM_LINE*>( item ), gal );
105  break;
106 
108  draw( static_cast<const WS_DRAW_ITEM_RECT*>( item ), gal );
109  break;
110 
112  draw( static_cast<const WS_DRAW_ITEM_POLYGON*>( item ), gal );
113  break;
114 
116  draw( static_cast<const WS_DRAW_ITEM_TEXT*>( item ), gal );
117  break;
118 
120  break;
121  }
122 
123  item = drawList.GetNext();
124  }
125 
126  // Draw gray line that outlines the sheet size
127  drawBorder( gal );
128 }
129 
130 
131 void WORKSHEET_VIEWITEM::ViewGetLayers( int aLayers[], int& aCount ) const
132 {
133  aCount = 1;
134  aLayers[0] = LAYER_WORKSHEET;
135 }
136 
137 
138 void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_LINE* aItem, GAL* aGal ) const
139 {
140  aGal->SetIsStroke( true );
141  aGal->SetIsFill( false );
142  aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
143  aGal->SetLineWidth( aItem->GetPenWidth() );
144  aGal->DrawLine( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
145 }
146 
147 
148 void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_RECT* aItem, GAL* aGal ) const
149 {
150  aGal->SetIsStroke( true );
151  aGal->SetIsFill( false );
152  aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
153  aGal->SetLineWidth( aItem->GetPenWidth() );
154  aGal->DrawRectangle( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
155 }
156 
157 
158 void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_POLYGON* aItem, GAL* aGal ) const
159 {
160  std::deque<VECTOR2D> corners;
161  for( wxPoint point : aItem->m_Corners )
162  {
163  corners.push_back( VECTOR2D( point ) );
164  }
165 
166  if( aItem->IsFilled() )
167  {
168  aGal->SetFillColor( COLOR4D( aItem->GetColor() ) );
169  aGal->SetIsFill( true );
170  aGal->SetIsStroke( false );
171  aGal->DrawPolygon( corners );
172  }
173  else
174  {
175  aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
176  aGal->SetIsFill( false );
177  aGal->SetIsStroke( true );
178  aGal->SetLineWidth( aItem->GetPenWidth() );
179  aGal->DrawPolyline( corners );
180  }
181 }
182 
183 
184 void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_TEXT* aItem, GAL* aGal ) const
185 {
186  VECTOR2D position( aItem->GetTextPos().x, aItem->GetTextPos().y );
187 
188  aGal->Save();
189  aGal->Translate( position );
190  aGal->Rotate( -aItem->GetTextAngle() * M_PI / 1800.0 );
191  aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
192  aGal->SetLineWidth( aItem->GetThickness() );
193  aGal->SetTextAttributes( aItem );
194  aGal->StrokeText( aItem->GetShownText(), VECTOR2D( 0, 0 ), 0.0 );
195  aGal->Restore();
196 }
197 
198 
200 {
201  VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
202  VECTOR2D end = VECTOR2D( m_pageInfo->GetWidthMils() * 25400,
203  m_pageInfo->GetHeightMils() * 25400 );
204 
205  aGal->SetIsStroke( true );
206  // Use a gray color for the border color
207  aGal->SetStrokeColor( COLOR4D( 0.4, 0.4, 0.4, 1.0 ) );
208  aGal->SetIsFill( false );
209  aGal->DrawRectangle( origin, end );
210 }
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 & GetTextPos() const
Definition: eda_text.h:224
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:92
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
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590
Class TITLE_BLOCK holds the information shown in the lower right corner of a plot, printout, or editing view.
double GetTextAngle() const
Definition: eda_text.h:164
int GetThickness() const
Function GetThickness returns pen width.
Definition: eda_text.h:154
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:135
Class that handles properties and drawing of worksheet layout.
std::string m_fileName
File name displayed in the title block.
virtual void SetLineWidth(double aLineWidth)
Set the line width.
VECTOR2< double > VECTOR2D
Definition: vector2d.h:589
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:177
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
virtual void StrokeText(const wxString &aText, const VECTOR2D &aPosition, double aRotationAngle)
Draws a vector type text using preloaded Newstroke font.
void SetMaximum()
Definition: box2.h:61
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:196
int GetWidthMils() const
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:213
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:198
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:165
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 ...
virtual void Save()
Save the context.
WORKSHEET_VIEWITEM(const PAGE_INFO *aPageInfo, const TITLE_BLOCK *aTitleBlock)
Class VIEW.
Definition: view.h:58
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
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