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 (C) 2011-2024 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/tool_manager.h>
30#include <tool/actions.h>
31#include <confirm.h>
32
34
35#include <algorithm>
36
37
38PANEL_IMAGE_EDITOR::PANEL_IMAGE_EDITOR( wxWindow* aParent, const BITMAP_BASE& aItem ) :
39 PANEL_IMAGE_EDITOR_BASE( aParent ),
40 m_workingImage( std::make_unique<BITMAP_BASE>( aItem ) )
41{
42 wxString msg;
43 msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
44 m_textCtrlScale->SetValue( msg );
45
46 msg.Printf( wxT( "%d" ), m_workingImage->GetPPI() );
47 m_stPPI_Value->SetLabel( msg );
48}
49
50
52{
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#define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
72#define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
73 double tmp;
74 wxString msg = m_textCtrlScale->GetValue();
75
76 // Test number correctness
77 if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
78 {
79 wxMessageBox( _( "Incorrect scale number" ) );
80 return false;
81 }
82
83 // Test value correctness
84 VECTOR2I psize = m_workingImage->GetSizePixels();
85 int size_min = (int) std::min( ( psize.x * tmp ), ( psize.y * tmp ) );
86
87 if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
88 {
89 wxMessageDialog( wxGetTopLevelParent( this ),
90 wxString::Format( _( "This scale results in an image which is too "
91 "small (%.2f mm or %.1f mil)." ),
92 25.4 / 300 * size_min, 1000.0 / 300.0 * size_min ) );
93 return false;
94 }
95
96 int size_max = (int) std::max( ( psize.x * tmp ), ( psize.y * tmp ) );
97
98 if( size_max > MAX_SIZE )
99 {
100 // the actual size is 25.4/300 * size_max in mm
101 if( !IsOK( wxGetTopLevelParent( this ),
102 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, size_max / 300.0 ) ) )
105 {
106 return false;
107 }
108 }
109
110 return true;
111}
112
113
115{
116 return CheckValues();
117}
118
119
120void PANEL_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
121{
122 wxPaintDC dc( m_panelDraw );
123 wxSize display_size = m_panelDraw->GetClientSize();
124
125 double img_scale = 1.0 / m_workingImage->GetScalingFactor();
126 VECTOR2I img_size_pixels = m_workingImage->GetSizePixels();
127
128 // Adjust the display scale to use the full available display area
129 double scale_X = (double)display_size.x/img_size_pixels.x;
130 double scale_Y = (double)display_size.y/img_size_pixels.y;
131
132 double display_scale = img_scale * std::min( scale_X, scale_Y );
133
134 dc.SetUserScale( display_scale, display_scale );
135 m_workingImage->DrawBitmap( &dc, VECTOR2I( m_workingImage->GetSize()/2 ) );
136}
137
138
140{
141 wxString msg = m_textCtrlScale->GetValue();
142 double scale = 1.0;
143 msg.ToDouble( &scale );
144 m_workingImage->SetScale( scale );
145 aItem.ImportData( *m_workingImage );
146}
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.
Class PANEL_IMAGE_EDITOR_BASE.
PANEL_IMAGE_EDITOR(wxWindow *aParent, const BITMAP_BASE &aItem)
bool TransferDataFromWindow() override
std::unique_ptr< BITMAP_BASE > m_workingImage
void TransferToImage(BITMAP_BASE &aItem)
Function TransferToImage copy edited image to aItem.
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:250
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:691