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
40{
41 m_workingImage = new BITMAP_BASE( *aItem );
42
43 wxString msg;
44 msg.Printf( wxT( "%f" ), m_workingImage->GetScale() );
45 m_textCtrlScale->SetValue( msg );
46
47 msg.Printf( wxT( "%d" ), m_workingImage->GetPPI() );
48 m_stPPI_Value->SetLabel( msg );
49}
50
51
52void PANEL_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event )
53{
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#define MIN_SIZE 15 // Min size in pixels after scaling (50 mils)
68#define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches)
69 double tmp;
70 wxString msg = m_textCtrlScale->GetValue();
71
72 // Test number correctness
73 if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
74 {
75 wxMessageBox( _( "Incorrect scale number" ) );
76 return false;
77 }
78
79 // Test value correctness
81 int size_min = (int) std::min( ( psize.x * tmp ), ( psize.y * tmp ) );
82
83 if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
84 {
85 wxMessageDialog( wxGetTopLevelParent( this ),
86 wxString::Format( _( "This scale results in an image which is too "
87 "small (%.2f mm or %.1f mil)." ),
88 25.4 / 300 * size_min, 1000.0 / 300.0 * size_min ) );
89 return false;
90 }
91
92 int size_max = (int) std::max( ( psize.x * tmp ), ( psize.y * tmp ) );
93
94 if( size_max > MAX_SIZE )
95 {
96 // the actual size is 25.4/300 * size_max in mm
97 if( !IsOK( wxGetTopLevelParent( this ),
98 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, size_max / 300.0 ) ) )
101 {
102 return false;
103 }
104 }
105
106 return true;
107}
108
109
111{
112 return CheckValues();
113}
114
115
116void PANEL_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
117{
118 wxPaintDC dc( m_panelDraw );
119 wxSize display_size = m_panelDraw->GetClientSize();
120
121 double img_scale = 1.0 / m_workingImage->GetScalingFactor();
122 VECTOR2I img_size_pixels = m_workingImage->GetSizePixels();
123
124 // Adjust the display scale to use the full available display area
125 double scale_X = (double)display_size.x/img_size_pixels.x;
126 double scale_Y = (double)display_size.y/img_size_pixels.y;
127
128 double display_scale = img_scale * std::min( scale_X, scale_Y );
129
130 dc.SetUserScale( display_scale, display_scale );
132}
133
134
136{
137 wxString msg = m_textCtrlScale->GetValue();
138 double scale = 1.0;
139 msg.ToDouble( &scale );
141 aItem->ImportData( m_workingImage );
142}
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:250
This file is part of the common library.
#define _(s)
#define MAX_SIZE
#define MIN_SIZE
const int scale
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:673