annotate view/Pane.cpp @ 355:9aab6bd27fcc

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