You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
7.6 KiB

  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. /**
  25. * @file worksheet_viewitem.cpp
  26. * @brief Class that handles properties and drawing of worksheet layout.
  27. */
  28. #include <worksheet_viewitem.h>
  29. #include <worksheet_shape_builder.h>
  30. #include <worksheet_dataitem.h>
  31. #include <gal/graphics_abstraction_layer.h>
  32. #include <painter.h>
  33. #include <layers_id_colors_and_visibility.h>
  34. #include <page_info.h>
  35. #include <view/view.h>
  36. using namespace KIGFX;
  37. WORKSHEET_VIEWITEM::WORKSHEET_VIEWITEM( int aMils2IUscalefactor,
  38. const PAGE_INFO* aPageInfo, const TITLE_BLOCK* aTitleBlock ) :
  39. EDA_ITEM( NOT_USED ), // this item is never added to a BOARD so it needs no type
  40. m_mils2IUscalefactor( aMils2IUscalefactor ),
  41. m_titleBlock( aTitleBlock ), m_pageInfo( aPageInfo ), m_sheetNumber( 1 ), m_sheetCount( 1 ) {}
  42. void WORKSHEET_VIEWITEM::SetPageInfo( const PAGE_INFO* aPageInfo )
  43. {
  44. m_pageInfo = aPageInfo;
  45. }
  46. void WORKSHEET_VIEWITEM::SetTitleBlock( const TITLE_BLOCK* aTitleBlock )
  47. {
  48. m_titleBlock = aTitleBlock;
  49. }
  50. const BOX2I WORKSHEET_VIEWITEM::ViewBBox() const
  51. {
  52. BOX2I bbox;
  53. if( m_pageInfo != NULL )
  54. {
  55. bbox.SetOrigin( VECTOR2I( 0, 0 ) );
  56. bbox.SetEnd( VECTOR2I( m_pageInfo->GetWidthMils() * m_mils2IUscalefactor,
  57. m_pageInfo->GetHeightMils() * m_mils2IUscalefactor ) );
  58. }
  59. else
  60. {
  61. bbox.SetMaximum();
  62. }
  63. return bbox;
  64. }
  65. void WORKSHEET_VIEWITEM::ViewDraw( int aLayer, VIEW* aView ) const
  66. {
  67. auto gal = aView->GetGAL();
  68. auto settings = aView->GetPainter()->GetSettings();
  69. wxString fileName( m_fileName.c_str(), wxConvUTF8 );
  70. wxString sheetName( m_sheetName.c_str(), wxConvUTF8 );
  71. WS_DRAW_ITEM_LIST drawList;
  72. drawList.SetPenSize( settings->GetWorksheetLineWidth() );
  73. // Adjust the scaling factor for worksheet items:
  74. // worksheet items coordinates and sizes are stored in mils,
  75. // and must be scaled to the same units as the caller
  76. drawList.SetMilsToIUfactor( m_mils2IUscalefactor );
  77. drawList.SetSheetNumber( m_sheetNumber );
  78. drawList.SetSheetCount( m_sheetCount );
  79. drawList.SetFileName( fileName );
  80. drawList.SetSheetName( sheetName );
  81. COLOR4D color = settings->GetColor( this, aLayer );
  82. drawList.BuildWorkSheetGraphicList( *m_pageInfo, *m_titleBlock, color, color );
  83. // Draw the title block normally even if the view is flipped
  84. bool flipped = gal->IsFlippedX();
  85. if( flipped )
  86. {
  87. gal->Save();
  88. gal->Translate( VECTOR2D( m_pageInfo->GetWidthMils() * m_mils2IUscalefactor, 0 ) );
  89. gal->Scale( VECTOR2D( -1.0, 1.0 ) );
  90. }
  91. // Draw all the components that make the page layout
  92. WS_DRAW_ITEM_BASE* item = drawList.GetFirst();
  93. while( item )
  94. {
  95. switch( item->GetType() )
  96. {
  97. case WS_DRAW_ITEM_BASE::wsg_line:
  98. draw( static_cast<const WS_DRAW_ITEM_LINE*>( item ), gal );
  99. break;
  100. case WS_DRAW_ITEM_BASE::wsg_rect:
  101. draw( static_cast<const WS_DRAW_ITEM_RECT*>( item ), gal );
  102. break;
  103. case WS_DRAW_ITEM_BASE::wsg_poly:
  104. draw( static_cast<const WS_DRAW_ITEM_POLYGON*>( item ), gal );
  105. break;
  106. case WS_DRAW_ITEM_BASE::wsg_text:
  107. draw( static_cast<const WS_DRAW_ITEM_TEXT*>( item ), gal );
  108. break;
  109. case WS_DRAW_ITEM_BASE::wsg_bitmap:
  110. draw( static_cast<const WS_DRAW_ITEM_BITMAP*>( item ), gal );
  111. break;
  112. }
  113. item = drawList.GetNext();
  114. }
  115. // Draw gray line that outlines the sheet size
  116. if( settings->GetShowPageLimits() )
  117. drawBorder( gal );
  118. if( flipped )
  119. gal->Restore();
  120. }
  121. void WORKSHEET_VIEWITEM::ViewGetLayers( int aLayers[], int& aCount ) const
  122. {
  123. aCount = 1;
  124. aLayers[0] = LAYER_WORKSHEET;
  125. }
  126. void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_LINE* aItem, GAL* aGal ) const
  127. {
  128. aGal->SetIsStroke( true );
  129. aGal->SetIsFill( false );
  130. aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
  131. aGal->SetLineWidth( aItem->GetPenWidth() );
  132. aGal->DrawLine( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
  133. }
  134. void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_RECT* aItem, GAL* aGal ) const
  135. {
  136. aGal->SetIsStroke( true );
  137. aGal->SetIsFill( false );
  138. aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
  139. aGal->SetLineWidth( aItem->GetPenWidth() );
  140. aGal->DrawRectangle( VECTOR2D( aItem->GetStart() ), VECTOR2D( aItem->GetEnd() ) );
  141. }
  142. void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_POLYGON* aItem, GAL* aGal ) const
  143. {
  144. std::deque<VECTOR2D> corners;
  145. for( wxPoint point : aItem->m_Corners )
  146. {
  147. corners.push_back( VECTOR2D( point ) );
  148. }
  149. if( aItem->IsFilled() )
  150. {
  151. aGal->SetFillColor( COLOR4D( aItem->GetColor() ) );
  152. aGal->SetIsFill( true );
  153. aGal->SetIsStroke( false );
  154. aGal->DrawPolygon( corners );
  155. }
  156. else
  157. {
  158. aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
  159. aGal->SetIsFill( false );
  160. aGal->SetIsStroke( true );
  161. aGal->SetLineWidth( aItem->GetPenWidth() );
  162. aGal->DrawPolyline( corners );
  163. }
  164. }
  165. void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_TEXT* aItem, GAL* aGal ) const
  166. {
  167. VECTOR2D position( aItem->GetTextPos().x, aItem->GetTextPos().y );
  168. aGal->Save();
  169. aGal->Translate( position );
  170. aGal->Rotate( -aItem->GetTextAngle() * M_PI / 1800.0 );
  171. aGal->SetStrokeColor( COLOR4D( aItem->GetColor() ) );
  172. aGal->SetLineWidth( aItem->GetThickness() );
  173. aGal->SetTextAttributes( aItem );
  174. aGal->StrokeText( aItem->GetShownText(), VECTOR2D( 0, 0 ), 0.0 );
  175. aGal->Restore();
  176. }
  177. void WORKSHEET_VIEWITEM::draw( const WS_DRAW_ITEM_BITMAP* aItem, GAL* aGal ) const
  178. {
  179. aGal->Save();
  180. VECTOR2D position = aItem->GetPosition();
  181. aGal->Translate( position );
  182. WORKSHEET_DATAITEM_BITMAP* parent = static_cast<WORKSHEET_DATAITEM_BITMAP*>( aItem->GetParent() );
  183. // When the image scale factor is not 1.0, we need to modify the actual scale
  184. // as the image scale factor is similar to a local zoom
  185. double img_scale = parent->m_ImageBitmap->GetScale();
  186. if( img_scale != 1.0 )
  187. aGal->Scale( VECTOR2D( img_scale, img_scale ) );
  188. aGal->DrawBitmap( *parent->m_ImageBitmap );
  189. aGal->Restore();
  190. }
  191. void WORKSHEET_VIEWITEM::drawBorder( GAL* aGal ) const
  192. {
  193. VECTOR2D origin = VECTOR2D( 0.0, 0.0 );
  194. VECTOR2D end = VECTOR2D( m_pageInfo->GetWidthMils() * m_mils2IUscalefactor,
  195. m_pageInfo->GetHeightMils() * m_mils2IUscalefactor );
  196. aGal->SetIsStroke( true );
  197. // Use a gray color for the border color
  198. aGal->SetStrokeColor( COLOR4D( 0.4, 0.4, 0.4, 1.0 ) );
  199. aGal->SetIsFill( false );
  200. aGal->DrawRectangle( origin, end );
  201. }