2016-06-08 11:19:53 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-05-30 10:30:09 +00:00
|
|
|
* Copyright (C) 2017-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-06-08 11:19:53 +00:00
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <class_draw_panel_gal.h>
|
2019-06-08 22:14:57 +00:00
|
|
|
#include <eda_draw_frame.h>
|
2019-05-14 12:39:34 +00:00
|
|
|
#include <preview_items/selection_area.h>
|
2017-08-31 02:35:30 +00:00
|
|
|
#include <tool/actions.h>
|
2019-05-14 12:39:34 +00:00
|
|
|
#include <tool/tool_manager.h>
|
2017-08-31 02:35:30 +00:00
|
|
|
#include <tool/zoom_tool.h>
|
2019-05-14 12:39:34 +00:00
|
|
|
#include <view/view.h>
|
2016-06-08 11:19:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
ZOOM_TOOL::ZOOM_TOOL() :
|
2017-08-31 02:35:30 +00:00
|
|
|
TOOL_INTERACTIVE( "common.Control.zoomTool" )
|
2016-06-08 11:19:53 +00:00
|
|
|
{
|
2021-07-15 19:26:35 +00:00
|
|
|
m_frame = nullptr;
|
2016-06-08 11:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZOOM_TOOL::~ZOOM_TOOL() {}
|
|
|
|
|
|
|
|
|
2022-05-30 10:30:09 +00:00
|
|
|
|
|
|
|
bool ZOOM_TOOL::Init()
|
|
|
|
{
|
|
|
|
auto& ctxMenu = m_menu.GetMenu();
|
|
|
|
|
|
|
|
// cancel current tool goes in main context menu at the top if present
|
|
|
|
ctxMenu.AddItem( ACTIONS::cancelInteractive, SELECTION_CONDITIONS::ShowAlways, 1 );
|
|
|
|
ctxMenu.AddSeparator( 1 );
|
|
|
|
|
|
|
|
// Finally, add the standard zoom/grid items
|
|
|
|
getEditFrame<EDA_DRAW_FRAME>()->AddStandardSubMenus( m_menu );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-08 11:19:53 +00:00
|
|
|
void ZOOM_TOOL::Reset( RESET_REASON aReason )
|
|
|
|
{
|
2017-08-31 02:35:30 +00:00
|
|
|
m_frame = getEditFrame<EDA_DRAW_FRAME>();
|
2016-06-08 11:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2022-08-30 20:58:43 +00:00
|
|
|
std::string tool;
|
|
|
|
|
|
|
|
if( aEvent.GetCommandStr() )
|
|
|
|
tool = *aEvent.GetCommandStr();
|
|
|
|
|
2019-07-15 12:15:58 +00:00
|
|
|
m_frame->PushTool( tool );
|
2016-06-08 11:19:53 +00:00
|
|
|
|
2020-10-09 00:08:44 +00:00
|
|
|
auto setCursor =
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ZOOM_IN );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set initial cursor
|
|
|
|
setCursor();
|
|
|
|
|
2019-06-27 21:33:48 +00:00
|
|
|
while( TOOL_EVENT* evt = Wait() )
|
2016-06-08 11:19:53 +00:00
|
|
|
{
|
2020-10-09 00:08:44 +00:00
|
|
|
setCursor();
|
2019-06-27 21:33:48 +00:00
|
|
|
|
2019-07-01 21:01:33 +00:00
|
|
|
if( evt->IsCancelInteractive() || evt->IsActivate() )
|
2020-11-19 20:08:58 +00:00
|
|
|
{
|
2016-06-08 11:19:53 +00:00
|
|
|
break;
|
2020-11-19 20:08:58 +00:00
|
|
|
}
|
2017-08-31 02:35:30 +00:00
|
|
|
else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
|
2016-06-08 11:19:53 +00:00
|
|
|
{
|
|
|
|
if( selectRegion() )
|
|
|
|
break;
|
|
|
|
}
|
2022-05-30 10:30:09 +00:00
|
|
|
else if( evt->IsClick( BUT_RIGHT ) )
|
|
|
|
{
|
|
|
|
SELECTION dummy;
|
|
|
|
m_menu.ShowContextMenu( dummy );
|
|
|
|
}
|
2016-06-08 11:19:53 +00:00
|
|
|
else
|
2020-11-19 20:08:58 +00:00
|
|
|
{
|
2019-06-16 11:06:49 +00:00
|
|
|
evt->SetPassEvent();
|
2020-11-19 20:08:58 +00:00
|
|
|
}
|
2016-06-08 11:19:53 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 07:23:56 +00:00
|
|
|
// Exit zoom tool
|
2020-11-19 20:08:58 +00:00
|
|
|
m_frame->GetCanvas()->SetCurrentCursor( KICURSOR::ARROW );
|
2019-07-15 12:15:58 +00:00
|
|
|
m_frame->PopTool( tool );
|
2016-06-08 11:19:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ZOOM_TOOL::selectRegion()
|
|
|
|
{
|
2019-06-13 17:28:55 +00:00
|
|
|
bool cancelled = false;
|
|
|
|
KIGFX::VIEW* view = getView();
|
|
|
|
EDA_DRAW_PANEL_GAL* canvas = m_frame->GetCanvas();
|
|
|
|
|
2016-06-08 11:19:53 +00:00
|
|
|
getViewControls()->SetAutoPan( true );
|
|
|
|
|
2017-03-10 13:34:06 +00:00
|
|
|
KIGFX::PREVIEW::SELECTION_AREA area;
|
2016-06-08 11:19:53 +00:00
|
|
|
view->Add( &area );
|
|
|
|
|
2019-06-27 21:33:48 +00:00
|
|
|
while( TOOL_EVENT* evt = Wait() )
|
2016-06-08 11:19:53 +00:00
|
|
|
{
|
2019-07-01 21:01:33 +00:00
|
|
|
if( evt->IsCancelInteractive() || evt->IsActivate() )
|
2016-06-08 11:19:53 +00:00
|
|
|
{
|
|
|
|
cancelled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-31 02:35:30 +00:00
|
|
|
if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
|
2016-06-08 11:19:53 +00:00
|
|
|
{
|
|
|
|
area.SetOrigin( evt->DragOrigin() );
|
|
|
|
area.SetEnd( evt->Position() );
|
2016-12-02 17:58:12 +00:00
|
|
|
view->SetVisible( &area, true );
|
|
|
|
view->Update( &area, KIGFX::GEOMETRY );
|
2016-06-08 11:19:53 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 02:35:30 +00:00
|
|
|
if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
|
2016-06-08 11:19:53 +00:00
|
|
|
{
|
2016-12-02 17:58:12 +00:00
|
|
|
view->SetVisible( &area, false );
|
2016-06-08 11:19:53 +00:00
|
|
|
auto selectionBox = area.ViewBBox();
|
|
|
|
|
|
|
|
if( selectionBox.GetWidth() == 0 || selectionBox.GetHeight() == 0 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-17 20:35:23 +00:00
|
|
|
VECTOR2D sSize = view->ToWorld( canvas->GetClientSize(), false );
|
|
|
|
VECTOR2D vSize = selectionBox.GetSize();
|
2017-08-31 02:35:30 +00:00
|
|
|
double scale;
|
2019-04-17 20:35:23 +00:00
|
|
|
double ratio = std::max( fabs( vSize.x / sSize.x ), fabs( vSize.y / sSize.y ) );
|
2017-08-31 02:35:30 +00:00
|
|
|
|
|
|
|
if( evt->IsMouseUp( BUT_LEFT ) )
|
|
|
|
scale = view->GetScale() / ratio;
|
|
|
|
else
|
|
|
|
scale = view->GetScale() * ratio;
|
|
|
|
|
2016-06-08 11:19:53 +00:00
|
|
|
view->SetScale( scale );
|
|
|
|
view->SetCenter( selectionBox.Centre() );
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 17:58:12 +00:00
|
|
|
view->SetVisible( &area, false );
|
2016-06-08 11:19:53 +00:00
|
|
|
view->Remove( &area );
|
|
|
|
getViewControls()->SetAutoPan( false );
|
|
|
|
|
|
|
|
return cancelled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-31 12:30:51 +00:00
|
|
|
void ZOOM_TOOL::setTransitions()
|
2016-06-08 11:19:53 +00:00
|
|
|
{
|
2017-08-31 02:35:30 +00:00
|
|
|
Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
|
2016-06-08 11:19:53 +00:00
|
|
|
}
|