annotate widgets/Pane.cpp @ 55:128ebfeeebee

* Add Insert Instant function in main window * Ensure selections and window geometry are saved in session file * Add wait cursor on session file save * Various improvements to display of texts in pane (clearer readability) * Use commands for setting properties on layers and panes (still need to batch up multiple sets on the same property) * Fix failure of spectrogram to refresh when initial part became visible * Some fixes & paint optimisations in View &c * Make curve mode for time value layers work properly when resolution == 1 * Some vague improvements for time value layer vertical scale
author Chris Cannam
date Thu, 16 Mar 2006 18:46:00 +0000
parents d2eac322d71b
children fedaf3ffe80a
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@44 42 Pane::shouldIlluminateLocalFeatures(const Layer *layer, QPoint &pos) const
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@44 60 bool &closeToRight) const
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@51 136 int verticalScaleWidth = 0;
Chris@41 137
Chris@55 138 int fontHeight = paint.fontMetrics().height();
Chris@55 139 int fontAscent = paint.fontMetrics().ascent();
Chris@55 140
Chris@0 141 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@0 142 --vi;
Chris@0 143
Chris@41 144 if (dynamic_cast<WaveformLayer *>(*vi)) {
Chris@41 145 waveformModel = (*vi)->getModel();
Chris@41 146 }
Chris@41 147
Chris@51 148 verticalScaleWidth = (*vi)->getVerticalScaleWidth(this, paint);
Chris@0 149
Chris@51 150 if (verticalScaleWidth > 0 && r.left() < verticalScaleWidth) {
Chris@0 151
Chris@0 152 // Profiler profiler("Pane::paintEvent - painting vertical scale", true);
Chris@0 153
Chris@0 154 // std::cerr << "Pane::paintEvent: calling paint.save() in vertical scale block" << std::endl;
Chris@0 155 paint.save();
Chris@0 156
Chris@0 157 paint.setPen(Qt::black);
Chris@0 158 paint.setBrush(Qt::white);
Chris@51 159 paint.drawRect(0, 0, verticalScaleWidth, height());
Chris@0 160
Chris@0 161 paint.setBrush(Qt::NoBrush);
Chris@51 162 (*vi)->paintVerticalScale
Chris@51 163 (this, paint, QRect(0, 0, verticalScaleWidth, height()));
Chris@0 164
Chris@0 165 paint.restore();
Chris@0 166 }
Chris@41 167
Chris@0 168 if (m_identifyFeatures) {
Chris@25 169
Chris@25 170 QPoint pos = m_identifyPoint;
Chris@44 171 QString desc = (*vi)->getFeatureDescription(this, pos);
Chris@25 172
Chris@25 173 if (desc != "") {
Chris@25 174
Chris@25 175 paint.save();
Chris@25 176
Chris@25 177 int tabStop =
Chris@25 178 paint.fontMetrics().width(tr("Some lengthy prefix:"));
Chris@25 179
Chris@25 180 QRect boundingRect =
Chris@25 181 paint.fontMetrics().boundingRect
Chris@25 182 (rect(),
Chris@25 183 Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs,
Chris@25 184 desc, tabStop);
Chris@25 185
Chris@25 186 if (hasLightBackground()) {
Chris@25 187 paint.setPen(Qt::NoPen);
Chris@25 188 paint.setBrush(QColor(250, 250, 250, 200));
Chris@25 189 } else {
Chris@25 190 paint.setPen(Qt::NoPen);
Chris@25 191 paint.setBrush(QColor(50, 50, 50, 200));
Chris@25 192 }
Chris@25 193
Chris@25 194 int extra = paint.fontMetrics().descent();
Chris@25 195 paint.drawRect(width() - boundingRect.width() - 10 - extra,
Chris@25 196 10 - extra,
Chris@25 197 boundingRect.width() + 2 * extra,
Chris@25 198 boundingRect.height() + extra);
Chris@25 199
Chris@25 200 if (hasLightBackground()) {
Chris@25 201 paint.setPen(QColor(150, 20, 0));
Chris@25 202 } else {
Chris@25 203 paint.setPen(QColor(255, 150, 100));
Chris@25 204 }
Chris@25 205
Chris@25 206 QTextOption option;
Chris@25 207 option.setWrapMode(QTextOption::NoWrap);
Chris@25 208 option.setAlignment(Qt::AlignRight | Qt::AlignTop);
Chris@25 209 option.setTabStop(tabStop);
Chris@25 210 paint.drawText(QRectF(width() - boundingRect.width() - 10, 10,
Chris@25 211 boundingRect.width(),
Chris@25 212 boundingRect.height()),
Chris@25 213 desc,
Chris@25 214 option);
Chris@25 215
Chris@25 216 paint.restore();
Chris@25 217 }
Chris@0 218 }
Chris@0 219
Chris@0 220 break;
Chris@0 221 }
Chris@0 222
Chris@55 223 int sampleRate = getModelsSampleRate();
Chris@55 224 paint.setBrush(Qt::NoBrush);
Chris@55 225
Chris@0 226 if (m_centreLineVisible) {
Chris@0 227
Chris@0 228 if (hasLightBackground()) {
Chris@0 229 paint.setPen(QColor(50, 50, 50));
Chris@0 230 } else {
Chris@0 231 paint.setPen(QColor(200, 200, 200));
Chris@0 232 }
Chris@0 233 paint.drawLine(width() / 2, 0, width() / 2, height() - 1);
Chris@0 234
Chris@55 235 paint.setPen(QColor(50, 50, 50));
Chris@55 236
Chris@55 237 int y = height() - fontHeight
Chris@55 238 + fontAscent - 6;
Chris@0 239
Chris@0 240 LayerList::iterator vi = m_layers.end();
Chris@0 241
Chris@0 242 if (vi != m_layers.begin()) {
Chris@0 243
Chris@0 244 switch ((*--vi)->getPreferredFrameCountPosition()) {
Chris@0 245
Chris@0 246 case Layer::PositionTop:
Chris@55 247 y = fontAscent + 6;
Chris@0 248 break;
Chris@0 249
Chris@0 250 case Layer::PositionMiddle:
Chris@55 251 y = (height() - fontHeight) / 2
Chris@55 252 + fontAscent;
Chris@0 253 break;
Chris@0 254
Chris@0 255 case Layer::PositionBottom:
Chris@0 256 // y already set correctly
Chris@0 257 break;
Chris@0 258 }
Chris@0 259 }
Chris@0 260
Chris@0 261 if (sampleRate) {
Chris@0 262
Chris@0 263 QString text(QString::fromStdString
Chris@0 264 (RealTime::frame2RealTime
Chris@0 265 (m_centreFrame, sampleRate).toText(true)));
Chris@0 266
Chris@0 267 int tw = paint.fontMetrics().width(text);
Chris@0 268 int x = width()/2 - 4 - tw;
Chris@0 269
Chris@55 270 drawVisibleText(paint, x, y, text, OutlinedText);
Chris@0 271 }
Chris@0 272
Chris@0 273 QString text = QString("%1").arg(m_centreFrame);
Chris@0 274
Chris@0 275 int tw = paint.fontMetrics().width(text);
Chris@0 276 int x = width()/2 + 4;
Chris@55 277
Chris@55 278 drawVisibleText(paint, x, y, text, OutlinedText);
Chris@55 279
Chris@55 280 } else {
Chris@55 281
Chris@55 282 paint.setPen(QColor(50, 50, 50));
Chris@55 283 }
Chris@55 284
Chris@55 285 if (waveformModel &&
Chris@55 286 r.y() + r.height() >= height() - fontHeight - 6) {
Chris@55 287
Chris@55 288 size_t mainModelRate = m_manager->getMainModelSampleRate();
Chris@55 289 size_t playbackRate = m_manager->getPlaybackSampleRate();
Chris@55 290
Chris@55 291 QString srNote = "";
Chris@55 292
Chris@55 293 // Show (R) for waveform models that will be resampled on
Chris@55 294 // playback, and (X) for waveform models that will be played
Chris@55 295 // at the wrong rate because their rate differs from that of
Chris@55 296 // the main model.
Chris@55 297
Chris@55 298 if (sampleRate == mainModelRate) {
Chris@55 299 if (sampleRate != playbackRate) srNote = " " + tr("(R)");
Chris@0 300 } else {
Chris@55 301 std::cerr << "Sample rate = " << sampleRate << ", main model rate = " << mainModelRate << std::endl;
Chris@55 302 srNote = " " + tr("(X)");
Chris@0 303 }
Chris@41 304
Chris@55 305 QString desc = tr("%1 / %2Hz%3")
Chris@55 306 .arg(RealTime::frame2RealTime(waveformModel->getEndFrame(),
Chris@55 307 sampleRate)
Chris@55 308 .toText(false).c_str())
Chris@55 309 .arg(sampleRate)
Chris@55 310 .arg(srNote);
Chris@49 311
Chris@55 312 if (r.x() < verticalScaleWidth + 5 + paint.fontMetrics().width(desc)) {
Chris@55 313 drawVisibleText(paint, verticalScaleWidth + 5,
Chris@55 314 height() - fontHeight + fontAscent - 6,
Chris@55 315 desc, OutlinedText);
Chris@55 316 }
Chris@55 317 }
Chris@50 318
Chris@55 319 if (r.y() + r.height() >= height() - m_layers.size() * fontHeight - 6) {
Chris@51 320
Chris@51 321 std::vector<QString> texts;
Chris@51 322 int maxTextWidth = 0;
Chris@51 323
Chris@51 324 for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@51 325
Chris@51 326 QString layerName = (*i)->objectName();
Chris@51 327 QString modelName;
Chris@51 328 if ((*i)->getModel()) modelName = (*i)->getModel()->objectName();
Chris@51 329
Chris@51 330 QString text;
Chris@51 331 if (modelName != "") {
Chris@51 332 text = QString("%1: %2").arg(modelName).arg(layerName);
Chris@51 333 } else {
Chris@51 334 text = layerName;
Chris@51 335 }
Chris@55 336
Chris@51 337 texts.push_back(text);
Chris@51 338 int tw = paint.fontMetrics().width(text);
Chris@51 339 if (tw > maxTextWidth) maxTextWidth = tw;
Chris@51 340 }
Chris@55 341
Chris@51 342 int lly = height() - 6;
Chris@51 343
Chris@55 344 if (r.x() + r.width() >= width() - maxTextWidth - 5) {
Chris@55 345
Chris@55 346 for (int i = 0; i < texts.size(); ++i) {
Chris@51 347
Chris@55 348 if (i == texts.size() - 1) {
Chris@51 349 paint.setPen(Qt::black);
Chris@51 350 }
Chris@55 351
Chris@55 352 drawVisibleText(paint, width() - maxTextWidth - 5,
Chris@55 353 lly - fontHeight + fontAscent,
Chris@55 354 texts[i], OutlinedText);
Chris@55 355
Chris@55 356 lly -= fontHeight;
Chris@51 357 }
Chris@51 358 }
Chris@0 359 }
Chris@0 360
Chris@0 361 if (m_clickedInRange && m_shiftPressed) {
Chris@19 362 if (m_manager && (m_manager->getToolMode() == ViewManager::NavigateMode)) {
Chris@19 363 //!!! be nice if this looked a bit more in keeping with the
Chris@19 364 //selection block
Chris@19 365 paint.setPen(Qt::blue);
Chris@19 366 paint.drawRect(m_clickPos.x(), m_clickPos.y(),
Chris@19 367 m_mousePos.x() - m_clickPos.x(),
Chris@19 368 m_mousePos.y() - m_clickPos.y());
Chris@19 369 }
Chris@0 370 }
Chris@0 371
Chris@42 372 if (selectionIsBeingEdited()) {
Chris@42 373
Chris@42 374 int offset = m_mousePos.x() - m_clickPos.x();
Chris@42 375 int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@42 376 int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@42 377
Chris@42 378 if (m_editingSelectionEdge < 0) {
Chris@42 379 p1 = getXForFrame(m_editingSelection.getEndFrame());
Chris@42 380 } else if (m_editingSelectionEdge > 0) {
Chris@42 381 p0 = getXForFrame(m_editingSelection.getStartFrame());
Chris@42 382 }
Chris@42 383
Chris@42 384 paint.save();
Chris@42 385 if (hasLightBackground()) {
Chris@42 386 paint.setPen(QPen(Qt::black, 2));
Chris@42 387 } else {
Chris@42 388 paint.setPen(QPen(Qt::white, 2));
Chris@42 389 }
Chris@42 390
Chris@42 391 //!!! duplicating display policy with View::drawSelections
Chris@42 392
Chris@42 393 if (m_editingSelectionEdge < 0) {
Chris@42 394 paint.drawLine(p0, 1, p1, 1);
Chris@42 395 paint.drawLine(p0, 0, p0, height());
Chris@42 396 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@42 397 } else if (m_editingSelectionEdge > 0) {
Chris@42 398 paint.drawLine(p0, 1, p1, 1);
Chris@42 399 paint.drawLine(p1, 0, p1, height());
Chris@42 400 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@42 401 } else {
Chris@42 402 paint.setBrush(Qt::NoBrush);
Chris@42 403 paint.drawRect(p0, 1, p1 - p0, height() - 2);
Chris@42 404 }
Chris@42 405 paint.restore();
Chris@42 406 }
Chris@42 407
Chris@0 408 paint.end();
Chris@0 409 }
Chris@0 410
Chris@17 411 Selection
Chris@44 412 Pane::getSelectionAt(int x, bool &closeToLeftEdge, bool &closeToRightEdge) const
Chris@17 413 {
Chris@17 414 closeToLeftEdge = closeToRightEdge = false;
Chris@17 415
Chris@17 416 if (!m_manager) return Selection();
Chris@17 417
Chris@20 418 long testFrame = getFrameForX(x - 5);
Chris@17 419 if (testFrame < 0) {
Chris@20 420 testFrame = getFrameForX(x);
Chris@17 421 if (testFrame < 0) return Selection();
Chris@17 422 }
Chris@17 423
Chris@17 424 Selection selection = m_manager->getContainingSelection(testFrame, true);
Chris@17 425 if (selection.isEmpty()) return selection;
Chris@17 426
Chris@20 427 int lx = getXForFrame(selection.getStartFrame());
Chris@20 428 int rx = getXForFrame(selection.getEndFrame());
Chris@17 429
Chris@17 430 int fuzz = 2;
Chris@17 431 if (x < lx - fuzz || x > rx + fuzz) return Selection();
Chris@17 432
Chris@17 433 int width = rx - lx;
Chris@17 434 fuzz = 3;
Chris@17 435 if (width < 12) fuzz = width / 4;
Chris@17 436 if (fuzz < 1) fuzz = 1;
Chris@17 437
Chris@17 438 if (x < lx + fuzz) closeToLeftEdge = true;
Chris@17 439 if (x > rx - fuzz) closeToRightEdge = true;
Chris@17 440
Chris@17 441 return selection;
Chris@17 442 }
Chris@17 443
Chris@0 444 void
Chris@0 445 Pane::mousePressEvent(QMouseEvent *e)
Chris@0 446 {
Chris@0 447 m_clickPos = e->pos();
Chris@0 448 m_clickedInRange = true;
Chris@42 449 m_editingSelection = Selection();
Chris@42 450 m_editingSelectionEdge = 0;
Chris@0 451 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@13 452 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@13 453
Chris@13 454 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 455 if (m_manager) mode = m_manager->getToolMode();
Chris@13 456
Chris@17 457 m_navigating = false;
Chris@13 458
Chris@17 459 if (mode == ViewManager::NavigateMode || (e->buttons() & Qt::MidButton)) {
Chris@17 460
Chris@17 461 if (mode != ViewManager::NavigateMode) {
Chris@17 462 setCursor(Qt::PointingHandCursor);
Chris@17 463 }
Chris@17 464
Chris@17 465 m_navigating = true;
Chris@13 466 m_dragCentreFrame = m_centreFrame;
Chris@13 467
Chris@13 468 } else if (mode == ViewManager::SelectMode) {
Chris@13 469
Chris@17 470 bool closeToLeft = false, closeToRight = false;
Chris@17 471 Selection selection = getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@17 472
Chris@17 473 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@17 474
Chris@17 475 m_manager->removeSelection(selection);
Chris@17 476
Chris@17 477 if (closeToLeft) {
Chris@17 478 m_selectionStartFrame = selection.getEndFrame();
Chris@17 479 } else {
Chris@17 480 m_selectionStartFrame = selection.getStartFrame();
Chris@17 481 }
Chris@17 482
Chris@17 483 m_manager->setInProgressSelection(selection, false);
Chris@17 484 m_resizing = true;
Chris@13 485
Chris@17 486 } else {
Chris@17 487
Chris@20 488 int mouseFrame = getFrameForX(e->x());
Chris@17 489 size_t resolution = 1;
Chris@17 490 int snapFrame = mouseFrame;
Chris@17 491
Chris@17 492 Layer *layer = getSelectedLayer();
Chris@17 493 if (layer) {
Chris@44 494 layer->snapToFeatureFrame(this, snapFrame,
Chris@44 495 resolution, Layer::SnapLeft);
Chris@17 496 }
Chris@17 497
Chris@17 498 if (snapFrame < 0) snapFrame = 0;
Chris@17 499 m_selectionStartFrame = snapFrame;
Chris@17 500 if (m_manager) {
Chris@17 501 m_manager->setInProgressSelection(Selection(snapFrame,
Chris@17 502 snapFrame + resolution),
Chris@17 503 !m_ctrlPressed);
Chris@17 504 }
Chris@17 505
Chris@17 506 m_resizing = false;
Chris@17 507 }
Chris@17 508
Chris@17 509 update();
Chris@17 510
Chris@17 511 } else if (mode == ViewManager::DrawMode) {
Chris@17 512
Chris@13 513 Layer *layer = getSelectedLayer();
Chris@23 514 if (layer && layer->isLayerEditable()) {
Chris@44 515 layer->drawStart(this, e);
Chris@13 516 }
Chris@18 517
Chris@18 518 } else if (mode == ViewManager::EditMode) {
Chris@18 519
Chris@42 520 if (!editSelectionStart(e)) {
Chris@42 521 Layer *layer = getSelectedLayer();
Chris@42 522 if (layer && layer->isLayerEditable()) {
Chris@44 523 layer->editStart(this, e);
Chris@42 524 }
Chris@18 525 }
Chris@13 526 }
Chris@0 527
Chris@0 528 emit paneInteractedWith();
Chris@0 529 }
Chris@0 530
Chris@0 531 void
Chris@0 532 Pane::mouseReleaseEvent(QMouseEvent *e)
Chris@0 533 {
Chris@13 534 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 535 if (m_manager) mode = m_manager->getToolMode();
Chris@13 536
Chris@0 537 if (m_clickedInRange) {
Chris@0 538 mouseMoveEvent(e);
Chris@0 539 }
Chris@0 540
Chris@17 541 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@17 542
Chris@17 543 m_navigating = false;
Chris@17 544
Chris@17 545 if (mode != ViewManager::NavigateMode) {
Chris@17 546 // restore cursor
Chris@17 547 toolModeChanged();
Chris@17 548 }
Chris@0 549
Chris@13 550 if (m_shiftPressed) {
Chris@0 551
Chris@13 552 int x0 = std::min(m_clickPos.x(), m_mousePos.x());
Chris@13 553 int x1 = std::max(m_clickPos.x(), m_mousePos.x());
Chris@13 554 int w = x1 - x0;
Chris@13 555
Chris@20 556 long newStartFrame = getFrameForX(x0);
Chris@13 557
Chris@20 558 long visibleFrames = getEndFrame() - getStartFrame();
Chris@20 559 if (newStartFrame <= -visibleFrames) {
Chris@20 560 newStartFrame = -visibleFrames + 1;
Chris@13 561 }
Chris@13 562
Chris@13 563 if (newStartFrame >= long(getModelsEndFrame())) {
Chris@13 564 newStartFrame = getModelsEndFrame() - 1;
Chris@13 565 }
Chris@13 566
Chris@13 567 float ratio = float(w) / float(width());
Chris@13 568 // std::cerr << "ratio: " << ratio << std::endl;
Chris@13 569 size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio);
Chris@13 570 if (newZoomLevel < 1) newZoomLevel = 1;
Chris@13 571
Chris@13 572 // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl;
Chris@13 573 setZoomLevel(getZoomConstraintBlockSize(newZoomLevel));
Chris@13 574 setStartFrame(newStartFrame);
Chris@13 575
Chris@13 576 //cerr << "mouseReleaseEvent: start frame now " << m_startFrame << endl;
Chris@13 577 // update();
Chris@0 578 }
Chris@0 579
Chris@13 580 } else if (mode == ViewManager::SelectMode) {
Chris@13 581
Chris@13 582 if (m_manager && m_manager->haveInProgressSelection()) {
Chris@13 583
Chris@13 584 bool exclusive;
Chris@13 585 Selection selection = m_manager->getInProgressSelection(exclusive);
Chris@13 586
Chris@13 587 if (selection.getEndFrame() < selection.getStartFrame() + 2) {
Chris@13 588 selection = Selection();
Chris@13 589 }
Chris@13 590
Chris@13 591 m_manager->clearInProgressSelection();
Chris@13 592
Chris@13 593 if (exclusive) {
Chris@13 594 m_manager->setSelection(selection);
Chris@13 595 } else {
Chris@13 596 m_manager->addSelection(selection);
Chris@13 597 }
Chris@0 598 }
Chris@13 599
Chris@13 600 update();
Chris@17 601
Chris@17 602 } else if (mode == ViewManager::DrawMode) {
Chris@17 603
Chris@17 604 Layer *layer = getSelectedLayer();
Chris@23 605 if (layer && layer->isLayerEditable()) {
Chris@44 606 layer->drawEnd(this, e);
Chris@17 607 update();
Chris@17 608 }
Chris@18 609
Chris@18 610 } else if (mode == ViewManager::EditMode) {
Chris@18 611
Chris@42 612 if (!editSelectionEnd(e)) {
Chris@42 613 Layer *layer = getSelectedLayer();
Chris@42 614 if (layer && layer->isLayerEditable()) {
Chris@44 615 layer->editEnd(this, e);
Chris@42 616 update();
Chris@42 617 }
Chris@18 618 }
Chris@17 619 }
Chris@0 620
Chris@0 621 m_clickedInRange = false;
Chris@0 622
Chris@0 623 emit paneInteractedWith();
Chris@0 624 }
Chris@0 625
Chris@0 626 void
Chris@0 627 Pane::mouseMoveEvent(QMouseEvent *e)
Chris@0 628 {
Chris@13 629 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@13 630 if (m_manager) mode = m_manager->getToolMode();
Chris@13 631
Chris@28 632 QPoint prevPoint = m_identifyPoint;
Chris@28 633 m_identifyPoint = e->pos();
Chris@28 634
Chris@0 635 if (!m_clickedInRange) {
Chris@0 636
Chris@17 637 if (mode == ViewManager::SelectMode) {
Chris@17 638 bool closeToLeft = false, closeToRight = false;
Chris@17 639 getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@17 640 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@17 641 setCursor(Qt::SizeHorCursor);
Chris@17 642 } else {
Chris@17 643 setCursor(Qt::ArrowCursor);
Chris@17 644 }
Chris@17 645 }
Chris@0 646
Chris@35 647 //!!! if (mode != ViewManager::DrawMode) {
Chris@0 648
Chris@55 649 if (getSelectedLayer()) {
Chris@55 650
Chris@17 651 bool previouslyIdentifying = m_identifyFeatures;
Chris@17 652 m_identifyFeatures = true;
Chris@17 653
Chris@17 654 if (m_identifyFeatures != previouslyIdentifying ||
Chris@17 655 m_identifyPoint != prevPoint) {
Chris@17 656 update();
Chris@17 657 }
Chris@55 658 }
Chris@55 659
Chris@35 660 // }
Chris@0 661
Chris@13 662 return;
Chris@13 663 }
Chris@0 664
Chris@17 665 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@0 666
Chris@13 667 if (m_shiftPressed) {
Chris@0 668
Chris@13 669 m_mousePos = e->pos();
Chris@13 670 update();
Chris@0 671
Chris@0 672 } else {
Chris@13 673
Chris@20 674 long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x());
Chris@20 675
Chris@13 676 size_t newCentreFrame = m_dragCentreFrame;
Chris@13 677
Chris@13 678 if (frameOff < 0) {
Chris@13 679 newCentreFrame -= frameOff;
Chris@13 680 } else if (newCentreFrame >= size_t(frameOff)) {
Chris@13 681 newCentreFrame -= frameOff;
Chris@13 682 } else {
Chris@13 683 newCentreFrame = 0;
Chris@13 684 }
Chris@13 685
Chris@13 686 if (newCentreFrame >= getModelsEndFrame()) {
Chris@13 687 newCentreFrame = getModelsEndFrame();
Chris@13 688 if (newCentreFrame > 0) --newCentreFrame;
Chris@13 689 }
Chris@20 690
Chris@20 691 if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) {
Chris@13 692 setCentreFrame(newCentreFrame);
Chris@13 693 }
Chris@0 694 }
Chris@0 695
Chris@13 696 } else if (mode == ViewManager::SelectMode) {
Chris@13 697
Chris@20 698 int mouseFrame = getFrameForX(e->x());
Chris@13 699 size_t resolution = 1;
Chris@13 700 int snapFrameLeft = mouseFrame;
Chris@13 701 int snapFrameRight = mouseFrame;
Chris@13 702
Chris@13 703 Layer *layer = getSelectedLayer();
Chris@13 704 if (layer) {
Chris@44 705 layer->snapToFeatureFrame(this, snapFrameLeft,
Chris@44 706 resolution, Layer::SnapLeft);
Chris@44 707 layer->snapToFeatureFrame(this, snapFrameRight,
Chris@44 708 resolution, Layer::SnapRight);
Chris@13 709 }
Chris@13 710
Chris@37 711 // std::cerr << "snap: frame = " << mouseFrame << ", start frame = " << m_selectionStartFrame << ", left = " << snapFrameLeft << ", right = " << snapFrameRight << std::endl;
Chris@28 712
Chris@13 713 if (snapFrameLeft < 0) snapFrameLeft = 0;
Chris@13 714 if (snapFrameRight < 0) snapFrameRight = 0;
Chris@13 715
Chris@13 716 size_t min, max;
Chris@13 717
Chris@13 718 if (m_selectionStartFrame > snapFrameLeft) {
Chris@13 719 min = snapFrameLeft;
Chris@13 720 max = m_selectionStartFrame;
Chris@13 721 } else if (snapFrameRight > m_selectionStartFrame) {
Chris@13 722 min = m_selectionStartFrame;
Chris@13 723 max = snapFrameRight;
Chris@13 724 } else {
Chris@13 725 min = snapFrameLeft;
Chris@13 726 max = snapFrameRight;
Chris@0 727 }
Chris@0 728
Chris@13 729 if (m_manager) {
Chris@13 730 m_manager->setInProgressSelection(Selection(min, max),
Chris@17 731 !m_resizing && !m_ctrlPressed);
Chris@0 732 }
Chris@15 733
Chris@15 734 bool doScroll = false;
Chris@15 735 if (!m_manager) doScroll = true;
Chris@15 736 if (!m_manager->isPlaying()) doScroll = true;
Chris@15 737 if (m_followPlay != PlaybackScrollContinuous) doScroll = true;
Chris@15 738
Chris@15 739 if (doScroll) {
Chris@13 740 int offset = mouseFrame - getStartFrame();
Chris@13 741 int available = getEndFrame() - getStartFrame();
Chris@15 742 if (offset >= available * 0.95) {
Chris@15 743 int move = int(offset - available * 0.95) + 1;
Chris@14 744 setCentreFrame(m_centreFrame + move);
Chris@15 745 } else if (offset <= available * 0.10) {
Chris@15 746 int move = int(available * 0.10 - offset) + 1;
Chris@14 747 if (m_centreFrame > move) {
Chris@14 748 setCentreFrame(m_centreFrame - move);
Chris@14 749 } else {
Chris@14 750 setCentreFrame(0);
Chris@14 751 }
Chris@13 752 }
Chris@13 753 }
Chris@13 754
Chris@13 755 update();
Chris@17 756
Chris@17 757 } else if (mode == ViewManager::DrawMode) {
Chris@17 758
Chris@17 759 Layer *layer = getSelectedLayer();
Chris@23 760 if (layer && layer->isLayerEditable()) {
Chris@44 761 layer->drawDrag(this, e);
Chris@17 762 }
Chris@18 763
Chris@18 764 } else if (mode == ViewManager::EditMode) {
Chris@18 765
Chris@42 766 if (!editSelectionDrag(e)) {
Chris@42 767 Layer *layer = getSelectedLayer();
Chris@42 768 if (layer && layer->isLayerEditable()) {
Chris@44 769 layer->editDrag(this, e);
Chris@42 770 }
Chris@18 771 }
Chris@0 772 }
Chris@0 773 }
Chris@0 774
Chris@0 775 void
Chris@0 776 Pane::mouseDoubleClickEvent(QMouseEvent *e)
Chris@0 777 {
Chris@0 778 std::cerr << "mouseDoubleClickEvent" << std::endl;
Chris@36 779
Chris@36 780 m_clickPos = e->pos();
Chris@36 781 m_clickedInRange = true;
Chris@36 782 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@36 783 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@36 784
Chris@36 785 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@36 786 if (m_manager) mode = m_manager->getToolMode();
Chris@36 787
Chris@36 788 if (mode == ViewManager::EditMode) {
Chris@36 789
Chris@36 790 Layer *layer = getSelectedLayer();
Chris@36 791 if (layer && layer->isLayerEditable()) {
Chris@44 792 layer->editOpen(this, e);
Chris@36 793 }
Chris@36 794 }
Chris@0 795 }
Chris@0 796
Chris@0 797 void
Chris@0 798 Pane::leaveEvent(QEvent *)
Chris@0 799 {
Chris@0 800 bool previouslyIdentifying = m_identifyFeatures;
Chris@0 801 m_identifyFeatures = false;
Chris@0 802 if (previouslyIdentifying) update();
Chris@0 803 }
Chris@0 804
Chris@0 805 void
Chris@0 806 Pane::wheelEvent(QWheelEvent *e)
Chris@0 807 {
Chris@0 808 //std::cerr << "wheelEvent, delta " << e->delta() << std::endl;
Chris@0 809
Chris@0 810 int count = e->delta();
Chris@0 811
Chris@0 812 if (count > 0) {
Chris@0 813 if (count >= 120) count /= 120;
Chris@0 814 else count = 1;
Chris@0 815 }
Chris@0 816
Chris@0 817 if (count < 0) {
Chris@0 818 if (count <= -120) count /= 120;
Chris@0 819 else count = -1;
Chris@0 820 }
Chris@17 821
Chris@17 822 if (e->modifiers() & Qt::ControlModifier) {
Chris@17 823
Chris@20 824 // Scroll left or right, rapidly
Chris@20 825
Chris@17 826 if (getStartFrame() < 0 &&
Chris@17 827 getEndFrame() >= getModelsEndFrame()) return;
Chris@17 828
Chris@17 829 long delta = ((width() / 2) * count * m_zoomLevel);
Chris@17 830
Chris@17 831 if (int(m_centreFrame) < delta) {
Chris@17 832 setCentreFrame(0);
Chris@17 833 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@17 834 setCentreFrame(getModelsEndFrame());
Chris@17 835 } else {
Chris@17 836 setCentreFrame(m_centreFrame - delta);
Chris@17 837 }
Chris@17 838
Chris@17 839 } else {
Chris@17 840
Chris@20 841 // Zoom in or out
Chris@20 842
Chris@17 843 int newZoomLevel = m_zoomLevel;
Chris@0 844
Chris@17 845 while (count > 0) {
Chris@17 846 if (newZoomLevel <= 2) {
Chris@17 847 newZoomLevel = 1;
Chris@17 848 break;
Chris@17 849 }
Chris@17 850 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@17 851 ZoomConstraint::RoundDown);
Chris@17 852 --count;
Chris@0 853 }
Chris@17 854
Chris@17 855 while (count < 0) {
Chris@17 856 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@17 857 ZoomConstraint::RoundUp);
Chris@17 858 ++count;
Chris@17 859 }
Chris@17 860
Chris@17 861 if (newZoomLevel != m_zoomLevel) {
Chris@17 862 setZoomLevel(newZoomLevel);
Chris@17 863 }
Chris@0 864 }
Chris@0 865
Chris@0 866 emit paneInteractedWith();
Chris@0 867 }
Chris@8 868
Chris@42 869 bool
Chris@42 870 Pane::editSelectionStart(QMouseEvent *e)
Chris@42 871 {
Chris@43 872 if (!m_identifyFeatures ||
Chris@43 873 !m_manager ||
Chris@43 874 m_manager->getToolMode() != ViewManager::EditMode) {
Chris@43 875 return false;
Chris@43 876 }
Chris@43 877
Chris@42 878 bool closeToLeft, closeToRight;
Chris@42 879 Selection s(getSelectionAt(e->x(), closeToLeft, closeToRight));
Chris@42 880 if (s.isEmpty()) return false;
Chris@42 881 m_editingSelection = s;
Chris@42 882 m_editingSelectionEdge = (closeToLeft ? -1 : closeToRight ? 1 : 0);
Chris@42 883 m_mousePos = e->pos();
Chris@42 884 return true;
Chris@42 885 }
Chris@42 886
Chris@42 887 bool
Chris@42 888 Pane::editSelectionDrag(QMouseEvent *e)
Chris@42 889 {
Chris@42 890 if (m_editingSelection.isEmpty()) return false;
Chris@42 891 m_mousePos = e->pos();
Chris@42 892 update();
Chris@42 893 return true;
Chris@42 894 }
Chris@42 895
Chris@42 896 bool
Chris@42 897 Pane::editSelectionEnd(QMouseEvent *e)
Chris@42 898 {
Chris@42 899 if (m_editingSelection.isEmpty()) return false;
Chris@43 900
Chris@43 901 int offset = m_mousePos.x() - m_clickPos.x();
Chris@43 902 Layer *layer = getSelectedLayer();
Chris@43 903
Chris@43 904 if (offset == 0 || !layer) {
Chris@43 905 m_editingSelection = Selection();
Chris@43 906 return true;
Chris@43 907 }
Chris@43 908
Chris@43 909 int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@43 910 int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@43 911
Chris@43 912 long f0 = getFrameForX(p0);
Chris@43 913 long f1 = getFrameForX(p1);
Chris@43 914
Chris@43 915 Selection newSelection(f0, f1);
Chris@43 916
Chris@43 917 if (m_editingSelectionEdge == 0) {
Chris@43 918
Chris@43 919 layer->moveSelection(m_editingSelection, f0);
Chris@43 920
Chris@43 921 } else {
Chris@43 922
Chris@43 923 if (m_editingSelectionEdge < 0) {
Chris@43 924 f1 = m_editingSelection.getEndFrame();
Chris@43 925 } else {
Chris@43 926 f0 = m_editingSelection.getStartFrame();
Chris@43 927 }
Chris@43 928
Chris@43 929 newSelection = Selection(f0, f1);
Chris@43 930 layer->resizeSelection(m_editingSelection, newSelection);
Chris@43 931 }
Chris@43 932
Chris@43 933 m_manager->removeSelection(m_editingSelection);
Chris@43 934 m_manager->addSelection(newSelection);
Chris@43 935
Chris@42 936 m_editingSelection = Selection();
Chris@42 937 return true;
Chris@42 938 }
Chris@42 939
Chris@13 940 void
Chris@13 941 Pane::toolModeChanged()
Chris@13 942 {
Chris@13 943 ViewManager::ToolMode mode = m_manager->getToolMode();
Chris@13 944 std::cerr << "Pane::toolModeChanged(" << mode << ")" << std::endl;
Chris@13 945
Chris@13 946 switch (mode) {
Chris@13 947
Chris@13 948 case ViewManager::NavigateMode:
Chris@13 949 setCursor(Qt::PointingHandCursor);
Chris@13 950 break;
Chris@13 951
Chris@13 952 case ViewManager::SelectMode:
Chris@13 953 setCursor(Qt::ArrowCursor);
Chris@13 954 break;
Chris@13 955
Chris@13 956 case ViewManager::EditMode:
Chris@19 957 setCursor(Qt::UpArrowCursor);
Chris@13 958 break;
Chris@13 959
Chris@13 960 case ViewManager::DrawMode:
Chris@13 961 setCursor(Qt::CrossCursor);
Chris@13 962 break;
Chris@36 963 /*
Chris@13 964 case ViewManager::TextMode:
Chris@13 965 setCursor(Qt::IBeamCursor);
Chris@13 966 break;
Chris@36 967 */
Chris@13 968 }
Chris@13 969 }
Chris@13 970
Chris@8 971 QString
Chris@8 972 Pane::toXmlString(QString indent, QString extraAttributes) const
Chris@8 973 {
Chris@8 974 return View::toXmlString
Chris@8 975 (indent,
Chris@55 976 QString("type=\"pane\" centreLineVisible=\"%1\" height=\"%2\" %3")
Chris@55 977 .arg(m_centreLineVisible).arg(height()).arg(extraAttributes));
Chris@8 978 }
Chris@8 979
Chris@0 980
Chris@0 981 #ifdef INCLUDE_MOCFILES
Chris@0 982 #include "Pane.moc.cpp"
Chris@0 983 #endif
Chris@0 984