~ubuntu-branches/ubuntu/edgy/koffice/edgy-updates

« back to all changes in this revision

Viewing changes to kivio/kiviopart/kivio_canvas.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040509113300-xi5t1z4yxe7n03x7
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "kivio_view.h"
24
24
#include "kivio_doc.h"
25
25
#include "kivio_tabbar.h"
26
 
#include "kivio_ruler.h"
27
26
 
28
27
#include "kivio_icon_view.h"
29
28
#include "kivio_stencil.h"
32
31
#include "kivio_stackbar.h"
33
32
#include "kivio_screen_painter.h"
34
33
#include "kivio_grid_data.h"
35
 
#include "kivio_guidelines.h"
36
34
 
37
35
#include "tool_controller.h"
38
36
#include "tool.h"
41
39
#include <klocale.h>
42
40
#include <kmessagebox.h>
43
41
#include <kcursor.h>
 
42
#include <koGlobal.h>
 
43
#include <kozoomhandler.h>
 
44
#include <koSize.h>
 
45
#include <koRuler.h>
44
46
 
45
47
#include <assert.h>
46
48
#include <stdio.h>
50
52
#include <qtimer.h>
51
53
#include <qsize.h>
52
54
 
 
55
using namespace Kivio;
53
56
 
54
 
KivioCanvas::KivioCanvas( QWidget *par, KivioView* view, KivioDoc* doc, ToolController* tc, QScrollBar* vs, QScrollBar* hs, KivioRuler* vr, KivioRuler* hr )
 
57
KivioCanvas::KivioCanvas( QWidget *par, KivioView* view, KivioDoc* doc, ToolController* tc, QScrollBar* vs, QScrollBar* hs)
55
58
: QWidget(par, "KivioCanvas", WResizeNoErase | WRepaintNoErase),
56
59
  m_pView(view),
57
60
  m_pDoc(doc),
58
61
  m_pToolsController(tc),
59
62
  m_pVertScrollBar(vs),
60
 
  m_pHorzScrollBar(hs),
61
 
  m_pVRuler(vr),
62
 
  m_pHRuler(hr)
 
63
  m_pHorzScrollBar(hs)
63
64
{
64
65
  setBackgroundMode(NoBackground);
65
66
  setAcceptDrops(true);
71
72
  storedCursor = 0;
72
73
  pressGuideline = 0;
73
74
 
74
 
  m_pVRuler->installEventFilter(this);
75
 
  m_pHRuler->installEventFilter(this);
76
 
 
77
75
  m_pVertScrollBar->setLineStep(1);
78
76
  m_pHorzScrollBar->setLineStep(1);
79
77
 
84
82
  connect(m_pVertScrollBar, SIGNAL(valueChanged(int)), SLOT(scrollV(int)));
85
83
  connect( m_pHorzScrollBar, SIGNAL(valueChanged(int)), SLOT(scrollH(int)));
86
84
 
87
 
  m_pZoom = 0.5;
88
 
 
89
85
  m_iXOffset = 0;
90
86
  m_iYOffset = 0;
91
87
 
92
88
  m_pScrollX = 0;
93
89
  m_pScrollY = 0;
94
90
 
 
91
  m_pasteMoving = false;
 
92
 
95
93
  m_buffer = new QPixmap();
96
94
 
97
95
  m_pDragStencil = 0L;
108
106
KivioCanvas::~KivioCanvas()
109
107
{
110
108
  delete m_buffer;
 
109
  delete m_borderTimer;
 
110
  delete m_guideLinesTimer;
 
111
  delete storedCursor;
 
112
  delete unclippedPainter;
111
113
}
112
114
 
113
115
KivioPage* KivioCanvas::findPage( const QString& _name )
137
139
  bitBlt(m_buffer, dx, 0, m_buffer);
138
140
  scroll(dx, 0);
139
141
 
140
 
  updateRulers(true,false);
141
142
  emit visibleAreaChanged();
142
143
}
143
144
 
153
154
  bitBlt(m_buffer, 0, dy, m_buffer);
154
155
  scroll(0, dy);
155
156
 
156
 
  updateRulers(false,true);
157
157
  emit visibleAreaChanged();
158
158
}
159
159
 
175
175
  m_pVertScrollBar->setValue(value);
176
176
}
177
177
 
178
 
void KivioCanvas::updateRulers( bool horiz, bool vert )
179
 
{
180
 
  if (!isUpdatesEnabled())
181
 
    return;
182
 
 
183
 
  if (horiz) {
184
 
    int pw = (int)actualPaperSizePt().w;
185
 
    int x0 = (int)((width()-pw)/2) - xOffset();
186
 
    m_pHRuler->setZoom(m_pZoom);
187
 
    m_pHRuler->updateVisibleArea(-x0,0);
188
 
  }
189
 
 
190
 
  if (vert) {
191
 
    int ph = (int)actualPaperSizePt().h;
192
 
    int y0 = (int)((height()-ph)/2) - yOffset();
193
 
    m_pVRuler->setZoom(m_pZoom);
194
 
    m_pVRuler->updateVisibleArea(0,-y0);
195
 
  }
196
 
}
197
 
 
198
178
void KivioCanvas::resizeEvent( QResizeEvent* )
199
179
{
200
180
  KivioGuideLines::resize(size(),m_pDoc);
201
181
  m_buffer->resize(size());
202
 
  updateRulers(true,true);
203
182
  updateScrollBars();
204
183
 
205
184
  emit visibleAreaChanged();
216
195
    zoomOut(p);
217
196
  }
218
197
*/
219
 
  QPoint p = ev->pos();
220
 
  if( (ev->delta()<0))
 
198
  //QPoint p = ev->pos();
 
199
  if( (ev->delta()>0))
221
200
  {
222
 
     m_pVertScrollBar->subtractPage();
 
201
     m_pVertScrollBar->setValue(m_pVertScrollBar->value() - 30);
223
202
  }
224
203
  else
225
204
  {
226
 
     m_pVertScrollBar->addPage();
 
205
     m_pVertScrollBar->setValue(m_pVertScrollBar->value() + 30);
227
206
  }
228
207
}
229
208
 
236
215
    --i;
237
216
    if (i == 0) {
238
217
      update();
239
 
      updateRulers(true,true);
240
218
      updateScrollBars();
241
219
 
242
220
      blockSignals(false);
243
221
 
244
 
      emit zoomChanges(m_pZoom);
 
222
      emit zoomChanges();
245
223
      emit visibleAreaChanged();
246
224
    }
247
225
  } else {
250
228
  }
251
229
}
252
230
 
253
 
void KivioCanvas::zoomIn(QPoint p)
 
231
void KivioCanvas::zoomIn(const QPoint &p)
254
232
{
255
233
  setUpdatesEnabled(false);
256
 
  TKPoint p0 = mapFromScreen(p);
257
 
  setZoom(m_pZoom*(100.0f+25.0f)/100.0f);
 
234
  KoPoint p0 = mapFromScreen(p);
 
235
  setZoom(m_pView->zoomHandler()->zoom() + 25);
258
236
  QPoint p1 = mapToScreen(p0);
259
237
  scrollDx(-p1.x()+p.x());
260
238
  scrollDy(-p1.y()+p.y());
261
239
  setUpdatesEnabled(true);
262
240
}
263
241
 
264
 
void KivioCanvas::zoomOut(QPoint p)
 
242
void KivioCanvas::zoomOut(const QPoint &p)
265
243
{
266
244
  setUpdatesEnabled(false);
267
 
  TKPoint p0 = mapFromScreen(p);
268
 
  setZoom(m_pZoom/(100.0f+25.0f)*100.0f);
269
 
  QPoint p1 = mapToScreen(p0);
270
 
  scrollDx(-p1.x()+p.x());
271
 
  scrollDy(-p1.y()+p.y());
 
245
  KoPoint p0 = mapFromScreen(p);
 
246
  int newZoom = m_pView->zoomHandler()->zoom() - 25;
 
247
 
 
248
  if(newZoom > 0) {
 
249
    setZoom(newZoom);
 
250
    QPoint p1 = mapToScreen(p0);
 
251
    scrollDx(-p1.x()+p.x());
 
252
    scrollDy(-p1.y()+p.y());
 
253
  }
272
254
  setUpdatesEnabled(true);
273
255
}
274
256
 
293
275
    int w = x + rect.width();
294
276
    int h = y + rect.height();
295
277
 
296
 
    TKSize dxy = actualGridFrequency();
297
 
    dxy.convertToPt();
 
278
    KoSize dxy = m_pDoc->grid().freq;
298
279
 
299
 
    TKPoint p;
300
 
    p.set(0,0,UnitPoint);
 
280
    KoPoint p(0, 0);
301
281
 
302
282
    painter.setPen(m_pDoc->grid().color);
303
283
 
304
284
    QPoint p0 = mapToScreen(p);
305
 
    while (p0.x()<x) {
306
 
      p.x += dxy.w;
307
 
      p0 = mapToScreen(p);
308
 
    }
309
 
    while (p0.x()<=w) {
310
 
      painter.drawLine(p0.x(),y,p0.x(),h);
311
 
 
312
 
      p.x += dxy.w;
313
 
      p0 = mapToScreen(p);
314
 
    }
315
 
 
316
 
    p.set(0,0,UnitPoint);
317
 
    p0 = mapToScreen(p);
318
 
    while (p0.x()>w) {
319
 
      p.x -= dxy.w;
320
 
      p0 = mapToScreen(p);
321
 
    }
322
 
    while (p0.x()>=x) {
323
 
      painter.drawLine(p0.x(),y,p0.x(),h);
324
 
 
325
 
      p.x -= dxy.w;
326
 
      p0 = mapToScreen(p);
327
 
    }
328
 
 
329
 
    p.set(0,0,UnitPoint);
330
 
    p0 = mapToScreen(p);
331
 
    while (p0.y()<y) {
332
 
      p.y += dxy.h;
333
 
      p0 = mapToScreen(p);
334
 
    }
335
 
    while (p0.y()<=h) {
336
 
      painter.drawLine(x,p0.y(),w,p0.y());
337
 
 
338
 
      p.y += dxy.h;
339
 
      p0 = mapToScreen(p);
340
 
    }
341
 
 
342
 
    p.set(0,0,UnitPoint);
343
 
    p0 = mapToScreen(p);
344
 
    while (p0.y()>h) {
345
 
      p.y -= dxy.h;
346
 
      p0 = mapToScreen(p);
347
 
    }
348
 
    while (p0.y()>=y) {
349
 
      painter.drawLine(x,p0.y(),w,p0.y());
350
 
 
351
 
      p.y -= dxy.h;
 
285
 
 
286
    while (p0.x() < x) {
 
287
      p.rx() += dxy.width();
 
288
      p0 = mapToScreen(p);
 
289
    }
 
290
 
 
291
    while (p0.x() <= w) {
 
292
      painter.drawLine(p0.x(),y,p0.x(),h);
 
293
 
 
294
      p.rx() += dxy.width();
 
295
      p0 = mapToScreen(p);
 
296
    }
 
297
 
 
298
    p.setCoords(0, 0);
 
299
    p0 = mapToScreen(p);
 
300
 
 
301
    while (p0.x() > w) {
 
302
      p.rx() -= dxy.width();
 
303
      p0 = mapToScreen(p);
 
304
    }
 
305
 
 
306
    while (p0.x() >= x) {
 
307
      painter.drawLine(p0.x(),y,p0.x(),h);
 
308
 
 
309
      p.rx() -= dxy.width();
 
310
      p0 = mapToScreen(p);
 
311
    }
 
312
 
 
313
    p.setCoords(0, 0);
 
314
    p0 = mapToScreen(p);
 
315
 
 
316
    while (p0.y() < y) {
 
317
      p.ry() += dxy.height();
 
318
      p0 = mapToScreen(p);
 
319
    }
 
320
 
 
321
    while (p0.y() <= h) {
 
322
      painter.drawLine(x,p0.y(),w,p0.y());
 
323
 
 
324
      p.ry() += dxy.height();
 
325
      p0 = mapToScreen(p);
 
326
    }
 
327
 
 
328
    p.setCoords(0, 0);
 
329
    p0 = mapToScreen(p);
 
330
 
 
331
    while (p0.y() > h) {
 
332
      p.ry() -= dxy.height();
 
333
      p0 = mapToScreen(p);
 
334
    }
 
335
 
 
336
    while (p0.y() >= y) {
 
337
      painter.drawLine(x,p0.y(),w,p0.y());
 
338
 
 
339
      p.ry() -= dxy.height();
352
340
      p0 = mapToScreen(p);
353
341
    }
354
342
  }
355
343
 
356
344
  painter.translate(-m_iXOffset,-m_iYOffset);
357
345
 
358
 
  QPoint p0 = actualPaperOrigin();
359
 
  int pw = (int)actualPaperSizePt().w;
360
 
  int ph = (int)actualPaperSizePt().h;
361
 
  int px0 = p0.x();
362
 
  int py0 = p0.y();
 
346
  KoPageLayout pl = page->paperLayout();
 
347
  int pw = m_pView->zoomHandler()->zoomItX(pl.ptWidth);
 
348
  int ph = m_pView->zoomHandler()->zoomItY(pl.ptHeight);
363
349
 
364
350
  if (m_pView->isShowPageMargins()) {
365
 
    float zf = m_pZoom;
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);
371
355
 
372
356
    painter.save();
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();
376
360
  }
377
361
 
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);
383
367
  }
384
368
 
385
369
  // Draw content
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);
391
375
  kpainter.stop();
392
376
 
393
377
  paintGuides(false);
396
380
  bitBlt(this,rect.left(),rect.top(),m_buffer,rect.left(),rect.top(),rect.width(),rect.height());
397
381
}
398
382
 
399
 
void KivioCanvas::centerPage()
400
 
{
401
 
  m_pHorzScrollBar->setValue(0);
402
 
  m_pVertScrollBar->setValue(0);
403
 
}
404
 
 
405
383
void KivioCanvas::updateScrollBars()
406
384
{
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();
409
388
 
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() )
413
392
  {
414
393
    m_pHorzScrollBar->setValue(0);
415
394
  }
416
395
 
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() )
420
399
  {
427
406
 
428
407
QSize KivioCanvas::actualSize()
429
408
{
430
 
  return QSize(2*m_pScrollX + width(), 2*m_pScrollY + height());
431
 
}
432
 
 
433
 
QPoint KivioCanvas::actualPaperOrigin()
434
 
{
435
 
  int pw = (int)actualPaperSizePt().w;
436
 
  int ph = (int)actualPaperSizePt().h;
437
 
  int px0 = (width()-pw)/2;
438
 
  int py0 = (height()-ph)/2;
439
 
 
440
 
  return QPoint(px0,py0);
441
 
}
442
 
 
443
 
float KivioCanvas::zoom()
444
 
{
445
 
  return m_pZoom;
446
 
}
447
 
 
448
 
 
449
 
void KivioCanvas::setZoom(float zoom)
450
 
{
451
 
  m_pZoom = QMAX(0.05f,QMIN(100.0f,zoom));
 
409
  return QSize(m_pScrollX, m_pScrollY);
 
410
}
 
411
 
 
412
void KivioCanvas::setZoom(int zoom)
 
413
{
 
414
  if(zoom < 10 || zoom > 2000) {
 
415
    return;
 
416
  }
 
417
  
 
418
  m_pView->zoomHandler()->setZoomAndResolution(zoom, QPaintDevice::x11AppDpiX(),
 
419
    QPaintDevice::x11AppDpiY());
452
420
 
453
421
  updateScrollBars();
454
 
  updateRulers(true,true);
455
 
 
456
422
  erase();
457
423
  repaint();
458
424
 
459
 
  emit zoomChanges(m_pZoom);
 
425
  emit zoomChanges();
460
426
  emit visibleAreaChanged();
461
427
}
462
428
 
463
 
TKSize KivioCanvas::actualGridFrequency()
 
429
KoSize KivioCanvas::actualGridFrequency()
464
430
{
465
 
  TKSize actual;
466
 
  int f = 0;
467
 
  do {
468
 
    f++;
469
 
    actual = m_pDoc->grid().freq;
470
 
    actual.w *= f;
471
 
    actual.h *= f;
472
 
    actual.convertToPt(m_pZoom);
473
 
  } while ( actual.w < 10 || actual.h < 10 );
474
 
 
475
 
  actual = m_pDoc->grid().freq;
476
 
  actual.w *= f;
477
 
  actual.h *= f;
 
431
  KoSize actual;
 
432
  actual.setWidth(m_pView->zoomHandler()->zoomItX(m_pDoc->grid().freq.width()));
 
433
  actual.setHeight(m_pView->zoomHandler()->zoomItY(m_pDoc->grid().freq.height()));
478
434
 
479
435
  return actual;
480
436
}
481
437
 
482
 
TKSize KivioCanvas::actualPaperSizePt()
483
 
{
484
 
  TKSize ps;
485
 
  TKPageLayout pl = activePage()->paperLayout();
486
 
 
487
 
  float w = pl.ptWidth()*m_pZoom;
488
 
  float h = pl.ptHeight()*m_pZoom;
489
 
  ps.set(w,h,UnitPoint);
490
 
 
491
 
  return ps;
492
 
}
493
 
 
494
 
void KivioCanvas::toggleShowRulers( bool b )
495
 
{
496
 
  if (b) {
497
 
    m_pHRuler->show();
498
 
    m_pVRuler->show();
499
 
  } else {
500
 
    m_pHRuler->hide();
501
 
    m_pVRuler->hide();
502
 
  }
503
 
}
504
 
 
505
438
bool KivioCanvas::event( QEvent* e )
506
439
{
507
440
  bool f = QWidget::event(e);
518
451
 
519
452
void KivioCanvas::leaveEvent( QEvent* )
520
453
{
521
 
  m_pVRuler->updatePointer(-1,-1);
522
 
  m_pHRuler->updatePointer(-1,-1);
 
454
  m_pView->setMousePos(-1, -1);
523
455
}
524
456
 
525
457
void KivioCanvas::mousePressEvent(QMouseEvent* e)
526
458
{
527
459
    if(!m_pDoc->isReadWrite())
528
460
        return;
 
461
    if(m_pasteMoving) {
 
462
      endPasteMoving();
 
463
      return;
 
464
    }
 
465
 
529
466
    if(m_pView->isShowGuides())
530
467
    {
531
468
        lastPoint = e->pos();
532
 
        TKPoint p = mapFromScreen(e->pos());
 
469
        KoPoint p = mapFromScreen(e->pos());
533
470
        KivioGuideLines* gl = activePage()->guideLines();
534
471
 
535
472
        bool unselectAllGuideLines = true;
536
473
        pressGuideline = 0;
537
474
 
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));
540
477
            if (gd) {
541
478
                pressGuideline = gd;
542
479
                if ((e->button() == RightButton) || ((e->button() & ShiftButton) == ShiftButton)) {
566
503
 
567
504
void KivioCanvas::mouseReleaseEvent(QMouseEvent* e)
568
505
{
569
 
    if(!m_pDoc->isReadWrite())
570
 
        return;
 
506
  if(!m_pDoc->isReadWrite())
 
507
    return;
 
508
 
571
509
  if (pressGuideline) {
572
510
    m_guideLinesTimer->stop();
573
 
    TKPoint p = mapFromScreen(e->pos());
 
511
    KoPoint p = mapFromScreen(e->pos());
 
512
    bool insideCanvas = geometry().contains(mapFromGlobal(e->globalPos()));
574
513
    KivioGuideLines* gl = activePage()->guideLines();
575
 
    KivioGuideLineData* gd = gl->find(p.x,p.y,2.0/m_pZoom);
 
514
 
 
515
    if(!insideCanvas) {
 
516
      eraseGuides();
 
517
      gl->remove(pressGuideline);
 
518
      paintGuides();
 
519
      delegateThisEvent = false;
 
520
      pressGuideline = 0;
 
521
      return;
 
522
    }
 
523
 
 
524
    KivioGuideLineData* gd = gl->find(p.x(),p.y(),m_pView->zoomHandler()->unzoomItY(2));
576
525
    if (gd) {
577
526
      setCursor(gd->orientation()==Qt::Vertical ? sizeHorCursor:sizeVerCursor);
578
527
    } else {
585
534
 
586
535
void KivioCanvas::mouseMoveEvent(QMouseEvent* e)
587
536
{
588
 
    if(!m_pDoc->isReadWrite())
589
 
        return;
 
537
  if(!m_pDoc->isReadWrite())
 
538
    return;
 
539
 
 
540
  if(m_pasteMoving) {
 
541
    continuePasteMoving(e->pos());
 
542
  } else {
590
543
    if(m_pView->isShowGuides())
591
544
    {
592
 
        m_pVRuler->updatePointer(e->pos().x(),e->pos().y());
593
 
        m_pHRuler->updatePointer(e->pos().x(),e->pos().y());
594
 
 
595
 
        TKPoint p = mapFromScreen(e->pos());
596
 
        KivioGuideLines* gl = activePage()->guideLines();
597
 
 
598
 
        if ((e->state() & LeftButton == LeftButton) && gl->hasSelected()) {
599
 
            if (m_guideLinesTimer->isActive()) {
600
 
                m_guideLinesTimer->stop();
601
 
                guideLinesTimerTimeout();
602
 
            }
 
545
      m_pView->setMousePos(e->pos().x(),e->pos().y());
 
546
 
 
547
      KivioGuideLines* gl = activePage()->guideLines();
 
548
 
 
549
      if ((e->state() & LeftButton == LeftButton) && gl->hasSelected()) {
 
550
        if (m_guideLinesTimer->isActive()) {
 
551
          m_guideLinesTimer->stop();
 
552
          guideLinesTimerTimeout();
 
553
        }
 
554
        delegateThisEvent = false;
 
555
        eraseGuides();
 
556
        QPoint p = e->pos();
 
557
        p -= lastPoint;
 
558
        if (p.x() != 0)
 
559
          gl->moveSelectedByX(m_pView->zoomHandler()->unzoomItX(p.x()));
 
560
        if (p.y() != 0)
 
561
          gl->moveSelectedByY(m_pView->zoomHandler()->unzoomItY(p.y()));
 
562
        m_pDoc->setModified( true );
 
563
        paintGuides();
 
564
      } else {
 
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));
 
568
          if (gd) {
603
569
            delegateThisEvent = false;
604
 
            eraseGuides();
605
 
            QPoint p = e->pos();
606
 
            p -= lastPoint;
607
 
            if (p.x() != 0)
608
 
                gl->moveSelectedByX(p.x()/m_pZoom);
609
 
            if (p.y() != 0)
610
 
                gl->moveSelectedByY(p.y()/m_pZoom);
611
 
            paintGuides();
612
 
        } else {
613
 
            if ((e->state() & ~ShiftButton) == NoButton) {
614
 
                KivioGuideLineData* gd = gl->find(p.x,p.y,2.0/m_pZoom);
615
 
                if (gd) {
616
 
                    delegateThisEvent = false;
617
 
                    if (!storedCursor)
618
 
                        storedCursor = new QCursor(cursor());
619
 
                    setCursor(gd->orientation()==Qt::Vertical ? sizeHorCursor:sizeVerCursor);
620
 
                } else {
621
 
                    updateGuidesCursor();
622
 
                }
623
 
            }
 
570
            if (!storedCursor)
 
571
              storedCursor = new QCursor(cursor());
 
572
            setCursor(gd->orientation()==Qt::Vertical ? sizeHorCursor:sizeVerCursor);
 
573
          } else {
 
574
            updateGuidesCursor();
 
575
          }
624
576
        }
 
577
      }
625
578
    }
626
 
//  float xf = p.xToUnit(UnitMillimeter);
627
 
//  float yf = p.yToUnit(UnitMillimeter);
 
579
  }
628
580
 
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();
631
582
}
632
583
 
633
 
QPoint KivioCanvas::mapToScreen( TKPoint pos )
 
584
QPoint KivioCanvas::mapToScreen( KoPoint pos )
634
585
{
635
586
  QPoint p;
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());
639
589
 
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 );
642
592
 
643
593
  return p;
644
594
}
645
595
 
646
 
TKPoint KivioCanvas::mapFromScreen( QPoint pos )
 
596
KoPoint KivioCanvas::mapFromScreen( const QPoint & pos )
647
597
{
648
 
  QPoint p0 = actualPaperOrigin();
649
 
  int x = pos.x() + m_iXOffset - p0.x();
650
 
  int y = pos.y() + m_iYOffset - p0.y();
651
 
 
652
 
  float xf = x/m_pZoom;
653
 
  float yf = y/m_pZoom;
654
 
 
655
 
  TKPoint p;
656
 
  p.set(xf,yf,UnitPoint);
657
 
 
 
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);
 
602
 
 
603
  KoPoint p(xf, yf);
658
604
  return p;
659
605
}
660
606
 
712
658
 */
713
659
void KivioCanvas::startSpawnerDragDraw( const QPoint &p )
714
660
{
715
 
    currRect = QRect( 0, 0, -1, -1 );
716
 
 
717
 
    KivioStencilSpawner *pSpawner = KivioIconView::curDragSpawner();
718
 
    if( !pSpawner )
719
 
        return;
720
 
 
721
 
    // If we for some reason didn't delete an old drag stencil,
722
 
    // do so now.
723
 
    if( m_pDragStencil )
724
 
    {
725
 
       kdDebug() << "KivioCanvas::startSpawnerDragDraw() - m_pDragStencil still exists.  BUG!" << endl;
726
 
        delete m_pDragStencil;
727
 
        m_pDragStencil = 0L;
728
 
    }
729
 
 
730
 
    // Map the point from screenspace to page space
731
 
    TKPoint qp = mapFromScreen( p );
732
 
    qp = snapToGrid(qp);
733
 
 
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() );
738
 
 
739
 
    // Invalidate the rectangle
740
 
    oldRectValid = true;
741
 
 
742
 
    // Create a new painter object
743
 
    beginUnclippedSpawnerPainter();
744
 
 
745
 
    // Calculate the zoom value
746
 
    float zf = m_pZoom;
747
 
 
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 );
752
 
 
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;
757
 
 
758
 
    // Draw the outline of the stencil
759
 
    m_pDragStencil->paintOutline( &m_dragStencilData );
760
 
 
761
 
    unclippedSpawnerPainter->painter()->restore();
762
 
 
 
661
  currRect = QRect( 0, 0, -1, -1 );
 
662
 
 
663
  KivioStencilSpawner *pSpawner = KivioIconView::curDragSpawner();
 
664
  if( !pSpawner )
 
665
    return;
 
666
 
 
667
  // If we for some reason didn't delete an old drag stencil,
 
668
  // do so now.
 
669
  if( m_pDragStencil )
 
670
  {
 
671
    kdDebug(43000) << "KivioCanvas::startSpawnerDragDraw() - m_pDragStencil still exists.  BUG!" << endl;
 
672
    delete m_pDragStencil;
 
673
    m_pDragStencil = 0L;
 
674
  }
 
675
 
 
676
  // Map the point from screenspace to page space
 
677
  KoPoint qp = mapFromScreen( p );
 
678
  qp = snapToGrid(qp);
 
679
 
 
680
  // Allocate a new stencil for dragging around
 
681
  m_pDragStencil = pSpawner->newStencil();
 
682
  m_pDragStencil->setPosition( qp.x(), qp.y() );
 
683
 
 
684
  // Invalidate the rectangle
 
685
  oldRectValid = true;
 
686
 
 
687
  // Create a new painter object
 
688
  beginUnclippedSpawnerPainter();
 
689
 
 
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 );
 
693
 
 
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();
 
698
 
 
699
  // Draw the outline of the stencil
 
700
  m_pDragStencil->paintOutline( &m_dragStencilData );
 
701
 
 
702
  unclippedSpawnerPainter->painter()->restore();
763
703
}
764
704
 
765
705
/**
767
707
 */
768
708
void KivioCanvas::continueSpawnerDragDraw( const QPoint &p )
769
709
{
770
 
   bool snappedX, snappedY;
771
 
 
772
 
    QPoint pos = p;
773
 
    QPoint p2 = pos;
774
 
 
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 );
779
 
 
780
 
    // Undraw the old outline
781
 
    if( oldRectValid )
782
 
    {
783
 
        m_pDragStencil->paintOutline( &m_dragStencilData );
784
 
    }
785
 
 
786
 
    // Map the new point from screenspace to page space
787
 
    TKPoint orig = mapFromScreen(p2);
788
 
    TKPoint qp = snapToGrid( orig );
789
 
 
790
 
    // First snap to screen
791
 
    qp = snapToGrid(qp);
792
 
    m_pDragStencil->setPosition( qp.x, qp.y );
793
 
 
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());
799
 
    }
800
 
    if( snappedY==true ) {
801
 
       m_pDragStencil->setY(qp.y - m_pDragStencil->h());
802
 
    }
803
 
 
804
 
    qp.set( orig.x, orig.y, UnitPoint );
805
 
    qp = snapToGuides(qp, snappedX, snappedY);
806
 
    if( snappedX==true ) {
807
 
       m_pDragStencil->setX(qp.x);
808
 
    }
809
 
    if( snappedY==true ) {
810
 
       m_pDragStencil->setY(qp.y);
811
 
    }
812
 
 
813
 
    // Redraw the new outline
814
 
    oldRectValid = true;
 
710
  bool snappedX, snappedY;
 
711
 
 
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 );
 
715
 
 
716
  // Undraw the old outline
 
717
  if( oldRectValid )
 
718
  {
815
719
    m_pDragStencil->paintOutline( &m_dragStencilData );
816
 
    unclippedSpawnerPainter->painter()->restore();
 
720
  }
 
721
 
 
722
  // Map the new point from screenspace to page space
 
723
  KoPoint orig = mapFromScreen(p);
 
724
  KoPoint qp = snapToGrid( orig );
 
725
 
 
726
  // First snap to screen
 
727
  qp = snapToGrid(qp);
 
728
  m_pDragStencil->setPosition( qp.x(), qp.y() );
 
729
 
 
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);
 
733
 
 
734
  if(snappedX) {
 
735
    m_pDragStencil->setX(qp.x() - m_pDragStencil->w());
 
736
  }
 
737
 
 
738
  if(snappedY) {
 
739
    m_pDragStencil->setY(qp.y() - m_pDragStencil->h());
 
740
  }
 
741
 
 
742
  qp.setCoords(orig.x(), orig.y());
 
743
  qp = snapToGuides(qp, snappedX, snappedY);
 
744
 
 
745
  if(snappedX) {
 
746
    m_pDragStencil->setX(qp.x());
 
747
  }
 
748
 
 
749
  if(snappedY) {
 
750
    m_pDragStencil->setY(qp.y());
 
751
  }
 
752
 
 
753
  // Redraw the new outline
 
754
  oldRectValid = true;
 
755
  m_pDragStencil->paintOutline( &m_dragStencilData );
 
756
  unclippedSpawnerPainter->painter()->restore();
817
757
 
818
758
}
819
759
 
823
763
 */
824
764
void KivioCanvas::endSpawnerDragDraw()
825
765
{
826
 
    // Avoid the noid
827
 
    if ( !unclippedSpawnerPainter )
828
 
        return;
829
 
 
830
 
    // If we have a valid old drawing spot, undraw it
831
 
    if ( oldRectValid )
832
 
    {
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();
838
 
    }
839
 
 
840
 
    // Smack the painter around a bit
841
 
    endUnclippedSpawnerPainter();
842
 
 
843
 
    // If we have a stencil we were dragging around, delete it.
844
 
    if( m_pDragStencil )
845
 
    {
846
 
        delete m_pDragStencil;
847
 
        m_pDragStencil = 0L;
848
 
    }
849
 
 
 
766
  // Avoid the noid
 
767
  if ( !unclippedSpawnerPainter )
 
768
    return;
 
769
 
 
770
  // If we have a valid old drawing spot, undraw it
 
771
  if ( oldRectValid )
 
772
  {
 
773
    unclippedSpawnerPainter->painter()->save();
 
774
    unclippedSpawnerPainter->painter()->translate( -m_iXOffset, -m_iYOffset );
 
775
    m_pDragStencil->paintOutline( &m_dragStencilData );
 
776
    unclippedSpawnerPainter->painter()->restore();
 
777
  }
 
778
 
 
779
  // Smack the painter around a bit
 
780
  endUnclippedSpawnerPainter();
 
781
 
 
782
  // If we have a stencil we were dragging around, delete it.
 
783
  if( m_pDragStencil )
 
784
  {
 
785
    delete m_pDragStencil;
 
786
    m_pDragStencil = 0L;
 
787
  }
 
788
 
 
789
  setFocus();
850
790
}
851
791
 
852
792
 
1040
980
    KivioPage *pPage = activePage();
1041
981
    if( !pPage )
1042
982
    {
1043
 
       kdDebug() << "KivioCanvas::dropEvent() - No active page for stencil to drop on" << endl;
 
983
       kdDebug(43000) << "KivioCanvas::dropEvent() - No active page for stencil to drop on" << endl;
1044
984
        return;
1045
985
    }
1046
986
 
1049
989
 
1050
990
    // Set the current stencil settings
1051
991
    QPoint pos = e->pos();
1052
 
    TKPoint pagePoint = snapToGrid(mapFromScreen( pos ));
1053
 
    pNewStencil->setX( pagePoint.x );
1054
 
    pNewStencil->setY( pagePoint.y );
 
992
    KoPoint pagePoint = snapToGrid(mapFromScreen( pos ));
 
993
    pNewStencil->setX( pagePoint.x() );
 
994
    pNewStencil->setY( pagePoint.y() );
1055
995
    pNewStencil->setW( pSpawner->defWidth() );
1056
996
    pNewStencil->setH( pSpawner->defHeight() );
 
997
    pNewStencil->setTextFont(doc()->defaultFont());
1057
998
 
1058
999
    // Only set these properties if we held ctrl down
1059
1000
    // FIXME: Make this happen!
1100
1041
    if ( !unclippedSpawnerPainter )
1101
1042
        return;
1102
1043
 
1103
 
    float zf = m_pZoom;
1104
 
 
1105
1044
    // Translate the painter so that 0,0 means where the page starts on the canvas
1106
 
    QPoint paperOrigin = actualPaperOrigin();
1107
1045
    unclippedSpawnerPainter->painter()->save();
1108
 
    unclippedSpawnerPainter->painter()->translate( paperOrigin.x() - m_iXOffset, paperOrigin.y() - m_iYOffset );
 
1046
    unclippedSpawnerPainter->painter()->translate( -m_iXOffset, -m_iYOffset );
1109
1047
 
1110
1048
    // Assign the painter object to the intra-stencil data object, as well
1111
1049
    // as the zoom factor
1112
1050
    m_dragStencilData.painter = unclippedSpawnerPainter;
1113
 
    m_dragStencilData.scale = zf;
 
1051
    m_dragStencilData.zoomHandler = m_pView->zoomHandler();
1114
1052
 
1115
1053
    KivioStencil *pStencil = activePage()->selectedStencils()->first();
1116
1054
    while( pStencil )
1129
1067
    if ( !unclippedSpawnerPainter )
1130
1068
        return;
1131
1069
 
1132
 
    float zf = m_pZoom;
1133
 
 
1134
1070
    // Translate the painter so that 0,0 means where the page starts on the canvas
1135
 
    QPoint paperOrigin = actualPaperOrigin();
1136
1071
    unclippedSpawnerPainter->painter()->save();
1137
 
    unclippedSpawnerPainter->painter()->translate( paperOrigin.x() - m_iXOffset, paperOrigin.y() - m_iYOffset );
 
1072
    unclippedSpawnerPainter->painter()->translate( -m_iXOffset, -m_iYOffset );
1138
1073
 
1139
1074
    // Assign the painter object to the intra-stencil data object, as well
1140
1075
    // as the zoom factor
1141
1076
    m_dragStencilData.painter = unclippedSpawnerPainter;
1142
 
    m_dragStencilData.scale = zf;
 
1077
    m_dragStencilData.zoomHandler = m_pView->zoomHandler();
1143
1078
 
1144
1079
    pStencil->paintOutline( &m_dragStencilData );
1145
1080
 
1157
1092
              gl->removeSelected();
1158
1093
              paintGuides();
1159
1094
              updateGuidesCursor();
 
1095
              m_pDoc->setModified( true );
1160
1096
            } else {
1161
1097
              activePage()->deleteSelectedStencils();
1162
1098
              m_pDoc->updateView(activePage());
1163
1099
            }
1164
1100
            break;
1165
1101
        }
1166
 
    }
1167
 
}
1168
 
 
1169
 
TKPoint KivioCanvas::snapToGridAndGuides(TKPoint point)
1170
 
{
1171
 
    TKPoint p = point;
1172
 
 
1173
 
    TKSize dist = m_pDoc->grid().snap;
1174
 
    TKSize dxy = m_pDoc->grid().freq;
1175
 
 
1176
 
    dxy.convertToPt();
1177
 
    dist.convertToPt();
1178
 
 
1179
 
    int dx = (int)(p.x/dxy.w);
1180
 
    int dy = (int)(p.y/dxy.h);
1181
 
 
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)));
1184
 
 
1185
 
    if( m_pDoc->grid().isSnap)
1186
 
    {
1187
 
       if ( distx < dist.w) {
1188
 
          if ( QABS(p.x-dxy.w*dx) < QABS(p.x-dxy.w*(dx+1)) )
1189
 
             p.x = dxy.w*dx;
1190
 
          else
1191
 
             p.x = dxy.w*(dx+1);
1192
 
       }
1193
 
 
1194
 
       if ( disty < dist.h) {
1195
 
          if ( QABS(p.y-dxy.h*dy) < QABS(p.y-dxy.h*(dy+1)) )
1196
 
             p.y = dxy.h*dy;
1197
 
          else
1198
 
             p.y = dxy.h*(dy+1);
1199
 
       }
1200
 
    }
1201
 
 
1202
 
    /*
1203
 
     * Now if the point is within 4 pixels of a gridline, snap
1204
 
     * to the grid line.
1205
 
     */
1206
 
    if (m_pView->isSnapGuides())
1207
 
    {
1208
 
       float four = 4.0f / m_pZoom;
1209
 
       KivioGuideLines *pGuides = activePage()->guideLines();
1210
 
       KivioGuideLineData *pData = pGuides->findHorizontal( point.y, four );
1211
 
       if( pData )
1212
 
       {
1213
 
          p.y = (float)pData->position();
1214
 
       }
1215
 
 
1216
 
       pData = pGuides->findVertical( point.x, four );
1217
 
       if( pData )
1218
 
       {
1219
 
          p.x = (float)pData->position();
1220
 
       }
1221
 
    }
1222
 
 
1223
 
    return p;
1224
 
}
1225
 
 
1226
 
TKPoint KivioCanvas::snapToGrid(TKPoint point)
1227
 
{
1228
 
    if (!m_pDoc->grid().isSnap)
1229
 
      return point;
1230
 
 
1231
 
    TKPoint p = point;
1232
 
 
1233
 
    TKSize dist = m_pDoc->grid().snap;
1234
 
    TKSize dxy = m_pDoc->grid().freq;
1235
 
 
1236
 
    dxy.convertToPt();
1237
 
    dist.convertToPt();
1238
 
 
1239
 
    int dx = (int)(p.x/dxy.w);
1240
 
    int dy = (int)(p.y/dxy.h);
1241
 
 
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)));
1244
 
 
1245
 
    if ( distx < dist.w) {
1246
 
      if ( QABS(p.x-dxy.w*dx) < QABS(p.x-dxy.w*(dx+1)) )
1247
 
        p.x = dxy.w*dx;
1248
 
      else
1249
 
        p.x = dxy.w*(dx+1);
1250
 
    }
1251
 
 
1252
 
    if ( disty < dist.h) {
1253
 
      if ( QABS(p.y-dxy.h*dy) < QABS(p.y-dxy.h*(dy+1)) )
1254
 
        p.y = dxy.h*dy;
1255
 
      else
1256
 
        p.y = dxy.h*(dy+1);
1257
 
    }
1258
 
 
1259
 
    return p;
1260
 
}
1261
 
 
1262
 
TKPoint KivioCanvas::snapToGuides(TKPoint point, bool &snappedX, bool &snappedY)
1263
 
{
1264
 
    snappedX = false;
1265
 
    snappedY = false;
1266
 
    TKPoint p = point;
1267
 
 
1268
 
    if (m_pView->isSnapGuides())
1269
 
    {
1270
 
       float four = 4.0f / m_pZoom;
1271
 
       KivioGuideLines *pGuides = activePage()->guideLines();
1272
 
       KivioGuideLineData *pData = pGuides->findHorizontal( point.y, four );
1273
 
       if( pData )
1274
 
       {
1275
 
          snappedY = true;
1276
 
          p.y = (float)pData->position();
1277
 
       }
1278
 
 
1279
 
       pData = pGuides->findVertical( point.x, four );
1280
 
       if( pData )
1281
 
       {
1282
 
          snappedX = true;
1283
 
          p.x = (float)pData->position();
1284
 
       }
1285
 
    }
1286
 
 
1287
 
    return p;
1288
 
}
1289
 
 
1290
 
float KivioCanvas::snapToGridX(float z)
1291
 
{
1292
 
  TKPoint p;
1293
 
  p.set(z,0,UnitPoint);
1294
 
  return snapToGrid(p).x;
1295
 
}
1296
 
 
1297
 
float KivioCanvas::snapToGridY(float z)
1298
 
{
1299
 
  TKPoint p;
1300
 
  p.set(0,z,UnitPoint);
1301
 
  return snapToGrid(p).y;
 
1102
        case Key_Escape: {
 
1103
            m_pToolsController->activateDefault();
 
1104
        }break;
 
1105
    }
 
1106
}
 
1107
 
 
1108
KoPoint KivioCanvas::snapToGridAndGuides(KoPoint point)
 
1109
{
 
1110
  KoPoint p = point;
 
1111
 
 
1112
  KoSize dist = m_pDoc->grid().snap;
 
1113
  KoSize dxy = m_pDoc->grid().freq;
 
1114
 
 
1115
  int dx = qRound(p.x()/dxy.width());
 
1116
  int dy = qRound(p.y()/dxy.height());
 
1117
 
 
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)));
 
1120
 
 
1121
  if( m_pDoc->grid().isSnap)
 
1122
  {
 
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;
 
1126
      } else {
 
1127
        p.rx() = dxy.width() * (dx + 1);
 
1128
      }
 
1129
    }
 
1130
 
 
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;
 
1134
      } else {
 
1135
        p.ry() = dxy.height() * (dy + 1);
 
1136
      }
 
1137
    }
 
1138
  }
 
1139
 
 
1140
  /*
 
1141
  * Now if the point is within 4 pixels of a gridline, snap
 
1142
  * to the grid line.
 
1143
  */
 
1144
  if (m_pView->isSnapGuides())
 
1145
  {
 
1146
    float four = m_pView->zoomHandler()->unzoomItY(4);
 
1147
    KivioGuideLines *pGuides = activePage()->guideLines();
 
1148
    KivioGuideLineData *pData = pGuides->findHorizontal( point.y(), four );
 
1149
 
 
1150
    if( pData )
 
1151
    {
 
1152
      p.ry() = (float)pData->position();
 
1153
    }
 
1154
 
 
1155
    pData = pGuides->findVertical( point.x(), four );
 
1156
 
 
1157
    if( pData )
 
1158
    {
 
1159
      p.rx() = (float)pData->position();
 
1160
    }
 
1161
  }
 
1162
 
 
1163
  return p;
 
1164
}
 
1165
 
 
1166
KoPoint KivioCanvas::snapToGrid(KoPoint point)
 
1167
{
 
1168
  if (!m_pDoc->grid().isSnap)
 
1169
    return point;
 
1170
 
 
1171
  KoPoint p = point;
 
1172
 
 
1173
  KoSize dist = m_pDoc->grid().snap;
 
1174
  KoSize dxy = m_pDoc->grid().freq;
 
1175
 
 
1176
  int dx = qRound(p.x() / dxy.width());
 
1177
  int dy = qRound(p.y() / dxy.height());
 
1178
 
 
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)));
 
1181
 
 
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;
 
1185
    } else {
 
1186
      p.rx() = dxy.width() * (dx + 1);
 
1187
    }
 
1188
  }
 
1189
 
 
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;
 
1193
    } else {
 
1194
      p.ry() = dxy.height() * (dy + 1);
 
1195
    }
 
1196
  }
 
1197
 
 
1198
  return p;
 
1199
}
 
1200
 
 
1201
KoPoint KivioCanvas::snapToGuides(KoPoint point, bool &snappedX, bool &snappedY)
 
1202
{
 
1203
  snappedX = false;
 
1204
  snappedY = false;
 
1205
  KoPoint p = point;
 
1206
 
 
1207
  if (m_pView->isSnapGuides())
 
1208
  {
 
1209
    float four = m_pView->zoomHandler()->unzoomItY(4);
 
1210
    KivioGuideLines *pGuides = activePage()->guideLines();
 
1211
    KivioGuideLineData *pData = pGuides->findHorizontal( point.y(), four );
 
1212
 
 
1213
    if(pData)
 
1214
    {
 
1215
      snappedY = true;
 
1216
      p.ry() = pData->position();
 
1217
    }
 
1218
 
 
1219
    pData = pGuides->findVertical( point.x(), four );
 
1220
 
 
1221
    if(pData)
 
1222
    {
 
1223
      snappedX = true;
 
1224
      p.rx() = pData->position();
 
1225
    }
 
1226
  }
 
1227
 
 
1228
  return p;
 
1229
}
 
1230
 
 
1231
double KivioCanvas::snapToGridX(double z)
 
1232
{
 
1233
  KoPoint p(z, 0);
 
1234
  return snapToGrid(p).x();
 
1235
}
 
1236
 
 
1237
double KivioCanvas::snapToGridY(double z)
 
1238
{
 
1239
  KoPoint p(0, z);
 
1240
  return snapToGrid(p).y();
1302
1241
}
1303
1242
 
1304
1243
void KivioCanvas::updateGuides()
1309
1248
 
1310
1249
void KivioCanvas::guideLinesTimerTimeout()
1311
1250
{
1312
 
  if (!storedCursor)
 
1251
  if (!storedCursor) {
1313
1252
    storedCursor = new QCursor(cursor());
 
1253
  }
 
1254
 
1314
1255
  setCursor(sizeAllCursor);
1315
1256
}
1316
1257
 
1325
1266
 
1326
1267
bool KivioCanvas::eventFilter(QObject* o, QEvent* e)
1327
1268
{
1328
 
  if ((o == m_pVRuler || o == m_pHRuler) && (e->type() == QEvent::MouseMove || e->type() == QEvent::MouseButtonRelease) && m_pView->isShowGuides()) {
1329
 
 
 
1269
  if ((o == view()->vertRuler() || o == view()->horzRuler()) && (e->type() == QEvent::MouseMove || e->type() ==
 
1270
    QEvent::MouseButtonRelease) && m_pView->isShowGuides())
 
1271
  {
1330
1272
    QMouseEvent* me = (QMouseEvent*)e;
1331
1273
    QPoint p = mapFromGlobal(me->globalPos());
1332
1274
    KivioGuideLines* gl = activePage()->guideLines();
1333
1275
 
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)) {
1337
1279
        enterEvent(0);
1338
1280
 
1339
1281
        eraseGuides();
1340
1282
        gl->unselectAll();
1341
1283
        KivioGuideLineData* gd;
1342
 
        TKPoint tp = mapFromScreen(p);
1343
 
        if (o == m_pVRuler)
1344
 
          gd = gl->add(tp.x,Qt::Vertical);
1345
 
        else
1346
 
          gd = gl->add(tp.y,Qt::Horizontal);
 
1284
        KoPoint tp = mapFromScreen(p);
 
1285
 
 
1286
        if (o == view()->vertRuler()) {
 
1287
          gd = gl->add(tp.x(),Qt::Vertical);
 
1288
        } else {
 
1289
          gd = gl->add(tp.y(),Qt::Horizontal);
 
1290
        }
1347
1291
 
1348
1292
        pressGuideline = gd;
1349
1293
        gl->select(gd);
1355
1299
        w->setCursor(sizeAllCursor);
1356
1300
 
1357
1301
        lastPoint = p;
1358
 
 
1359
 
      } else {
1360
 
        if (pressGuideline && !f) {
1361
 
          leaveEvent(0);
1362
 
 
1363
 
          eraseGuides();
1364
 
          gl->remove(pressGuideline);
1365
 
          paintGuides();
1366
 
 
1367
 
          if (storedCursor) {
1368
 
            QWidget* w = (QWidget*)o;
1369
 
            w->setCursor(*storedCursor);
1370
 
            delete storedCursor;
1371
 
            storedCursor = 0;
1372
 
          }
1373
 
 
1374
 
          pressGuideline = 0;
1375
 
        } else {
1376
 
          if (pressGuideline && f) {
1377
 
            QMouseEvent* m = new QMouseEvent(QEvent::MouseMove, p, me->globalPos(), me->button(), me->state());
1378
 
            mouseMoveEvent(m);
1379
 
            delete m;
1380
 
            delegateThisEvent = true;
1381
 
          }
 
1302
      } else if (pressGuideline && !f) {
 
1303
        leaveEvent(0);
 
1304
 
 
1305
        eraseGuides();
 
1306
        gl->remove(pressGuideline);
 
1307
        paintGuides();
 
1308
 
 
1309
        if (storedCursor) {
 
1310
          QWidget* w = (QWidget*)o;
 
1311
          w->setCursor(*storedCursor);
 
1312
          delete storedCursor;
 
1313
          storedCursor = 0;
1382
1314
        }
 
1315
 
 
1316
        pressGuideline = 0;
 
1317
      } else if (pressGuideline && f) {
 
1318
        QMouseEvent* m = new QMouseEvent(QEvent::MouseMove, p, me->globalPos(), me->button(), me->state());
 
1319
        mouseMoveEvent(m);
 
1320
        delete m;
 
1321
        delegateThisEvent = true;
1383
1322
      }
1384
1323
    }
 
1324
 
1385
1325
    if (e->type() == QEvent::MouseButtonRelease && pressGuideline) {
1386
 
 
1387
1326
      eraseGuides();
1388
1327
      gl->unselect(pressGuideline);
1389
1328
      paintGuides();
1401
1340
      delete m;
1402
1341
      delegateThisEvent = true;
1403
1342
    }
 
1343
 
 
1344
    if(o == view()->vertRuler()) {
 
1345
      view()->vertRuler()->update();
 
1346
    } else {
 
1347
      view()->horzRuler()->update();
 
1348
    }
1404
1349
  }
1405
1350
 
1406
 
  return QWidget::eventFilter(o,e);
 
1351
  return QWidget::eventFilter(o, e);
1407
1352
}
1408
1353
 
1409
1354
void KivioCanvas::eraseGuides()
1427
1372
{
1428
1373
  setUpdatesEnabled(false);
1429
1374
 
1430
 
  TKPageLayout pl = activePage()->paperLayout();
1431
 
  float w = pl.ptWidth()/2.0;
1432
 
  float h = pl.ptHeight()/2.0;
1433
 
 
1434
 
  w = w - p.x();
1435
 
  h = h - p.y();
1436
 
 
1437
 
  centerPage();
1438
 
 
1439
 
  scrollDx((int)(w*m_pZoom));
1440
 
  scrollDy((int)(h*m_pZoom));
 
1375
  KivioRect va = visibleArea();
 
1376
 
 
1377
  float x = QMAX(0, p.x() - (va.w() / 2));
 
1378
  float y = QMAX(0, p.y() - (va.h() / 2));
 
1379
 
 
1380
  m_pVertScrollBar->setValue(m_pView->zoomHandler()->zoomItY(y));
 
1381
  m_pHorzScrollBar->setValue(m_pView->zoomHandler()->zoomItX(x));
1441
1382
 
1442
1383
  setUpdatesEnabled(true);
1443
1384
}
1444
1385
 
1445
1386
KivioRect KivioCanvas::visibleArea()
1446
1387
{
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));
1449
1390
 
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());
1451
1392
}
1452
1393
 
1453
1394
void KivioCanvas::setVisibleArea(KivioRect r, int margin)
1454
1395
{
1455
1396
  setUpdatesEnabled(false);
1456
 
 
1457
 
  int cw = width() - 2*margin;
1458
 
  int ch = height() - 2*margin;
1459
 
 
1460
 
  float zw = cw/r.w();
1461
 
  float zh = ch/r.h();
1462
 
  float z = QMIN(zw,zh);
1463
 
 
1464
 
  setZoom(z);
 
1397
  KoZoomHandler zoom;
 
1398
  zoom.setZoomAndResolution(100, QPaintDevice::x11AppDpiX(),
 
1399
    QPaintDevice::x11AppDpiY());
 
1400
 
 
1401
  float cw = width() - 2 * margin;
 
1402
  float ch = height() - 2 * margin;
 
1403
 
 
1404
  float zw = cw / (float)zoom.zoomItX(r.w());
 
1405
  float zh = ch / (float)zoom.zoomItY(r.h());
 
1406
  float z = QMIN(zw, zh);
 
1407
 
 
1408
  setZoom(qRound(z * 100));
1465
1409
 
1466
1410
  KivioPoint c = r.center();
1467
1411
 
1472
1416
void KivioCanvas::setVisibleAreaByWidth(KivioRect r, int margin)
1473
1417
{
1474
1418
  setUpdatesEnabled(false);
1475
 
 
1476
 
  int cw = width() - 2*margin;
1477
 
  float z = cw/r.w();
1478
 
 
1479
 
  setZoom(z);
 
1419
  KoZoomHandler zoom;
 
1420
  zoom.setZoomAndResolution(100, QPaintDevice::x11AppDpiX(),
 
1421
    QPaintDevice::x11AppDpiY());
 
1422
 
 
1423
  float cw = width() - 2*margin;
 
1424
  float z = cw / (float)zoom.zoomItX(r.w());
 
1425
 
 
1426
  setZoom(qRound(z * 100));
1480
1427
 
1481
1428
  KivioPoint c = r.center();
1482
1429
 
1487
1434
void KivioCanvas::setVisibleAreaByHeight(KivioRect r, int margin)
1488
1435
{
1489
1436
  setUpdatesEnabled(false);
1490
 
 
1491
 
  int ch = height() - 2*margin;
1492
 
  float z = ch/r.h();
1493
 
 
1494
 
  setZoom(z);
 
1437
  KoZoomHandler zoom;
 
1438
  zoom.setZoomAndResolution(100, QPaintDevice::x11AppDpiX(),
 
1439
    QPaintDevice::x11AppDpiY());
 
1440
 
 
1441
  float ch = height() - 2*margin;
 
1442
  float z = ch / (float)zoom.zoomItY(r.h());
 
1443
 
 
1444
  setZoom(qRound(z * 100));
1495
1445
 
1496
1446
  KivioPoint c = r.center();
1497
1447
 
1498
1448
  setViewCenterPoint(c);
1499
1449
  setUpdatesEnabled(true);
1500
1450
}
 
1451
 
 
1452
void KivioCanvas::startPasteMoving()
 
1453
{
 
1454
  setEnabled(false);
 
1455
  KivioPoint p = activePage()->getRectForAllSelectedStencils().center();
 
1456
  m_origPoint.setCoords(p.x(), p.y());
 
1457
 
 
1458
  // Create a new painter object
 
1459
  beginUnclippedSpawnerPainter();
 
1460
  drawSelectedStencilsXOR();
 
1461
 
 
1462
  // Build the list of old geometry
 
1463
  KivioRect *pData;
 
1464
  m_lstOldGeometry.clear();
 
1465
  KivioStencil* pStencil = activePage()->selectedStencils()->first();
 
1466
 
 
1467
  while( pStencil )
 
1468
  {
 
1469
    pData = new KivioRect;
 
1470
    *pData = pStencil->rect();
 
1471
    m_lstOldGeometry.append(pData);
 
1472
 
 
1473
    pStencil = activePage()->selectedStencils()->next();
 
1474
  }
 
1475
 
 
1476
  continuePasteMoving(lastPoint);
 
1477
  m_pasteMoving = true;
 
1478
  setEnabled(true);
 
1479
}
 
1480
 
 
1481
void KivioCanvas::continuePasteMoving(const QPoint &pos)
 
1482
{
 
1483
  KoPoint pagePoint = mapFromScreen( pos );
 
1484
 
 
1485
  double dx = pagePoint.x() - m_origPoint.x();
 
1486
  double dy = pagePoint.y() - m_origPoint.y();
 
1487
 
 
1488
  bool snappedX;
 
1489
  bool snappedY;
 
1490
 
 
1491
  double newX, newY;
 
1492
 
 
1493
  // Undraw the old stencils
 
1494
  drawSelectedStencilsXOR();
 
1495
 
 
1496
  // Translate to the new position
 
1497
  KoPoint p;
 
1498
  KivioRect selectedRect = activePage()->getRectForAllSelectedStencils();
 
1499
 
 
1500
  newX = selectedRect.x() + dx;
 
1501
  newY = selectedRect.y() + dy;
 
1502
 
 
1503
  // First attempt a snap-to-grid
 
1504
  p.setCoords(newX, newY);
 
1505
  p = snapToGrid(p);
 
1506
 
 
1507
  newX = p.x();
 
1508
  newY = p.y();
 
1509
 
 
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);
 
1513
 
 
1514
  if(snappedX) {
 
1515
    newX = p.x() - selectedRect.w();
 
1516
  }
 
1517
 
 
1518
  if(snappedY) {
 
1519
    newY = p.y() - selectedRect.h();
 
1520
  }
 
1521
 
 
1522
  p.setCoords(selectedRect.x() + dx, selectedRect.y() + dy);
 
1523
  p = snapToGuides(p, snappedX, snappedY);
 
1524
 
 
1525
  if(snappedX) {
 
1526
    newX = p.x();
 
1527
  }
 
1528
 
 
1529
  if(snappedY) {
 
1530
    newY = p.y();
 
1531
  }
 
1532
 
 
1533
  dx = newX - selectedRect.x();
 
1534
  dy = newY - selectedRect.y();
 
1535
 
 
1536
  // Translate to the new position
 
1537
  KivioStencil *pStencil = activePage()->selectedStencils()->first();
 
1538
  KivioRect* pData = m_lstOldGeometry.first();
 
1539
  // bool move = true;
 
1540
 
 
1541
  while( pStencil && pData )
 
1542
  {
 
1543
    newX = pData->x() + dx;
 
1544
    newY = pData->y() + dy;
 
1545
 
 
1546
    if( pStencil->protection()->at( kpX ) == false ) {
 
1547
      pStencil->setX(newX);
 
1548
    }
 
1549
    if( pStencil->protection()->at( kpY ) == false ) {
 
1550
      pStencil->setY(newY);
 
1551
    }
 
1552
 
 
1553
    pData = m_lstOldGeometry.next();
 
1554
    pStencil = activePage()->selectedStencils()->next();
 
1555
  }
 
1556
 
 
1557
  // Draw the stencils
 
1558
  drawSelectedStencilsXOR();
 
1559
  m_pView->updateToolBars();
 
1560
}
 
1561
 
 
1562
void KivioCanvas::endPasteMoving()
 
1563
{
 
1564
  KivioStencil *pStencil = activePage()->selectedStencils()->first();
 
1565
  KivioRect *pData = m_lstOldGeometry.first();
 
1566
 
 
1567
  while( pStencil && pData )
 
1568
  {
 
1569
    if(pStencil->type() == kstConnector) {
 
1570
      pStencil->searchForConnections(m_pView->activePage(), m_pView->zoomHandler()->unzoomItY(4));
 
1571
    }
 
1572
 
 
1573
    pData = m_lstOldGeometry.next();
 
1574
    pStencil = activePage()->selectedStencils()->next();
 
1575
  }
 
1576
 
 
1577
  drawSelectedStencilsXOR();
 
1578
 
 
1579
  endUnclippedSpawnerPainter();
 
1580
 
 
1581
  // Clear the list of old geometry
 
1582
  m_lstOldGeometry.clear();
 
1583
  m_pasteMoving = false;
 
1584
}
 
1585
 
1501
1586
#include "kivio_canvas.moc"