annotate widgets/Pane.cpp @ 59:705f05ab42e3

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