annotate view/Pane.cpp @ 173:9c40dc10c88c

* Rename the existing Panner to Overview (big duh!) * Fixes to the new Panner.
author Chris Cannam
date Thu, 19 Oct 2006 09:57:27 +0000
parents d0b95a8cac96
children 96b8a790730a
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@127 7 This file copyright 2006 Chris Cannam.
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@133 37
Chris@127 38 using std::cerr;
Chris@127 39 using std::endl;
Chris@127 40
Chris@127 41 Pane::Pane(QWidget *w) :
Chris@127 42 View(w, true),
Chris@127 43 m_identifyFeatures(false),
Chris@127 44 m_clickedInRange(false),
Chris@127 45 m_shiftPressed(false),
Chris@127 46 m_ctrlPressed(false),
Chris@127 47 m_navigating(false),
Chris@127 48 m_resizing(false),
Chris@133 49 m_centreLineVisible(true),
Chris@133 50 m_headsUpDisplay(0)
Chris@127 51 {
Chris@127 52 setObjectName("Pane");
Chris@127 53 setMouseTracking(true);
Chris@133 54
Chris@133 55 updateHeadsUpDisplay();
Chris@133 56 }
Chris@133 57
Chris@133 58 void
Chris@133 59 Pane::updateHeadsUpDisplay()
Chris@133 60 {
Chris@132 61 /*
Chris@132 62 int count = 0;
Chris@132 63 int currentLevel = 1;
Chris@132 64 int level = 1;
Chris@132 65 while (true) {
Chris@132 66 if (getZoomLevel() == level) currentLevel = count;
Chris@132 67 int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@132 68 ZoomConstraint::RoundUp);
Chris@132 69 if (newLevel == level) break;
Chris@132 70 if (newLevel == 131072) break; //!!! just because
Chris@132 71 level = newLevel;
Chris@132 72 ++count;
Chris@132 73 }
Chris@132 74
Chris@132 75 std::cerr << "Have " << count+1 << " zoom levels" << std::endl;
Chris@132 76 */
Chris@133 77
Chris@133 78 if (!m_headsUpDisplay) {
Chris@133 79
Chris@133 80 m_headsUpDisplay = new QFrame(this);
Chris@133 81
Chris@133 82 QGridLayout *layout = new QGridLayout;
Chris@133 83 layout->setMargin(0);
Chris@133 84 layout->setSpacing(0);
Chris@133 85 m_headsUpDisplay->setLayout(layout);
Chris@133 86
Chris@133 87 m_hthumb = new Thumbwheel(Qt::Horizontal);
Chris@173 88 layout->addWidget(m_hthumb, 1, 0, 1, 2);
Chris@133 89 m_hthumb->setFixedWidth(70);
Chris@133 90 m_hthumb->setFixedHeight(16);
Chris@133 91 m_hthumb->setDefaultValue(0);
Chris@165 92 m_hthumb->setSpeed(0.6);
Chris@133 93 connect(m_hthumb, SIGNAL(valueChanged(int)), this,
Chris@133 94 SLOT(horizontalThumbwheelMoved(int)));
Chris@172 95
Chris@172 96 m_vpan = new Panner;
Chris@172 97 layout->addWidget(m_vpan, 0, 1);
Chris@173 98 m_vpan->setFixedWidth(12);
Chris@172 99 m_vpan->setFixedHeight(70);
Chris@173 100 // m_vpan->setRectExtents(0.1, 0.1, 0.4, 0.4);
Chris@172 101
Chris@133 102 m_vthumb = new Thumbwheel(Qt::Vertical);
Chris@172 103 layout->addWidget(m_vthumb, 0, 2);
Chris@133 104 m_vthumb->setFixedWidth(16);
Chris@133 105 m_vthumb->setFixedHeight(70);
Chris@133 106 connect(m_vthumb, SIGNAL(valueChanged(int)), this,
Chris@133 107 SLOT(verticalThumbwheelMoved(int)));
Chris@133 108
Chris@133 109 QPushButton *reset = new QPushButton;
Chris@133 110 reset->setFixedHeight(16);
Chris@133 111 reset->setFixedWidth(16);
Chris@172 112 layout->addWidget(reset, 1, 2);
Chris@133 113 connect(reset, SIGNAL(clicked()), m_hthumb, SLOT(resetToDefault()));
Chris@133 114 connect(reset, SIGNAL(clicked()), m_vthumb, SLOT(resetToDefault()));
Chris@133 115 }
Chris@133 116
Chris@133 117 int count = 0;
Chris@133 118 int current = 0;
Chris@133 119 int level = 1;
Chris@133 120
Chris@137 121 //!!! pull out into function (presumably in View)
Chris@137 122 bool haveConstraint = false;
Chris@137 123 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end();
Chris@137 124 ++i) {
Chris@137 125 if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) {
Chris@137 126 haveConstraint = true;
Chris@137 127 break;
Chris@137 128 }
Chris@137 129 }
Chris@137 130
Chris@137 131 if (haveConstraint) {
Chris@137 132 while (true) {
Chris@137 133 if (getZoomLevel() == level) current = count;
Chris@137 134 int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@137 135 ZoomConstraint::RoundUp);
Chris@137 136 if (newLevel == level) break;
Chris@137 137 level = newLevel;
Chris@137 138 if (++count == 50) break;
Chris@137 139 }
Chris@137 140 } else {
Chris@137 141 // if we have no particular constraints, we can really spread out
Chris@137 142 while (true) {
Chris@137 143 if (getZoomLevel() >= level) current = count;
Chris@137 144 int step = level / 10;
Chris@137 145 int pwr = 0;
Chris@137 146 while (step > 0) {
Chris@137 147 ++pwr;
Chris@137 148 step /= 2;
Chris@137 149 }
Chris@137 150 step = 1;
Chris@137 151 while (pwr > 0) {
Chris@137 152 step *= 2;
Chris@137 153 --pwr;
Chris@137 154 }
Chris@154 155 // std::cerr << level << std::endl;
Chris@137 156 level += step;
Chris@137 157 if (++count == 100 || level > 262144) break;
Chris@137 158 }
Chris@133 159 }
Chris@133 160
Chris@133 161 // std::cerr << "Have " << count << " zoom levels" << std::endl;
Chris@133 162
Chris@133 163 m_hthumb->setMinimumValue(0);
Chris@133 164 m_hthumb->setMaximumValue(count);
Chris@133 165 m_hthumb->setValue(count - current);
Chris@133 166
Chris@133 167 // std::cerr << "set value to " << count-current << std::endl;
Chris@133 168
Chris@133 169 // std::cerr << "default value is " << m_hthumb->getDefaultValue() << std::endl;
Chris@133 170
Chris@133 171 if (count != 50 && m_hthumb->getDefaultValue() == 0) {
Chris@133 172 m_hthumb->setDefaultValue(count - current);
Chris@133 173 // std::cerr << "set default value to " << m_hthumb->getDefaultValue() << std::endl;
Chris@133 174 }
Chris@133 175
Chris@133 176 Layer *layer = 0;
Chris@133 177 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@133 178 if (layer) {
Chris@133 179 int defaultStep = 0;
Chris@133 180 int max = layer->getVerticalZoomSteps(defaultStep);
Chris@133 181 if (max == 0) {
Chris@133 182 m_vthumb->hide();
Chris@133 183 } else {
Chris@133 184 m_vthumb->show();
Chris@133 185 m_vthumb->setMinimumValue(0);
Chris@133 186 m_vthumb->setMaximumValue(max);
Chris@133 187 m_vthumb->setDefaultValue(defaultStep);
Chris@133 188 m_vthumb->setValue(layer->getCurrentVerticalZoomStep());
Chris@135 189
Chris@135 190 std::cerr << "Vertical thumbwheel: min 0, max " << max
Chris@135 191 << ", default " << defaultStep << ", value "
Chris@135 192 << m_vthumb->getValue() << std::endl;
Chris@135 193
Chris@133 194 }
Chris@133 195 }
Chris@133 196
Chris@133 197 if (m_manager && m_manager->getZoomWheelsEnabled() &&
Chris@133 198 width() > 120 && height() > 100) {
Chris@165 199 if (!m_headsUpDisplay->isVisible()) {
Chris@165 200 m_headsUpDisplay->show();
Chris@165 201 connect(m_manager, SIGNAL(zoomLevelChanged()),
Chris@165 202 this, SLOT(zoomLevelChanged()));
Chris@165 203 }
Chris@133 204 if (m_vthumb->isVisible()) {
Chris@133 205 m_headsUpDisplay->move(width() - 86, height() - 86);
Chris@133 206 } else {
Chris@133 207 m_headsUpDisplay->move(width() - 86, height() - 51);
Chris@133 208 }
Chris@133 209 } else {
Chris@133 210 m_headsUpDisplay->hide();
Chris@133 211 if (m_manager) {
Chris@133 212 disconnect(m_manager, SIGNAL(zoomLevelChanged()),
Chris@133 213 this, SLOT(zoomLevelChanged()));
Chris@133 214 }
Chris@133 215 }
Chris@127 216 }
Chris@127 217
Chris@127 218 bool
Chris@127 219 Pane::shouldIlluminateLocalFeatures(const Layer *layer, QPoint &pos) const
Chris@127 220 {
Chris@127 221 QPoint discard;
Chris@127 222 bool b0, b1;
Chris@127 223
Chris@127 224 if (layer == getSelectedLayer() &&
Chris@127 225 !shouldIlluminateLocalSelection(discard, b0, b1)) {
Chris@127 226
Chris@127 227 pos = m_identifyPoint;
Chris@127 228 return m_identifyFeatures;
Chris@127 229 }
Chris@127 230
Chris@127 231 return false;
Chris@127 232 }
Chris@127 233
Chris@127 234 bool
Chris@127 235 Pane::shouldIlluminateLocalSelection(QPoint &pos,
Chris@127 236 bool &closeToLeft,
Chris@127 237 bool &closeToRight) const
Chris@127 238 {
Chris@127 239 if (m_identifyFeatures &&
Chris@127 240 m_manager &&
Chris@127 241 m_manager->getToolMode() == ViewManager::EditMode &&
Chris@127 242 !m_manager->getSelections().empty() &&
Chris@127 243 !selectionIsBeingEdited()) {
Chris@127 244
Chris@127 245 Selection s(getSelectionAt(m_identifyPoint.x(),
Chris@127 246 closeToLeft, closeToRight));
Chris@127 247
Chris@127 248 if (!s.isEmpty()) {
Chris@127 249 if (getSelectedLayer() && getSelectedLayer()->isLayerEditable()) {
Chris@127 250
Chris@127 251 pos = m_identifyPoint;
Chris@127 252 return true;
Chris@127 253 }
Chris@127 254 }
Chris@127 255 }
Chris@127 256
Chris@127 257 return false;
Chris@127 258 }
Chris@127 259
Chris@127 260 bool
Chris@127 261 Pane::selectionIsBeingEdited() const
Chris@127 262 {
Chris@127 263 if (!m_editingSelection.isEmpty()) {
Chris@127 264 if (m_mousePos != m_clickPos &&
Chris@127 265 getFrameForX(m_mousePos.x()) != getFrameForX(m_clickPos.x())) {
Chris@127 266 return true;
Chris@127 267 }
Chris@127 268 }
Chris@127 269 return false;
Chris@127 270 }
Chris@127 271
Chris@127 272 void
Chris@127 273 Pane::setCentreLineVisible(bool visible)
Chris@127 274 {
Chris@127 275 m_centreLineVisible = visible;
Chris@127 276 update();
Chris@127 277 }
Chris@127 278
Chris@127 279 void
Chris@127 280 Pane::paintEvent(QPaintEvent *e)
Chris@127 281 {
Chris@127 282 // Profiler profiler("Pane::paintEvent", true);
Chris@127 283
Chris@127 284 QPainter paint;
Chris@127 285
Chris@127 286 QRect r(rect());
Chris@127 287
Chris@127 288 if (e) {
Chris@127 289 r = e->rect();
Chris@127 290 }
Chris@127 291 /*
Chris@127 292 paint.begin(this);
Chris@127 293 paint.setClipRect(r);
Chris@127 294
Chris@127 295 if (hasLightBackground()) {
Chris@127 296 paint.setPen(Qt::white);
Chris@127 297 paint.setBrush(Qt::white);
Chris@127 298 } else {
Chris@127 299 paint.setPen(Qt::black);
Chris@127 300 paint.setBrush(Qt::black);
Chris@127 301 }
Chris@127 302 paint.drawRect(r);
Chris@127 303
Chris@127 304 paint.end();
Chris@127 305 */
Chris@127 306 View::paintEvent(e);
Chris@127 307
Chris@127 308 paint.begin(this);
Chris@127 309
Chris@127 310 if (e) {
Chris@127 311 paint.setClipRect(r);
Chris@127 312 }
Chris@127 313
Chris@127 314 const Model *waveformModel = 0; // just for reporting purposes
Chris@127 315 int verticalScaleWidth = 0;
Chris@127 316
Chris@127 317 int fontHeight = paint.fontMetrics().height();
Chris@127 318 int fontAscent = paint.fontMetrics().ascent();
Chris@127 319
Chris@127 320 if (m_manager &&
Chris@127 321 !m_manager->isPlaying() &&
Chris@127 322 m_manager->getToolMode() == ViewManager::SelectMode) {
Chris@127 323
Chris@127 324 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@127 325 --vi;
Chris@127 326
Chris@127 327 std::vector<QRect> crosshairExtents;
Chris@127 328
Chris@127 329 if ((*vi)->getCrosshairExtents(this, paint, m_identifyPoint,
Chris@127 330 crosshairExtents)) {
Chris@127 331 (*vi)->paintCrosshairs(this, paint, m_identifyPoint);
Chris@127 332 break;
Chris@127 333 } else if ((*vi)->isLayerOpaque()) {
Chris@127 334 break;
Chris@127 335 }
Chris@127 336 }
Chris@127 337 }
Chris@127 338
Chris@127 339 for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@127 340 --vi;
Chris@127 341
Chris@127 342 if (dynamic_cast<WaveformLayer *>(*vi)) {
Chris@127 343 waveformModel = (*vi)->getModel();
Chris@127 344 }
Chris@127 345
Chris@127 346 if (!m_manager ||
Chris@127 347 m_manager->getOverlayMode() == ViewManager::NoOverlays) {
Chris@127 348 break;
Chris@127 349 }
Chris@127 350
Chris@127 351 verticalScaleWidth = (*vi)->getVerticalScaleWidth(this, paint);
Chris@127 352
Chris@127 353 if (verticalScaleWidth > 0 && r.left() < verticalScaleWidth) {
Chris@127 354
Chris@127 355 // Profiler profiler("Pane::paintEvent - painting vertical scale", true);
Chris@127 356
Chris@127 357 // std::cerr << "Pane::paintEvent: calling paint.save() in vertical scale block" << std::endl;
Chris@127 358 paint.save();
Chris@127 359
Chris@127 360 paint.setPen(Qt::black);
Chris@127 361 paint.setBrush(Qt::white);
Chris@127 362 paint.drawRect(0, -1, verticalScaleWidth, height()+1);
Chris@127 363
Chris@127 364 paint.setBrush(Qt::NoBrush);
Chris@127 365 (*vi)->paintVerticalScale
Chris@127 366 (this, paint, QRect(0, 0, verticalScaleWidth, height()));
Chris@127 367
Chris@127 368 paint.restore();
Chris@127 369 }
Chris@127 370
Chris@127 371 if (m_identifyFeatures) {
Chris@127 372
Chris@127 373 QPoint pos = m_identifyPoint;
Chris@127 374 QString desc = (*vi)->getFeatureDescription(this, pos);
Chris@127 375
Chris@127 376 if (desc != "") {
Chris@127 377
Chris@127 378 paint.save();
Chris@127 379
Chris@127 380 int tabStop =
Chris@127 381 paint.fontMetrics().width(tr("Some lengthy prefix:"));
Chris@127 382
Chris@127 383 QRect boundingRect =
Chris@127 384 paint.fontMetrics().boundingRect
Chris@127 385 (rect(),
Chris@127 386 Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs,
Chris@127 387 desc, tabStop);
Chris@127 388
Chris@127 389 if (hasLightBackground()) {
Chris@127 390 paint.setPen(Qt::NoPen);
Chris@127 391 paint.setBrush(QColor(250, 250, 250, 200));
Chris@127 392 } else {
Chris@127 393 paint.setPen(Qt::NoPen);
Chris@127 394 paint.setBrush(QColor(50, 50, 50, 200));
Chris@127 395 }
Chris@127 396
Chris@127 397 int extra = paint.fontMetrics().descent();
Chris@127 398 paint.drawRect(width() - boundingRect.width() - 10 - extra,
Chris@127 399 10 - extra,
Chris@127 400 boundingRect.width() + 2 * extra,
Chris@127 401 boundingRect.height() + extra);
Chris@127 402
Chris@127 403 if (hasLightBackground()) {
Chris@127 404 paint.setPen(QColor(150, 20, 0));
Chris@127 405 } else {
Chris@127 406 paint.setPen(QColor(255, 150, 100));
Chris@127 407 }
Chris@127 408
Chris@127 409 QTextOption option;
Chris@127 410 option.setWrapMode(QTextOption::NoWrap);
Chris@127 411 option.setAlignment(Qt::AlignRight | Qt::AlignTop);
Chris@127 412 option.setTabStop(tabStop);
Chris@127 413 paint.drawText(QRectF(width() - boundingRect.width() - 10, 10,
Chris@127 414 boundingRect.width(),
Chris@127 415 boundingRect.height()),
Chris@127 416 desc,
Chris@127 417 option);
Chris@127 418
Chris@127 419 paint.restore();
Chris@127 420 }
Chris@127 421 }
Chris@127 422
Chris@127 423 break;
Chris@127 424 }
Chris@127 425
Chris@127 426 int sampleRate = getModelsSampleRate();
Chris@127 427 paint.setBrush(Qt::NoBrush);
Chris@127 428
Chris@127 429 if (m_centreLineVisible) {
Chris@127 430
Chris@127 431 if (hasLightBackground()) {
Chris@127 432 paint.setPen(QColor(50, 50, 50));
Chris@127 433 } else {
Chris@127 434 paint.setPen(QColor(200, 200, 200));
Chris@127 435 }
Chris@127 436 paint.drawLine(width() / 2, 0, width() / 2, height() - 1);
Chris@127 437
Chris@127 438 paint.setPen(QColor(50, 50, 50));
Chris@127 439
Chris@127 440 int y = height() - fontHeight
Chris@127 441 + fontAscent - 6;
Chris@127 442
Chris@127 443 LayerList::iterator vi = m_layers.end();
Chris@127 444
Chris@127 445 if (vi != m_layers.begin()) {
Chris@127 446
Chris@127 447 switch ((*--vi)->getPreferredFrameCountPosition()) {
Chris@127 448
Chris@127 449 case Layer::PositionTop:
Chris@127 450 y = fontAscent + 6;
Chris@127 451 break;
Chris@127 452
Chris@127 453 case Layer::PositionMiddle:
Chris@127 454 y = (height() - fontHeight) / 2
Chris@127 455 + fontAscent;
Chris@127 456 break;
Chris@127 457
Chris@127 458 case Layer::PositionBottom:
Chris@127 459 // y already set correctly
Chris@127 460 break;
Chris@127 461 }
Chris@127 462 }
Chris@127 463
Chris@127 464 if (m_manager &&
Chris@127 465 m_manager->getOverlayMode() != ViewManager::NoOverlays) {
Chris@127 466
Chris@127 467 if (sampleRate) {
Chris@127 468
Chris@127 469 QString text(QString::fromStdString
Chris@127 470 (RealTime::frame2RealTime
Chris@127 471 (m_centreFrame, sampleRate).toText(true)));
Chris@127 472
Chris@127 473 int tw = paint.fontMetrics().width(text);
Chris@127 474 int x = width()/2 - 4 - tw;
Chris@127 475
Chris@127 476 drawVisibleText(paint, x, y, text, OutlinedText);
Chris@127 477 }
Chris@127 478
Chris@127 479 QString text = QString("%1").arg(m_centreFrame);
Chris@127 480
Chris@127 481 int tw = paint.fontMetrics().width(text);
Chris@127 482 int x = width()/2 + 4;
Chris@127 483
Chris@127 484 drawVisibleText(paint, x, y, text, OutlinedText);
Chris@127 485 }
Chris@127 486
Chris@127 487 } else {
Chris@127 488
Chris@127 489 paint.setPen(QColor(50, 50, 50));
Chris@127 490 }
Chris@127 491
Chris@127 492 if (waveformModel &&
Chris@127 493 m_manager &&
Chris@127 494 m_manager->getOverlayMode() != ViewManager::NoOverlays &&
Chris@127 495 r.y() + r.height() >= height() - fontHeight - 6) {
Chris@127 496
Chris@150 497 size_t modelRate = waveformModel->getSampleRate();
Chris@127 498 size_t mainModelRate = m_manager->getMainModelSampleRate();
Chris@127 499 size_t playbackRate = m_manager->getPlaybackSampleRate();
Chris@127 500
Chris@127 501 QString srNote = "";
Chris@127 502
Chris@127 503 // Show (R) for waveform models that will be resampled on
Chris@127 504 // playback, and (X) for waveform models that will be played
Chris@127 505 // at the wrong rate because their rate differs from that of
Chris@127 506 // the main model.
Chris@127 507
Chris@150 508 if (modelRate == mainModelRate) {
Chris@150 509 if (modelRate != playbackRate) srNote = " " + tr("(R)");
Chris@127 510 } else {
Chris@150 511 // std::cerr << "Sample rate = " << modelRate << ", main model rate = " << mainModelRate << std::endl;
Chris@127 512 srNote = " " + tr("(X)");
Chris@127 513 }
Chris@127 514
Chris@127 515 QString desc = tr("%1 / %2Hz%3")
Chris@127 516 .arg(RealTime::frame2RealTime(waveformModel->getEndFrame(),
Chris@127 517 sampleRate)
Chris@127 518 .toText(false).c_str())
Chris@150 519 .arg(modelRate)
Chris@127 520 .arg(srNote);
Chris@127 521
Chris@127 522 if (r.x() < verticalScaleWidth + 5 + paint.fontMetrics().width(desc)) {
Chris@127 523 drawVisibleText(paint, verticalScaleWidth + 5,
Chris@127 524 height() - fontHeight + fontAscent - 6,
Chris@127 525 desc, OutlinedText);
Chris@127 526 }
Chris@127 527 }
Chris@127 528
Chris@127 529 if (m_manager &&
Chris@127 530 m_manager->getOverlayMode() == ViewManager::AllOverlays &&
Chris@127 531 r.y() + r.height() >= height() - m_layers.size() * fontHeight - 6) {
Chris@127 532
Chris@127 533 std::vector<QString> texts;
Chris@127 534 int maxTextWidth = 0;
Chris@127 535
Chris@127 536 for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@127 537
Chris@127 538 QString text = (*i)->getLayerPresentationName();
Chris@127 539 int tw = paint.fontMetrics().width(text);
Chris@127 540 bool reduced = false;
Chris@127 541 while (tw > width() / 3 && text.length() > 4) {
Chris@127 542 if (!reduced && text.length() > 8) {
Chris@127 543 text = text.left(text.length() - 4);
Chris@127 544 } else {
Chris@127 545 text = text.left(text.length() - 2);
Chris@127 546 }
Chris@127 547 reduced = true;
Chris@127 548 tw = paint.fontMetrics().width(text + "...");
Chris@127 549 }
Chris@127 550 if (reduced) {
Chris@127 551 texts.push_back(text + "...");
Chris@127 552 } else {
Chris@127 553 texts.push_back(text);
Chris@127 554 }
Chris@127 555 if (tw > maxTextWidth) maxTextWidth = tw;
Chris@127 556 }
Chris@127 557
Chris@127 558 int lly = height() - 6;
Chris@133 559 int llx = width() - maxTextWidth - 5;
Chris@127 560
Chris@133 561 if (m_manager->getZoomWheelsEnabled()) {
Chris@133 562 lly -= 20;
Chris@173 563 llx -= 36;
Chris@133 564 }
Chris@133 565
Chris@133 566 if (r.x() + r.width() >= llx) {
Chris@127 567
Chris@127 568 for (int i = 0; i < texts.size(); ++i) {
Chris@127 569
Chris@127 570 if (i == texts.size() - 1) {
Chris@127 571 paint.setPen(Qt::black);
Chris@127 572 }
Chris@127 573
Chris@133 574 drawVisibleText(paint, llx,
Chris@127 575 lly - fontHeight + fontAscent,
Chris@127 576 texts[i], OutlinedText);
Chris@127 577
Chris@127 578 lly -= fontHeight;
Chris@127 579 }
Chris@127 580 }
Chris@127 581 }
Chris@127 582
Chris@127 583 if (m_clickedInRange && m_shiftPressed) {
Chris@127 584 if (m_manager && (m_manager->getToolMode() == ViewManager::NavigateMode)) {
Chris@127 585 //!!! be nice if this looked a bit more in keeping with the
Chris@127 586 //selection block
Chris@127 587 paint.setPen(Qt::blue);
Chris@127 588 paint.drawRect(m_clickPos.x(), m_clickPos.y(),
Chris@127 589 m_mousePos.x() - m_clickPos.x(),
Chris@127 590 m_mousePos.y() - m_clickPos.y());
Chris@127 591 }
Chris@127 592 }
Chris@127 593
Chris@127 594 if (selectionIsBeingEdited()) {
Chris@127 595
Chris@127 596 int offset = m_mousePos.x() - m_clickPos.x();
Chris@127 597 int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@127 598 int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@127 599
Chris@127 600 if (m_editingSelectionEdge < 0) {
Chris@127 601 p1 = getXForFrame(m_editingSelection.getEndFrame());
Chris@127 602 } else if (m_editingSelectionEdge > 0) {
Chris@127 603 p0 = getXForFrame(m_editingSelection.getStartFrame());
Chris@127 604 }
Chris@127 605
Chris@127 606 paint.save();
Chris@127 607 if (hasLightBackground()) {
Chris@127 608 paint.setPen(QPen(Qt::black, 2));
Chris@127 609 } else {
Chris@127 610 paint.setPen(QPen(Qt::white, 2));
Chris@127 611 }
Chris@127 612
Chris@127 613 //!!! duplicating display policy with View::drawSelections
Chris@127 614
Chris@127 615 if (m_editingSelectionEdge < 0) {
Chris@127 616 paint.drawLine(p0, 1, p1, 1);
Chris@127 617 paint.drawLine(p0, 0, p0, height());
Chris@127 618 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@127 619 } else if (m_editingSelectionEdge > 0) {
Chris@127 620 paint.drawLine(p0, 1, p1, 1);
Chris@127 621 paint.drawLine(p1, 0, p1, height());
Chris@127 622 paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@127 623 } else {
Chris@127 624 paint.setBrush(Qt::NoBrush);
Chris@127 625 paint.drawRect(p0, 1, p1 - p0, height() - 2);
Chris@127 626 }
Chris@127 627 paint.restore();
Chris@127 628 }
Chris@127 629
Chris@127 630 paint.end();
Chris@127 631 }
Chris@127 632
Chris@127 633 Selection
Chris@127 634 Pane::getSelectionAt(int x, bool &closeToLeftEdge, bool &closeToRightEdge) const
Chris@127 635 {
Chris@127 636 closeToLeftEdge = closeToRightEdge = false;
Chris@127 637
Chris@127 638 if (!m_manager) return Selection();
Chris@127 639
Chris@127 640 long testFrame = getFrameForX(x - 5);
Chris@127 641 if (testFrame < 0) {
Chris@127 642 testFrame = getFrameForX(x);
Chris@127 643 if (testFrame < 0) return Selection();
Chris@127 644 }
Chris@127 645
Chris@127 646 Selection selection = m_manager->getContainingSelection(testFrame, true);
Chris@127 647 if (selection.isEmpty()) return selection;
Chris@127 648
Chris@127 649 int lx = getXForFrame(selection.getStartFrame());
Chris@127 650 int rx = getXForFrame(selection.getEndFrame());
Chris@127 651
Chris@127 652 int fuzz = 2;
Chris@127 653 if (x < lx - fuzz || x > rx + fuzz) return Selection();
Chris@127 654
Chris@127 655 int width = rx - lx;
Chris@127 656 fuzz = 3;
Chris@127 657 if (width < 12) fuzz = width / 4;
Chris@127 658 if (fuzz < 1) fuzz = 1;
Chris@127 659
Chris@127 660 if (x < lx + fuzz) closeToLeftEdge = true;
Chris@127 661 if (x > rx - fuzz) closeToRightEdge = true;
Chris@127 662
Chris@127 663 return selection;
Chris@127 664 }
Chris@127 665
Chris@127 666 void
Chris@127 667 Pane::mousePressEvent(QMouseEvent *e)
Chris@127 668 {
Chris@127 669 if (e->buttons() & Qt::RightButton) {
Chris@127 670 emit rightButtonMenuRequested(mapToGlobal(e->pos()));
Chris@127 671 return;
Chris@127 672 }
Chris@127 673
Chris@127 674 m_clickPos = e->pos();
Chris@127 675 m_clickedInRange = true;
Chris@127 676 m_editingSelection = Selection();
Chris@127 677 m_editingSelectionEdge = 0;
Chris@127 678 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@127 679 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@150 680 m_dragMode = UnresolvedDrag;
Chris@127 681
Chris@127 682 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 683 if (m_manager) mode = m_manager->getToolMode();
Chris@127 684
Chris@127 685 m_navigating = false;
Chris@127 686
Chris@127 687 if (mode == ViewManager::NavigateMode || (e->buttons() & Qt::MidButton)) {
Chris@127 688
Chris@127 689 if (mode != ViewManager::NavigateMode) {
Chris@127 690 setCursor(Qt::PointingHandCursor);
Chris@127 691 }
Chris@127 692
Chris@127 693 m_navigating = true;
Chris@127 694 m_dragCentreFrame = m_centreFrame;
Chris@127 695
Chris@136 696 //!!! pull out into function to go with mouse move code
Chris@136 697
Chris@136 698 m_dragStartMinValue = 0;
Chris@136 699
Chris@136 700 Layer *layer = 0;
Chris@136 701 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@136 702
Chris@136 703 if (layer) {
Chris@136 704 float min = 0.f, max = 0.f;
Chris@136 705 if (layer->getDisplayExtents(min, max)) {
Chris@136 706 m_dragStartMinValue = min;
Chris@136 707 }
Chris@136 708 }
Chris@136 709
Chris@127 710 } else if (mode == ViewManager::SelectMode) {
Chris@127 711
Chris@127 712 bool closeToLeft = false, closeToRight = false;
Chris@127 713 Selection selection = getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@127 714
Chris@127 715 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@127 716
Chris@127 717 m_manager->removeSelection(selection);
Chris@127 718
Chris@127 719 if (closeToLeft) {
Chris@127 720 m_selectionStartFrame = selection.getEndFrame();
Chris@127 721 } else {
Chris@127 722 m_selectionStartFrame = selection.getStartFrame();
Chris@127 723 }
Chris@127 724
Chris@127 725 m_manager->setInProgressSelection(selection, false);
Chris@127 726 m_resizing = true;
Chris@127 727
Chris@127 728 } else {
Chris@127 729
Chris@127 730 int mouseFrame = getFrameForX(e->x());
Chris@127 731 size_t resolution = 1;
Chris@127 732 int snapFrame = mouseFrame;
Chris@127 733
Chris@127 734 Layer *layer = getSelectedLayer();
Chris@127 735 if (layer && !m_shiftPressed) {
Chris@127 736 layer->snapToFeatureFrame(this, snapFrame,
Chris@127 737 resolution, Layer::SnapLeft);
Chris@127 738 }
Chris@127 739
Chris@127 740 if (snapFrame < 0) snapFrame = 0;
Chris@127 741 m_selectionStartFrame = snapFrame;
Chris@127 742 if (m_manager) {
Chris@127 743 m_manager->setInProgressSelection(Selection(snapFrame,
Chris@127 744 snapFrame + resolution),
Chris@127 745 !m_ctrlPressed);
Chris@127 746 }
Chris@127 747
Chris@127 748 m_resizing = false;
Chris@127 749 }
Chris@127 750
Chris@127 751 update();
Chris@127 752
Chris@127 753 } else if (mode == ViewManager::DrawMode) {
Chris@127 754
Chris@127 755 Layer *layer = getSelectedLayer();
Chris@127 756 if (layer && layer->isLayerEditable()) {
Chris@127 757 layer->drawStart(this, e);
Chris@127 758 }
Chris@127 759
Chris@127 760 } else if (mode == ViewManager::EditMode) {
Chris@127 761
Chris@127 762 if (!editSelectionStart(e)) {
Chris@127 763 Layer *layer = getSelectedLayer();
Chris@127 764 if (layer && layer->isLayerEditable()) {
Chris@127 765 layer->editStart(this, e);
Chris@127 766 }
Chris@127 767 }
Chris@127 768 }
Chris@127 769
Chris@127 770 emit paneInteractedWith();
Chris@127 771 }
Chris@127 772
Chris@127 773 void
Chris@127 774 Pane::mouseReleaseEvent(QMouseEvent *e)
Chris@127 775 {
Chris@127 776 if (e->buttons() & Qt::RightButton) {
Chris@127 777 return;
Chris@127 778 }
Chris@127 779
Chris@127 780 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 781 if (m_manager) mode = m_manager->getToolMode();
Chris@127 782
Chris@127 783 if (m_clickedInRange) {
Chris@127 784 mouseMoveEvent(e);
Chris@127 785 }
Chris@127 786
Chris@127 787 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@127 788
Chris@127 789 m_navigating = false;
Chris@127 790
Chris@127 791 if (mode != ViewManager::NavigateMode) {
Chris@127 792 // restore cursor
Chris@127 793 toolModeChanged();
Chris@127 794 }
Chris@127 795
Chris@127 796 if (m_shiftPressed) {
Chris@127 797
Chris@127 798 int x0 = std::min(m_clickPos.x(), m_mousePos.x());
Chris@127 799 int x1 = std::max(m_clickPos.x(), m_mousePos.x());
Chris@127 800 int w = x1 - x0;
Chris@127 801
Chris@127 802 int y0 = std::min(m_clickPos.y(), m_mousePos.y());
Chris@127 803 int y1 = std::max(m_clickPos.y(), m_mousePos.y());
Chris@127 804 // int h = y1 - y0;
Chris@127 805
Chris@127 806 long newStartFrame = getFrameForX(x0);
Chris@127 807
Chris@127 808 long visibleFrames = getEndFrame() - getStartFrame();
Chris@127 809 if (newStartFrame <= -visibleFrames) {
Chris@127 810 newStartFrame = -visibleFrames + 1;
Chris@127 811 }
Chris@127 812
Chris@127 813 if (newStartFrame >= long(getModelsEndFrame())) {
Chris@127 814 newStartFrame = getModelsEndFrame() - 1;
Chris@127 815 }
Chris@127 816
Chris@127 817 float ratio = float(w) / float(width());
Chris@127 818 // std::cerr << "ratio: " << ratio << std::endl;
Chris@127 819 size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio);
Chris@127 820 if (newZoomLevel < 1) newZoomLevel = 1;
Chris@127 821
Chris@127 822 // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl;
Chris@127 823 setZoomLevel(getZoomConstraintBlockSize(newZoomLevel));
Chris@127 824 setStartFrame(newStartFrame);
Chris@127 825
Chris@127 826 //!!! lots of faff, shouldn't be here
Chris@127 827
Chris@127 828 QString unit;
Chris@127 829 float min, max;
Chris@127 830 bool log;
Chris@127 831 Layer *layer = 0;
Chris@127 832 for (LayerList::const_iterator i = m_layers.begin();
Chris@127 833 i != m_layers.end(); ++i) {
Chris@127 834 if ((*i)->getValueExtents(min, max, log, unit) &&
Chris@127 835 (*i)->getDisplayExtents(min, max)) {
Chris@127 836 layer = *i;
Chris@127 837 break;
Chris@127 838 }
Chris@127 839 }
Chris@127 840
Chris@127 841 if (layer) {
Chris@127 842 if (log) {
Chris@127 843 min = (min < 0.0) ? -log10f(-min) : (min == 0.0) ? 0.0 : log10f(min);
Chris@127 844 max = (max < 0.0) ? -log10f(-max) : (max == 0.0) ? 0.0 : log10f(max);
Chris@127 845 }
Chris@127 846 float rmin = min + ((max - min) * (height() - y1)) / height();
Chris@127 847 float rmax = min + ((max - min) * (height() - y0)) / height();
Chris@127 848 std::cerr << "min: " << min << ", max: " << max << ", y0: " << y0 << ", y1: " << y1 << ", h: " << height() << ", rmin: " << rmin << ", rmax: " << rmax << std::endl;
Chris@127 849 if (log) {
Chris@127 850 rmin = powf(10, rmin);
Chris@127 851 rmax = powf(10, rmax);
Chris@127 852 }
Chris@127 853 std::cerr << "finally: rmin: " << rmin << ", rmax: " << rmax << " " << unit.toStdString() << std::endl;
Chris@127 854
Chris@127 855 layer->setDisplayExtents(rmin, rmax);
Chris@127 856 }
Chris@127 857
Chris@127 858 //cerr << "mouseReleaseEvent: start frame now " << m_startFrame << endl;
Chris@127 859 // update();
Chris@127 860 }
Chris@127 861
Chris@127 862 } else if (mode == ViewManager::SelectMode) {
Chris@127 863
Chris@127 864 if (m_manager && m_manager->haveInProgressSelection()) {
Chris@127 865
Chris@127 866 bool exclusive;
Chris@127 867 Selection selection = m_manager->getInProgressSelection(exclusive);
Chris@127 868
Chris@127 869 if (selection.getEndFrame() < selection.getStartFrame() + 2) {
Chris@127 870 selection = Selection();
Chris@127 871 }
Chris@127 872
Chris@127 873 m_manager->clearInProgressSelection();
Chris@127 874
Chris@127 875 if (exclusive) {
Chris@127 876 m_manager->setSelection(selection);
Chris@127 877 } else {
Chris@127 878 m_manager->addSelection(selection);
Chris@127 879 }
Chris@127 880 }
Chris@127 881
Chris@127 882 update();
Chris@127 883
Chris@127 884 } else if (mode == ViewManager::DrawMode) {
Chris@127 885
Chris@127 886 Layer *layer = getSelectedLayer();
Chris@127 887 if (layer && layer->isLayerEditable()) {
Chris@127 888 layer->drawEnd(this, e);
Chris@127 889 update();
Chris@127 890 }
Chris@127 891
Chris@127 892 } else if (mode == ViewManager::EditMode) {
Chris@127 893
Chris@127 894 if (!editSelectionEnd(e)) {
Chris@127 895 Layer *layer = getSelectedLayer();
Chris@127 896 if (layer && layer->isLayerEditable()) {
Chris@127 897 layer->editEnd(this, e);
Chris@127 898 update();
Chris@127 899 }
Chris@127 900 }
Chris@127 901 }
Chris@127 902
Chris@127 903 m_clickedInRange = false;
Chris@127 904
Chris@127 905 emit paneInteractedWith();
Chris@127 906 }
Chris@127 907
Chris@127 908 void
Chris@127 909 Pane::mouseMoveEvent(QMouseEvent *e)
Chris@127 910 {
Chris@127 911 if (e->buttons() & Qt::RightButton) {
Chris@127 912 return;
Chris@127 913 }
Chris@127 914
Chris@127 915 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 916 if (m_manager) mode = m_manager->getToolMode();
Chris@127 917
Chris@127 918 QPoint prevPoint = m_identifyPoint;
Chris@127 919 m_identifyPoint = e->pos();
Chris@127 920
Chris@127 921 if (!m_clickedInRange) {
Chris@127 922
Chris@127 923 if (mode == ViewManager::SelectMode) {
Chris@127 924 bool closeToLeft = false, closeToRight = false;
Chris@127 925 getSelectionAt(e->x(), closeToLeft, closeToRight);
Chris@127 926 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@127 927 setCursor(Qt::SizeHorCursor);
Chris@127 928 } else {
Chris@127 929 setCursor(Qt::ArrowCursor);
Chris@127 930 }
Chris@127 931 }
Chris@127 932
Chris@127 933 //!!! if (mode != ViewManager::DrawMode) {
Chris@127 934
Chris@127 935 if (!m_manager->isPlaying()) {
Chris@127 936
Chris@127 937 if (getSelectedLayer()) {
Chris@127 938
Chris@127 939 bool previouslyIdentifying = m_identifyFeatures;
Chris@127 940 m_identifyFeatures = true;
Chris@127 941
Chris@127 942 if (m_identifyFeatures != previouslyIdentifying ||
Chris@127 943 m_identifyPoint != prevPoint) {
Chris@127 944 update();
Chris@127 945 }
Chris@127 946 }
Chris@127 947
Chris@127 948 }
Chris@127 949
Chris@127 950 // }
Chris@127 951
Chris@127 952 return;
Chris@127 953 }
Chris@127 954
Chris@127 955 if (m_navigating || mode == ViewManager::NavigateMode) {
Chris@127 956
Chris@127 957 if (m_shiftPressed) {
Chris@127 958
Chris@127 959 m_mousePos = e->pos();
Chris@127 960 update();
Chris@127 961
Chris@127 962 } else {
Chris@127 963
Chris@145 964 //!!! want to do some cleverness to avoid dragging left/right
Chris@145 965 // at the same time as up/down when the user moves the mouse
Chris@145 966 // diagonally.
Chris@145 967 // e.g. have horizontal and vertical thresholds and a series
Chris@145 968 // of states: unknown, constrained, free
Chris@145 969 //
Chris@145 970 // -> when the mouse first moves we're in unknown state:
Chris@145 971 // what then? the thing we really want to avoid is moving
Chris@145 972 // a tiny amount in the wrong direction, because usually
Chris@145 973 // the problem is that to move at all is expensive -- so what
Chris@145 974 // do we do?
Chris@145 975 //
Chris@145 976 // -> when it's moved more than say 10px in h or v
Chris@145 977 // direction we lock into h or v constrained mode. If it
Chris@145 978 // moves more than say 20px in the other direction
Chris@145 979 // subsequently, then we switch into free mode.
Chris@145 980
Chris@150 981 int xdiff = e->x() - m_clickPos.x();
Chris@150 982 int ydiff = e->y() - m_clickPos.y();
Chris@150 983 int smallThreshold = 10, bigThreshold = 50;
Chris@136 984
Chris@150 985 bool canMoveVertical = true;
Chris@150 986 bool canMoveHorizontal = true;
Chris@136 987
Chris@150 988 //!!! need to test whether the layer is actually draggable
Chris@150 989 // vertically before we do any of this
Chris@136 990
Chris@150 991 if (m_dragMode == UnresolvedDrag) {
Chris@136 992
Chris@150 993 if (abs(ydiff) > smallThreshold &&
Chris@150 994 abs(ydiff) > abs(xdiff) * 2) {
Chris@150 995 m_dragMode = VerticalDrag;
Chris@150 996 } else if (abs(xdiff) > smallThreshold &&
Chris@150 997 abs(xdiff) > abs(ydiff) * 2) {
Chris@150 998 m_dragMode = HorizontalDrag;
Chris@150 999 } else if (abs(xdiff) > smallThreshold &&
Chris@150 1000 abs(ydiff) > smallThreshold) {
Chris@150 1001 m_dragMode = FreeDrag;
Chris@150 1002 } else {
Chris@150 1003 // When playing, we don't want to disturb the play
Chris@150 1004 // position too easily; when not playing, we don't
Chris@150 1005 // want to move up/down too easily
Chris@150 1006 if (m_manager && m_manager->isPlaying()) {
Chris@150 1007 canMoveHorizontal = false;
Chris@150 1008 } else {
Chris@150 1009 canMoveVertical = false;
Chris@136 1010 }
Chris@136 1011 }
Chris@136 1012 }
Chris@150 1013
Chris@150 1014 if (m_dragMode == VerticalDrag) {
Chris@150 1015 if (abs(xdiff) > bigThreshold) m_dragMode = FreeDrag;
Chris@150 1016 else canMoveHorizontal = false;
Chris@150 1017 }
Chris@150 1018
Chris@150 1019 if (m_dragMode == HorizontalDrag) {
Chris@150 1020 if (abs(ydiff) > bigThreshold) m_dragMode = FreeDrag;
Chris@150 1021 else canMoveVertical = false;
Chris@150 1022 }
Chris@150 1023
Chris@150 1024 if (canMoveHorizontal) {
Chris@150 1025
Chris@150 1026 long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x());
Chris@150 1027
Chris@150 1028 size_t newCentreFrame = m_dragCentreFrame;
Chris@150 1029
Chris@150 1030 if (frameOff < 0) {
Chris@150 1031 newCentreFrame -= frameOff;
Chris@150 1032 } else if (newCentreFrame >= size_t(frameOff)) {
Chris@150 1033 newCentreFrame -= frameOff;
Chris@150 1034 } else {
Chris@150 1035 newCentreFrame = 0;
Chris@150 1036 }
Chris@150 1037
Chris@150 1038 if (newCentreFrame >= getModelsEndFrame()) {
Chris@150 1039 newCentreFrame = getModelsEndFrame();
Chris@150 1040 if (newCentreFrame > 0) --newCentreFrame;
Chris@150 1041 }
Chris@150 1042
Chris@150 1043 if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) {
Chris@150 1044 setCentreFrame(newCentreFrame);
Chris@150 1045 }
Chris@150 1046 }
Chris@150 1047
Chris@150 1048 //!!! For drag up/down, we need to: call getValueExtents
Chris@150 1049 //and getDisplayExtents and see whether both return true
Chris@150 1050 //(we can only drag up/down if they do); and check whether
Chris@150 1051 //the ranges returned differ (likewise). Then, we know
Chris@150 1052 //the height of the layer, so...
Chris@150 1053
Chris@150 1054 //!!! this should have its own function
Chris@150 1055
Chris@150 1056 if (canMoveVertical) {
Chris@150 1057
Chris@150 1058 Layer *layer = 0;
Chris@150 1059 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@150 1060
Chris@150 1061 if (layer) {
Chris@150 1062
Chris@150 1063 float vmin = 0.f, vmax = 0.f;
Chris@150 1064 bool vlog = false;
Chris@150 1065 QString vunit;
Chris@150 1066
Chris@150 1067 float dmin = 0.f, dmax = 0.f;
Chris@150 1068
Chris@150 1069 if (layer->getValueExtents(vmin, vmax, vlog, vunit) &&
Chris@150 1070 layer->getDisplayExtents(dmin, dmax) &&
Chris@150 1071 (dmin > vmin || dmax < vmax)) {
Chris@150 1072
Chris@150 1073 std::cerr << "ydiff = " << ydiff << std::endl;
Chris@150 1074
Chris@150 1075 float perpix = (dmax - dmin) / height();
Chris@150 1076 float valdiff = ydiff * perpix;
Chris@150 1077 std::cerr << "valdiff = " << valdiff << std::endl;
Chris@150 1078
Chris@150 1079 float newmin = m_dragStartMinValue + valdiff;
Chris@150 1080 float newmax = m_dragStartMinValue + (dmax - dmin) + valdiff;
Chris@150 1081 if (newmin < vmin) {
Chris@150 1082 newmax += vmin - newmin;
Chris@150 1083 newmin += vmin - newmin;
Chris@150 1084 }
Chris@150 1085 if (newmax > vmax) {
Chris@150 1086 newmin -= newmax - vmax;
Chris@150 1087 newmax -= newmax - vmax;
Chris@150 1088 }
Chris@150 1089 std::cerr << "(" << dmin << ", " << dmax << ") -> ("
Chris@150 1090 << newmin << ", " << newmax << ") (drag start " << m_dragStartMinValue << ")" << std::endl;
Chris@150 1091 layer->setDisplayExtents(newmin, newmax);
Chris@150 1092 }
Chris@150 1093 }
Chris@150 1094 }
Chris@150 1095 }
Chris@127 1096
Chris@127 1097 } else if (mode == ViewManager::SelectMode) {
Chris@127 1098
Chris@127 1099 int mouseFrame = getFrameForX(e->x());
Chris@127 1100 size_t resolution = 1;
Chris@127 1101 int snapFrameLeft = mouseFrame;
Chris@127 1102 int snapFrameRight = mouseFrame;
Chris@127 1103
Chris@127 1104 Layer *layer = getSelectedLayer();
Chris@127 1105 if (layer && !m_shiftPressed) {
Chris@127 1106 layer->snapToFeatureFrame(this, snapFrameLeft,
Chris@127 1107 resolution, Layer::SnapLeft);
Chris@127 1108 layer->snapToFeatureFrame(this, snapFrameRight,
Chris@127 1109 resolution, Layer::SnapRight);
Chris@127 1110 }
Chris@127 1111
Chris@127 1112 // std::cerr << "snap: frame = " << mouseFrame << ", start frame = " << m_selectionStartFrame << ", left = " << snapFrameLeft << ", right = " << snapFrameRight << std::endl;
Chris@127 1113
Chris@127 1114 if (snapFrameLeft < 0) snapFrameLeft = 0;
Chris@127 1115 if (snapFrameRight < 0) snapFrameRight = 0;
Chris@127 1116
Chris@127 1117 size_t min, max;
Chris@127 1118
Chris@127 1119 if (m_selectionStartFrame > snapFrameLeft) {
Chris@127 1120 min = snapFrameLeft;
Chris@127 1121 max = m_selectionStartFrame;
Chris@127 1122 } else if (snapFrameRight > m_selectionStartFrame) {
Chris@127 1123 min = m_selectionStartFrame;
Chris@127 1124 max = snapFrameRight;
Chris@127 1125 } else {
Chris@127 1126 min = snapFrameLeft;
Chris@127 1127 max = snapFrameRight;
Chris@127 1128 }
Chris@127 1129
Chris@127 1130 if (m_manager) {
Chris@127 1131 m_manager->setInProgressSelection(Selection(min, max),
Chris@127 1132 !m_resizing && !m_ctrlPressed);
Chris@127 1133 }
Chris@127 1134
Chris@127 1135 bool doScroll = false;
Chris@127 1136 if (!m_manager) doScroll = true;
Chris@127 1137 if (!m_manager->isPlaying()) doScroll = true;
Chris@127 1138 if (m_followPlay != PlaybackScrollContinuous) doScroll = true;
Chris@127 1139
Chris@127 1140 if (doScroll) {
Chris@127 1141 int offset = mouseFrame - getStartFrame();
Chris@127 1142 int available = getEndFrame() - getStartFrame();
Chris@127 1143 if (offset >= available * 0.95) {
Chris@127 1144 int move = int(offset - available * 0.95) + 1;
Chris@127 1145 setCentreFrame(m_centreFrame + move);
Chris@127 1146 } else if (offset <= available * 0.10) {
Chris@127 1147 int move = int(available * 0.10 - offset) + 1;
Chris@127 1148 if (m_centreFrame > move) {
Chris@127 1149 setCentreFrame(m_centreFrame - move);
Chris@127 1150 } else {
Chris@127 1151 setCentreFrame(0);
Chris@127 1152 }
Chris@127 1153 }
Chris@127 1154 }
Chris@127 1155
Chris@127 1156 update();
Chris@127 1157
Chris@127 1158 } else if (mode == ViewManager::DrawMode) {
Chris@127 1159
Chris@127 1160 Layer *layer = getSelectedLayer();
Chris@127 1161 if (layer && layer->isLayerEditable()) {
Chris@127 1162 layer->drawDrag(this, e);
Chris@127 1163 }
Chris@127 1164
Chris@127 1165 } else if (mode == ViewManager::EditMode) {
Chris@127 1166
Chris@127 1167 if (!editSelectionDrag(e)) {
Chris@127 1168 Layer *layer = getSelectedLayer();
Chris@127 1169 if (layer && layer->isLayerEditable()) {
Chris@127 1170 layer->editDrag(this, e);
Chris@127 1171 }
Chris@127 1172 }
Chris@127 1173 }
Chris@127 1174 }
Chris@127 1175
Chris@127 1176 void
Chris@127 1177 Pane::mouseDoubleClickEvent(QMouseEvent *e)
Chris@127 1178 {
Chris@127 1179 if (e->buttons() & Qt::RightButton) {
Chris@127 1180 return;
Chris@127 1181 }
Chris@127 1182
Chris@127 1183 // std::cerr << "mouseDoubleClickEvent" << std::endl;
Chris@127 1184
Chris@127 1185 m_clickPos = e->pos();
Chris@127 1186 m_clickedInRange = true;
Chris@127 1187 m_shiftPressed = (e->modifiers() & Qt::ShiftModifier);
Chris@127 1188 m_ctrlPressed = (e->modifiers() & Qt::ControlModifier);
Chris@127 1189
Chris@127 1190 ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@127 1191 if (m_manager) mode = m_manager->getToolMode();
Chris@127 1192
Chris@127 1193 if (mode == ViewManager::NavigateMode ||
Chris@127 1194 mode == ViewManager::EditMode) {
Chris@127 1195
Chris@127 1196 Layer *layer = getSelectedLayer();
Chris@127 1197 if (layer && layer->isLayerEditable()) {
Chris@127 1198 layer->editOpen(this, e);
Chris@127 1199 }
Chris@127 1200 }
Chris@127 1201 }
Chris@127 1202
Chris@127 1203 void
Chris@127 1204 Pane::leaveEvent(QEvent *)
Chris@127 1205 {
Chris@127 1206 bool previouslyIdentifying = m_identifyFeatures;
Chris@127 1207 m_identifyFeatures = false;
Chris@127 1208 if (previouslyIdentifying) update();
Chris@127 1209 }
Chris@127 1210
Chris@127 1211 void
Chris@133 1212 Pane::resizeEvent(QResizeEvent *)
Chris@133 1213 {
Chris@133 1214 updateHeadsUpDisplay();
Chris@133 1215 }
Chris@133 1216
Chris@133 1217 void
Chris@127 1218 Pane::wheelEvent(QWheelEvent *e)
Chris@127 1219 {
Chris@127 1220 //std::cerr << "wheelEvent, delta " << e->delta() << std::endl;
Chris@127 1221
Chris@127 1222 int count = e->delta();
Chris@127 1223
Chris@127 1224 if (count > 0) {
Chris@127 1225 if (count >= 120) count /= 120;
Chris@127 1226 else count = 1;
Chris@127 1227 }
Chris@127 1228
Chris@127 1229 if (count < 0) {
Chris@127 1230 if (count <= -120) count /= 120;
Chris@127 1231 else count = -1;
Chris@127 1232 }
Chris@127 1233
Chris@127 1234 if (e->modifiers() & Qt::ControlModifier) {
Chris@127 1235
Chris@127 1236 // Scroll left or right, rapidly
Chris@127 1237
Chris@127 1238 if (getStartFrame() < 0 &&
Chris@127 1239 getEndFrame() >= getModelsEndFrame()) return;
Chris@127 1240
Chris@127 1241 long delta = ((width() / 2) * count * m_zoomLevel);
Chris@127 1242
Chris@127 1243 if (int(m_centreFrame) < delta) {
Chris@127 1244 setCentreFrame(0);
Chris@127 1245 } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) {
Chris@127 1246 setCentreFrame(getModelsEndFrame());
Chris@127 1247 } else {
Chris@127 1248 setCentreFrame(m_centreFrame - delta);
Chris@127 1249 }
Chris@127 1250
Chris@127 1251 } else {
Chris@127 1252
Chris@127 1253 // Zoom in or out
Chris@127 1254
Chris@127 1255 int newZoomLevel = m_zoomLevel;
Chris@127 1256
Chris@127 1257 while (count > 0) {
Chris@127 1258 if (newZoomLevel <= 2) {
Chris@127 1259 newZoomLevel = 1;
Chris@127 1260 break;
Chris@127 1261 }
Chris@127 1262 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1,
Chris@127 1263 ZoomConstraint::RoundDown);
Chris@127 1264 --count;
Chris@127 1265 }
Chris@127 1266
Chris@127 1267 while (count < 0) {
Chris@127 1268 newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1,
Chris@127 1269 ZoomConstraint::RoundUp);
Chris@127 1270 ++count;
Chris@127 1271 }
Chris@127 1272
Chris@127 1273 if (newZoomLevel != m_zoomLevel) {
Chris@127 1274 setZoomLevel(newZoomLevel);
Chris@127 1275 }
Chris@127 1276 }
Chris@127 1277
Chris@127 1278 emit paneInteractedWith();
Chris@127 1279 }
Chris@127 1280
Chris@132 1281 void
Chris@132 1282 Pane::horizontalThumbwheelMoved(int value)
Chris@132 1283 {
Chris@137 1284 //!!! dupe with updateHeadsUpDisplay
Chris@137 1285
Chris@132 1286 int count = 0;
Chris@132 1287 int level = 1;
Chris@137 1288
Chris@137 1289
Chris@137 1290 //!!! pull out into function (presumably in View)
Chris@137 1291 bool haveConstraint = false;
Chris@137 1292 for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end();
Chris@137 1293 ++i) {
Chris@137 1294 if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) {
Chris@137 1295 haveConstraint = true;
Chris@137 1296 break;
Chris@137 1297 }
Chris@132 1298 }
Chris@132 1299
Chris@137 1300 if (haveConstraint) {
Chris@137 1301 while (true) {
Chris@137 1302 if (m_hthumb->getMaximumValue() - value == count) break;
Chris@137 1303 int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@137 1304 ZoomConstraint::RoundUp);
Chris@137 1305 if (newLevel == level) break;
Chris@137 1306 level = newLevel;
Chris@137 1307 if (++count == 50) break;
Chris@137 1308 }
Chris@137 1309 } else {
Chris@137 1310 while (true) {
Chris@137 1311 if (m_hthumb->getMaximumValue() - value == count) break;
Chris@137 1312 int step = level / 10;
Chris@137 1313 int pwr = 0;
Chris@137 1314 while (step > 0) {
Chris@137 1315 ++pwr;
Chris@137 1316 step /= 2;
Chris@137 1317 }
Chris@137 1318 step = 1;
Chris@137 1319 while (pwr > 0) {
Chris@137 1320 step *= 2;
Chris@137 1321 --pwr;
Chris@137 1322 }
Chris@137 1323 // std::cerr << level << std::endl;
Chris@137 1324 level += step;
Chris@137 1325 if (++count == 100 || level > 262144) break;
Chris@137 1326 }
Chris@137 1327 }
Chris@137 1328
Chris@137 1329 std::cerr << "new level is " << level << std::endl;
Chris@132 1330 setZoomLevel(level);
Chris@132 1331 }
Chris@132 1332
Chris@132 1333 void
Chris@132 1334 Pane::verticalThumbwheelMoved(int value)
Chris@132 1335 {
Chris@133 1336 Layer *layer = 0;
Chris@133 1337 if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@133 1338 if (layer) {
Chris@133 1339 int defaultStep = 0;
Chris@133 1340 int max = layer->getVerticalZoomSteps(defaultStep);
Chris@133 1341 if (max == 0) {
Chris@133 1342 updateHeadsUpDisplay();
Chris@133 1343 return;
Chris@133 1344 }
Chris@133 1345 if (value > max) {
Chris@133 1346 value = max;
Chris@133 1347 }
Chris@133 1348 layer->setVerticalZoomStep(value);
Chris@133 1349 }
Chris@132 1350 }
Chris@132 1351
Chris@127 1352 bool
Chris@127 1353 Pane::editSelectionStart(QMouseEvent *e)
Chris@127 1354 {
Chris@127 1355 if (!m_identifyFeatures ||
Chris@127 1356 !m_manager ||
Chris@127 1357 m_manager->getToolMode() != ViewManager::EditMode) {
Chris@127 1358 return false;
Chris@127 1359 }
Chris@127 1360
Chris@127 1361 bool closeToLeft, closeToRight;
Chris@127 1362 Selection s(getSelectionAt(e->x(), closeToLeft, closeToRight));
Chris@127 1363 if (s.isEmpty()) return false;
Chris@127 1364 m_editingSelection = s;
Chris@127 1365 m_editingSelectionEdge = (closeToLeft ? -1 : closeToRight ? 1 : 0);
Chris@127 1366 m_mousePos = e->pos();
Chris@127 1367 return true;
Chris@127 1368 }
Chris@127 1369
Chris@127 1370 bool
Chris@127 1371 Pane::editSelectionDrag(QMouseEvent *e)
Chris@127 1372 {
Chris@127 1373 if (m_editingSelection.isEmpty()) return false;
Chris@127 1374 m_mousePos = e->pos();
Chris@127 1375 update();
Chris@127 1376 return true;
Chris@127 1377 }
Chris@127 1378
Chris@127 1379 bool
Chris@127 1380 Pane::editSelectionEnd(QMouseEvent *e)
Chris@127 1381 {
Chris@127 1382 if (m_editingSelection.isEmpty()) return false;
Chris@127 1383
Chris@127 1384 int offset = m_mousePos.x() - m_clickPos.x();
Chris@127 1385 Layer *layer = getSelectedLayer();
Chris@127 1386
Chris@127 1387 if (offset == 0 || !layer) {
Chris@127 1388 m_editingSelection = Selection();
Chris@127 1389 return true;
Chris@127 1390 }
Chris@127 1391
Chris@127 1392 int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@127 1393 int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@127 1394
Chris@127 1395 long f0 = getFrameForX(p0);
Chris@127 1396 long f1 = getFrameForX(p1);
Chris@127 1397
Chris@127 1398 Selection newSelection(f0, f1);
Chris@127 1399
Chris@127 1400 if (m_editingSelectionEdge == 0) {
Chris@127 1401
Chris@127 1402 CommandHistory::getInstance()->startCompoundOperation
Chris@127 1403 (tr("Drag Selection"), true);
Chris@127 1404
Chris@127 1405 layer->moveSelection(m_editingSelection, f0);
Chris@127 1406
Chris@127 1407 } else {
Chris@127 1408
Chris@127 1409 CommandHistory::getInstance()->startCompoundOperation
Chris@127 1410 (tr("Resize Selection"), true);
Chris@127 1411
Chris@127 1412 if (m_editingSelectionEdge < 0) {
Chris@127 1413 f1 = m_editingSelection.getEndFrame();
Chris@127 1414 } else {
Chris@127 1415 f0 = m_editingSelection.getStartFrame();
Chris@127 1416 }
Chris@127 1417
Chris@127 1418 newSelection = Selection(f0, f1);
Chris@127 1419 layer->resizeSelection(m_editingSelection, newSelection);
Chris@127 1420 }
Chris@127 1421
Chris@127 1422 m_manager->removeSelection(m_editingSelection);
Chris@127 1423 m_manager->addSelection(newSelection);
Chris@127 1424
Chris@127 1425 CommandHistory::getInstance()->endCompoundOperation();
Chris@127 1426
Chris@127 1427 m_editingSelection = Selection();
Chris@127 1428 return true;
Chris@127 1429 }
Chris@127 1430
Chris@127 1431 void
Chris@127 1432 Pane::toolModeChanged()
Chris@127 1433 {
Chris@127 1434 ViewManager::ToolMode mode = m_manager->getToolMode();
Chris@127 1435 // std::cerr << "Pane::toolModeChanged(" << mode << ")" << std::endl;
Chris@127 1436
Chris@127 1437 switch (mode) {
Chris@127 1438
Chris@127 1439 case ViewManager::NavigateMode:
Chris@127 1440 setCursor(Qt::PointingHandCursor);
Chris@127 1441 break;
Chris@127 1442
Chris@127 1443 case ViewManager::SelectMode:
Chris@127 1444 setCursor(Qt::ArrowCursor);
Chris@127 1445 break;
Chris@127 1446
Chris@127 1447 case ViewManager::EditMode:
Chris@127 1448 setCursor(Qt::UpArrowCursor);
Chris@127 1449 break;
Chris@127 1450
Chris@127 1451 case ViewManager::DrawMode:
Chris@127 1452 setCursor(Qt::CrossCursor);
Chris@127 1453 break;
Chris@127 1454 /*
Chris@127 1455 case ViewManager::TextMode:
Chris@127 1456 setCursor(Qt::IBeamCursor);
Chris@127 1457 break;
Chris@127 1458 */
Chris@127 1459 }
Chris@127 1460 }
Chris@127 1461
Chris@133 1462 void
Chris@133 1463 Pane::zoomWheelsEnabledChanged()
Chris@133 1464 {
Chris@133 1465 updateHeadsUpDisplay();
Chris@133 1466 update();
Chris@133 1467 }
Chris@133 1468
Chris@133 1469 void
Chris@133 1470 Pane::zoomLevelChanged()
Chris@133 1471 {
Chris@133 1472 if (m_manager && m_manager->getZoomWheelsEnabled()) {
Chris@133 1473 updateHeadsUpDisplay();
Chris@133 1474 }
Chris@133 1475 }
Chris@133 1476
Chris@133 1477 void
Chris@133 1478 Pane::propertyContainerSelected(View *v, PropertyContainer *pc)
Chris@133 1479 {
Chris@133 1480 Layer *layer = 0;
Chris@133 1481
Chris@133 1482 if (getLayerCount() > 0) {
Chris@133 1483 layer = getLayer(getLayerCount() - 1);
Chris@133 1484 disconnect(layer, SIGNAL(verticalZoomChanged()),
Chris@133 1485 this, SLOT(verticalZoomChanged()));
Chris@133 1486 }
Chris@133 1487
Chris@133 1488 View::propertyContainerSelected(v, pc);
Chris@133 1489 updateHeadsUpDisplay();
Chris@133 1490
Chris@133 1491 if (getLayerCount() > 0) {
Chris@133 1492 layer = getLayer(getLayerCount() - 1);
Chris@133 1493 connect(layer, SIGNAL(verticalZoomChanged()),
Chris@133 1494 this, SLOT(verticalZoomChanged()));
Chris@133 1495 }
Chris@133 1496 }
Chris@133 1497
Chris@133 1498 void
Chris@133 1499 Pane::verticalZoomChanged()
Chris@133 1500 {
Chris@133 1501 Layer *layer = 0;
Chris@133 1502
Chris@133 1503 if (getLayerCount() > 0) {
Chris@133 1504
Chris@133 1505 layer = getLayer(getLayerCount() - 1);
Chris@133 1506
Chris@133 1507 if (m_vthumb && m_vthumb->isVisible()) {
Chris@133 1508 m_vthumb->setValue(layer->getCurrentVerticalZoomStep());
Chris@133 1509 }
Chris@133 1510 }
Chris@133 1511 }
Chris@133 1512
Chris@127 1513 QString
Chris@127 1514 Pane::toXmlString(QString indent, QString extraAttributes) const
Chris@127 1515 {
Chris@127 1516 return View::toXmlString
Chris@127 1517 (indent,
Chris@127 1518 QString("type=\"pane\" centreLineVisible=\"%1\" height=\"%2\" %3")
Chris@127 1519 .arg(m_centreLineVisible).arg(height()).arg(extraAttributes));
Chris@127 1520 }
Chris@127 1521
Chris@127 1522
Chris@127 1523 #ifdef INCLUDE_MOCFILES
Chris@127 1524 #include "Pane.moc.cpp"
Chris@127 1525 #endif
Chris@127 1526