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.

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