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.

146 lines
4.7 KiB

4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 jean-pierre.charras
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  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. #include <wx/dcclient.h>
  25. #include <wx/msgdlg.h>
  26. #include <bitmap_base.h>
  27. #include <pcb_base_edit_frame.h>
  28. #include <tool/tool_manager.h>
  29. #include <tool/actions.h>
  30. #include <confirm.h>
  31. #include <dialogs/panel_image_editor.h>
  32. #include <algorithm>
  33. PANEL_IMAGE_EDITOR::PANEL_IMAGE_EDITOR( wxWindow* aParent, const BITMAP_BASE& aItem ) :
  34. PANEL_IMAGE_EDITOR_BASE( aParent ),
  35. m_workingImage( std::make_unique<BITMAP_BASE>( aItem ) )
  36. {
  37. wxString msg;
  38. msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
  39. m_textCtrlScale->SetValue( msg );
  40. msg.Printf( wxT( "%d" ), m_workingImage->GetPPI() );
  41. m_stPPI_Value->SetLabel( msg );
  42. }
  43. PANEL_IMAGE_EDITOR::~PANEL_IMAGE_EDITOR()
  44. {
  45. }
  46. void PANEL_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
  47. {
  48. m_workingImage->ConvertToGreyscale();
  49. m_panelDraw->Refresh();
  50. }
  51. /*
  52. * Test params values correctness
  53. * Currently scale value must give an actual image > MIN_SIZE pixels (mandatory to be able to
  54. * see the image) and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
  55. * Note: The image definition is 300ppi in drawing routines.
  56. */
  57. bool PANEL_IMAGE_EDITOR::CheckValues()
  58. {
  59. #define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
  60. #define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
  61. double tmp;
  62. wxString msg = m_textCtrlScale->GetValue();
  63. // Test number correctness
  64. if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
  65. {
  66. wxMessageBox( _( "Incorrect scale number" ) );
  67. return false;
  68. }
  69. // Test value correctness
  70. VECTOR2I psize = m_workingImage->GetSizePixels();
  71. int size_min = (int) std::min( ( psize.x * tmp ), ( psize.y * tmp ) );
  72. if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
  73. {
  74. wxMessageDialog( wxGetTopLevelParent( this ),
  75. wxString::Format( _( "This scale results in an image which is too "
  76. "small (%.2f mm or %.1f mil)." ),
  77. 25.4 / 300 * size_min, 1000.0 / 300.0 * size_min ) );
  78. return false;
  79. }
  80. int size_max = (int) std::max( ( psize.x * tmp ), ( psize.y * tmp ) );
  81. if( size_max > MAX_SIZE )
  82. {
  83. // the actual size is 25.4/300 * size_max in mm
  84. if( !IsOK( wxGetTopLevelParent( this ),
  85. wxString::Format( _( "This scale results in an image which is very large "
  86. "(%.1f mm or %.2f in). Are you sure?" ),
  87. 25.4 / 300 * size_max, size_max / 300.0 ) ) )
  88. {
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. bool PANEL_IMAGE_EDITOR::TransferDataFromWindow()
  95. {
  96. return CheckValues();
  97. }
  98. void PANEL_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
  99. {
  100. wxPaintDC dc( m_panelDraw );
  101. wxSize display_size = m_panelDraw->GetClientSize();
  102. double img_scale = 1.0 / m_workingImage->GetScalingFactor();
  103. VECTOR2I img_size_pixels = m_workingImage->GetSizePixels();
  104. // Adjust the display scale to use the full available display area
  105. double scale_X = (double)display_size.x/img_size_pixels.x;
  106. double scale_Y = (double)display_size.y/img_size_pixels.y;
  107. double display_scale = img_scale * std::min( scale_X, scale_Y );
  108. dc.SetUserScale( display_scale, display_scale );
  109. m_workingImage->DrawBitmap( &dc, VECTOR2I( m_workingImage->GetSize()/2 ) );
  110. }
  111. void PANEL_IMAGE_EDITOR::TransferToImage( BITMAP_BASE& aItem )
  112. {
  113. wxString msg = m_textCtrlScale->GetValue();
  114. double scale = 1.0;
  115. msg.ToDouble( &scale );
  116. m_workingImage->SetScale( scale );
  117. aItem.ImportData( *m_workingImage );
  118. }