annotate widgets/Pane.cpp @ 20:1deb5f87a18c

* Add getXForFrame / getFrameForX in preference to using the zoom level everywhere
author Chris Cannam
date Mon, 30 Jan 2006 13:19:42 +0000
parents 46d8f5add6f0
children 3a506d25d95a
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@5 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 #include "widgets/Pane.h"
Chris@0 11 #include "base/Layer.h"
Chris@0 12 #include "base/Model.h"
Chris@0 13 #include "base/ZoomConstraint.h"
Chris@0 14 #include "base/RealTime.h"
Chris@0 15 #include "base/Profiler.h"
Chris@13 16 #include "base/ViewManager.h"
Chris@0 17
Chris@0 18 #include <QPaintEvent>
Chris@0 19 #include <QPainter>
Chris@0 20 #include <iostream>
Chris@0 21 #include <cmath>
Chris@0 22
Chris@0 23 using std::cerr;
Chris@0 24 using std::endl;
Chris@0 25
Chris@0 26 Pane::Pane(QWidget *w) :
Chris@0 27 View(w, true),
Chris@0 28 m_identifyFeatures(false),
Chris@0 29 m_clickedInRange(false),
Chris@0 30 m_shiftPressed(false),
Chris@13 31 m_ctrlPressed(false),
Chris@17 32 m_navigating(false),
Chris@17 33 m_resizing(false),
Chris@0 34 m_centreLineVisible(true)
Chris@0 35 {
Chris@0 36 setObjectName("Pane");
Chris@0 37 setMouseTracking(true);
Chris@0 38 }
Chris@0 39
Chris@0 40 bool
Chris@0 41 Pane::shouldIlluminateLocalFeatures(const Layer *layer, QPoint &pos)
Chris@0 42 {
Chris@0 43 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@0 44 --vi;
Chris@0 45 if (layer != *vi) return false;
Chris@0 46 pos = m_identifyPoint;
Chris@0 47 return m_identifyFeatures;
Chris@0 48 }
Chris@0 49
Chris@0 50 return false;
Chris@0 51 }
Chris@0 52
Chris@0 53 void
Chris@0 54 Pane::setCentreLineVisible(bool visible)
Chris@0 55 {
Chris@0 56 m_centreLineVisible = visible;
Chris@0 57 update();
Chris@0 58 }
Chris@0 59
Chris@0 60 void
Chris@0 61 Pane::paintEvent(QPaintEvent *e)
Chris@0 62 {
Chris@0 63 QPainter paint;
Chris@0 64
Chris@0 65 QRect r(rect());
Chris@0 66
Chris@0 67 if (e) {
Chris@0 68 r = e->rect();
Chris@0 69 }
Chris@0 70 /*
Chris@0 71 paint.begin(this);
Chris@0 72 paint.setClipRect(r);
Chris@0 73
Chris@0 74 if (hasLightBackground()) {
Chris@0 75 paint.setPen(Qt::white);
Chris@0 76 paint.setBrush(Qt::white);
Chris@0 77 } else {
Chris@0 78 paint.setPen(Qt::black);
Chris@0 79 paint.setBrush(Qt::black);
Chris@0 80 }
Chris@0 81 paint.drawRect(r);
Chris@0 82
Chris@0 83 paint.end();
Chris@0 84 */
Chris@0 85 View::paintEvent(e);
Chris@0 86
Chris@0 87 paint.begin(this);
Chris@0 88
Chris@0 89 if (e) {
Chris@0 90 paint.setClipRect(r);
Chris@0 91 }
Chris@0 92
Chris@0 93 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@0 94 --vi;
Chris@0 95
Chris@0 96 int sw = (*vi)->getVerticalScaleWidth(paint);
Chris@0 97
Chris@0 98 if (sw > 0 && r.left() < sw) {
Chris@0 99
Chris@0 100 // Profiler profiler("Pane::paintEvent - painting vertical scale", true);
Chris@0 101
Chris@0 102 // std::cerr << "Pane::paintEvent: calling paint.save() in vertical scale block" << std::endl;
Chris@0 103 paint.save();
Chris@0 104
Chris@0 105 paint.setPen(Qt::black);
Chris@0 106 paint.setBrush(Qt::white);
Chris@0 107 paint.drawRect(0, 0, sw, height());
Chris@0 108
Chris@0 109 paint.setBrush(Qt::NoBrush);
Chris@0 110 (*vi)->paintVerticalScale(paint, QRect(0, 0, sw, height()));
Chris@0 111
Chris@0 112 paint.restore();
Chris@0 113 }
Chris@0 114
Chris@0 115 if (m_identifyFeatures) {
Chris@0 116 QRect descRect = (*vi)->getFeatureDescriptionRect(paint,
Chris@0 117 m_identifyPoint);
Chris@0 118 if (descRect.width() > 0 && descRect.height() > 0 &&
Chris@0 119 r.left() + r.width() >= width() - descRect.width() &&
Chris@0 120 r.top() < descRect.height()) {
Chris@0 121
Chris@0 122 // Profiler profiler("Pane::paintEvent - painting local feature description", true);
Chris@0 123
Chris@0 124 // std::cerr << "Pane::paintEvent: calling paint.save() in feature description block" << std::endl;
Chris@0 125 paint.save();
Chris@0 126
Chris@0 127 paint.setPen(Qt::black);
Chris@0 128 paint.setBrush(Qt::white);
Chris@0 129
Chris@0 130 QRect rect(width() - descRect.width() - 1, 0,
Chris@0 131 descRect.width(), descRect.height());
Chris@0 132
Chris@0 133 paint.drawRect(rect);
Chris@0 134
Chris@0 135 paint.setBrush(Qt::NoBrush);
Chris@0 136 (*vi)->paintLocalFeatureDescription(paint, rect, m_identifyPoint);
Chris@0 137
Chris@0 138 paint.restore();
Chris@0 139 }
Chris@0 140 }
Chris@0 141
Chris@0 142 break;
Chris@0 143 }
Chris@0 144
Chris@0 145 if (m_centreLineVisible) {
Chris@0 146
Chris@0 147 if (hasLightBackground()) {
Chris@0 148 paint.setPen(QColor(50, 50, 50));
Chris@0 149 } else {
Chris@0 150 paint.setPen(QColor(200, 200, 200));
Chris@0 151 }
Chris@0 152 paint.setBrush(Qt::NoBrush);
Chris@0 153 paint.drawLine(width() / 2, 0, width() / 2, height() - 1);
Chris@0 154
Chris@0 155 // QFont font(paint.font());
Chris@0 156 // font.setBold(true);
Chris@0 157 // paint.setFont(font);
Chris@0 158
Chris@0 159 int sampleRate = getModelsSampleRate();
Chris@0 160 int y = height() - paint.fontMetrics().height()
Chris@0 161 + paint.fontMetrics().ascent() - 6;
Chris@0 162
Chris@0 163 LayerList::iterator vi = m_layers.end();
Chris@0 164
Chris@0 165 if (vi != m_layers.begin()) {
Chris@0 166
Chris@0 167 switch ((*--vi)->getPreferredFrameCountPosition()) {
Chris@0 168
Chris@0 169 case Layer::PositionTop:
Chris@0 170 y = paint.fontMetrics().ascent() + 6;
Chris@0 171 break;
Chris@0 172
Chris@0 173 case Layer::PositionMiddle:
Chris@0 174 y = (height() - paint.fontMetrics().height()) / 2
Chris@0 175 + paint.fontMetrics().ascent();
Chris@0 176 break;
Chris@0 177
Chris@0 178 case Layer::PositionBottom:
Chris@0 179 // y already set correctly
Chris@0 180 break;
Chris@0 181 }
Chris@0 182 }
Chris@0 183
Chris@0 184 if (sampleRate) {
Chris@0 185
Chris@0 186 QString text(QString::fromStdString
Chris@0 187 (RealTime::frame2RealTime
Chris@0 188 (m_centreFrame, sampleRate).toText(true)));
Chris@0 189
Chris@0 190 int tw = paint.fontMetrics().width(text);
Chris@0 191 int x = width()/2 - 4 - tw;
Chris@0 192
Chris@0 193 if (hasLightBackground()) {
Chris@0 194 paint.setPen(palette().background().color());
Chris@0 195 for (int dx = -1; dx <= 1; ++dx) {
Chris@0 196 for (int dy = -1; dy <= 1; ++dy) {
Chris@0 197 if ((dx && dy) || !(dx || dy)) continue;
Chris@0 198 paint.drawText(x + dx, y + dy, text);
Chris@0 199 }
Chris@0 200 }
Chris@0 201 paint.setPen(QColor(50, 50, 50));
Chris@0 202 } else {
Chris@0 203 paint.setPen(QColor(200, 200, 200));
Chris@0 204 }
Chris@0 205
Chris@0 206 paint.drawText(x, y, text);
Chris@0 207 }
Chris@0 208
Chris@0 209 QString text = QString("%1").arg(m_centreFrame);
Chris@0 210
Chris@0 211 int tw = paint.fontMetrics().width(text);
Chris@0 212 int x = width()/2 + 4;
Chris@0 213
Chris@0 214 if (hasLightBackground()) {
Chris@0 215 paint.setPen(palette().background().color());
Chris@0 216 for (int dx = -1; dx <= 1; ++dx) {
Chris@0 217 for (int dy = -1; dy <= 1; ++dy) {
Chris@0 218 if ((dx && dy) || !(dx || dy)) continue;
Chris@0 219 paint.drawText(x + dx, y + dy, text);
Chris@0 220 }
Chris@0 221 }
Chris@0 222 paint.setPen(QColor(50, 50, 50));
Chris@0 223 } else {
Chris@0 224 paint.setPen(QColor(200, 200, 200));
Chris@0 225 }
Chris@0 226 paint.drawText(x, y, text);
Chris@0 227 }
Chris@0 228
Chris@0 229 if (m_clickedInRange && m_shiftPressed) {
Chris@19 230 if (m_manager && (m_manager->getToolMode() == ViewManager::NavigateMode)) {
Chris@19 231 //!!! be nice if this looked a bit more in keeping with the
Chris@19 232 //selection block
Chris@19 233 paint.setPen(Qt::blue);
Chris@19 234 paint.drawRect(m_clickPos.x(), m_clickPos.y(),
Chris@19 235 m_mousePos.x() - m_clickPos.x(),
Chris@19 236 m_mousePos.y() - m_clickPos.y());
Chris@19 237 }
Chris@0 238 }
Chris@0 239
Chris@0 240 paint.end();
Chris@0 241 }
Chris@0 242
Chris@17 243 Selection
Chris@17 244 Pane::getSelectionAt(int x, bool &closeToLeftEdge, bool &closeToRightEdge)
Chris@17 245 {
Chris@17 246 closeToLeftEdge = closeToRightEdge = false;
Chris@17 247
Chris@17 248 if (!m_manager) return Selection();
Chris@17 249
Chris@20 250 long testFrame = getFrameForX(x - 5);
Chris@17 251 if (testFrame < 0) {
Chris@20 252 testFrame = getFrameForX(x);
Chris@17 253 if (testFrame < 0) return Selection();
Chris@17 254 }
Chris@17 255
Chris@17 256 Selection selection = m_manager->getContainingSelection(testFrame, true);
Chris@17 257 if (selection.isEmpty()) return selection;
Chris@17 258
Chris@20 259 int lx = getXForFrame(selection.getStartFrame());
Chris@20 260 int rx = getXForFrame(selection.getEndFrame());
Chris@17 261
Chris@17 262 int fuzz = 2;
Chris@17 263 if (x < lx - fuzz || x > rx + fuzz) return Selection();
Chris@17 264
Chris@17 265 int width = rx - lx;
Chris@17 266 fuzz = 3;
Chris@17 267 if (width < 12) fuzz = width / 4;
Chris@17 268 if (fuzz < 1) fuzz = 1;
Chris@17 269
Chris@17 270 if (x < lx + fuzz) closeToLeftEdge = true;
Chris@17 271 if (x > rx - fuzz) closeToRightEdge = true;
Chris@17 272
Chris@17 273 return selection;
Chris@17 274 }
Chris@17 275
Chris@0 276 void
Chris@0 277 Pane::mousePressEvent(QMouseEvent *e)
Chris@0 278 {
Chris@0 279 m_clickPos = e->pos();
Chris@0 280 m_clickedInRange = true;
Chris@0 281 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@13 282 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@13 283
Chris@13 284 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 285 if (m_manager) mode = m_manager->getToolMode();
Chris@13 286
Chris@17 287 m_navigating = false;
Chris@13 288
Chris@17 289 if (mode == ViewManager::NavigateMode || (e->buttons() & Qt::MidButton)) {
Chris@17 290
Chris@17 291 if (mode != ViewManager::NavigateMode) {
Chris@17 292 setCursor(Qt::PointingHandCursor);
Chris@17 293 }
Chris@17 294
Chris@17 295 m_navigating = true;
Chris@13 296 m_dragCentreFrame = m_centreFrame;
Chris@13 297
Chris@13 298 } else if (mode == ViewManager::SelectMode) {
Chris@13 299
Chris@17 300 bool closeToLeft = false, closeToRight = false;
Chris@17 301 Selection selection = getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@17 302
Chris@17 303 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@17 304
Chris@17 305 m_manager->removeSelection(selection);
Chris@17 306
Chris@17 307 if (closeToLeft) {
Chris@17 308 m_selectionStartFrame = selection.getEndFrame();
Chris@17 309 } else {
Chris@17 310 m_selectionStartFrame = selection.getStartFrame();
Chris@17 311 }
Chris@17 312
Chris@17 313 m_manager->setInProgressSelection(selection, false);
Chris@17 314 m_resizing = true;
Chris@13 315
Chris@17 316 } else {
Chris@17 317
Chris@20 318 int mouseFrame = getFrameForX(e->x());
Chris@17 319 size_t resolution = 1;
Chris@17 320 int snapFrame = mouseFrame;
Chris@17 321
Chris@17 322 Layer *layer = getSelectedLayer();
Chris@17 323 if (layer) {
Chris@17 324 snapFrame = layer->getNearestFeatureFrame(mouseFrame, resolution,
Chris@17 325 false);
Chris@17 326 }
Chris@17 327
Chris@17 328 if (snapFrame < 0) snapFrame = 0;
Chris@17 329 m_selectionStartFrame = snapFrame;
Chris@17 330 if (m_manager) {
Chris@17 331 m_manager->setInProgressSelection(Selection(snapFrame,
Chris@17 332 snapFrame + resolution),
Chris@17 333 !m_ctrlPressed);
Chris@17 334 }
Chris@17 335
Chris@17 336 m_resizing = false;
Chris@17 337 }
Chris@17 338
Chris@17 339 update();
Chris@17 340
Chris@17 341 } else if (mode == ViewManager::DrawMode) {
Chris@17 342
Chris@13 343 Layer *layer = getSelectedLayer();
Chris@13 344 if (layer) {
Chris@17 345 layer->drawStart(e);
Chris@13 346 }
Chris@18 347
Chris@18 348 } else if (mode == ViewManager::EditMode) {
Chris@18 349
Chris@18 350 Layer *layer = getSelectedLayer();
Chris@18 351 if (layer) {
Chris@18 352 layer->editStart(e);
Chris@18 353 }
Chris@13 354 }
Chris@0 355
Chris@0 356 emit paneInteractedWith();
Chris@0 357 }
Chris@0 358
Chris@0 359 void
Chris@0 360 Pane::mouseReleaseEvent(QMouseEvent *e)
Chris@0 361 {
Chris@13 362 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 363 if (m_manager) mode = m_manager->getToolMode();
Chris@13 364
Chris@0 365 if (m_clickedInRange) {
Chris@0 366 mouseMoveEvent(e);
Chris@0 367 }
Chris@0 368
Chris@17 369 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@17 370
Chris@17 371 m_navigating = false;
Chris@17 372
Chris@17 373 if (mode != ViewManager::NavigateMode) {
Chris@17 374 // restore cursor
Chris@17 375 toolModeChanged();
Chris@17 376 }
Chris@0 377
Chris@13 378 if (m_shiftPressed) {
Chris@0 379
Chris@13 380 int x0 = std::min(m_clickPos.x(), m_mousePos.x());
Chris@13 381 int x1 = std::max(m_clickPos.x(), m_mousePos.x());
Chris@13 382 int w = x1 - x0;
Chris@13 383
Chris@20 384 long newStartFrame = getFrameForX(x0);
Chris@13 385
Chris@20 386 long visibleFrames = getEndFrame() - getStartFrame();
Chris@20 387 if (newStartFrame <= -visibleFrames) {
Chris@20 388 newStartFrame = -visibleFrames + 1;
Chris@13 389 }
Chris@13 390
Chris@13 391 if (newStartFrame >= long(getModelsEndFrame())) {
Chris@13 392 newStartFrame = getModelsEndFrame() - 1;
Chris@13 393 }
Chris@13 394
Chris@13 395 float ratio = float(w) / float(width());
Chris@13 396 // std::cerr << "ratio: " << ratio << std::endl;
Chris@13 397 size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio);
Chris@13 398 if (newZoomLevel < 1) newZoomLevel = 1;
Chris@13 399
Chris@13 400 // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl;
Chris@13 401 setZoomLevel(getZoomConstraintBlockSize(newZoomLevel));
Chris@13 402 setStartFrame(newStartFrame);
Chris@13 403
Chris@13 404 //cerr << "mouseReleaseEvent: start frame now " << m_startFrame << endl;
Chris@13 405 // update();
Chris@0 406 }
Chris@0 407
Chris@13 408 } else if (mode == ViewManager::SelectMode) {
Chris@13 409
Chris@13 410 if (m_manager && m_manager->haveInProgressSelection()) {
Chris@13 411
Chris@13 412 bool exclusive;
Chris@13 413 Selection selection = m_manager->getInProgressSelection(exclusive);
Chris@13 414
Chris@13 415 if (selection.getEndFrame() < selection.getStartFrame() + 2) {
Chris@13 416 selection = Selection();
Chris@13 417 }
Chris@13 418
Chris@13 419 m_manager->clearInProgressSelection();
Chris@13 420
Chris@13 421 if (exclusive) {
Chris@13 422 m_manager->setSelection(selection);
Chris@13 423 } else {
Chris@13 424 m_manager->addSelection(selection);
Chris@13 425 }
Chris@0 426 }
Chris@13 427
Chris@13 428 update();
Chris@17 429
Chris@17 430 } else if (mode == ViewManager::DrawMode) {
Chris@17 431
Chris@17 432 Layer *layer = getSelectedLayer();
Chris@17 433 if (layer) {
Chris@17 434 layer->drawEnd(e);
Chris@17 435 update();
Chris@17 436 }
Chris@18 437
Chris@18 438 } else if (mode == ViewManager::EditMode) {
Chris@18 439
Chris@18 440 Layer *layer = getSelectedLayer();
Chris@18 441 if (layer) {
Chris@18 442 layer->editEnd(e);
Chris@18 443 update();
Chris@18 444 }
Chris@17 445 }
Chris@0 446
Chris@0 447 m_clickedInRange = false;
Chris@0 448
Chris@0 449 emit paneInteractedWith();
Chris@0 450 }
Chris@0 451
Chris@0 452 void
Chris@0 453 Pane::mouseMoveEvent(QMouseEvent *e)
Chris@0 454 {
Chris@13 455 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 456 if (m_manager) mode = m_manager->getToolMode();
Chris@13 457
Chris@0 458 if (!m_clickedInRange) {
Chris@0 459
Chris@17 460 if (mode == ViewManager::SelectMode) {
Chris@17 461 bool closeToLeft = false, closeToRight = false;
Chris@17 462 getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@17 463 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@17 464 setCursor(Qt::SizeHorCursor);
Chris@17 465 } else {
Chris@17 466 setCursor(Qt::ArrowCursor);
Chris@17 467 }
Chris@17 468 }
Chris@0 469
Chris@17 470 if (mode != ViewManager::DrawMode) {
Chris@0 471
Chris@17 472 bool previouslyIdentifying = m_identifyFeatures;
Chris@17 473 QPoint prevPoint = m_identifyPoint;
Chris@0 474
Chris@17 475 m_identifyFeatures = true;
Chris@17 476 m_identifyPoint = e->pos();
Chris@17 477
Chris@17 478 if (m_identifyFeatures != previouslyIdentifying ||
Chris@17 479 m_identifyPoint != prevPoint) {
Chris@17 480 update();
Chris@17 481 }
Chris@0 482 }
Chris@0 483
Chris@13 484 return;
Chris@13 485 }
Chris@0 486
Chris@17 487 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@0 488
Chris@13 489 if (m_shiftPressed) {
Chris@0 490
Chris@13 491 m_mousePos = e->pos();
Chris@13 492 update();
Chris@0 493
Chris@0 494 } else {
Chris@13 495
Chris@20 496 long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x());
Chris@20 497
Chris@13 498 size_t newCentreFrame = m_dragCentreFrame;
Chris@13 499
Chris@13 500 if (frameOff < 0) {
Chris@13 501 newCentreFrame -= frameOff;
Chris@13 502 } else if (newCentreFrame >= size_t(frameOff)) {
Chris@13 503 newCentreFrame -= frameOff;
Chris@13 504 } else {
Chris@13 505 newCentreFrame = 0;
Chris@13 506 }
Chris@13 507
Chris@13 508 if (newCentreFrame >= getModelsEndFrame()) {
Chris@13 509 newCentreFrame = getModelsEndFrame();
Chris@13 510 if (newCentreFrame > 0) --newCentreFrame;
Chris@13 511 }
Chris@20 512
Chris@20 513 if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) {
Chris@13 514 setCentreFrame(newCentreFrame);
Chris@13 515 }
Chris@0 516 }
Chris@0 517
Chris@13 518 } else if (mode == ViewManager::SelectMode) {
Chris@13 519
Chris@20 520 int mouseFrame = getFrameForX(e->x());
Chris@13 521 size_t resolution = 1;
Chris@13 522 int snapFrameLeft = mouseFrame;
Chris@13 523 int snapFrameRight = mouseFrame;
Chris@13 524
Chris@13 525 Layer *layer = getSelectedLayer();
Chris@13 526 if (layer) {
Chris@13 527 snapFrameLeft = layer->getNearestFeatureFrame(mouseFrame, resolution,
Chris@13 528 false);
Chris@13 529 snapFrameRight = layer->getNearestFeatureFrame(mouseFrame, resolution,
Chris@13 530 true);
Chris@13 531 }
Chris@13 532
Chris@13 533 if (snapFrameLeft < 0) snapFrameLeft = 0;
Chris@13 534 if (snapFrameRight < 0) snapFrameRight = 0;
Chris@13 535
Chris@13 536 size_t min, max;
Chris@13 537
Chris@13 538 if (m_selectionStartFrame > snapFrameLeft) {
Chris@13 539 min = snapFrameLeft;
Chris@13 540 max = m_selectionStartFrame;
Chris@13 541 } else if (snapFrameRight > m_selectionStartFrame) {
Chris@13 542 min = m_selectionStartFrame;
Chris@13 543 max = snapFrameRight;
Chris@13 544 } else {
Chris@13 545 min = snapFrameLeft;
Chris@13 546 max = snapFrameRight;
Chris@0 547 }
Chris@0 548
Chris@13 549 if (m_manager) {
Chris@13 550 m_manager->setInProgressSelection(Selection(min, max),
Chris@17 551 !m_resizing && !m_ctrlPressed);
Chris@0 552 }
Chris@15 553
Chris@15 554 bool doScroll = false;
Chris@15 555 if (!m_manager) doScroll = true;
Chris@15 556 if (!m_manager->isPlaying()) doScroll = true;
Chris@15 557 if (m_followPlay != PlaybackScrollContinuous) doScroll = true;
Chris@15 558
Chris@15 559 if (doScroll) {
Chris@13 560 int offset = mouseFrame - getStartFrame();
Chris@13 561 int available = getEndFrame() - getStartFrame();
Chris@15 562 if (offset >= available * 0.95) {
Chris@15 563 int move = int(offset - available * 0.95) + 1;
Chris@14 564 setCentreFrame(m_centreFrame + move);
Chris@15 565 } else if (offset <= available * 0.10) {
Chris@15 566 int move = int(available * 0.10 - offset) + 1;
Chris@14 567 if (m_centreFrame > move) {
Chris@14 568 setCentreFrame(m_centreFrame - move);
Chris@14 569 } else {
Chris@14 570 setCentreFrame(0);
Chris@14 571 }
Chris@13 572 }
Chris@13 573 }
Chris@13 574
Chris@13 575 update();
Chris@17 576
Chris@17 577 } else if (mode == ViewManager::DrawMode) {
Chris@17 578
Chris@17 579 Layer *layer = getSelectedLayer();
Chris@17 580 if (layer) {
Chris@17 581 layer->drawDrag(e);
Chris@19 582 update();
Chris@17 583 }
Chris@18 584
Chris@18 585 } else if (mode == ViewManager::EditMode) {
Chris@18 586
Chris@18 587 Layer *layer = getSelectedLayer();
Chris@18 588 if (layer) {
Chris@18 589 layer->editDrag(e);
Chris@19 590 update();
Chris@18 591 }
Chris@0 592 }
Chris@0 593 }
Chris@0 594
Chris@0 595 void
Chris@0 596 Pane::mouseDoubleClickEvent(QMouseEvent *e)
Chris@0 597 {
Chris@0 598 std::cerr << "mouseDoubleClickEvent" << std::endl;
Chris@0 599 }
Chris@0 600
Chris@0 601 void
Chris@0 602 Pane::leaveEvent(QEvent *)
Chris@0 603 {
Chris@0 604 bool previouslyIdentifying = m_identifyFeatures;
Chris@0 605 m_identifyFeatures = false;
Chris@0 606 if (previouslyIdentifying) update();
Chris@0 607 }
Chris@0 608
Chris@0 609 void
Chris@0 610 Pane::wheelEvent(QWheelEvent *e)
Chris@0 611 {
Chris@0 612 //std::cerr << "wheelEvent, delta " << e->delta() << std::endl;
Chris@0 613
Chris@0 614 int count = e->delta();
Chris@0 615
Chris@0 616 if (count > 0) {
Chris@0 617 if (count >= 120) count /= 120;
Chris@0 618 else count = 1;
Chris@0 619 }
Chris@0 620
Chris@0 621 if (count < 0) {
Chris@0 622 if (count <= -120) count /= 120;
Chris@0 623 else count = -1;
Chris@0 624 }
Chris@17 625
Chris@17 626 if (e->modifiers() & Qt::ControlModifier) {
Chris@17 627
Chris@20 628 // Scroll left or right, rapidly
Chris@20 629
Chris@17 630 if (getStartFrame() < 0 &&
Chris@17 631 getEndFrame() >= getModelsEndFrame()) return;
Chris@17 632
Chris@17 633 long delta = ((width() / 2) * count * m_zoomLevel);
Chris@17 634
Chris@17 635 if (int(m_centreFrame) < delta) {
Chris@17 636 setCentreFrame(0);
Chris@17 637 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@17 638 setCentreFrame(getModelsEndFrame());
Chris@17 639 } else {
Chris@17 640 setCentreFrame(m_centreFrame - delta);
Chris@17 641 }
Chris@17 642
Chris@17 643 } else {
Chris@17 644
Chris@20 645 // Zoom in or out
Chris@20 646
Chris@17 647 int newZoomLevel = m_zoomLevel;
Chris@0 648
Chris@17 649 while (count > 0) {
Chris@17 650 if (newZoomLevel <= 2) {
Chris@17 651 newZoomLevel = 1;
Chris@17 652 break;
Chris@17 653 }
Chris@17 654 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@17 655 ZoomConstraint::RoundDown);
Chris@17 656 --count;
Chris@0 657 }
Chris@17 658
Chris@17 659 while (count < 0) {
Chris@17 660 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@17 661 ZoomConstraint::RoundUp);
Chris@17 662 ++count;
Chris@17 663 }
Chris@17 664
Chris@17 665 if (newZoomLevel != m_zoomLevel) {
Chris@17 666 setZoomLevel(newZoomLevel);
Chris@17 667 }
Chris@0 668 }
Chris@0 669
Chris@0 670 emit paneInteractedWith();
Chris@0 671 }
Chris@8 672
Chris@13 673 void
Chris@13 674 Pane::toolModeChanged()
Chris@13 675 {
Chris@13 676 ViewManager::ToolMode mode = m_manager->getToolMode();
Chris@13 677 std::cerr << "Pane::toolModeChanged(" << mode << ")" << std::endl;
Chris@13 678
Chris@13 679 switch (mode) {
Chris@13 680
Chris@13 681 case ViewManager::NavigateMode:
Chris@13 682 setCursor(Qt::PointingHandCursor);
Chris@13 683 break;
Chris@13 684
Chris@13 685 case ViewManager::SelectMode:
Chris@13 686 setCursor(Qt::ArrowCursor);
Chris@13 687 break;
Chris@13 688
Chris@13 689 case ViewManager::EditMode:
Chris@19 690 setCursor(Qt::UpArrowCursor);
Chris@13 691 break;
Chris@13 692
Chris@13 693 case ViewManager::DrawMode:
Chris@13 694 setCursor(Qt::CrossCursor);
Chris@13 695 break;
Chris@13 696
Chris@13 697 case ViewManager::TextMode:
Chris@13 698 setCursor(Qt::IBeamCursor);
Chris@13 699 break;
Chris@13 700 }
Chris@13 701 }
Chris@13 702
Chris@8 703 QString
Chris@8 704 Pane::toXmlString(QString indent, QString extraAttributes) const
Chris@8 705 {
Chris@8 706 return View::toXmlString
Chris@8 707 (indent,
Chris@8 708 QString("type=\"pane\" centreLineVisible=\"%1\" %2")
Chris@8 709 .arg(m_centreLineVisible).arg(extraAttributes));
Chris@8 710 }
Chris@8 711
Chris@0 712
Chris@0 713 #ifdef INCLUDE_MOCFILES
Chris@0 714 #include "Pane.moc.cpp"
Chris@0 715 #endif
Chris@0 716