KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_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 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
25#include <wx/dcclient.h>
26#include <wx/msgdlg.h>
27#include <bitmap_base.h>
28#include <pcb_base_edit_frame.h>
29#include <tool/actions.h>
30#include <confirm.h>
31#include <units_provider.h>
33
34#include <algorithm>
35
36
37PANEL_IMAGE_EDITOR::PANEL_IMAGE_EDITOR( UNITS_PROVIDER* aUnitsProvider, wxWindow* aParent, const BITMAP_BASE& aItem ) :
38 PANEL_IMAGE_EDITOR_BASE( aParent ),
39 m_scale( aUnitsProvider, aParent, m_staticTextScale, m_textCtrlScale, nullptr ),
40 m_workingImage( std::make_unique<BITMAP_BASE>( aItem ) )
41{
42 m_scale.SetUnits( EDA_UNITS::UNSCALED );
43}
44
45
47{
48 m_scale.SetDoubleValue( m_workingImage->GetScale() );
49
50 m_stPPI_Value->SetLabel( wxString::Format( wxT( "%d" ), m_workingImage->GetPPI() ) );
51
52 return true;
53}
54
55
56void PANEL_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
57{
58 m_workingImage->ConvertToGreyscale();
59 m_panelDraw->Refresh();
60}
61
62
63/*
64 * Test params values correctness
65 * Currently scale value must give an actual image > MIN_SIZE pixels (mandatory to be able to
66 * see the image) and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
67 * Note: The image definition is 300ppi in drawing routines.
68 */
70{
71 wxWindow* host = wxGetTopLevelParent( this );
72
73#define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
74#define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
75 double tmp = m_scale.GetDoubleValue();
76
77 // Test number correctness
78 if( tmp < 0.0 )
79 {
80 DisplayErrorMessage( host, _( "Scale must be a positive number." ) );
81 return false;
82 }
83
84 // Test value correctness
85 VECTOR2I psize = m_workingImage->GetSizePixels();
86 int size_min = (int) std::min( ( psize.x * tmp ), ( psize.y * tmp ) );
87
88 if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
89 {
90 DisplayErrorMessage( host, wxString::Format( _( "This scale results in an image which is too small "
91 "(%.2f mm or %.1f mil)." ),
92 25.4 / 300 * size_min,
93 1000.0 / 300.0 * size_min ) );
94 return false;
95 }
96
97 int size_max = (int) std::max( ( psize.x * tmp ), ( psize.y * tmp ) );
98
99 if( size_max > MAX_SIZE )
100 {
101 // the actual size is 25.4/300 * size_max in mm
102 if( !IsOK( host, wxString::Format( _( "This scale results in an image which is very large "
103 "(%.1f mm or %.2f in). Are you sure?" ),
104 25.4 / 300 * size_max,
105 size_max / 300.0 ) ) )
106 {
107 return false;
108 }
109 }
110
111 return true;
112}
113
114
119
120
122{
123 return m_scale.GetDoubleValue();
124}
125
126
127void PANEL_IMAGE_EDITOR::SetScale( double aScale )
128{
129 m_scale.ChangeDoubleValue( aScale );
130 m_workingImage->SetScale( aScale );
131 m_panelDraw->Refresh();
132}
133
134
136{
137 return m_workingImage->GetSize();
138}
139
140
141void PANEL_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
142{
143 wxPaintDC dc( m_panelDraw );
144 wxSize display_size = m_panelDraw->GetClientSize();
145
146 double img_scale = 1.0 / m_workingImage->GetScalingFactor();
147 VECTOR2I img_size_pixels = m_workingImage->GetSizePixels();
148
149 // Adjust the display scale to use the full available display area
150 double scale_X = (double)display_size.x/img_size_pixels.x;
151 double scale_Y = (double)display_size.y/img_size_pixels.y;
152
153 double display_scale = img_scale * std::min( scale_X, scale_Y );
154
155 dc.SetUserScale( display_scale, display_scale );
156 m_workingImage->DrawBitmap( &dc, VECTOR2I( m_workingImage->GetSize()/2 ) );
157}
158
159
161{
162 wxString msg = m_textCtrlScale->GetValue();
163 double scale = 1.0;
164 msg.ToDouble( &scale );
165 m_workingImage->SetScale( scale );
166 aItem.ImportData( *m_workingImage );
167}
This class handle bitmap images in KiCad.
Definition bitmap_base.h:49
void ImportData(BITMAP_BASE &aItem)
Copy aItem image to this object and update m_bitmap.
PANEL_IMAGE_EDITOR_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
bool TransferDataFromWindow() override
void SetScale(double aScale)
PANEL_IMAGE_EDITOR(UNITS_PROVIDER *aUnitsProvider, wxWindow *aParent, const BITMAP_BASE &aItem)
std::unique_ptr< BITMAP_BASE > m_workingImage
bool TransferDataToWindow() override
void TransferToImage(BITMAP_BASE &aItem)
Copy edited image to aItem.
VECTOR2I GetImageSize() const
void OnRedrawPanel(wxPaintEvent &event) override
void OnGreyScaleConvert(wxCommandEvent &event) override
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:251
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:194
This file is part of the common library.
#define _(s)
STL namespace.
#define MAX_SIZE
#define MIN_SIZE
const int scale
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695