annotate widgets/Pane.cpp @ 58:01ab51f72e84

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