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.

184 lines
5.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "widgets/footprint_diff_widget.h"
  20. #include <wx/sizer.h>
  21. #include <wx/stattext.h>
  22. #include <wx/slider.h>
  23. #include <wx/bmpbuttn.h>
  24. #include <bitmaps.h>
  25. #include <footprint.h>
  26. #include <hotkeys_basic.h>
  27. #include <pcb_painter.h>
  28. #include <settings/settings_manager.h>
  29. FOOTPRINT_DIFF_WIDGET::FOOTPRINT_DIFF_WIDGET( wxWindow* aParent, KIWAY& aKiway ) :
  30. FOOTPRINT_PREVIEW_WIDGET( aParent, aKiway ),
  31. m_libraryItem( nullptr ),
  32. m_slider( nullptr )
  33. {
  34. wxBoxSizer* bottomSizer = new wxBoxSizer( wxHORIZONTAL );
  35. wxStaticText* schLabel = new wxStaticText( this, wxID_ANY, _( "Board" ) );
  36. wxStaticText* libLabel = new wxStaticText( this, wxID_ANY, _( "Library" ) );
  37. m_slider = new wxSlider( this, wxID_ANY, 50, 0, 100 );
  38. bottomSizer->Add( schLabel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_CENTRE_VERTICAL, 6 );
  39. bottomSizer->Add( m_slider, 1, wxLEFT | wxRIGHT | wxALIGN_BOTTOM, 30 );
  40. bottomSizer->Add( libLabel, 0, wxLEFT | wxRIGHT | wxBOTTOM | wxALIGN_CENTRE_VERTICAL, 6 );
  41. m_toggleButton = new wxBitmapButton( this, wxID_ANY, KiBitmapBundle( BITMAPS::swap ) );
  42. wxString toggleTooltip = _( "Toggle between A and B display" );
  43. toggleTooltip = AddHotkeyName( toggleTooltip, '/', HOTKEY_ACTION_TYPE::IS_COMMENT );
  44. m_toggleButton->SetToolTip( toggleTooltip );
  45. bottomSizer->Add( m_toggleButton, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL, 6 );
  46. m_outerSizer->Add( bottomSizer, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, 10 );
  47. Layout();
  48. m_slider->Bind( wxEVT_SCROLL_TOP, &FOOTPRINT_DIFF_WIDGET::onSlider, this );
  49. m_slider->Bind( wxEVT_SCROLL_BOTTOM, &FOOTPRINT_DIFF_WIDGET::onSlider, this );
  50. m_slider->Bind( wxEVT_SCROLL_LINEUP, &FOOTPRINT_DIFF_WIDGET::onSlider, this );
  51. m_slider->Bind( wxEVT_SCROLL_LINEDOWN, &FOOTPRINT_DIFF_WIDGET::onSlider, this );
  52. m_slider->Bind( wxEVT_SCROLL_PAGEUP, &FOOTPRINT_DIFF_WIDGET::onSlider, this );
  53. m_slider->Bind( wxEVT_SCROLL_PAGEDOWN, &FOOTPRINT_DIFF_WIDGET::onSlider, this );
  54. m_slider->Bind( wxEVT_SCROLL_THUMBTRACK, &FOOTPRINT_DIFF_WIDGET::onSlider, this );
  55. m_slider->Bind( wxEVT_SCROLL_THUMBRELEASE, &FOOTPRINT_DIFF_WIDGET::onSlider, this );
  56. m_slider->Bind( wxEVT_SCROLL_CHANGED, &FOOTPRINT_DIFF_WIDGET::onSlider, this );
  57. // Bind keys
  58. Bind( wxEVT_CHAR_HOOK, &FOOTPRINT_DIFF_WIDGET::onCharHook, this );
  59. m_toggleButton->Bind( wxEVT_BUTTON,
  60. [&]( wxCommandEvent& aEvent )
  61. {
  62. ToggleAB();
  63. } );
  64. }
  65. void FOOTPRINT_DIFF_WIDGET::DisplayDiff( FOOTPRINT* aBoardFootprint,
  66. std::shared_ptr<FOOTPRINT>& aLibFootprint )
  67. {
  68. m_boardItemCopy.reset( static_cast<FOOTPRINT*>( aBoardFootprint->Clone() ) );
  69. m_boardItemCopy->ClearSelected();
  70. m_boardItemCopy->ClearBrightened();
  71. m_boardItemCopy->RunOnChildren(
  72. [&]( BOARD_ITEM* item )
  73. {
  74. item->ClearSelected();
  75. item->ClearBrightened();
  76. },
  77. RECURSE_MODE::RECURSE );
  78. m_boardItemCopy->Move( -m_boardItemCopy->GetPosition() );
  79. if( m_boardItemCopy->IsFlipped() )
  80. m_boardItemCopy->Flip( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );
  81. if( m_boardItemCopy->GetOrientation() != ANGLE_0 )
  82. m_boardItemCopy->Rotate( { 0, 0 }, -m_boardItemCopy->GetOrientation() );
  83. m_libraryItem = aLibFootprint;
  84. DisplayFootprints( m_boardItemCopy, m_libraryItem );
  85. wxScrollEvent dummy;
  86. onSlider( dummy );
  87. }
  88. void FOOTPRINT_DIFF_WIDGET::ToggleAB()
  89. {
  90. const int val = m_slider->GetValue();
  91. if( val == 0 )
  92. m_slider->SetValue( 100 );
  93. else
  94. m_slider->SetValue( 0 );
  95. wxScrollEvent dummy;
  96. onSlider( dummy );
  97. }
  98. void FOOTPRINT_DIFF_WIDGET::onSlider( wxScrollEvent& aEvent )
  99. {
  100. double pct = (double) m_slider->GetValue() / 100.0;
  101. if( m_boardItemCopy )
  102. {
  103. double val;
  104. if( pct < 0.5 )
  105. val = 0.0;
  106. else
  107. val = ( pct - 0.5 ) * 2;
  108. m_boardItemCopy->SetForcedTransparency( val );
  109. m_boardItemCopy->RunOnChildren(
  110. [&]( BOARD_ITEM* item )
  111. {
  112. item->SetForcedTransparency( val );
  113. },
  114. RECURSE_MODE::RECURSE );
  115. }
  116. if( m_libraryItem )
  117. {
  118. double val;
  119. if( pct > 0.5 )
  120. val = 0.0;
  121. else
  122. val = 1.0 - ( pct * 2 );
  123. m_libraryItem->SetForcedTransparency( val );
  124. m_libraryItem->RunOnChildren(
  125. [&]( BOARD_ITEM* item )
  126. {
  127. item->SetForcedTransparency( val );
  128. },
  129. RECURSE_MODE::RECURSE );
  130. }
  131. RefreshAll();
  132. aEvent.Skip();
  133. }
  134. void FOOTPRINT_DIFF_WIDGET::onCharHook( wxKeyEvent& aEvent )
  135. {
  136. if( aEvent.GetKeyCode() == '/' )
  137. {
  138. ToggleAB();
  139. }
  140. else
  141. {
  142. aEvent.Skip();
  143. }
  144. }