KiCad PCB EDA Suite
dialog_image_editor.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KICAD, a free EDA CAD application.
7  *
8  * Copyright (C) 2011 jean-pierre.charras
9  * Copyright (C) 2011-2016 Kicad Developers, see change_log.txt for contributors.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <fctsys.h>
30 #include <gr_basic.h>
31 #include <common.h>
32 #include <class_bitmap_base.h>
33 
34 #include <dialog_image_editor.h>
35 
36 
38  : DIALOG_IMAGE_EDITOR_BASE( aParent )
39 {
40  m_workingImage = new BITMAP_BASE( * aItem );
41  m_lastImage = NULL;
42  m_buttonUndoLast->Enable( false );
43  wxString msg;
44  msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
45  m_textCtrlScale->SetValue( msg );
46 
47  GetSizer()->SetSizeHints( this );
48  Layout();
49  Fit();
50  SetMinSize( GetBestSize() );
51 
52  Centre();
53  SetFocus();
54 }
55 
56 
57 void DIALOG_IMAGE_EDITOR::OnUndoLastChange( wxCommandEvent& event )
58 {
61  delete tmp;
62  m_buttonUndoLast->Enable( false );
63  m_lastImage = NULL;
64  m_panelDraw->Refresh();
65 }
66 
67 
68 void DIALOG_IMAGE_EDITOR::OnMirrorX_click( wxCommandEvent& event )
69 {
70  delete m_lastImage;
72  m_buttonUndoLast->Enable( true );
73  m_buttonUndoLast->Enable( true );
74  m_workingImage->Mirror( true );
75  m_panelDraw->Refresh();
76 }
77 
78 
79 void DIALOG_IMAGE_EDITOR::OnMirrorY_click( wxCommandEvent& event )
80 {
81  delete m_lastImage;
83  m_buttonUndoLast->Enable( true );
84  m_workingImage->Mirror( false );
85  m_panelDraw->Refresh();
86 }
87 
88 
89 void DIALOG_IMAGE_EDITOR::OnRotateClick( wxCommandEvent& event )
90 {
91  delete m_lastImage;
93  m_buttonUndoLast->Enable( true );
94  m_workingImage->Rotate( false );
95  m_panelDraw->Refresh();
96 }
97 
98 
99 void DIALOG_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
100 {
101  delete m_lastImage;
103  m_buttonUndoLast->Enable( true );
104  wxImage& image = *m_workingImage->GetImageData();
105  image = image.ConvertToGreyscale();
107  m_panelDraw->Refresh();
108 }
109 
110 
111 void DIALOG_IMAGE_EDITOR::OnHalfSize( wxCommandEvent& event )
112 {
113  delete m_lastImage;
115  m_buttonUndoLast->Enable( true );
116  wxSize psize = m_workingImage->GetSizePixels();
117  wxImage& image = *m_workingImage->GetImageData();
118 
119  image = image.Scale( psize.x/2, psize.y/2, wxIMAGE_QUALITY_HIGH );
121  m_panelDraw->Refresh();
122 }
123 
124 
125 /* Test params values correctness
126  * Currently scale value must give an actual image
127  * > MIN_SIZE pixels and < MAX_SIZE pixels
128  */
130 {
131  #define MIN_SIZE 16
132  #define MAX_SIZE 6000
133  double tmp;
134  wxString msg = m_textCtrlScale->GetValue();
135 
136  // Test number correctness
137  if( ! msg.ToDouble( &tmp ) )
138  {
139  wxMessageBox( _("Incorrect scale number" ) );
140  return false;
141  }
142 
143  // Test value correctness
144  wxSize psize = m_workingImage->GetSizePixels();
145 
146  if( (psize.x * tmp) < MIN_SIZE || (psize.y * tmp) < MIN_SIZE )
147  {
148  wxMessageBox( _("Scale is too small for this image" ) );
149  return false;
150  }
151 
152  if( (psize.x * tmp) > MAX_SIZE || (psize.y * tmp) > MAX_SIZE )
153  {
154  wxMessageBox( _("Scale is too large for this image" ) );
155  return false;
156  }
157 
158  return true;
159 }
160 
161 
162 void DIALOG_IMAGE_EDITOR::OnOK_Button( wxCommandEvent& aEvent )
163 {
164  if( CheckValues() )
165  EndModal( wxID_OK );
166 
167  return;
168 }
169 
170 
171 void DIALOG_IMAGE_EDITOR::OnCancel_Button( wxCommandEvent& aEvent )
172 {
173  EndModal( wxID_CANCEL );
174 }
175 
176 
177 void DIALOG_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
178 {
179  wxPaintDC dc( m_panelDraw );
180  wxSize size = m_panelDraw->GetClientSize();
181  dc.SetDeviceOrigin( size.x/2, size.y/2 );
182 
183  double scale = 1.0 / m_workingImage->GetScalingFactor();
184  dc.SetUserScale( scale, scale );
185  m_workingImage->DrawBitmap( NULL, &dc, wxPoint( 0, 0 ) );
186 }
187 
188 
190 {
191  wxString msg = m_textCtrlScale->GetValue();
192  double scale = 1.0;
193  msg.ToDouble( &scale );
194  m_workingImage->SetScale( scale );
195  aItem->ImportData( m_workingImage );
196 }
Class DIALOG_IMAGE_EDITOR_BASE.
void TransfertToImage(BITMAP_BASE *aItem)
Function TransfertToImage copy edited image to aItem.
BITMAP_BASE * m_workingImage
void SetScale(double aScale)
void RebuildBitmap()
wxImage * GetImageData()
DIALOG_IMAGE_EDITOR(wxWindow *aParent, BITMAP_BASE *aItem)
void OnMirrorY_click(wxCommandEvent &event) override
void Mirror(bool aVertically)
Function Mirror Mirror image vertically (i.e.
void OnUndoLastChange(wxCommandEvent &event) override
#define MIN_SIZE
#define MAX_SIZE
void OnRedrawPanel(wxPaintEvent &event) override
This class handle bitmap images in KiCad.
void OnGreyScaleConvert(wxCommandEvent &event) override
void OnMirrorX_click(wxCommandEvent &event) override
void OnHalfSize(wxCommandEvent &event) override
wxSize GetSizePixels() const
Function GetSizePixels.
void OnCancel_Button(wxCommandEvent &aEvent) override
double GetScale() const
void ImportData(BITMAP_BASE *aItem)
Function ImportData Copy aItem image to me and update m_bitmap.
const int scale
void OnOK_Button(wxCommandEvent &aEvent) override
The common library.
double GetScalingFactor() const
Function GetScalingFactor.
void OnRotateClick(wxCommandEvent &event) override
void DrawBitmap(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPos)
void Rotate(bool aRotateCCW)
Function Rotate Rotate image CW or CCW.