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.

171 lines
5.9 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013-2020 CERN
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <layer_ids.h>
  26. #include <page_info.h>
  27. #include <drawing_sheet/ds_proxy_view_item.h>
  28. #include <drawing_sheet/ds_draw_item.h>
  29. #include <drawing_sheet/ds_data_item.h>
  30. #include <drawing_sheet/ds_painter.h>
  31. #include <gal/graphics_abstraction_layer.h>
  32. #include <project.h>
  33. #include <view/view.h>
  34. using namespace KIGFX;
  35. DS_PROXY_VIEW_ITEM::DS_PROXY_VIEW_ITEM( const EDA_IU_SCALE& aIuScale, const PAGE_INFO* aPageInfo,
  36. const PROJECT* aProject, const TITLE_BLOCK* aTitleBlock,
  37. const std::map<wxString, wxString>* aProperties ) :
  38. EDA_ITEM( NOT_USED ), // this item is never added to a BOARD so it needs no type
  39. m_iuScale( aIuScale ),
  40. m_titleBlock( aTitleBlock ),
  41. m_pageInfo( aPageInfo ),
  42. m_pageNumber( "1" ),
  43. m_sheetCount( 1 ),
  44. m_isFirstPage( false ),
  45. m_project( aProject ),
  46. m_properties( aProperties ),
  47. m_colorLayer( LAYER_DRAWINGSHEET ),
  48. m_pageBorderColorLayer( LAYER_PAGE_LIMITS )
  49. {
  50. }
  51. const BOX2I DS_PROXY_VIEW_ITEM::ViewBBox() const
  52. {
  53. BOX2I bbox;
  54. if( m_pageInfo )
  55. {
  56. bbox.SetOrigin( VECTOR2I( 0, 0 ) );
  57. bbox.SetEnd( VECTOR2I( m_iuScale.MilsToIU( m_pageInfo->GetWidthMils() ),
  58. m_iuScale.MilsToIU( m_pageInfo->GetHeightMils() ) ) );
  59. }
  60. else
  61. {
  62. bbox.SetMaximum();
  63. }
  64. return bbox;
  65. }
  66. void DS_PROXY_VIEW_ITEM::buildDrawList( VIEW* aView,
  67. const std::map<wxString, wxString>* aProperties,
  68. DS_DRAW_ITEM_LIST* aDrawList ) const
  69. {
  70. RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
  71. wxString fileName( m_fileName.c_str(), wxConvUTF8 );
  72. wxString sheetName( m_sheetName.c_str(), wxConvUTF8 );
  73. wxString sheetPath( m_sheetPath.c_str(), wxConvUTF8 );
  74. aDrawList->SetDefaultPenSize( (int) settings->GetDrawingSheetLineWidth() );
  75. aDrawList->SetIsFirstPage( m_isFirstPage );
  76. aDrawList->SetPageNumber( m_pageNumber );
  77. aDrawList->SetSheetCount( m_sheetCount );
  78. aDrawList->SetFileName( fileName );
  79. aDrawList->SetSheetName( sheetName );
  80. aDrawList->SetSheetPath( sheetPath );
  81. aDrawList->SetSheetLayer( settings->GetLayerName() );
  82. aDrawList->SetProject( m_project );
  83. aDrawList->SetProperties( aProperties );
  84. aDrawList->BuildDrawItemsList( *m_pageInfo, *m_titleBlock );
  85. }
  86. void DS_PROXY_VIEW_ITEM::ViewDraw( int aLayer, VIEW* aView ) const
  87. {
  88. GAL* gal = aView->GetGAL();
  89. RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
  90. DS_DRAW_ITEM_LIST drawList( m_iuScale );
  91. buildDrawList( aView, m_properties, &drawList );
  92. BOX2I viewport = BOX2ISafe( aView->GetViewport() );
  93. // Draw the title block normally even if the view is flipped
  94. bool flipped = gal->IsFlippedX();
  95. if( flipped )
  96. {
  97. int pageWidth = m_iuScale.MilsToIU( m_pageInfo->GetWidthMils() );
  98. gal->Save();
  99. gal->Translate( VECTOR2D( pageWidth, 0 ) );
  100. gal->Scale( VECTOR2D( -1.0, 1.0 ) );
  101. int right = pageWidth - viewport.GetLeft();
  102. int left = right - viewport.GetWidth();
  103. viewport.SetOrigin( left, viewport.GetTop() );
  104. }
  105. DS_PAINTER ws_painter( gal );
  106. auto ws_settings = static_cast<DS_RENDER_SETTINGS*>( ws_painter.GetSettings() );
  107. ws_settings->SetNormalColor( settings->GetLayerColor( m_colorLayer ) );
  108. ws_settings->SetSelectedColor( settings->GetLayerColor( LAYER_SELECT_OVERLAY ) );
  109. ws_settings->SetBrightenedColor( settings->GetLayerColor( LAYER_BRIGHTENED ) );
  110. ws_settings->SetPageBorderColor( settings->GetLayerColor( m_pageBorderColorLayer ) );
  111. ws_settings->SetDefaultFont( settings->GetDefaultFont() );
  112. // Draw all the components that make the drawing sheet
  113. for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
  114. {
  115. if( viewport.Intersects( item->GetApproxBBox() ) )
  116. ws_painter.Draw( item, LAYER_DRAWINGSHEET );
  117. }
  118. // Draw gray line that outlines the sheet size
  119. if( settings->GetShowPageLimits() )
  120. ws_painter.DrawBorder( m_pageInfo, m_iuScale.IU_PER_MILS );
  121. if( flipped )
  122. gal->Restore();
  123. }
  124. std::vector<int> DS_PROXY_VIEW_ITEM::ViewGetLayers() const
  125. {
  126. std::vector<int> layer{ LAYER_DRAWINGSHEET };
  127. return layer;
  128. }
  129. bool DS_PROXY_VIEW_ITEM::HitTestDrawingSheetItems( VIEW* aView, const VECTOR2I& aPosition )
  130. {
  131. int accuracy = (int) aView->ToWorld( 5.0 ); // five pixels at current zoom
  132. DS_DRAW_ITEM_LIST drawList( m_iuScale );
  133. buildDrawList( aView, m_properties, &drawList );
  134. for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
  135. {
  136. if( item->HitTest( aPosition, accuracy ) )
  137. return true;
  138. }
  139. return false;
  140. }