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/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 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;
76 wxString msg = m_textCtrlScale->GetValue();
77
78 // Test number correctness
79 if( !msg.ToDouble( &tmp ) || tmp < 0.0 )
80 {
81 DisplayErrorMessage( host, _( "Scale must be a positive number." ) );
82 return false;
83 }
84
85 // Test value correctness
86 VECTOR2I psize = m_workingImage->GetSizePixels();
87 int size_min = (int) std::min( ( psize.x * tmp ), ( psize.y * tmp ) );
88
89 if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate
90 {
91 DisplayErrorMessage( host, wxString::Format( _( "This scale results in an image which is too small "
92 "(%.2f mm or %.1f mil)." ),
93 25.4 / 300 * size_min,
94 1000.0 / 300.0 * size_min ) );
95 return false;
96 }
97
98 int size_max = (int) std::max( ( psize.x * tmp ), ( psize.y * tmp ) );
99
100 if( size_max > MAX_SIZE )
101 {
102 // the actual size is 25.4/300 * size_max in mm
103 if( !IsOK( host, wxString::Format( _( "This scale results in an image which is very large "
104 "(%.1f mm or %.2f in). Are you sure?" ),
105 25.4 / 300 * size_max,
106 size_max / 300.0 ) ) )
107 {
108 return false;
109 }
110 }
111
112 return true;
113}
114
115
117{
118 return CheckValues();
119}
120
121
122void PANEL_IMAGE_EDITOR::OnRedrawPanel( wxPaintEvent& event )
123{
124 wxPaintDC dc( m_panelDraw );
125 wxSize display_size = m_panelDraw->GetClientSize();
126
127 double img_scale = 1.0 / m_workingImage->GetScalingFactor();
128 VECTOR2I img_size_pixels = m_workingImage->GetSizePixels();
129
130 // Adjust the display scale to use the full available display area
131 double scale_X = (double)display_size.x/img_size_pixels.x;
132 double scale_Y = (double)display_size.y/img_size_pixels.y;
133
134 double display_scale = img_scale * std::min( scale_X, scale_Y );
135
136 dc.SetUserScale( display_scale, display_scale );
137 m_workingImage->DrawBitmap( &dc, VECTOR2I( m_workingImage->GetSize()/2 ) );
138}
139
140
142{
143 wxString msg = m_textCtrlScale->GetValue();
144 double scale = 1.0;
145 msg.ToDouble( &scale );
146 m_workingImage->SetScale( scale );
147 aItem.ImportData( *m_workingImage );
148}
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)
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: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