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.
381 lines
10 KiB
381 lines
10 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (C) 2001, 2002, 2003 The Karbon Developers
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library 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
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
14 years ago
|
#include <tqcursor.h>
|
||
|
#include <tqpainter.h>
|
||
|
#include <tqpixmap.h>
|
||
15 years ago
|
|
||
|
#include "karbon_view.h"
|
||
|
#include "karbon_part.h"
|
||
|
#include "karbon_drag.h"
|
||
|
#include "vcanvas.h"
|
||
|
#include "vdocument.h"
|
||
|
#include "vpainter.h"
|
||
|
#include "vqpainter.h"
|
||
|
#include "vpainterfactory.h"
|
||
|
#include "vselection.h"
|
||
|
#include "vtoolcontroller.h"
|
||
|
#include "vtool.h"
|
||
|
|
||
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
15 years ago
|
#include <kcolordrag.h>
|
||
|
|
||
|
int
|
||
|
VCanvas::pageOffsetX() const
|
||
|
{
|
||
|
double zoomedWidth = m_part->document().width() * m_view->zoom();
|
||
|
if( contentsWidth() < visibleWidth() )
|
||
|
return int( 0.5 * ( visibleWidth() - zoomedWidth ) );
|
||
|
else
|
||
|
return int( 0.5 * ( contentsWidth() - zoomedWidth ) );
|
||
|
}
|
||
|
|
||
|
int
|
||
|
VCanvas::pageOffsetY() const
|
||
|
{
|
||
|
double zoomedHeight = m_part->document().height() * m_view->zoom();
|
||
|
if( contentsHeight() < visibleHeight() )
|
||
|
return int( 0.5 * ( visibleHeight() - zoomedHeight ) );
|
||
|
else
|
||
|
return int( 0.5 * ( contentsHeight() - zoomedHeight ) );
|
||
|
}
|
||
|
|
||
|
KoPoint VCanvas::snapToGrid( const KoPoint &point )
|
||
|
{
|
||
|
if( !m_part->document().grid().isSnap )
|
||
|
return point;
|
||
|
|
||
|
KoPoint p = point;
|
||
|
|
||
|
KoSize dist = m_part->document().grid().snap;
|
||
|
KoSize dxy = m_part->document().grid().freq;
|
||
|
|
||
14 years ago
|
int dx = tqRound( p.x() / dxy.width() );
|
||
|
int dy = tqRound( p.y() / dxy.height() );
|
||
15 years ago
|
|
||
14 years ago
|
float distx = TQMIN( TQABS( p.x() - dxy.width() * dx ), TQABS( p.x() - dxy.width() * ( dx + 1 ) ) );
|
||
|
float disty = TQMIN( TQABS( p.y() - dxy.height() * dy ), TQABS( p.y() - dxy.height() * ( dy + 1 ) ) );
|
||
15 years ago
|
|
||
|
if( distx < dist.width() )
|
||
|
{
|
||
14 years ago
|
if( TQABS(p.x() - dxy.width() * dx ) < TQABS( p.x() - dxy.width() * ( dx + 1 ) ) )
|
||
15 years ago
|
p.rx() = dxy.width() * dx;
|
||
|
else
|
||
|
p.rx() = dxy.width() * ( dx + 1 );
|
||
|
}
|
||
|
|
||
|
if( disty < dist.height() )
|
||
|
{
|
||
14 years ago
|
if( TQABS( p.y() - dxy.height() * dy ) < TQABS( p.y() - dxy.height() * ( dy + 1 ) ) )
|
||
15 years ago
|
p.ry() = dxy.height() * dy;
|
||
|
else
|
||
|
p.ry() = dxy.height() * ( dy + 1 );
|
||
|
}
|
||
|
|
||
|
return p;
|
||
|
}
|
||
|
|
||
|
|
||
13 years ago
|
VCanvas::VCanvas( TQWidget *parent, KarbonView* view, KarbonPart* part )
|
||
|
: TQScrollView( parent, "canvas", WStaticContents/*WNorthWestGravity*/ | WResizeNoErase |
|
||
15 years ago
|
WRepaintNoErase ), m_part( part ), m_view( view )
|
||
|
{
|
||
14 years ago
|
connect(this, TQT_SIGNAL( contentsMoving( int, int ) ), this, TQT_SLOT( slotContentsMoving( int, int ) ) );
|
||
|
viewport()->setFocusPolicy( TQ_StrongFocus );
|
||
15 years ago
|
|
||
|
viewport()->setMouseTracking( true );
|
||
|
setMouseTracking( true );
|
||
|
|
||
14 years ago
|
viewport()->setBackgroundColor( TQt::white );
|
||
|
viewport()->setBackgroundMode( TQWidget::NoBackground );
|
||
15 years ago
|
viewport()->installEventFilter( this );
|
||
|
|
||
|
resizeContents( 800, 600 );
|
||
14 years ago
|
m_pixmap = new TQPixmap( 800, 600 );
|
||
15 years ago
|
|
||
|
setFocus();
|
||
|
|
||
|
setAcceptDrops( true );
|
||
|
}
|
||
|
|
||
|
VCanvas::~VCanvas()
|
||
|
{
|
||
|
delete m_pixmap;
|
||
|
m_view = 0L;
|
||
|
m_part = 0L;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
VCanvas::setPos( const KoPoint& p )
|
||
|
{
|
||
|
KoPoint p2 = toViewport( p );
|
||
14 years ago
|
TQCursor::setPos( mapToGlobal( TQPoint( int(p2.x()), int(p2.y()) ) ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
bool
|
||
14 years ago
|
VCanvas::eventFilter( TQObject* object, TQEvent* event )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQScrollView::eventFilter( object, event );
|
||
15 years ago
|
|
||
14 years ago
|
if( event->type() == TQEvent::AccelOverride || event->type() == TQEvent::Accel )
|
||
|
return TQScrollView::eventFilter( object, event );
|
||
15 years ago
|
|
||
14 years ago
|
if( event->type() == TQEvent::KeyPress || event->type() == TQEvent::KeyRelease )
|
||
15 years ago
|
return m_view->keyEvent( event );
|
||
|
|
||
14 years ago
|
TQMouseEvent* mouseEvent = dynamic_cast<TQMouseEvent*>( event );
|
||
15 years ago
|
|
||
|
if( mouseEvent && m_view )
|
||
|
{
|
||
|
KoPoint canvasCoordinate = toContents( KoPoint( mouseEvent->pos() ) );
|
||
|
return m_view->mouseEvent( mouseEvent, canvasCoordinate );
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
13 years ago
|
// This causes a repaint normally, so just overwriting it omits the repainting
|
||
15 years ago
|
void
|
||
14 years ago
|
VCanvas::focusInEvent( TQFocusEvent * )
|
||
15 years ago
|
{
|
||
|
}
|
||
|
|
||
|
KoPoint
|
||
|
VCanvas::toViewport( const KoPoint &p ) const
|
||
|
{
|
||
|
KoPoint p2 = p;
|
||
|
p2.setX( ( p.x() * m_view->zoom() ) - contentsX() + pageOffsetX() );
|
||
|
if( contentsHeight() > height() )
|
||
|
p2.setY( ( contentsHeight() - ( p.y() * m_view->zoom() + contentsY() + pageOffsetY() ) ) );
|
||
|
else
|
||
|
p2.setY( ( height() - p.y() * m_view->zoom() + pageOffsetY() ) );
|
||
|
return p2;
|
||
|
}
|
||
|
|
||
|
KoPoint
|
||
|
VCanvas::toContents( const KoPoint &p ) const
|
||
|
{
|
||
|
KoPoint p2 = p;
|
||
|
p2.setX( ( p.x() + contentsX() - pageOffsetX() ) / m_view->zoom() );
|
||
|
if( contentsHeight() > height() )
|
||
|
p2.setY( ( contentsHeight() - ( p.y() + contentsY() + pageOffsetY()) ) / m_view->zoom() );
|
||
|
else
|
||
|
p2.setY( ( height() - p.y() - pageOffsetY() ) / m_view->zoom() );
|
||
|
return p2;
|
||
|
}
|
||
|
|
||
|
KoRect
|
||
|
VCanvas::boundingBox() const
|
||
|
{
|
||
|
KoPoint p1( 0, 0 );
|
||
|
KoPoint p2( width(), height() );
|
||
|
if( !m_view->documentDeleted() )
|
||
|
{
|
||
|
p1 = toContents( p1 );
|
||
|
p2 = toContents( p2 );
|
||
|
}
|
||
|
return KoRect( p1, p2 ).normalize();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
VCanvas::setYMirroring( VPainter *p )
|
||
|
{
|
||
14 years ago
|
TQWMatrix mat;
|
||
15 years ago
|
|
||
|
mat.scale( 1, -1 );
|
||
|
mat.translate( pageOffsetX(), pageOffsetY() );
|
||
|
|
||
|
if( contentsHeight() > visibleHeight() )
|
||
|
mat.translate( -contentsX(), contentsY() - contentsHeight() );
|
||
|
else
|
||
|
mat.translate( 0, -visibleHeight() );
|
||
|
|
||
|
p->setWorldMatrix( mat );
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
VCanvas::viewportPaintEvent( TQPaintEvent *e )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQRect eventRect = e->rect();
|
||
|
KoRect rect = KoRect::fromTQRect( eventRect );
|
||
15 years ago
|
|
||
|
setYMirroring( m_view->painterFactory()->editpainter() );
|
||
|
viewport()->setUpdatesEnabled( false );
|
||
|
VPainter *p = m_view->painterFactory()->painter();
|
||
|
|
||
|
// TODO : only update ROIs
|
||
|
p->begin();
|
||
14 years ago
|
p->clear( rect, TQColor( 195, 194, 193 ) );
|
||
15 years ago
|
p->setZoomFactor( m_view->zoom() );
|
||
|
setYMirroring( p );
|
||
|
|
||
|
// TRICK : slightly adjust the matrix so libart AA looks better
|
||
13 years ago
|
TQWMatrix mat = p->worldMatrix();
|
||
15 years ago
|
p->setWorldMatrix( mat.translate( -.5, -.5 ) );
|
||
|
|
||
|
// set up clippath
|
||
|
p->newPath();
|
||
|
p->moveTo( rect.topLeft() );
|
||
|
p->lineTo( rect.topRight() );
|
||
|
p->lineTo( rect.bottomRight() );
|
||
|
p->lineTo( rect.bottomLeft() );
|
||
|
p->lineTo( rect.topLeft() );
|
||
|
p->setClipPath();
|
||
|
|
||
|
m_part->document().drawPage( p, m_part->pageLayout(), m_view->showPageMargins() );
|
||
|
KoRect bbox = boundingBox();
|
||
|
m_part->document().draw( p, &bbox );
|
||
|
|
||
|
p->resetClipPath();
|
||
|
p->end();
|
||
|
|
||
|
// draw handle:
|
||
14 years ago
|
VTQPainter qpainter( p->device() );
|
||
15 years ago
|
setYMirroring( &qpainter );
|
||
|
qpainter.setZoomFactor( m_view->zoom() );
|
||
|
m_part->document().selection()->draw( &qpainter, m_view->zoom() );
|
||
|
|
||
|
if( m_view->toolController()->currentTool() )
|
||
|
m_view->toolController()->currentTool()->draw( &qpainter );
|
||
|
|
||
|
bitBlt( viewport(), eventRect.topLeft(), p->device(), eventRect );
|
||
|
viewport()->setUpdatesEnabled( true );
|
||
|
}
|
||
|
|
||
|
void
|
||
|
VCanvas::setViewport( double centerX, double centerY )
|
||
|
{
|
||
|
setContentsPos( int( centerX * contentsWidth() - 0.5 * visibleWidth() ),
|
||
|
int( centerY * contentsHeight() - 0.5 * visibleHeight() ) );
|
||
|
}
|
||
|
|
||
|
void
|
||
|
VCanvas::setViewportRect( const KoRect &r )
|
||
|
{
|
||
|
viewport()->setUpdatesEnabled( false );
|
||
|
double zoomX = m_view->zoom() * ( ( visibleWidth() / m_view->zoom() ) / r.width() );
|
||
|
double zoomY = m_view->zoom() * ( ( visibleHeight() / m_view->zoom() ) / r.height() );
|
||
|
double pageOffX = ( contentsWidth() - ( m_part->document().width() * m_view->zoom() ) ) / 2.0;
|
||
|
double centerX = double( ( r.center().x() ) * m_view->zoom() + pageOffX ) / double( contentsWidth() );
|
||
|
double pageOffY = ( contentsHeight() - ( m_part->document().height() * m_view->zoom() ) ) / 2.0;
|
||
|
double centerY = double( ( r.center().y() ) * m_view->zoom() + pageOffY ) / double( contentsHeight() );
|
||
|
double zoom = zoomX < zoomY ? zoomX : zoomY;
|
||
|
resizeContents( int( ( zoom / m_view->zoom() ) * contentsWidth() ),
|
||
|
int( ( zoom / m_view->zoom() ) * contentsHeight() ) );
|
||
|
setViewport( centerX, 1.0 - centerY );
|
||
|
m_view->setZoomAt( zoom );
|
||
|
viewport()->setUpdatesEnabled( true );
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
VCanvas::drawContents( TQPainter* painter, int clipx, int clipy,
|
||
15 years ago
|
int clipw, int cliph )
|
||
|
{
|
||
|
drawDocument( painter, KoRect( clipx, clipy, clipw, cliph ) );
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
VCanvas::drawDocument( TQPainter* /*painter*/, const KoRect&, bool drawVObjects )
|
||
15 years ago
|
{
|
||
|
setYMirroring( m_view->painterFactory()->editpainter() );
|
||
|
|
||
|
VPainter* p = m_view->painterFactory()->painter();
|
||
|
if( drawVObjects )
|
||
|
{
|
||
|
p->begin();
|
||
14 years ago
|
p->clear( TQColor( 195, 194, 193 ) );
|
||
15 years ago
|
p->setZoomFactor( m_view->zoom() );
|
||
|
setYMirroring( p );
|
||
|
// TRICK : slightly adjust the matrix so libart AA looks better
|
||
13 years ago
|
TQWMatrix mat = p->worldMatrix();
|
||
15 years ago
|
p->setWorldMatrix( mat.translate( -.5, -.5 ) );
|
||
|
|
||
|
m_part->document().drawPage( p, m_part->pageLayout(), m_view->showPageMargins() );
|
||
|
KoRect r2 = boundingBox();
|
||
|
m_part->document().draw( p, &r2 );
|
||
|
|
||
|
p->end();
|
||
|
}
|
||
|
|
||
|
// draw handle:
|
||
14 years ago
|
VTQPainter qpainter( p->device() );
|
||
15 years ago
|
setYMirroring( &qpainter );
|
||
|
qpainter.setZoomFactor( m_view->zoom() );
|
||
|
m_part->document().selection()->draw( &qpainter, m_view->zoom() );
|
||
|
|
||
|
if( m_view->toolController()->currentTool() )
|
||
|
m_view->toolController()->currentTool()->draw( &qpainter );
|
||
|
|
||
|
bitBlt( viewport(), 0, 0, p->device(), 0, 0, width(), height() );
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
VCanvas::repaintAll( bool drawVObjects )
|
||
15 years ago
|
{
|
||
|
drawDocument( 0, KoRect( 0, 0, width(), height() ), drawVObjects );
|
||
|
}
|
||
|
|
||
14 years ago
|
/// repaints just a rect area (no scrolling)
|
||
15 years ago
|
void
|
||
14 years ago
|
VCanvas::repaintAll( const KoRect &r )
|
||
15 years ago
|
{
|
||
|
drawDocument( 0, r );
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
VCanvas::resizeEvent( TQResizeEvent* event )
|
||
15 years ago
|
{
|
||
|
double centerX = double( contentsX() + 0.5 * visibleWidth() ) / double( contentsWidth() );
|
||
|
double centerY = double( contentsY() + 0.5 * visibleHeight() ) / double( contentsHeight() );
|
||
|
|
||
14 years ago
|
TQScrollView::resizeEvent( event );
|
||
15 years ago
|
if( !m_pixmap )
|
||
14 years ago
|
m_pixmap = new TQPixmap( width(), height() );
|
||
15 years ago
|
else
|
||
|
m_pixmap->resize( width(), height() );
|
||
|
|
||
|
VPainter *p = m_view->painterFactory()->painter();
|
||
|
p->resize( width(), height() );
|
||
14 years ago
|
p->clear( TQColor( 195, 194, 193 ) );
|
||
15 years ago
|
setViewport( centerX, centerY );
|
||
|
}
|
||
|
|
||
|
void
|
||
|
VCanvas::slotContentsMoving( int /*x*/, int /*y*/ )
|
||
|
{
|
||
|
emit viewportChanged();
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
VCanvas::dragEnterEvent( TQDragEnterEvent *e )
|
||
15 years ago
|
{
|
||
|
e->accept( KarbonDrag::canDecode( e ) || KColorDrag::canDecode( e ) );
|
||
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
VCanvas::dropEvent( TQDropEvent *e )
|
||
15 years ago
|
{
|
||
|
m_view->dropEvent( e );
|
||
|
}
|
||
|
|
||
|
#include "vcanvas.moc"
|
||
|
|