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 <confirm.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
33
34#include <algorithm>
35
36
39{
40 m_workingImage = new 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
51void PANEL_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
52{
54 m_panelDraw->Refresh();
55}
56
57
58/*
59 * Test params values correctness
60 * Currently scale value must give an actual image > MIN_SIZE pixels (mandatory to be able to
61 * see the image) and < MAX_SIZE pixels (if bigger, a confirmation will be asked)
62 * Note: The image definition is 300ppi in drawing routines.
63 */
65{
66#define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
67#define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
68 double tmp;
69 wxString msg = m_textCtrlScale->GetValue();
70
71 // Test number correctness
72 if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
73 {
74 wxMessageBox( _( "Incorrect scale number" ) );
75 return false;
76 }
77
78 // Test value correctness
80 int size_min = (int) std::min( ( psize.x * tmp ), ( psize.y * tmp ) );
81
82 if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
83 {
84 wxMessageDialog( wxGetTopLevelParent( this ),
85 wxString::Format( _( "This scale results in an image which is too "
86 "small (%.2f mm or %.1f mil)." ),
87 25.4 / 300 * size_min, 1000.0 / 300.0 * size_min ) );
88 return false;
89 }
90
91 int size_max = (int) std::max( ( psize.x * tmp ), ( psize.y * tmp ) );
92
93 if( size_max > MAX_SIZE )
94 {
95 // the actual size is 25.4/300 * size_max in mm
96 if( !IsOK( wxGetTopLevelParent( this ),
97 wxString::Format( _( "This scale results in an image which is very large "
98 "(%.1f mm or %.2f in). Are you sure?" ),
99 25.4 / 300 * size_max, size_max / 300.0 ) ) )
100 {
101 return false;
102 }
103 }
104
105 return true;
106}
107
108
110{
111 return CheckValues();
112}
113
114
115void PANEL_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
116{
117 wxPaintDC dc( m_panelDraw );
118 wxSize display_size = m_panelDraw->GetClientSize();
119
120 double img_scale = 1.0 / m_workingImage->GetScalingFactor();
121 VECTOR2I img_size_pixels = m_workingImage->GetSizePixels();
122
123 // Adjust the display scale to use the full available display area
124 double scale_X = (double)display_size.x/img_size_pixels.x;
125 double scale_Y = (double)display_size.y/img_size_pixels.y;
126
127 double display_scale = img_scale * std::min( scale_X, scale_Y );
128
129 dc.SetUserScale( display_scale, display_scale );
131}
132
133
135{
136 wxString msg = m_textCtrlScale->GetValue();
137 double scale = 1.0;
138 msg.ToDouble( &scale );
140 aItem->ImportData( m_workingImage );
141}
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:48
double GetScalingFactor() const
This scaling factor depends on m_pixelSizeIu and m_scale.
Definition: bitmap_base.h:93
VECTOR2I GetSizePixels() const
Definition: bitmap_base.h:106
double GetScale() const
Definition: bitmap_base.h:72
void DrawBitmap(wxDC *aDC, const VECTOR2I &aPos, const KIGFX::COLOR4D &aBackgroundColor=KIGFX::COLOR4D::UNSPECIFIED)
void ImportData(BITMAP_BASE *aItem)
Copy aItem image to this object and update m_bitmap.
void ConvertToGreyscale()
int GetPPI() const
Definition: bitmap_base.h:117
VECTOR2I GetSize() const
void SetScale(double aScale)
Definition: bitmap_base.h:73
Class PANEL_IMAGE_EDITOR_BASE.
void TransferToImage(BITMAP_BASE *aItem)
Function TransferToImage copy edited image to aItem.
bool TransferDataFromWindow() override
BITMAP_BASE * m_workingImage
PANEL_IMAGE_EDITOR(wxWindow *aParent, BITMAP_BASE *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:360
This file is part of the common library.
#define _(s)
#define MAX_SIZE
#define MIN_SIZE
const int scale
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588