annotate widgets/Pane.cpp @ 54:8dae7f6732c1

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