kicad/common/zoom.cpp

308 lines
9.5 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
* Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file zoom.cpp
*/
/*
* Manage zoom, grid step, and auto crop.
*/
#include <fctsys.h>
#include <id.h>
#include <class_drawpanel.h>
#include <class_drawpanel_gal.h>
#include <gal/graphics_abstraction_layer.h>
#include <view/view.h>
#include <class_base_screen.h>
#include <wxstruct.h>
#include <kicad_device_context.h>
#include <hotkeys_basic.h>
#include <menus_helpers.h>
#include <base_units.h>
void EDA_DRAW_FRAME::RedrawScreen( const wxPoint& aCenterPoint, bool aWarpPointer )
{
AdjustScrollBars( aCenterPoint );
// Move the mouse cursor to the on grid graphic cursor position
if( aWarpPointer )
m_canvas->MoveCursorToCrossHair();
m_canvas->Refresh();
m_canvas->Update();
}
void EDA_DRAW_FRAME::RedrawScreen2( const wxPoint& posBefore )
{
wxPoint dPos = posBefore - m_canvas->GetClientSize() / 2; // relative screen position to center before zoom
wxPoint newScreenPos = m_canvas->ToDeviceXY( GetScreen()->GetCrossHairPosition() ); // screen position of crosshair after zoom
wxPoint newCenter = m_canvas->ToLogicalXY( newScreenPos - dPos );
AdjustScrollBars( newCenter );
m_canvas->Refresh();
m_canvas->Update();
}
void EDA_DRAW_FRAME::Zoom_Automatique( bool aWarpPointer )
{
BASE_SCREEN* screen = GetScreen();
// Set the best zoom and get center point.
// BestZoom() can compute an illegal zoom if the client window size
// is small, say because frame is not maximized. So use the clamping form
// of SetZoom():
double bestzoom = BestZoom();
screen->SetScalingFactor( bestzoom );
if( screen->m_FirstRedraw )
screen->SetCrossHairPosition( screen->GetScrollCenterPosition() );
Introduction of Graphics Abstraction Layer based rendering for pcbnew. New classes: - VIEW - represents view that is seen by user, takes care of layer ordering & visibility and how it is displayed (which location, how much zoomed, etc.) - VIEW_ITEM - Base class for every item that can be displayed on VIEW (the biggest change is that now it may be necessary to override ViewBBox & ViewGetLayers method for derived classes). - EDA_DRAW_PANEL_GAL - Inherits after EDA_DRAW_PANEL, displays VIEW output, right now it is not editable (in opposite to usual EDA_DRAW_PANEL). - GAL/OPENGL_GAL/CAIRO_GAL - Base Graphics Abstraction Layer class + two different flavours (Cairo is not fully supported yet), that offers methods to draw primitives using different libraries. - WX_VIEW_CONTROLS - Controller for VIEW, handles user events, allows zooming, panning, etc. - PAINTER/PCB_PAINTER - Classes that uses GAL interface to draw items (as you may have already guessed - PCB_PAINTER is a class for drawing PCB specific object, PAINTER is an abstract class). Its methods are invoked by VIEW, when an item has to be drawn. To display a new type of item - you need to implement draw(ITEM_TYPE*) method that draws it using GAL methods. - STROKE_FONT - Implements stroke font drawing using GAL methods. Most important changes to Kicad original code: * EDA_ITEM now inherits from VIEW_ITEM, which is a base class for all drawable objects. * EDA_DRAW_FRAME contains both usual EDA_DRAW_PANEL and new EDA_DRAW_PANEL_GAL, that can be switched anytime. * There are some new layers for displaying multilayer pads, vias & pads holes (these are not shown yet on the right sidebar in pcbnew) * Display order of layers is different than in previous versions (if you are curious - you may check m_galLayerOrder@pcbnew/basepcbframe.cpp). Preserving usual order would result in not very natural display, such as showing silkscreen texts on the bottom. * Introduced new hotkey (Alt+F12) and new menu option (View->Switch canvas) for switching canvas during runtime. * Some of classes (mostly derived from BOARD_ITEM) now includes ViewBBox & ViewGetLayers methods. * Removed tools/class_painter.h, as now it is extended and included in source code. Build changes: * GAL-based rendering option is turned on by a new compilation CMake option KICAD_GAL. * When compiling with CMake option KICAD_GAL=ON, GLEW and Cairo libraries are required. * GAL-related code is compiled into a static library (common/libgal). * Build with KICAD_GAL=OFF should not need any new libraries and should come out as a standard version of Kicad Currently most of items in pcbnew can be displayed using OpenGL (to be done are DIMENSIONS and MARKERS). More details about GAL can be found in: http://www.ohwr.org/attachments/1884/view-spec.pdf
2013-04-02 06:54:03 +00:00
if( !m_galCanvasActive )
RedrawScreen( screen->GetScrollCenterPosition(), aWarpPointer );
}
2007-10-29 10:05:07 +00:00
/** Compute the zoom factor and the new draw offset to draw the
* selected area (Rect) in full window screen
2008-04-03 19:38:24 +00:00
* @param Rect = selected area to show after zooming
*/
void EDA_DRAW_FRAME::Window_Zoom( EDA_RECT& Rect )
{
// Compute the best zoom
Rect.Normalize();
wxSize size = m_canvas->GetClientSize();
2008-04-03 19:38:24 +00:00
// Use ceil to at least show the full rect
double scalex = (double) Rect.GetSize().x / size.x;
double bestscale = (double) Rect.GetSize().y / size.y;
bestscale = std::max( bestscale, scalex );
GetScreen()->SetScalingFactor( bestscale );
RedrawScreen( Rect.Centre(), true );
}
/**
* Function OnZoom
* Called from any zoom event (toolbar , hotkey or popup )
*/
void EDA_DRAW_FRAME::OnZoom( wxCommandEvent& event )
{
if( m_canvas == NULL )
return;
2009-01-07 15:59:49 +00:00
int id = event.GetId();
bool zoom_at_cursor = false;
BASE_SCREEN* screen = GetScreen();
wxPoint center = screen->GetScrollCenterPosition();
switch( id )
{
case ID_OFFCENTER_ZOOM_IN:
center = m_canvas->ToDeviceXY( screen->GetCrossHairPosition() );
if( screen->SetPreviousZoom() )
RedrawScreen2( center );
break;
2008-02-17 21:19:13 +00:00
case ID_POPUP_ZOOM_IN:
2009-01-07 15:59:49 +00:00
zoom_at_cursor = true;
center = screen->GetCrossHairPosition();
// fall thru
2009-01-07 15:59:49 +00:00
case ID_ZOOM_IN:
if( screen->SetPreviousZoom() )
RedrawScreen( center, zoom_at_cursor );
2009-01-07 15:59:49 +00:00
break;
case ID_OFFCENTER_ZOOM_OUT:
center = m_canvas->ToDeviceXY( screen->GetCrossHairPosition() );
if( screen->SetNextZoom() )
RedrawScreen2( center );
break;
2008-02-17 21:19:13 +00:00
case ID_POPUP_ZOOM_OUT:
2009-01-07 15:59:49 +00:00
zoom_at_cursor = true;
center = screen->GetCrossHairPosition();
// fall thru
2009-01-07 15:59:49 +00:00
case ID_ZOOM_OUT:
if( screen->SetNextZoom() )
RedrawScreen( center, zoom_at_cursor );
2009-01-07 15:59:49 +00:00
break;
case ID_ZOOM_REDRAW:
m_canvas->Refresh();
2009-01-07 15:59:49 +00:00
break;
case ID_POPUP_ZOOM_CENTER:
2011-02-17 18:34:27 +00:00
center = screen->GetCrossHairPosition();
RedrawScreen( center, true );
2009-01-07 15:59:49 +00:00
break;
case ID_ZOOM_PAGE:
2009-12-19 19:24:49 +00:00
Zoom_Automatique( false );
break;
case ID_POPUP_ZOOM_SELECT:
break;
case ID_POPUP_CANCEL:
m_canvas->MoveCursorToCrossHair();
break;
default:
unsigned i;
i = id - ID_POPUP_ZOOM_LEVEL_START;
if( i >= screen->m_ZoomList.size() )
{
2009-12-02 13:06:55 +00:00
wxLogDebug( wxT( "%s %d: index %d is outside the bounds of the zoom list." ),
__TFILE__, __LINE__, i );
return;
}
if( screen->SetZoom( screen->m_ZoomList[i] ) )
RedrawScreen( center, true );
}
if( m_galCanvasActive )
{
// Apply computed view settings to GAL
KiGfx::VIEW* view = m_galCanvas->GetView();
KiGfx::GAL* gal = m_galCanvas->GetGAL();
double zoomFactor = gal->GetWorldScale() / gal->GetZoomFactor();
double zoom = 1.0 / ( zoomFactor * GetZoom() );
view->SetScale( zoom );
view->SetCenter( VECTOR2D( center ) );
m_galCanvas->Refresh();
}
UpdateStatusBar();
}
/* add the zoom list menu the the MasterMenu.
* used in OnRightClick(wxMouseEvent& event)
*/
void EDA_DRAW_FRAME::AddMenuZoomAndGrid( wxMenu* MasterMenu )
{
int maxZoomIds;
int zoom;
wxString msg;
BASE_SCREEN* screen = m_canvas->GetScreen();
msg = AddHotkeyName( _( "Center" ), m_HotkeysZoomAndGridList, HK_ZOOM_CENTER );
AddMenuItem( MasterMenu, ID_POPUP_ZOOM_CENTER, msg, KiBitmap( zoom_center_on_screen_xpm ) );
msg = AddHotkeyName( _( "Zoom in" ), m_HotkeysZoomAndGridList, HK_ZOOM_IN );
AddMenuItem( MasterMenu, ID_POPUP_ZOOM_IN, msg, KiBitmap( zoom_in_xpm ) );
msg = AddHotkeyName( _( "Zoom out" ), m_HotkeysZoomAndGridList, HK_ZOOM_OUT );
AddMenuItem( MasterMenu, ID_POPUP_ZOOM_OUT, msg, KiBitmap( zoom_out_xpm ) );
msg = AddHotkeyName( _( "Redraw view" ), m_HotkeysZoomAndGridList, HK_ZOOM_REDRAW );
AddMenuItem( MasterMenu, ID_ZOOM_REDRAW, msg, KiBitmap( zoom_redraw_xpm ) );
msg = AddHotkeyName( _( "Zoom auto" ), m_HotkeysZoomAndGridList, HK_ZOOM_AUTO );
AddMenuItem( MasterMenu, ID_ZOOM_PAGE, msg, KiBitmap( zoom_fit_in_page_xpm ) );
wxMenu* zoom_choice = new wxMenu;
AddMenuItem( MasterMenu, zoom_choice,
ID_POPUP_ZOOM_SELECT, _( "Zoom select" ),
KiBitmap( zoom_selection_xpm ) );
zoom = screen->GetZoom();
maxZoomIds = ID_POPUP_ZOOM_LEVEL_END - ID_POPUP_ZOOM_LEVEL_START;
maxZoomIds = ( (size_t) maxZoomIds < screen->m_ZoomList.size() ) ?
maxZoomIds : screen->m_ZoomList.size();
// Populate zoom submenu.
for( int i = 0; i < maxZoomIds; i++ )
{
msg.Printf( wxT( "%g" ), screen->m_ZoomList[i] );
zoom_choice->Append( ID_POPUP_ZOOM_LEVEL_START + i, _( "Zoom: " ) + msg,
wxEmptyString, wxITEM_CHECK );
if( zoom == screen->m_ZoomList[i] )
zoom_choice->Check( ID_POPUP_ZOOM_LEVEL_START + i, true );
}
// Create grid submenu as required.
if( screen->GetGridCount() )
{
wxMenu* gridMenu = new wxMenu;
AddMenuItem( MasterMenu, gridMenu, ID_POPUP_GRID_SELECT,
_( "Grid Select" ), KiBitmap( grid_select_xpm ) );
GRID_TYPE tmp;
wxRealPoint grid = screen->GetGridSize();
for( size_t i = 0; i < screen->GetGridCount(); i++ )
{
tmp = screen->GetGrid( i );
double gridValueInch = To_User_Unit( INCHES, tmp.m_Size.x );
double gridValue_mm = To_User_Unit( MILLIMETRES, tmp.m_Size.x );
if( tmp.m_Id == ID_POPUP_GRID_USER )
{
msg = _( "User Grid" );
}
else
{
switch( g_UserUnit )
{
case INCHES:
msg.Printf( wxT( "%.1f mils, (%.4f mm)" ),
gridValueInch * 1000, gridValue_mm );
break;
case MILLIMETRES:
msg.Printf( wxT( "%.4f mm, (%.1f mils)" ),
gridValue_mm, gridValueInch * 1000 );
break;
case UNSCALED_UNITS:
msg = wxT( "???" );
break;
}
}
gridMenu->Append( tmp.m_Id, msg, wxEmptyString, true );
if( grid == tmp.m_Size )
gridMenu->Check( tmp.m_Id, true );
}
}
MasterMenu->AppendSeparator();
AddMenuItem( MasterMenu, ID_POPUP_CANCEL, _( "Close" ), KiBitmap( cancel_xpm ) );
}