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.

162 lines
5.8 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 (C) 2018-2022 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 <project.h>
  32. #include <view/view.h>
  33. using namespace KIGFX;
  34. DS_PROXY_VIEW_ITEM::DS_PROXY_VIEW_ITEM( int aMils2IUscalefactor, const PAGE_INFO* aPageInfo,
  35. const PROJECT* aProject, const TITLE_BLOCK* aTitleBlock,
  36. const std::map<wxString, wxString>* aProperties ) :
  37. EDA_ITEM( NOT_USED ), // this item is never added to a BOARD so it needs no type
  38. m_mils2IUscalefactor( aMils2IUscalefactor ),
  39. m_titleBlock( aTitleBlock ),
  40. m_pageInfo( aPageInfo ),
  41. m_pageNumber( "1" ),
  42. m_sheetCount( 1 ),
  43. m_isFirstPage( false ),
  44. m_project( aProject ),
  45. m_properties( aProperties ),
  46. m_colorLayer( LAYER_DRAWINGSHEET ),
  47. m_pageBorderColorLayer( LAYER_PAGE_LIMITS )
  48. {
  49. }
  50. const BOX2I DS_PROXY_VIEW_ITEM::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 DS_PROXY_VIEW_ITEM::buildDrawList( VIEW* aView,
  66. const std::map<wxString, wxString>* aProperties,
  67. DS_DRAW_ITEM_LIST* aDrawList ) const
  68. {
  69. RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
  70. wxString fileName( m_fileName.c_str(), wxConvUTF8 );
  71. wxString sheetName( m_sheetName.c_str(), wxConvUTF8 );
  72. wxString sheetPath( m_sheetPath.c_str(), wxConvUTF8 );
  73. aDrawList->SetDefaultPenSize( (int) settings->GetDrawingSheetLineWidth() );
  74. // Adjust the scaling factor: drawing sheet item coordinates and sizes are stored in mils,
  75. // and must be scaled to the same units as the caller
  76. aDrawList->SetMilsToIUfactor( m_mils2IUscalefactor );
  77. aDrawList->SetIsFirstPage( m_isFirstPage );
  78. aDrawList->SetPageNumber( m_pageNumber );
  79. aDrawList->SetSheetCount( m_sheetCount );
  80. aDrawList->SetFileName( fileName );
  81. aDrawList->SetSheetName( sheetName );
  82. aDrawList->SetSheetPath( sheetPath );
  83. aDrawList->SetSheetLayer( settings->GetLayerName() );
  84. aDrawList->SetProject( m_project );
  85. aDrawList->SetProperties( aProperties );
  86. aDrawList->BuildDrawItemsList( *m_pageInfo, *m_titleBlock );
  87. }
  88. void DS_PROXY_VIEW_ITEM::ViewDraw( int aLayer, VIEW* aView ) const
  89. {
  90. GAL* gal = aView->GetGAL();
  91. RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
  92. DS_DRAW_ITEM_LIST drawList;
  93. buildDrawList( aView, m_properties, &drawList );
  94. // Draw the title block normally even if the view is flipped
  95. bool flipped = gal->IsFlippedX();
  96. if( flipped )
  97. {
  98. gal->Save();
  99. gal->Translate( VECTOR2D( m_pageInfo->GetWidthMils() * m_mils2IUscalefactor, 0 ) );
  100. gal->Scale( VECTOR2D( -1.0, 1.0 ) );
  101. }
  102. DS_PAINTER ws_painter( gal );
  103. auto ws_settings = static_cast<DS_RENDER_SETTINGS*>( ws_painter.GetSettings() );
  104. ws_settings->SetNormalColor( settings->GetLayerColor( m_colorLayer ) );
  105. ws_settings->SetSelectedColor( settings->GetLayerColor( LAYER_SELECT_OVERLAY ) );
  106. ws_settings->SetBrightenedColor( settings->GetLayerColor( LAYER_BRIGHTENED ) );
  107. ws_settings->SetPageBorderColor( settings->GetLayerColor( m_pageBorderColorLayer ) );
  108. ws_settings->SetDefaultFont( settings->GetDefaultFont() );
  109. // Draw all the components that make the drawing sheet
  110. for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
  111. ws_painter.Draw( item, LAYER_DRAWINGSHEET );
  112. // Draw gray line that outlines the sheet size
  113. if( settings->GetShowPageLimits() )
  114. ws_painter.DrawBorder( m_pageInfo, m_mils2IUscalefactor );
  115. if( flipped )
  116. gal->Restore();
  117. }
  118. void DS_PROXY_VIEW_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
  119. {
  120. aCount = 1;
  121. aLayers[0] = LAYER_DRAWINGSHEET;
  122. }
  123. bool DS_PROXY_VIEW_ITEM::HitTestDrawingSheetItems( VIEW* aView, const VECTOR2I& aPosition )
  124. {
  125. int accuracy = (int) aView->ToWorld( 5.0 ); // five pixels at current zoom
  126. DS_DRAW_ITEM_LIST drawList;
  127. buildDrawList( aView, m_properties, &drawList );
  128. for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
  129. {
  130. if( item->HitTest( aPosition, accuracy ) )
  131. return true;
  132. }
  133. return false;
  134. }