1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/* 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.
*/
#include <tqcursor.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#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>
#include <klocale.h>
#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;
int dx = tqRound( p.x() / dxy.width() );
int dy = tqRound( p.y() / dxy.height() );
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 ) ) );
if( distx < dist.width() )
{
if( TQABS(p.x() - dxy.width() * dx ) < TQABS( p.x() - dxy.width() * ( dx + 1 ) ) )
p.rx() = dxy.width() * dx;
else
p.rx() = dxy.width() * ( dx + 1 );
}
if( disty < dist.height() )
{
if( TQABS( p.y() - dxy.height() * dy ) < TQABS( p.y() - dxy.height() * ( dy + 1 ) ) )
p.ry() = dxy.height() * dy;
else
p.ry() = dxy.height() * ( dy + 1 );
}
return p;
}
VCanvas::VCanvas( TQWidget *tqparent, KarbonView* view, KarbonPart* part )
: TQScrollView( tqparent, "canvas", WStaticContents/*WNorthWestGravity*/ | WResizeNoErase |
WRepaintNoErase ), m_part( part ), m_view( view )
{
connect(this, TQT_SIGNAL( contentsMoving( int, int ) ), this, TQT_SLOT( slotContentsMoving( int, int ) ) );
viewport()->setFocusPolicy( TQ_StrongFocus );
viewport()->setMouseTracking( true );
setMouseTracking( true );
viewport()->setBackgroundColor( TQt::white );
viewport()->setBackgroundMode( TQWidget::NoBackground );
viewport()->installEventFilter( this );
resizeContents( 800, 600 );
m_pixmap = new TQPixmap( 800, 600 );
setFocus();
setAcceptDrops( true );
}
VCanvas::~VCanvas()
{
delete m_pixmap;
m_view = 0L;
m_part = 0L;
}
void
VCanvas::setPos( const KoPoint& p )
{
KoPoint p2 = toViewport( p );
TQCursor::setPos( mapToGlobal( TQPoint( int(p2.x()), int(p2.y()) ) ) );
}
bool
VCanvas::eventFilter( TQObject* object, TQEvent* event )
{
TQScrollView::eventFilter( object, event );
if( event->type() == TQEvent::AccelOverride || event->type() == TQEvent::Accel )
return TQScrollView::eventFilter( object, event );
if( event->type() == TQEvent::KeyPress || event->type() == TQEvent::KeyRelease )
return m_view->keyEvent( event );
TQMouseEvent* mouseEvent = dynamic_cast<TQMouseEvent*>( event );
if( mouseEvent && m_view )
{
KoPoint canvasCoordinate = toContents( KoPoint( mouseEvent->pos() ) );
return m_view->mouseEvent( mouseEvent, canvasCoordinate );
}
return false;
}
// This causes a tqrepaint normally, so just overwriting it omits the repainting
void
VCanvas::focusInEvent( TQFocusEvent * )
{
}
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 )
{
TQWMatrix mat;
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
VCanvas::viewportPaintEvent( TQPaintEvent *e )
{
TQRect eventRect = e->rect();
KoRect rect = KoRect::fromTQRect( eventRect );
setYMirroring( m_view->painterFactory()->editpainter() );
viewport()->setUpdatesEnabled( false );
VPainter *p = m_view->painterFactory()->painter();
// TODO : only update ROIs
p->begin();
p->clear( rect, TQColor( 195, 194, 193 ) );
p->setZoomFactor( m_view->zoom() );
setYMirroring( p );
// TRICK : slightly adjust the matrix so libart AA looks better
TQWMatrix mat = p->tqworldMatrix();
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:
VTQPainter qpainter( p->device() );
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
VCanvas::drawContents( TQPainter* painter, int clipx, int clipy,
int clipw, int cliph )
{
drawDocument( painter, KoRect( clipx, clipy, clipw, cliph ) );
}
void
VCanvas::drawDocument( TQPainter* /*painter*/, const KoRect&, bool drawVObjects )
{
setYMirroring( m_view->painterFactory()->editpainter() );
VPainter* p = m_view->painterFactory()->painter();
if( drawVObjects )
{
p->begin();
p->clear( TQColor( 195, 194, 193 ) );
p->setZoomFactor( m_view->zoom() );
setYMirroring( p );
// TRICK : slightly adjust the matrix so libart AA looks better
TQWMatrix mat = p->tqworldMatrix();
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:
VTQPainter qpainter( p->device() );
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
VCanvas::repaintAll( bool drawVObjects )
{
drawDocument( 0, KoRect( 0, 0, width(), height() ), drawVObjects );
}
/// repaints just a rect area (no scrolling)
void
VCanvas::repaintAll( const KoRect &r )
{
drawDocument( 0, r );
}
void
VCanvas::resizeEvent( TQResizeEvent* event )
{
double centerX = double( contentsX() + 0.5 * visibleWidth() ) / double( contentsWidth() );
double centerY = double( contentsY() + 0.5 * visibleHeight() ) / double( contentsHeight() );
TQScrollView::resizeEvent( event );
if( !m_pixmap )
m_pixmap = new TQPixmap( width(), height() );
else
m_pixmap->resize( width(), height() );
VPainter *p = m_view->painterFactory()->painter();
p->resize( width(), height() );
p->clear( TQColor( 195, 194, 193 ) );
setViewport( centerX, centerY );
}
void
VCanvas::slotContentsMoving( int /*x*/, int /*y*/ )
{
emit viewportChanged();
}
void
VCanvas::dragEnterEvent( TQDragEnterEvent *e )
{
e->accept( KarbonDrag::canDecode( e ) || KColorDrag::canDecode( e ) );
}
void
VCanvas::dropEvent( TQDropEvent *e )
{
m_view->dropEvent( e );
}
#include "vcanvas.moc"
|