KiCad PCB EDA Suite
edit_bitmap.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) 2012 jean-pierre.charras
5  * Copyright (C) 2012-2016 KiCad Developers, see change_log.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 
29 #include <fctsys.h>
30 #include <class_drawpanel.h>
31 
32 #include <schframe.h>
33 #include <sch_bitmap.h>
34 #include <dialog_image_editor.h>
35 
36 
37 static void abortMoveBitmap( EDA_DRAW_PANEL* aPanel, wxDC* aDC )
38 {
39  SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
40  SCH_BITMAP* item = (SCH_BITMAP*) screen->GetCurItem();
41  SCH_EDIT_FRAME* parent = (SCH_EDIT_FRAME*) aPanel->GetParent();
42 
43  parent->SetRepeatItem( NULL );
44 
45  if( item == NULL ) /* no current item */
46  return;
47 
48  if( item->IsNew() )
49  {
50  delete item;
51  item = NULL;
52  }
53  else // Move command on an existing text item, restore the data of the original.
54  {
55  item->ClearFlags();
56 
57  SCH_BITMAP * olditem = (SCH_BITMAP*) parent->GetUndoItem();
58 
59  wxCHECK_RET( olditem != NULL && item->Type() == olditem->Type() &&
60  item->Type() == SCH_BITMAP_T,
61  wxT( "Cannot restore undefined last text item." ) );
62 
63  // Never delete existing item, because it can be referenced by an undo/redo command
64  // Just restore its data
65  item->SwapData( olditem );
66  parent->SetUndoItem( NULL );
67  }
68 
69  screen->SetCurItem( item );
70  aPanel->Refresh();
71 }
72 
73 static void moveBitmap( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition, bool aErase )
74 {
75  SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
76  SCH_BITMAP* image = (SCH_BITMAP*) screen->GetCurItem();
77 
78  if( aErase )
79  {
80  // Erase the current bitmap at its current position.
81  // Note also items flagged IS_MOVING are not drawn,
82  // and if image is new, it is not yet il draw list
83  // so image is erased from screen
84  EDA_RECT dirty = image->GetBoundingBox();
85  dirty.Inflate( 4 ); // Give a margin
86  aPanel->SetMouseCapture( NULL, NULL ); // Avoid loop in redraw panel
87 
88  STATUS_FLAGS flgs = image->GetFlags();
89  image->ClearFlags();
90  aPanel->RefreshDrawingRect( dirty );
91  image->SetFlags( flgs );
93  }
94 
95  // Draw the bitmap at it's new position.
96  image->SetPosition( aPanel->GetParent()->GetCrossHairPosition() );
97  image->Draw( aPanel, aDC, wxPoint( 0, 0 ), GR_DEFAULT_DRAWMODE );
98 }
99 
100 
102 {
103  wxFileDialog fileDlg( this, _( "Choose Image" ), wxEmptyString, wxEmptyString,
104  _( "Image Files " ) + wxImage::GetImageExtWildcard(),
105  wxFD_OPEN );
106  int diag = fileDlg.ShowModal();
107 
108  if( diag != wxID_OK )
109  return NULL;
110 
111  wxString fullFilename = fileDlg.GetPath();
112 
113  if( !wxFileExists( fullFilename ) )
114  {
115  wxMessageBox( _( "Couldn't load image from <%s>" ), GetChars( fullFilename ) );
116  return NULL;
117  }
118 
120 
121  SCH_BITMAP* image = new SCH_BITMAP( pos );
122 
123  if( !image->ReadImageFile( fullFilename ) )
124  {
125  wxMessageBox( _( "Couldn't load image from <%s>" ), GetChars( fullFilename ) );
126  delete image;
127  return NULL;
128  }
129 
130 
131  image->SetFlags( IS_NEW | IS_MOVED );
132  image->Draw( m_canvas, aDC, wxPoint( 0, 0 ), GR_DEFAULT_DRAWMODE );
133 
135  GetScreen()->SetCurItem( image );
136 
137  OnModify();
138  return image;
139 }
140 
141 
142 void SCH_EDIT_FRAME::MoveImage( SCH_BITMAP* aImageItem, wxDC* aDC )
143 {
144  aImageItem->SetFlags( IS_MOVED );
145 
147  GetScreen()->SetCurItem( aImageItem );
148  SetRepeatItem( NULL );
149 
150  SetUndoItem( aImageItem );
151 
152  m_canvas->CrossHairOff( aDC );
153  SetCrossHairPosition( aImageItem->GetPosition() );
155  m_canvas->CrossHairOn( aDC );
156 
157  OnModify();
158 }
159 
160 
162 {
163  if( aItem->GetFlags( ) == 0 )
164  SaveCopyInUndoList( aItem, UR_ROTATED, aItem->GetPosition() );
165 
166  aItem->Rotate( aItem->GetPosition() );
167  OnModify();
168  m_canvas->Refresh();
169 }
170 
171 
172 void SCH_EDIT_FRAME::MirrorImage( SCH_BITMAP* aItem, bool Is_X_axis )
173 {
174  if( aItem->GetFlags( ) == 0 )
175  SaveCopyInUndoList( aItem, UR_CHANGED );
176 
177  if( Is_X_axis )
178  aItem->MirrorX( aItem->GetPosition().y );
179  else
180  aItem->MirrorY( aItem->GetPosition().x );
181 
182  OnModify();
183  m_canvas->Refresh();
184 }
185 
186 
188 {
189  // TODO: change image scale or more
190  DIALOG_IMAGE_EDITOR dlg( this, aItem->GetImage() );
191 
192  if( dlg.ShowModal() != wxID_OK )
193  return;
194 
195  // save old image in undo list if not already in edit
196  // or the image to be edited is part of a block
197  if( aItem->GetFlags() == 0 ||
199  SaveCopyInUndoList( aItem, UR_CHANGED );
200 
201  dlg.TransfertToImage( aItem->GetImage() );
202  OnModify();
203  m_canvas->Refresh();
204 }
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:255
BLOCK_SELECTOR m_BlockLocate
Block description for block commands.
void Draw(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aOffset, GR_DRAWMODE aDrawMode, COLOR4D aColor=COLOR4D::UNSPECIFIED) override
Function Draw Draw a schematic item.
Definition: sch_bitmap.cpp:193
void SaveCopyInUndoList(SCH_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, const wxPoint &aTransformPoint=wxPoint(0, 0))
Function SaveCopyInUndoList.
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Definition: draw_panel.cpp:326
bool IsNew() const
Definition: base_struct.h:216
void OnModify()
Function OnModify Must be called after a schematic change in order to set the "modify" flag of the cu...
Definition: schframe.cpp:761
static void moveBitmap(EDA_DRAW_PANEL *aPanel, wxDC *aDC, const wxPoint &aPosition, bool aErase)
Definition: edit_bitmap.cpp:73
void RefreshDrawingRect(const EDA_RECT &aRect, bool aEraseBackground=true)
Function RefreshDrawingRect redraws the contents of aRect in drawing units.
Definition: draw_panel.cpp:306
SCH_ITEM * GetUndoItem() const
Definition: schframe.h:1309
Schematic editor (Eeschema) main window.
Definition: schframe.h:117
void MirrorImage(SCH_BITMAP *aItem, bool Is_X_axis)
Function MirrorImage Mirror a bitmap.
wxPoint GetPosition() const override
Function GetPosition.
Definition: sch_bitmap.h:135
void MoveCursorToCrossHair()
Function MoveCursorToCrossHair warps the cursor to the current cross hair position.
Definition: draw_panel.cpp:348
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: sch_bitmap.cpp:183
static void abortMoveBitmap(EDA_DRAW_PANEL *aPanel, wxDC *aDC)
Definition: edit_bitmap.cpp:37
void SetRepeatItem(SCH_ITEM *aItem)
Function SetRepeatItem clones aItem and owns that clone in this container.
Definition: schframe.cpp:459
void MirrorY(int aYaxis_position) override
Function MirrorY mirrors item relative to the Y axis about aYaxis_position.
Definition: sch_bitmap.cpp:247
#define IS_NEW
New item, just created.
Definition: base_struct.h:113
SCH_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: schframe.cpp:520
void Rotate(wxPoint aPosition) override
Function Rotate rotates the item around aPosition 90 degrees in the clockwise direction.
Definition: sch_bitmap.cpp:254
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:253
BITMAP_BASE * GetImage()
Definition: sch_bitmap.h:61
SCH_ITEM * GetCurItem() const
Function GetCurItem returns the currently selected SCH_ITEM, overriding BASE_SCREEN::GetCurItem().
void SwapData(SCH_ITEM *aItem) override
Function SwapData swap the internal data structures aItem with the schematic item.
Definition: sch_bitmap.cpp:125
unsigned STATUS_FLAGS
Definition: base_struct.h:144
void RotateImage(SCH_BITMAP *aItem)
EDA_DRAW_FRAME * GetParent() const
Definition: draw_panel.cpp:175
void CrossHairOff(wxDC *DC)
Definition: draw_panel.cpp:253
SCH_BITMAP * CreateNewImage(wxDC *aDC)
void MoveImage(SCH_BITMAP *aItem, wxDC *aDC)
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
#define GR_DEFAULT_DRAWMODE
Definition: general.h:70
void CrossHairOn(wxDC *DC)
Definition: draw_panel.cpp:260
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
BLOCK_STATE_T GetState() const
void MirrorX(int aXaxis_position) override
Function MirrorX mirrors item relative to the X axis about aXaxis_position.
Definition: sch_bitmap.cpp:237
void SetCurItem(SCH_ITEM *aItem)
Function SetCurItem sets the currently selected object, m_CurrentItem.
Class EDA_RECT handles the component boundary box.
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:188
void EditImage(SCH_BITMAP *aItem)
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:254
bool ReadImageFile(const wxString &aFullFilename)
Function ReadImageFile Reads and stores an image file.
Definition: sch_bitmap.cpp:85
void SetUndoItem(const SCH_ITEM *aItem)
Function SetUndoItem clones aItem which can be used to restore the state of the item being edited whe...
Definition: schframe.cpp:579
void SetMouseCapture(MOUSE_CAPTURE_CALLBACK aMouseCaptureCallback, END_MOUSE_CAPTURE_CALLBACK aEndMouseCaptureCallback)
Function SetMouseCapture sets the mouse capture and end mouse capture callbacks to aMouseCaptureCallb...
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Function SetCrossHairPosition sets the screen cross hair position to aPosition in logical (drawing) u...
wxPoint GetCrossHairPosition(bool aInvertY=false) const
Function GetCrossHairPosition return the current cross hair position in logical (drawing) coordinates...
void SetPosition(const wxPoint &aPosition) override
Function SetPosition set the schematic item position to aPosition.
Definition: sch_bitmap.h:137
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
#define IS_MOVED
Item being moved.
Definition: base_struct.h:112