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.

181 lines
5.5 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->RunOnDescendants(
  72. [&]( BOARD_ITEM* item )
  73. {
  74. item->ClearSelected();
  75. item->ClearBrightened();
  76. } );
  77. m_boardItemCopy->Move( -m_boardItemCopy->GetPosition() );
  78. if( m_boardItemCopy->IsFlipped() )
  79. m_boardItemCopy->Flip( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );
  80. if( m_boardItemCopy->GetOrientation() != ANGLE_0 )
  81. m_boardItemCopy->Rotate( { 0, 0 }, -m_boardItemCopy->GetOrientation() );
  82. m_libraryItem = aLibFootprint;
  83. DisplayFootprints( m_boardItemCopy, m_libraryItem );
  84. wxScrollEvent dummy;
  85. onSlider( dummy );
  86. }
  87. void FOOTPRINT_DIFF_WIDGET::ToggleAB()
  88. {
  89. const int val = m_slider->GetValue();
  90. if( val == 0 )
  91. m_slider->SetValue( 100 );
  92. else
  93. m_slider->SetValue( 0 );
  94. wxScrollEvent dummy;
  95. onSlider( dummy );
  96. }
  97. void FOOTPRINT_DIFF_WIDGET::onSlider( wxScrollEvent& aEvent )
  98. {
  99. double pct = (double) m_slider->GetValue() / 100.0;
  100. if( m_boardItemCopy )
  101. {
  102. double val;
  103. if( pct < 0.5 )
  104. val = 0.0;
  105. else
  106. val = ( pct - 0.5 ) * 2;
  107. m_boardItemCopy->SetForcedTransparency( val );
  108. m_boardItemCopy->RunOnDescendants(
  109. [&]( BOARD_ITEM* item )
  110. {
  111. item->SetForcedTransparency( val );
  112. } );
  113. }
  114. if( m_libraryItem )
  115. {
  116. double val;
  117. if( pct > 0.5 )
  118. val = 0.0;
  119. else
  120. val = 1.0 - ( pct * 2 );
  121. m_libraryItem->SetForcedTransparency( val );
  122. m_libraryItem->RunOnDescendants(
  123. [&]( BOARD_ITEM* item )
  124. {
  125. item->SetForcedTransparency( val );
  126. } );
  127. }
  128. RefreshAll();
  129. aEvent.Skip();
  130. }
  131. void FOOTPRINT_DIFF_WIDGET::onCharHook( wxKeyEvent& aEvent )
  132. {
  133. if( aEvent.GetKeyCode() == '/' )
  134. {
  135. ToggleAB();
  136. }
  137. else
  138. {
  139. aEvent.Skip();
  140. }
  141. }