You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
6.0 KiB

  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 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. /**
  25. * @file edit_bitmap.cpp
  26. */
  27. #include <fctsys.h>
  28. #include <class_drawpanel.h>
  29. #include <wxEeschemaStruct.h>
  30. #include <sch_bitmap.h>
  31. #include <dialog_image_editor.h>
  32. static void abortMoveBitmap( EDA_DRAW_PANEL* aPanel, wxDC* aDC )
  33. {
  34. SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
  35. SCH_BITMAP* item = (SCH_BITMAP*) screen->GetCurItem();
  36. SCH_EDIT_FRAME* parent = (SCH_EDIT_FRAME*) aPanel->GetParent();
  37. parent->SetRepeatItem( NULL );
  38. if( item == NULL ) /* no current item */
  39. return;
  40. if( item->IsNew() )
  41. {
  42. delete item;
  43. item = NULL;
  44. }
  45. else // Move command on an existing text item, restore the data of the original.
  46. {
  47. item->ClearFlags();
  48. SCH_BITMAP * olditem = (SCH_BITMAP*) parent->GetUndoItem();
  49. wxCHECK_RET( olditem != NULL && item->Type() == olditem->Type() &&
  50. item->Type() == SCH_BITMAP_T,
  51. wxT( "Cannot restore undefined last text item." ) );
  52. // Never delete existing item, because it can be referenced by an undo/redo command
  53. // Just restore its data
  54. item->SwapData( olditem );
  55. parent->SetUndoItem( NULL );
  56. }
  57. screen->SetCurItem( item );
  58. aPanel->Refresh();
  59. }
  60. static void moveBitmap( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition, bool aErase )
  61. {
  62. SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
  63. SCH_BITMAP* image = (SCH_BITMAP*) screen->GetCurItem();
  64. if( aErase )
  65. {
  66. // Erase the current bitmap at its current position.
  67. // Note also items flagged IS_MOVING are not drawn,
  68. // and if image is new, it is not yet il draw list
  69. // so image is erased from screen
  70. EDA_RECT dirty = image->GetBoundingBox();
  71. dirty.Inflate( 4 ); // Give a margin
  72. aPanel->SetMouseCapture( NULL, NULL ); // Avoid loop in redraw panel
  73. STATUS_FLAGS flgs = image->GetFlags();
  74. image->ClearFlags();
  75. aPanel->RefreshDrawingRect( dirty );
  76. image->SetFlags( flgs );
  77. aPanel->SetMouseCapture( moveBitmap, abortMoveBitmap );
  78. }
  79. // Draw the bitmap at it's new position.
  80. image->SetPosition( aPanel->GetParent()->GetCrossHairPosition() );
  81. image->Draw( aPanel, aDC, wxPoint( 0, 0 ), GR_DEFAULT_DRAWMODE );
  82. }
  83. SCH_BITMAP* SCH_EDIT_FRAME::CreateNewImage( wxDC* aDC )
  84. {
  85. wxFileDialog fileDlg( this, _( "Choose Image" ), wxEmptyString, wxEmptyString,
  86. _( "Image Files " ) + wxImage::GetImageExtWildcard(),
  87. wxFD_OPEN );
  88. int diag = fileDlg.ShowModal();
  89. if( diag != wxID_OK )
  90. return NULL;
  91. wxString fullFilename = fileDlg.GetPath();
  92. if( !wxFileExists( fullFilename ) )
  93. {
  94. wxMessageBox( _( "Couldn't load image from <%s>" ), GetChars( fullFilename ) );
  95. return NULL;
  96. }
  97. wxPoint pos = GetCrossHairPosition();
  98. SCH_BITMAP* image = new SCH_BITMAP( pos );
  99. if( !image->ReadImageFile( fullFilename ) )
  100. {
  101. wxMessageBox( _( "Couldn't load image from <%s>" ), GetChars( fullFilename ) );
  102. delete image;
  103. return NULL;
  104. }
  105. image->SetFlags( IS_NEW | IS_MOVED );
  106. image->Draw( m_canvas, aDC, wxPoint( 0, 0 ), GR_DEFAULT_DRAWMODE );
  107. m_canvas->SetMouseCapture( moveBitmap, abortMoveBitmap );
  108. GetScreen()->SetCurItem( image );
  109. OnModify();
  110. return image;
  111. }
  112. void SCH_EDIT_FRAME::MoveImage( SCH_BITMAP* aImageItem, wxDC* aDC )
  113. {
  114. aImageItem->SetFlags( IS_MOVED );
  115. m_canvas->SetMouseCapture( moveBitmap, abortMoveBitmap );
  116. GetScreen()->SetCurItem( aImageItem );
  117. SetRepeatItem( NULL );
  118. SetUndoItem( aImageItem );
  119. m_canvas->CrossHairOff( aDC );
  120. SetCrossHairPosition( aImageItem->GetPosition() );
  121. m_canvas->MoveCursorToCrossHair();
  122. m_canvas->CrossHairOn( aDC );
  123. OnModify();
  124. }
  125. void SCH_EDIT_FRAME::RotateImage( SCH_BITMAP* aItem )
  126. {
  127. if( aItem->GetFlags( ) == 0 )
  128. SaveCopyInUndoList( aItem, UR_ROTATED, aItem->GetPosition() );
  129. aItem->Rotate( aItem->GetPosition() );
  130. OnModify();
  131. m_canvas->Refresh();
  132. }
  133. void SCH_EDIT_FRAME::MirrorImage( SCH_BITMAP* aItem, bool Is_X_axis )
  134. {
  135. if( aItem->GetFlags( ) == 0 )
  136. SaveCopyInUndoList( aItem, UR_CHANGED );
  137. if( Is_X_axis )
  138. aItem->MirrorX( aItem->GetPosition().y );
  139. else
  140. aItem->MirrorY( aItem->GetPosition().x );
  141. OnModify();
  142. m_canvas->Refresh();
  143. }
  144. void SCH_EDIT_FRAME::EditImage( SCH_BITMAP* aItem )
  145. {
  146. // TODO: change image scale or more
  147. DIALOG_IMAGE_EDITOR dlg( this, aItem->m_Image );
  148. if( dlg.ShowModal() != wxID_OK )
  149. return;
  150. // save old image in undo list if not already in edit
  151. // or the image to be edited is part of a block
  152. if( aItem->GetFlags() == 0 ||
  153. GetScreen()->m_BlockLocate.GetState() != STATE_NO_BLOCK )
  154. SaveCopyInUndoList( aItem, UR_CHANGED );
  155. dlg.TransfertToImage(aItem->m_Image);
  156. OnModify();
  157. m_canvas->Refresh();
  158. }