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.

128 lines
4.2 KiB

  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 (C) 2011-2019 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 <fctsys.h>
  25. #include <gr_basic.h>
  26. #include <common.h>
  27. #include <confirm.h>
  28. #include <bitmap_base.h>
  29. #include <dialog_image_editor.h>
  30. DIALOG_IMAGE_EDITOR::DIALOG_IMAGE_EDITOR( wxWindow* aParent, BITMAP_BASE* aItem )
  31. : DIALOG_IMAGE_EDITOR_BASE( aParent )
  32. {
  33. m_workingImage = new BITMAP_BASE( *aItem );
  34. wxString msg;
  35. msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
  36. m_textCtrlScale->SetValue( msg );
  37. FinishDialogSettings();
  38. m_sdbSizerOK->SetDefault();
  39. }
  40. void DIALOG_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
  41. {
  42. wxImage& image = *m_workingImage->GetImageData();
  43. image = image.ConvertToGreyscale();
  44. m_workingImage->RebuildBitmap();
  45. m_panelDraw->Refresh();
  46. }
  47. /*
  48. * Test params values correctness
  49. * Currently scale value must give an actual image > MIN_SIZE pixels (mandatory to be able to
  50. * see the image) and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
  51. * Note: The image definition is 300ppi in drawing routines.
  52. */
  53. bool DIALOG_IMAGE_EDITOR::CheckValues()
  54. {
  55. #define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
  56. #define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
  57. double tmp;
  58. wxString msg = m_textCtrlScale->GetValue();
  59. // Test number correctness
  60. if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
  61. {
  62. wxMessageBox( _( "Incorrect scale number" ) );
  63. return false;
  64. }
  65. // Test value correctness
  66. wxSize psize = m_workingImage->GetSizePixels();
  67. int size_min = (int)std::min( (psize.x * tmp), (psize.y * tmp) );
  68. if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
  69. {
  70. wxMessageBox( wxString::Format( _( "This scale results in an image which is too small "
  71. "(%.2f mm or %.1f mil)." ),
  72. 25.4 / 300 * size_min, 1000.0/300.0 * size_min ) );
  73. return false;
  74. }
  75. int size_max = (int)std::max( (psize.x * tmp), (psize.y * tmp) );
  76. if( size_max > MAX_SIZE )
  77. {
  78. // the actual size is 25.4/300 * size_max in mm
  79. if( !IsOK( this, wxString::Format( _( "This scale results in an image which is very large "
  80. "(%.1f mm or %.2f in). Are you sure?" ),
  81. 25.4 / 300 * size_max, size_max /300.0 ) ) )
  82. return false;
  83. }
  84. return true;
  85. }
  86. bool DIALOG_IMAGE_EDITOR::TransferDataFromWindow()
  87. {
  88. return CheckValues();
  89. }
  90. void DIALOG_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
  91. {
  92. wxPaintDC dc( m_panelDraw );
  93. wxSize size = m_panelDraw->GetClientSize();
  94. dc.SetDeviceOrigin( size.x/2, size.y/2 );
  95. double scale = 1.0 / m_workingImage->GetScalingFactor();
  96. dc.SetUserScale( scale, scale );
  97. m_workingImage->DrawBitmap( &dc, wxPoint( 0, 0 ) );
  98. }
  99. void DIALOG_IMAGE_EDITOR::TransferToImage( BITMAP_BASE* aItem )
  100. {
  101. wxString msg = m_textCtrlScale->GetValue();
  102. double scale = 1.0;
  103. msg.ToDouble( &scale );
  104. m_workingImage->SetScale( scale );
  105. aItem->ImportData( m_workingImage );
  106. }