~ubuntu-branches/ubuntu/quantal/muse/quantal

« back to all changes in this revision

Viewing changes to widgets/canvas.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2005-08-23 17:19:39 UTC
  • mto: (4.1.1 breezy) (1.1.9) (10.1.6 sid)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20050823171939-hd8fgzokb4dbj007
Tags: upstream-0.7.1+0.7.2pre2
ImportĀ upstreamĀ versionĀ 0.7.1+0.7.2pre2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//=========================================================
2
 
//  MusE
3
 
//  Linux Music Editor
4
 
//    $Id: canvas.cpp,v 1.2 2003/10/29 14:19:26 lunar_shuttle Exp $
5
 
//  (C) Copyright 1999 Werner Schweer (ws@seh.de)
6
 
//=========================================================
7
 
 
8
 
#include <stdio.h>
9
 
#include "canvas.h"
10
 
#include <qpainter.h>
11
 
#include "song.h"
12
 
#include "citem.h"
13
 
#include <qpopupmenu.h>
14
 
#include <qcursor.h>
15
 
#include "icons.h"
16
 
#include "../marker/marker.h"
17
 
#include "event.h"
18
 
#define ABS(x)  ((x) < 0) ? -(x) : (x)
19
 
 
20
 
//---------------------------------------------------------
21
 
//   Canvas
22
 
//---------------------------------------------------------
23
 
 
24
 
Canvas::Canvas(QWidget* parent, int sx, int sy, const char* name)
25
 
   : View(parent, sx, sy, name)
26
 
      {
27
 
      canvasTools = 0;
28
 
      itemPopupMenu = 0;
29
 
      drag    = DRAG_OFF;
30
 
      _tool   = PointerTool;
31
 
      pos[0]  = song->cpos();
32
 
      pos[1]  = song->lpos();
33
 
      pos[2]  = song->rpos();
34
 
      curPart = NULL;
35
 
      curPartId = -1;
36
 
      curItem = NULL;
37
 
      connect(song, SIGNAL(posChanged(int, int, bool)), this, SLOT(setPos(int, int, bool)));
38
 
      }
39
 
 
40
 
//---------------------------------------------------------
41
 
//   setPos
42
 
//    set one of three markers
43
 
//    idx   - 0-cpos  1-lpos  2-rpos
44
 
//    flag  - emit followEvent()
45
 
//---------------------------------------------------------
46
 
 
47
 
void Canvas::setPos(int idx, int val, bool adjustScrollbar)
48
 
      {
49
 
      if (pos[idx] == val)
50
 
            return;
51
 
 
52
 
      int opos = mapx(pos[idx]);
53
 
      int npos = mapx(val);
54
 
 
55
 
      if (adjustScrollbar && idx == 0) {
56
 
            switch (song->follow()) {
57
 
                  case  Song::NO:
58
 
                        break;
59
 
                  case Song::JUMP:
60
 
                        if (npos >= width()) {
61
 
                              int ppos =  val - rmapxDev(width()/8);
62
 
                              if (ppos < 0)
63
 
                                    ppos = 0;
64
 
                              emit followEvent(ppos);
65
 
                              opos = mapx(pos[idx]);
66
 
                              npos = mapx(val);
67
 
                              }
68
 
                        else if (npos < 0) {
69
 
                              int ppos =  val - rmapxDev(width()*3/4);
70
 
                              if (ppos < 0)
71
 
                                    ppos = 0;
72
 
                              emit followEvent(ppos);
73
 
                              opos = mapx(pos[idx]);
74
 
                              npos = mapx(val);
75
 
                              }
76
 
                        break;
77
 
                  case Song::CONTINUOUS:
78
 
                        if (npos > (width()/2)) {
79
 
                              int ppos =  pos[idx] - rmapxDev(width()/2);
80
 
                              if (ppos < 0)
81
 
                                    ppos = 0;
82
 
                              emit followEvent(ppos);
83
 
                              opos = mapx(pos[idx]);
84
 
                              npos = mapx(val);
85
 
                              }
86
 
                        else if (npos < (width()/2)) {
87
 
                              int ppos =  pos[idx] - rmapxDev(width()/2);
88
 
                              if (ppos < 0)
89
 
                                    ppos = 0;
90
 
                              emit followEvent(ppos);
91
 
                              opos = mapx(pos[idx]);
92
 
                              npos = mapx(val);
93
 
                              }
94
 
                        break;
95
 
                  }
96
 
            }
97
 
 
98
 
      int x;
99
 
      int w = 1;
100
 
      if (opos > npos) {
101
 
            w += opos - npos;
102
 
            x = npos;
103
 
            }
104
 
      else {
105
 
            w += npos - opos;
106
 
            x = opos;
107
 
            }
108
 
      pos[idx] = val;
109
 
      redraw(QRect(x-1, 0, w+2, height()));
110
 
      }
111
 
 
112
 
//---------------------------------------------------------
113
 
//   draw
114
 
//---------------------------------------------------------
115
 
 
116
 
void Canvas::draw(QPainter& p, const QRect& rect)
117
 
      {
118
 
//      printf("draw canvas %x virt %d\n", this, virt());
119
 
 
120
 
      int x = rect.x();
121
 
      int y = rect.y();
122
 
      int w = rect.width();
123
 
      int h = rect.height();
124
 
      int x2 = x + w;
125
 
 
126
 
      if (virt()) {
127
 
            drawCanvas(p, rect);
128
 
 
129
 
            //---------------------------------------------------
130
 
            // draw Canvas Items
131
 
            //---------------------------------------------------
132
 
 
133
 
            iCItem to(items.lower_bound(x2));
134
 
            for (iCItem i = items.begin(); i != to; ++i) {
135
 
                  if (i->second->intersects(rect))
136
 
                        drawItem(p, i->second, i->second->bbox().intersect(rect));
137
 
                  }
138
 
            to = moving.lower_bound(x2);
139
 
            for (iCItem i = moving.begin(); i != to; ++i) {
140
 
                  if (i->second->intersects(rect))
141
 
                        drawItem(p, i->second, i->second->bbox().intersect(rect));
142
 
                  }
143
 
 
144
 
            }
145
 
      else {
146
 
            p.save();
147
 
            setPainter(p);
148
 
 
149
 
            if (xmag < 0) {
150
 
                  x -= 1;
151
 
                  w += 2;
152
 
                  x = (x + xpos + rmapx(xorg)) * (-xmag);
153
 
                  w = w * (-xmag);
154
 
                  }
155
 
            else {
156
 
                  x = (x + xpos + rmapx(xorg)) / xmag;
157
 
                  w = (w + xmag - 1) / xmag;
158
 
                  x -= 1;
159
 
                  w += 2;
160
 
                  }
161
 
            if (ymag < 0) {
162
 
                  y -= 1;
163
 
                  h += 2;
164
 
                  y = (y + ypos + rmapy(yorg)) * (-ymag);
165
 
                  h = h * (-ymag);
166
 
                  }
167
 
            else {
168
 
                  y = (rect.y() + ypos + rmapy(yorg))/ymag;
169
 
                  h = (rect.height()+ymag-1)/ymag;
170
 
                  y -= 1;
171
 
                  h += 2;
172
 
                  }
173
 
 
174
 
            if (x < 0)
175
 
                  x = 0;
176
 
            if (y < 0)
177
 
                  y = 0;
178
 
            x2 = x + w;
179
 
 
180
 
            drawCanvas(p, QRect(x, y, w, h));
181
 
            p.restore();
182
 
 
183
 
            //---------------------------------------------------
184
 
            // draw Canvas Items
185
 
            //---------------------------------------------------
186
 
 
187
 
            for (iCItem i = items.begin(); i != items.end(); ++i) {
188
 
                  CItem* ci = i->second;
189
 
                  QRect r(ci->bbox());
190
 
                  r.moveCenter(map((ci->pos())));
191
 
                  if (r.intersects(rect))
192
 
                        drawItem(p, ci, rect);
193
 
                  }
194
 
            for (iCItem i = moving.begin(); i != moving.end(); ++i) {
195
 
                  QRect r(i->second->bbox());
196
 
                  r.moveCenter(map(i->second->pos()));
197
 
                  if (r.intersects(rect))
198
 
                        drawItem(p, i->second, rect);
199
 
                  }
200
 
            setPainter(p);
201
 
            }
202
 
 
203
 
      //---------------------------------------------------
204
 
      //    draw marker
205
 
      //---------------------------------------------------
206
 
 
207
 
      int y2 = y + h;
208
 
      MarkerList* marker = song->marker();
209
 
      for (iMarker m = marker->begin(); m != marker->end(); ++m) {
210
 
            int xp = m->second.posTick();
211
 
            if (xp >= x && xp < x+w) {
212
 
                  p.setPen(green);
213
 
                  p.drawLine(xp, y, xp, y2);
214
 
                  }
215
 
            }
216
 
 
217
 
      //---------------------------------------------------
218
 
      //    draw location marker
219
 
      //---------------------------------------------------
220
 
 
221
 
      p.setPen(blue);
222
 
      if (pos[1] >= x && pos[1] < x2) {
223
 
            p.drawLine(pos[1], y, pos[1], y2);
224
 
            }
225
 
      if (pos[2] >= x && pos[2] < x2)
226
 
            p.drawLine(pos[2], y, pos[2], y2);
227
 
      p.setPen(red);
228
 
      if (pos[0] >= x && pos[0] < x2) {
229
 
            p.drawLine(pos[0], y, pos[0], y2);
230
 
            }
231
 
 
232
 
      //---------------------------------------------------
233
 
      //    draw lasso
234
 
      //---------------------------------------------------
235
 
 
236
 
      if (drag == DRAG_LASSO) {
237
 
            p.setPen(blue);
238
 
            p.setBrush(NoBrush);
239
 
            p.drawRect(lasso);
240
 
            }
241
 
      }
242
 
 
243
 
//---------------------------------------------------------
244
 
//   deselectAll
245
 
//---------------------------------------------------------
246
 
 
247
 
void Canvas::deselectAll()
248
 
      {
249
 
      for (iCItem i = items.begin(); i != items.end(); ++i)
250
 
            i->second->setSelected(false);
251
 
      }
252
 
 
253
 
//---------------------------------------------------------
254
 
//   selectItem
255
 
//---------------------------------------------------------
256
 
 
257
 
void Canvas::selectItem(CItem* e, bool flag)
258
 
      {
259
 
      e->setSelected(flag);
260
 
      }
261
 
 
262
 
//---------------------------------------------------------
263
 
//   startMoving
264
 
//    copy selection-List to moving-List
265
 
//---------------------------------------------------------
266
 
 
267
 
void Canvas::startMoving(const QPoint& pos, DragType)
268
 
      {
269
 
      for (iCItem i = items.begin(); i != items.end(); ++i) {
270
 
            if (i->second->isSelected()) {
271
 
                  i->second->setMoving(true);
272
 
                  moving.add(i->second);
273
 
                  }
274
 
            }
275
 
      moveItems(pos, 0);
276
 
      }
277
 
 
278
 
//---------------------------------------------------------
279
 
//   moveItems
280
 
//    dir = 0     move in all directions
281
 
//          1     move only horizontal
282
 
//          2     move only vertical
283
 
//---------------------------------------------------------
284
 
 
285
 
void Canvas::moveItems(const QPoint& pos, int dir = 0)
286
 
      {
287
 
      int dp = y2pitch(pos.y()) - y2pitch(start.y());
288
 
      int dx = pos.x() - start.x();
289
 
      if (dir == 1)
290
 
            dp = 0;
291
 
      else if (dir == 2)
292
 
            dx = 0;
293
 
      for (iCItem i = moving.begin(); i != moving.end(); ++i) {
294
 
            int x = i->second->pos().x();
295
 
            int y = i->second->pos().y();
296
 
            int nx = x + dx;
297
 
            int ny = pitch2y(y2pitch(y) + dp);
298
 
            QPoint mp(raster(QPoint(nx, ny)));
299
 
            if (i->second->mp() != mp) {
300
 
                  i->second->setMp(mp);
301
 
                  itemMoved(i->second, mp);
302
 
                  }
303
 
            }
304
 
      redraw();
305
 
      }
306
 
 
307
 
//---------------------------------------------------------
308
 
//   viewKeyPressEvent
309
 
//---------------------------------------------------------
310
 
 
311
 
void Canvas::viewKeyPressEvent(QKeyEvent* event)
312
 
      {
313
 
      keyPress(event);
314
 
      }
315
 
 
316
 
//---------------------------------------------------------
317
 
//   viewMousePressEvent
318
 
//---------------------------------------------------------
319
 
 
320
 
void Canvas::viewMousePressEvent(QMouseEvent* event)
321
 
      {
322
 
      keyState = event->state();
323
 
 
324
 
      // special events if right button is clicked while operations
325
 
      // like moving or drawing lasso is performed.
326
 
      if (event->stateAfter() & RightButton) {
327
 
            switch (drag) { 
328
 
                  case DRAG_LASSO: 
329
 
                        drag = DRAG_OFF; 
330
 
                        redraw(); 
331
 
                        return; 
332
 
                  case DRAG_MOVE: 
333
 
                        drag = DRAG_OFF; 
334
 
                        endMoveItems (start, MOVE_MOVE, 0); 
335
 
                        return; 
336
 
                  default: 
337
 
                  break; 
338
 
            } 
339
 
      }
340
 
 
341
 
      // ignore event if (another) button is already active:
342
 
      if (keyState & (LeftButton|RightButton|MidButton)) {
343
 
            return;
344
 
            }
345
 
      bool shift      = keyState & ShiftButton;
346
 
      bool alt        = keyState & AltButton;
347
 
      bool ctrl       = keyState & ControlButton; 
348
 
      start           = event->pos();
349
 
 
350
 
      //---------------------------------------------------
351
 
      //    set curItem to item mouse is pointing
352
 
      //    (if any)
353
 
      //---------------------------------------------------
354
 
 
355
 
      if (virt())
356
 
            curItem = items.find(start);
357
 
      else {
358
 
            curItem = 0;
359
 
            for (iCItem i = items.begin(); i != items.end(); ++i) {
360
 
                  QRect box = i->second->bbox();
361
 
                  int x = rmapxDev(box.x());
362
 
                  int y = rmapyDev(box.y());
363
 
                  int w = rmapxDev(box.width());
364
 
                  int h = rmapyDev(box.height());
365
 
                  QRect r(x, y, w, h);
366
 
                  r.moveBy(i->second->pos().x(), i->second->pos().y());
367
 
                  if (r.contains(start)) {
368
 
                        curItem = i->second;
369
 
                        break;
370
 
                        }
371
 
                  }
372
 
            }
373
 
 
374
 
      if (curItem && (event->button() == QMouseEvent::MidButton)) {
375
 
            if (!curItem->isSelected()) {
376
 
                  selectItem(curItem, true);
377
 
                  updateSelection();
378
 
                  redraw();
379
 
                  }
380
 
            startDrag(curItem, shift);
381
 
            }
382
 
      else if (event->button() == QMouseEvent::RightButton) {
383
 
            if (curItem) {
384
 
                  itemPopupMenu = genItemPopup(curItem);
385
 
                  if (itemPopupMenu) {
386
 
                        int n = itemPopupMenu->exec(QCursor::pos());
387
 
                        if (n != -1)
388
 
                              itemPopup(curItem, n, start);
389
 
                        delete itemPopupMenu;
390
 
                        }
391
 
                  }
392
 
            else {
393
 
                  canvasPopupMenu = genCanvasPopup();
394
 
                  if (canvasPopupMenu) {
395
 
                        int n = canvasPopupMenu->exec(QCursor::pos(), 0);
396
 
                        if (n != -1)
397
 
                              canvasPopup(n);
398
 
                        delete canvasPopupMenu;
399
 
                        }
400
 
                  }
401
 
            }
402
 
      else if (event->button() == QMouseEvent::LeftButton) {
403
 
            switch (_tool) {
404
 
                  case PointerTool:
405
 
                        if (curItem) {
406
 
                              itemPressed(curItem);
407
 
                              if (curItem->part() != curPart) {
408
 
                                    curPart = curItem->part();
409
 
                                    curPartId = curPart->sn();
410
 
                                    curPartChanged();
411
 
                                    }
412
 
                              if (shift)
413
 
                                    drag = DRAG_COPY_START;
414
 
                              else if (alt) {
415
 
                                    drag = DRAG_CLONE_START;
416
 
// printf("CLONE start\n");
417
 
                                    }
418
 
                              else if (ctrl) { //Select all on the same pitch (e.g. same y-value) 
419
 
                                          deselectAll(); 
420
 
                                          for (iCItem i = items.begin(); i != items.end(); ++i) { 
421
 
                                                if (i->second->y() == curItem->y() ) 
422
 
                                                      selectItem(i->second, true); 
423
 
                                          } 
424
 
                                          updateSelection(); 
425
 
                                          redraw(); 
426
 
                                    } 
427
 
                              else
428
 
                                    drag = DRAG_MOVE_START;
429
 
                              }
430
 
                        else
431
 
                              drag = DRAG_LASSO_START;
432
 
                        setCursor();
433
 
                        break;
434
 
 
435
 
                  case RubberTool:
436
 
                        deleteItem(start);
437
 
                        drag = DRAG_DELETE;
438
 
                        setCursor();
439
 
                        break;
440
 
 
441
 
                  case PencilTool:
442
 
                        if (curItem) {
443
 
                              drag = DRAG_RESIZE;
444
 
                              setCursor();
445
 
                              int dx = start.x() - curItem->x();
446
 
                              curItem->setWidth(dx);
447
 
                              start.setX(curItem->x());
448
 
                              }
449
 
                        else {
450
 
                              drag = DRAG_NEW;
451
 
                              setCursor();
452
 
                              curItem = newItem(start, event->state());
453
 
                              if (curItem)
454
 
                                    items.add(curItem);
455
 
                              else {
456
 
                                    drag = DRAG_OFF;
457
 
                                    setCursor();
458
 
                                    }
459
 
                              }
460
 
                        deselectAll();
461
 
                        if (curItem)
462
 
                              selectItem(curItem, true);
463
 
                        updateSelection();
464
 
                        redraw();
465
 
                        break;
466
 
 
467
 
                  default:
468
 
                        break;
469
 
                  }
470
 
            }
471
 
      mousePress(event);
472
 
      }
473
 
 
474
 
//---------------------------------------------------------
475
 
//   viewMouseMoveEvent
476
 
//---------------------------------------------------------
477
 
 
478
 
void Canvas::viewMouseMoveEvent(QMouseEvent* event)
479
 
      {
480
 
      QPoint pos  = event->pos();
481
 
      QPoint dist = pos - start;
482
 
      int ax      = ABS(rmapx(dist.x()));
483
 
      int ay      = ABS(rmapy(dist.y()));
484
 
      bool moving = (ax >= 2) || (ay > 2);
485
 
 
486
 
      switch (drag) {
487
 
            case DRAG_LASSO_START:
488
 
                  if (!moving)
489
 
                        break;
490
 
                  drag = DRAG_LASSO;
491
 
                  setCursor();
492
 
                  // proceed with DRAG_LASSO:
493
 
 
494
 
            case DRAG_LASSO:
495
 
                  lasso = QRect(start.x(), start.y(), dist.x(), dist.y());
496
 
                  redraw();
497
 
                  break;
498
 
 
499
 
            case DRAG_MOVE_START:
500
 
            case DRAG_COPY_START:
501
 
            case DRAG_CLONE_START:
502
 
                  if (!moving)
503
 
                        break;
504
 
                  if (keyState & ControlButton) {
505
 
                        if (ax > ay) {
506
 
                              if (drag == DRAG_MOVE_START)
507
 
                                    drag = DRAGX_MOVE;
508
 
                              else if (drag == DRAG_COPY_START)
509
 
                                    drag = DRAGX_COPY;
510
 
                              else
511
 
                                    drag = DRAGX_CLONE;
512
 
                              }
513
 
                        else {
514
 
                              if (drag == DRAG_MOVE_START)
515
 
                                    drag = DRAGY_MOVE;
516
 
                              else if (drag == DRAG_COPY_START)
517
 
                                    drag = DRAGY_COPY;
518
 
                              else
519
 
                                    drag = DRAGY_CLONE;
520
 
                              }
521
 
                        }
522
 
                  else {
523
 
                        if (drag == DRAG_MOVE_START)
524
 
                              drag = DRAG_MOVE;
525
 
                        else if (drag == DRAG_COPY_START)
526
 
                              drag = DRAG_COPY;
527
 
                        else
528
 
                              drag = DRAG_CLONE;
529
 
                        }
530
 
                  setCursor();
531
 
                  if (!curItem->isSelected()) {
532
 
                        if (drag == DRAG_MOVE)
533
 
                              deselectAll();
534
 
                        selectItem(curItem, true);
535
 
                        updateSelection();
536
 
                        redraw();
537
 
                        }
538
 
                  DragType dt;
539
 
                  if (drag == DRAG_MOVE)
540
 
                        dt = MOVE_MOVE;
541
 
                  else if (drag == DRAG_COPY)
542
 
                        dt = MOVE_COPY;
543
 
                  else
544
 
                        dt = MOVE_CLONE;
545
 
                  startMoving(pos, dt);
546
 
                  break;
547
 
 
548
 
            case DRAG_MOVE:
549
 
            case DRAG_COPY:
550
 
            case DRAG_CLONE:
551
 
                  moveItems(pos, 0);
552
 
                  break;
553
 
 
554
 
            case DRAGX_MOVE:
555
 
            case DRAGX_COPY:
556
 
            case DRAGX_CLONE:
557
 
                  moveItems(pos, 1);
558
 
                  break;
559
 
 
560
 
            case DRAGY_MOVE:
561
 
            case DRAGY_COPY:
562
 
            case DRAGY_CLONE:
563
 
                  moveItems(pos, 2);
564
 
                  break;
565
 
 
566
 
            case DRAG_NEW:
567
 
            case DRAG_RESIZE:
568
 
                  if (dist.x()) {
569
 
                        if (dist.x() < 1)
570
 
                              curItem->setWidth(1);
571
 
                        else
572
 
                              curItem->setWidth(dist.x());
573
 
                        redraw();
574
 
                        }
575
 
                  break;
576
 
            case DRAG_DELETE:
577
 
                  deleteItem(pos);
578
 
                  break;
579
 
 
580
 
            case DRAG_OFF:
581
 
                  break;
582
 
            }
583
 
      mouseMove(pos);
584
 
      }
585
 
 
586
 
//---------------------------------------------------------
587
 
//   viewMouseReleaseEvent
588
 
//---------------------------------------------------------
589
 
 
590
 
void Canvas::viewMouseReleaseEvent(QMouseEvent* event)
591
 
      {
592
 
// printf("release %x %x\n", event->state(), event->button());
593
 
 
594
 
      if (event->state() & (LeftButton|RightButton|MidButton) & ~(event->button())) {
595
 
            printf("ignore %x  %x\n", keyState, event->button());
596
 
            return;
597
 
            }
598
 
 
599
 
      QPoint pos = event->pos();
600
 
      bool shift = event->state() & ShiftButton;
601
 
      bool redrawFlag = false;
602
 
 
603
 
      switch (drag) {
604
 
            case DRAG_MOVE_START:
605
 
            case DRAG_COPY_START:
606
 
            case DRAG_CLONE_START:
607
 
                  if (!shift)
608
 
                        deselectAll();
609
 
                  selectItem(curItem, !(shift && curItem->isSelected()));
610
 
                  updateSelection();
611
 
                  redrawFlag = true;
612
 
                  itemReleased(curItem, curItem->pos());
613
 
                  break;
614
 
            case DRAG_COPY:
615
 
                  endMoveItems(pos, MOVE_COPY, 0);
616
 
                  break;
617
 
            case DRAGX_COPY:
618
 
                  endMoveItems(pos, MOVE_COPY, 1);
619
 
                  break;
620
 
            case DRAGY_COPY:
621
 
                  endMoveItems(pos, MOVE_COPY, 2);
622
 
                  break;
623
 
            case DRAG_MOVE:
624
 
                  endMoveItems(pos, MOVE_MOVE, 0);
625
 
                  break;
626
 
            case DRAGX_MOVE:
627
 
                  endMoveItems(pos, MOVE_MOVE, 1);
628
 
                  break;
629
 
            case DRAGY_MOVE:
630
 
                  endMoveItems(pos, MOVE_MOVE, 2);
631
 
                  break;
632
 
            case DRAG_CLONE:
633
 
                  endMoveItems(pos, MOVE_CLONE, 0);
634
 
                  break;
635
 
            case DRAGX_CLONE:
636
 
                  endMoveItems(pos, MOVE_CLONE, 1);
637
 
                  break;
638
 
            case DRAGY_CLONE:
639
 
                  endMoveItems(pos, MOVE_CLONE, 2);
640
 
                  break;
641
 
            case DRAG_OFF:
642
 
                  break;
643
 
            case DRAG_RESIZE:
644
 
                  resizeItem(curItem, false);
645
 
                  break;
646
 
            case DRAG_NEW:
647
 
                  newItem(curItem, false);
648
 
                  redrawFlag = true;
649
 
                  break;
650
 
            case DRAG_LASSO_START:
651
 
                  lasso.setRect(-1, -1, -1, -1);
652
 
                  if (!shift)
653
 
                        deselectAll();
654
 
                  updateSelection();
655
 
                  redrawFlag = true;
656
 
                  break;
657
 
 
658
 
            case DRAG_LASSO:
659
 
                  if (!shift)
660
 
                        deselectAll();
661
 
                  lasso = lasso.normalize();
662
 
                  selectLasso(shift);
663
 
                  updateSelection();
664
 
                  redrawFlag = true;
665
 
                  break;
666
 
 
667
 
            case DRAG_DELETE:
668
 
                  break;
669
 
            }
670
 
      drag = DRAG_OFF;
671
 
      if (redrawFlag)
672
 
            redraw();
673
 
      setCursor();
674
 
      }
675
 
 
676
 
//---------------------------------------------------------
677
 
//   selectLasso
678
 
//---------------------------------------------------------
679
 
 
680
 
void Canvas::selectLasso(bool toggle)
681
 
      {
682
 
      int n = 0;
683
 
      if (virt()) {
684
 
            for (iCItem i = items.begin(); i != items.end(); ++i) {
685
 
                  if (i->second->intersects(lasso)) {
686
 
                        selectItem(i->second, !(toggle && i->second->isSelected()));
687
 
                        ++n;
688
 
                        }
689
 
                  }
690
 
            }
691
 
      else {
692
 
            for (iCItem i = items.begin(); i != items.end(); ++i) {
693
 
                  QRect box = i->second->bbox();
694
 
                  int x = rmapxDev(box.x());
695
 
                  int y = rmapyDev(box.y());
696
 
                  int w = rmapxDev(box.width());
697
 
                  int h = rmapyDev(box.height());
698
 
                  QRect r(x, y, w, h);
699
 
                  r.moveBy(i->second->pos().x(), i->second->pos().y());
700
 
                  if (r.intersects(lasso)) {
701
 
                        selectItem(i->second, !(toggle && i->second->isSelected()));
702
 
                        ++n;
703
 
                        }
704
 
                  }
705
 
            }
706
 
      if (n) {
707
 
            updateSelection();
708
 
            redraw();
709
 
            }
710
 
      }
711
 
 
712
 
//---------------------------------------------------------
713
 
//   endMoveItems
714
 
//    dir = 0     move in all directions
715
 
//          1     move only horizontal
716
 
//          2     move only vertical
717
 
//---------------------------------------------------------
718
 
 
719
 
void Canvas::endMoveItems(const QPoint& pos, DragType dragtype, int dir)
720
 
      {
721
 
      startUndo(dragtype);
722
 
 
723
 
      int dp = y2pitch(pos.y()) - y2pitch(start.y());
724
 
      int dx = pos.x() - start.x();
725
 
 
726
 
      if (dir == 1)
727
 
            dp = 0;
728
 
      else if (dir == 2)
729
 
            dx = 0;
730
 
      for (iCItem i = moving.begin(); i != moving.end(); ++i) {
731
 
            int x = i->second->pos().x();
732
 
            int y = i->second->pos().y();
733
 
            int nx = x + dx;
734
 
            int ny = pitch2y(y2pitch(y) + dp);
735
 
            QPoint newpos = raster(QPoint(nx, ny));
736
 
            selectItem(i->second, true);
737
 
            if (moveItem(i->second, newpos, dragtype))
738
 
                  i->second->move(newpos);
739
 
            if (moving.size() == 1) {
740
 
                  itemReleased(curItem, newpos);
741
 
                  }
742
 
            if (dragtype == MOVE_COPY || dragtype == MOVE_CLONE)
743
 
                  selectItem(i->second, false);
744
 
            }
745
 
      endUndo(dragtype);
746
 
      moving.clear();
747
 
      updateSelection();
748
 
      redraw();
749
 
      }
750
 
 
751
 
//---------------------------------------------------------
752
 
//   deleteItem
753
 
//---------------------------------------------------------
754
 
 
755
 
void Canvas::deleteItem(const QPoint& p)
756
 
      {
757
 
      if (virt()) {
758
 
            for (iCItem i = items.begin(); i != items.end(); ++i) {
759
 
                  if (i->second->contains(p)) {
760
 
                        if (deleteItem(i->second))
761
 
                              selectItem(i->second, false);
762
 
                        else {
763
 
                              if (drag == DRAG_DELETE)
764
 
                                    drag = DRAG_OFF;
765
 
                              }
766
 
                        break;
767
 
                        }
768
 
                  }
769
 
            }
770
 
      else {
771
 
            for (iCItem i = items.begin(); i != items.end(); ++i) {
772
 
                  QRect box = i->second->bbox();
773
 
                  int x = rmapxDev(box.x());
774
 
                  int y = rmapyDev(box.y());
775
 
                  int w = rmapxDev(box.width());
776
 
                  int h = rmapyDev(box.height());
777
 
                  QRect r(x, y, w, h);
778
 
                  r.moveBy(i->second->pos().x(), i->second->pos().y());
779
 
                  if (r.contains(p)) {
780
 
                        if (deleteItem(i->second)) {
781
 
                              selectItem(i->second, false);
782
 
                              break;
783
 
                              }
784
 
                        }
785
 
                  }
786
 
            }
787
 
      }
788
 
 
789
 
//---------------------------------------------------------
790
 
//   setTool
791
 
//---------------------------------------------------------
792
 
 
793
 
void Canvas::setTool(int t)
794
 
      {
795
 
      if (_tool == Tool(t))
796
 
            return;
797
 
      _tool = Tool(t);
798
 
      setCursor();
799
 
      }
800
 
 
801
 
//---------------------------------------------------------
802
 
//   setCursor
803
 
//---------------------------------------------------------
804
 
 
805
 
void Canvas::setCursor()
806
 
      {
807
 
      switch (drag) {
808
 
            case DRAGX_MOVE:
809
 
            case DRAGX_COPY:
810
 
            case DRAGX_CLONE:
811
 
                  QWidget::setCursor(QCursor(SizeHorCursor));
812
 
                  break;
813
 
 
814
 
            case DRAGY_MOVE:
815
 
            case DRAGY_COPY:
816
 
            case DRAGY_CLONE:
817
 
                  QWidget::setCursor(QCursor(SizeVerCursor));
818
 
                  break;
819
 
 
820
 
            case DRAG_MOVE:
821
 
            case DRAG_COPY:
822
 
            case DRAG_CLONE:
823
 
                  QWidget::setCursor(QCursor(SizeAllCursor));
824
 
                  break;
825
 
 
826
 
            case DRAG_RESIZE:
827
 
//                  QWidget::setCursor(QCursor(SizeHorCursor));
828
 
//                  break;
829
 
 
830
 
            case DRAG_DELETE:
831
 
            case DRAG_COPY_START:
832
 
            case DRAG_CLONE_START:
833
 
            case DRAG_MOVE_START:
834
 
            case DRAG_NEW:
835
 
            case DRAG_LASSO_START:
836
 
            case DRAG_LASSO:
837
 
            case DRAG_OFF:
838
 
                  switch(_tool) {
839
 
                        case PencilTool:
840
 
                              QWidget::setCursor(QCursor(*pencilIcon, 4, 15));
841
 
                              break;
842
 
                        case RubberTool:
843
 
                              QWidget::setCursor(QCursor(*deleteIcon, 4, 15));
844
 
                              break;
845
 
                        case GlueTool:
846
 
                              QWidget::setCursor(QCursor(*glueIcon, 4, 15));
847
 
                              break;
848
 
                        case CutTool:
849
 
                              QWidget::setCursor(QCursor(*cutIcon, 4, 15));
850
 
                              break;
851
 
                        case MuteTool:
852
 
                              QWidget::setCursor(QCursor(*editmuteIcon, 4, 15));
853
 
                              break;
854
 
                        default:
855
 
                              QWidget::setCursor(QCursor(ArrowCursor));
856
 
                              break;
857
 
                        }
858
 
                  break;
859
 
            }
860
 
      }
861
 
 
862
 
//---------------------------------------------------------
863
 
//   keyPress
864
 
//---------------------------------------------------------
865
 
 
866
 
void Canvas::keyPress(QKeyEvent* event)
867
 
      {
868
 
      //Alt-P, shortcut for both DrumEditor and PianoRoll. Sets locators to include selected events 
869
 
      if (event->key() == Key_P && event->state() & AltButton) {
870
 
            int tick_max = 0;
871
 
            int tick_min = INT_MAX;
872
 
            bool found = false;
873
 
 
874
 
            for (iCItem i= items.begin(); i != items.end(); i++) {
875
 
                  if (!i->second->isSelected())
876
 
                        continue;
877
 
 
878
 
                  int tick = i->second->x();
879
 
                  int len = i->second->event()->lenTick();
880
 
                  found = true;
881
 
                  if (tick + len > tick_max)
882
 
                        tick_max = tick + len;
883
 
                  if (tick < tick_min)
884
 
                        tick_min = tick;
885
 
            }
886
 
            if (found) {
887
 
                  song->setPos(1, tick_min);
888
 
                  song->setPos(2, tick_max);
889
 
            }
890
 
      }
891
 
      else
892
 
            event->ignore();
893
 
      }
894
 
 
895
 
//---------------------------------------------------------
896
 
//   isSingleSelection
897
 
//---------------------------------------------------------
898
 
 
899
 
bool Canvas::isSingleSelection()
900
 
      {
901
 
      return selectionSize() == 1;
902
 
      }
903
 
 
904
 
//---------------------------------------------------------
905
 
//   selectionSize
906
 
//---------------------------------------------------------
907
 
 
908
 
int Canvas::selectionSize()
909
 
      {
910
 
      int n = 0;
911
 
      for (iCItem i = items.begin(); i != items.end(); ++i) {
912
 
            if (i->second->isSelected())
913
 
                  ++n;
914
 
            }
915
 
      return n;
916
 
      }
917
 
 
918
 
//---------------------------------------------------------
919
 
//   genCanvasPopup
920
 
//---------------------------------------------------------
921
 
 
922
 
QPopupMenu* Canvas::genCanvasPopup()
923
 
      {
924
 
      if (canvasTools == 0)
925
 
            return 0;
926
 
      QPopupMenu* canvasPopup = new QPopupMenu(this);
927
 
 
928
 
      for (unsigned i = 0; i < 9; ++i) {
929
 
            if ((canvasTools & (1 << i))==0)
930
 
                  continue;
931
 
            canvasPopup->insertItem(**toolList[i].icon, tr(toolList[i].tip), 1<<i);
932
 
            }
933
 
      canvasPopup->setActiveItem(0);
934
 
      return canvasPopup;
935
 
      }
936
 
 
937
 
//---------------------------------------------------------
938
 
//   canvasPopup
939
 
//---------------------------------------------------------
940
 
 
941
 
void Canvas::canvasPopup(int n)
942
 
      {
943
 
      setTool(n);
944
 
      emit toolChanged(n);
945
 
      }
946
 
 
947