annotate widgets/Pane.cpp @ 72:1d176af87056

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