annotate widgets/Pane.cpp @ 26:94381052a6c9

* Add natty segmentation display to time-value layer. Need to do the same for time-instants layer. * Make sure dense 3D model bin names are saved and restored properly. * Fix to chromagram normalization
author Chris Cannam
date Thu, 02 Feb 2006 17:31:08 +0000
parents dcdb21b62dbb
children 38fe0ea9e46e
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@25 116
Chris@25 117 QPoint pos = m_identifyPoint;
Chris@25 118 QString desc = (*vi)->getFeatureDescription(pos);
Chris@25 119
Chris@25 120 if (desc != "") {
Chris@25 121
Chris@25 122 paint.save();
Chris@25 123
Chris@25 124 int tabStop =
Chris@25 125 paint.fontMetrics().width(tr("Some lengthy prefix:"));
Chris@25 126
Chris@25 127 QRect boundingRect =
Chris@25 128 paint.fontMetrics().boundingRect
Chris@25 129 (rect(),
Chris@25 130 Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs,
Chris@25 131 desc, tabStop);
Chris@25 132
Chris@25 133 if (hasLightBackground()) {
Chris@25 134 paint.setPen(Qt::NoPen);
Chris@25 135 paint.setBrush(QColor(250, 250, 250, 200));
Chris@25 136 } else {
Chris@25 137 paint.setPen(Qt::NoPen);
Chris@25 138 paint.setBrush(QColor(50, 50, 50, 200));
Chris@25 139 }
Chris@25 140
Chris@25 141 int extra = paint.fontMetrics().descent();
Chris@25 142 paint.drawRect(width() - boundingRect.width() - 10 - extra,
Chris@25 143 10 - extra,
Chris@25 144 boundingRect.width() + 2 * extra,
Chris@25 145 boundingRect.height() + extra);
Chris@25 146
Chris@25 147 if (hasLightBackground()) {
Chris@25 148 paint.setPen(QColor(150, 20, 0));
Chris@25 149 } else {
Chris@25 150 paint.setPen(QColor(255, 150, 100));
Chris@25 151 }
Chris@25 152
Chris@25 153 QTextOption option;
Chris@25 154 option.setWrapMode(QTextOption::NoWrap);
Chris@25 155 option.setAlignment(Qt::AlignRight | Qt::AlignTop);
Chris@25 156 option.setTabStop(tabStop);
Chris@25 157 paint.drawText(QRectF(width() - boundingRect.width() - 10, 10,
Chris@25 158 boundingRect.width(),
Chris@25 159 boundingRect.height()),
Chris@25 160 desc,
Chris@25 161 option);
Chris@25 162
Chris@25 163 paint.restore();
Chris@25 164 }
Chris@25 165
Chris@25 166 /*!!!
Chris@0 167 QRect descRect = (*vi)->getFeatureDescriptionRect(paint,
Chris@0 168 m_identifyPoint);
Chris@0 169 if (descRect.width() > 0 && descRect.height() > 0 &&
Chris@0 170 r.left() + r.width() >= width() - descRect.width() &&
Chris@0 171 r.top() < descRect.height()) {
Chris@0 172
Chris@0 173 // Profiler profiler("Pane::paintEvent - painting local feature description", true);
Chris@0 174
Chris@0 175 // std::cerr << "Pane::paintEvent: calling paint.save() in feature description block" << std::endl;
Chris@0 176 paint.save();
Chris@0 177
Chris@0 178 paint.setPen(Qt::black);
Chris@0 179 paint.setBrush(Qt::white);
Chris@0 180
Chris@0 181 QRect rect(width() - descRect.width() - 1, 0,
Chris@0 182 descRect.width(), descRect.height());
Chris@0 183
Chris@0 184 paint.drawRect(rect);
Chris@0 185
Chris@0 186 paint.setBrush(Qt::NoBrush);
Chris@0 187 (*vi)->paintLocalFeatureDescription(paint, rect, m_identifyPoint);
Chris@0 188
Chris@0 189 paint.restore();
Chris@0 190 }
Chris@25 191
Chris@25 192 */
Chris@0 193 }
Chris@0 194
Chris@0 195 break;
Chris@0 196 }
Chris@0 197
Chris@0 198 if (m_centreLineVisible) {
Chris@0 199
Chris@0 200 if (hasLightBackground()) {
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 paint.setBrush(Qt::NoBrush);
Chris@0 206 paint.drawLine(width() / 2, 0, width() / 2, height() - 1);
Chris@0 207
Chris@0 208 // QFont font(paint.font());
Chris@0 209 // font.setBold(true);
Chris@0 210 // paint.setFont(font);
Chris@0 211
Chris@0 212 int sampleRate = getModelsSampleRate();
Chris@0 213 int y = height() - paint.fontMetrics().height()
Chris@0 214 + paint.fontMetrics().ascent() - 6;
Chris@0 215
Chris@0 216 LayerList::iterator vi = m_layers.end();
Chris@0 217
Chris@0 218 if (vi != m_layers.begin()) {
Chris@0 219
Chris@0 220 switch ((*--vi)->getPreferredFrameCountPosition()) {
Chris@0 221
Chris@0 222 case Layer::PositionTop:
Chris@0 223 y = paint.fontMetrics().ascent() + 6;
Chris@0 224 break;
Chris@0 225
Chris@0 226 case Layer::PositionMiddle:
Chris@0 227 y = (height() - paint.fontMetrics().height()) / 2
Chris@0 228 + paint.fontMetrics().ascent();
Chris@0 229 break;
Chris@0 230
Chris@0 231 case Layer::PositionBottom:
Chris@0 232 // y already set correctly
Chris@0 233 break;
Chris@0 234 }
Chris@0 235 }
Chris@0 236
Chris@0 237 if (sampleRate) {
Chris@0 238
Chris@0 239 QString text(QString::fromStdString
Chris@0 240 (RealTime::frame2RealTime
Chris@0 241 (m_centreFrame, sampleRate).toText(true)));
Chris@0 242
Chris@0 243 int tw = paint.fontMetrics().width(text);
Chris@0 244 int x = width()/2 - 4 - tw;
Chris@0 245
Chris@0 246 if (hasLightBackground()) {
Chris@0 247 paint.setPen(palette().background().color());
Chris@0 248 for (int dx = -1; dx <= 1; ++dx) {
Chris@0 249 for (int dy = -1; dy <= 1; ++dy) {
Chris@0 250 if ((dx && dy) || !(dx || dy)) continue;
Chris@0 251 paint.drawText(x + dx, y + dy, text);
Chris@0 252 }
Chris@0 253 }
Chris@0 254 paint.setPen(QColor(50, 50, 50));
Chris@0 255 } else {
Chris@0 256 paint.setPen(QColor(200, 200, 200));
Chris@0 257 }
Chris@0 258
Chris@0 259 paint.drawText(x, y, text);
Chris@0 260 }
Chris@0 261
Chris@0 262 QString text = QString("%1").arg(m_centreFrame);
Chris@0 263
Chris@0 264 int tw = paint.fontMetrics().width(text);
Chris@0 265 int x = width()/2 + 4;
Chris@0 266
Chris@0 267 if (hasLightBackground()) {
Chris@0 268 paint.setPen(palette().background().color());
Chris@0 269 for (int dx = -1; dx <= 1; ++dx) {
Chris@0 270 for (int dy = -1; dy <= 1; ++dy) {
Chris@0 271 if ((dx && dy) || !(dx || dy)) continue;
Chris@0 272 paint.drawText(x + dx, y + dy, text);
Chris@0 273 }
Chris@0 274 }
Chris@0 275 paint.setPen(QColor(50, 50, 50));
Chris@0 276 } else {
Chris@0 277 paint.setPen(QColor(200, 200, 200));
Chris@0 278 }
Chris@0 279 paint.drawText(x, y, text);
Chris@0 280 }
Chris@0 281
Chris@0 282 if (m_clickedInRange && m_shiftPressed) {
Chris@19 283 if (m_manager && (m_manager->getToolMode() == ViewManager::NavigateMode)) {
Chris@19 284 //!!! be nice if this looked a bit more in keeping with the
Chris@19 285 //selection block
Chris@19 286 paint.setPen(Qt::blue);
Chris@19 287 paint.drawRect(m_clickPos.x(), m_clickPos.y(),
Chris@19 288 m_mousePos.x() - m_clickPos.x(),
Chris@19 289 m_mousePos.y() - m_clickPos.y());
Chris@19 290 }
Chris@0 291 }
Chris@0 292
Chris@0 293 paint.end();
Chris@0 294 }
Chris@0 295
Chris@17 296 Selection
Chris@17 297 Pane::getSelectionAt(int x, bool &closeToLeftEdge, bool &closeToRightEdge)
Chris@17 298 {
Chris@17 299 closeToLeftEdge = closeToRightEdge = false;
Chris@17 300
Chris@17 301 if (!m_manager) return Selection();
Chris@17 302
Chris@20 303 long testFrame = getFrameForX(x - 5);
Chris@17 304 if (testFrame < 0) {
Chris@20 305 testFrame = getFrameForX(x);
Chris@17 306 if (testFrame < 0) return Selection();
Chris@17 307 }
Chris@17 308
Chris@17 309 Selection selection = m_manager->getContainingSelection(testFrame, true);
Chris@17 310 if (selection.isEmpty()) return selection;
Chris@17 311
Chris@20 312 int lx = getXForFrame(selection.getStartFrame());
Chris@20 313 int rx = getXForFrame(selection.getEndFrame());
Chris@17 314
Chris@17 315 int fuzz = 2;
Chris@17 316 if (x < lx - fuzz || x > rx + fuzz) return Selection();
Chris@17 317
Chris@17 318 int width = rx - lx;
Chris@17 319 fuzz = 3;
Chris@17 320 if (width < 12) fuzz = width / 4;
Chris@17 321 if (fuzz < 1) fuzz = 1;
Chris@17 322
Chris@17 323 if (x < lx + fuzz) closeToLeftEdge = true;
Chris@17 324 if (x > rx - fuzz) closeToRightEdge = true;
Chris@17 325
Chris@17 326 return selection;
Chris@17 327 }
Chris@17 328
Chris@0 329 void
Chris@0 330 Pane::mousePressEvent(QMouseEvent *e)
Chris@0 331 {
Chris@0 332 m_clickPos = e->pos();
Chris@0 333 m_clickedInRange = true;
Chris@0 334 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@13 335 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@13 336
Chris@13 337 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 338 if (m_manager) mode = m_manager->getToolMode();
Chris@13 339
Chris@17 340 m_navigating = false;
Chris@13 341
Chris@17 342 if (mode == ViewManager::NavigateMode || (e->buttons() & Qt::MidButton)) {
Chris@17 343
Chris@17 344 if (mode != ViewManager::NavigateMode) {
Chris@17 345 setCursor(Qt::PointingHandCursor);
Chris@17 346 }
Chris@17 347
Chris@17 348 m_navigating = true;
Chris@13 349 m_dragCentreFrame = m_centreFrame;
Chris@13 350
Chris@13 351 } else if (mode == ViewManager::SelectMode) {
Chris@13 352
Chris@17 353 bool closeToLeft = false, closeToRight = false;
Chris@17 354 Selection selection = getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@17 355
Chris@17 356 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@17 357
Chris@17 358 m_manager->removeSelection(selection);
Chris@17 359
Chris@17 360 if (closeToLeft) {
Chris@17 361 m_selectionStartFrame = selection.getEndFrame();
Chris@17 362 } else {
Chris@17 363 m_selectionStartFrame = selection.getStartFrame();
Chris@17 364 }
Chris@17 365
Chris@17 366 m_manager->setInProgressSelection(selection, false);
Chris@17 367 m_resizing = true;
Chris@13 368
Chris@17 369 } else {
Chris@17 370
Chris@20 371 int mouseFrame = getFrameForX(e->x());
Chris@17 372 size_t resolution = 1;
Chris@17 373 int snapFrame = mouseFrame;
Chris@17 374
Chris@17 375 Layer *layer = getSelectedLayer();
Chris@17 376 if (layer) {
Chris@17 377 snapFrame = layer->getNearestFeatureFrame(mouseFrame, resolution,
Chris@17 378 false);
Chris@17 379 }
Chris@17 380
Chris@17 381 if (snapFrame < 0) snapFrame = 0;
Chris@17 382 m_selectionStartFrame = snapFrame;
Chris@17 383 if (m_manager) {
Chris@17 384 m_manager->setInProgressSelection(Selection(snapFrame,
Chris@17 385 snapFrame + resolution),
Chris@17 386 !m_ctrlPressed);
Chris@17 387 }
Chris@17 388
Chris@17 389 m_resizing = false;
Chris@17 390 }
Chris@17 391
Chris@17 392 update();
Chris@17 393
Chris@17 394 } else if (mode == ViewManager::DrawMode) {
Chris@17 395
Chris@13 396 Layer *layer = getSelectedLayer();
Chris@23 397 if (layer && layer->isLayerEditable()) {
Chris@17 398 layer->drawStart(e);
Chris@13 399 }
Chris@18 400
Chris@18 401 } else if (mode == ViewManager::EditMode) {
Chris@18 402
Chris@18 403 Layer *layer = getSelectedLayer();
Chris@23 404 if (layer && layer->isLayerEditable()) {
Chris@18 405 layer->editStart(e);
Chris@18 406 }
Chris@13 407 }
Chris@0 408
Chris@0 409 emit paneInteractedWith();
Chris@0 410 }
Chris@0 411
Chris@0 412 void
Chris@0 413 Pane::mouseReleaseEvent(QMouseEvent *e)
Chris@0 414 {
Chris@13 415 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 416 if (m_manager) mode = m_manager->getToolMode();
Chris@13 417
Chris@0 418 if (m_clickedInRange) {
Chris@0 419 mouseMoveEvent(e);
Chris@0 420 }
Chris@0 421
Chris@17 422 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@17 423
Chris@17 424 m_navigating = false;
Chris@17 425
Chris@17 426 if (mode != ViewManager::NavigateMode) {
Chris@17 427 // restore cursor
Chris@17 428 toolModeChanged();
Chris@17 429 }
Chris@0 430
Chris@13 431 if (m_shiftPressed) {
Chris@0 432
Chris@13 433 int x0 = std::min(m_clickPos.x(), m_mousePos.x());
Chris@13 434 int x1 = std::max(m_clickPos.x(), m_mousePos.x());
Chris@13 435 int w = x1 - x0;
Chris@13 436
Chris@20 437 long newStartFrame = getFrameForX(x0);
Chris@13 438
Chris@20 439 long visibleFrames = getEndFrame() - getStartFrame();
Chris@20 440 if (newStartFrame <= -visibleFrames) {
Chris@20 441 newStartFrame = -visibleFrames + 1;
Chris@13 442 }
Chris@13 443
Chris@13 444 if (newStartFrame >= long(getModelsEndFrame())) {
Chris@13 445 newStartFrame = getModelsEndFrame() - 1;
Chris@13 446 }
Chris@13 447
Chris@13 448 float ratio = float(w) / float(width());
Chris@13 449 // std::cerr << "ratio: " << ratio << std::endl;
Chris@13 450 size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio);
Chris@13 451 if (newZoomLevel < 1) newZoomLevel = 1;
Chris@13 452
Chris@13 453 // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl;
Chris@13 454 setZoomLevel(getZoomConstraintBlockSize(newZoomLevel));
Chris@13 455 setStartFrame(newStartFrame);
Chris@13 456
Chris@13 457 //cerr << "mouseReleaseEvent: start frame now " << m_startFrame << endl;
Chris@13 458 // update();
Chris@0 459 }
Chris@0 460
Chris@13 461 } else if (mode == ViewManager::SelectMode) {
Chris@13 462
Chris@13 463 if (m_manager && m_manager->haveInProgressSelection()) {
Chris@13 464
Chris@13 465 bool exclusive;
Chris@13 466 Selection selection = m_manager->getInProgressSelection(exclusive);
Chris@13 467
Chris@13 468 if (selection.getEndFrame() < selection.getStartFrame() + 2) {
Chris@13 469 selection = Selection();
Chris@13 470 }
Chris@13 471
Chris@13 472 m_manager->clearInProgressSelection();
Chris@13 473
Chris@13 474 if (exclusive) {
Chris@13 475 m_manager->setSelection(selection);
Chris@13 476 } else {
Chris@13 477 m_manager->addSelection(selection);
Chris@13 478 }
Chris@0 479 }
Chris@13 480
Chris@13 481 update();
Chris@17 482
Chris@17 483 } else if (mode == ViewManager::DrawMode) {
Chris@17 484
Chris@17 485 Layer *layer = getSelectedLayer();
Chris@23 486 if (layer && layer->isLayerEditable()) {
Chris@17 487 layer->drawEnd(e);
Chris@17 488 update();
Chris@17 489 }
Chris@18 490
Chris@18 491 } else if (mode == ViewManager::EditMode) {
Chris@18 492
Chris@18 493 Layer *layer = getSelectedLayer();
Chris@23 494 if (layer && layer->isLayerEditable()) {
Chris@18 495 layer->editEnd(e);
Chris@18 496 update();
Chris@18 497 }
Chris@17 498 }
Chris@0 499
Chris@0 500 m_clickedInRange = false;
Chris@0 501
Chris@0 502 emit paneInteractedWith();
Chris@0 503 }
Chris@0 504
Chris@0 505 void
Chris@0 506 Pane::mouseMoveEvent(QMouseEvent *e)
Chris@0 507 {
Chris@13 508 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 509 if (m_manager) mode = m_manager->getToolMode();
Chris@13 510
Chris@0 511 if (!m_clickedInRange) {
Chris@0 512
Chris@17 513 if (mode == ViewManager::SelectMode) {
Chris@17 514 bool closeToLeft = false, closeToRight = false;
Chris@17 515 getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@17 516 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@17 517 setCursor(Qt::SizeHorCursor);
Chris@17 518 } else {
Chris@17 519 setCursor(Qt::ArrowCursor);
Chris@17 520 }
Chris@17 521 }
Chris@0 522
Chris@17 523 if (mode != ViewManager::DrawMode) {
Chris@0 524
Chris@17 525 bool previouslyIdentifying = m_identifyFeatures;
Chris@17 526 QPoint prevPoint = m_identifyPoint;
Chris@0 527
Chris@17 528 m_identifyFeatures = true;
Chris@17 529 m_identifyPoint = e->pos();
Chris@17 530
Chris@17 531 if (m_identifyFeatures != previouslyIdentifying ||
Chris@17 532 m_identifyPoint != prevPoint) {
Chris@17 533 update();
Chris@17 534 }
Chris@0 535 }
Chris@0 536
Chris@13 537 return;
Chris@13 538 }
Chris@0 539
Chris@17 540 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@0 541
Chris@13 542 if (m_shiftPressed) {
Chris@0 543
Chris@13 544 m_mousePos = e->pos();
Chris@13 545 update();
Chris@0 546
Chris@0 547 } else {
Chris@13 548
Chris@20 549 long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x());
Chris@20 550
Chris@13 551 size_t newCentreFrame = m_dragCentreFrame;
Chris@13 552
Chris@13 553 if (frameOff < 0) {
Chris@13 554 newCentreFrame -= frameOff;
Chris@13 555 } else if (newCentreFrame >= size_t(frameOff)) {
Chris@13 556 newCentreFrame -= frameOff;
Chris@13 557 } else {
Chris@13 558 newCentreFrame = 0;
Chris@13 559 }
Chris@13 560
Chris@13 561 if (newCentreFrame >= getModelsEndFrame()) {
Chris@13 562 newCentreFrame = getModelsEndFrame();
Chris@13 563 if (newCentreFrame > 0) --newCentreFrame;
Chris@13 564 }
Chris@20 565
Chris@20 566 if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) {
Chris@13 567 setCentreFrame(newCentreFrame);
Chris@13 568 }
Chris@0 569 }
Chris@0 570
Chris@13 571 } else if (mode == ViewManager::SelectMode) {
Chris@13 572
Chris@20 573 int mouseFrame = getFrameForX(e->x());
Chris@13 574 size_t resolution = 1;
Chris@13 575 int snapFrameLeft = mouseFrame;
Chris@13 576 int snapFrameRight = mouseFrame;
Chris@13 577
Chris@13 578 Layer *layer = getSelectedLayer();
Chris@13 579 if (layer) {
Chris@13 580 snapFrameLeft = layer->getNearestFeatureFrame(mouseFrame, resolution,
Chris@13 581 false);
Chris@13 582 snapFrameRight = layer->getNearestFeatureFrame(mouseFrame, resolution,
Chris@13 583 true);
Chris@13 584 }
Chris@13 585
Chris@13 586 if (snapFrameLeft < 0) snapFrameLeft = 0;
Chris@13 587 if (snapFrameRight < 0) snapFrameRight = 0;
Chris@13 588
Chris@13 589 size_t min, max;
Chris@13 590
Chris@13 591 if (m_selectionStartFrame > snapFrameLeft) {
Chris@13 592 min = snapFrameLeft;
Chris@13 593 max = m_selectionStartFrame;
Chris@13 594 } else if (snapFrameRight > m_selectionStartFrame) {
Chris@13 595 min = m_selectionStartFrame;
Chris@13 596 max = snapFrameRight;
Chris@13 597 } else {
Chris@13 598 min = snapFrameLeft;
Chris@13 599 max = snapFrameRight;
Chris@0 600 }
Chris@0 601
Chris@13 602 if (m_manager) {
Chris@13 603 m_manager->setInProgressSelection(Selection(min, max),
Chris@17 604 !m_resizing && !m_ctrlPressed);
Chris@0 605 }
Chris@15 606
Chris@15 607 bool doScroll = false;
Chris@15 608 if (!m_manager) doScroll = true;
Chris@15 609 if (!m_manager->isPlaying()) doScroll = true;
Chris@15 610 if (m_followPlay != PlaybackScrollContinuous) doScroll = true;
Chris@15 611
Chris@15 612 if (doScroll) {
Chris@13 613 int offset = mouseFrame - getStartFrame();
Chris@13 614 int available = getEndFrame() - getStartFrame();
Chris@15 615 if (offset >= available * 0.95) {
Chris@15 616 int move = int(offset - available * 0.95) + 1;
Chris@14 617 setCentreFrame(m_centreFrame + move);
Chris@15 618 } else if (offset <= available * 0.10) {
Chris@15 619 int move = int(available * 0.10 - offset) + 1;
Chris@14 620 if (m_centreFrame > move) {
Chris@14 621 setCentreFrame(m_centreFrame - move);
Chris@14 622 } else {
Chris@14 623 setCentreFrame(0);
Chris@14 624 }
Chris@13 625 }
Chris@13 626 }
Chris@13 627
Chris@13 628 update();
Chris@17 629
Chris@17 630 } else if (mode == ViewManager::DrawMode) {
Chris@17 631
Chris@17 632 Layer *layer = getSelectedLayer();
Chris@23 633 if (layer && layer->isLayerEditable()) {
Chris@17 634 layer->drawDrag(e);
Chris@17 635 }
Chris@18 636
Chris@18 637 } else if (mode == ViewManager::EditMode) {
Chris@18 638
Chris@18 639 Layer *layer = getSelectedLayer();
Chris@23 640 if (layer && layer->isLayerEditable()) {
Chris@18 641 layer->editDrag(e);
Chris@18 642 }
Chris@0 643 }
Chris@0 644 }
Chris@0 645
Chris@0 646 void
Chris@0 647 Pane::mouseDoubleClickEvent(QMouseEvent *e)
Chris@0 648 {
Chris@0 649 std::cerr << "mouseDoubleClickEvent" << std::endl;
Chris@0 650 }
Chris@0 651
Chris@0 652 void
Chris@0 653 Pane::leaveEvent(QEvent *)
Chris@0 654 {
Chris@0 655 bool previouslyIdentifying = m_identifyFeatures;
Chris@0 656 m_identifyFeatures = false;
Chris@0 657 if (previouslyIdentifying) update();
Chris@0 658 }
Chris@0 659
Chris@0 660 void
Chris@0 661 Pane::wheelEvent(QWheelEvent *e)
Chris@0 662 {
Chris@0 663 //std::cerr << "wheelEvent, delta " << e->delta() << std::endl;
Chris@0 664
Chris@0 665 int count = e->delta();
Chris@0 666
Chris@0 667 if (count > 0) {
Chris@0 668 if (count >= 120) count /= 120;
Chris@0 669 else count = 1;
Chris@0 670 }
Chris@0 671
Chris@0 672 if (count < 0) {
Chris@0 673 if (count <= -120) count /= 120;
Chris@0 674 else count = -1;
Chris@0 675 }
Chris@17 676
Chris@17 677 if (e->modifiers() & Qt::ControlModifier) {
Chris@17 678
Chris@20 679 // Scroll left or right, rapidly
Chris@20 680
Chris@17 681 if (getStartFrame() < 0 &&
Chris@17 682 getEndFrame() >= getModelsEndFrame()) return;
Chris@17 683
Chris@17 684 long delta = ((width() / 2) * count * m_zoomLevel);
Chris@17 685
Chris@17 686 if (int(m_centreFrame) < delta) {
Chris@17 687 setCentreFrame(0);
Chris@17 688 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@17 689 setCentreFrame(getModelsEndFrame());
Chris@17 690 } else {
Chris@17 691 setCentreFrame(m_centreFrame - delta);
Chris@17 692 }
Chris@17 693
Chris@17 694 } else {
Chris@17 695
Chris@20 696 // Zoom in or out
Chris@20 697
Chris@17 698 int newZoomLevel = m_zoomLevel;
Chris@0 699
Chris@17 700 while (count > 0) {
Chris@17 701 if (newZoomLevel <= 2) {
Chris@17 702 newZoomLevel = 1;
Chris@17 703 break;
Chris@17 704 }
Chris@17 705 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@17 706 ZoomConstraint::RoundDown);
Chris@17 707 --count;
Chris@0 708 }
Chris@17 709
Chris@17 710 while (count < 0) {
Chris@17 711 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@17 712 ZoomConstraint::RoundUp);
Chris@17 713 ++count;
Chris@17 714 }
Chris@17 715
Chris@17 716 if (newZoomLevel != m_zoomLevel) {
Chris@17 717 setZoomLevel(newZoomLevel);
Chris@17 718 }
Chris@0 719 }
Chris@0 720
Chris@0 721 emit paneInteractedWith();
Chris@0 722 }
Chris@8 723
Chris@13 724 void
Chris@13 725 Pane::toolModeChanged()
Chris@13 726 {
Chris@13 727 ViewManager::ToolMode mode = m_manager->getToolMode();
Chris@13 728 std::cerr << "Pane::toolModeChanged(" << mode << ")" << std::endl;
Chris@13 729
Chris@13 730 switch (mode) {
Chris@13 731
Chris@13 732 case ViewManager::NavigateMode:
Chris@13 733 setCursor(Qt::PointingHandCursor);
Chris@13 734 break;
Chris@13 735
Chris@13 736 case ViewManager::SelectMode:
Chris@13 737 setCursor(Qt::ArrowCursor);
Chris@13 738 break;
Chris@13 739
Chris@13 740 case ViewManager::EditMode:
Chris@19 741 setCursor(Qt::UpArrowCursor);
Chris@13 742 break;
Chris@13 743
Chris@13 744 case ViewManager::DrawMode:
Chris@13 745 setCursor(Qt::CrossCursor);
Chris@13 746 break;
Chris@13 747
Chris@13 748 case ViewManager::TextMode:
Chris@13 749 setCursor(Qt::IBeamCursor);
Chris@13 750 break;
Chris@13 751 }
Chris@13 752 }
Chris@13 753
Chris@8 754 QString
Chris@8 755 Pane::toXmlString(QString indent, QString extraAttributes) const
Chris@8 756 {
Chris@8 757 return View::toXmlString
Chris@8 758 (indent,
Chris@8 759 QString("type=\"pane\" centreLineVisible=\"%1\" %2")
Chris@8 760 .arg(m_centreLineVisible).arg(extraAttributes));
Chris@8 761 }
Chris@8 762
Chris@0 763
Chris@0 764 #ifdef INCLUDE_MOCFILES
Chris@0 765 #include "Pane.moc.cpp"
Chris@0 766 #endif
Chris@0 767