annotate view/Pane.cpp @ 248:28c8e8e3c537

* Fix many compile warnings, remove some debug output
author Chris Cannam
date Mon, 30 Apr 2007 13:36:23 +0000
parents 5105be027d8e
children e175ade2d6b0
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@182 7 This file copyright 2006 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@127 24 #include "layer/WaveformLayer.h"
Chris@127 25
Chris@127 26 #include <QPaintEvent>
Chris@127 27 #include <QPainter>
Chris@127 28 #include <iostream>
Chris@127 29 #include <cmath>
Chris@127 30
Chris@133 31 //!!! for HUD -- pull out into a separate class
Chris@133 32 #include <QFrame>
Chris@133 33 #include <QGridLayout>
Chris@133 34 #include <QPushButton>
Chris@133 35 #include "widgets/Thumbwheel.h"
Chris@172 36 #include "widgets/Panner.h"
Chris@188 37 #include "widgets/RangeInputDialog.h"
Chris@189 38 #include "widgets/NotifyingPushButton.h"
Chris@133 39
Chris@127 40 using std::cerr;
Chris@127 41 using std::endl;
Chris@127 42
Chris@127 43 Pane::Pane(QWidget *w) :
Chris@127 44 View(w, true),
Chris@127 45 m_identifyFeatures(false),
Chris@127 46 m_clickedInRange(false),
Chris@127 47 m_shiftPressed(false),
Chris@127 48 m_ctrlPressed(false),
Chris@127 49 m_navigating(false),
Chris@127 50 m_resizing(false),
Chris@133 51 m_centreLineVisible(true),
Chris@222 52 m_scaleWidth(0),
Chris@237 53 m_headsUpDisplay(0),
Chris@237 54 m_vpan(0),
Chris@237 55 m_hthumb(0),
Chris@237 56 m_vthumb(0),
Chris@237 57 m_reset(0)
Chris@127 58 {
Chris@127 59 setObjectName("Pane");
Chris@127 60 setMouseTracking(true);
Chris@133 61
Chris@133 62 updateHeadsUpDisplay();
Chris@133 63 }
Chris@133 64
Chris@133 65 void
Chris@133 66 Pane::updateHeadsUpDisplay()
Chris@133 67 {
Chris@187 68 Profiler profiler("Pane::updateHeadsUpDisplay", true);
Chris@187 69
Chris@192 70 if (!isVisible()) return;
Chris@192 71
Chris@132 72 /*
Chris@132 73 int count = 0;
Chris@132 74 int currentLevel = 1;
Chris@132 75 int level = 1;
Chris@132 76 while (true) {
Chris@132 77 if (getZoomLevel() == level) currentLevel = count;
Chris@132 78 int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@132 79 ZoomConstraint::RoundUp);
Chris@132 80 if (newLevel == level) break;
Chris@132 81 if (newLevel == 131072) break; //!!! just because
Chris@132 82 level = newLevel;
Chris@132 83 ++count;
Chris@132 84 }
Chris@132 85
Chris@132 86 std::cerr << "Have " << count+1 << " zoom levels" << std::endl;
Chris@132 87 */
Chris@133 88
Chris@188 89 Layer *layer = 0;
Chris@188 90 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@188 91
Chris@133 92 if (!m_headsUpDisplay) {
Chris@133 93
Chris@133 94 m_headsUpDisplay = new QFrame(this);
Chris@133 95
Chris@133 96 QGridLayout *layout = new QGridLayout;
Chris@133 97 layout->setMargin(0);
Chris@133 98 layout->setSpacing(0);
Chris@133 99 m_headsUpDisplay->setLayout(layout);
Chris@133 100
Chris@133 101 m_hthumb = new Thumbwheel(Qt::Horizontal);
Chris@187 102 m_hthumb->setObjectName(tr("Horizontal Zoom"));
Chris@173 103 layout->addWidget(m_hthumb, 1, 0, 1, 2);
Chris@133 104 m_hthumb->setFixedWidth(70);
Chris@133 105 m_hthumb->setFixedHeight(16);
Chris@133 106 m_hthumb->setDefaultValue(0);
Chris@165 107 m_hthumb->setSpeed(0.6);
Chris@133 108 connect(m_hthumb, SIGNAL(valueChanged(int)), this,
Chris@133 109 SLOT(horizontalThumbwheelMoved(int)));
Chris@189 110 connect(m_hthumb, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189 111 connect(m_hthumb, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@172 112
Chris@172 113 m_vpan = new Panner;
Chris@172 114 layout->addWidget(m_vpan, 0, 1);
Chris@173 115 m_vpan->setFixedWidth(12);
Chris@172 116 m_vpan->setFixedHeight(70);
Chris@174 117 m_vpan->setAlpha(80, 130);
Chris@174 118 connect(m_vpan, SIGNAL(rectExtentsChanged(float, float, float, float)),
Chris@174 119 this, SLOT(verticalPannerMoved(float, float, float, float)));
Chris@188 120 connect(m_vpan, SIGNAL(doubleClicked()),
Chris@188 121 this, SLOT(editVerticalPannerExtents()));
Chris@189 122 connect(m_vpan, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189 123 connect(m_vpan, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@172 124
Chris@133 125 m_vthumb = new Thumbwheel(Qt::Vertical);
Chris@187 126 m_vthumb->setObjectName(tr("Vertical Zoom"));
Chris@172 127 layout->addWidget(m_vthumb, 0, 2);
Chris@133 128 m_vthumb->setFixedWidth(16);
Chris@133 129 m_vthumb->setFixedHeight(70);
Chris@133 130 connect(m_vthumb, SIGNAL(valueChanged(int)), this,
Chris@133 131 SLOT(verticalThumbwheelMoved(int)));
Chris@189 132 connect(m_vthumb, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189 133 connect(m_vthumb, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@133 134
Chris@188 135 if (layer) {
Chris@188 136 RangeMapper *rm = layer->getNewVerticalZoomRangeMapper();
Chris@188 137 if (rm) m_vthumb->setRangeMapper(rm);
Chris@188 138 }
Chris@188 139
Chris@189 140 m_reset = new NotifyingPushButton;
Chris@189 141 m_reset->setFixedHeight(16);
Chris@189 142 m_reset->setFixedWidth(16);
Chris@189 143 layout->addWidget(m_reset, 1, 2);
Chris@189 144 connect(m_reset, SIGNAL(clicked()), m_hthumb, SLOT(resetToDefault()));
Chris@189 145 connect(m_reset, SIGNAL(clicked()), m_vthumb, SLOT(resetToDefault()));
Chris@189 146 connect(m_reset, SIGNAL(clicked()), m_vpan, SLOT(resetToDefault()));
Chris@189 147 connect(m_reset, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189 148 connect(m_reset, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@133 149 }
Chris@133 150
Chris@133 151 int count = 0;
Chris@133 152 int current = 0;
Chris@133 153 int level = 1;
Chris@133 154
Chris@137 155 //!!! pull out into function (presumably in View)
Chris@137 156 bool haveConstraint = false;
Chris@137 157 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end();
Chris@137 158 ++i) {
Chris@137 159 if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) {
Chris@137 160 haveConstraint = true;
Chris@137 161 break;
Chris@137 162 }
Chris@137 163 }
Chris@137 164
Chris@137 165 if (haveConstraint) {
Chris@137 166 while (true) {
Chris@137 167 if (getZoomLevel() == level) current = count;
Chris@137 168 int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@137 169 ZoomConstraint::RoundUp);
Chris@137 170 if (newLevel == level) break;
Chris@137 171 level = newLevel;
Chris@137 172 if (++count == 50) break;
Chris@137 173 }
Chris@137 174 } else {
Chris@137 175 // if we have no particular constraints, we can really spread out
Chris@137 176 while (true) {
Chris@137 177 if (getZoomLevel() >= level) current = count;
Chris@137 178 int step = level / 10;
Chris@137 179 int pwr = 0;
Chris@137 180 while (step > 0) {
Chris@137 181 ++pwr;
Chris@137 182 step /= 2;
Chris@137 183 }
Chris@137 184 step = 1;
Chris@137 185 while (pwr > 0) {
Chris@137 186 step *= 2;
Chris@137 187 --pwr;
Chris@137 188 }
Chris@154 189 // std::cerr << level << std::endl;
Chris@137 190 level += step;
Chris@137 191 if (++count == 100 || level > 262144) break;
Chris@137 192 }
Chris@133 193 }
Chris@133 194
Chris@133 195 // std::cerr << "Have " << count << " zoom levels" << std::endl;
Chris@133 196
Chris@133 197 m_hthumb->setMinimumValue(0);
Chris@133 198 m_hthumb->setMaximumValue(count);
Chris@133 199 m_hthumb->setValue(count - current);
Chris@133 200
Chris@133 201 // std::cerr << "set value to " << count-current << std::endl;
Chris@133 202
Chris@133 203 // std::cerr << "default value is " << m_hthumb->getDefaultValue() << std::endl;
Chris@133 204
Chris@133 205 if (count != 50 && m_hthumb->getDefaultValue() == 0) {
Chris@133 206 m_hthumb->setDefaultValue(count - current);
Chris@133 207 // std::cerr << "set default value to " << m_hthumb->getDefaultValue() << std::endl;
Chris@133 208 }
Chris@133 209
Chris@204 210 bool haveVThumb = false;
Chris@204 211
Chris@133 212 if (layer) {
Chris@133 213 int defaultStep = 0;
Chris@133 214 int max = layer->getVerticalZoomSteps(defaultStep);
Chris@133 215 if (max == 0) {
Chris@133 216 m_vthumb->hide();
Chris@133 217 } else {
Chris@204 218 haveVThumb = true;
Chris@133 219 m_vthumb->show();
Chris@187 220 m_vthumb->blockSignals(true);
Chris@133 221 m_vthumb->setMinimumValue(0);
Chris@133 222 m_vthumb->setMaximumValue(max);
Chris@133 223 m_vthumb->setDefaultValue(defaultStep);
Chris@133 224 m_vthumb->setValue(layer->getCurrentVerticalZoomStep());
Chris@187 225 m_vthumb->blockSignals(false);
Chris@135 226
Chris@205 227 // std::cerr << "Vertical thumbwheel: min 0, max " << max
Chris@205 228 // << ", default " << defaultStep << ", value "
Chris@205 229 // << m_vthumb->getValue() << std::endl;
Chris@135 230
Chris@133 231 }
Chris@133 232 }
Chris@133 233
Chris@174 234 updateVerticalPanner();
Chris@174 235
Chris@133 236 if (m_manager && m_manager->getZoomWheelsEnabled() &&
Chris@133 237 width() > 120 && height() > 100) {
Chris@165 238 if (!m_headsUpDisplay->isVisible()) {
Chris@165 239 m_headsUpDisplay->show();
Chris@165 240 }
Chris@204 241 if (haveVThumb) {
Chris@204 242 m_headsUpDisplay->setFixedHeight(m_vthumb->height() + m_hthumb->height());
Chris@133 243 m_headsUpDisplay->move(width() - 86, height() - 86);
Chris@133 244 } else {
Chris@204 245 m_headsUpDisplay->setFixedHeight(m_hthumb->height());
Chris@204 246 m_headsUpDisplay->move(width() - 86, height() - 16);
Chris@133 247 }
Chris@133 248 } else {
Chris@133 249 m_headsUpDisplay->hide();
Chris@133 250 }
Chris@127 251 }
Chris@127 252
Chris@174 253 void
Chris@174 254 Pane::updateVerticalPanner()
Chris@174 255 {
Chris@174 256 if (!m_vpan || !m_manager || !m_manager->getZoomWheelsEnabled()) return;
Chris@174 257
Chris@208 258 // In principle we should show or hide the panner on the basis of
Chris@208 259 // whether the top layer has adjustable display extents, and we do
Chris@208 260 // that below. However, we have no basis for layout of the panner
Chris@208 261 // if the vertical scroll wheel is not also present. So if we
Chris@208 262 // have no vertical scroll wheel, we should remove the panner as
Chris@208 263 // well. Ideally any layer that implements display extents should
Chris@208 264 // implement vertical zoom steps as well, but they don't all at
Chris@208 265 // the moment.
Chris@208 266
Chris@208 267 Layer *layer = 0;
Chris@208 268 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@208 269 int discard;
Chris@208 270 if (layer && layer->getVerticalZoomSteps(discard) == 0) {
Chris@208 271 m_vpan->hide();
Chris@208 272 return;
Chris@208 273 }
Chris@208 274
Chris@174 275 float vmin, vmax, dmin, dmax;
Chris@174 276 if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax) && vmax != vmin) {
Chris@174 277 float y0 = (dmin - vmin) / (vmax - vmin);
Chris@174 278 float y1 = (dmax - vmin) / (vmax - vmin);
Chris@174 279 m_vpan->blockSignals(true);
Chris@174 280 m_vpan->setRectExtents(0, 1.0 - y1, 1, y1 - y0);
Chris@174 281 m_vpan->blockSignals(false);
Chris@174 282 m_vpan->show();
Chris@174 283 } else {
Chris@174 284 m_vpan->hide();
Chris@174 285 }
Chris@174 286 }
Chris@174 287
Chris@127 288 bool
Chris@127 289 Pane::shouldIlluminateLocalFeatures(const Layer *layer, QPoint &pos) const
Chris@127 290 {
Chris@127 291 QPoint discard;
Chris@127 292 bool b0, b1;
Chris@127 293
Chris@127 294 if (layer == getSelectedLayer() &&
Chris@127 295 !shouldIlluminateLocalSelection(discard, b0, b1)) {
Chris@127 296
Chris@127 297 pos = m_identifyPoint;
Chris@127 298 return m_identifyFeatures;
Chris@127 299 }
Chris@127 300
Chris@127 301 return false;
Chris@127 302 }
Chris@127 303
Chris@127 304 bool
Chris@127 305 Pane::shouldIlluminateLocalSelection(QPoint &pos,
Chris@127 306 bool &closeToLeft,
Chris@127 307 bool &closeToRight) const
Chris@127 308 {
Chris@127 309 if (m_identifyFeatures &&
Chris@127 310 m_manager &&
Chris@127 311 m_manager->getToolMode() == ViewManager::EditMode &&
Chris@127 312 !m_manager->getSelections().empty() &&
Chris@127 313 !selectionIsBeingEdited()) {
Chris@127 314
Chris@127 315 Selection s(getSelectionAt(m_identifyPoint.x(),
Chris@127 316 closeToLeft, closeToRight));
Chris@127 317
Chris@127 318 if (!s.isEmpty()) {
Chris@127 319 if (getSelectedLayer() && getSelectedLayer()->isLayerEditable()) {
Chris@127 320
Chris@127 321 pos = m_identifyPoint;
Chris@127 322 return true;
Chris@127 323 }
Chris@127 324 }
Chris@127 325 }
Chris@127 326
Chris@127 327 return false;
Chris@127 328 }
Chris@127 329
Chris@127 330 bool
Chris@127 331 Pane::selectionIsBeingEdited() const
Chris@127 332 {
Chris@127 333 if (!m_editingSelection.isEmpty()) {
Chris@127 334 if (m_mousePos != m_clickPos &&
Chris@127 335 getFrameForX(m_mousePos.x()) != getFrameForX(m_clickPos.x())) {
Chris@127 336 return true;
Chris@127 337 }
Chris@127 338 }
Chris@127 339 return false;
Chris@127 340 }
Chris@127 341
Chris@127 342 void
Chris@127 343 Pane::setCentreLineVisible(bool visible)
Chris@127 344 {
Chris@127 345 m_centreLineVisible = visible;
Chris@127 346 update();
Chris@127 347 }
Chris@127 348
Chris@127 349 void
Chris@127 350 Pane::paintEvent(QPaintEvent *e)
Chris@127 351 {
Chris@127 352 // Profiler profiler("Pane::paintEvent", true);
Chris@127 353
Chris@127 354 QPainter paint;
Chris@127 355
Chris@127 356 QRect r(rect());
Chris@127 357
Chris@127 358 if (e) {
Chris@127 359 r = e->rect();
Chris@127 360 }
Chris@127 361 /*
Chris@127 362 paint.begin(this);
Chris@127 363 paint.setClipRect(r);
Chris@127 364
Chris@127 365 if (hasLightBackground()) {
Chris@127 366 paint.setPen(Qt::white);
Chris@127 367 paint.setBrush(Qt::white);
Chris@127 368 } else {
Chris@127 369 paint.setPen(Qt::black);
Chris@127 370 paint.setBrush(Qt::black);
Chris@127 371 }
Chris@127 372 paint.drawRect(r);
Chris@127 373
Chris@127 374 paint.end();
Chris@127 375 */
Chris@127 376 View::paintEvent(e);
Chris@127 377
Chris@127 378 paint.begin(this);
Chris@127 379
Chris@127 380 if (e) {
Chris@127 381 paint.setClipRect(r);
Chris@127 382 }
Chris@127 383
Chris@127 384 const Model *waveformModel = 0; // just for reporting purposes
Chris@127 385
Chris@127 386 int fontHeight = paint.fontMetrics().height();
Chris@127 387 int fontAscent = paint.fontMetrics().ascent();
Chris@127 388
Chris@127 389 if (m_manager &&
Chris@127 390 !m_manager->isPlaying() &&
Chris@127 391 m_manager->getToolMode() == ViewManager::SelectMode) {
Chris@127 392
Chris@127 393 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@127 394 --vi;
Chris@127 395
Chris@127 396 std::vector<QRect> crosshairExtents;
Chris@127 397
Chris@127 398 if ((*vi)->getCrosshairExtents(this, paint, m_identifyPoint,
Chris@127 399 crosshairExtents)) {
Chris@127 400 (*vi)->paintCrosshairs(this, paint, m_identifyPoint);
Chris@127 401 break;
Chris@127 402 } else if ((*vi)->isLayerOpaque()) {
Chris@127 403 break;
Chris@127 404 }
Chris@127 405 }
Chris@127 406 }
Chris@127 407
Chris@127 408 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@127 409 --vi;
Chris@127 410
Chris@127 411 if (dynamic_cast<WaveformLayer *>(*vi)) {
Chris@127 412 waveformModel = (*vi)->getModel();
Chris@127 413 }
Chris@127 414
Chris@189 415 if (!m_manager || !m_manager->shouldShowVerticalScale()) {
Chris@227 416 m_scaleWidth = 0;
Chris@227 417 } else {
Chris@227 418 m_scaleWidth = (*vi)->getVerticalScaleWidth(this, paint);
Chris@127 419 }
Chris@127 420
Chris@222 421 if (m_scaleWidth > 0 && r.left() < m_scaleWidth) {
Chris@127 422
Chris@127 423 // Profiler profiler("Pane::paintEvent - painting vertical scale", true);
Chris@127 424
Chris@127 425 // std::cerr << "Pane::paintEvent: calling paint.save() in vertical scale block" << std::endl;
Chris@127 426 paint.save();
Chris@127 427
Chris@127 428 paint.setPen(Qt::black);
Chris@127 429 paint.setBrush(Qt::white);
Chris@222 430 paint.drawRect(0, -1, m_scaleWidth, height()+1);
Chris@127 431
Chris@127 432 paint.setBrush(Qt::NoBrush);
Chris@127 433 (*vi)->paintVerticalScale
Chris@222 434 (this, paint, QRect(0, 0, m_scaleWidth, height()));
Chris@127 435
Chris@127 436 paint.restore();
Chris@127 437 }
Chris@127 438
Chris@127 439 if (m_identifyFeatures) {
Chris@127 440
Chris@127 441 QPoint pos = m_identifyPoint;
Chris@127 442 QString desc = (*vi)->getFeatureDescription(this, pos);
Chris@127 443
Chris@127 444 if (desc != "") {
Chris@127 445
Chris@127 446 paint.save();
Chris@127 447
Chris@127 448 int tabStop =
Chris@127 449 paint.fontMetrics().width(tr("Some lengthy prefix:"));
Chris@127 450
Chris@127 451 QRect boundingRect =
Chris@127 452 paint.fontMetrics().boundingRect
Chris@127 453 (rect(),
Chris@127 454 Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs,
Chris@127 455 desc, tabStop);
Chris@127 456
Chris@127 457 if (hasLightBackground()) {
Chris@127 458 paint.setPen(Qt::NoPen);
Chris@127 459 paint.setBrush(QColor(250, 250, 250, 200));
Chris@127 460 } else {
Chris@127 461 paint.setPen(Qt::NoPen);
Chris@127 462 paint.setBrush(QColor(50, 50, 50, 200));
Chris@127 463 }
Chris@127 464
Chris@127 465 int extra = paint.fontMetrics().descent();
Chris@127 466 paint.drawRect(width() - boundingRect.width() - 10 - extra,
Chris@127 467 10 - extra,
Chris@127 468 boundingRect.width() + 2 * extra,
Chris@127 469 boundingRect.height() + extra);
Chris@127 470
Chris@127 471 if (hasLightBackground()) {
Chris@127 472 paint.setPen(QColor(150, 20, 0));
Chris@127 473 } else {
Chris@127 474 paint.setPen(QColor(255, 150, 100));
Chris@127 475 }
Chris@127 476
Chris@127 477 QTextOption option;
Chris@127 478 option.setWrapMode(QTextOption::NoWrap);
Chris@127 479 option.setAlignment(Qt::AlignRight | Qt::AlignTop);
Chris@127 480 option.setTabStop(tabStop);
Chris@127 481 paint.drawText(QRectF(width() - boundingRect.width() - 10, 10,
Chris@127 482 boundingRect.width(),
Chris@127 483 boundingRect.height()),
Chris@127 484 desc,
Chris@127 485 option);
Chris@127 486
Chris@127 487 paint.restore();
Chris@127 488 }
Chris@127 489 }
Chris@127 490
Chris@127 491 break;
Chris@127 492 }
Chris@127 493
Chris@127 494 int sampleRate = getModelsSampleRate();
Chris@127 495 paint.setBrush(Qt::NoBrush);
Chris@127 496
Chris@189 497 if (m_centreLineVisible &&
Chris@189 498 m_manager &&
Chris@189 499 m_manager->shouldShowCentreLine()) {
Chris@127 500
Chris@189 501 QColor c = QColor(0, 0, 0);
Chris@189 502 if (!hasLightBackground()) {
Chris@189 503 c = QColor(240, 240, 240);
Chris@189 504 }
Chris@189 505 paint.setPen(c);
Chris@189 506 int x = width() / 2 + 1;
Chris@189 507 paint.drawLine(x, 0, x, height() - 1);
Chris@189 508 paint.drawLine(x-1, 1, x+1, 1);
Chris@189 509 paint.drawLine(x-2, 0, x+2, 0);
Chris@189 510 paint.drawLine(x-1, height() - 2, x+1, height() - 2);
Chris@189 511 paint.drawLine(x-2, height() - 1, x+2, height() - 1);
Chris@127 512
Chris@127 513 paint.setPen(QColor(50, 50, 50));
Chris@127 514
Chris@127 515 int y = height() - fontHeight
Chris@127 516 + fontAscent - 6;
Chris@127 517
Chris@127 518 LayerList::iterator vi = m_layers.end();
Chris@127 519
Chris@127 520 if (vi != m_layers.begin()) {
Chris@127 521
Chris@127 522 switch ((*--vi)->getPreferredFrameCountPosition()) {
Chris@127 523
Chris@127 524 case Layer::PositionTop:
Chris@127 525 y = fontAscent + 6;
Chris@127 526 break;
Chris@127 527
Chris@127 528 case Layer::PositionMiddle:
Chris@127 529 y = (height() - fontHeight) / 2
Chris@127 530 + fontAscent;
Chris@127 531 break;
Chris@127 532
Chris@127 533 case Layer::PositionBottom:
Chris@127 534 // y already set correctly
Chris@127 535 break;
Chris@127 536 }
Chris@127 537 }
Chris@127 538
Chris@189 539 if (m_manager && m_manager->shouldShowFrameCount()) {
Chris@127 540
Chris@127 541 if (sampleRate) {
Chris@127 542
Chris@127 543 QString text(QString::fromStdString
Chris@127 544 (RealTime::frame2RealTime
Chris@127 545 (m_centreFrame, sampleRate).toText(true)));
Chris@127 546
Chris@127 547 int tw = paint.fontMetrics().width(text);
Chris@127 548 int x = width()/2 - 4 - tw;
Chris@127 549
Chris@127 550 drawVisibleText(paint, x, y, text, OutlinedText);
Chris@127 551 }
Chris@127 552
Chris@127 553 QString text = QString("%1").arg(m_centreFrame);
Chris@127 554
Chris@127 555 int x = width()/2 + 4;
Chris@127 556
Chris@127 557 drawVisibleText(paint, x, y, text, OutlinedText);
Chris@127 558 }
Chris@127 559
Chris@127 560 } else {
Chris@127 561
Chris@127 562 paint.setPen(QColor(50, 50, 50));
Chris@127 563 }
Chris@127 564
Chris@127 565 if (waveformModel &&
Chris@127 566 m_manager &&
Chris@189 567 m_manager->shouldShowDuration() &&
Chris@127 568 r.y() + r.height() >= height() - fontHeight - 6) {
Chris@127 569
Chris@150 570 size_t modelRate = waveformModel->getSampleRate();
Chris@127 571 size_t playbackRate = m_manager->getPlaybackSampleRate();
Chris@224 572 size_t outputRate = m_manager->getOutputSampleRate();
Chris@224 573
Chris@127 574 QString srNote = "";
Chris@127 575
Chris@127 576 // Show (R) for waveform models that will be resampled on
Chris@127 577 // playback, and (X) for waveform models that will be played
Chris@224 578 // at the wrong rate because their rate differs from the
Chris@224 579 // current playback rate (which is not necessarily that of the
Chris@224 580 // main model).
Chris@127 581
Chris@224 582 if (playbackRate != 0) {
Chris@224 583 if (modelRate == playbackRate) {
Chris@224 584 if (modelRate != outputRate) srNote = " " + tr("(R)");
Chris@224 585 } else {
Chris@224 586 srNote = " " + tr("(X)");
Chris@224 587 }
Chris@224 588 }
Chris@127 589
Chris@127 590 QString desc = tr("%1 / %2Hz%3")
Chris@127 591 .arg(RealTime::frame2RealTime(waveformModel->getEndFrame(),
Chris@127 592 sampleRate)
Chris@127 593 .toText(false).c_str())
Chris@150 594 .arg(modelRate)
Chris@127 595 .arg(srNote);
Chris@127 596
Chris@223 597 if (r.x() < m_scaleWidth + 5 + paint.fontMetrics().width(desc)) {
Chris@223 598 drawVisibleText(paint, m_scaleWidth + 5,
Chris@127 599 height() - fontHeight + fontAscent - 6,
Chris@127 600 desc, OutlinedText);
Chris@127 601 }
Chris@127 602 }
Chris@127 603
Chris@127 604 if (m_manager &&
Chris@189 605 m_manager->shouldShowLayerNames() &&
Chris@248 606 r.y() + r.height() >= height() - int(m_layers.size()) * fontHeight - 6) {
Chris@127 607
Chris@127 608 std::vector<QString> texts;
Chris@127 609 int maxTextWidth = 0;
Chris@127 610
Chris@127 611 for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 612
Chris@127 613 QString text = (*i)->getLayerPresentationName();
Chris@127 614 int tw = paint.fontMetrics().width(text);
Chris@127 615 bool reduced = false;
Chris@127 616 while (tw > width() / 3 && text.length() > 4) {
Chris@127 617 if (!reduced && text.length() > 8) {
Chris@127 618 text = text.left(text.length() - 4);
Chris@127 619 } else {
Chris@127 620 text = text.left(text.length() - 2);
Chris@127 621 }
Chris@127 622 reduced = true;
Chris@127 623 tw = paint.fontMetrics().width(text + "...");
Chris@127 624 }
Chris@127 625 if (reduced) {
Chris@127 626 texts.push_back(text + "...");
Chris@127 627 } else {
Chris@127 628 texts.push_back(text);
Chris@127 629 }
Chris@127 630 if (tw > maxTextWidth) maxTextWidth = tw;
Chris@127 631 }
Chris@127 632
Chris@127 633 int lly = height() - 6;
Chris@133 634 int llx = width() - maxTextWidth - 5;
Chris@127 635
Chris@133 636 if (m_manager->getZoomWheelsEnabled()) {
Chris@133 637 lly -= 20;
Chris@173 638 llx -= 36;
Chris@133 639 }
Chris@133 640
Chris@133 641 if (r.x() + r.width() >= llx) {
Chris@127 642
Chris@248 643 for (size_t i = 0; i < texts.size(); ++i) {
Chris@127 644
Chris@248 645 if (i + 1 == texts.size()) {
Chris@127 646 paint.setPen(Qt::black);
Chris@127 647 }
Chris@127 648
Chris@133 649 drawVisibleText(paint, llx,
Chris@127 650 lly - fontHeight + fontAscent,
Chris@127 651 texts[i], OutlinedText);
Chris@127 652
Chris@127 653 lly -= fontHeight;
Chris@127 654 }
Chris@127 655 }
Chris@127 656 }
Chris@127 657
Chris@127 658 if (m_clickedInRange && m_shiftPressed) {
Chris@127 659 if (m_manager && (m_manager->getToolMode() == ViewManager::NavigateMode)) {
Chris@127 660 //!!! be nice if this looked a bit more in keeping with the
Chris@127 661 //selection block
Chris@127 662 paint.setPen(Qt::blue);
Chris@127 663 paint.drawRect(m_clickPos.x(), m_clickPos.y(),
Chris@127 664 m_mousePos.x() - m_clickPos.x(),
Chris@127 665 m_mousePos.y() - m_clickPos.y());
Chris@127 666 }
Chris@127 667 }
Chris@127 668
Chris@127 669 if (selectionIsBeingEdited()) {
Chris@127 670
Chris@127 671 int offset = m_mousePos.x() - m_clickPos.x();
Chris@127 672 int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@127 673 int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@127 674
Chris@127 675 if (m_editingSelectionEdge < 0) {
Chris@127 676 p1 = getXForFrame(m_editingSelection.getEndFrame());
Chris@127 677 } else if (m_editingSelectionEdge > 0) {
Chris@127 678 p0 = getXForFrame(m_editingSelection.getStartFrame());
Chris@127 679 }
Chris@127 680
Chris@127 681 paint.save();
Chris@127 682 if (hasLightBackground()) {
Chris@127 683 paint.setPen(QPen(Qt::black, 2));
Chris@127 684 } else {
Chris@127 685 paint.setPen(QPen(Qt::white, 2));
Chris@127 686 }
Chris@127 687
Chris@127 688 //!!! duplicating display policy with View::drawSelections
Chris@127 689
Chris@127 690 if (m_editingSelectionEdge < 0) {
Chris@127 691 paint.drawLine(p0, 1, p1, 1);
Chris@127 692 paint.drawLine(p0, 0, p0, height());
Chris@127 693 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@127 694 } else if (m_editingSelectionEdge > 0) {
Chris@127 695 paint.drawLine(p0, 1, p1, 1);
Chris@127 696 paint.drawLine(p1, 0, p1, height());
Chris@127 697 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@127 698 } else {
Chris@127 699 paint.setBrush(Qt::NoBrush);
Chris@127 700 paint.drawRect(p0, 1, p1 - p0, height() - 2);
Chris@127 701 }
Chris@127 702 paint.restore();
Chris@127 703 }
Chris@127 704
Chris@127 705 paint.end();
Chris@127 706 }
Chris@127 707
Chris@227 708 bool
Chris@229 709 Pane::render(QPainter &paint, int xorigin, size_t f0, size_t f1)
Chris@227 710 {
Chris@229 711 if (!View::render(paint, xorigin + m_scaleWidth, f0, f1)) {
Chris@227 712 return false;
Chris@227 713 }
Chris@227 714
Chris@227 715 if (m_scaleWidth > 0) {
Chris@227 716
Chris@227 717 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@227 718 --vi;
Chris@227 719
Chris@227 720 paint.save();
Chris@227 721
Chris@227 722 paint.setPen(Qt::black);
Chris@227 723 paint.setBrush(Qt::white);
Chris@229 724 paint.drawRect(xorigin, -1, m_scaleWidth, height()+1);
Chris@227 725
Chris@227 726 paint.setBrush(Qt::NoBrush);
Chris@227 727 (*vi)->paintVerticalScale
Chris@229 728 (this, paint, QRect(xorigin, 0, m_scaleWidth, height()));
Chris@227 729
Chris@227 730 paint.restore();
Chris@227 731 break;
Chris@227 732 }
Chris@227 733 }
Chris@227 734
Chris@227 735 return true;
Chris@227 736 }
Chris@227 737
Chris@227 738 QImage *
Chris@229 739 Pane::toNewImage(size_t f0, size_t f1)
Chris@227 740 {
Chris@227 741 size_t x0 = f0 / getZoomLevel();
Chris@227 742 size_t x1 = f1 / getZoomLevel();
Chris@227 743
Chris@227 744 QImage *image = new QImage(x1 - x0 + m_scaleWidth,
Chris@227 745 height(), QImage::Format_RGB32);
Chris@227 746
Chris@227 747 int formerScaleWidth = m_scaleWidth;
Chris@227 748
Chris@227 749 if (m_manager && m_manager->shouldShowVerticalScale()) {
Chris@227 750 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@227 751 --vi;
Chris@227 752 QPainter paint(image);
Chris@227 753 m_scaleWidth = (*vi)->getVerticalScaleWidth(this, paint);
Chris@227 754 break;
Chris@227 755 }
Chris@227 756 } else {
Chris@227 757 m_scaleWidth = 0;
Chris@227 758 }
Chris@227 759
Chris@227 760 if (m_scaleWidth != formerScaleWidth) {
Chris@227 761 delete image;
Chris@227 762 image = new QImage(x1 - x0 + m_scaleWidth,
Chris@227 763 height(), QImage::Format_RGB32);
Chris@227 764 }
Chris@227 765
Chris@227 766 QPainter *paint = new QPainter(image);
Chris@229 767 if (!render(*paint, 0, f0, f1)) {
Chris@227 768 delete paint;
Chris@227 769 delete image;
Chris@227 770 return 0;
Chris@227 771 } else {
Chris@227 772 delete paint;
Chris@227 773 return image;
Chris@227 774 }
Chris@227 775 }
Chris@227 776
Chris@229 777 QSize
Chris@229 778 Pane::getImageSize(size_t f0, size_t f1)
Chris@229 779 {
Chris@229 780 QSize s = View::getImageSize(f0, f1);
Chris@229 781 QImage *image = new QImage(100, 100, QImage::Format_RGB32);
Chris@229 782 QPainter paint(image);
Chris@229 783
Chris@229 784 int sw = 0;
Chris@229 785 if (m_manager && m_manager->shouldShowVerticalScale()) {
Chris@229 786 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@229 787 --vi;
Chris@229 788 QPainter paint(image);
Chris@229 789 sw = (*vi)->getVerticalScaleWidth(this, paint);
Chris@229 790 break;
Chris@229 791 }
Chris@229 792 }
Chris@229 793
Chris@229 794 return QSize(sw + s.width(), s.height());
Chris@229 795 }
Chris@229 796
Chris@222 797 size_t
Chris@222 798 Pane::getFirstVisibleFrame() const
Chris@222 799 {
Chris@222 800 long f0 = getFrameForX(m_scaleWidth);
Chris@222 801 size_t f = View::getFirstVisibleFrame();
Chris@222 802 if (f0 < 0 || f0 < long(f)) return f;
Chris@222 803 return f0;
Chris@222 804 }
Chris@222 805
Chris@127 806 Selection
Chris@127 807 Pane::getSelectionAt(int x, bool &closeToLeftEdge, bool &closeToRightEdge) const
Chris@127 808 {
Chris@127 809 closeToLeftEdge = closeToRightEdge = false;
Chris@127 810
Chris@127 811 if (!m_manager) return Selection();
Chris@127 812
Chris@127 813 long testFrame = getFrameForX(x - 5);
Chris@127 814 if (testFrame < 0) {
Chris@127 815 testFrame = getFrameForX(x);
Chris@127 816 if (testFrame < 0) return Selection();
Chris@127 817 }
Chris@127 818
Chris@127 819 Selection selection = m_manager->getContainingSelection(testFrame, true);
Chris@127 820 if (selection.isEmpty()) return selection;
Chris@127 821
Chris@127 822 int lx = getXForFrame(selection.getStartFrame());
Chris@127 823 int rx = getXForFrame(selection.getEndFrame());
Chris@127 824
Chris@127 825 int fuzz = 2;
Chris@127 826 if (x < lx - fuzz || x > rx + fuzz) return Selection();
Chris@127 827
Chris@127 828 int width = rx - lx;
Chris@127 829 fuzz = 3;
Chris@127 830 if (width < 12) fuzz = width / 4;
Chris@127 831 if (fuzz < 1) fuzz = 1;
Chris@127 832
Chris@127 833 if (x < lx + fuzz) closeToLeftEdge = true;
Chris@127 834 if (x > rx - fuzz) closeToRightEdge = true;
Chris@127 835
Chris@127 836 return selection;
Chris@127 837 }
Chris@127 838
Chris@174 839 bool
Chris@174 840 Pane::canTopLayerMoveVertical()
Chris@174 841 {
Chris@174 842 float vmin, vmax, dmin, dmax;
Chris@174 843 if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) return false;
Chris@174 844 if (dmin <= vmin && dmax >= vmax) return false;
Chris@174 845 return true;
Chris@174 846 }
Chris@174 847
Chris@174 848 bool
Chris@174 849 Pane::getTopLayerDisplayExtents(float &vmin, float &vmax,
Chris@188 850 float &dmin, float &dmax,
Chris@188 851 QString *unit)
Chris@174 852 {
Chris@174 853 Layer *layer = 0;
Chris@174 854 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@174 855 if (!layer) return false;
Chris@174 856 bool vlog;
Chris@174 857 QString vunit;
Chris@188 858 bool rv = (layer->getValueExtents(vmin, vmax, vlog, vunit) &&
Chris@188 859 layer->getDisplayExtents(dmin, dmax));
Chris@188 860 if (unit) *unit = vunit;
Chris@188 861 return rv;
Chris@174 862 }
Chris@174 863
Chris@174 864 bool
Chris@174 865 Pane::setTopLayerDisplayExtents(float dmin, float dmax)
Chris@174 866 {
Chris@174 867 Layer *layer = 0;
Chris@174 868 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@174 869 if (!layer) return false;
Chris@174 870 return layer->setDisplayExtents(dmin, dmax);
Chris@174 871 }
Chris@174 872
Chris@127 873 void
Chris@127 874 Pane::mousePressEvent(QMouseEvent *e)
Chris@127 875 {
Chris@127 876 if (e->buttons() & Qt::RightButton) {
Chris@189 877 emit contextHelpChanged("");
Chris@127 878 emit rightButtonMenuRequested(mapToGlobal(e->pos()));
Chris@127 879 return;
Chris@127 880 }
Chris@127 881
Chris@127 882 m_clickPos = e->pos();
Chris@127 883 m_clickedInRange = true;
Chris@127 884 m_editingSelection = Selection();
Chris@127 885 m_editingSelectionEdge = 0;
Chris@127 886 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@127 887 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@150 888 m_dragMode = UnresolvedDrag;
Chris@127 889
Chris@127 890 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 891 if (m_manager) mode = m_manager->getToolMode();
Chris@127 892
Chris@127 893 m_navigating = false;
Chris@127 894
Chris@127 895 if (mode == ViewManager::NavigateMode || (e->buttons() & Qt::MidButton)) {
Chris@127 896
Chris@127 897 if (mode != ViewManager::NavigateMode) {
Chris@127 898 setCursor(Qt::PointingHandCursor);
Chris@127 899 }
Chris@127 900
Chris@127 901 m_navigating = true;
Chris@127 902 m_dragCentreFrame = m_centreFrame;
Chris@136 903 m_dragStartMinValue = 0;
Chris@174 904
Chris@174 905 float vmin, vmax, dmin, dmax;
Chris@174 906 if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) {
Chris@174 907 m_dragStartMinValue = dmin;
Chris@136 908 }
Chris@136 909
Chris@127 910 } else if (mode == ViewManager::SelectMode) {
Chris@127 911
Chris@217 912 if (!hasTopLayerTimeXAxis()) return;
Chris@217 913
Chris@127 914 bool closeToLeft = false, closeToRight = false;
Chris@127 915 Selection selection = getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@127 916
Chris@127 917 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@127 918
Chris@127 919 m_manager->removeSelection(selection);
Chris@127 920
Chris@127 921 if (closeToLeft) {
Chris@127 922 m_selectionStartFrame = selection.getEndFrame();
Chris@127 923 } else {
Chris@127 924 m_selectionStartFrame = selection.getStartFrame();
Chris@127 925 }
Chris@127 926
Chris@127 927 m_manager->setInProgressSelection(selection, false);
Chris@127 928 m_resizing = true;
Chris@127 929
Chris@127 930 } else {
Chris@127 931
Chris@127 932 int mouseFrame = getFrameForX(e->x());
Chris@127 933 size_t resolution = 1;
Chris@127 934 int snapFrame = mouseFrame;
Chris@127 935
Chris@127 936 Layer *layer = getSelectedLayer();
Chris@127 937 if (layer && !m_shiftPressed) {
Chris@127 938 layer->snapToFeatureFrame(this, snapFrame,
Chris@127 939 resolution, Layer::SnapLeft);
Chris@127 940 }
Chris@127 941
Chris@127 942 if (snapFrame < 0) snapFrame = 0;
Chris@127 943 m_selectionStartFrame = snapFrame;
Chris@127 944 if (m_manager) {
Chris@127 945 m_manager->setInProgressSelection(Selection(snapFrame,
Chris@127 946 snapFrame + resolution),
Chris@127 947 !m_ctrlPressed);
Chris@127 948 }
Chris@127 949
Chris@127 950 m_resizing = false;
Chris@127 951 }
Chris@127 952
Chris@127 953 update();
Chris@127 954
Chris@127 955 } else if (mode == ViewManager::DrawMode) {
Chris@127 956
Chris@127 957 Layer *layer = getSelectedLayer();
Chris@127 958 if (layer && layer->isLayerEditable()) {
Chris@127 959 layer->drawStart(this, e);
Chris@127 960 }
Chris@127 961
Chris@127 962 } else if (mode == ViewManager::EditMode) {
Chris@127 963
Chris@127 964 if (!editSelectionStart(e)) {
Chris@127 965 Layer *layer = getSelectedLayer();
Chris@127 966 if (layer && layer->isLayerEditable()) {
Chris@127 967 layer->editStart(this, e);
Chris@127 968 }
Chris@127 969 }
Chris@127 970 }
Chris@127 971
Chris@127 972 emit paneInteractedWith();
Chris@127 973 }
Chris@127 974
Chris@127 975 void
Chris@127 976 Pane::mouseReleaseEvent(QMouseEvent *e)
Chris@127 977 {
Chris@127 978 if (e->buttons() & Qt::RightButton) {
Chris@127 979 return;
Chris@127 980 }
Chris@127 981
Chris@127 982 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 983 if (m_manager) mode = m_manager->getToolMode();
Chris@127 984
Chris@127 985 if (m_clickedInRange) {
Chris@127 986 mouseMoveEvent(e);
Chris@127 987 }
Chris@127 988
Chris@127 989 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@127 990
Chris@127 991 m_navigating = false;
Chris@127 992
Chris@127 993 if (mode != ViewManager::NavigateMode) {
Chris@127 994 // restore cursor
Chris@127 995 toolModeChanged();
Chris@127 996 }
Chris@127 997
Chris@127 998 if (m_shiftPressed) {
Chris@127 999
Chris@127 1000 int x0 = std::min(m_clickPos.x(), m_mousePos.x());
Chris@127 1001 int x1 = std::max(m_clickPos.x(), m_mousePos.x());
Chris@127 1002
Chris@127 1003 int y0 = std::min(m_clickPos.y(), m_mousePos.y());
Chris@127 1004 int y1 = std::max(m_clickPos.y(), m_mousePos.y());
Chris@127 1005
Chris@174 1006 zoomToRegion(x0, y0, x1, y1);
Chris@127 1007 }
Chris@127 1008
Chris@127 1009 } else if (mode == ViewManager::SelectMode) {
Chris@127 1010
Chris@217 1011 if (!hasTopLayerTimeXAxis()) return;
Chris@217 1012
Chris@127 1013 if (m_manager && m_manager->haveInProgressSelection()) {
Chris@127 1014
Chris@127 1015 bool exclusive;
Chris@127 1016 Selection selection = m_manager->getInProgressSelection(exclusive);
Chris@127 1017
Chris@127 1018 if (selection.getEndFrame() < selection.getStartFrame() + 2) {
Chris@127 1019 selection = Selection();
Chris@127 1020 }
Chris@127 1021
Chris@127 1022 m_manager->clearInProgressSelection();
Chris@127 1023
Chris@127 1024 if (exclusive) {
Chris@127 1025 m_manager->setSelection(selection);
Chris@127 1026 } else {
Chris@127 1027 m_manager->addSelection(selection);
Chris@127 1028 }
Chris@127 1029 }
Chris@127 1030
Chris@127 1031 update();
Chris@127 1032
Chris@127 1033 } else if (mode == ViewManager::DrawMode) {
Chris@127 1034
Chris@127 1035 Layer *layer = getSelectedLayer();
Chris@127 1036 if (layer && layer->isLayerEditable()) {
Chris@127 1037 layer->drawEnd(this, e);
Chris@127 1038 update();
Chris@127 1039 }
Chris@127 1040
Chris@127 1041 } else if (mode == ViewManager::EditMode) {
Chris@127 1042
Chris@127 1043 if (!editSelectionEnd(e)) {
Chris@127 1044 Layer *layer = getSelectedLayer();
Chris@127 1045 if (layer && layer->isLayerEditable()) {
Chris@127 1046 layer->editEnd(this, e);
Chris@127 1047 update();
Chris@127 1048 }
Chris@127 1049 }
Chris@127 1050 }
Chris@127 1051
Chris@127 1052 m_clickedInRange = false;
Chris@127 1053
Chris@127 1054 emit paneInteractedWith();
Chris@127 1055 }
Chris@127 1056
Chris@127 1057 void
Chris@127 1058 Pane::mouseMoveEvent(QMouseEvent *e)
Chris@127 1059 {
Chris@127 1060 if (e->buttons() & Qt::RightButton) {
Chris@127 1061 return;
Chris@127 1062 }
Chris@127 1063
Chris@189 1064 updateContextHelp(&e->pos());
Chris@189 1065
Chris@127 1066 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 1067 if (m_manager) mode = m_manager->getToolMode();
Chris@127 1068
Chris@127 1069 QPoint prevPoint = m_identifyPoint;
Chris@127 1070 m_identifyPoint = e->pos();
Chris@127 1071
Chris@127 1072 if (!m_clickedInRange) {
Chris@127 1073
Chris@217 1074 if (mode == ViewManager::SelectMode && hasTopLayerTimeXAxis()) {
Chris@127 1075 bool closeToLeft = false, closeToRight = false;
Chris@127 1076 getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@127 1077 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@127 1078 setCursor(Qt::SizeHorCursor);
Chris@127 1079 } else {
Chris@127 1080 setCursor(Qt::ArrowCursor);
Chris@127 1081 }
Chris@127 1082 }
Chris@127 1083
Chris@127 1084 if (!m_manager->isPlaying()) {
Chris@127 1085
Chris@174 1086 if (getSelectedLayer()) {
Chris@127 1087
Chris@174 1088 bool previouslyIdentifying = m_identifyFeatures;
Chris@174 1089 m_identifyFeatures = true;
Chris@174 1090
Chris@174 1091 if (m_identifyFeatures != previouslyIdentifying ||
Chris@174 1092 m_identifyPoint != prevPoint) {
Chris@174 1093 update();
Chris@174 1094 }
Chris@174 1095 }
Chris@127 1096 }
Chris@127 1097
Chris@127 1098 return;
Chris@127 1099 }
Chris@127 1100
Chris@127 1101 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@127 1102
Chris@127 1103 if (m_shiftPressed) {
Chris@127 1104
Chris@127 1105 m_mousePos = e->pos();
Chris@127 1106 update();
Chris@127 1107
Chris@127 1108 } else {
Chris@127 1109
Chris@174 1110 dragTopLayer(e);
Chris@150 1111 }
Chris@127 1112
Chris@127 1113 } else if (mode == ViewManager::SelectMode) {
Chris@127 1114
Chris@217 1115 if (!hasTopLayerTimeXAxis()) return;
Chris@217 1116
Chris@174 1117 dragExtendSelection(e);
Chris@127 1118
Chris@127 1119 } else if (mode == ViewManager::DrawMode) {
Chris@127 1120
Chris@127 1121 Layer *layer = getSelectedLayer();
Chris@127 1122 if (layer && layer->isLayerEditable()) {
Chris@127 1123 layer->drawDrag(this, e);
Chris@127 1124 }
Chris@127 1125
Chris@127 1126 } else if (mode == ViewManager::EditMode) {
Chris@127 1127
Chris@127 1128 if (!editSelectionDrag(e)) {
Chris@127 1129 Layer *layer = getSelectedLayer();
Chris@127 1130 if (layer && layer->isLayerEditable()) {
Chris@127 1131 layer->editDrag(this, e);
Chris@127 1132 }
Chris@127 1133 }
Chris@127 1134 }
Chris@127 1135 }
Chris@127 1136
Chris@127 1137 void
Chris@174 1138 Pane::zoomToRegion(int x0, int y0, int x1, int y1)
Chris@174 1139 {
Chris@174 1140 int w = x1 - x0;
Chris@174 1141
Chris@174 1142 long newStartFrame = getFrameForX(x0);
Chris@174 1143
Chris@174 1144 long visibleFrames = getEndFrame() - getStartFrame();
Chris@174 1145 if (newStartFrame <= -visibleFrames) {
Chris@174 1146 newStartFrame = -visibleFrames + 1;
Chris@174 1147 }
Chris@174 1148
Chris@174 1149 if (newStartFrame >= long(getModelsEndFrame())) {
Chris@174 1150 newStartFrame = getModelsEndFrame() - 1;
Chris@174 1151 }
Chris@174 1152
Chris@174 1153 float ratio = float(w) / float(width());
Chris@174 1154 // std::cerr << "ratio: " << ratio << std::endl;
Chris@174 1155 size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio);
Chris@174 1156 if (newZoomLevel < 1) newZoomLevel = 1;
Chris@174 1157
Chris@174 1158 // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl;
Chris@174 1159 setZoomLevel(getZoomConstraintBlockSize(newZoomLevel));
Chris@174 1160 setStartFrame(newStartFrame);
Chris@174 1161
Chris@174 1162 QString unit;
Chris@174 1163 float min, max;
Chris@174 1164 bool log;
Chris@174 1165 Layer *layer = 0;
Chris@174 1166 for (LayerList::const_iterator i = m_layers.begin();
Chris@174 1167 i != m_layers.end(); ++i) {
Chris@174 1168 if ((*i)->getValueExtents(min, max, log, unit) &&
Chris@174 1169 (*i)->getDisplayExtents(min, max)) {
Chris@174 1170 layer = *i;
Chris@174 1171 break;
Chris@174 1172 }
Chris@174 1173 }
Chris@174 1174
Chris@174 1175 if (layer) {
Chris@174 1176 if (log) {
Chris@174 1177 min = (min < 0.0) ? -log10f(-min) : (min == 0.0) ? 0.0 : log10f(min);
Chris@174 1178 max = (max < 0.0) ? -log10f(-max) : (max == 0.0) ? 0.0 : log10f(max);
Chris@174 1179 }
Chris@174 1180 float rmin = min + ((max - min) * (height() - y1)) / height();
Chris@174 1181 float rmax = min + ((max - min) * (height() - y0)) / height();
Chris@174 1182 std::cerr << "min: " << min << ", max: " << max << ", y0: " << y0 << ", y1: " << y1 << ", h: " << height() << ", rmin: " << rmin << ", rmax: " << rmax << std::endl;
Chris@174 1183 if (log) {
Chris@174 1184 rmin = powf(10, rmin);
Chris@174 1185 rmax = powf(10, rmax);
Chris@174 1186 }
Chris@174 1187 std::cerr << "finally: rmin: " << rmin << ", rmax: " << rmax << " " << unit.toStdString() << std::endl;
Chris@174 1188
Chris@174 1189 layer->setDisplayExtents(rmin, rmax);
Chris@174 1190 updateVerticalPanner();
Chris@174 1191 }
Chris@174 1192 }
Chris@174 1193
Chris@174 1194 void
Chris@174 1195 Pane::dragTopLayer(QMouseEvent *e)
Chris@174 1196 {
Chris@174 1197 // We need to avoid making it too easy to drag both
Chris@174 1198 // horizontally and vertically, in the case where the
Chris@174 1199 // mouse is moved "mostly" in horizontal or vertical axis
Chris@174 1200 // with only a small variation in the other axis. This is
Chris@174 1201 // particularly important during playback (when we want to
Chris@174 1202 // avoid small horizontal motions) or in slow refresh
Chris@174 1203 // layers like spectrogram (when we want to avoid small
Chris@174 1204 // vertical motions).
Chris@174 1205 //
Chris@174 1206 // To this end we have horizontal and vertical thresholds
Chris@174 1207 // and a series of states: unresolved, horizontally or
Chris@174 1208 // vertically constrained, free.
Chris@174 1209 //
Chris@174 1210 // When the mouse first moves, we're unresolved: we
Chris@174 1211 // restrict ourselves to whichever direction seems safest,
Chris@174 1212 // until the mouse has passed a small threshold distance
Chris@174 1213 // from the click point. Then we lock in to one of the
Chris@174 1214 // constrained modes, based on which axis that distance
Chris@174 1215 // was measured in first. Finally, if it turns out we've
Chris@174 1216 // also moved more than a certain larger distance in the
Chris@174 1217 // other direction as well, we may switch into free mode.
Chris@174 1218 //
Chris@174 1219 // If the top layer is incapable of being dragged
Chris@174 1220 // vertically, the logic is short circuited.
Chris@174 1221
Chris@174 1222 int xdiff = e->x() - m_clickPos.x();
Chris@174 1223 int ydiff = e->y() - m_clickPos.y();
Chris@174 1224 int smallThreshold = 10, bigThreshold = 50;
Chris@174 1225
Chris@174 1226 bool canMoveVertical = canTopLayerMoveVertical();
Chris@174 1227 bool canMoveHorizontal = true;
Chris@174 1228
Chris@174 1229 if (!canMoveHorizontal) {
Chris@174 1230 m_dragMode = HorizontalDrag;
Chris@174 1231 }
Chris@174 1232
Chris@174 1233 if (m_dragMode == UnresolvedDrag) {
Chris@174 1234
Chris@174 1235 if (abs(ydiff) > smallThreshold &&
Chris@174 1236 abs(ydiff) > abs(xdiff) * 2) {
Chris@174 1237 m_dragMode = VerticalDrag;
Chris@174 1238 } else if (abs(xdiff) > smallThreshold &&
Chris@174 1239 abs(xdiff) > abs(ydiff) * 2) {
Chris@174 1240 m_dragMode = HorizontalDrag;
Chris@174 1241 } else if (abs(xdiff) > smallThreshold &&
Chris@174 1242 abs(ydiff) > smallThreshold) {
Chris@174 1243 m_dragMode = FreeDrag;
Chris@174 1244 } else {
Chris@174 1245 // When playing, we don't want to disturb the play
Chris@174 1246 // position too easily; when not playing, we don't
Chris@174 1247 // want to move up/down too easily
Chris@174 1248 if (m_manager && m_manager->isPlaying()) {
Chris@174 1249 canMoveHorizontal = false;
Chris@174 1250 } else {
Chris@174 1251 canMoveVertical = false;
Chris@174 1252 }
Chris@174 1253 }
Chris@174 1254 }
Chris@174 1255
Chris@174 1256 if (m_dragMode == VerticalDrag) {
Chris@174 1257 if (abs(xdiff) > bigThreshold) m_dragMode = FreeDrag;
Chris@174 1258 else canMoveHorizontal = false;
Chris@174 1259 }
Chris@174 1260
Chris@174 1261 if (m_dragMode == HorizontalDrag && canMoveVertical) {
Chris@174 1262 if (abs(ydiff) > bigThreshold) m_dragMode = FreeDrag;
Chris@174 1263 else canMoveVertical = false;
Chris@174 1264 }
Chris@174 1265
Chris@174 1266 if (canMoveHorizontal) {
Chris@174 1267
Chris@174 1268 long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x());
Chris@174 1269
Chris@174 1270 size_t newCentreFrame = m_dragCentreFrame;
Chris@174 1271
Chris@174 1272 if (frameOff < 0) {
Chris@174 1273 newCentreFrame -= frameOff;
Chris@174 1274 } else if (newCentreFrame >= size_t(frameOff)) {
Chris@174 1275 newCentreFrame -= frameOff;
Chris@174 1276 } else {
Chris@174 1277 newCentreFrame = 0;
Chris@174 1278 }
Chris@174 1279
Chris@174 1280 if (newCentreFrame >= getModelsEndFrame()) {
Chris@174 1281 newCentreFrame = getModelsEndFrame();
Chris@174 1282 if (newCentreFrame > 0) --newCentreFrame;
Chris@174 1283 }
Chris@174 1284
Chris@174 1285 if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) {
Chris@174 1286 setCentreFrame(newCentreFrame);
Chris@174 1287 }
Chris@174 1288 }
Chris@174 1289
Chris@174 1290 if (canMoveVertical) {
Chris@174 1291
Chris@174 1292 float vmin = 0.f, vmax = 0.f;
Chris@174 1293 float dmin = 0.f, dmax = 0.f;
Chris@174 1294
Chris@174 1295 if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) {
Chris@174 1296
Chris@248 1297 // std::cerr << "ydiff = " << ydiff << std::endl;
Chris@174 1298
Chris@174 1299 float perpix = (dmax - dmin) / height();
Chris@174 1300 float valdiff = ydiff * perpix;
Chris@248 1301 // std::cerr << "valdiff = " << valdiff << std::endl;
Chris@174 1302
Chris@174 1303 float newmin = m_dragStartMinValue + valdiff;
Chris@174 1304 float newmax = m_dragStartMinValue + (dmax - dmin) + valdiff;
Chris@174 1305 if (newmin < vmin) {
Chris@174 1306 newmax += vmin - newmin;
Chris@174 1307 newmin += vmin - newmin;
Chris@174 1308 }
Chris@174 1309 if (newmax > vmax) {
Chris@174 1310 newmin -= newmax - vmax;
Chris@174 1311 newmax -= newmax - vmax;
Chris@174 1312 }
Chris@248 1313 // std::cerr << "(" << dmin << ", " << dmax << ") -> ("
Chris@248 1314 // << newmin << ", " << newmax << ") (drag start " << m_dragStartMinValue << ")" << std::endl;
Chris@174 1315
Chris@174 1316 setTopLayerDisplayExtents(newmin, newmax);
Chris@174 1317 updateVerticalPanner();
Chris@174 1318 }
Chris@174 1319 }
Chris@174 1320 }
Chris@174 1321
Chris@174 1322 void
Chris@174 1323 Pane::dragExtendSelection(QMouseEvent *e)
Chris@174 1324 {
Chris@174 1325 int mouseFrame = getFrameForX(e->x());
Chris@174 1326 size_t resolution = 1;
Chris@174 1327 int snapFrameLeft = mouseFrame;
Chris@174 1328 int snapFrameRight = mouseFrame;
Chris@174 1329
Chris@174 1330 Layer *layer = getSelectedLayer();
Chris@174 1331 if (layer && !m_shiftPressed) {
Chris@174 1332 layer->snapToFeatureFrame(this, snapFrameLeft,
Chris@174 1333 resolution, Layer::SnapLeft);
Chris@174 1334 layer->snapToFeatureFrame(this, snapFrameRight,
Chris@174 1335 resolution, Layer::SnapRight);
Chris@174 1336 }
Chris@174 1337
Chris@174 1338 // std::cerr << "snap: frame = " << mouseFrame << ", start frame = " << m_selectionStartFrame << ", left = " << snapFrameLeft << ", right = " << snapFrameRight << std::endl;
Chris@174 1339
Chris@174 1340 if (snapFrameLeft < 0) snapFrameLeft = 0;
Chris@174 1341 if (snapFrameRight < 0) snapFrameRight = 0;
Chris@174 1342
Chris@174 1343 size_t min, max;
Chris@174 1344
Chris@248 1345 if (m_selectionStartFrame > size_t(snapFrameLeft)) {
Chris@174 1346 min = snapFrameLeft;
Chris@174 1347 max = m_selectionStartFrame;
Chris@248 1348 } else if (size_t(snapFrameRight) > m_selectionStartFrame) {
Chris@174 1349 min = m_selectionStartFrame;
Chris@174 1350 max = snapFrameRight;
Chris@174 1351 } else {
Chris@174 1352 min = snapFrameLeft;
Chris@174 1353 max = snapFrameRight;
Chris@174 1354 }
Chris@174 1355
Chris@174 1356 if (m_manager) {
Chris@174 1357 m_manager->setInProgressSelection(Selection(min, max),
Chris@174 1358 !m_resizing && !m_ctrlPressed);
Chris@174 1359 }
Chris@174 1360
Chris@174 1361 bool doScroll = false;
Chris@174 1362 if (!m_manager) doScroll = true;
Chris@174 1363 if (!m_manager->isPlaying()) doScroll = true;
Chris@174 1364 if (m_followPlay != PlaybackScrollContinuous) doScroll = true;
Chris@174 1365
Chris@174 1366 if (doScroll) {
Chris@174 1367 int offset = mouseFrame - getStartFrame();
Chris@174 1368 int available = getEndFrame() - getStartFrame();
Chris@174 1369 if (offset >= available * 0.95) {
Chris@174 1370 int move = int(offset - available * 0.95) + 1;
Chris@174 1371 setCentreFrame(m_centreFrame + move);
Chris@174 1372 } else if (offset <= available * 0.10) {
Chris@174 1373 int move = int(available * 0.10 - offset) + 1;
Chris@248 1374 if (move < 0) {
Chris@248 1375 setCentreFrame(m_centreFrame + (-move));
Chris@248 1376 } else if (m_centreFrame > move) {
Chris@174 1377 setCentreFrame(m_centreFrame - move);
Chris@174 1378 } else {
Chris@174 1379 setCentreFrame(0);
Chris@174 1380 }
Chris@174 1381 }
Chris@174 1382 }
Chris@174 1383
Chris@174 1384 update();
Chris@174 1385 }
Chris@174 1386
Chris@174 1387 void
Chris@127 1388 Pane::mouseDoubleClickEvent(QMouseEvent *e)
Chris@127 1389 {
Chris@127 1390 if (e->buttons() & Qt::RightButton) {
Chris@127 1391 return;
Chris@127 1392 }
Chris@127 1393
Chris@127 1394 // std::cerr << "mouseDoubleClickEvent" << std::endl;
Chris@127 1395
Chris@127 1396 m_clickPos = e->pos();
Chris@127 1397 m_clickedInRange = true;
Chris@127 1398 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@127 1399 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@127 1400
Chris@127 1401 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 1402 if (m_manager) mode = m_manager->getToolMode();
Chris@127 1403
Chris@127 1404 if (mode == ViewManager::NavigateMode ||
Chris@127 1405 mode == ViewManager::EditMode) {
Chris@127 1406
Chris@127 1407 Layer *layer = getSelectedLayer();
Chris@127 1408 if (layer && layer->isLayerEditable()) {
Chris@127 1409 layer->editOpen(this, e);
Chris@127 1410 }
Chris@127 1411 }
Chris@127 1412 }
Chris@127 1413
Chris@127 1414 void
Chris@127 1415 Pane::leaveEvent(QEvent *)
Chris@127 1416 {
Chris@127 1417 bool previouslyIdentifying = m_identifyFeatures;
Chris@127 1418 m_identifyFeatures = false;
Chris@127 1419 if (previouslyIdentifying) update();
Chris@189 1420 emit contextHelpChanged("");
Chris@127 1421 }
Chris@127 1422
Chris@127 1423 void
Chris@133 1424 Pane::resizeEvent(QResizeEvent *)
Chris@133 1425 {
Chris@133 1426 updateHeadsUpDisplay();
Chris@133 1427 }
Chris@133 1428
Chris@133 1429 void
Chris@127 1430 Pane::wheelEvent(QWheelEvent *e)
Chris@127 1431 {
Chris@127 1432 //std::cerr << "wheelEvent, delta " << e->delta() << std::endl;
Chris@127 1433
Chris@127 1434 int count = e->delta();
Chris@127 1435
Chris@127 1436 if (count > 0) {
Chris@127 1437 if (count >= 120) count /= 120;
Chris@127 1438 else count = 1;
Chris@127 1439 }
Chris@127 1440
Chris@127 1441 if (count < 0) {
Chris@127 1442 if (count <= -120) count /= 120;
Chris@127 1443 else count = -1;
Chris@127 1444 }
Chris@127 1445
Chris@127 1446 if (e->modifiers() & Qt::ControlModifier) {
Chris@127 1447
Chris@127 1448 // Scroll left or right, rapidly
Chris@127 1449
Chris@127 1450 if (getStartFrame() < 0 &&
Chris@127 1451 getEndFrame() >= getModelsEndFrame()) return;
Chris@127 1452
Chris@127 1453 long delta = ((width() / 2) * count * m_zoomLevel);
Chris@127 1454
Chris@127 1455 if (int(m_centreFrame) < delta) {
Chris@127 1456 setCentreFrame(0);
Chris@127 1457 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@127 1458 setCentreFrame(getModelsEndFrame());
Chris@127 1459 } else {
Chris@127 1460 setCentreFrame(m_centreFrame - delta);
Chris@127 1461 }
Chris@127 1462
Chris@127 1463 } else {
Chris@127 1464
Chris@127 1465 // Zoom in or out
Chris@127 1466
Chris@127 1467 int newZoomLevel = m_zoomLevel;
Chris@127 1468
Chris@127 1469 while (count > 0) {
Chris@127 1470 if (newZoomLevel <= 2) {
Chris@127 1471 newZoomLevel = 1;
Chris@127 1472 break;
Chris@127 1473 }
Chris@127 1474 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@127 1475 ZoomConstraint::RoundDown);
Chris@127 1476 --count;
Chris@127 1477 }
Chris@127 1478
Chris@127 1479 while (count < 0) {
Chris@127 1480 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@127 1481 ZoomConstraint::RoundUp);
Chris@127 1482 ++count;
Chris@127 1483 }
Chris@127 1484
Chris@127 1485 if (newZoomLevel != m_zoomLevel) {
Chris@127 1486 setZoomLevel(newZoomLevel);
Chris@127 1487 }
Chris@127 1488 }
Chris@127 1489
Chris@127 1490 emit paneInteractedWith();
Chris@127 1491 }
Chris@127 1492
Chris@132 1493 void
Chris@132 1494 Pane::horizontalThumbwheelMoved(int value)
Chris@132 1495 {
Chris@137 1496 //!!! dupe with updateHeadsUpDisplay
Chris@137 1497
Chris@132 1498 int count = 0;
Chris@132 1499 int level = 1;
Chris@137 1500
Chris@137 1501
Chris@137 1502 //!!! pull out into function (presumably in View)
Chris@137 1503 bool haveConstraint = false;
Chris@137 1504 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end();
Chris@137 1505 ++i) {
Chris@137 1506 if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) {
Chris@137 1507 haveConstraint = true;
Chris@137 1508 break;
Chris@137 1509 }
Chris@132 1510 }
Chris@132 1511
Chris@137 1512 if (haveConstraint) {
Chris@137 1513 while (true) {
Chris@137 1514 if (m_hthumb->getMaximumValue() - value == count) break;
Chris@137 1515 int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@137 1516 ZoomConstraint::RoundUp);
Chris@137 1517 if (newLevel == level) break;
Chris@137 1518 level = newLevel;
Chris@137 1519 if (++count == 50) break;
Chris@137 1520 }
Chris@137 1521 } else {
Chris@137 1522 while (true) {
Chris@137 1523 if (m_hthumb->getMaximumValue() - value == count) break;
Chris@137 1524 int step = level / 10;
Chris@137 1525 int pwr = 0;
Chris@137 1526 while (step > 0) {
Chris@137 1527 ++pwr;
Chris@137 1528 step /= 2;
Chris@137 1529 }
Chris@137 1530 step = 1;
Chris@137 1531 while (pwr > 0) {
Chris@137 1532 step *= 2;
Chris@137 1533 --pwr;
Chris@137 1534 }
Chris@137 1535 // std::cerr << level << std::endl;
Chris@137 1536 level += step;
Chris@137 1537 if (++count == 100 || level > 262144) break;
Chris@137 1538 }
Chris@137 1539 }
Chris@137 1540
Chris@244 1541 // std::cerr << "new level is " << level << std::endl;
Chris@132 1542 setZoomLevel(level);
Chris@132 1543 }
Chris@132 1544
Chris@132 1545 void
Chris@132 1546 Pane::verticalThumbwheelMoved(int value)
Chris@132 1547 {
Chris@133 1548 Layer *layer = 0;
Chris@133 1549 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@133 1550 if (layer) {
Chris@133 1551 int defaultStep = 0;
Chris@133 1552 int max = layer->getVerticalZoomSteps(defaultStep);
Chris@133 1553 if (max == 0) {
Chris@133 1554 updateHeadsUpDisplay();
Chris@133 1555 return;
Chris@133 1556 }
Chris@133 1557 if (value > max) {
Chris@133 1558 value = max;
Chris@133 1559 }
Chris@133 1560 layer->setVerticalZoomStep(value);
Chris@174 1561 updateVerticalPanner();
Chris@133 1562 }
Chris@132 1563 }
Chris@132 1564
Chris@174 1565 void
Chris@174 1566 Pane::verticalPannerMoved(float x0, float y0, float w, float h)
Chris@174 1567 {
Chris@174 1568 float vmin, vmax, dmin, dmax;
Chris@174 1569 if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) return;
Chris@174 1570 float y1 = y0 + h;
Chris@174 1571 float newmax = vmin + ((1.0 - y0) * (vmax - vmin));
Chris@174 1572 float newmin = vmin + ((1.0 - y1) * (vmax - vmin));
Chris@174 1573 std::cerr << "verticalPannerMoved: (" << x0 << "," << y0 << "," << w
Chris@174 1574 << "," << h << ") -> (" << newmin << "," << newmax << ")" << std::endl;
Chris@174 1575 setTopLayerDisplayExtents(newmin, newmax);
Chris@174 1576 }
Chris@174 1577
Chris@188 1578 void
Chris@188 1579 Pane::editVerticalPannerExtents()
Chris@188 1580 {
Chris@188 1581 if (!m_vpan || !m_manager || !m_manager->getZoomWheelsEnabled()) return;
Chris@188 1582
Chris@188 1583 float vmin, vmax, dmin, dmax;
Chris@188 1584 QString unit;
Chris@188 1585 if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax, &unit)
Chris@188 1586 || vmax == vmin) {
Chris@188 1587 return;
Chris@188 1588 }
Chris@188 1589
Chris@188 1590 RangeInputDialog dialog(tr("Enter new range"),
Chris@188 1591 tr("New vertical display range, from %1 to %2 %4:")
Chris@188 1592 .arg(vmin).arg(vmax).arg(unit),
Chris@188 1593 unit, vmin, vmax, this);
Chris@188 1594 dialog.setRange(dmin, dmax);
Chris@188 1595
Chris@188 1596 if (dialog.exec() == QDialog::Accepted) {
Chris@188 1597 dialog.getRange(dmin, dmax);
Chris@188 1598 setTopLayerDisplayExtents(dmin, dmax);
Chris@188 1599 updateVerticalPanner();
Chris@188 1600 }
Chris@188 1601 }
Chris@188 1602
Chris@127 1603 bool
Chris@127 1604 Pane::editSelectionStart(QMouseEvent *e)
Chris@127 1605 {
Chris@127 1606 if (!m_identifyFeatures ||
Chris@127 1607 !m_manager ||
Chris@127 1608 m_manager->getToolMode() != ViewManager::EditMode) {
Chris@127 1609 return false;
Chris@127 1610 }
Chris@127 1611
Chris@127 1612 bool closeToLeft, closeToRight;
Chris@127 1613 Selection s(getSelectionAt(e->x(), closeToLeft, closeToRight));
Chris@127 1614 if (s.isEmpty()) return false;
Chris@127 1615 m_editingSelection = s;
Chris@127 1616 m_editingSelectionEdge = (closeToLeft ? -1 : closeToRight ? 1 : 0);
Chris@127 1617 m_mousePos = e->pos();
Chris@127 1618 return true;
Chris@127 1619 }
Chris@127 1620
Chris@127 1621 bool
Chris@127 1622 Pane::editSelectionDrag(QMouseEvent *e)
Chris@127 1623 {
Chris@127 1624 if (m_editingSelection.isEmpty()) return false;
Chris@127 1625 m_mousePos = e->pos();
Chris@127 1626 update();
Chris@127 1627 return true;
Chris@127 1628 }
Chris@127 1629
Chris@127 1630 bool
Chris@248 1631 Pane::editSelectionEnd(QMouseEvent *)
Chris@127 1632 {
Chris@127 1633 if (m_editingSelection.isEmpty()) return false;
Chris@127 1634
Chris@127 1635 int offset = m_mousePos.x() - m_clickPos.x();
Chris@127 1636 Layer *layer = getSelectedLayer();
Chris@127 1637
Chris@127 1638 if (offset == 0 || !layer) {
Chris@127 1639 m_editingSelection = Selection();
Chris@127 1640 return true;
Chris@127 1641 }
Chris@127 1642
Chris@127 1643 int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@127 1644 int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@127 1645
Chris@127 1646 long f0 = getFrameForX(p0);
Chris@127 1647 long f1 = getFrameForX(p1);
Chris@127 1648
Chris@127 1649 Selection newSelection(f0, f1);
Chris@127 1650
Chris@127 1651 if (m_editingSelectionEdge == 0) {
Chris@127 1652
Chris@127 1653 CommandHistory::getInstance()->startCompoundOperation
Chris@127 1654 (tr("Drag Selection"), true);
Chris@127 1655
Chris@127 1656 layer->moveSelection(m_editingSelection, f0);
Chris@127 1657
Chris@127 1658 } else {
Chris@127 1659
Chris@127 1660 CommandHistory::getInstance()->startCompoundOperation
Chris@127 1661 (tr("Resize Selection"), true);
Chris@127 1662
Chris@127 1663 if (m_editingSelectionEdge < 0) {
Chris@127 1664 f1 = m_editingSelection.getEndFrame();
Chris@127 1665 } else {
Chris@127 1666 f0 = m_editingSelection.getStartFrame();
Chris@127 1667 }
Chris@127 1668
Chris@127 1669 newSelection = Selection(f0, f1);
Chris@127 1670 layer->resizeSelection(m_editingSelection, newSelection);
Chris@127 1671 }
Chris@127 1672
Chris@127 1673 m_manager->removeSelection(m_editingSelection);
Chris@127 1674 m_manager->addSelection(newSelection);
Chris@127 1675
Chris@127 1676 CommandHistory::getInstance()->endCompoundOperation();
Chris@127 1677
Chris@127 1678 m_editingSelection = Selection();
Chris@127 1679 return true;
Chris@127 1680 }
Chris@127 1681
Chris@127 1682 void
Chris@127 1683 Pane::toolModeChanged()
Chris@127 1684 {
Chris@127 1685 ViewManager::ToolMode mode = m_manager->getToolMode();
Chris@127 1686 // std::cerr << "Pane::toolModeChanged(" << mode << ")" << std::endl;
Chris@127 1687
Chris@127 1688 switch (mode) {
Chris@127 1689
Chris@127 1690 case ViewManager::NavigateMode:
Chris@127 1691 setCursor(Qt::PointingHandCursor);
Chris@127 1692 break;
Chris@127 1693
Chris@127 1694 case ViewManager::SelectMode:
Chris@127 1695 setCursor(Qt::ArrowCursor);
Chris@127 1696 break;
Chris@127 1697
Chris@127 1698 case ViewManager::EditMode:
Chris@127 1699 setCursor(Qt::UpArrowCursor);
Chris@127 1700 break;
Chris@127 1701
Chris@127 1702 case ViewManager::DrawMode:
Chris@127 1703 setCursor(Qt::CrossCursor);
Chris@127 1704 break;
Chris@127 1705 /*
Chris@127 1706 case ViewManager::TextMode:
Chris@127 1707 setCursor(Qt::IBeamCursor);
Chris@127 1708 break;
Chris@127 1709 */
Chris@127 1710 }
Chris@127 1711 }
Chris@127 1712
Chris@133 1713 void
Chris@133 1714 Pane::zoomWheelsEnabledChanged()
Chris@133 1715 {
Chris@133 1716 updateHeadsUpDisplay();
Chris@133 1717 update();
Chris@133 1718 }
Chris@133 1719
Chris@133 1720 void
Chris@224 1721 Pane::viewZoomLevelChanged(View *v, unsigned long z, bool locked)
Chris@133 1722 {
Chris@212 1723 // std::cerr << "Pane[" << this << "]::zoomLevelChanged (global now "
Chris@212 1724 // << (m_manager ? m_manager->getGlobalZoom() : 0) << ")" << std::endl;
Chris@192 1725
Chris@224 1726 View::viewZoomLevelChanged(v, z, locked);
Chris@224 1727
Chris@232 1728 if (m_hthumb && !m_hthumb->isVisible()) return;
Chris@224 1729
Chris@222 1730 if (v != this) {
Chris@222 1731 if (!locked || !m_followZoom) return;
Chris@222 1732 }
Chris@222 1733
Chris@133 1734 if (m_manager && m_manager->getZoomWheelsEnabled()) {
Chris@133 1735 updateHeadsUpDisplay();
Chris@133 1736 }
Chris@133 1737 }
Chris@133 1738
Chris@133 1739 void
Chris@133 1740 Pane::propertyContainerSelected(View *v, PropertyContainer *pc)
Chris@133 1741 {
Chris@133 1742 Layer *layer = 0;
Chris@133 1743
Chris@133 1744 if (getLayerCount() > 0) {
Chris@133 1745 layer = getLayer(getLayerCount() - 1);
Chris@133 1746 disconnect(layer, SIGNAL(verticalZoomChanged()),
Chris@133 1747 this, SLOT(verticalZoomChanged()));
Chris@133 1748 }
Chris@133 1749
Chris@133 1750 View::propertyContainerSelected(v, pc);
Chris@133 1751 updateHeadsUpDisplay();
Chris@133 1752
Chris@187 1753 if (m_vthumb) {
Chris@187 1754 RangeMapper *rm = 0;
Chris@187 1755 if (layer) rm = layer->getNewVerticalZoomRangeMapper();
Chris@187 1756 if (rm) m_vthumb->setRangeMapper(rm);
Chris@187 1757 }
Chris@187 1758
Chris@133 1759 if (getLayerCount() > 0) {
Chris@133 1760 layer = getLayer(getLayerCount() - 1);
Chris@133 1761 connect(layer, SIGNAL(verticalZoomChanged()),
Chris@133 1762 this, SLOT(verticalZoomChanged()));
Chris@133 1763 }
Chris@133 1764 }
Chris@133 1765
Chris@133 1766 void
Chris@133 1767 Pane::verticalZoomChanged()
Chris@133 1768 {
Chris@133 1769 Layer *layer = 0;
Chris@133 1770
Chris@133 1771 if (getLayerCount() > 0) {
Chris@133 1772
Chris@133 1773 layer = getLayer(getLayerCount() - 1);
Chris@133 1774
Chris@133 1775 if (m_vthumb && m_vthumb->isVisible()) {
Chris@133 1776 m_vthumb->setValue(layer->getCurrentVerticalZoomStep());
Chris@133 1777 }
Chris@133 1778 }
Chris@133 1779 }
Chris@133 1780
Chris@189 1781 void
Chris@189 1782 Pane::updateContextHelp(const QPoint *pos)
Chris@189 1783 {
Chris@189 1784 QString help = "";
Chris@189 1785
Chris@189 1786 if (m_clickedInRange) {
Chris@189 1787 emit contextHelpChanged("");
Chris@189 1788 return;
Chris@189 1789 }
Chris@189 1790
Chris@189 1791 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@189 1792 if (m_manager) mode = m_manager->getToolMode();
Chris@189 1793
Chris@189 1794 bool editable = false;
Chris@189 1795 Layer *layer = getSelectedLayer();
Chris@189 1796 if (layer && layer->isLayerEditable()) {
Chris@189 1797 editable = true;
Chris@189 1798 }
Chris@189 1799
Chris@189 1800 if (mode == ViewManager::NavigateMode) {
Chris@189 1801
Chris@189 1802 help = tr("Click and drag to navigate");
Chris@189 1803
Chris@189 1804 } else if (mode == ViewManager::SelectMode) {
Chris@189 1805
Chris@217 1806 if (!hasTopLayerTimeXAxis()) return;
Chris@217 1807
Chris@189 1808 bool haveSelection = (m_manager && !m_manager->getSelections().empty());
Chris@189 1809
Chris@189 1810 if (haveSelection) {
Chris@189 1811 if (editable) {
Chris@189 1812 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 1813 } else {
Chris@189 1814 help = tr("Click and drag to select a range; hold Ctrl for multi-select; middle-click and drag to navigate");
Chris@189 1815 }
Chris@189 1816
Chris@189 1817 if (pos) {
Chris@189 1818 bool closeToLeft = false, closeToRight = false;
Chris@189 1819 Selection selection = getSelectionAt(pos->x(), closeToLeft, closeToRight);
Chris@189 1820 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@189 1821
Chris@189 1822 help = tr("Click and drag to move the selection boundary");
Chris@189 1823 }
Chris@189 1824 }
Chris@189 1825 } else {
Chris@189 1826 if (editable) {
Chris@189 1827 help = tr("Click and drag to select a range; hold Shift to avoid snapping to items; middle-click to navigate");
Chris@189 1828 } else {
Chris@189 1829 help = tr("Click and drag to select a range; middle-click and drag to navigate");
Chris@189 1830 }
Chris@189 1831 }
Chris@189 1832
Chris@189 1833 } else if (mode == ViewManager::DrawMode) {
Chris@189 1834
Chris@189 1835 //!!! could call through to a layer function to find out exact meaning
Chris@189 1836 if (editable) {
Chris@189 1837 help = tr("Click to add a new item in the active layer");
Chris@189 1838 }
Chris@189 1839
Chris@189 1840 } else if (mode == ViewManager::EditMode) {
Chris@189 1841
Chris@189 1842 //!!! could call through to layer
Chris@189 1843 if (editable) {
Chris@189 1844 help = tr("Click and drag an item in the active layer to move it");
Chris@189 1845 if (pos) {
Chris@189 1846 bool closeToLeft = false, closeToRight = false;
Chris@189 1847 Selection selection = getSelectionAt(pos->x(), closeToLeft, closeToRight);
Chris@189 1848 if (!selection.isEmpty()) {
Chris@189 1849 help = tr("Click and drag to move all items in the selected range");
Chris@189 1850 }
Chris@189 1851 }
Chris@189 1852 }
Chris@189 1853 }
Chris@189 1854
Chris@189 1855 emit contextHelpChanged(help);
Chris@189 1856 }
Chris@189 1857
Chris@189 1858 void
Chris@189 1859 Pane::mouseEnteredWidget()
Chris@189 1860 {
Chris@189 1861 QWidget *w = dynamic_cast<QWidget *>(sender());
Chris@189 1862 if (!w) return;
Chris@189 1863
Chris@189 1864 if (w == m_vpan) {
Chris@189 1865 emit contextHelpChanged(tr("Click and drag to adjust the visible range of the vertical scale"));
Chris@189 1866 } else if (w == m_vthumb) {
Chris@189 1867 emit contextHelpChanged(tr("Click and drag to adjust the vertical zoom level"));
Chris@189 1868 } else if (w == m_hthumb) {
Chris@189 1869 emit contextHelpChanged(tr("Click and drag to adjust the horizontal zoom level"));
Chris@189 1870 } else if (w == m_reset) {
Chris@189 1871 emit contextHelpChanged(tr("Reset horizontal and vertical zoom levels to their defaults"));
Chris@189 1872 }
Chris@189 1873 }
Chris@189 1874
Chris@189 1875 void
Chris@189 1876 Pane::mouseLeftWidget()
Chris@189 1877 {
Chris@189 1878 emit contextHelpChanged("");
Chris@189 1879 }
Chris@189 1880
Chris@127 1881 QString
Chris@127 1882 Pane::toXmlString(QString indent, QString extraAttributes) const
Chris@127 1883 {
Chris@127 1884 return View::toXmlString
Chris@127 1885 (indent,
Chris@127 1886 QString("type=\"pane\" centreLineVisible=\"%1\" height=\"%2\" %3")
Chris@127 1887 .arg(m_centreLineVisible).arg(height()).arg(extraAttributes));
Chris@127 1888 }
Chris@127 1889
Chris@127 1890