24#include "karbon_view.h"
25#include "karbon_part.h"
26#include "karbon_drag.h"
31#include "vpainterfactory.h"
32#include "vselection.h"
33#include "vtoolcontroller.h"
38#include <kcolordrag.h>
41VCanvas::pageOffsetX()
const
43 double zoomedWidth = m_part->document().width() * m_view->zoom();
44 if( contentsWidth() < visibleWidth() )
45 return int( 0.5 * ( visibleWidth() - zoomedWidth ) );
47 return int( 0.5 * ( contentsWidth() - zoomedWidth ) );
51VCanvas::pageOffsetY()
const
53 double zoomedHeight = m_part->document().height() * m_view->zoom();
54 if( contentsHeight() < visibleHeight() )
55 return int( 0.5 * ( visibleHeight() - zoomedHeight ) );
57 return int( 0.5 * ( contentsHeight() - zoomedHeight ) );
60KoPoint VCanvas::snapToGrid(
const KoPoint &point )
62 if( !m_part->document().grid().isSnap )
67 KoSize dist = m_part->document().grid().snap;
68 KoSize dxy = m_part->document().grid().freq;
70 int dx = tqRound( p.x() / dxy.width() );
71 int dy = tqRound( p.y() / dxy.height() );
73 float distx = TQMIN( TQABS( p.x() - dxy.width() * dx ), TQABS( p.x() - dxy.width() * ( dx + 1 ) ) );
74 float disty = TQMIN( TQABS( p.y() - dxy.height() * dy ), TQABS( p.y() - dxy.height() * ( dy + 1 ) ) );
76 if( distx < dist.width() )
78 if( TQABS(p.x() - dxy.width() * dx ) < TQABS( p.x() - dxy.width() * ( dx + 1 ) ) )
79 p.rx() = dxy.width() * dx;
81 p.rx() = dxy.width() * ( dx + 1 );
84 if( disty < dist.height() )
86 if( TQABS( p.y() - dxy.height() * dy ) < TQABS( p.y() - dxy.height() * ( dy + 1 ) ) )
87 p.ry() = dxy.height() * dy;
89 p.ry() = dxy.height() * ( dy + 1 );
96VCanvas::VCanvas( TQWidget *parent, KarbonView* view,
KarbonPart* part )
97 : TQScrollView( parent,
"canvas", WStaticContents | WResizeNoErase |
98 WRepaintNoErase ), m_part( part ), m_view( view )
100 connect(
this, TQ_SIGNAL( contentsMoving(
int,
int ) ),
this, TQ_SLOT( slotContentsMoving(
int,
int ) ) );
101 viewport()->setFocusPolicy( TQWidget::StrongFocus );
103 viewport()->setMouseTracking(
true );
104 setMouseTracking(
true );
106 viewport()->setBackgroundColor( TQt::white );
107 viewport()->setBackgroundMode( TQWidget::NoBackground );
108 viewport()->installEventFilter(
this );
110 resizeContents( 800, 600 );
111 m_pixmap =
new TQPixmap( 800, 600 );
115 setAcceptDrops(
true );
126VCanvas::setPos(
const KoPoint& p )
128 KoPoint p2 = toViewport( p );
129 TQCursor::setPos( mapToGlobal( TQPoint(
int(p2.x()),
int(p2.y()) ) ) );
133VCanvas::eventFilter( TQObject*
object, TQEvent* event )
135 TQScrollView::eventFilter(
object, event );
137 if( event->type() == TQEvent::AccelOverride || event->type() == TQEvent::Accel )
138 return TQScrollView::eventFilter(
object, event );
140 if( event->type() == TQEvent::KeyPress || event->type() == TQEvent::KeyRelease )
141 return m_view->keyEvent( event );
143 TQMouseEvent* mouseEvent =
dynamic_cast<TQMouseEvent*
>( event );
145 if( mouseEvent && m_view )
147 KoPoint canvasCoordinate = toContents( KoPoint( mouseEvent->pos() ) );
148 return m_view->mouseEvent( mouseEvent, canvasCoordinate );
157VCanvas::focusInEvent( TQFocusEvent * )
162VCanvas::toViewport(
const KoPoint &p )
const
165 p2.setX( ( p.x() * m_view->zoom() ) - contentsX() + pageOffsetX() );
166 if( contentsHeight() > height() )
167 p2.setY( ( contentsHeight() - ( p.y() * m_view->zoom() + contentsY() + pageOffsetY() ) ) );
169 p2.setY( ( height() - p.y() * m_view->zoom() + pageOffsetY() ) );
174VCanvas::toContents(
const KoPoint &p )
const
177 p2.setX( ( p.x() + contentsX() - pageOffsetX() ) / m_view->zoom() );
178 if( contentsHeight() > height() )
179 p2.setY( ( contentsHeight() - ( p.y() + contentsY() + pageOffsetY()) ) / m_view->zoom() );
181 p2.setY( ( height() - p.y() - pageOffsetY() ) / m_view->zoom() );
186VCanvas::boundingBox()
const
189 KoPoint p2( width(), height() );
190 if( !m_view->documentDeleted() )
192 p1 = toContents( p1 );
193 p2 = toContents( p2 );
195 return KoRect( p1, p2 ).normalize();
199VCanvas::setYMirroring( VPainter *p )
204 mat.translate( pageOffsetX(), pageOffsetY() );
206 if( contentsHeight() > visibleHeight() )
207 mat.translate( -contentsX(), contentsY() - contentsHeight() );
209 mat.translate( 0, -visibleHeight() );
211 p->setWorldMatrix( mat );
215VCanvas::viewportPaintEvent( TQPaintEvent *e )
217 TQRect eventRect = e->rect();
218 KoRect rect = KoRect::fromTQRect( eventRect );
220 setYMirroring( m_view->painterFactory()->editpainter() );
221 viewport()->setUpdatesEnabled(
false );
222 VPainter *p = m_view->painterFactory()->painter();
226 p->clear( rect, TQColor( 195, 194, 193 ) );
227 p->setZoomFactor( m_view->zoom() );
231 TQWMatrix mat = p->worldMatrix();
232 p->setWorldMatrix( mat.translate( -.5, -.5 ) );
236 p->moveTo( rect.topLeft() );
237 p->lineTo( rect.topRight() );
238 p->lineTo( rect.bottomRight() );
239 p->lineTo( rect.bottomLeft() );
240 p->lineTo( rect.topLeft() );
243 m_part->document().drawPage( p, m_part->pageLayout(), m_view->showPageMargins() );
244 KoRect bbox = boundingBox();
245 m_part->document().draw( p, &bbox );
251 VTQPainter qpainter( p->device() );
252 setYMirroring( &qpainter );
253 qpainter.setZoomFactor( m_view->zoom() );
254 m_part->document().selection()->draw( &qpainter, m_view->zoom() );
256 if( m_view->toolController()->currentTool() )
257 m_view->toolController()->currentTool()->draw( &qpainter );
259 bitBlt( viewport(), eventRect.topLeft(), p->device(), eventRect );
260 viewport()->setUpdatesEnabled(
true );
264VCanvas::setViewport(
double centerX,
double centerY )
266 setContentsPos(
int( centerX * contentsWidth() - 0.5 * visibleWidth() ),
267 int( centerY * contentsHeight() - 0.5 * visibleHeight() ) );
271VCanvas::setViewportRect(
const KoRect &r )
273 viewport()->setUpdatesEnabled(
false );
274 double zoomX = m_view->zoom() * ( ( visibleWidth() / m_view->zoom() ) / r.width() );
275 double zoomY = m_view->zoom() * ( ( visibleHeight() / m_view->zoom() ) / r.height() );
276 double pageOffX = ( contentsWidth() - ( m_part->document().width() * m_view->zoom() ) ) / 2.0;
277 double centerX = double( ( r.center().x() ) * m_view->zoom() + pageOffX ) / double( contentsWidth() );
278 double pageOffY = ( contentsHeight() - ( m_part->document().height() * m_view->zoom() ) ) / 2.0;
279 double centerY = double( ( r.center().y() ) * m_view->zoom() + pageOffY ) / double( contentsHeight() );
280 double zoom = zoomX < zoomY ? zoomX : zoomY;
281 resizeContents(
int( ( zoom / m_view->zoom() ) * contentsWidth() ),
282 int( ( zoom / m_view->zoom() ) * contentsHeight() ) );
283 setViewport( centerX, 1.0 - centerY );
284 m_view->setZoomAt( zoom );
285 viewport()->setUpdatesEnabled(
true );
289VCanvas::drawContents( TQPainter* painter,
int clipx,
int clipy,
290 int clipw,
int cliph )
292 drawDocument( painter, KoRect( clipx, clipy, clipw, cliph ) );
296VCanvas::drawDocument( TQPainter* ,
const KoRect&,
bool drawVObjects )
298 setYMirroring( m_view->painterFactory()->editpainter() );
300 VPainter* p = m_view->painterFactory()->painter();
304 p->clear( TQColor( 195, 194, 193 ) );
305 p->setZoomFactor( m_view->zoom() );
308 TQWMatrix mat = p->worldMatrix();
309 p->setWorldMatrix( mat.translate( -.5, -.5 ) );
311 m_part->document().drawPage( p, m_part->pageLayout(), m_view->showPageMargins() );
312 KoRect r2 = boundingBox();
313 m_part->document().draw( p, &r2 );
319 VTQPainter qpainter( p->device() );
320 setYMirroring( &qpainter );
321 qpainter.setZoomFactor( m_view->zoom() );
322 m_part->document().selection()->draw( &qpainter, m_view->zoom() );
324 if( m_view->toolController()->currentTool() )
325 m_view->toolController()->currentTool()->draw( &qpainter );
327 bitBlt( viewport(), 0, 0, p->device(), 0, 0, width(), height() );
331VCanvas::repaintAll(
bool drawVObjects )
333 drawDocument( 0, KoRect( 0, 0, width(), height() ), drawVObjects );
338VCanvas::repaintAll(
const KoRect &r )
340 drawDocument( 0, r );
344VCanvas::resizeEvent( TQResizeEvent* event )
346 double centerX = double( contentsX() + 0.5 * visibleWidth() ) / double( contentsWidth() );
347 double centerY = double( contentsY() + 0.5 * visibleHeight() ) / double( contentsHeight() );
349 TQScrollView::resizeEvent( event );
351 m_pixmap =
new TQPixmap( width(), height() );
353 m_pixmap->resize( width(), height() );
355 VPainter *p = m_view->painterFactory()->painter();
356 p->resize( width(), height() );
357 p->clear( TQColor( 195, 194, 193 ) );
358 setViewport( centerX, centerY );
362VCanvas::slotContentsMoving(
int ,
int )
364 emit viewportChanged();
368VCanvas::dragEnterEvent( TQDragEnterEvent *e )
370 e->accept( KarbonDrag::canDecode( e ) || KColorDrag::canDecode( e ) );
374VCanvas::dropEvent( TQDropEvent *e )
376 m_view->dropEvent( e );
379#include "vcanvas.moc"
Keeps track of visual per document properties.