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.

213 lines
6.6 KiB

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