annotate widgets/Pane.cpp @ 42:1bdf285c4eac

* Add "Export Audio File" option * Make note layer align in frequency with any spectrogram layer on the same view (if it's set to frequency mode) * Start to implement mouse editing for ranges of points by dragging the selection * First scrappy attempt at a vertical scale for time value layer
author Chris Cannam
date Mon, 27 Feb 2006 17:34:41 +0000
parents f2c416cbdaa9
children 78515b1e29eb
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@41 17 #include "layer/WaveformLayer.h"
Chris@0 18
Chris@0 19 #include <QPaintEvent>
Chris@0 20 #include <QPainter>
Chris@0 21 #include <iostream>
Chris@0 22 #include <cmath>
Chris@0 23
Chris@0 24 using std::cerr;
Chris@0 25 using std::endl;
Chris@0 26
Chris@0 27 Pane::Pane(QWidget *w) :
Chris@0 28 View(w, true),
Chris@0 29 m_identifyFeatures(false),
Chris@0 30 m_clickedInRange(false),
Chris@0 31 m_shiftPressed(false),
Chris@13 32 m_ctrlPressed(false),
Chris@17 33 m_navigating(false),
Chris@17 34 m_resizing(false),
Chris@0 35 m_centreLineVisible(true)
Chris@0 36 {
Chris@0 37 setObjectName("Pane");
Chris@0 38 setMouseTracking(true);
Chris@0 39 }
Chris@0 40
Chris@0 41 bool
Chris@0 42 Pane::shouldIlluminateLocalFeatures(const Layer *layer, QPoint &pos)
Chris@0 43 {
Chris@42 44 QPoint discard;
Chris@42 45 bool b0, b1;
Chris@42 46
Chris@42 47 if (layer == getSelectedLayer() &&
Chris@42 48 !shouldIlluminateLocalSelection(discard, b0, b1)) {
Chris@42 49
Chris@27 50 pos = m_identifyPoint;
Chris@27 51 return m_identifyFeatures;
Chris@27 52 }
Chris@0 53
Chris@0 54 return false;
Chris@0 55 }
Chris@0 56
Chris@42 57 bool
Chris@42 58 Pane::shouldIlluminateLocalSelection(QPoint &pos,
Chris@42 59 bool &closeToLeft,
Chris@42 60 bool &closeToRight)
Chris@42 61 {
Chris@42 62 if (m_identifyFeatures &&
Chris@42 63 m_manager &&
Chris@42 64 m_manager->getToolMode() == ViewManager::EditMode &&
Chris@42 65 !m_manager->getSelections().empty() &&
Chris@42 66 !selectionIsBeingEdited()) {
Chris@42 67
Chris@42 68 Selection s(getSelectionAt(m_identifyPoint.x(),
Chris@42 69 closeToLeft, closeToRight));
Chris@42 70
Chris@42 71 if (!s.isEmpty()) {
Chris@42 72 if (getSelectedLayer() && getSelectedLayer()->isLayerEditable()) {
Chris@42 73
Chris@42 74 pos = m_identifyPoint;
Chris@42 75 return true;
Chris@42 76 }
Chris@42 77 }
Chris@42 78 }
Chris@42 79
Chris@42 80 return false;
Chris@42 81 }
Chris@42 82
Chris@42 83 bool
Chris@42 84 Pane::selectionIsBeingEdited() const
Chris@42 85 {
Chris@42 86 if (!m_editingSelection.isEmpty()) {
Chris@42 87 if (m_mousePos != m_clickPos &&
Chris@42 88 getFrameForX(m_mousePos.x()) != getFrameForX(m_clickPos.x())) {
Chris@42 89 return true;
Chris@42 90 }
Chris@42 91 }
Chris@42 92 return false;
Chris@42 93 }
Chris@42 94
Chris@0 95 void
Chris@0 96 Pane::setCentreLineVisible(bool visible)
Chris@0 97 {
Chris@0 98 m_centreLineVisible = visible;
Chris@0 99 update();
Chris@0 100 }
Chris@0 101
Chris@0 102 void
Chris@0 103 Pane::paintEvent(QPaintEvent *e)
Chris@0 104 {
Chris@0 105 QPainter paint;
Chris@0 106
Chris@0 107 QRect r(rect());
Chris@0 108
Chris@0 109 if (e) {
Chris@0 110 r = e->rect();
Chris@0 111 }
Chris@0 112 /*
Chris@0 113 paint.begin(this);
Chris@0 114 paint.setClipRect(r);
Chris@0 115
Chris@0 116 if (hasLightBackground()) {
Chris@0 117 paint.setPen(Qt::white);
Chris@0 118 paint.setBrush(Qt::white);
Chris@0 119 } else {
Chris@0 120 paint.setPen(Qt::black);
Chris@0 121 paint.setBrush(Qt::black);
Chris@0 122 }
Chris@0 123 paint.drawRect(r);
Chris@0 124
Chris@0 125 paint.end();
Chris@0 126 */
Chris@0 127 View::paintEvent(e);
Chris@0 128
Chris@0 129 paint.begin(this);
Chris@0 130
Chris@0 131 if (e) {
Chris@0 132 paint.setClipRect(r);
Chris@0 133 }
Chris@41 134
Chris@41 135 const Model *waveformModel = 0; // just for reporting purposes
Chris@41 136
Chris@0 137 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@0 138 --vi;
Chris@0 139
Chris@41 140 if (dynamic_cast<WaveformLayer *>(*vi)) {
Chris@41 141 waveformModel = (*vi)->getModel();
Chris@41 142 }
Chris@41 143
Chris@0 144 int sw = (*vi)->getVerticalScaleWidth(paint);
Chris@0 145
Chris@0 146 if (sw > 0 && r.left() < sw) {
Chris@0 147
Chris@0 148 // Profiler profiler("Pane::paintEvent - painting vertical scale", true);
Chris@0 149
Chris@0 150 // std::cerr << "Pane::paintEvent: calling paint.save() in vertical scale block" << std::endl;
Chris@0 151 paint.save();
Chris@0 152
Chris@0 153 paint.setPen(Qt::black);
Chris@0 154 paint.setBrush(Qt::white);
Chris@0 155 paint.drawRect(0, 0, sw, height());
Chris@0 156
Chris@0 157 paint.setBrush(Qt::NoBrush);
Chris@0 158 (*vi)->paintVerticalScale(paint, QRect(0, 0, sw, height()));
Chris@0 159
Chris@0 160 paint.restore();
Chris@0 161 }
Chris@41 162
Chris@0 163 if (m_identifyFeatures) {
Chris@25 164
Chris@25 165 QPoint pos = m_identifyPoint;
Chris@25 166 QString desc = (*vi)->getFeatureDescription(pos);
Chris@25 167
Chris@25 168 if (desc != "") {
Chris@25 169
Chris@25 170 paint.save();
Chris@25 171
Chris@25 172 int tabStop =
Chris@25 173 paint.fontMetrics().width(tr("Some lengthy prefix:"));
Chris@25 174
Chris@25 175 QRect boundingRect =
Chris@25 176 paint.fontMetrics().boundingRect
Chris@25 177 (rect(),
Chris@25 178 Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs,
Chris@25 179 desc, tabStop);
Chris@25 180
Chris@25 181 if (hasLightBackground()) {
Chris@25 182 paint.setPen(Qt::NoPen);
Chris@25 183 paint.setBrush(QColor(250, 250, 250, 200));
Chris@25 184 } else {
Chris@25 185 paint.setPen(Qt::NoPen);
Chris@25 186 paint.setBrush(QColor(50, 50, 50, 200));
Chris@25 187 }
Chris@25 188
Chris@25 189 int extra = paint.fontMetrics().descent();
Chris@25 190 paint.drawRect(width() - boundingRect.width() - 10 - extra,
Chris@25 191 10 - extra,
Chris@25 192 boundingRect.width() + 2 * extra,
Chris@25 193 boundingRect.height() + extra);
Chris@25 194
Chris@25 195 if (hasLightBackground()) {
Chris@25 196 paint.setPen(QColor(150, 20, 0));
Chris@25 197 } else {
Chris@25 198 paint.setPen(QColor(255, 150, 100));
Chris@25 199 }
Chris@25 200
Chris@25 201 QTextOption option;
Chris@25 202 option.setWrapMode(QTextOption::NoWrap);
Chris@25 203 option.setAlignment(Qt::AlignRight | Qt::AlignTop);
Chris@25 204 option.setTabStop(tabStop);
Chris@25 205 paint.drawText(QRectF(width() - boundingRect.width() - 10, 10,
Chris@25 206 boundingRect.width(),
Chris@25 207 boundingRect.height()),
Chris@25 208 desc,
Chris@25 209 option);
Chris@25 210
Chris@25 211 paint.restore();
Chris@25 212 }
Chris@0 213 }
Chris@0 214
Chris@0 215 break;
Chris@0 216 }
Chris@0 217
Chris@0 218 if (m_centreLineVisible) {
Chris@0 219
Chris@0 220 if (hasLightBackground()) {
Chris@0 221 paint.setPen(QColor(50, 50, 50));
Chris@0 222 } else {
Chris@0 223 paint.setPen(QColor(200, 200, 200));
Chris@0 224 }
Chris@0 225 paint.setBrush(Qt::NoBrush);
Chris@0 226 paint.drawLine(width() / 2, 0, width() / 2, height() - 1);
Chris@0 227
Chris@0 228 // QFont font(paint.font());
Chris@0 229 // font.setBold(true);
Chris@0 230 // paint.setFont(font);
Chris@0 231
Chris@0 232 int sampleRate = getModelsSampleRate();
Chris@0 233 int y = height() - paint.fontMetrics().height()
Chris@0 234 + paint.fontMetrics().ascent() - 6;
Chris@0 235
Chris@0 236 LayerList::iterator vi = m_layers.end();
Chris@0 237
Chris@0 238 if (vi != m_layers.begin()) {
Chris@0 239
Chris@0 240 switch ((*--vi)->getPreferredFrameCountPosition()) {
Chris@0 241
Chris@0 242 case Layer::PositionTop:
Chris@0 243 y = paint.fontMetrics().ascent() + 6;
Chris@0 244 break;
Chris@0 245
Chris@0 246 case Layer::PositionMiddle:
Chris@0 247 y = (height() - paint.fontMetrics().height()) / 2
Chris@0 248 + paint.fontMetrics().ascent();
Chris@0 249 break;
Chris@0 250
Chris@0 251 case Layer::PositionBottom:
Chris@0 252 // y already set correctly
Chris@0 253 break;
Chris@0 254 }
Chris@0 255 }
Chris@0 256
Chris@0 257 if (sampleRate) {
Chris@0 258
Chris@0 259 QString text(QString::fromStdString
Chris@0 260 (RealTime::frame2RealTime
Chris@0 261 (m_centreFrame, sampleRate).toText(true)));
Chris@0 262
Chris@0 263 int tw = paint.fontMetrics().width(text);
Chris@0 264 int x = width()/2 - 4 - tw;
Chris@0 265
Chris@0 266 if (hasLightBackground()) {
Chris@0 267 paint.setPen(palette().background().color());
Chris@0 268 for (int dx = -1; dx <= 1; ++dx) {
Chris@0 269 for (int dy = -1; dy <= 1; ++dy) {
Chris@0 270 if ((dx && dy) || !(dx || dy)) continue;
Chris@0 271 paint.drawText(x + dx, y + dy, text);
Chris@0 272 }
Chris@0 273 }
Chris@0 274 paint.setPen(QColor(50, 50, 50));
Chris@0 275 } else {
Chris@0 276 paint.setPen(QColor(200, 200, 200));
Chris@0 277 }
Chris@0 278
Chris@0 279 paint.drawText(x, y, text);
Chris@0 280 }
Chris@0 281
Chris@0 282 QString text = QString("%1").arg(m_centreFrame);
Chris@0 283
Chris@0 284 int tw = paint.fontMetrics().width(text);
Chris@0 285 int x = width()/2 + 4;
Chris@0 286
Chris@0 287 if (hasLightBackground()) {
Chris@0 288 paint.setPen(palette().background().color());
Chris@0 289 for (int dx = -1; dx <= 1; ++dx) {
Chris@0 290 for (int dy = -1; dy <= 1; ++dy) {
Chris@0 291 if ((dx && dy) || !(dx || dy)) continue;
Chris@0 292 paint.drawText(x + dx, y + dy, text);
Chris@0 293 }
Chris@0 294 }
Chris@0 295 paint.setPen(QColor(50, 50, 50));
Chris@0 296 } else {
Chris@0 297 paint.setPen(QColor(200, 200, 200));
Chris@0 298 }
Chris@0 299 paint.drawText(x, y, text);
Chris@41 300
Chris@41 301 if (waveformModel) {
Chris@41 302
Chris@41 303 QString desc = tr("%1 / %2Hz")
Chris@41 304 .arg(RealTime::frame2RealTime(waveformModel->getEndFrame(),
Chris@41 305 waveformModel->getSampleRate())
Chris@41 306 .toText(false).c_str())
Chris@41 307 .arg(waveformModel->getSampleRate());
Chris@41 308
Chris@41 309 paint.drawText(width() - paint.fontMetrics().width(desc) - 5,
Chris@41 310 height() - paint.fontMetrics().height() +
Chris@41 311 paint.fontMetrics().ascent() - 6,
Chris@41 312 desc);
Chris@41 313 }
Chris@0 314 }
Chris@0 315
Chris@0 316 if (m_clickedInRange && m_shiftPressed) {
Chris@19 317 if (m_manager && (m_manager->getToolMode() == ViewManager::NavigateMode)) {
Chris@19 318 //!!! be nice if this looked a bit more in keeping with the
Chris@19 319 //selection block
Chris@19 320 paint.setPen(Qt::blue);
Chris@19 321 paint.drawRect(m_clickPos.x(), m_clickPos.y(),
Chris@19 322 m_mousePos.x() - m_clickPos.x(),
Chris@19 323 m_mousePos.y() - m_clickPos.y());
Chris@19 324 }
Chris@0 325 }
Chris@0 326
Chris@42 327 if (selectionIsBeingEdited()) {
Chris@42 328
Chris@42 329 int offset = m_mousePos.x() - m_clickPos.x();
Chris@42 330 int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@42 331 int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@42 332
Chris@42 333 if (m_editingSelectionEdge < 0) {
Chris@42 334 p1 = getXForFrame(m_editingSelection.getEndFrame());
Chris@42 335 } else if (m_editingSelectionEdge > 0) {
Chris@42 336 p0 = getXForFrame(m_editingSelection.getStartFrame());
Chris@42 337 }
Chris@42 338
Chris@42 339 paint.save();
Chris@42 340 if (hasLightBackground()) {
Chris@42 341 paint.setPen(QPen(Qt::black, 2));
Chris@42 342 } else {
Chris@42 343 paint.setPen(QPen(Qt::white, 2));
Chris@42 344 }
Chris@42 345
Chris@42 346 //!!! duplicating display policy with View::drawSelections
Chris@42 347
Chris@42 348 if (m_editingSelectionEdge < 0) {
Chris@42 349 paint.drawLine(p0, 1, p1, 1);
Chris@42 350 paint.drawLine(p0, 0, p0, height());
Chris@42 351 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@42 352 } else if (m_editingSelectionEdge > 0) {
Chris@42 353 paint.drawLine(p0, 1, p1, 1);
Chris@42 354 paint.drawLine(p1, 0, p1, height());
Chris@42 355 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@42 356 } else {
Chris@42 357 paint.setBrush(Qt::NoBrush);
Chris@42 358 paint.drawRect(p0, 1, p1 - p0, height() - 2);
Chris@42 359 }
Chris@42 360 paint.restore();
Chris@42 361 }
Chris@42 362
Chris@0 363 paint.end();
Chris@0 364 }
Chris@0 365
Chris@17 366 Selection
Chris@17 367 Pane::getSelectionAt(int x, bool &closeToLeftEdge, bool &closeToRightEdge)
Chris@17 368 {
Chris@17 369 closeToLeftEdge = closeToRightEdge = false;
Chris@17 370
Chris@17 371 if (!m_manager) return Selection();
Chris@17 372
Chris@20 373 long testFrame = getFrameForX(x - 5);
Chris@17 374 if (testFrame < 0) {
Chris@20 375 testFrame = getFrameForX(x);
Chris@17 376 if (testFrame < 0) return Selection();
Chris@17 377 }
Chris@17 378
Chris@17 379 Selection selection = m_manager->getContainingSelection(testFrame, true);
Chris@17 380 if (selection.isEmpty()) return selection;
Chris@17 381
Chris@20 382 int lx = getXForFrame(selection.getStartFrame());
Chris@20 383 int rx = getXForFrame(selection.getEndFrame());
Chris@17 384
Chris@17 385 int fuzz = 2;
Chris@17 386 if (x < lx - fuzz || x > rx + fuzz) return Selection();
Chris@17 387
Chris@17 388 int width = rx - lx;
Chris@17 389 fuzz = 3;
Chris@17 390 if (width < 12) fuzz = width / 4;
Chris@17 391 if (fuzz < 1) fuzz = 1;
Chris@17 392
Chris@17 393 if (x < lx + fuzz) closeToLeftEdge = true;
Chris@17 394 if (x > rx - fuzz) closeToRightEdge = true;
Chris@17 395
Chris@17 396 return selection;
Chris@17 397 }
Chris@17 398
Chris@0 399 void
Chris@0 400 Pane::mousePressEvent(QMouseEvent *e)
Chris@0 401 {
Chris@0 402 m_clickPos = e->pos();
Chris@0 403 m_clickedInRange = true;
Chris@42 404 m_editingSelection = Selection();
Chris@42 405 m_editingSelectionEdge = 0;
Chris@0 406 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@13 407 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@13 408
Chris@13 409 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 410 if (m_manager) mode = m_manager->getToolMode();
Chris@13 411
Chris@17 412 m_navigating = false;
Chris@13 413
Chris@17 414 if (mode == ViewManager::NavigateMode || (e->buttons() & Qt::MidButton)) {
Chris@17 415
Chris@17 416 if (mode != ViewManager::NavigateMode) {
Chris@17 417 setCursor(Qt::PointingHandCursor);
Chris@17 418 }
Chris@17 419
Chris@17 420 m_navigating = true;
Chris@13 421 m_dragCentreFrame = m_centreFrame;
Chris@13 422
Chris@13 423 } else if (mode == ViewManager::SelectMode) {
Chris@13 424
Chris@17 425 bool closeToLeft = false, closeToRight = false;
Chris@17 426 Selection selection = getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@17 427
Chris@17 428 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@17 429
Chris@17 430 m_manager->removeSelection(selection);
Chris@17 431
Chris@17 432 if (closeToLeft) {
Chris@17 433 m_selectionStartFrame = selection.getEndFrame();
Chris@17 434 } else {
Chris@17 435 m_selectionStartFrame = selection.getStartFrame();
Chris@17 436 }
Chris@17 437
Chris@17 438 m_manager->setInProgressSelection(selection, false);
Chris@17 439 m_resizing = true;
Chris@13 440
Chris@17 441 } else {
Chris@17 442
Chris@20 443 int mouseFrame = getFrameForX(e->x());
Chris@17 444 size_t resolution = 1;
Chris@17 445 int snapFrame = mouseFrame;
Chris@17 446
Chris@17 447 Layer *layer = getSelectedLayer();
Chris@17 448 if (layer) {
Chris@28 449 layer->snapToFeatureFrame(snapFrame, resolution, Layer::SnapLeft);
Chris@17 450 }
Chris@17 451
Chris@17 452 if (snapFrame < 0) snapFrame = 0;
Chris@17 453 m_selectionStartFrame = snapFrame;
Chris@17 454 if (m_manager) {
Chris@17 455 m_manager->setInProgressSelection(Selection(snapFrame,
Chris@17 456 snapFrame + resolution),
Chris@17 457 !m_ctrlPressed);
Chris@17 458 }
Chris@17 459
Chris@17 460 m_resizing = false;
Chris@17 461 }
Chris@17 462
Chris@17 463 update();
Chris@17 464
Chris@17 465 } else if (mode == ViewManager::DrawMode) {
Chris@17 466
Chris@13 467 Layer *layer = getSelectedLayer();
Chris@23 468 if (layer && layer->isLayerEditable()) {
Chris@17 469 layer->drawStart(e);
Chris@13 470 }
Chris@18 471
Chris@18 472 } else if (mode == ViewManager::EditMode) {
Chris@18 473
Chris@42 474 if (!editSelectionStart(e)) {
Chris@42 475 Layer *layer = getSelectedLayer();
Chris@42 476 if (layer && layer->isLayerEditable()) {
Chris@42 477 layer->editStart(e);
Chris@42 478 }
Chris@18 479 }
Chris@13 480 }
Chris@0 481
Chris@0 482 emit paneInteractedWith();
Chris@0 483 }
Chris@0 484
Chris@0 485 void
Chris@0 486 Pane::mouseReleaseEvent(QMouseEvent *e)
Chris@0 487 {
Chris@13 488 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 489 if (m_manager) mode = m_manager->getToolMode();
Chris@13 490
Chris@0 491 if (m_clickedInRange) {
Chris@0 492 mouseMoveEvent(e);
Chris@0 493 }
Chris@0 494
Chris@17 495 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@17 496
Chris@17 497 m_navigating = false;
Chris@17 498
Chris@17 499 if (mode != ViewManager::NavigateMode) {
Chris@17 500 // restore cursor
Chris@17 501 toolModeChanged();
Chris@17 502 }
Chris@0 503
Chris@13 504 if (m_shiftPressed) {
Chris@0 505
Chris@13 506 int x0 = std::min(m_clickPos.x(), m_mousePos.x());
Chris@13 507 int x1 = std::max(m_clickPos.x(), m_mousePos.x());
Chris@13 508 int w = x1 - x0;
Chris@13 509
Chris@20 510 long newStartFrame = getFrameForX(x0);
Chris@13 511
Chris@20 512 long visibleFrames = getEndFrame() - getStartFrame();
Chris@20 513 if (newStartFrame <= -visibleFrames) {
Chris@20 514 newStartFrame = -visibleFrames + 1;
Chris@13 515 }
Chris@13 516
Chris@13 517 if (newStartFrame >= long(getModelsEndFrame())) {
Chris@13 518 newStartFrame = getModelsEndFrame() - 1;
Chris@13 519 }
Chris@13 520
Chris@13 521 float ratio = float(w) / float(width());
Chris@13 522 // std::cerr << "ratio: " << ratio << std::endl;
Chris@13 523 size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio);
Chris@13 524 if (newZoomLevel < 1) newZoomLevel = 1;
Chris@13 525
Chris@13 526 // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl;
Chris@13 527 setZoomLevel(getZoomConstraintBlockSize(newZoomLevel));
Chris@13 528 setStartFrame(newStartFrame);
Chris@13 529
Chris@13 530 //cerr << "mouseReleaseEvent: start frame now " << m_startFrame << endl;
Chris@13 531 // update();
Chris@0 532 }
Chris@0 533
Chris@13 534 } else if (mode == ViewManager::SelectMode) {
Chris@13 535
Chris@13 536 if (m_manager && m_manager->haveInProgressSelection()) {
Chris@13 537
Chris@13 538 bool exclusive;
Chris@13 539 Selection selection = m_manager->getInProgressSelection(exclusive);
Chris@13 540
Chris@13 541 if (selection.getEndFrame() < selection.getStartFrame() + 2) {
Chris@13 542 selection = Selection();
Chris@13 543 }
Chris@13 544
Chris@13 545 m_manager->clearInProgressSelection();
Chris@13 546
Chris@13 547 if (exclusive) {
Chris@13 548 m_manager->setSelection(selection);
Chris@13 549 } else {
Chris@13 550 m_manager->addSelection(selection);
Chris@13 551 }
Chris@0 552 }
Chris@13 553
Chris@13 554 update();
Chris@17 555
Chris@17 556 } else if (mode == ViewManager::DrawMode) {
Chris@17 557
Chris@17 558 Layer *layer = getSelectedLayer();
Chris@23 559 if (layer && layer->isLayerEditable()) {
Chris@17 560 layer->drawEnd(e);
Chris@17 561 update();
Chris@17 562 }
Chris@18 563
Chris@18 564 } else if (mode == ViewManager::EditMode) {
Chris@18 565
Chris@42 566 if (!editSelectionEnd(e)) {
Chris@42 567 Layer *layer = getSelectedLayer();
Chris@42 568 if (layer && layer->isLayerEditable()) {
Chris@42 569 layer->editEnd(e);
Chris@42 570 update();
Chris@42 571 }
Chris@18 572 }
Chris@17 573 }
Chris@0 574
Chris@0 575 m_clickedInRange = false;
Chris@0 576
Chris@0 577 emit paneInteractedWith();
Chris@0 578 }
Chris@0 579
Chris@0 580 void
Chris@0 581 Pane::mouseMoveEvent(QMouseEvent *e)
Chris@0 582 {
Chris@13 583 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 584 if (m_manager) mode = m_manager->getToolMode();
Chris@13 585
Chris@28 586 QPoint prevPoint = m_identifyPoint;
Chris@28 587 m_identifyPoint = e->pos();
Chris@28 588
Chris@0 589 if (!m_clickedInRange) {
Chris@0 590
Chris@17 591 if (mode == ViewManager::SelectMode) {
Chris@17 592 bool closeToLeft = false, closeToRight = false;
Chris@17 593 getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@17 594 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@17 595 setCursor(Qt::SizeHorCursor);
Chris@17 596 } else {
Chris@17 597 setCursor(Qt::ArrowCursor);
Chris@17 598 }
Chris@17 599 }
Chris@0 600
Chris@35 601 //!!! if (mode != ViewManager::DrawMode) {
Chris@0 602
Chris@17 603 bool previouslyIdentifying = m_identifyFeatures;
Chris@17 604 m_identifyFeatures = true;
Chris@17 605
Chris@17 606 if (m_identifyFeatures != previouslyIdentifying ||
Chris@17 607 m_identifyPoint != prevPoint) {
Chris@17 608 update();
Chris@17 609 }
Chris@35 610 // }
Chris@0 611
Chris@13 612 return;
Chris@13 613 }
Chris@0 614
Chris@17 615 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@0 616
Chris@13 617 if (m_shiftPressed) {
Chris@0 618
Chris@13 619 m_mousePos = e->pos();
Chris@13 620 update();
Chris@0 621
Chris@0 622 } else {
Chris@13 623
Chris@20 624 long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x());
Chris@20 625
Chris@13 626 size_t newCentreFrame = m_dragCentreFrame;
Chris@13 627
Chris@13 628 if (frameOff < 0) {
Chris@13 629 newCentreFrame -= frameOff;
Chris@13 630 } else if (newCentreFrame >= size_t(frameOff)) {
Chris@13 631 newCentreFrame -= frameOff;
Chris@13 632 } else {
Chris@13 633 newCentreFrame = 0;
Chris@13 634 }
Chris@13 635
Chris@13 636 if (newCentreFrame >= getModelsEndFrame()) {
Chris@13 637 newCentreFrame = getModelsEndFrame();
Chris@13 638 if (newCentreFrame > 0) --newCentreFrame;
Chris@13 639 }
Chris@20 640
Chris@20 641 if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) {
Chris@13 642 setCentreFrame(newCentreFrame);
Chris@13 643 }
Chris@0 644 }
Chris@0 645
Chris@13 646 } else if (mode == ViewManager::SelectMode) {
Chris@13 647
Chris@20 648 int mouseFrame = getFrameForX(e->x());
Chris@13 649 size_t resolution = 1;
Chris@13 650 int snapFrameLeft = mouseFrame;
Chris@13 651 int snapFrameRight = mouseFrame;
Chris@13 652
Chris@13 653 Layer *layer = getSelectedLayer();
Chris@13 654 if (layer) {
Chris@28 655 layer->snapToFeatureFrame(snapFrameLeft, resolution, Layer::SnapLeft);
Chris@28 656 layer->snapToFeatureFrame(snapFrameRight, resolution, Layer::SnapRight);
Chris@13 657 }
Chris@13 658
Chris@37 659 // std::cerr << "snap: frame = " << mouseFrame << ", start frame = " << m_selectionStartFrame << ", left = " << snapFrameLeft << ", right = " << snapFrameRight << std::endl;
Chris@28 660
Chris@13 661 if (snapFrameLeft < 0) snapFrameLeft = 0;
Chris@13 662 if (snapFrameRight < 0) snapFrameRight = 0;
Chris@13 663
Chris@13 664 size_t min, max;
Chris@13 665
Chris@13 666 if (m_selectionStartFrame > snapFrameLeft) {
Chris@13 667 min = snapFrameLeft;
Chris@13 668 max = m_selectionStartFrame;
Chris@13 669 } else if (snapFrameRight > m_selectionStartFrame) {
Chris@13 670 min = m_selectionStartFrame;
Chris@13 671 max = snapFrameRight;
Chris@13 672 } else {
Chris@13 673 min = snapFrameLeft;
Chris@13 674 max = snapFrameRight;
Chris@0 675 }
Chris@0 676
Chris@13 677 if (m_manager) {
Chris@13 678 m_manager->setInProgressSelection(Selection(min, max),
Chris@17 679 !m_resizing && !m_ctrlPressed);
Chris@0 680 }
Chris@15 681
Chris@15 682 bool doScroll = false;
Chris@15 683 if (!m_manager) doScroll = true;
Chris@15 684 if (!m_manager->isPlaying()) doScroll = true;
Chris@15 685 if (m_followPlay != PlaybackScrollContinuous) doScroll = true;
Chris@15 686
Chris@15 687 if (doScroll) {
Chris@13 688 int offset = mouseFrame - getStartFrame();
Chris@13 689 int available = getEndFrame() - getStartFrame();
Chris@15 690 if (offset >= available * 0.95) {
Chris@15 691 int move = int(offset - available * 0.95) + 1;
Chris@14 692 setCentreFrame(m_centreFrame + move);
Chris@15 693 } else if (offset <= available * 0.10) {
Chris@15 694 int move = int(available * 0.10 - offset) + 1;
Chris@14 695 if (m_centreFrame > move) {
Chris@14 696 setCentreFrame(m_centreFrame - move);
Chris@14 697 } else {
Chris@14 698 setCentreFrame(0);
Chris@14 699 }
Chris@13 700 }
Chris@13 701 }
Chris@13 702
Chris@13 703 update();
Chris@17 704
Chris@17 705 } else if (mode == ViewManager::DrawMode) {
Chris@17 706
Chris@17 707 Layer *layer = getSelectedLayer();
Chris@23 708 if (layer && layer->isLayerEditable()) {
Chris@17 709 layer->drawDrag(e);
Chris@17 710 }
Chris@18 711
Chris@18 712 } else if (mode == ViewManager::EditMode) {
Chris@18 713
Chris@42 714 if (!editSelectionDrag(e)) {
Chris@42 715 Layer *layer = getSelectedLayer();
Chris@42 716 if (layer && layer->isLayerEditable()) {
Chris@42 717 layer->editDrag(e);
Chris@42 718 }
Chris@18 719 }
Chris@0 720 }
Chris@0 721 }
Chris@0 722
Chris@0 723 void
Chris@0 724 Pane::mouseDoubleClickEvent(QMouseEvent *e)
Chris@0 725 {
Chris@0 726 std::cerr << "mouseDoubleClickEvent" << std::endl;
Chris@36 727
Chris@36 728 m_clickPos = e->pos();
Chris@36 729 m_clickedInRange = true;
Chris@36 730 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@36 731 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@36 732
Chris@36 733 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@36 734 if (m_manager) mode = m_manager->getToolMode();
Chris@36 735
Chris@36 736 if (mode == ViewManager::EditMode) {
Chris@36 737
Chris@36 738 Layer *layer = getSelectedLayer();
Chris@36 739 if (layer && layer->isLayerEditable()) {
Chris@36 740 layer->editOpen(e);
Chris@36 741 }
Chris@36 742 }
Chris@0 743 }
Chris@0 744
Chris@0 745 void
Chris@0 746 Pane::leaveEvent(QEvent *)
Chris@0 747 {
Chris@0 748 bool previouslyIdentifying = m_identifyFeatures;
Chris@0 749 m_identifyFeatures = false;
Chris@0 750 if (previouslyIdentifying) update();
Chris@0 751 }
Chris@0 752
Chris@0 753 void
Chris@0 754 Pane::wheelEvent(QWheelEvent *e)
Chris@0 755 {
Chris@0 756 //std::cerr << "wheelEvent, delta " << e->delta() << std::endl;
Chris@0 757
Chris@0 758 int count = e->delta();
Chris@0 759
Chris@0 760 if (count > 0) {
Chris@0 761 if (count >= 120) count /= 120;
Chris@0 762 else count = 1;
Chris@0 763 }
Chris@0 764
Chris@0 765 if (count < 0) {
Chris@0 766 if (count <= -120) count /= 120;
Chris@0 767 else count = -1;
Chris@0 768 }
Chris@17 769
Chris@17 770 if (e->modifiers() & Qt::ControlModifier) {
Chris@17 771
Chris@20 772 // Scroll left or right, rapidly
Chris@20 773
Chris@17 774 if (getStartFrame() < 0 &&
Chris@17 775 getEndFrame() >= getModelsEndFrame()) return;
Chris@17 776
Chris@17 777 long delta = ((width() / 2) * count * m_zoomLevel);
Chris@17 778
Chris@17 779 if (int(m_centreFrame) < delta) {
Chris@17 780 setCentreFrame(0);
Chris@17 781 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@17 782 setCentreFrame(getModelsEndFrame());
Chris@17 783 } else {
Chris@17 784 setCentreFrame(m_centreFrame - delta);
Chris@17 785 }
Chris@17 786
Chris@17 787 } else {
Chris@17 788
Chris@20 789 // Zoom in or out
Chris@20 790
Chris@17 791 int newZoomLevel = m_zoomLevel;
Chris@0 792
Chris@17 793 while (count > 0) {
Chris@17 794 if (newZoomLevel <= 2) {
Chris@17 795 newZoomLevel = 1;
Chris@17 796 break;
Chris@17 797 }
Chris@17 798 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@17 799 ZoomConstraint::RoundDown);
Chris@17 800 --count;
Chris@0 801 }
Chris@17 802
Chris@17 803 while (count < 0) {
Chris@17 804 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@17 805 ZoomConstraint::RoundUp);
Chris@17 806 ++count;
Chris@17 807 }
Chris@17 808
Chris@17 809 if (newZoomLevel != m_zoomLevel) {
Chris@17 810 setZoomLevel(newZoomLevel);
Chris@17 811 }
Chris@0 812 }
Chris@0 813
Chris@0 814 emit paneInteractedWith();
Chris@0 815 }
Chris@8 816
Chris@42 817 bool
Chris@42 818 Pane::editSelectionStart(QMouseEvent *e)
Chris@42 819 {
Chris@42 820 bool closeToLeft, closeToRight;
Chris@42 821 Selection s(getSelectionAt(e->x(), closeToLeft, closeToRight));
Chris@42 822 if (s.isEmpty()) return false;
Chris@42 823 m_editingSelection = s;
Chris@42 824 m_editingSelectionEdge = (closeToLeft ? -1 : closeToRight ? 1 : 0);
Chris@42 825 m_mousePos = e->pos();
Chris@42 826 return true;
Chris@42 827 }
Chris@42 828
Chris@42 829 bool
Chris@42 830 Pane::editSelectionDrag(QMouseEvent *e)
Chris@42 831 {
Chris@42 832 if (m_editingSelection.isEmpty()) return false;
Chris@42 833 m_mousePos = e->pos();
Chris@42 834 update();
Chris@42 835 return true;
Chris@42 836 }
Chris@42 837
Chris@42 838 bool
Chris@42 839 Pane::editSelectionEnd(QMouseEvent *e)
Chris@42 840 {
Chris@42 841 if (m_editingSelection.isEmpty()) return false;
Chris@42 842 m_editingSelection = Selection();
Chris@42 843 return true;
Chris@42 844 }
Chris@42 845
Chris@13 846 void
Chris@13 847 Pane::toolModeChanged()
Chris@13 848 {
Chris@13 849 ViewManager::ToolMode mode = m_manager->getToolMode();
Chris@13 850 std::cerr << "Pane::toolModeChanged(" << mode << ")" << std::endl;
Chris@13 851
Chris@13 852 switch (mode) {
Chris@13 853
Chris@13 854 case ViewManager::NavigateMode:
Chris@13 855 setCursor(Qt::PointingHandCursor);
Chris@13 856 break;
Chris@13 857
Chris@13 858 case ViewManager::SelectMode:
Chris@13 859 setCursor(Qt::ArrowCursor);
Chris@13 860 break;
Chris@13 861
Chris@13 862 case ViewManager::EditMode:
Chris@19 863 setCursor(Qt::UpArrowCursor);
Chris@13 864 break;
Chris@13 865
Chris@13 866 case ViewManager::DrawMode:
Chris@13 867 setCursor(Qt::CrossCursor);
Chris@13 868 break;
Chris@36 869 /*
Chris@13 870 case ViewManager::TextMode:
Chris@13 871 setCursor(Qt::IBeamCursor);
Chris@13 872 break;
Chris@36 873 */
Chris@13 874 }
Chris@13 875 }
Chris@13 876
Chris@8 877 QString
Chris@8 878 Pane::toXmlString(QString indent, QString extraAttributes) const
Chris@8 879 {
Chris@8 880 return View::toXmlString
Chris@8 881 (indent,
Chris@8 882 QString("type=\"pane\" centreLineVisible=\"%1\" %2")
Chris@8 883 .arg(m_centreLineVisible).arg(extraAttributes));
Chris@8 884 }
Chris@8 885
Chris@0 886
Chris@0 887 #ifdef INCLUDE_MOCFILES
Chris@0 888 #include "Pane.moc.cpp"
Chris@0 889 #endif
Chris@0 890