293
275
int w = x + rect.width();
294
276
int h = y + rect.height();
296
TKSize dxy = actualGridFrequency();
278
KoSize dxy = m_pDoc->grid().freq;
300
p.set(0,0,UnitPoint);
302
282
painter.setPen(m_pDoc->grid().color);
304
284
QPoint p0 = mapToScreen(p);
310
painter.drawLine(p0.x(),y,p0.x(),h);
316
p.set(0,0,UnitPoint);
323
painter.drawLine(p0.x(),y,p0.x(),h);
329
p.set(0,0,UnitPoint);
336
painter.drawLine(x,p0.y(),w,p0.y());
342
p.set(0,0,UnitPoint);
349
painter.drawLine(x,p0.y(),w,p0.y());
287
p.rx() += dxy.width();
291
while (p0.x() <= w) {
292
painter.drawLine(p0.x(),y,p0.x(),h);
294
p.rx() += dxy.width();
302
p.rx() -= dxy.width();
306
while (p0.x() >= x) {
307
painter.drawLine(p0.x(),y,p0.x(),h);
309
p.rx() -= dxy.width();
317
p.ry() += dxy.height();
321
while (p0.y() <= h) {
322
painter.drawLine(x,p0.y(),w,p0.y());
324
p.ry() += dxy.height();
332
p.ry() -= dxy.height();
336
while (p0.y() >= y) {
337
painter.drawLine(x,p0.y(),w,p0.y());
339
p.ry() -= dxy.height();
352
340
p0 = mapToScreen(p);
356
344
painter.translate(-m_iXOffset,-m_iYOffset);
358
QPoint p0 = actualPaperOrigin();
359
int pw = (int)actualPaperSizePt().w;
360
int ph = (int)actualPaperSizePt().h;
346
KoPageLayout pl = page->paperLayout();
347
int pw = m_pView->zoomHandler()->zoomItX(pl.ptWidth);
348
int ph = m_pView->zoomHandler()->zoomItY(pl.ptHeight);
364
350
if (m_pView->isShowPageMargins()) {
366
TKPageLayout pl = page->paperLayout();
367
int ml = (int)(pl.ptLeft()*zf);
368
int mt = (int)(pl.ptTop()*zf);
369
int mr = (int)(pl.ptRight()*zf);
370
int mb = (int)(pl.ptBottom()*zf);
351
int ml = m_pView->zoomHandler()->zoomItX(pl.ptLeft);
352
int mt = m_pView->zoomHandler()->zoomItY(pl.ptTop);
353
int mr = m_pView->zoomHandler()->zoomItX(pl.ptRight);
354
int mb = m_pView->zoomHandler()->zoomItY(pl.ptBottom);
373
357
painter.setPen(QPen(blue,1,DotLine));
374
painter.drawRect(px0+ml,py0+mt,pw-ml-mr,ph-mt-mb);
358
painter.drawRect(ml,mt,pw-ml-mr,ph-mt-mb);
375
359
painter.restore();
378
362
if (m_pView->isShowPageBorders()) {
379
363
painter.setPen(black);
380
painter.fillRect(px0+pw,py0+3,5,ph,gray);
381
painter.fillRect(px0+3,py0+ph,pw,3,gray);
382
painter.drawRect(px0,py0,pw,ph);
364
painter.fillRect(pw, 3, 5, ph, gray);
365
painter.fillRect(3, ph, pw, 3, gray);
366
painter.drawRect(0, 0, pw, ph);
386
370
KivioScreenPainter kpainter;
387
371
kpainter.start( m_buffer );
388
372
kpainter.painter()->translate( -m_iXOffset, -m_iYOffset );
389
kpainter.painter()->translate( px0, py0 );
390
m_pDoc->paintContent(kpainter, rect, false, page, p0, m_pZoom, true);
373
kpainter.painter()->translate( 0, 0 );
374
m_pDoc->paintContent(kpainter, rect, false, page, QPoint(0, 0), m_pView->zoomHandler(), true);
393
377
paintGuides(false);
396
380
bitBlt(this,rect.left(),rect.top(),m_buffer,rect.left(),rect.top(),rect.width(),rect.height());
399
void KivioCanvas::centerPage()
401
m_pHorzScrollBar->setValue(0);
402
m_pVertScrollBar->setValue(0);
405
383
void KivioCanvas::updateScrollBars()
407
m_pScrollX = (int)actualPaperSizePt().w/2;
408
m_pScrollY = (int)actualPaperSizePt().h/2;
385
KoPageLayout pl = activePage()->paperLayout();
386
m_pScrollX = m_pView->zoomHandler()->zoomItX(pl.ptWidth) - width();
387
m_pScrollY = m_pView->zoomHandler()->zoomItY(pl.ptHeight) - height();
410
m_pHorzScrollBar->setRange(-m_pScrollX,m_pScrollX);
389
m_pHorzScrollBar->setRange(0, m_pScrollX);
411
390
if ( m_pHorzScrollBar->value() > m_pHorzScrollBar->maxValue() ||
412
391
m_pHorzScrollBar->value() < m_pHorzScrollBar->minValue() )
414
393
m_pHorzScrollBar->setValue(0);
417
m_pVertScrollBar->setRange(-m_pScrollY,m_pScrollY);
396
m_pVertScrollBar->setRange(0, m_pScrollY);
418
397
if ( m_pVertScrollBar->value() > m_pVertScrollBar->maxValue() ||
419
398
m_pVertScrollBar->value() < m_pVertScrollBar->minValue() )
428
407
QSize KivioCanvas::actualSize()
430
return QSize(2*m_pScrollX + width(), 2*m_pScrollY + height());
433
QPoint KivioCanvas::actualPaperOrigin()
435
int pw = (int)actualPaperSizePt().w;
436
int ph = (int)actualPaperSizePt().h;
437
int px0 = (width()-pw)/2;
438
int py0 = (height()-ph)/2;
440
return QPoint(px0,py0);
443
float KivioCanvas::zoom()
449
void KivioCanvas::setZoom(float zoom)
451
m_pZoom = QMAX(0.05f,QMIN(100.0f,zoom));
409
return QSize(m_pScrollX, m_pScrollY);
412
void KivioCanvas::setZoom(int zoom)
414
if(zoom < 10 || zoom > 2000) {
418
m_pView->zoomHandler()->setZoomAndResolution(zoom, QPaintDevice::x11AppDpiX(),
419
QPaintDevice::x11AppDpiY());
453
421
updateScrollBars();
454
updateRulers(true,true);
459
emit zoomChanges(m_pZoom);
460
426
emit visibleAreaChanged();
463
TKSize KivioCanvas::actualGridFrequency()
429
KoSize KivioCanvas::actualGridFrequency()
469
actual = m_pDoc->grid().freq;
472
actual.convertToPt(m_pZoom);
473
} while ( actual.w < 10 || actual.h < 10 );
475
actual = m_pDoc->grid().freq;
432
actual.setWidth(m_pView->zoomHandler()->zoomItX(m_pDoc->grid().freq.width()));
433
actual.setHeight(m_pView->zoomHandler()->zoomItY(m_pDoc->grid().freq.height()));
482
TKSize KivioCanvas::actualPaperSizePt()
485
TKPageLayout pl = activePage()->paperLayout();
487
float w = pl.ptWidth()*m_pZoom;
488
float h = pl.ptHeight()*m_pZoom;
489
ps.set(w,h,UnitPoint);
494
void KivioCanvas::toggleShowRulers( bool b )
505
438
bool KivioCanvas::event( QEvent* e )
507
440
bool f = QWidget::event(e);
519
452
void KivioCanvas::leaveEvent( QEvent* )
521
m_pVRuler->updatePointer(-1,-1);
522
m_pHRuler->updatePointer(-1,-1);
454
m_pView->setMousePos(-1, -1);
525
457
void KivioCanvas::mousePressEvent(QMouseEvent* e)
527
459
if(!m_pDoc->isReadWrite())
529
466
if(m_pView->isShowGuides())
531
468
lastPoint = e->pos();
532
TKPoint p = mapFromScreen(e->pos());
469
KoPoint p = mapFromScreen(e->pos());
533
470
KivioGuideLines* gl = activePage()->guideLines();
535
472
bool unselectAllGuideLines = true;
536
473
pressGuideline = 0;
538
475
if ((e->state() & ~ShiftButton) == NoButton) {
539
KivioGuideLineData* gd = gl->find(p.x,p.y,2.0/m_pZoom);
476
KivioGuideLineData* gd = gl->find(p.x(),p.y(), m_pView->zoomHandler()->unzoomItY(2));
541
478
pressGuideline = gd;
542
479
if ((e->button() == RightButton) || ((e->button() & ShiftButton) == ShiftButton)) {
586
535
void KivioCanvas::mouseMoveEvent(QMouseEvent* e)
588
if(!m_pDoc->isReadWrite())
537
if(!m_pDoc->isReadWrite())
541
continuePasteMoving(e->pos());
590
543
if(m_pView->isShowGuides())
592
m_pVRuler->updatePointer(e->pos().x(),e->pos().y());
593
m_pHRuler->updatePointer(e->pos().x(),e->pos().y());
595
TKPoint p = mapFromScreen(e->pos());
596
KivioGuideLines* gl = activePage()->guideLines();
598
if ((e->state() & LeftButton == LeftButton) && gl->hasSelected()) {
599
if (m_guideLinesTimer->isActive()) {
600
m_guideLinesTimer->stop();
601
guideLinesTimerTimeout();
545
m_pView->setMousePos(e->pos().x(),e->pos().y());
547
KivioGuideLines* gl = activePage()->guideLines();
549
if ((e->state() & LeftButton == LeftButton) && gl->hasSelected()) {
550
if (m_guideLinesTimer->isActive()) {
551
m_guideLinesTimer->stop();
552
guideLinesTimerTimeout();
554
delegateThisEvent = false;
559
gl->moveSelectedByX(m_pView->zoomHandler()->unzoomItX(p.x()));
561
gl->moveSelectedByY(m_pView->zoomHandler()->unzoomItY(p.y()));
562
m_pDoc->setModified( true );
565
if ((e->state() & ~ShiftButton) == NoButton) {
566
KoPoint p = mapFromScreen(e->pos());
567
KivioGuideLineData* gd = gl->find(p.x(), p.y(), m_pView->zoomHandler()->unzoomItY(2));
603
569
delegateThisEvent = false;
608
gl->moveSelectedByX(p.x()/m_pZoom);
610
gl->moveSelectedByY(p.y()/m_pZoom);
613
if ((e->state() & ~ShiftButton) == NoButton) {
614
KivioGuideLineData* gd = gl->find(p.x,p.y,2.0/m_pZoom);
616
delegateThisEvent = false;
618
storedCursor = new QCursor(cursor());
619
setCursor(gd->orientation()==Qt::Vertical ? sizeHorCursor:sizeVerCursor);
621
updateGuidesCursor();
571
storedCursor = new QCursor(cursor());
572
setCursor(gd->orientation()==Qt::Vertical ? sizeHorCursor:sizeVerCursor);
574
updateGuidesCursor();
626
// float xf = p.xToUnit(UnitMillimeter);
627
// float yf = p.yToUnit(UnitMillimeter);
629
// debug("%s %s",(const char*)QString::number(xf,'f',2),(const char*)QString::number(yf,'f',2));
630
lastPoint = e->pos();
581
lastPoint = e->pos();
633
QPoint KivioCanvas::mapToScreen( TKPoint pos )
584
QPoint KivioCanvas::mapToScreen( KoPoint pos )
636
QPoint p0 = actualPaperOrigin();
637
int x = (int)(pos.x*m_pZoom);
638
int y = (int)(pos.y*m_pZoom);
587
int x = m_pView->zoomHandler()->zoomItX(pos.x());
588
int y = m_pView->zoomHandler()->zoomItY(pos.y());
640
p.setX( x + p0.x() - m_iXOffset );
641
p.setY( y + p0.y() - m_iYOffset );
590
p.setX( x - m_iXOffset );
591
p.setY( y - m_iYOffset );
646
TKPoint KivioCanvas::mapFromScreen( QPoint pos )
596
KoPoint KivioCanvas::mapFromScreen( const QPoint & pos )
648
QPoint p0 = actualPaperOrigin();
649
int x = pos.x() + m_iXOffset - p0.x();
650
int y = pos.y() + m_iYOffset - p0.y();
652
float xf = x/m_pZoom;
653
float yf = y/m_pZoom;
656
p.set(xf,yf,UnitPoint);
598
int x = pos.x() + m_iXOffset;
599
int y = pos.y() + m_iYOffset;
600
double xf = m_pView->zoomHandler()->unzoomItX(x);
601
double yf = m_pView->zoomHandler()->unzoomItY(y);
713
659
void KivioCanvas::startSpawnerDragDraw( const QPoint &p )
715
currRect = QRect( 0, 0, -1, -1 );
717
KivioStencilSpawner *pSpawner = KivioIconView::curDragSpawner();
721
// If we for some reason didn't delete an old drag stencil,
725
kdDebug() << "KivioCanvas::startSpawnerDragDraw() - m_pDragStencil still exists. BUG!" << endl;
726
delete m_pDragStencil;
730
// Map the point from screenspace to page space
731
TKPoint qp = mapFromScreen( p );
734
// Allocate a new stencil for dragging around
735
m_pDragStencil = pSpawner->newStencil();
736
m_pDragStencil->setPosition( qp.x, qp.y );
737
// m_pDragStencil->setDimensions( pSpawner->defWidth(), pSpawner->defHeight() );
739
// Invalidate the rectangle
742
// Create a new painter object
743
beginUnclippedSpawnerPainter();
745
// Calculate the zoom value
748
// Translate the painter so that 0,0 means where the page starts on the canvas
749
QPoint paperOrigin = actualPaperOrigin();
750
unclippedSpawnerPainter->painter()->save();
751
unclippedSpawnerPainter->painter()->translate( paperOrigin.x() - m_iXOffset, paperOrigin.y() - m_iYOffset );
753
// Assign the painter object to the intra-stencil data object, as well
754
// as the zoom factor
755
m_dragStencilData.painter = unclippedSpawnerPainter;
756
m_dragStencilData.scale = zf;
758
// Draw the outline of the stencil
759
m_pDragStencil->paintOutline( &m_dragStencilData );
761
unclippedSpawnerPainter->painter()->restore();
661
currRect = QRect( 0, 0, -1, -1 );
663
KivioStencilSpawner *pSpawner = KivioIconView::curDragSpawner();
667
// If we for some reason didn't delete an old drag stencil,
671
kdDebug(43000) << "KivioCanvas::startSpawnerDragDraw() - m_pDragStencil still exists. BUG!" << endl;
672
delete m_pDragStencil;
676
// Map the point from screenspace to page space
677
KoPoint qp = mapFromScreen( p );
680
// Allocate a new stencil for dragging around
681
m_pDragStencil = pSpawner->newStencil();
682
m_pDragStencil->setPosition( qp.x(), qp.y() );
684
// Invalidate the rectangle
687
// Create a new painter object
688
beginUnclippedSpawnerPainter();
690
// Translate the painter so that 0,0 means where the page starts on the canvas
691
unclippedSpawnerPainter->painter()->save();
692
unclippedSpawnerPainter->painter()->translate( -m_iXOffset, -m_iYOffset );
694
// Assign the painter object to the intra-stencil data object, as well
695
// as the zoom factor
696
m_dragStencilData.painter = unclippedSpawnerPainter;
697
m_dragStencilData.zoomHandler = m_pView->zoomHandler();
699
// Draw the outline of the stencil
700
m_pDragStencil->paintOutline( &m_dragStencilData );
702
unclippedSpawnerPainter->painter()->restore();
768
708
void KivioCanvas::continueSpawnerDragDraw( const QPoint &p )
770
bool snappedX, snappedY;
775
// Translate the painter so that 0,0 means where the page starts on the canvas
776
QPoint paperOrigin = actualPaperOrigin();
777
unclippedSpawnerPainter->painter()->save();
778
unclippedSpawnerPainter->painter()->translate( paperOrigin.x() - m_iXOffset, paperOrigin.y() - m_iYOffset );
780
// Undraw the old outline
783
m_pDragStencil->paintOutline( &m_dragStencilData );
786
// Map the new point from screenspace to page space
787
TKPoint orig = mapFromScreen(p2);
788
TKPoint qp = snapToGrid( orig );
790
// First snap to screen
792
m_pDragStencil->setPosition( qp.x, qp.y );
794
// Now snap to the guides
795
qp.set( orig.x + m_pDragStencil->w(), orig.y + m_pDragStencil->h(), UnitPoint );
796
qp = snapToGuides(qp, snappedX, snappedY);
797
if( snappedX==true ) {
798
m_pDragStencil->setX(qp.x - m_pDragStencil->w());
800
if( snappedY==true ) {
801
m_pDragStencil->setY(qp.y - m_pDragStencil->h());
804
qp.set( orig.x, orig.y, UnitPoint );
805
qp = snapToGuides(qp, snappedX, snappedY);
806
if( snappedX==true ) {
807
m_pDragStencil->setX(qp.x);
809
if( snappedY==true ) {
810
m_pDragStencil->setY(qp.y);
813
// Redraw the new outline
710
bool snappedX, snappedY;
712
// Translate the painter so that 0,0 means where the page starts on the canvas
713
unclippedSpawnerPainter->painter()->save();
714
unclippedSpawnerPainter->painter()->translate( -m_iXOffset, -m_iYOffset );
716
// Undraw the old outline
815
719
m_pDragStencil->paintOutline( &m_dragStencilData );
816
unclippedSpawnerPainter->painter()->restore();
722
// Map the new point from screenspace to page space
723
KoPoint orig = mapFromScreen(p);
724
KoPoint qp = snapToGrid( orig );
726
// First snap to screen
728
m_pDragStencil->setPosition( qp.x(), qp.y() );
730
// Now snap to the guides
731
qp.setCoords(orig.x() + m_pDragStencil->w(), orig.y() + m_pDragStencil->h());
732
qp = snapToGuides(qp, snappedX, snappedY);
735
m_pDragStencil->setX(qp.x() - m_pDragStencil->w());
739
m_pDragStencil->setY(qp.y() - m_pDragStencil->h());
742
qp.setCoords(orig.x(), orig.y());
743
qp = snapToGuides(qp, snappedX, snappedY);
746
m_pDragStencil->setX(qp.x());
750
m_pDragStencil->setY(qp.y());
753
// Redraw the new outline
755
m_pDragStencil->paintOutline( &m_dragStencilData );
756
unclippedSpawnerPainter->painter()->restore();
824
764
void KivioCanvas::endSpawnerDragDraw()
827
if ( !unclippedSpawnerPainter )
830
// If we have a valid old drawing spot, undraw it
833
QPoint paperOrigin = actualPaperOrigin();
834
unclippedSpawnerPainter->painter()->save();
835
unclippedSpawnerPainter->painter()->translate( paperOrigin.x() - m_iXOffset, paperOrigin.y() - m_iYOffset );
836
m_pDragStencil->paintOutline( &m_dragStencilData );
837
unclippedSpawnerPainter->painter()->restore();
840
// Smack the painter around a bit
841
endUnclippedSpawnerPainter();
843
// If we have a stencil we were dragging around, delete it.
846
delete m_pDragStencil;
767
if ( !unclippedSpawnerPainter )
770
// If we have a valid old drawing spot, undraw it
773
unclippedSpawnerPainter->painter()->save();
774
unclippedSpawnerPainter->painter()->translate( -m_iXOffset, -m_iYOffset );
775
m_pDragStencil->paintOutline( &m_dragStencilData );
776
unclippedSpawnerPainter->painter()->restore();
779
// Smack the painter around a bit
780
endUnclippedSpawnerPainter();
782
// If we have a stencil we were dragging around, delete it.
785
delete m_pDragStencil;
1157
1092
gl->removeSelected();
1159
1094
updateGuidesCursor();
1095
m_pDoc->setModified( true );
1161
1097
activePage()->deleteSelectedStencils();
1162
1098
m_pDoc->updateView(activePage());
1169
TKPoint KivioCanvas::snapToGridAndGuides(TKPoint point)
1173
TKSize dist = m_pDoc->grid().snap;
1174
TKSize dxy = m_pDoc->grid().freq;
1179
int dx = (int)(p.x/dxy.w);
1180
int dy = (int)(p.y/dxy.h);
1182
float distx = QMIN(QABS(p.x-dxy.w*dx),QABS(p.x-dxy.w*(dx+1)));
1183
float disty = QMIN(QABS(p.y-dxy.h*dy),QABS(p.y-dxy.h*(dy+1)));
1185
if( m_pDoc->grid().isSnap)
1187
if ( distx < dist.w) {
1188
if ( QABS(p.x-dxy.w*dx) < QABS(p.x-dxy.w*(dx+1)) )
1194
if ( disty < dist.h) {
1195
if ( QABS(p.y-dxy.h*dy) < QABS(p.y-dxy.h*(dy+1)) )
1203
* Now if the point is within 4 pixels of a gridline, snap
1206
if (m_pView->isSnapGuides())
1208
float four = 4.0f / m_pZoom;
1209
KivioGuideLines *pGuides = activePage()->guideLines();
1210
KivioGuideLineData *pData = pGuides->findHorizontal( point.y, four );
1213
p.y = (float)pData->position();
1216
pData = pGuides->findVertical( point.x, four );
1219
p.x = (float)pData->position();
1226
TKPoint KivioCanvas::snapToGrid(TKPoint point)
1228
if (!m_pDoc->grid().isSnap)
1233
TKSize dist = m_pDoc->grid().snap;
1234
TKSize dxy = m_pDoc->grid().freq;
1239
int dx = (int)(p.x/dxy.w);
1240
int dy = (int)(p.y/dxy.h);
1242
float distx = QMIN(QABS(p.x-dxy.w*dx),QABS(p.x-dxy.w*(dx+1)));
1243
float disty = QMIN(QABS(p.y-dxy.h*dy),QABS(p.y-dxy.h*(dy+1)));
1245
if ( distx < dist.w) {
1246
if ( QABS(p.x-dxy.w*dx) < QABS(p.x-dxy.w*(dx+1)) )
1252
if ( disty < dist.h) {
1253
if ( QABS(p.y-dxy.h*dy) < QABS(p.y-dxy.h*(dy+1)) )
1262
TKPoint KivioCanvas::snapToGuides(TKPoint point, bool &snappedX, bool &snappedY)
1268
if (m_pView->isSnapGuides())
1270
float four = 4.0f / m_pZoom;
1271
KivioGuideLines *pGuides = activePage()->guideLines();
1272
KivioGuideLineData *pData = pGuides->findHorizontal( point.y, four );
1276
p.y = (float)pData->position();
1279
pData = pGuides->findVertical( point.x, four );
1283
p.x = (float)pData->position();
1290
float KivioCanvas::snapToGridX(float z)
1293
p.set(z,0,UnitPoint);
1294
return snapToGrid(p).x;
1297
float KivioCanvas::snapToGridY(float z)
1300
p.set(0,z,UnitPoint);
1301
return snapToGrid(p).y;
1103
m_pToolsController->activateDefault();
1108
KoPoint KivioCanvas::snapToGridAndGuides(KoPoint point)
1112
KoSize dist = m_pDoc->grid().snap;
1113
KoSize dxy = m_pDoc->grid().freq;
1115
int dx = qRound(p.x()/dxy.width());
1116
int dy = qRound(p.y()/dxy.height());
1118
float distx = QMIN(QABS(p.x() - dxy.width() * dx), QABS(p.x() - dxy.width() * (dx + 1)));
1119
float disty = QMIN(QABS(p.y() - dxy.height() * dy), QABS(p.y() - dxy.height()* (dy + 1)));
1121
if( m_pDoc->grid().isSnap)
1123
if ( distx < dist.width()) {
1124
if (QABS(p.x() - dxy.width() * dx) < QABS(p.x() - dxy.width() * (dx + 1))) {
1125
p.rx() = dxy.width() * dx;
1127
p.rx() = dxy.width() * (dx + 1);
1131
if ( disty < dist.height()) {
1132
if (QABS(p.y() - dxy.height() * dy) < QABS(p.y() - dxy.height() * (dy + 1))) {
1133
p.ry() = dxy.height() * dy;
1135
p.ry() = dxy.height() * (dy + 1);
1141
* Now if the point is within 4 pixels of a gridline, snap
1144
if (m_pView->isSnapGuides())
1146
float four = m_pView->zoomHandler()->unzoomItY(4);
1147
KivioGuideLines *pGuides = activePage()->guideLines();
1148
KivioGuideLineData *pData = pGuides->findHorizontal( point.y(), four );
1152
p.ry() = (float)pData->position();
1155
pData = pGuides->findVertical( point.x(), four );
1159
p.rx() = (float)pData->position();
1166
KoPoint KivioCanvas::snapToGrid(KoPoint point)
1168
if (!m_pDoc->grid().isSnap)
1173
KoSize dist = m_pDoc->grid().snap;
1174
KoSize dxy = m_pDoc->grid().freq;
1176
int dx = qRound(p.x() / dxy.width());
1177
int dy = qRound(p.y() / dxy.height());
1179
float distx = QMIN(QABS(p.x() - dxy.width() * dx), QABS(p.x() - dxy.width() * (dx + 1)));
1180
float disty = QMIN(QABS(p.y() - dxy.height() * dy), QABS(p.y() - dxy.height() * (dy + 1)));
1182
if(distx < dist.width()) {
1183
if(QABS(p.x() - dxy.width() * dx) < QABS(p.x() - dxy.width() * (dx + 1))) {
1184
p.rx() = dxy.width() * dx;
1186
p.rx() = dxy.width() * (dx + 1);
1190
if(disty < dist.height()) {
1191
if(QABS(p.y() - dxy.height() * dy) < QABS(p.y() - dxy.height() * (dy + 1))) {
1192
p.ry() = dxy.height() * dy;
1194
p.ry() = dxy.height() * (dy + 1);
1201
KoPoint KivioCanvas::snapToGuides(KoPoint point, bool &snappedX, bool &snappedY)
1207
if (m_pView->isSnapGuides())
1209
float four = m_pView->zoomHandler()->unzoomItY(4);
1210
KivioGuideLines *pGuides = activePage()->guideLines();
1211
KivioGuideLineData *pData = pGuides->findHorizontal( point.y(), four );
1216
p.ry() = pData->position();
1219
pData = pGuides->findVertical( point.x(), four );
1224
p.rx() = pData->position();
1231
double KivioCanvas::snapToGridX(double z)
1234
return snapToGrid(p).x();
1237
double KivioCanvas::snapToGridY(double z)
1240
return snapToGrid(p).y();
1304
1243
void KivioCanvas::updateGuides()
1326
1267
bool KivioCanvas::eventFilter(QObject* o, QEvent* e)
1328
if ((o == m_pVRuler || o == m_pHRuler) && (e->type() == QEvent::MouseMove || e->type() == QEvent::MouseButtonRelease) && m_pView->isShowGuides()) {
1269
if ((o == view()->vertRuler() || o == view()->horzRuler()) && (e->type() == QEvent::MouseMove || e->type() ==
1270
QEvent::MouseButtonRelease) && m_pView->isShowGuides())
1330
1272
QMouseEvent* me = (QMouseEvent*)e;
1331
1273
QPoint p = mapFromGlobal(me->globalPos());
1332
1274
KivioGuideLines* gl = activePage()->guideLines();
1334
1276
if (e->type() == QEvent::MouseMove) {
1335
1277
bool f = geometry().contains(p);
1336
if (!pressGuideline && f) {
1278
if (!pressGuideline && f && (me->state() == QMouseEvent::LeftButton)) {
1340
1282
gl->unselectAll();
1341
1283
KivioGuideLineData* gd;
1342
TKPoint tp = mapFromScreen(p);
1344
gd = gl->add(tp.x,Qt::Vertical);
1346
gd = gl->add(tp.y,Qt::Horizontal);
1284
KoPoint tp = mapFromScreen(p);
1286
if (o == view()->vertRuler()) {
1287
gd = gl->add(tp.x(),Qt::Vertical);
1289
gd = gl->add(tp.y(),Qt::Horizontal);
1348
1292
pressGuideline = gd;
1349
1293
gl->select(gd);
1355
1299
w->setCursor(sizeAllCursor);
1360
if (pressGuideline && !f) {
1364
gl->remove(pressGuideline);
1368
QWidget* w = (QWidget*)o;
1369
w->setCursor(*storedCursor);
1370
delete storedCursor;
1376
if (pressGuideline && f) {
1377
QMouseEvent* m = new QMouseEvent(QEvent::MouseMove, p, me->globalPos(), me->button(), me->state());
1380
delegateThisEvent = true;
1302
} else if (pressGuideline && !f) {
1306
gl->remove(pressGuideline);
1310
QWidget* w = (QWidget*)o;
1311
w->setCursor(*storedCursor);
1312
delete storedCursor;
1317
} else if (pressGuideline && f) {
1318
QMouseEvent* m = new QMouseEvent(QEvent::MouseMove, p, me->globalPos(), me->button(), me->state());
1321
delegateThisEvent = true;
1385
1325
if (e->type() == QEvent::MouseButtonRelease && pressGuideline) {
1388
1327
gl->unselect(pressGuideline);
1428
1373
setUpdatesEnabled(false);
1430
TKPageLayout pl = activePage()->paperLayout();
1431
float w = pl.ptWidth()/2.0;
1432
float h = pl.ptHeight()/2.0;
1439
scrollDx((int)(w*m_pZoom));
1440
scrollDy((int)(h*m_pZoom));
1375
KivioRect va = visibleArea();
1377
float x = QMAX(0, p.x() - (va.w() / 2));
1378
float y = QMAX(0, p.y() - (va.h() / 2));
1380
m_pVertScrollBar->setValue(m_pView->zoomHandler()->zoomItY(y));
1381
m_pHorzScrollBar->setValue(m_pView->zoomHandler()->zoomItX(x));
1442
1383
setUpdatesEnabled(true);
1445
1386
KivioRect KivioCanvas::visibleArea()
1447
TKPoint p0 = mapFromScreen(QPoint(0,0));
1448
TKPoint p1 = mapFromScreen(QPoint(width()-1,height()-1));
1388
KoPoint p0 = mapFromScreen(QPoint(0,0));
1389
KoPoint p1 = mapFromScreen(QPoint(width()-1,height()-1));
1450
return KivioRect(p0.x, p0.y, p1.x - p0.x, p1.y - p0.y);
1391
return KivioRect(p0.x(), p0.y(), p1.x() - p0.x(), p1.y() - p0.y());
1453
1394
void KivioCanvas::setVisibleArea(KivioRect r, int margin)
1455
1396
setUpdatesEnabled(false);
1457
int cw = width() - 2*margin;
1458
int ch = height() - 2*margin;
1460
float zw = cw/r.w();
1461
float zh = ch/r.h();
1462
float z = QMIN(zw,zh);
1398
zoom.setZoomAndResolution(100, QPaintDevice::x11AppDpiX(),
1399
QPaintDevice::x11AppDpiY());
1401
float cw = width() - 2 * margin;
1402
float ch = height() - 2 * margin;
1404
float zw = cw / (float)zoom.zoomItX(r.w());
1405
float zh = ch / (float)zoom.zoomItY(r.h());
1406
float z = QMIN(zw, zh);
1408
setZoom(qRound(z * 100));
1466
1410
KivioPoint c = r.center();
1487
1434
void KivioCanvas::setVisibleAreaByHeight(KivioRect r, int margin)
1489
1436
setUpdatesEnabled(false);
1491
int ch = height() - 2*margin;
1438
zoom.setZoomAndResolution(100, QPaintDevice::x11AppDpiX(),
1439
QPaintDevice::x11AppDpiY());
1441
float ch = height() - 2*margin;
1442
float z = ch / (float)zoom.zoomItY(r.h());
1444
setZoom(qRound(z * 100));
1496
1446
KivioPoint c = r.center();
1498
1448
setViewCenterPoint(c);
1499
1449
setUpdatesEnabled(true);
1452
void KivioCanvas::startPasteMoving()
1455
KivioPoint p = activePage()->getRectForAllSelectedStencils().center();
1456
m_origPoint.setCoords(p.x(), p.y());
1458
// Create a new painter object
1459
beginUnclippedSpawnerPainter();
1460
drawSelectedStencilsXOR();
1462
// Build the list of old geometry
1464
m_lstOldGeometry.clear();
1465
KivioStencil* pStencil = activePage()->selectedStencils()->first();
1469
pData = new KivioRect;
1470
*pData = pStencil->rect();
1471
m_lstOldGeometry.append(pData);
1473
pStencil = activePage()->selectedStencils()->next();
1476
continuePasteMoving(lastPoint);
1477
m_pasteMoving = true;
1481
void KivioCanvas::continuePasteMoving(const QPoint &pos)
1483
KoPoint pagePoint = mapFromScreen( pos );
1485
double dx = pagePoint.x() - m_origPoint.x();
1486
double dy = pagePoint.y() - m_origPoint.y();
1493
// Undraw the old stencils
1494
drawSelectedStencilsXOR();
1496
// Translate to the new position
1498
KivioRect selectedRect = activePage()->getRectForAllSelectedStencils();
1500
newX = selectedRect.x() + dx;
1501
newY = selectedRect.y() + dy;
1503
// First attempt a snap-to-grid
1504
p.setCoords(newX, newY);
1510
// Now the guides override the grid so we attempt to snap to them
1511
p.setCoords(selectedRect.x() + dx + selectedRect.w(), selectedRect.y() + dy + selectedRect.h());
1512
p = snapToGuides(p, snappedX, snappedY);
1515
newX = p.x() - selectedRect.w();
1519
newY = p.y() - selectedRect.h();
1522
p.setCoords(selectedRect.x() + dx, selectedRect.y() + dy);
1523
p = snapToGuides(p, snappedX, snappedY);
1533
dx = newX - selectedRect.x();
1534
dy = newY - selectedRect.y();
1536
// Translate to the new position
1537
KivioStencil *pStencil = activePage()->selectedStencils()->first();
1538
KivioRect* pData = m_lstOldGeometry.first();
1539
// bool move = true;
1541
while( pStencil && pData )
1543
newX = pData->x() + dx;
1544
newY = pData->y() + dy;
1546
if( pStencil->protection()->at( kpX ) == false ) {
1547
pStencil->setX(newX);
1549
if( pStencil->protection()->at( kpY ) == false ) {
1550
pStencil->setY(newY);
1553
pData = m_lstOldGeometry.next();
1554
pStencil = activePage()->selectedStencils()->next();
1557
// Draw the stencils
1558
drawSelectedStencilsXOR();
1559
m_pView->updateToolBars();
1562
void KivioCanvas::endPasteMoving()
1564
KivioStencil *pStencil = activePage()->selectedStencils()->first();
1565
KivioRect *pData = m_lstOldGeometry.first();
1567
while( pStencil && pData )
1569
if(pStencil->type() == kstConnector) {
1570
pStencil->searchForConnections(m_pView->activePage(), m_pView->zoomHandler()->unzoomItY(4));
1573
pData = m_lstOldGeometry.next();
1574
pStencil = activePage()->selectedStencils()->next();
1577
drawSelectedStencilsXOR();
1579
endUnclippedSpawnerPainter();
1581
// Clear the list of old geometry
1582
m_lstOldGeometry.clear();
1583
m_pasteMoving = false;
1501
1586
#include "kivio_canvas.moc"