annotate widgets/Pane.cpp @ 96:095916d7ed4d

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