KiCad PCB EDA Suite
dialog_image_editor.cpp
Go to the documentation of this file.
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 
25 #include <fctsys.h>
26 #include <gr_basic.h>
27 #include <common.h>
28 #include <confirm.h>
29 #include <bitmap_base.h>
30 
31 #include <dialog_image_editor.h>
32 
33 
35  : DIALOG_IMAGE_EDITOR_BASE( aParent )
36 {
37  m_workingImage = new BITMAP_BASE( *aItem );
38  wxString msg;
39  msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
40  m_textCtrlScale->SetValue( msg );
41 
43  m_sdbSizerOK->SetDefault();
44 }
45 
46 
47 void DIALOG_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
48 {
49  wxImage& image = *m_workingImage->GetImageData();
50  image = image.ConvertToGreyscale();
52  m_panelDraw->Refresh();
53 }
54 
55 
56 /*
57  * Test params values correctness
58  * Currently scale value must give an actual image > MIN_SIZE pixels (mandatory to be able to
59  * see the image) and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
60  * Note: The image definition is 300ppi in drawing routines.
61  */
63 {
64  #define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
65  #define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
66  double tmp;
67  wxString msg = m_textCtrlScale->GetValue();
68 
69  // Test number correctness
70  if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
71  {
72  wxMessageBox( _( "Incorrect scale number" ) );
73  return false;
74  }
75 
76  // Test value correctness
77  wxSize psize = m_workingImage->GetSizePixels();
78  int size_min = (int)std::min( (psize.x * tmp), (psize.y * tmp) );
79 
80  if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
81  {
82  wxMessageBox( wxString::Format( _( "This scale results in an image which is too small "
83  "(%.2f mm or %.1f mil)." ),
84  25.4 / 300 * size_min, 1000.0/300.0 * size_min ) );
85  return false;
86  }
87 
88  int size_max = (int)std::max( (psize.x * tmp), (psize.y * tmp) );
89 
90  if( size_max > MAX_SIZE )
91  {
92  // the actual size is 25.4/300 * size_max in mm
93  if( !IsOK( this, wxString::Format( _( "This scale results in an image which is very large "
94  "(%.1f mm or %.2f in). Are you sure?" ),
95  25.4 / 300 * size_max, size_max /300.0 ) ) )
96  return false;
97  }
98 
99  return true;
100 }
101 
102 
104 {
105  return CheckValues();
106 }
107 
108 
109 void DIALOG_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
110 {
111  wxPaintDC dc( m_panelDraw );
112  wxSize size = m_panelDraw->GetClientSize();
113  dc.SetDeviceOrigin( size.x/2, size.y/2 );
114 
115  double scale = 1.0 / m_workingImage->GetScalingFactor();
116  dc.SetUserScale( scale, scale );
117  m_workingImage->DrawBitmap( &dc, wxPoint( 0, 0 ) );
118 }
119 
120 
122 {
123  wxString msg = m_textCtrlScale->GetValue();
124  double scale = 1.0;
125  msg.ToDouble( &scale );
127  aItem->ImportData( m_workingImage );
128 }
double GetScalingFactor() const
Function GetScalingFactor.
Definition: bitmap_base.h:125
Class DIALOG_IMAGE_EDITOR_BASE.
This file is part of the common library.
BITMAP_BASE * m_workingImage
void SetScale(double aScale)
Definition: bitmap_base.h:93
void RebuildBitmap()
Definition: bitmap_base.h:100
double GetScale() const
Definition: bitmap_base.h:92
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
wxImage * GetImageData()
Definition: bitmap_base.h:83
DIALOG_IMAGE_EDITOR(wxWindow *aParent, BITMAP_BASE *aItem)
#define MIN_SIZE
#define MAX_SIZE
void OnRedrawPanel(wxPaintEvent &event) override
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:51
void OnGreyScaleConvert(wxCommandEvent &event) override
void ImportData(BITMAP_BASE *aItem)
Function ImportData Copy aItem image to me and update m_bitmap.
Definition: bitmap_base.cpp:77
const int scale
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:33
wxSize GetSizePixels() const
Function GetSizePixels.
Definition: bitmap_base.h:141
void TransferToImage(BITMAP_BASE *aItem)
Function TransferToImage copy edited image to aItem.
The common library.
void DrawBitmap(wxDC *aDC, const wxPoint &aPos)
bool TransferDataFromWindow() override
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:283