annotate view/Pane.cpp @ 320:984c1975f1ff

* Some tidying up to handling of alignment; add alignment status label to pane; ensure alignment when dragging with mouse as well as when playing
author Chris Cannam
date Thu, 25 Oct 2007 14:32:23 +0000
parents c0b9eec70639
children 4f4f38a11cd2
rev   line source
Chris@127 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@127 2
Chris@127 3 /*
Chris@127 4 Sonic Visualiser
Chris@127 5 An audio file viewer and annotation editor.
Chris@127 6 Centre for Digital Music, Queen Mary, University of London.
Chris@264 7 This file copyright 2006-2007 Chris Cannam and QMUL.
Chris@127 8
Chris@127 9 This program is free software; you can redistribute it and/or
Chris@127 10 modify it under the terms of the GNU General Public License as
Chris@127 11 published by the Free Software Foundation; either version 2 of the
Chris@127 12 License, or (at your option) any later version. See the file
Chris@127 13 COPYING included with this distribution for more information.
Chris@127 14 */
Chris@127 15
Chris@128 16 #include "Pane.h"
Chris@128 17 #include "layer/Layer.h"
Chris@128 18 #include "data/model/Model.h"
Chris@127 19 #include "base/ZoomConstraint.h"
Chris@127 20 #include "base/RealTime.h"
Chris@127 21 #include "base/Profiler.h"
Chris@128 22 #include "ViewManager.h"
Chris@127 23 #include "base/CommandHistory.h"
Chris@294 24 #include "base/TextAbbrev.h"
Chris@127 25 #include "layer/WaveformLayer.h"
Chris@127 26
Chris@127 27 #include <QPaintEvent>
Chris@127 28 #include <QPainter>
Chris@257 29 #include <QBitmap>
Chris@312 30 #include <QDragEnterEvent>
Chris@312 31 #include <QDropEvent>
Chris@257 32 #include <QCursor>
Chris@316 33 #include <QTextStream>
Chris@316 34
Chris@127 35 #include <iostream>
Chris@127 36 #include <cmath>
Chris@127 37
Chris@133 38 //!!! for HUD -- pull out into a separate class
Chris@133 39 #include <QFrame>
Chris@133 40 #include <QGridLayout>
Chris@133 41 #include <QPushButton>
Chris@133 42 #include "widgets/Thumbwheel.h"
Chris@172 43 #include "widgets/Panner.h"
Chris@188 44 #include "widgets/RangeInputDialog.h"
Chris@189 45 #include "widgets/NotifyingPushButton.h"
Chris@133 46
Chris@282 47 #include "widgets/KeyReference.h" //!!! should probably split KeyReference into a data class in base and another that shows the widget
Chris@282 48
Chris@127 49 using std::cerr;
Chris@127 50 using std::endl;
Chris@127 51
Chris@267 52 QCursor *Pane::m_measureCursor1 = 0;
Chris@267 53 QCursor *Pane::m_measureCursor2 = 0;
Chris@262 54
Chris@127 55 Pane::Pane(QWidget *w) :
Chris@127 56 View(w, true),
Chris@127 57 m_identifyFeatures(false),
Chris@127 58 m_clickedInRange(false),
Chris@127 59 m_shiftPressed(false),
Chris@127 60 m_ctrlPressed(false),
Chris@127 61 m_navigating(false),
Chris@127 62 m_resizing(false),
Chris@133 63 m_centreLineVisible(true),
Chris@222 64 m_scaleWidth(0),
Chris@237 65 m_headsUpDisplay(0),
Chris@237 66 m_vpan(0),
Chris@237 67 m_hthumb(0),
Chris@237 68 m_vthumb(0),
Chris@290 69 m_reset(0),
Chris@290 70 m_mouseInWidget(false)
Chris@127 71 {
Chris@127 72 setObjectName("Pane");
Chris@127 73 setMouseTracking(true);
Chris@312 74 setAcceptDrops(true);
Chris@133 75
Chris@133 76 updateHeadsUpDisplay();
Chris@133 77 }
Chris@133 78
Chris@133 79 void
Chris@133 80 Pane::updateHeadsUpDisplay()
Chris@133 81 {
Chris@187 82 Profiler profiler("Pane::updateHeadsUpDisplay", true);
Chris@187 83
Chris@192 84 if (!isVisible()) return;
Chris@192 85
Chris@132 86 /*
Chris@132 87 int count = 0;
Chris@132 88 int currentLevel = 1;
Chris@132 89 int level = 1;
Chris@132 90 while (true) {
Chris@132 91 if (getZoomLevel() == level) currentLevel = count;
Chris@132 92 int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@132 93 ZoomConstraint::RoundUp);
Chris@132 94 if (newLevel == level) break;
Chris@132 95 if (newLevel == 131072) break; //!!! just because
Chris@132 96 level = newLevel;
Chris@132 97 ++count;
Chris@132 98 }
Chris@132 99
Chris@132 100 std::cerr << "Have " << count+1 << " zoom levels" << std::endl;
Chris@132 101 */
Chris@133 102
Chris@188 103 Layer *layer = 0;
Chris@188 104 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@188 105
Chris@133 106 if (!m_headsUpDisplay) {
Chris@133 107
Chris@133 108 m_headsUpDisplay = new QFrame(this);
Chris@133 109
Chris@133 110 QGridLayout *layout = new QGridLayout;
Chris@133 111 layout->setMargin(0);
Chris@133 112 layout->setSpacing(0);
Chris@133 113 m_headsUpDisplay->setLayout(layout);
Chris@133 114
Chris@133 115 m_hthumb = new Thumbwheel(Qt::Horizontal);
Chris@187 116 m_hthumb->setObjectName(tr("Horizontal Zoom"));
Chris@260 117 m_hthumb->setCursor(Qt::ArrowCursor);
Chris@173 118 layout->addWidget(m_hthumb, 1, 0, 1, 2);
Chris@133 119 m_hthumb->setFixedWidth(70);
Chris@133 120 m_hthumb->setFixedHeight(16);
Chris@133 121 m_hthumb->setDefaultValue(0);
Chris@165 122 m_hthumb->setSpeed(0.6);
Chris@133 123 connect(m_hthumb, SIGNAL(valueChanged(int)), this,
Chris@133 124 SLOT(horizontalThumbwheelMoved(int)));
Chris@189 125 connect(m_hthumb, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189 126 connect(m_hthumb, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@172 127
Chris@172 128 m_vpan = new Panner;
Chris@260 129 m_vpan->setCursor(Qt::ArrowCursor);
Chris@172 130 layout->addWidget(m_vpan, 0, 1);
Chris@173 131 m_vpan->setFixedWidth(12);
Chris@172 132 m_vpan->setFixedHeight(70);
Chris@174 133 m_vpan->setAlpha(80, 130);
Chris@174 134 connect(m_vpan, SIGNAL(rectExtentsChanged(float, float, float, float)),
Chris@174 135 this, SLOT(verticalPannerMoved(float, float, float, float)));
Chris@188 136 connect(m_vpan, SIGNAL(doubleClicked()),
Chris@188 137 this, SLOT(editVerticalPannerExtents()));
Chris@189 138 connect(m_vpan, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189 139 connect(m_vpan, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@172 140
Chris@133 141 m_vthumb = new Thumbwheel(Qt::Vertical);
Chris@187 142 m_vthumb->setObjectName(tr("Vertical Zoom"));
Chris@260 143 m_vthumb->setCursor(Qt::ArrowCursor);
Chris@172 144 layout->addWidget(m_vthumb, 0, 2);
Chris@133 145 m_vthumb->setFixedWidth(16);
Chris@133 146 m_vthumb->setFixedHeight(70);
Chris@133 147 connect(m_vthumb, SIGNAL(valueChanged(int)), this,
Chris@133 148 SLOT(verticalThumbwheelMoved(int)));
Chris@189 149 connect(m_vthumb, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189 150 connect(m_vthumb, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@133 151
Chris@188 152 if (layer) {
Chris@188 153 RangeMapper *rm = layer->getNewVerticalZoomRangeMapper();
Chris@188 154 if (rm) m_vthumb->setRangeMapper(rm);
Chris@188 155 }
Chris@188 156
Chris@189 157 m_reset = new NotifyingPushButton;
Chris@260 158 m_reset->setCursor(Qt::ArrowCursor);
Chris@189 159 m_reset->setFixedHeight(16);
Chris@189 160 m_reset->setFixedWidth(16);
Chris@189 161 layout->addWidget(m_reset, 1, 2);
Chris@189 162 connect(m_reset, SIGNAL(clicked()), m_hthumb, SLOT(resetToDefault()));
Chris@189 163 connect(m_reset, SIGNAL(clicked()), m_vthumb, SLOT(resetToDefault()));
Chris@189 164 connect(m_reset, SIGNAL(clicked()), m_vpan, SLOT(resetToDefault()));
Chris@189 165 connect(m_reset, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189 166 connect(m_reset, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@133 167 }
Chris@133 168
Chris@133 169 int count = 0;
Chris@133 170 int current = 0;
Chris@133 171 int level = 1;
Chris@133 172
Chris@137 173 //!!! pull out into function (presumably in View)
Chris@137 174 bool haveConstraint = false;
Chris@137 175 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end();
Chris@137 176 ++i) {
Chris@137 177 if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) {
Chris@137 178 haveConstraint = true;
Chris@137 179 break;
Chris@137 180 }
Chris@137 181 }
Chris@137 182
Chris@137 183 if (haveConstraint) {
Chris@137 184 while (true) {
Chris@137 185 if (getZoomLevel() == level) current = count;
Chris@137 186 int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@137 187 ZoomConstraint::RoundUp);
Chris@137 188 if (newLevel == level) break;
Chris@137 189 level = newLevel;
Chris@137 190 if (++count == 50) break;
Chris@137 191 }
Chris@137 192 } else {
Chris@137 193 // if we have no particular constraints, we can really spread out
Chris@137 194 while (true) {
Chris@137 195 if (getZoomLevel() >= level) current = count;
Chris@137 196 int step = level / 10;
Chris@137 197 int pwr = 0;
Chris@137 198 while (step > 0) {
Chris@137 199 ++pwr;
Chris@137 200 step /= 2;
Chris@137 201 }
Chris@137 202 step = 1;
Chris@137 203 while (pwr > 0) {
Chris@137 204 step *= 2;
Chris@137 205 --pwr;
Chris@137 206 }
Chris@154 207 // std::cerr << level << std::endl;
Chris@137 208 level += step;
Chris@137 209 if (++count == 100 || level > 262144) break;
Chris@137 210 }
Chris@133 211 }
Chris@133 212
Chris@133 213 // std::cerr << "Have " << count << " zoom levels" << std::endl;
Chris@133 214
Chris@133 215 m_hthumb->setMinimumValue(0);
Chris@133 216 m_hthumb->setMaximumValue(count);
Chris@133 217 m_hthumb->setValue(count - current);
Chris@133 218
Chris@133 219 // std::cerr << "set value to " << count-current << std::endl;
Chris@133 220
Chris@133 221 // std::cerr << "default value is " << m_hthumb->getDefaultValue() << std::endl;
Chris@133 222
Chris@133 223 if (count != 50 && m_hthumb->getDefaultValue() == 0) {
Chris@133 224 m_hthumb->setDefaultValue(count - current);
Chris@133 225 // std::cerr << "set default value to " << m_hthumb->getDefaultValue() << std::endl;
Chris@133 226 }
Chris@133 227
Chris@204 228 bool haveVThumb = false;
Chris@204 229
Chris@133 230 if (layer) {
Chris@133 231 int defaultStep = 0;
Chris@133 232 int max = layer->getVerticalZoomSteps(defaultStep);
Chris@133 233 if (max == 0) {
Chris@133 234 m_vthumb->hide();
Chris@133 235 } else {
Chris@204 236 haveVThumb = true;
Chris@133 237 m_vthumb->show();
Chris@187 238 m_vthumb->blockSignals(true);
Chris@133 239 m_vthumb->setMinimumValue(0);
Chris@133 240 m_vthumb->setMaximumValue(max);
Chris@133 241 m_vthumb->setDefaultValue(defaultStep);
Chris@133 242 m_vthumb->setValue(layer->getCurrentVerticalZoomStep());
Chris@187 243 m_vthumb->blockSignals(false);
Chris@135 244
Chris@205 245 // std::cerr << "Vertical thumbwheel: min 0, max " << max
Chris@205 246 // << ", default " << defaultStep << ", value "
Chris@205 247 // << m_vthumb->getValue() << std::endl;
Chris@135 248
Chris@133 249 }
Chris@133 250 }
Chris@133 251
Chris@174 252 updateVerticalPanner();
Chris@174 253
Chris@133 254 if (m_manager && m_manager->getZoomWheelsEnabled() &&
Chris@133 255 width() > 120 && height() > 100) {
Chris@165 256 if (!m_headsUpDisplay->isVisible()) {
Chris@165 257 m_headsUpDisplay->show();
Chris@165 258 }
Chris@204 259 if (haveVThumb) {
Chris@204 260 m_headsUpDisplay->setFixedHeight(m_vthumb->height() + m_hthumb->height());
Chris@133 261 m_headsUpDisplay->move(width() - 86, height() - 86);
Chris@133 262 } else {
Chris@204 263 m_headsUpDisplay->setFixedHeight(m_hthumb->height());
Chris@204 264 m_headsUpDisplay->move(width() - 86, height() - 16);
Chris@133 265 }
Chris@133 266 } else {
Chris@133 267 m_headsUpDisplay->hide();
Chris@133 268 }
Chris@127 269 }
Chris@127 270
Chris@174 271 void
Chris@174 272 Pane::updateVerticalPanner()
Chris@174 273 {
Chris@174 274 if (!m_vpan || !m_manager || !m_manager->getZoomWheelsEnabled()) return;
Chris@174 275
Chris@208 276 // In principle we should show or hide the panner on the basis of
Chris@208 277 // whether the top layer has adjustable display extents, and we do
Chris@208 278 // that below. However, we have no basis for layout of the panner
Chris@208 279 // if the vertical scroll wheel is not also present. So if we
Chris@208 280 // have no vertical scroll wheel, we should remove the panner as
Chris@208 281 // well. Ideally any layer that implements display extents should
Chris@208 282 // implement vertical zoom steps as well, but they don't all at
Chris@208 283 // the moment.
Chris@208 284
Chris@208 285 Layer *layer = 0;
Chris@208 286 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@208 287 int discard;
Chris@208 288 if (layer && layer->getVerticalZoomSteps(discard) == 0) {
Chris@208 289 m_vpan->hide();
Chris@208 290 return;
Chris@208 291 }
Chris@208 292
Chris@174 293 float vmin, vmax, dmin, dmax;
Chris@174 294 if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax) && vmax != vmin) {
Chris@174 295 float y0 = (dmin - vmin) / (vmax - vmin);
Chris@174 296 float y1 = (dmax - vmin) / (vmax - vmin);
Chris@174 297 m_vpan->blockSignals(true);
Chris@174 298 m_vpan->setRectExtents(0, 1.0 - y1, 1, y1 - y0);
Chris@174 299 m_vpan->blockSignals(false);
Chris@174 300 m_vpan->show();
Chris@174 301 } else {
Chris@174 302 m_vpan->hide();
Chris@174 303 }
Chris@174 304 }
Chris@174 305
Chris@127 306 bool
Chris@127 307 Pane::shouldIlluminateLocalFeatures(const Layer *layer, QPoint &pos) const
Chris@127 308 {
Chris@127 309 QPoint discard;
Chris@127 310 bool b0, b1;
Chris@127 311
Chris@262 312 if (m_manager && m_manager->getToolMode() == ViewManager::MeasureMode) {
Chris@262 313 return false;
Chris@262 314 }
Chris@262 315
Chris@127 316 if (layer == getSelectedLayer() &&
Chris@127 317 !shouldIlluminateLocalSelection(discard, b0, b1)) {
Chris@127 318
Chris@127 319 pos = m_identifyPoint;
Chris@127 320 return m_identifyFeatures;
Chris@127 321 }
Chris@127 322
Chris@127 323 return false;
Chris@127 324 }
Chris@127 325
Chris@127 326 bool
Chris@127 327 Pane::shouldIlluminateLocalSelection(QPoint &pos,
Chris@127 328 bool &closeToLeft,
Chris@127 329 bool &closeToRight) const
Chris@127 330 {
Chris@127 331 if (m_identifyFeatures &&
Chris@127 332 m_manager &&
Chris@127 333 m_manager->getToolMode() == ViewManager::EditMode &&
Chris@127 334 !m_manager->getSelections().empty() &&
Chris@127 335 !selectionIsBeingEdited()) {
Chris@127 336
Chris@127 337 Selection s(getSelectionAt(m_identifyPoint.x(),
Chris@127 338 closeToLeft, closeToRight));
Chris@127 339
Chris@127 340 if (!s.isEmpty()) {
Chris@127 341 if (getSelectedLayer() && getSelectedLayer()->isLayerEditable()) {
Chris@127 342
Chris@127 343 pos = m_identifyPoint;
Chris@127 344 return true;
Chris@127 345 }
Chris@127 346 }
Chris@127 347 }
Chris@127 348
Chris@127 349 return false;
Chris@127 350 }
Chris@127 351
Chris@127 352 bool
Chris@127 353 Pane::selectionIsBeingEdited() const
Chris@127 354 {
Chris@127 355 if (!m_editingSelection.isEmpty()) {
Chris@127 356 if (m_mousePos != m_clickPos &&
Chris@127 357 getFrameForX(m_mousePos.x()) != getFrameForX(m_clickPos.x())) {
Chris@127 358 return true;
Chris@127 359 }
Chris@127 360 }
Chris@127 361 return false;
Chris@127 362 }
Chris@127 363
Chris@127 364 void
Chris@127 365 Pane::setCentreLineVisible(bool visible)
Chris@127 366 {
Chris@127 367 m_centreLineVisible = visible;
Chris@127 368 update();
Chris@127 369 }
Chris@127 370
Chris@127 371 void
Chris@127 372 Pane::paintEvent(QPaintEvent *e)
Chris@127 373 {
Chris@127 374 // Profiler profiler("Pane::paintEvent", true);
Chris@127 375
Chris@127 376 QPainter paint;
Chris@127 377
Chris@127 378 QRect r(rect());
Chris@261 379 if (e) r = e->rect();
Chris@127 380
Chris@127 381 View::paintEvent(e);
Chris@127 382
Chris@127 383 paint.begin(this);
Chris@127 384
Chris@261 385 if (e) paint.setClipRect(r);
Chris@127 386
Chris@259 387 ViewManager::ToolMode toolMode = m_manager->getToolMode();
Chris@259 388
Chris@127 389 if (m_manager &&
Chris@290 390 // !m_manager->isPlaying() &&
Chris@290 391 m_mouseInWidget &&
Chris@259 392 toolMode == ViewManager::MeasureMode) {
Chris@127 393
Chris@127 394 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@127 395 --vi;
Chris@127 396
Chris@127 397 std::vector<QRect> crosshairExtents;
Chris@127 398
Chris@127 399 if ((*vi)->getCrosshairExtents(this, paint, m_identifyPoint,
Chris@127 400 crosshairExtents)) {
Chris@127 401 (*vi)->paintCrosshairs(this, paint, m_identifyPoint);
Chris@127 402 break;
Chris@127 403 } else if ((*vi)->isLayerOpaque()) {
Chris@127 404 break;
Chris@127 405 }
Chris@127 406 }
Chris@127 407 }
Chris@127 408
Chris@268 409 Layer *topLayer = getTopLayer();
Chris@277 410 bool haveSomeTimeXAxis = false;
Chris@268 411
Chris@258 412 const Model *waveformModel = 0; // just for reporting purposes
Chris@127 413 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@127 414 --vi;
Chris@277 415 if (!haveSomeTimeXAxis && (*vi)->hasTimeXAxis()) {
Chris@277 416 haveSomeTimeXAxis = true;
Chris@277 417 }
Chris@127 418 if (dynamic_cast<WaveformLayer *>(*vi)) {
Chris@127 419 waveformModel = (*vi)->getModel();
Chris@127 420 }
Chris@277 421 if (waveformModel && haveSomeTimeXAxis) break;
Chris@258 422 }
Chris@127 423
Chris@261 424 m_scaleWidth = 0;
Chris@261 425
Chris@261 426 if (m_manager && m_manager->shouldShowVerticalScale() && topLayer) {
Chris@261 427 drawVerticalScale(r, topLayer, paint);
Chris@261 428 }
Chris@261 429
Chris@261 430 if (m_identifyFeatures && topLayer) {
Chris@261 431 drawFeatureDescription(topLayer, paint);
Chris@261 432 }
Chris@261 433
Chris@261 434 int sampleRate = getModelsSampleRate();
Chris@261 435 paint.setBrush(Qt::NoBrush);
Chris@261 436
Chris@261 437 if (m_centreLineVisible &&
Chris@261 438 m_manager &&
Chris@261 439 m_manager->shouldShowCentreLine()) {
Chris@277 440 drawCentreLine(sampleRate, paint, !haveSomeTimeXAxis);
Chris@261 441 }
Chris@261 442
Chris@261 443 paint.setPen(QColor(50, 50, 50));
Chris@261 444
Chris@261 445 if (waveformModel &&
Chris@261 446 m_manager &&
Chris@261 447 m_manager->shouldShowDuration()) {
Chris@261 448 drawDurationAndRate(r, waveformModel, sampleRate, paint);
Chris@261 449 }
Chris@261 450
Chris@320 451 if (waveformModel &&
Chris@320 452 m_manager &&
Chris@320 453 m_manager->getAlignMode()) {
Chris@320 454 drawAlignmentStatus(r, paint, waveformModel);
Chris@320 455 }
Chris@320 456
Chris@261 457 if (m_manager &&
Chris@261 458 m_manager->shouldShowLayerNames()) {
Chris@261 459 drawLayerNames(r, paint);
Chris@261 460 }
Chris@261 461
Chris@262 462 if (m_shiftPressed && m_clickedInRange &&
Chris@283 463 (toolMode == ViewManager::NavigateMode || m_navigating)) {
Chris@261 464
Chris@261 465 //!!! be nice if this looked a bit more in keeping with the
Chris@261 466 //selection block
Chris@262 467
Chris@262 468 paint.setPen(Qt::blue);
Chris@262 469 //!!! shouldn't use clickPos -- needs to use a clicked frame
Chris@262 470 paint.drawRect(m_clickPos.x(), m_clickPos.y(),
Chris@262 471 m_mousePos.x() - m_clickPos.x(),
Chris@262 472 m_mousePos.y() - m_clickPos.y());
Chris@261 473
Chris@262 474 }
Chris@261 475
Chris@266 476 if (toolMode == ViewManager::MeasureMode && topLayer) {
Chris@272 477 bool showFocus = false;
Chris@272 478 if (!m_manager || !m_manager->isPlaying()) showFocus = true;
Chris@272 479 topLayer->paintMeasurementRects(this, paint, showFocus, m_identifyPoint);
Chris@261 480 }
Chris@261 481
Chris@261 482 if (selectionIsBeingEdited()) {
Chris@261 483 drawEditingSelection(paint);
Chris@261 484 }
Chris@261 485
Chris@261 486 paint.end();
Chris@261 487 }
Chris@261 488
Chris@276 489 size_t
Chris@276 490 Pane::getVerticalScaleWidth() const
Chris@276 491 {
Chris@276 492 if (m_scaleWidth > 0) return m_scaleWidth;
Chris@276 493 else return 0;
Chris@276 494 }
Chris@276 495
Chris@261 496 void
Chris@261 497 Pane::drawVerticalScale(QRect r, Layer *topLayer, QPainter &paint)
Chris@261 498 {
Chris@258 499 Layer *scaleLayer = 0;
Chris@258 500
Chris@261 501 float min, max;
Chris@261 502 bool log;
Chris@261 503 QString unit;
Chris@258 504
Chris@261 505 // If the top layer has no scale and reports no display
Chris@261 506 // extents, but does report a unit, then the scale should be
Chris@261 507 // drawn from any underlying layer with a scale and that unit.
Chris@261 508 // If the top layer has no scale and no value extents at all,
Chris@261 509 // then the scale should be drawn from any underlying layer
Chris@261 510 // with a scale regardless of unit.
Chris@258 511
Chris@261 512 int sw = topLayer->getVerticalScaleWidth(this, paint);
Chris@258 513
Chris@261 514 if (sw > 0) {
Chris@261 515 scaleLayer = topLayer;
Chris@261 516 m_scaleWidth = sw;
Chris@258 517
Chris@261 518 } else {
Chris@258 519
Chris@261 520 bool hasDisplayExtents = topLayer->getDisplayExtents(min, max);
Chris@261 521 bool hasValueExtents = topLayer->getValueExtents(min, max, log, unit);
Chris@261 522
Chris@261 523 if (!hasDisplayExtents) {
Chris@258 524
Chris@261 525 if (!hasValueExtents) {
Chris@258 526
Chris@261 527 for (LayerList::iterator vi = m_layers.end();
Chris@261 528 vi != m_layers.begin(); ) {
Chris@261 529
Chris@261 530 --vi;
Chris@261 531
Chris@261 532 if ((*vi) == topLayer) continue;
Chris@261 533
Chris@261 534 sw = (*vi)->getVerticalScaleWidth(this, paint);
Chris@261 535
Chris@261 536 if (sw > 0) {
Chris@261 537 scaleLayer = *vi;
Chris@261 538 m_scaleWidth = sw;
Chris@261 539 break;
Chris@261 540 }
Chris@261 541 }
Chris@261 542 } else if (unit != "") { // && hasValueExtents && !hasDisplayExtents
Chris@258 543
Chris@261 544 QString requireUnit = unit;
Chris@261 545
Chris@261 546 for (LayerList::iterator vi = m_layers.end();
Chris@261 547 vi != m_layers.begin(); ) {
Chris@258 548
Chris@261 549 --vi;
Chris@258 550
Chris@261 551 if ((*vi) == topLayer) continue;
Chris@258 552
Chris@261 553 if ((*vi)->getDisplayExtents(min, max)) {
Chris@261 554
Chris@261 555 // search no further than this: if the
Chris@261 556 // scale from this layer isn't suitable,
Chris@261 557 // we'll have to draw no scale (else we'd
Chris@261 558 // risk ending up with the wrong scale)
Chris@261 559
Chris@261 560 if ((*vi)->getValueExtents(min, max, log, unit) &&
Chris@261 561 unit == requireUnit) {
Chris@261 562
Chris@261 563 sw = (*vi)->getVerticalScaleWidth(this, paint);
Chris@261 564 if (sw > 0) {
Chris@261 565 scaleLayer = *vi;
Chris@261 566 m_scaleWidth = sw;
Chris@261 567 }
Chris@258 568 }
Chris@261 569 break;
Chris@258 570 }
Chris@258 571 }
Chris@258 572 }
Chris@127 573 }
Chris@258 574 }
Chris@127 575
Chris@258 576 if (!scaleLayer) m_scaleWidth = 0;
Chris@258 577
Chris@258 578 if (m_scaleWidth > 0 && r.left() < m_scaleWidth) {
Chris@127 579
Chris@127 580 // Profiler profiler("Pane::paintEvent - painting vertical scale", true);
Chris@127 581
Chris@127 582 // std::cerr << "Pane::paintEvent: calling paint.save() in vertical scale block" << std::endl;
Chris@258 583 paint.save();
Chris@258 584
Chris@287 585 paint.setPen(getForeground());
Chris@287 586 paint.setBrush(getBackground());
Chris@258 587 paint.drawRect(0, -1, m_scaleWidth, height()+1);
Chris@258 588
Chris@258 589 paint.setBrush(Qt::NoBrush);
Chris@258 590 scaleLayer->paintVerticalScale
Chris@258 591 (this, paint, QRect(0, 0, m_scaleWidth, height()));
Chris@258 592
Chris@258 593 paint.restore();
Chris@258 594 }
Chris@261 595 }
Chris@261 596
Chris@261 597 void
Chris@261 598 Pane::drawFeatureDescription(Layer *topLayer, QPainter &paint)
Chris@261 599 {
Chris@261 600 QPoint pos = m_identifyPoint;
Chris@261 601 QString desc = topLayer->getFeatureDescription(this, pos);
Chris@261 602
Chris@261 603 if (desc != "") {
Chris@261 604
Chris@261 605 paint.save();
Chris@261 606
Chris@261 607 int tabStop =
Chris@261 608 paint.fontMetrics().width(tr("Some lengthy prefix:"));
Chris@261 609
Chris@261 610 QRect boundingRect =
Chris@261 611 paint.fontMetrics().boundingRect
Chris@261 612 (rect(),
Chris@261 613 Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs,
Chris@261 614 desc, tabStop);
Chris@261 615
Chris@261 616 if (hasLightBackground()) {
Chris@261 617 paint.setPen(Qt::NoPen);
Chris@261 618 paint.setBrush(QColor(250, 250, 250, 200));
Chris@261 619 } else {
Chris@261 620 paint.setPen(Qt::NoPen);
Chris@261 621 paint.setBrush(QColor(50, 50, 50, 200));
Chris@261 622 }
Chris@261 623
Chris@261 624 int extra = paint.fontMetrics().descent();
Chris@261 625 paint.drawRect(width() - boundingRect.width() - 10 - extra,
Chris@261 626 10 - extra,
Chris@261 627 boundingRect.width() + 2 * extra,
Chris@261 628 boundingRect.height() + extra);
Chris@261 629
Chris@261 630 if (hasLightBackground()) {
Chris@261 631 paint.setPen(QColor(150, 20, 0));
Chris@261 632 } else {
Chris@261 633 paint.setPen(QColor(255, 150, 100));
Chris@261 634 }
Chris@261 635
Chris@261 636 QTextOption option;
Chris@261 637 option.setWrapMode(QTextOption::NoWrap);
Chris@261 638 option.setAlignment(Qt::AlignRight | Qt::AlignTop);
Chris@261 639 option.setTabStop(tabStop);
Chris@261 640 paint.drawText(QRectF(width() - boundingRect.width() - 10, 10,
Chris@261 641 boundingRect.width(),
Chris@261 642 boundingRect.height()),
Chris@261 643 desc,
Chris@261 644 option);
Chris@261 645
Chris@261 646 paint.restore();
Chris@261 647 }
Chris@261 648 }
Chris@258 649
Chris@261 650 void
Chris@277 651 Pane::drawCentreLine(int sampleRate, QPainter &paint, bool omitLine)
Chris@261 652 {
Chris@261 653 int fontHeight = paint.fontMetrics().height();
Chris@261 654 int fontAscent = paint.fontMetrics().ascent();
Chris@261 655
Chris@261 656 QColor c = QColor(0, 0, 0);
Chris@261 657 if (!hasLightBackground()) {
Chris@261 658 c = QColor(240, 240, 240);
Chris@261 659 }
Chris@277 660
Chris@261 661 paint.setPen(c);
Chris@274 662 int x = width() / 2;
Chris@277 663
Chris@277 664 if (!omitLine) {
Chris@277 665 paint.drawLine(x, 0, x, height() - 1);
Chris@277 666 paint.drawLine(x-1, 1, x+1, 1);
Chris@277 667 paint.drawLine(x-2, 0, x+2, 0);
Chris@277 668 paint.drawLine(x-1, height() - 2, x+1, height() - 2);
Chris@277 669 paint.drawLine(x-2, height() - 1, x+2, height() - 1);
Chris@277 670 }
Chris@261 671
Chris@261 672 paint.setPen(QColor(50, 50, 50));
Chris@261 673
Chris@261 674 int y = height() - fontHeight + fontAscent - 6;
Chris@261 675
Chris@261 676 LayerList::iterator vi = m_layers.end();
Chris@261 677
Chris@261 678 if (vi != m_layers.begin()) {
Chris@261 679
Chris@261 680 switch ((*--vi)->getPreferredFrameCountPosition()) {
Chris@258 681
Chris@261 682 case Layer::PositionTop:
Chris@261 683 y = fontAscent + 6;
Chris@261 684 break;
Chris@258 685
Chris@261 686 case Layer::PositionMiddle:
Chris@261 687 y = (height() - fontHeight) / 2
Chris@261 688 + fontAscent;
Chris@261 689 break;
Chris@127 690
Chris@261 691 case Layer::PositionBottom:
Chris@261 692 // y already set correctly
Chris@261 693 break;
Chris@127 694 }
Chris@127 695 }
Chris@127 696
Chris@261 697 if (m_manager && m_manager->shouldShowFrameCount()) {
Chris@261 698
Chris@261 699 if (sampleRate) {
Chris@127 700
Chris@261 701 QString text(QString::fromStdString
Chris@261 702 (RealTime::frame2RealTime
Chris@261 703 (m_centreFrame, sampleRate).toText(true)));
Chris@127 704
Chris@261 705 int tw = paint.fontMetrics().width(text);
Chris@261 706 int x = width()/2 - 4 - tw;
Chris@127 707
Chris@127 708 drawVisibleText(paint, x, y, text, OutlinedText);
Chris@127 709 }
Chris@261 710
Chris@261 711 QString text = QString("%1").arg(m_centreFrame);
Chris@261 712
Chris@261 713 int x = width()/2 + 4;
Chris@261 714
Chris@261 715 drawVisibleText(paint, x, y, text, OutlinedText);
Chris@261 716 }
Chris@261 717 }
Chris@127 718
Chris@261 719 void
Chris@320 720 Pane::drawAlignmentStatus(QRect r, QPainter &paint, const Model *model)
Chris@320 721 {
Chris@320 722 const Model *reference = model->getAlignmentReference();
Chris@320 723 /*
Chris@320 724 if (!reference) {
Chris@320 725 std::cerr << "Pane[" << this << "]::drawAlignmentStatus: No reference" << std::endl;
Chris@320 726 } else if (reference == model) {
Chris@320 727 std::cerr << "Pane[" << this << "]::drawAlignmentStatus: This is the reference model" << std::endl;
Chris@320 728 } else {
Chris@320 729 std::cerr << "Pane[" << this << "]::drawAlignmentStatus: This is not the reference" << std::endl;
Chris@320 730 }
Chris@320 731 */
Chris@320 732 QString text;
Chris@320 733 int completion = 100;
Chris@320 734
Chris@320 735 if (reference == model) {
Chris@320 736 text = tr("Reference");
Chris@320 737 } else if (!reference) {
Chris@320 738 text = tr("Unaligned");
Chris@320 739 } else {
Chris@320 740 completion = model->getAlignmentCompletion();
Chris@320 741 if (completion == 0) {
Chris@320 742 text = tr("Unaligned");
Chris@320 743 } else if (completion < 100) {
Chris@320 744 text = tr("Aligning: %1%").arg(completion);
Chris@320 745 } else {
Chris@320 746 text = tr("Aligned");
Chris@320 747 }
Chris@320 748 }
Chris@320 749
Chris@320 750 int w = paint.fontMetrics().width(text), h = paint.fontMetrics().height();
Chris@320 751 if (r.top() > h + 5 || r.left() > w + m_scaleWidth + 5) return;
Chris@320 752
Chris@320 753 paint.save();
Chris@320 754 QFont font(paint.font());
Chris@320 755 font.setBold(true);
Chris@320 756 paint.setFont(font);
Chris@320 757 if (completion < 100) paint.setPen(Qt::red);
Chris@320 758
Chris@320 759 drawVisibleText(paint, m_scaleWidth + 5,
Chris@320 760 paint.fontMetrics().ascent() + 5, text, OutlinedText);
Chris@320 761
Chris@320 762 paint.restore();
Chris@320 763 }
Chris@320 764
Chris@320 765 void
Chris@320 766 Pane::modelAlignmentCompletionChanged()
Chris@320 767 {
Chris@320 768 View::modelAlignmentCompletionChanged();
Chris@320 769 update(QRect(0, 0, 300, 100));
Chris@320 770 }
Chris@320 771
Chris@320 772 void
Chris@261 773 Pane::drawLayerNames(QRect r, QPainter &paint)
Chris@261 774 {
Chris@261 775 int fontHeight = paint.fontMetrics().height();
Chris@261 776 int fontAscent = paint.fontMetrics().ascent();
Chris@127 777
Chris@300 778 int lly = height() - 6;
Chris@300 779 if (m_manager->getZoomWheelsEnabled()) {
Chris@300 780 lly -= 20;
Chris@300 781 }
Chris@300 782
Chris@300 783 if (r.y() + r.height() < lly - int(m_layers.size()) * fontHeight) {
Chris@261 784 return;
Chris@127 785 }
Chris@127 786
Chris@294 787 QStringList texts;
Chris@299 788 std::vector<QPixmap> pixmaps;
Chris@294 789 for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@294 790 texts.push_back((*i)->getLayerPresentationName());
Chris@299 791 // std::cerr << "Pane " << this << ": Layer presentation name for " << *i << ": "
Chris@299 792 // << texts[texts.size()-1].toStdString() << std::endl;
Chris@299 793 pixmaps.push_back((*i)->getLayerPresentationPixmap
Chris@299 794 (QSize(fontAscent, fontAscent)));
Chris@294 795 }
Chris@127 796
Chris@294 797 int maxTextWidth = width() / 3;
Chris@294 798 texts = TextAbbrev::abbreviate(texts, paint.fontMetrics(), maxTextWidth);
Chris@294 799
Chris@261 800 int llx = width() - maxTextWidth - 5;
Chris@261 801 if (m_manager->getZoomWheelsEnabled()) {
Chris@261 802 llx -= 36;
Chris@261 803 }
Chris@261 804
Chris@300 805 if (r.x() + r.width() >= llx - fontAscent - 3) {
Chris@261 806
Chris@261 807 for (size_t i = 0; i < texts.size(); ++i) {
Chris@299 808
Chris@299 809 // std::cerr << "Pane "<< this << ": text " << i << ": " << texts[i].toStdString() << std::endl;
Chris@261 810
Chris@261 811 if (i + 1 == texts.size()) {
Chris@287 812 paint.setPen(getForeground());
Chris@261 813 }
Chris@261 814
Chris@261 815 drawVisibleText(paint, llx,
Chris@261 816 lly - fontHeight + fontAscent,
Chris@261 817 texts[i], OutlinedText);
Chris@299 818
Chris@299 819 if (!pixmaps[i].isNull()) {
Chris@299 820 paint.drawPixmap(llx - fontAscent - 3,
Chris@299 821 lly - fontHeight + (fontHeight-fontAscent)/2,
Chris@299 822 pixmaps[i]);
Chris@299 823 }
Chris@261 824
Chris@261 825 lly -= fontHeight;
Chris@261 826 }
Chris@261 827 }
Chris@261 828 }
Chris@127 829
Chris@261 830 void
Chris@261 831 Pane::drawEditingSelection(QPainter &paint)
Chris@261 832 {
Chris@261 833 int offset = m_mousePos.x() - m_clickPos.x();
Chris@261 834 int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@261 835 int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@261 836
Chris@261 837 if (m_editingSelectionEdge < 0) {
Chris@261 838 p1 = getXForFrame(m_editingSelection.getEndFrame());
Chris@261 839 } else if (m_editingSelectionEdge > 0) {
Chris@261 840 p0 = getXForFrame(m_editingSelection.getStartFrame());
Chris@127 841 }
Chris@127 842
Chris@261 843 paint.save();
Chris@287 844 paint.setPen(QPen(getForeground(), 2));
Chris@261 845
Chris@261 846 //!!! duplicating display policy with View::drawSelections
Chris@261 847
Chris@261 848 if (m_editingSelectionEdge < 0) {
Chris@261 849 paint.drawLine(p0, 1, p1, 1);
Chris@261 850 paint.drawLine(p0, 0, p0, height());
Chris@261 851 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@261 852 } else if (m_editingSelectionEdge > 0) {
Chris@261 853 paint.drawLine(p0, 1, p1, 1);
Chris@261 854 paint.drawLine(p1, 0, p1, height());
Chris@261 855 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@261 856 } else {
Chris@261 857 paint.setBrush(Qt::NoBrush);
Chris@261 858 paint.drawRect(p0, 1, p1 - p0, height() - 2);
Chris@261 859 }
Chris@261 860 paint.restore();
Chris@261 861 }
Chris@127 862
Chris@261 863 void
Chris@261 864 Pane::drawDurationAndRate(QRect r, const Model *waveformModel,
Chris@261 865 int sampleRate, QPainter &paint)
Chris@261 866 {
Chris@261 867 int fontHeight = paint.fontMetrics().height();
Chris@261 868 int fontAscent = paint.fontMetrics().ascent();
Chris@127 869
Chris@261 870 if (r.y() + r.height() < height() - fontHeight - 6) return;
Chris@127 871
Chris@261 872 size_t modelRate = waveformModel->getSampleRate();
Chris@301 873 size_t nativeRate = waveformModel->getNativeRate();
Chris@261 874 size_t playbackRate = m_manager->getPlaybackSampleRate();
Chris@261 875 size_t outputRate = m_manager->getOutputSampleRate();
Chris@261 876
Chris@261 877 QString srNote = "";
Chris@127 878
Chris@301 879 // Show (R) for waveform models that have been resampled or will
Chris@301 880 // be resampled on playback, and (X) for waveform models that will
Chris@301 881 // be played at the wrong rate because their rate differs from the
Chris@261 882 // current playback rate (which is not necessarily that of the
Chris@261 883 // main model).
Chris@127 884
Chris@261 885 if (playbackRate != 0) {
Chris@261 886 if (modelRate == playbackRate) {
Chris@301 887 if (modelRate != outputRate || modelRate != nativeRate) {
Chris@301 888 srNote = " " + tr("(R)");
Chris@301 889 }
Chris@261 890 } else {
Chris@261 891 srNote = " " + tr("(X)");
Chris@261 892 }
Chris@127 893 }
Chris@127 894
Chris@261 895 QString desc = tr("%1 / %2Hz%3")
Chris@261 896 .arg(RealTime::frame2RealTime(waveformModel->getEndFrame(),
Chris@261 897 sampleRate)
Chris@261 898 .toText(false).c_str())
Chris@301 899 .arg(nativeRate)
Chris@261 900 .arg(srNote);
Chris@261 901
Chris@261 902 if (r.x() < m_scaleWidth + 5 + paint.fontMetrics().width(desc)) {
Chris@261 903 drawVisibleText(paint, m_scaleWidth + 5,
Chris@261 904 height() - fontHeight + fontAscent - 6,
Chris@261 905 desc, OutlinedText);
Chris@261 906 }
Chris@127 907 }
Chris@127 908
Chris@227 909 bool
Chris@229 910 Pane::render(QPainter &paint, int xorigin, size_t f0, size_t f1)
Chris@227 911 {
Chris@229 912 if (!View::render(paint, xorigin + m_scaleWidth, f0, f1)) {
Chris@227 913 return false;
Chris@227 914 }
Chris@227 915
Chris@227 916 if (m_scaleWidth > 0) {
Chris@227 917
Chris@227 918 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@227 919 --vi;
Chris@227 920
Chris@227 921 paint.save();
Chris@227 922
Chris@287 923 paint.setPen(getForeground());
Chris@287 924 paint.setBrush(getBackground());
Chris@229 925 paint.drawRect(xorigin, -1, m_scaleWidth, height()+1);
Chris@227 926
Chris@227 927 paint.setBrush(Qt::NoBrush);
Chris@227 928 (*vi)->paintVerticalScale
Chris@229 929 (this, paint, QRect(xorigin, 0, m_scaleWidth, height()));
Chris@227 930
Chris@227 931 paint.restore();
Chris@227 932 break;
Chris@227 933 }
Chris@227 934 }
Chris@227 935
Chris@227 936 return true;
Chris@227 937 }
Chris@227 938
Chris@227 939 QImage *
Chris@229 940 Pane::toNewImage(size_t f0, size_t f1)
Chris@227 941 {
Chris@227 942 size_t x0 = f0 / getZoomLevel();
Chris@227 943 size_t x1 = f1 / getZoomLevel();
Chris@227 944
Chris@227 945 QImage *image = new QImage(x1 - x0 + m_scaleWidth,
Chris@227 946 height(), QImage::Format_RGB32);
Chris@227 947
Chris@227 948 int formerScaleWidth = m_scaleWidth;
Chris@227 949
Chris@227 950 if (m_manager && m_manager->shouldShowVerticalScale()) {
Chris@227 951 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@227 952 --vi;
Chris@227 953 QPainter paint(image);
Chris@227 954 m_scaleWidth = (*vi)->getVerticalScaleWidth(this, paint);
Chris@227 955 break;
Chris@227 956 }
Chris@227 957 } else {
Chris@227 958 m_scaleWidth = 0;
Chris@227 959 }
Chris@227 960
Chris@227 961 if (m_scaleWidth != formerScaleWidth) {
Chris@227 962 delete image;
Chris@227 963 image = new QImage(x1 - x0 + m_scaleWidth,
Chris@227 964 height(), QImage::Format_RGB32);
Chris@227 965 }
Chris@227 966
Chris@227 967 QPainter *paint = new QPainter(image);
Chris@229 968 if (!render(*paint, 0, f0, f1)) {
Chris@227 969 delete paint;
Chris@227 970 delete image;
Chris@227 971 return 0;
Chris@227 972 } else {
Chris@227 973 delete paint;
Chris@227 974 return image;
Chris@227 975 }
Chris@227 976 }
Chris@227 977
Chris@229 978 QSize
Chris@229 979 Pane::getImageSize(size_t f0, size_t f1)
Chris@229 980 {
Chris@229 981 QSize s = View::getImageSize(f0, f1);
Chris@229 982 QImage *image = new QImage(100, 100, QImage::Format_RGB32);
Chris@229 983 QPainter paint(image);
Chris@229 984
Chris@229 985 int sw = 0;
Chris@229 986 if (m_manager && m_manager->shouldShowVerticalScale()) {
Chris@229 987 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@229 988 --vi;
Chris@229 989 QPainter paint(image);
Chris@229 990 sw = (*vi)->getVerticalScaleWidth(this, paint);
Chris@229 991 break;
Chris@229 992 }
Chris@229 993 }
Chris@229 994
Chris@229 995 return QSize(sw + s.width(), s.height());
Chris@229 996 }
Chris@229 997
Chris@222 998 size_t
Chris@222 999 Pane::getFirstVisibleFrame() const
Chris@222 1000 {
Chris@222 1001 long f0 = getFrameForX(m_scaleWidth);
Chris@222 1002 size_t f = View::getFirstVisibleFrame();
Chris@222 1003 if (f0 < 0 || f0 < long(f)) return f;
Chris@222 1004 return f0;
Chris@222 1005 }
Chris@222 1006
Chris@127 1007 Selection
Chris@127 1008 Pane::getSelectionAt(int x, bool &closeToLeftEdge, bool &closeToRightEdge) const
Chris@127 1009 {
Chris@127 1010 closeToLeftEdge = closeToRightEdge = false;
Chris@127 1011
Chris@127 1012 if (!m_manager) return Selection();
Chris@127 1013
Chris@127 1014 long testFrame = getFrameForX(x - 5);
Chris@127 1015 if (testFrame < 0) {
Chris@127 1016 testFrame = getFrameForX(x);
Chris@127 1017 if (testFrame < 0) return Selection();
Chris@127 1018 }
Chris@127 1019
Chris@127 1020 Selection selection = m_manager->getContainingSelection(testFrame, true);
Chris@127 1021 if (selection.isEmpty()) return selection;
Chris@127 1022
Chris@127 1023 int lx = getXForFrame(selection.getStartFrame());
Chris@127 1024 int rx = getXForFrame(selection.getEndFrame());
Chris@127 1025
Chris@127 1026 int fuzz = 2;
Chris@127 1027 if (x < lx - fuzz || x > rx + fuzz) return Selection();
Chris@127 1028
Chris@127 1029 int width = rx - lx;
Chris@127 1030 fuzz = 3;
Chris@127 1031 if (width < 12) fuzz = width / 4;
Chris@127 1032 if (fuzz < 1) fuzz = 1;
Chris@127 1033
Chris@127 1034 if (x < lx + fuzz) closeToLeftEdge = true;
Chris@127 1035 if (x > rx - fuzz) closeToRightEdge = true;
Chris@127 1036
Chris@127 1037 return selection;
Chris@127 1038 }
Chris@127 1039
Chris@174 1040 bool
Chris@174 1041 Pane::canTopLayerMoveVertical()
Chris@174 1042 {
Chris@174 1043 float vmin, vmax, dmin, dmax;
Chris@174 1044 if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) return false;
Chris@174 1045 if (dmin <= vmin && dmax >= vmax) return false;
Chris@174 1046 return true;
Chris@174 1047 }
Chris@174 1048
Chris@174 1049 bool
Chris@174 1050 Pane::getTopLayerDisplayExtents(float &vmin, float &vmax,
Chris@188 1051 float &dmin, float &dmax,
Chris@188 1052 QString *unit)
Chris@174 1053 {
Chris@268 1054 Layer *layer = getTopLayer();
Chris@174 1055 if (!layer) return false;
Chris@174 1056 bool vlog;
Chris@174 1057 QString vunit;
Chris@188 1058 bool rv = (layer->getValueExtents(vmin, vmax, vlog, vunit) &&
Chris@188 1059 layer->getDisplayExtents(dmin, dmax));
Chris@188 1060 if (unit) *unit = vunit;
Chris@188 1061 return rv;
Chris@174 1062 }
Chris@174 1063
Chris@174 1064 bool
Chris@174 1065 Pane::setTopLayerDisplayExtents(float dmin, float dmax)
Chris@174 1066 {
Chris@268 1067 Layer *layer = getTopLayer();
Chris@174 1068 if (!layer) return false;
Chris@174 1069 return layer->setDisplayExtents(dmin, dmax);
Chris@174 1070 }
Chris@174 1071
Chris@127 1072 void
Chris@282 1073 Pane::registerShortcuts(KeyReference &kr)
Chris@282 1074 {
Chris@282 1075 kr.setCategory(tr("Zoom"));
Chris@282 1076 kr.registerAlternativeShortcut(tr("Zoom In"), tr("Wheel Up"));
Chris@282 1077 kr.registerAlternativeShortcut(tr("Zoom Out"), tr("Wheel Down"));
Chris@282 1078
Chris@282 1079 kr.setCategory(tr("General Pane Mouse Actions"));
Chris@282 1080
Chris@282 1081 kr.registerShortcut(tr("Zoom"), tr("Wheel"),
Chris@282 1082 tr("Zoom in or out in time axis"));
Chris@282 1083 kr.registerShortcut(tr("Ctrl+Wheel"), tr("Scroll"),
Chris@282 1084 tr("Scroll rapidly left or right in time axis"));
Chris@282 1085 kr.registerShortcut(tr("Zoom Vertically"), tr("Shift+Wheel"),
Chris@282 1086 tr("Zoom in or out in the vertical axis"));
Chris@282 1087 kr.registerShortcut(tr("Scroll Vertically"), tr("Alt+Wheel"),
Chris@282 1088 tr("Scroll up or down in the vertical axis"));
Chris@282 1089 kr.registerShortcut(tr("Navigate"), tr("Middle"),
Chris@282 1090 tr("Click middle button and drag to navigate with any tool"));
Chris@282 1091 kr.registerShortcut(tr("Relocate"), tr("Double-Click Middle"),
Chris@282 1092 tr("Double-click middle button to relocate with any tool"));
Chris@282 1093 kr.registerShortcut(tr("Menu"), tr("Right"),
Chris@282 1094 tr("Show pane context menu"));
Chris@282 1095
Chris@282 1096 kr.setCategory(tr("Navigate Tool Mouse Actions"));
Chris@282 1097
Chris@282 1098 kr.registerShortcut(tr("Navigate"), tr("Left"),
Chris@282 1099 tr("Click left button and drag to move around"));
Chris@282 1100 kr.registerShortcut(tr("Zoom to Area"), tr("Shift+Left"),
Chris@282 1101 tr("Shift-click left button and drag to zoom to a rectangular area"));
Chris@282 1102 kr.registerShortcut(tr("Relocate"), tr("Double-Click Left"),
Chris@282 1103 tr("Double-click left button to jump to clicked location"));
Chris@282 1104 kr.registerShortcut(tr("Edit"), tr("Double-Click Left"),
Chris@282 1105 tr("Double-click left button on an item to edit it"));
Chris@282 1106
Chris@282 1107 kr.setCategory(tr("Select Tool Mouse Actions"));
Chris@282 1108 kr.registerShortcut(tr("Select"), tr("Left"),
Chris@282 1109 tr("Click left button and drag to select region; drag region edge to resize"));
Chris@282 1110 kr.registerShortcut(tr("Multi Select"), tr("Ctrl+Left"),
Chris@282 1111 tr("Ctrl-click left button and drag to select an additional region"));
Chris@283 1112 kr.registerShortcut(tr("Fine Select"), tr("Shift+Left"),
Chris@283 1113 tr("Shift-click left button and drag to select without snapping to items or grid"));
Chris@282 1114
Chris@282 1115 kr.setCategory(tr("Edit Tool Mouse Actions"));
Chris@282 1116 kr.registerShortcut(tr("Move"), tr("Left"),
Chris@282 1117 tr("Click left button on an item or selected region and drag to move"));
Chris@282 1118 kr.registerShortcut(tr("Edit"), tr("Double-Click Left"),
Chris@282 1119 tr("Double-click left button on an item to edit it"));
Chris@282 1120
Chris@282 1121 kr.setCategory(tr("Draw Tool Mouse Actions"));
Chris@282 1122 kr.registerShortcut(tr("Draw"), tr("Left"),
Chris@282 1123 tr("Click left button and drag to create new item"));
Chris@282 1124
Chris@282 1125 kr.setCategory(tr("Measure Tool Mouse Actions"));
Chris@282 1126 kr.registerShortcut(tr("Measure Area"), tr("Left"),
Chris@282 1127 tr("Click left button and drag to measure a rectangular area"));
Chris@282 1128 kr.registerShortcut(tr("Measure Item"), tr("Double-Click Left"),
Chris@282 1129 tr("Click left button and drag to measure extents of an item or shape"));
Chris@283 1130 kr.registerShortcut(tr("Zoom to Area"), tr("Shift+Left"),
Chris@283 1131 tr("Shift-click left button and drag to zoom to a rectangular area"));
Chris@282 1132 }
Chris@282 1133
Chris@282 1134 void
Chris@127 1135 Pane::mousePressEvent(QMouseEvent *e)
Chris@127 1136 {
Chris@127 1137 if (e->buttons() & Qt::RightButton) {
Chris@189 1138 emit contextHelpChanged("");
Chris@127 1139 emit rightButtonMenuRequested(mapToGlobal(e->pos()));
Chris@127 1140 return;
Chris@127 1141 }
Chris@127 1142
Chris@127 1143 m_clickPos = e->pos();
Chris@262 1144 m_mousePos = m_clickPos;
Chris@127 1145 m_clickedInRange = true;
Chris@127 1146 m_editingSelection = Selection();
Chris@127 1147 m_editingSelectionEdge = 0;
Chris@127 1148 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@127 1149 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@150 1150 m_dragMode = UnresolvedDrag;
Chris@127 1151
Chris@127 1152 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 1153 if (m_manager) mode = m_manager->getToolMode();
Chris@127 1154
Chris@127 1155 m_navigating = false;
Chris@127 1156
Chris@283 1157 if (mode == ViewManager::NavigateMode ||
Chris@283 1158 (e->buttons() & Qt::MidButton) ||
Chris@283 1159 (mode == ViewManager::MeasureMode &&
Chris@283 1160 (e->buttons() & Qt::LeftButton) && m_shiftPressed)) {
Chris@127 1161
Chris@127 1162 if (mode != ViewManager::NavigateMode) {
Chris@127 1163 setCursor(Qt::PointingHandCursor);
Chris@127 1164 }
Chris@127 1165
Chris@127 1166 m_navigating = true;
Chris@127 1167 m_dragCentreFrame = m_centreFrame;
Chris@136 1168 m_dragStartMinValue = 0;
Chris@174 1169
Chris@174 1170 float vmin, vmax, dmin, dmax;
Chris@174 1171 if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) {
Chris@174 1172 m_dragStartMinValue = dmin;
Chris@136 1173 }
Chris@136 1174
Chris@127 1175 } else if (mode == ViewManager::SelectMode) {
Chris@127 1176
Chris@217 1177 if (!hasTopLayerTimeXAxis()) return;
Chris@217 1178
Chris@127 1179 bool closeToLeft = false, closeToRight = false;
Chris@127 1180 Selection selection = getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@127 1181
Chris@127 1182 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@127 1183
Chris@127 1184 m_manager->removeSelection(selection);
Chris@127 1185
Chris@127 1186 if (closeToLeft) {
Chris@127 1187 m_selectionStartFrame = selection.getEndFrame();
Chris@127 1188 } else {
Chris@127 1189 m_selectionStartFrame = selection.getStartFrame();
Chris@127 1190 }
Chris@127 1191
Chris@127 1192 m_manager->setInProgressSelection(selection, false);
Chris@127 1193 m_resizing = true;
Chris@127 1194
Chris@127 1195 } else {
Chris@127 1196
Chris@127 1197 int mouseFrame = getFrameForX(e->x());
Chris@127 1198 size_t resolution = 1;
Chris@127 1199 int snapFrame = mouseFrame;
Chris@127 1200
Chris@127 1201 Layer *layer = getSelectedLayer();
Chris@127 1202 if (layer && !m_shiftPressed) {
Chris@127 1203 layer->snapToFeatureFrame(this, snapFrame,
Chris@127 1204 resolution, Layer::SnapLeft);
Chris@127 1205 }
Chris@127 1206
Chris@127 1207 if (snapFrame < 0) snapFrame = 0;
Chris@127 1208 m_selectionStartFrame = snapFrame;
Chris@127 1209 if (m_manager) {
Chris@127 1210 m_manager->setInProgressSelection(Selection(snapFrame,
Chris@127 1211 snapFrame + resolution),
Chris@127 1212 !m_ctrlPressed);
Chris@127 1213 }
Chris@127 1214
Chris@127 1215 m_resizing = false;
Chris@127 1216 }
Chris@127 1217
Chris@127 1218 update();
Chris@127 1219
Chris@127 1220 } else if (mode == ViewManager::DrawMode) {
Chris@127 1221
Chris@127 1222 Layer *layer = getSelectedLayer();
Chris@127 1223 if (layer && layer->isLayerEditable()) {
Chris@127 1224 layer->drawStart(this, e);
Chris@127 1225 }
Chris@127 1226
Chris@127 1227 } else if (mode == ViewManager::EditMode) {
Chris@127 1228
Chris@127 1229 if (!editSelectionStart(e)) {
Chris@127 1230 Layer *layer = getSelectedLayer();
Chris@127 1231 if (layer && layer->isLayerEditable()) {
Chris@127 1232 layer->editStart(this, e);
Chris@127 1233 }
Chris@127 1234 }
Chris@262 1235
Chris@262 1236 } else if (mode == ViewManager::MeasureMode) {
Chris@262 1237
Chris@268 1238 Layer *layer = getTopLayer();
Chris@267 1239 if (layer) layer->measureStart(this, e);
Chris@262 1240 update();
Chris@127 1241 }
Chris@127 1242
Chris@127 1243 emit paneInteractedWith();
Chris@127 1244 }
Chris@127 1245
Chris@127 1246 void
Chris@127 1247 Pane::mouseReleaseEvent(QMouseEvent *e)
Chris@127 1248 {
Chris@127 1249 if (e->buttons() & Qt::RightButton) {
Chris@127 1250 return;
Chris@127 1251 }
Chris@127 1252
Chris@127 1253 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 1254 if (m_manager) mode = m_manager->getToolMode();
Chris@127 1255
Chris@127 1256 if (m_clickedInRange) {
Chris@127 1257 mouseMoveEvent(e);
Chris@127 1258 }
Chris@127 1259
Chris@127 1260 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@127 1261
Chris@127 1262 m_navigating = false;
Chris@127 1263
Chris@127 1264 if (mode != ViewManager::NavigateMode) {
Chris@127 1265 // restore cursor
Chris@127 1266 toolModeChanged();
Chris@127 1267 }
Chris@127 1268
Chris@127 1269 if (m_shiftPressed) {
Chris@127 1270
Chris@127 1271 int x0 = std::min(m_clickPos.x(), m_mousePos.x());
Chris@127 1272 int x1 = std::max(m_clickPos.x(), m_mousePos.x());
Chris@127 1273
Chris@127 1274 int y0 = std::min(m_clickPos.y(), m_mousePos.y());
Chris@127 1275 int y1 = std::max(m_clickPos.y(), m_mousePos.y());
Chris@127 1276
Chris@174 1277 zoomToRegion(x0, y0, x1, y1);
Chris@127 1278 }
Chris@127 1279
Chris@127 1280 } else if (mode == ViewManager::SelectMode) {
Chris@127 1281
Chris@217 1282 if (!hasTopLayerTimeXAxis()) return;
Chris@217 1283
Chris@127 1284 if (m_manager && m_manager->haveInProgressSelection()) {
Chris@127 1285
Chris@127 1286 bool exclusive;
Chris@127 1287 Selection selection = m_manager->getInProgressSelection(exclusive);
Chris@127 1288
Chris@127 1289 if (selection.getEndFrame() < selection.getStartFrame() + 2) {
Chris@127 1290 selection = Selection();
Chris@127 1291 }
Chris@127 1292
Chris@127 1293 m_manager->clearInProgressSelection();
Chris@127 1294
Chris@127 1295 if (exclusive) {
Chris@127 1296 m_manager->setSelection(selection);
Chris@127 1297 } else {
Chris@127 1298 m_manager->addSelection(selection);
Chris@127 1299 }
Chris@127 1300 }
Chris@127 1301
Chris@127 1302 update();
Chris@127 1303
Chris@127 1304 } else if (mode == ViewManager::DrawMode) {
Chris@127 1305
Chris@127 1306 Layer *layer = getSelectedLayer();
Chris@127 1307 if (layer && layer->isLayerEditable()) {
Chris@127 1308 layer->drawEnd(this, e);
Chris@127 1309 update();
Chris@127 1310 }
Chris@127 1311
Chris@127 1312 } else if (mode == ViewManager::EditMode) {
Chris@127 1313
Chris@127 1314 if (!editSelectionEnd(e)) {
Chris@127 1315 Layer *layer = getSelectedLayer();
Chris@127 1316 if (layer && layer->isLayerEditable()) {
Chris@127 1317 layer->editEnd(this, e);
Chris@127 1318 update();
Chris@127 1319 }
Chris@127 1320 }
Chris@262 1321
Chris@262 1322 } else if (mode == ViewManager::MeasureMode) {
Chris@262 1323
Chris@268 1324 Layer *layer = getTopLayer();
Chris@267 1325 if (layer) layer->measureEnd(this, e);
Chris@267 1326 if (m_measureCursor1) setCursor(*m_measureCursor1);
Chris@267 1327 update();
Chris@127 1328 }
Chris@127 1329
Chris@127 1330 m_clickedInRange = false;
Chris@127 1331
Chris@127 1332 emit paneInteractedWith();
Chris@127 1333 }
Chris@127 1334
Chris@127 1335 void
Chris@127 1336 Pane::mouseMoveEvent(QMouseEvent *e)
Chris@127 1337 {
Chris@127 1338 if (e->buttons() & Qt::RightButton) {
Chris@127 1339 return;
Chris@127 1340 }
Chris@127 1341
Chris@189 1342 updateContextHelp(&e->pos());
Chris@189 1343
Chris@127 1344 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 1345 if (m_manager) mode = m_manager->getToolMode();
Chris@127 1346
Chris@127 1347 QPoint prevPoint = m_identifyPoint;
Chris@127 1348 m_identifyPoint = e->pos();
Chris@127 1349
Chris@127 1350 if (!m_clickedInRange) {
Chris@127 1351
Chris@217 1352 if (mode == ViewManager::SelectMode && hasTopLayerTimeXAxis()) {
Chris@127 1353 bool closeToLeft = false, closeToRight = false;
Chris@127 1354 getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@127 1355 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@127 1356 setCursor(Qt::SizeHorCursor);
Chris@127 1357 } else {
Chris@127 1358 setCursor(Qt::ArrowCursor);
Chris@127 1359 }
Chris@127 1360 }
Chris@127 1361
Chris@127 1362 if (!m_manager->isPlaying()) {
Chris@127 1363
Chris@272 1364 bool updating = false;
Chris@272 1365
Chris@174 1366 if (getSelectedLayer()) {
Chris@127 1367
Chris@174 1368 bool previouslyIdentifying = m_identifyFeatures;
Chris@174 1369 m_identifyFeatures = true;
Chris@174 1370
Chris@174 1371 if (m_identifyFeatures != previouslyIdentifying ||
Chris@174 1372 m_identifyPoint != prevPoint) {
Chris@174 1373 update();
Chris@272 1374 updating = true;
Chris@272 1375 }
Chris@272 1376 }
Chris@272 1377
Chris@272 1378 if (!updating && mode == ViewManager::MeasureMode &&
Chris@272 1379 m_manager && !m_manager->isPlaying()) {
Chris@272 1380
Chris@272 1381 Layer *layer = getTopLayer();
Chris@272 1382 if (layer && layer->nearestMeasurementRectChanged
Chris@272 1383 (this, prevPoint, m_identifyPoint)) {
Chris@272 1384 update();
Chris@174 1385 }
Chris@174 1386 }
Chris@127 1387 }
Chris@127 1388
Chris@127 1389 return;
Chris@127 1390 }
Chris@127 1391
Chris@127 1392 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@127 1393
Chris@127 1394 if (m_shiftPressed) {
Chris@127 1395
Chris@127 1396 m_mousePos = e->pos();
Chris@127 1397 update();
Chris@127 1398
Chris@127 1399 } else {
Chris@127 1400
Chris@174 1401 dragTopLayer(e);
Chris@150 1402 }
Chris@127 1403
Chris@127 1404 } else if (mode == ViewManager::SelectMode) {
Chris@127 1405
Chris@217 1406 if (!hasTopLayerTimeXAxis()) return;
Chris@217 1407
Chris@174 1408 dragExtendSelection(e);
Chris@127 1409
Chris@127 1410 } else if (mode == ViewManager::DrawMode) {
Chris@127 1411
Chris@127 1412 Layer *layer = getSelectedLayer();
Chris@127 1413 if (layer && layer->isLayerEditable()) {
Chris@127 1414 layer->drawDrag(this, e);
Chris@127 1415 }
Chris@127 1416
Chris@127 1417 } else if (mode == ViewManager::EditMode) {
Chris@127 1418
Chris@127 1419 if (!editSelectionDrag(e)) {
Chris@127 1420 Layer *layer = getSelectedLayer();
Chris@127 1421 if (layer && layer->isLayerEditable()) {
Chris@127 1422 layer->editDrag(this, e);
Chris@127 1423 }
Chris@127 1424 }
Chris@259 1425
Chris@259 1426 } else if (mode == ViewManager::MeasureMode) {
Chris@259 1427
Chris@267 1428 if (m_measureCursor2) setCursor(*m_measureCursor2);
Chris@266 1429
Chris@268 1430 Layer *layer = getTopLayer();
Chris@290 1431 if (layer) {
Chris@290 1432 layer->measureDrag(this, e);
Chris@290 1433 if (layer->hasTimeXAxis()) edgeScrollMaybe(e->x());
Chris@290 1434 }
Chris@267 1435
Chris@267 1436 update();
Chris@127 1437 }
Chris@127 1438 }
Chris@127 1439
Chris@127 1440 void
Chris@174 1441 Pane::zoomToRegion(int x0, int y0, int x1, int y1)
Chris@174 1442 {
Chris@174 1443 int w = x1 - x0;
Chris@174 1444
Chris@174 1445 long newStartFrame = getFrameForX(x0);
Chris@174 1446
Chris@174 1447 long visibleFrames = getEndFrame() - getStartFrame();
Chris@174 1448 if (newStartFrame <= -visibleFrames) {
Chris@174 1449 newStartFrame = -visibleFrames + 1;
Chris@174 1450 }
Chris@174 1451
Chris@174 1452 if (newStartFrame >= long(getModelsEndFrame())) {
Chris@174 1453 newStartFrame = getModelsEndFrame() - 1;
Chris@174 1454 }
Chris@174 1455
Chris@174 1456 float ratio = float(w) / float(width());
Chris@174 1457 // std::cerr << "ratio: " << ratio << std::endl;
Chris@174 1458 size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio);
Chris@174 1459 if (newZoomLevel < 1) newZoomLevel = 1;
Chris@174 1460
Chris@174 1461 // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl;
Chris@174 1462 setZoomLevel(getZoomConstraintBlockSize(newZoomLevel));
Chris@174 1463 setStartFrame(newStartFrame);
Chris@174 1464
Chris@174 1465 QString unit;
Chris@174 1466 float min, max;
Chris@174 1467 bool log;
Chris@174 1468 Layer *layer = 0;
Chris@174 1469 for (LayerList::const_iterator i = m_layers.begin();
Chris@174 1470 i != m_layers.end(); ++i) {
Chris@174 1471 if ((*i)->getValueExtents(min, max, log, unit) &&
Chris@174 1472 (*i)->getDisplayExtents(min, max)) {
Chris@174 1473 layer = *i;
Chris@174 1474 break;
Chris@174 1475 }
Chris@174 1476 }
Chris@174 1477
Chris@174 1478 if (layer) {
Chris@174 1479 if (log) {
Chris@174 1480 min = (min < 0.0) ? -log10f(-min) : (min == 0.0) ? 0.0 : log10f(min);
Chris@174 1481 max = (max < 0.0) ? -log10f(-max) : (max == 0.0) ? 0.0 : log10f(max);
Chris@174 1482 }
Chris@174 1483 float rmin = min + ((max - min) * (height() - y1)) / height();
Chris@174 1484 float rmax = min + ((max - min) * (height() - y0)) / height();
Chris@174 1485 std::cerr << "min: " << min << ", max: " << max << ", y0: " << y0 << ", y1: " << y1 << ", h: " << height() << ", rmin: " << rmin << ", rmax: " << rmax << std::endl;
Chris@174 1486 if (log) {
Chris@174 1487 rmin = powf(10, rmin);
Chris@174 1488 rmax = powf(10, rmax);
Chris@174 1489 }
Chris@174 1490 std::cerr << "finally: rmin: " << rmin << ", rmax: " << rmax << " " << unit.toStdString() << std::endl;
Chris@174 1491
Chris@174 1492 layer->setDisplayExtents(rmin, rmax);
Chris@174 1493 updateVerticalPanner();
Chris@174 1494 }
Chris@174 1495 }
Chris@174 1496
Chris@174 1497 void
Chris@174 1498 Pane::dragTopLayer(QMouseEvent *e)
Chris@174 1499 {
Chris@174 1500 // We need to avoid making it too easy to drag both
Chris@174 1501 // horizontally and vertically, in the case where the
Chris@174 1502 // mouse is moved "mostly" in horizontal or vertical axis
Chris@174 1503 // with only a small variation in the other axis. This is
Chris@174 1504 // particularly important during playback (when we want to
Chris@174 1505 // avoid small horizontal motions) or in slow refresh
Chris@174 1506 // layers like spectrogram (when we want to avoid small
Chris@174 1507 // vertical motions).
Chris@174 1508 //
Chris@174 1509 // To this end we have horizontal and vertical thresholds
Chris@174 1510 // and a series of states: unresolved, horizontally or
Chris@174 1511 // vertically constrained, free.
Chris@174 1512 //
Chris@174 1513 // When the mouse first moves, we're unresolved: we
Chris@174 1514 // restrict ourselves to whichever direction seems safest,
Chris@174 1515 // until the mouse has passed a small threshold distance
Chris@174 1516 // from the click point. Then we lock in to one of the
Chris@174 1517 // constrained modes, based on which axis that distance
Chris@174 1518 // was measured in first. Finally, if it turns out we've
Chris@174 1519 // also moved more than a certain larger distance in the
Chris@174 1520 // other direction as well, we may switch into free mode.
Chris@174 1521 //
Chris@174 1522 // If the top layer is incapable of being dragged
Chris@174 1523 // vertically, the logic is short circuited.
Chris@174 1524
Chris@174 1525 int xdiff = e->x() - m_clickPos.x();
Chris@174 1526 int ydiff = e->y() - m_clickPos.y();
Chris@174 1527 int smallThreshold = 10, bigThreshold = 50;
Chris@174 1528
Chris@174 1529 bool canMoveVertical = canTopLayerMoveVertical();
Chris@174 1530 bool canMoveHorizontal = true;
Chris@174 1531
Chris@174 1532 if (!canMoveHorizontal) {
Chris@174 1533 m_dragMode = HorizontalDrag;
Chris@174 1534 }
Chris@174 1535
Chris@174 1536 if (m_dragMode == UnresolvedDrag) {
Chris@174 1537
Chris@174 1538 if (abs(ydiff) > smallThreshold &&
Chris@174 1539 abs(ydiff) > abs(xdiff) * 2) {
Chris@174 1540 m_dragMode = VerticalDrag;
Chris@174 1541 } else if (abs(xdiff) > smallThreshold &&
Chris@174 1542 abs(xdiff) > abs(ydiff) * 2) {
Chris@174 1543 m_dragMode = HorizontalDrag;
Chris@174 1544 } else if (abs(xdiff) > smallThreshold &&
Chris@174 1545 abs(ydiff) > smallThreshold) {
Chris@174 1546 m_dragMode = FreeDrag;
Chris@174 1547 } else {
Chris@174 1548 // When playing, we don't want to disturb the play
Chris@174 1549 // position too easily; when not playing, we don't
Chris@174 1550 // want to move up/down too easily
Chris@174 1551 if (m_manager && m_manager->isPlaying()) {
Chris@174 1552 canMoveHorizontal = false;
Chris@174 1553 } else {
Chris@174 1554 canMoveVertical = false;
Chris@174 1555 }
Chris@174 1556 }
Chris@174 1557 }
Chris@174 1558
Chris@174 1559 if (m_dragMode == VerticalDrag) {
Chris@174 1560 if (abs(xdiff) > bigThreshold) m_dragMode = FreeDrag;
Chris@174 1561 else canMoveHorizontal = false;
Chris@174 1562 }
Chris@174 1563
Chris@174 1564 if (m_dragMode == HorizontalDrag && canMoveVertical) {
Chris@174 1565 if (abs(ydiff) > bigThreshold) m_dragMode = FreeDrag;
Chris@174 1566 else canMoveVertical = false;
Chris@174 1567 }
Chris@174 1568
Chris@174 1569 if (canMoveHorizontal) {
Chris@174 1570
Chris@174 1571 long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x());
Chris@174 1572
Chris@174 1573 size_t newCentreFrame = m_dragCentreFrame;
Chris@174 1574
Chris@174 1575 if (frameOff < 0) {
Chris@174 1576 newCentreFrame -= frameOff;
Chris@174 1577 } else if (newCentreFrame >= size_t(frameOff)) {
Chris@174 1578 newCentreFrame -= frameOff;
Chris@174 1579 } else {
Chris@174 1580 newCentreFrame = 0;
Chris@174 1581 }
Chris@174 1582
Chris@174 1583 if (newCentreFrame >= getModelsEndFrame()) {
Chris@174 1584 newCentreFrame = getModelsEndFrame();
Chris@174 1585 if (newCentreFrame > 0) --newCentreFrame;
Chris@174 1586 }
Chris@174 1587
Chris@174 1588 if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) {
Chris@174 1589 setCentreFrame(newCentreFrame);
Chris@174 1590 }
Chris@174 1591 }
Chris@174 1592
Chris@174 1593 if (canMoveVertical) {
Chris@174 1594
Chris@174 1595 float vmin = 0.f, vmax = 0.f;
Chris@174 1596 float dmin = 0.f, dmax = 0.f;
Chris@174 1597
Chris@174 1598 if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) {
Chris@174 1599
Chris@248 1600 // std::cerr << "ydiff = " << ydiff << std::endl;
Chris@174 1601
Chris@174 1602 float perpix = (dmax - dmin) / height();
Chris@174 1603 float valdiff = ydiff * perpix;
Chris@248 1604 // std::cerr << "valdiff = " << valdiff << std::endl;
Chris@174 1605
Chris@174 1606 float newmin = m_dragStartMinValue + valdiff;
Chris@174 1607 float newmax = m_dragStartMinValue + (dmax - dmin) + valdiff;
Chris@174 1608 if (newmin < vmin) {
Chris@174 1609 newmax += vmin - newmin;
Chris@174 1610 newmin += vmin - newmin;
Chris@174 1611 }
Chris@174 1612 if (newmax > vmax) {
Chris@174 1613 newmin -= newmax - vmax;
Chris@174 1614 newmax -= newmax - vmax;
Chris@174 1615 }
Chris@248 1616 // std::cerr << "(" << dmin << ", " << dmax << ") -> ("
Chris@248 1617 // << newmin << ", " << newmax << ") (drag start " << m_dragStartMinValue << ")" << std::endl;
Chris@174 1618
Chris@174 1619 setTopLayerDisplayExtents(newmin, newmax);
Chris@174 1620 updateVerticalPanner();
Chris@174 1621 }
Chris@174 1622 }
Chris@174 1623 }
Chris@174 1624
Chris@174 1625 void
Chris@174 1626 Pane::dragExtendSelection(QMouseEvent *e)
Chris@174 1627 {
Chris@174 1628 int mouseFrame = getFrameForX(e->x());
Chris@174 1629 size_t resolution = 1;
Chris@174 1630 int snapFrameLeft = mouseFrame;
Chris@174 1631 int snapFrameRight = mouseFrame;
Chris@174 1632
Chris@174 1633 Layer *layer = getSelectedLayer();
Chris@174 1634 if (layer && !m_shiftPressed) {
Chris@174 1635 layer->snapToFeatureFrame(this, snapFrameLeft,
Chris@174 1636 resolution, Layer::SnapLeft);
Chris@174 1637 layer->snapToFeatureFrame(this, snapFrameRight,
Chris@174 1638 resolution, Layer::SnapRight);
Chris@174 1639 }
Chris@174 1640
Chris@174 1641 // std::cerr << "snap: frame = " << mouseFrame << ", start frame = " << m_selectionStartFrame << ", left = " << snapFrameLeft << ", right = " << snapFrameRight << std::endl;
Chris@174 1642
Chris@174 1643 if (snapFrameLeft < 0) snapFrameLeft = 0;
Chris@174 1644 if (snapFrameRight < 0) snapFrameRight = 0;
Chris@174 1645
Chris@174 1646 size_t min, max;
Chris@174 1647
Chris@248 1648 if (m_selectionStartFrame > size_t(snapFrameLeft)) {
Chris@174 1649 min = snapFrameLeft;
Chris@174 1650 max = m_selectionStartFrame;
Chris@248 1651 } else if (size_t(snapFrameRight) > m_selectionStartFrame) {
Chris@174 1652 min = m_selectionStartFrame;
Chris@174 1653 max = snapFrameRight;
Chris@174 1654 } else {
Chris@174 1655 min = snapFrameLeft;
Chris@174 1656 max = snapFrameRight;
Chris@174 1657 }
Chris@174 1658
Chris@174 1659 if (m_manager) {
Chris@174 1660 m_manager->setInProgressSelection(Selection(min, max),
Chris@174 1661 !m_resizing && !m_ctrlPressed);
Chris@174 1662 }
Chris@174 1663
Chris@259 1664 edgeScrollMaybe(e->x());
Chris@259 1665
Chris@259 1666 update();
Chris@259 1667 }
Chris@259 1668
Chris@259 1669 void
Chris@259 1670 Pane::edgeScrollMaybe(int x)
Chris@259 1671 {
Chris@259 1672 int mouseFrame = getFrameForX(x);
Chris@259 1673
Chris@174 1674 bool doScroll = false;
Chris@174 1675 if (!m_manager) doScroll = true;
Chris@174 1676 if (!m_manager->isPlaying()) doScroll = true;
Chris@174 1677 if (m_followPlay != PlaybackScrollContinuous) doScroll = true;
Chris@174 1678
Chris@174 1679 if (doScroll) {
Chris@174 1680 int offset = mouseFrame - getStartFrame();
Chris@174 1681 int available = getEndFrame() - getStartFrame();
Chris@259 1682 int move = 0;
Chris@174 1683 if (offset >= available * 0.95) {
Chris@259 1684 move = int(offset - available * 0.95) + 1;
Chris@259 1685 } else if (offset <= available * 0.10) {
Chris@259 1686 move = int(available * 0.10 - offset) + 1;
Chris@259 1687 move = -move;
Chris@259 1688 }
Chris@259 1689 if (move != 0) {
Chris@174 1690 setCentreFrame(m_centreFrame + move);
Chris@259 1691 update();
Chris@174 1692 }
Chris@174 1693 }
Chris@174 1694 }
Chris@174 1695
Chris@174 1696 void
Chris@127 1697 Pane::mouseDoubleClickEvent(QMouseEvent *e)
Chris@127 1698 {
Chris@127 1699 if (e->buttons() & Qt::RightButton) {
Chris@127 1700 return;
Chris@127 1701 }
Chris@127 1702
Chris@127 1703 // std::cerr << "mouseDoubleClickEvent" << std::endl;
Chris@127 1704
Chris@127 1705 m_clickPos = e->pos();
Chris@127 1706 m_clickedInRange = true;
Chris@127 1707 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@127 1708 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@127 1709
Chris@127 1710 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 1711 if (m_manager) mode = m_manager->getToolMode();
Chris@127 1712
Chris@255 1713 bool relocate = (mode == ViewManager::NavigateMode ||
Chris@255 1714 (e->buttons() & Qt::MidButton));
Chris@255 1715
Chris@127 1716 if (mode == ViewManager::NavigateMode ||
Chris@127 1717 mode == ViewManager::EditMode) {
Chris@127 1718
Chris@127 1719 Layer *layer = getSelectedLayer();
Chris@127 1720 if (layer && layer->isLayerEditable()) {
Chris@255 1721 if (layer->editOpen(this, e)) relocate = false;
Chris@127 1722 }
Chris@280 1723
Chris@280 1724 } else if (mode == ViewManager::MeasureMode) {
Chris@280 1725
Chris@280 1726 Layer *layer = getTopLayer();
Chris@280 1727 if (layer) layer->measureDoubleClick(this, e);
Chris@280 1728 update();
Chris@127 1729 }
Chris@255 1730
Chris@255 1731 if (relocate) {
Chris@255 1732
Chris@255 1733 long f = getFrameForX(e->x());
Chris@255 1734
Chris@255 1735 setCentreFrame(f);
Chris@255 1736
Chris@255 1737 m_dragCentreFrame = f;
Chris@255 1738 m_dragStartMinValue = 0;
Chris@255 1739 m_dragMode = UnresolvedDrag;
Chris@255 1740
Chris@255 1741 float vmin, vmax, dmin, dmax;
Chris@255 1742 if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) {
Chris@255 1743 m_dragStartMinValue = dmin;
Chris@255 1744 }
Chris@255 1745 }
Chris@127 1746 }
Chris@127 1747
Chris@127 1748 void
Chris@290 1749 Pane::enterEvent(QEvent *)
Chris@290 1750 {
Chris@290 1751 m_mouseInWidget = true;
Chris@290 1752 }
Chris@290 1753
Chris@290 1754 void
Chris@127 1755 Pane::leaveEvent(QEvent *)
Chris@127 1756 {
Chris@290 1757 m_mouseInWidget = false;
Chris@127 1758 bool previouslyIdentifying = m_identifyFeatures;
Chris@127 1759 m_identifyFeatures = false;
Chris@127 1760 if (previouslyIdentifying) update();
Chris@189 1761 emit contextHelpChanged("");
Chris@127 1762 }
Chris@127 1763
Chris@127 1764 void
Chris@133 1765 Pane::resizeEvent(QResizeEvent *)
Chris@133 1766 {
Chris@133 1767 updateHeadsUpDisplay();
Chris@133 1768 }
Chris@133 1769
Chris@133 1770 void
Chris@127 1771 Pane::wheelEvent(QWheelEvent *e)
Chris@127 1772 {
Chris@127 1773 //std::cerr << "wheelEvent, delta " << e->delta() << std::endl;
Chris@127 1774
Chris@127 1775 int count = e->delta();
Chris@127 1776
Chris@127 1777 if (count > 0) {
Chris@127 1778 if (count >= 120) count /= 120;
Chris@127 1779 else count = 1;
Chris@127 1780 }
Chris@127 1781
Chris@127 1782 if (count < 0) {
Chris@127 1783 if (count <= -120) count /= 120;
Chris@127 1784 else count = -1;
Chris@127 1785 }
Chris@127 1786
Chris@127 1787 if (e->modifiers() & Qt::ControlModifier) {
Chris@127 1788
Chris@127 1789 // Scroll left or right, rapidly
Chris@127 1790
Chris@127 1791 if (getStartFrame() < 0 &&
Chris@127 1792 getEndFrame() >= getModelsEndFrame()) return;
Chris@127 1793
Chris@127 1794 long delta = ((width() / 2) * count * m_zoomLevel);
Chris@127 1795
Chris@127 1796 if (int(m_centreFrame) < delta) {
Chris@127 1797 setCentreFrame(0);
Chris@127 1798 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@127 1799 setCentreFrame(getModelsEndFrame());
Chris@127 1800 } else {
Chris@127 1801 setCentreFrame(m_centreFrame - delta);
Chris@127 1802 }
Chris@127 1803
Chris@256 1804 } else if (e->modifiers() & Qt::ShiftModifier) {
Chris@256 1805
Chris@256 1806 // Zoom vertically
Chris@256 1807
Chris@256 1808 if (m_vpan) {
Chris@256 1809 m_vpan->scroll(e->delta() > 0);
Chris@256 1810 }
Chris@256 1811
Chris@256 1812 } else if (e->modifiers() & Qt::AltModifier) {
Chris@256 1813
Chris@256 1814 // Zoom vertically
Chris@256 1815
Chris@256 1816 if (m_vthumb) {
Chris@256 1817 m_vthumb->scroll(e->delta() > 0);
Chris@256 1818 }
Chris@256 1819
Chris@127 1820 } else {
Chris@127 1821
Chris@127 1822 // Zoom in or out
Chris@127 1823
Chris@127 1824 int newZoomLevel = m_zoomLevel;
Chris@127 1825
Chris@127 1826 while (count > 0) {
Chris@127 1827 if (newZoomLevel <= 2) {
Chris@127 1828 newZoomLevel = 1;
Chris@127 1829 break;
Chris@127 1830 }
Chris@127 1831 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@127 1832 ZoomConstraint::RoundDown);
Chris@127 1833 --count;
Chris@127 1834 }
Chris@127 1835
Chris@127 1836 while (count < 0) {
Chris@127 1837 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@127 1838 ZoomConstraint::RoundUp);
Chris@127 1839 ++count;
Chris@127 1840 }
Chris@127 1841
Chris@127 1842 if (newZoomLevel != m_zoomLevel) {
Chris@127 1843 setZoomLevel(newZoomLevel);
Chris@127 1844 }
Chris@127 1845 }
Chris@127 1846
Chris@127 1847 emit paneInteractedWith();
Chris@127 1848 }
Chris@127 1849
Chris@132 1850 void
Chris@132 1851 Pane::horizontalThumbwheelMoved(int value)
Chris@132 1852 {
Chris@137 1853 //!!! dupe with updateHeadsUpDisplay
Chris@137 1854
Chris@132 1855 int count = 0;
Chris@132 1856 int level = 1;
Chris@137 1857
Chris@137 1858
Chris@137 1859 //!!! pull out into function (presumably in View)
Chris@137 1860 bool haveConstraint = false;
Chris@137 1861 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end();
Chris@137 1862 ++i) {
Chris@137 1863 if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) {
Chris@137 1864 haveConstraint = true;
Chris@137 1865 break;
Chris@137 1866 }
Chris@132 1867 }
Chris@132 1868
Chris@137 1869 if (haveConstraint) {
Chris@137 1870 while (true) {
Chris@137 1871 if (m_hthumb->getMaximumValue() - value == count) break;
Chris@137 1872 int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@137 1873 ZoomConstraint::RoundUp);
Chris@137 1874 if (newLevel == level) break;
Chris@137 1875 level = newLevel;
Chris@137 1876 if (++count == 50) break;
Chris@137 1877 }
Chris@137 1878 } else {
Chris@137 1879 while (true) {
Chris@137 1880 if (m_hthumb->getMaximumValue() - value == count) break;
Chris@137 1881 int step = level / 10;
Chris@137 1882 int pwr = 0;
Chris@137 1883 while (step > 0) {
Chris@137 1884 ++pwr;
Chris@137 1885 step /= 2;
Chris@137 1886 }
Chris@137 1887 step = 1;
Chris@137 1888 while (pwr > 0) {
Chris@137 1889 step *= 2;
Chris@137 1890 --pwr;
Chris@137 1891 }
Chris@137 1892 // std::cerr << level << std::endl;
Chris@137 1893 level += step;
Chris@137 1894 if (++count == 100 || level > 262144) break;
Chris@137 1895 }
Chris@137 1896 }
Chris@137 1897
Chris@244 1898 // std::cerr << "new level is " << level << std::endl;
Chris@132 1899 setZoomLevel(level);
Chris@132 1900 }
Chris@132 1901
Chris@132 1902 void
Chris@132 1903 Pane::verticalThumbwheelMoved(int value)
Chris@132 1904 {
Chris@133 1905 Layer *layer = 0;
Chris@133 1906 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@133 1907 if (layer) {
Chris@133 1908 int defaultStep = 0;
Chris@133 1909 int max = layer->getVerticalZoomSteps(defaultStep);
Chris@133 1910 if (max == 0) {
Chris@133 1911 updateHeadsUpDisplay();
Chris@133 1912 return;
Chris@133 1913 }
Chris@133 1914 if (value > max) {
Chris@133 1915 value = max;
Chris@133 1916 }
Chris@133 1917 layer->setVerticalZoomStep(value);
Chris@174 1918 updateVerticalPanner();
Chris@133 1919 }
Chris@132 1920 }
Chris@132 1921
Chris@174 1922 void
Chris@174 1923 Pane::verticalPannerMoved(float x0, float y0, float w, float h)
Chris@174 1924 {
Chris@174 1925 float vmin, vmax, dmin, dmax;
Chris@174 1926 if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) return;
Chris@174 1927 float y1 = y0 + h;
Chris@174 1928 float newmax = vmin + ((1.0 - y0) * (vmax - vmin));
Chris@174 1929 float newmin = vmin + ((1.0 - y1) * (vmax - vmin));
Chris@174 1930 std::cerr << "verticalPannerMoved: (" << x0 << "," << y0 << "," << w
Chris@174 1931 << "," << h << ") -> (" << newmin << "," << newmax << ")" << std::endl;
Chris@174 1932 setTopLayerDisplayExtents(newmin, newmax);
Chris@174 1933 }
Chris@174 1934
Chris@188 1935 void
Chris@188 1936 Pane::editVerticalPannerExtents()
Chris@188 1937 {
Chris@188 1938 if (!m_vpan || !m_manager || !m_manager->getZoomWheelsEnabled()) return;
Chris@188 1939
Chris@188 1940 float vmin, vmax, dmin, dmax;
Chris@188 1941 QString unit;
Chris@188 1942 if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax, &unit)
Chris@188 1943 || vmax == vmin) {
Chris@188 1944 return;
Chris@188 1945 }
Chris@188 1946
Chris@188 1947 RangeInputDialog dialog(tr("Enter new range"),
Chris@188 1948 tr("New vertical display range, from %1 to %2 %4:")
Chris@188 1949 .arg(vmin).arg(vmax).arg(unit),
Chris@188 1950 unit, vmin, vmax, this);
Chris@188 1951 dialog.setRange(dmin, dmax);
Chris@188 1952
Chris@188 1953 if (dialog.exec() == QDialog::Accepted) {
Chris@188 1954 dialog.getRange(dmin, dmax);
Chris@188 1955 setTopLayerDisplayExtents(dmin, dmax);
Chris@188 1956 updateVerticalPanner();
Chris@188 1957 }
Chris@188 1958 }
Chris@188 1959
Chris@312 1960 void
Chris@312 1961 Pane::dragEnterEvent(QDragEnterEvent *e)
Chris@312 1962 {
Chris@312 1963 QStringList formats(e->mimeData()->formats());
Chris@312 1964 std::cerr << "dragEnterEvent: format: "
Chris@312 1965 << formats.join(",").toStdString()
Chris@312 1966 << ", possibleActions: " << e->possibleActions()
Chris@312 1967 << ", proposedAction: " << e->proposedAction() << std::endl;
Chris@312 1968
Chris@312 1969 if (e->provides("text/uri-list") || e->provides("text/plain")) {
Chris@312 1970
Chris@312 1971 if (e->proposedAction() & Qt::CopyAction) {
Chris@312 1972 e->acceptProposedAction();
Chris@312 1973 } else {
Chris@312 1974 e->setDropAction(Qt::CopyAction);
Chris@312 1975 e->accept();
Chris@312 1976 }
Chris@312 1977 }
Chris@312 1978 }
Chris@312 1979
Chris@312 1980 void
Chris@312 1981 Pane::dropEvent(QDropEvent *e)
Chris@312 1982 {
Chris@312 1983 std::cerr << "dropEvent: text: \"" << e->mimeData()->text().toStdString()
Chris@312 1984 << "\"" << std::endl;
Chris@312 1985
Chris@312 1986 if (e->provides("text/uri-list") || e->provides("text/plain")) {
Chris@312 1987
Chris@312 1988 if (e->proposedAction() & Qt::CopyAction) {
Chris@312 1989 e->acceptProposedAction();
Chris@312 1990 } else {
Chris@312 1991 e->setDropAction(Qt::CopyAction);
Chris@312 1992 e->accept();
Chris@312 1993 }
Chris@312 1994
Chris@312 1995 if (e->provides("text/uri-list")) {
Chris@312 1996
Chris@312 1997 std::cerr << "accepting... data is \"" << e->encodedData("text/uri-list").data() << "\"" << std::endl;
Chris@312 1998 emit dropAccepted(QString::fromLocal8Bit
Chris@312 1999 (e->encodedData("text/uri-list").data())
Chris@312 2000 .split(QRegExp("[\\r\\n]+"),
Chris@312 2001 QString::SkipEmptyParts));
Chris@312 2002 } else {
Chris@312 2003 emit dropAccepted(QString::fromLocal8Bit
Chris@312 2004 (e->encodedData("text/plain").data()));
Chris@312 2005 }
Chris@312 2006 }
Chris@312 2007 }
Chris@312 2008
Chris@127 2009 bool
Chris@127 2010 Pane::editSelectionStart(QMouseEvent *e)
Chris@127 2011 {
Chris@127 2012 if (!m_identifyFeatures ||
Chris@127 2013 !m_manager ||
Chris@127 2014 m_manager->getToolMode() != ViewManager::EditMode) {
Chris@127 2015 return false;
Chris@127 2016 }
Chris@127 2017
Chris@127 2018 bool closeToLeft, closeToRight;
Chris@127 2019 Selection s(getSelectionAt(e->x(), closeToLeft, closeToRight));
Chris@127 2020 if (s.isEmpty()) return false;
Chris@127 2021 m_editingSelection = s;
Chris@127 2022 m_editingSelectionEdge = (closeToLeft ? -1 : closeToRight ? 1 : 0);
Chris@127 2023 m_mousePos = e->pos();
Chris@127 2024 return true;
Chris@127 2025 }
Chris@127 2026
Chris@127 2027 bool
Chris@127 2028 Pane::editSelectionDrag(QMouseEvent *e)
Chris@127 2029 {
Chris@127 2030 if (m_editingSelection.isEmpty()) return false;
Chris@127 2031 m_mousePos = e->pos();
Chris@127 2032 update();
Chris@127 2033 return true;
Chris@127 2034 }
Chris@127 2035
Chris@127 2036 bool
Chris@248 2037 Pane::editSelectionEnd(QMouseEvent *)
Chris@127 2038 {
Chris@127 2039 if (m_editingSelection.isEmpty()) return false;
Chris@127 2040
Chris@127 2041 int offset = m_mousePos.x() - m_clickPos.x();
Chris@127 2042 Layer *layer = getSelectedLayer();
Chris@127 2043
Chris@127 2044 if (offset == 0 || !layer) {
Chris@127 2045 m_editingSelection = Selection();
Chris@127 2046 return true;
Chris@127 2047 }
Chris@127 2048
Chris@127 2049 int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@127 2050 int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@127 2051
Chris@127 2052 long f0 = getFrameForX(p0);
Chris@127 2053 long f1 = getFrameForX(p1);
Chris@127 2054
Chris@127 2055 Selection newSelection(f0, f1);
Chris@127 2056
Chris@127 2057 if (m_editingSelectionEdge == 0) {
Chris@127 2058
Chris@127 2059 CommandHistory::getInstance()->startCompoundOperation
Chris@127 2060 (tr("Drag Selection"), true);
Chris@127 2061
Chris@127 2062 layer->moveSelection(m_editingSelection, f0);
Chris@127 2063
Chris@127 2064 } else {
Chris@127 2065
Chris@127 2066 CommandHistory::getInstance()->startCompoundOperation
Chris@127 2067 (tr("Resize Selection"), true);
Chris@127 2068
Chris@127 2069 if (m_editingSelectionEdge < 0) {
Chris@127 2070 f1 = m_editingSelection.getEndFrame();
Chris@127 2071 } else {
Chris@127 2072 f0 = m_editingSelection.getStartFrame();
Chris@127 2073 }
Chris@127 2074
Chris@127 2075 newSelection = Selection(f0, f1);
Chris@127 2076 layer->resizeSelection(m_editingSelection, newSelection);
Chris@127 2077 }
Chris@127 2078
Chris@127 2079 m_manager->removeSelection(m_editingSelection);
Chris@127 2080 m_manager->addSelection(newSelection);
Chris@127 2081
Chris@127 2082 CommandHistory::getInstance()->endCompoundOperation();
Chris@127 2083
Chris@127 2084 m_editingSelection = Selection();
Chris@127 2085 return true;
Chris@127 2086 }
Chris@127 2087
Chris@127 2088 void
Chris@127 2089 Pane::toolModeChanged()
Chris@127 2090 {
Chris@127 2091 ViewManager::ToolMode mode = m_manager->getToolMode();
Chris@127 2092 // std::cerr << "Pane::toolModeChanged(" << mode << ")" << std::endl;
Chris@127 2093
Chris@267 2094 if (mode == ViewManager::MeasureMode && !m_measureCursor1) {
Chris@267 2095 m_measureCursor1 = new QCursor(QBitmap(":/icons/measure1cursor.xbm"),
Chris@267 2096 QBitmap(":/icons/measure1mask.xbm"),
Chris@267 2097 15, 14);
Chris@267 2098 m_measureCursor2 = new QCursor(QBitmap(":/icons/measure2cursor.xbm"),
Chris@267 2099 QBitmap(":/icons/measure2mask.xbm"),
Chris@267 2100 16, 17);
Chris@257 2101 }
Chris@257 2102
Chris@127 2103 switch (mode) {
Chris@127 2104
Chris@127 2105 case ViewManager::NavigateMode:
Chris@127 2106 setCursor(Qt::PointingHandCursor);
Chris@127 2107 break;
Chris@127 2108
Chris@127 2109 case ViewManager::SelectMode:
Chris@127 2110 setCursor(Qt::ArrowCursor);
Chris@127 2111 break;
Chris@127 2112
Chris@127 2113 case ViewManager::EditMode:
Chris@127 2114 setCursor(Qt::UpArrowCursor);
Chris@127 2115 break;
Chris@127 2116
Chris@127 2117 case ViewManager::DrawMode:
Chris@127 2118 setCursor(Qt::CrossCursor);
Chris@127 2119 break;
Chris@257 2120
Chris@257 2121 case ViewManager::MeasureMode:
Chris@267 2122 if (m_measureCursor1) setCursor(*m_measureCursor1);
Chris@257 2123 break;
Chris@257 2124
Chris@127 2125 /*
Chris@127 2126 case ViewManager::TextMode:
Chris@127 2127 setCursor(Qt::IBeamCursor);
Chris@127 2128 break;
Chris@127 2129 */
Chris@127 2130 }
Chris@127 2131 }
Chris@127 2132
Chris@133 2133 void
Chris@133 2134 Pane::zoomWheelsEnabledChanged()
Chris@133 2135 {
Chris@133 2136 updateHeadsUpDisplay();
Chris@133 2137 update();
Chris@133 2138 }
Chris@133 2139
Chris@133 2140 void
Chris@224 2141 Pane::viewZoomLevelChanged(View *v, unsigned long z, bool locked)
Chris@133 2142 {
Chris@212 2143 // std::cerr << "Pane[" << this << "]::zoomLevelChanged (global now "
Chris@212 2144 // << (m_manager ? m_manager->getGlobalZoom() : 0) << ")" << std::endl;
Chris@192 2145
Chris@224 2146 View::viewZoomLevelChanged(v, z, locked);
Chris@224 2147
Chris@232 2148 if (m_hthumb && !m_hthumb->isVisible()) return;
Chris@224 2149
Chris@222 2150 if (v != this) {
Chris@222 2151 if (!locked || !m_followZoom) return;
Chris@222 2152 }
Chris@222 2153
Chris@133 2154 if (m_manager && m_manager->getZoomWheelsEnabled()) {
Chris@133 2155 updateHeadsUpDisplay();
Chris@133 2156 }
Chris@133 2157 }
Chris@133 2158
Chris@133 2159 void
Chris@133 2160 Pane::propertyContainerSelected(View *v, PropertyContainer *pc)
Chris@133 2161 {
Chris@133 2162 Layer *layer = 0;
Chris@133 2163
Chris@133 2164 if (getLayerCount() > 0) {
Chris@133 2165 layer = getLayer(getLayerCount() - 1);
Chris@133 2166 disconnect(layer, SIGNAL(verticalZoomChanged()),
Chris@133 2167 this, SLOT(verticalZoomChanged()));
Chris@133 2168 }
Chris@133 2169
Chris@133 2170 View::propertyContainerSelected(v, pc);
Chris@133 2171 updateHeadsUpDisplay();
Chris@133 2172
Chris@187 2173 if (m_vthumb) {
Chris@187 2174 RangeMapper *rm = 0;
Chris@187 2175 if (layer) rm = layer->getNewVerticalZoomRangeMapper();
Chris@187 2176 if (rm) m_vthumb->setRangeMapper(rm);
Chris@187 2177 }
Chris@187 2178
Chris@133 2179 if (getLayerCount() > 0) {
Chris@133 2180 layer = getLayer(getLayerCount() - 1);
Chris@133 2181 connect(layer, SIGNAL(verticalZoomChanged()),
Chris@133 2182 this, SLOT(verticalZoomChanged()));
Chris@133 2183 }
Chris@133 2184 }
Chris@133 2185
Chris@133 2186 void
Chris@133 2187 Pane::verticalZoomChanged()
Chris@133 2188 {
Chris@133 2189 Layer *layer = 0;
Chris@133 2190
Chris@133 2191 if (getLayerCount() > 0) {
Chris@133 2192
Chris@133 2193 layer = getLayer(getLayerCount() - 1);
Chris@133 2194
Chris@133 2195 if (m_vthumb && m_vthumb->isVisible()) {
Chris@133 2196 m_vthumb->setValue(layer->getCurrentVerticalZoomStep());
Chris@133 2197 }
Chris@133 2198 }
Chris@133 2199 }
Chris@133 2200
Chris@189 2201 void
Chris@189 2202 Pane::updateContextHelp(const QPoint *pos)
Chris@189 2203 {
Chris@189 2204 QString help = "";
Chris@189 2205
Chris@189 2206 if (m_clickedInRange) {
Chris@189 2207 emit contextHelpChanged("");
Chris@189 2208 return;
Chris@189 2209 }
Chris@189 2210
Chris@189 2211 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@189 2212 if (m_manager) mode = m_manager->getToolMode();
Chris@189 2213
Chris@189 2214 bool editable = false;
Chris@189 2215 Layer *layer = getSelectedLayer();
Chris@189 2216 if (layer && layer->isLayerEditable()) {
Chris@189 2217 editable = true;
Chris@189 2218 }
Chris@189 2219
Chris@189 2220 if (mode == ViewManager::NavigateMode) {
Chris@189 2221
Chris@189 2222 help = tr("Click and drag to navigate");
Chris@189 2223
Chris@189 2224 } else if (mode == ViewManager::SelectMode) {
Chris@189 2225
Chris@217 2226 if (!hasTopLayerTimeXAxis()) return;
Chris@217 2227
Chris@189 2228 bool haveSelection = (m_manager && !m_manager->getSelections().empty());
Chris@189 2229
Chris@189 2230 if (haveSelection) {
Chris@189 2231 if (editable) {
Chris@189 2232 help = tr("Click and drag to select a range; hold Shift to avoid snapping to items; hold Ctrl for multi-select; middle-click and drag to navigate");
Chris@189 2233 } else {
Chris@189 2234 help = tr("Click and drag to select a range; hold Ctrl for multi-select; middle-click and drag to navigate");
Chris@189 2235 }
Chris@189 2236
Chris@189 2237 if (pos) {
Chris@189 2238 bool closeToLeft = false, closeToRight = false;
Chris@189 2239 Selection selection = getSelectionAt(pos->x(), closeToLeft, closeToRight);
Chris@189 2240 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@189 2241
Chris@189 2242 help = tr("Click and drag to move the selection boundary");
Chris@189 2243 }
Chris@189 2244 }
Chris@189 2245 } else {
Chris@189 2246 if (editable) {
Chris@189 2247 help = tr("Click and drag to select a range; hold Shift to avoid snapping to items; middle-click to navigate");
Chris@189 2248 } else {
Chris@189 2249 help = tr("Click and drag to select a range; middle-click and drag to navigate");
Chris@189 2250 }
Chris@189 2251 }
Chris@189 2252
Chris@189 2253 } else if (mode == ViewManager::DrawMode) {
Chris@189 2254
Chris@189 2255 //!!! could call through to a layer function to find out exact meaning
Chris@189 2256 if (editable) {
Chris@189 2257 help = tr("Click to add a new item in the active layer");
Chris@189 2258 }
Chris@189 2259
Chris@189 2260 } else if (mode == ViewManager::EditMode) {
Chris@189 2261
Chris@189 2262 //!!! could call through to layer
Chris@189 2263 if (editable) {
Chris@189 2264 help = tr("Click and drag an item in the active layer to move it");
Chris@189 2265 if (pos) {
Chris@189 2266 bool closeToLeft = false, closeToRight = false;
Chris@189 2267 Selection selection = getSelectionAt(pos->x(), closeToLeft, closeToRight);
Chris@189 2268 if (!selection.isEmpty()) {
Chris@189 2269 help = tr("Click and drag to move all items in the selected range");
Chris@189 2270 }
Chris@189 2271 }
Chris@189 2272 }
Chris@189 2273 }
Chris@189 2274
Chris@189 2275 emit contextHelpChanged(help);
Chris@189 2276 }
Chris@189 2277
Chris@189 2278 void
Chris@189 2279 Pane::mouseEnteredWidget()
Chris@189 2280 {
Chris@189 2281 QWidget *w = dynamic_cast<QWidget *>(sender());
Chris@189 2282 if (!w) return;
Chris@189 2283
Chris@189 2284 if (w == m_vpan) {
Chris@189 2285 emit contextHelpChanged(tr("Click and drag to adjust the visible range of the vertical scale"));
Chris@189 2286 } else if (w == m_vthumb) {
Chris@189 2287 emit contextHelpChanged(tr("Click and drag to adjust the vertical zoom level"));
Chris@189 2288 } else if (w == m_hthumb) {
Chris@189 2289 emit contextHelpChanged(tr("Click and drag to adjust the horizontal zoom level"));
Chris@189 2290 } else if (w == m_reset) {
Chris@189 2291 emit contextHelpChanged(tr("Reset horizontal and vertical zoom levels to their defaults"));
Chris@189 2292 }
Chris@189 2293 }
Chris@189 2294
Chris@189 2295 void
Chris@189 2296 Pane::mouseLeftWidget()
Chris@189 2297 {
Chris@189 2298 emit contextHelpChanged("");
Chris@189 2299 }
Chris@189 2300
Chris@316 2301 void
Chris@316 2302 Pane::toXml(QTextStream &stream,
Chris@316 2303 QString indent, QString extraAttributes) const
Chris@127 2304 {
Chris@316 2305 View::toXml
Chris@316 2306 (stream, indent,
Chris@127 2307 QString("type=\"pane\" centreLineVisible=\"%1\" height=\"%2\" %3")
Chris@127 2308 .arg(m_centreLineVisible).arg(height()).arg(extraAttributes));
Chris@127 2309 }
Chris@127 2310
Chris@127 2311