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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <wx/dcclient.h>
22#include <wx/msgdlg.h>
23#include <bitmap_base.h>
24#include <pcb_base_edit_frame.h>
25#include <tool/actions.h>
26#include <confirm.h>
27#include <units_provider.h>
29
30#include <algorithm>
31
32
33PANEL_IMAGE_EDITOR::PANEL_IMAGE_EDITOR( UNITS_PROVIDER* aUnitsProvider, wxWindow* aParent, const BITMAP_BASE& aItem ) :
34 PANEL_IMAGE_EDITOR_BASE( aParent ),
35 m_scale( aUnitsProvider, aParent, m_staticTextScale, m_textCtrlScale, nullptr ),
36 m_workingImage( std::make_unique<BITMAP_BASE>( aItem ) )
37{
38 m_scale.SetUnits( EDA_UNITS::UNSCALED );
39}
40
41
43{
44 m_scale.SetDoubleValue( m_workingImage->GetScale() );
45
46 m_stPPI_Value->SetLabel( wxString::Format( wxT( "%d" ), m_workingImage->GetPPI() ) );
47
48 return true;
49}
50
51
52void PANEL_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
53{
54 m_workingImage->ConvertToGreyscale();
55 m_panelDraw->Refresh();
56}
57
58
59/*
60 * Test params values correctness
61 * Currently scale value must give an actual image > MIN_SIZE pixels (mandatory to be able to
62 * see the image) and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
63 * Note: The image definition is 300ppi in drawing routines.
64 */
66{
67 wxWindow* host = wxGetTopLevelParent( this );
68
69#define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
70#define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
71 double tmp = m_scale.GetDoubleValue();
72
73 // Test number correctness
74 if( tmp < 0.0 )
75 {
76 DisplayErrorMessage( host, _( "Scale must be a positive number." ) );
77 return false;
78 }
79
80 // Test value correctness
81 VECTOR2I psize = m_workingImage->GetSizePixels();
82 int size_min = (int) std::min( ( psize.x * tmp ), ( psize.y * tmp ) );
83
84 if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
85 {
86 DisplayErrorMessage( host, wxString::Format( _( "This scale results in an image which is too small "
87 "(%.2f mm or %.1f mil)." ),
88 25.4 / 300 * size_min,
89 1000.0 / 300.0 * size_min ) );
90 return false;
91 }
92
93 int size_max = (int) std::max( ( psize.x * tmp ), ( psize.y * tmp ) );
94
95 if( size_max > MAX_SIZE )
96 {
97 // the actual size is 25.4/300 * size_max in mm
98 if( !IsOK( host, wxString::Format( _( "This scale results in an image which is very large "
99 "(%.1f mm or %.2f in). Are you sure?" ),
100 25.4 / 300 * size_max,
101 size_max / 300.0 ) ) )
102 {
103 return false;
104 }
105 }
106
107 return true;
108}
109
110
115
116
118{
119 return m_scale.GetDoubleValue();
120}
121
122
123void PANEL_IMAGE_EDITOR::SetScale( double aScale )
124{
125 m_scale.ChangeDoubleValue( aScale );
126 m_workingImage->SetScale( aScale );
127 m_panelDraw->Refresh();
128}
129
130
132{
133 return m_workingImage->GetSize();
134}
135
136
137void PANEL_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
138{
139 wxPaintDC dc( m_panelDraw );
140 wxSize display_size = m_panelDraw->GetClientSize();
141
142 double img_scale = 1.0 / m_workingImage->GetScalingFactor();
143 VECTOR2I img_size_pixels = m_workingImage->GetSizePixels();
144
145 // Adjust the display scale to use the full available display area
146 double scale_X = (double)display_size.x/img_size_pixels.x;
147 double scale_Y = (double)display_size.y/img_size_pixels.y;
148
149 double display_scale = img_scale * std::min( scale_X, scale_Y );
150
151 dc.SetUserScale( display_scale, display_scale );
152 m_workingImage->DrawBitmap( &dc, VECTOR2I( m_workingImage->GetSize()/2 ) );
153}
154
155
157{
158 wxString msg = m_textCtrlScale->GetValue();
159 double scale = 1.0;
160 msg.ToDouble( &scale );
161 m_workingImage->SetScale( scale );
162 aItem.ImportData( *m_workingImage );
163}
This class handle bitmap images in KiCad.
Definition bitmap_base.h:45
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:274
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
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:683