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) 2018 jean-pierre.charras
9  * Copyright (C) 2011-2018 Kicad Developers, see AUTHORS.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 <confirm.h>
33 #include <bitmap_base.h>
34 
35 #include <dialog_image_editor.h>
36 
37 
39  : DIALOG_IMAGE_EDITOR_BASE( aParent )
40 {
41  m_workingImage = new BITMAP_BASE( * aItem );
42  m_lastImage = NULL;
43  m_buttonUndoLast->Enable( false );
44  wxString msg;
45  msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
46  m_textCtrlScale->SetValue( msg );
47 
49  m_sdbSizerOK->SetDefault();
50 }
51 
52 
53 void DIALOG_IMAGE_EDITOR::OnUndoLastChange( wxCommandEvent& event )
54 {
57  delete tmp;
58  m_buttonUndoLast->Enable( false );
59  m_lastImage = NULL;
60  m_panelDraw->Refresh();
61 }
62 
63 
64 void DIALOG_IMAGE_EDITOR::OnMirrorX_click( wxCommandEvent& event )
65 {
66  delete m_lastImage;
68  m_buttonUndoLast->Enable( true );
69  m_buttonUndoLast->Enable( true );
70  m_workingImage->Mirror( true );
71  m_panelDraw->Refresh();
72 }
73 
74 
75 void DIALOG_IMAGE_EDITOR::OnMirrorY_click( wxCommandEvent& event )
76 {
77  delete m_lastImage;
79  m_buttonUndoLast->Enable( true );
80  m_workingImage->Mirror( false );
81  m_panelDraw->Refresh();
82 }
83 
84 
85 void DIALOG_IMAGE_EDITOR::OnRotateClick( wxCommandEvent& event )
86 {
87  delete m_lastImage;
89  m_buttonUndoLast->Enable( true );
90  m_workingImage->Rotate( false );
91  m_panelDraw->Refresh();
92 }
93 
94 
95 void DIALOG_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
96 {
97  delete m_lastImage;
99  m_buttonUndoLast->Enable( true );
100  wxImage& image = *m_workingImage->GetImageData();
101  image = image.ConvertToGreyscale();
103  m_panelDraw->Refresh();
104 }
105 
106 
107 void DIALOG_IMAGE_EDITOR::OnHalfSize( wxCommandEvent& event )
108 {
109  delete m_lastImage;
111  m_buttonUndoLast->Enable( true );
112  wxSize psize = m_workingImage->GetSizePixels();
113  wxImage& image = *m_workingImage->GetImageData();
114 
115  image = image.Scale( psize.x/2, psize.y/2, wxIMAGE_QUALITY_HIGH );
117  m_panelDraw->Refresh();
118 }
119 
120 
121 /* Test params values correctness
122  * Currently scale value must give an actual image
123  * > MIN_SIZE pixels (mandatory to be able to see the image)
124  * and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
125  * Note: The image definition is 300ppi in drawing routines.
126  */
128 {
129  #define MIN_SIZE 32 // Min size in pixels after scaling
130  #define MAX_SIZE 6000 // Max size in pixels after scaling
131  double tmp;
132  wxString msg = m_textCtrlScale->GetValue();
133 
134  // Test number correctness
135  if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
136  {
137  wxMessageBox( _("Incorrect scale number" ) );
138  return false;
139  }
140 
141  // Test value correctness
142  wxSize psize = m_workingImage->GetSizePixels();
143  int size_min = (int)std::min( (psize.x * tmp), (psize.y * tmp) );
144 
145  if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
146  {
147  wxMessageBox( wxString::Format(
148  _("This scale gives a too small image size (%.2f mm or %.1f mil)" ),
149  25.4 / 300 * size_min, 1000.0/300.0 * size_min ) );
150  return false;
151  }
152 
153  int size_max = (int)std::max( (psize.x * tmp), (psize.y * tmp) );
154 
155  if( size_max > MAX_SIZE )
156  {
157  // the actual size is 25.4/300 * size_max in mm
158  if( !IsOK( this, wxString::Format(
159  _("This scale gives a very large image size (%.1f mm or %.2f in). Are you sure?" ),
160  25.4 / 300 * size_max, size_max /300.0 ) ) )
161  return false;
162  }
163 
164  return true;
165 }
166 
167 
169 {
170  return CheckValues();
171 }
172 
173 
174 void DIALOG_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
175 {
176  wxPaintDC dc( m_panelDraw );
177  wxSize size = m_panelDraw->GetClientSize();
178  dc.SetDeviceOrigin( size.x/2, size.y/2 );
179 
180  double scale = 1.0 / m_workingImage->GetScalingFactor();
181  dc.SetUserScale( scale, scale );
182  m_workingImage->DrawBitmap( &dc, wxPoint( 0, 0 ) );
183 }
184 
185 
187 {
188  wxString msg = m_textCtrlScale->GetValue();
189  double scale = 1.0;
190  msg.ToDouble( &scale );
191  m_workingImage->SetScale( scale );
192  aItem->ImportData( m_workingImage );
193 }
Class DIALOG_IMAGE_EDITOR_BASE.
This file is part of the common library.
void TransfertToImage(BITMAP_BASE *aItem)
Function TransfertToImage copy edited image to aItem.
BITMAP_BASE * m_workingImage
void SetScale(double aScale)
Definition: bitmap_base.h:85
void RebuildBitmap()
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:77
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.
Definition: bitmap_base.h:46
void OnGreyScaleConvert(wxCommandEvent &event) override
void OnMirrorX_click(wxCommandEvent &event) override
void OnHalfSize(wxCommandEvent &event) override
wxSize GetSizePixels() const
Function GetSizePixels.
Definition: bitmap_base.h:133
double GetScale() const
Definition: bitmap_base.h:84
void ImportData(BITMAP_BASE *aItem)
Function ImportData Copy aItem image to me and update m_bitmap.
Definition: bitmap_base.cpp:71
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 max(a, b)
Definition: auxiliary.h:86
The common library.
double GetScalingFactor() const
Function GetScalingFactor.
Definition: bitmap_base.h:117
void DrawBitmap(wxDC *aDC, const wxPoint &aPos)
void OnRotateClick(wxCommandEvent &event) override
bool TransferDataFromWindow() override
void Rotate(bool aRotateCCW)
Function Rotate Rotate image CW or CCW.
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:295
#define min(a, b)
Definition: auxiliary.h:85