Chris@127: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@127: Chris@127: /* Chris@127: Sonic Visualiser Chris@127: An audio file viewer and annotation editor. Chris@127: Centre for Digital Music, Queen Mary, University of London. Chris@182: This file copyright 2006 Chris Cannam and QMUL. Chris@127: Chris@127: This program is free software; you can redistribute it and/or Chris@127: modify it under the terms of the GNU General Public License as Chris@127: published by the Free Software Foundation; either version 2 of the Chris@127: License, or (at your option) any later version. See the file Chris@127: COPYING included with this distribution for more information. Chris@127: */ Chris@127: Chris@128: #include "Pane.h" Chris@128: #include "layer/Layer.h" Chris@128: #include "data/model/Model.h" Chris@127: #include "base/ZoomConstraint.h" Chris@127: #include "base/RealTime.h" Chris@127: #include "base/Profiler.h" Chris@128: #include "ViewManager.h" Chris@127: #include "base/CommandHistory.h" Chris@127: #include "layer/WaveformLayer.h" Chris@127: Chris@127: #include Chris@127: #include Chris@127: #include Chris@127: #include Chris@127: Chris@133: //!!! for HUD -- pull out into a separate class Chris@133: #include Chris@133: #include Chris@133: #include Chris@133: #include "widgets/Thumbwheel.h" Chris@172: #include "widgets/Panner.h" Chris@188: #include "widgets/RangeInputDialog.h" Chris@189: #include "widgets/NotifyingPushButton.h" Chris@133: Chris@127: using std::cerr; Chris@127: using std::endl; Chris@127: Chris@127: Pane::Pane(QWidget *w) : Chris@127: View(w, true), Chris@127: m_identifyFeatures(false), Chris@127: m_clickedInRange(false), Chris@127: m_shiftPressed(false), Chris@127: m_ctrlPressed(false), Chris@127: m_navigating(false), Chris@127: m_resizing(false), Chris@133: m_centreLineVisible(true), Chris@222: m_scaleWidth(0), Chris@133: m_headsUpDisplay(0) Chris@127: { Chris@127: setObjectName("Pane"); Chris@127: setMouseTracking(true); Chris@133: Chris@133: updateHeadsUpDisplay(); Chris@133: } Chris@133: Chris@133: void Chris@133: Pane::updateHeadsUpDisplay() Chris@133: { Chris@187: Profiler profiler("Pane::updateHeadsUpDisplay", true); Chris@187: Chris@192: if (!isVisible()) return; Chris@192: Chris@132: /* Chris@132: int count = 0; Chris@132: int currentLevel = 1; Chris@132: int level = 1; Chris@132: while (true) { Chris@132: if (getZoomLevel() == level) currentLevel = count; Chris@132: int newLevel = getZoomConstraintBlockSize(level + 1, Chris@132: ZoomConstraint::RoundUp); Chris@132: if (newLevel == level) break; Chris@132: if (newLevel == 131072) break; //!!! just because Chris@132: level = newLevel; Chris@132: ++count; Chris@132: } Chris@132: Chris@132: std::cerr << "Have " << count+1 << " zoom levels" << std::endl; Chris@132: */ Chris@133: Chris@188: Layer *layer = 0; Chris@188: if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1); Chris@188: Chris@133: if (!m_headsUpDisplay) { Chris@133: Chris@133: m_headsUpDisplay = new QFrame(this); Chris@133: Chris@133: QGridLayout *layout = new QGridLayout; Chris@133: layout->setMargin(0); Chris@133: layout->setSpacing(0); Chris@133: m_headsUpDisplay->setLayout(layout); Chris@133: Chris@133: m_hthumb = new Thumbwheel(Qt::Horizontal); Chris@187: m_hthumb->setObjectName(tr("Horizontal Zoom")); Chris@173: layout->addWidget(m_hthumb, 1, 0, 1, 2); Chris@133: m_hthumb->setFixedWidth(70); Chris@133: m_hthumb->setFixedHeight(16); Chris@133: m_hthumb->setDefaultValue(0); Chris@165: m_hthumb->setSpeed(0.6); Chris@133: connect(m_hthumb, SIGNAL(valueChanged(int)), this, Chris@133: SLOT(horizontalThumbwheelMoved(int))); Chris@189: connect(m_hthumb, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget())); Chris@189: connect(m_hthumb, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget())); Chris@172: Chris@172: m_vpan = new Panner; Chris@172: layout->addWidget(m_vpan, 0, 1); Chris@173: m_vpan->setFixedWidth(12); Chris@172: m_vpan->setFixedHeight(70); Chris@174: m_vpan->setAlpha(80, 130); Chris@174: connect(m_vpan, SIGNAL(rectExtentsChanged(float, float, float, float)), Chris@174: this, SLOT(verticalPannerMoved(float, float, float, float))); Chris@188: connect(m_vpan, SIGNAL(doubleClicked()), Chris@188: this, SLOT(editVerticalPannerExtents())); Chris@189: connect(m_vpan, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget())); Chris@189: connect(m_vpan, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget())); Chris@172: Chris@133: m_vthumb = new Thumbwheel(Qt::Vertical); Chris@187: m_vthumb->setObjectName(tr("Vertical Zoom")); Chris@172: layout->addWidget(m_vthumb, 0, 2); Chris@133: m_vthumb->setFixedWidth(16); Chris@133: m_vthumb->setFixedHeight(70); Chris@133: connect(m_vthumb, SIGNAL(valueChanged(int)), this, Chris@133: SLOT(verticalThumbwheelMoved(int))); Chris@189: connect(m_vthumb, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget())); Chris@189: connect(m_vthumb, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget())); Chris@133: Chris@188: if (layer) { Chris@188: RangeMapper *rm = layer->getNewVerticalZoomRangeMapper(); Chris@188: if (rm) m_vthumb->setRangeMapper(rm); Chris@188: } Chris@188: Chris@189: m_reset = new NotifyingPushButton; Chris@189: m_reset->setFixedHeight(16); Chris@189: m_reset->setFixedWidth(16); Chris@189: layout->addWidget(m_reset, 1, 2); Chris@189: connect(m_reset, SIGNAL(clicked()), m_hthumb, SLOT(resetToDefault())); Chris@189: connect(m_reset, SIGNAL(clicked()), m_vthumb, SLOT(resetToDefault())); Chris@189: connect(m_reset, SIGNAL(clicked()), m_vpan, SLOT(resetToDefault())); Chris@189: connect(m_reset, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget())); Chris@189: connect(m_reset, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget())); Chris@133: } Chris@133: Chris@133: int count = 0; Chris@133: int current = 0; Chris@133: int level = 1; Chris@133: Chris@137: //!!! pull out into function (presumably in View) Chris@137: bool haveConstraint = false; Chris@137: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); Chris@137: ++i) { Chris@137: if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) { Chris@137: haveConstraint = true; Chris@137: break; Chris@137: } Chris@137: } Chris@137: Chris@137: if (haveConstraint) { Chris@137: while (true) { Chris@137: if (getZoomLevel() == level) current = count; Chris@137: int newLevel = getZoomConstraintBlockSize(level + 1, Chris@137: ZoomConstraint::RoundUp); Chris@137: if (newLevel == level) break; Chris@137: level = newLevel; Chris@137: if (++count == 50) break; Chris@137: } Chris@137: } else { Chris@137: // if we have no particular constraints, we can really spread out Chris@137: while (true) { Chris@137: if (getZoomLevel() >= level) current = count; Chris@137: int step = level / 10; Chris@137: int pwr = 0; Chris@137: while (step > 0) { Chris@137: ++pwr; Chris@137: step /= 2; Chris@137: } Chris@137: step = 1; Chris@137: while (pwr > 0) { Chris@137: step *= 2; Chris@137: --pwr; Chris@137: } Chris@154: // std::cerr << level << std::endl; Chris@137: level += step; Chris@137: if (++count == 100 || level > 262144) break; Chris@137: } Chris@133: } Chris@133: Chris@133: // std::cerr << "Have " << count << " zoom levels" << std::endl; Chris@133: Chris@133: m_hthumb->setMinimumValue(0); Chris@133: m_hthumb->setMaximumValue(count); Chris@133: m_hthumb->setValue(count - current); Chris@133: Chris@133: // std::cerr << "set value to " << count-current << std::endl; Chris@133: Chris@133: // std::cerr << "default value is " << m_hthumb->getDefaultValue() << std::endl; Chris@133: Chris@133: if (count != 50 && m_hthumb->getDefaultValue() == 0) { Chris@133: m_hthumb->setDefaultValue(count - current); Chris@133: // std::cerr << "set default value to " << m_hthumb->getDefaultValue() << std::endl; Chris@133: } Chris@133: Chris@204: bool haveVThumb = false; Chris@204: Chris@133: if (layer) { Chris@133: int defaultStep = 0; Chris@133: int max = layer->getVerticalZoomSteps(defaultStep); Chris@133: if (max == 0) { Chris@133: m_vthumb->hide(); Chris@133: } else { Chris@204: haveVThumb = true; Chris@133: m_vthumb->show(); Chris@187: m_vthumb->blockSignals(true); Chris@133: m_vthumb->setMinimumValue(0); Chris@133: m_vthumb->setMaximumValue(max); Chris@133: m_vthumb->setDefaultValue(defaultStep); Chris@133: m_vthumb->setValue(layer->getCurrentVerticalZoomStep()); Chris@187: m_vthumb->blockSignals(false); Chris@135: Chris@205: // std::cerr << "Vertical thumbwheel: min 0, max " << max Chris@205: // << ", default " << defaultStep << ", value " Chris@205: // << m_vthumb->getValue() << std::endl; Chris@135: Chris@133: } Chris@133: } Chris@133: Chris@174: updateVerticalPanner(); Chris@174: Chris@133: if (m_manager && m_manager->getZoomWheelsEnabled() && Chris@133: width() > 120 && height() > 100) { Chris@165: if (!m_headsUpDisplay->isVisible()) { Chris@165: m_headsUpDisplay->show(); Chris@165: } Chris@204: if (haveVThumb) { Chris@204: m_headsUpDisplay->setFixedHeight(m_vthumb->height() + m_hthumb->height()); Chris@133: m_headsUpDisplay->move(width() - 86, height() - 86); Chris@133: } else { Chris@204: m_headsUpDisplay->setFixedHeight(m_hthumb->height()); Chris@204: m_headsUpDisplay->move(width() - 86, height() - 16); Chris@133: } Chris@133: } else { Chris@133: m_headsUpDisplay->hide(); Chris@133: } Chris@127: } Chris@127: Chris@174: void Chris@174: Pane::updateVerticalPanner() Chris@174: { Chris@174: if (!m_vpan || !m_manager || !m_manager->getZoomWheelsEnabled()) return; Chris@174: Chris@208: // In principle we should show or hide the panner on the basis of Chris@208: // whether the top layer has adjustable display extents, and we do Chris@208: // that below. However, we have no basis for layout of the panner Chris@208: // if the vertical scroll wheel is not also present. So if we Chris@208: // have no vertical scroll wheel, we should remove the panner as Chris@208: // well. Ideally any layer that implements display extents should Chris@208: // implement vertical zoom steps as well, but they don't all at Chris@208: // the moment. Chris@208: Chris@208: Layer *layer = 0; Chris@208: if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1); Chris@208: int discard; Chris@208: if (layer && layer->getVerticalZoomSteps(discard) == 0) { Chris@208: m_vpan->hide(); Chris@208: return; Chris@208: } Chris@208: Chris@174: float vmin, vmax, dmin, dmax; Chris@174: if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax) && vmax != vmin) { Chris@174: float y0 = (dmin - vmin) / (vmax - vmin); Chris@174: float y1 = (dmax - vmin) / (vmax - vmin); Chris@174: m_vpan->blockSignals(true); Chris@174: m_vpan->setRectExtents(0, 1.0 - y1, 1, y1 - y0); Chris@174: m_vpan->blockSignals(false); Chris@174: m_vpan->show(); Chris@174: } else { Chris@174: m_vpan->hide(); Chris@174: } Chris@174: } Chris@174: Chris@127: bool Chris@127: Pane::shouldIlluminateLocalFeatures(const Layer *layer, QPoint &pos) const Chris@127: { Chris@127: QPoint discard; Chris@127: bool b0, b1; Chris@127: Chris@127: if (layer == getSelectedLayer() && Chris@127: !shouldIlluminateLocalSelection(discard, b0, b1)) { Chris@127: Chris@127: pos = m_identifyPoint; Chris@127: return m_identifyFeatures; Chris@127: } Chris@127: Chris@127: return false; Chris@127: } Chris@127: Chris@127: bool Chris@127: Pane::shouldIlluminateLocalSelection(QPoint &pos, Chris@127: bool &closeToLeft, Chris@127: bool &closeToRight) const Chris@127: { Chris@127: if (m_identifyFeatures && Chris@127: m_manager && Chris@127: m_manager->getToolMode() == ViewManager::EditMode && Chris@127: !m_manager->getSelections().empty() && Chris@127: !selectionIsBeingEdited()) { Chris@127: Chris@127: Selection s(getSelectionAt(m_identifyPoint.x(), Chris@127: closeToLeft, closeToRight)); Chris@127: Chris@127: if (!s.isEmpty()) { Chris@127: if (getSelectedLayer() && getSelectedLayer()->isLayerEditable()) { Chris@127: Chris@127: pos = m_identifyPoint; Chris@127: return true; Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: return false; Chris@127: } Chris@127: Chris@127: bool Chris@127: Pane::selectionIsBeingEdited() const Chris@127: { Chris@127: if (!m_editingSelection.isEmpty()) { Chris@127: if (m_mousePos != m_clickPos && Chris@127: getFrameForX(m_mousePos.x()) != getFrameForX(m_clickPos.x())) { Chris@127: return true; Chris@127: } Chris@127: } Chris@127: return false; Chris@127: } Chris@127: Chris@127: void Chris@127: Pane::setCentreLineVisible(bool visible) Chris@127: { Chris@127: m_centreLineVisible = visible; Chris@127: update(); Chris@127: } Chris@127: Chris@127: void Chris@127: Pane::paintEvent(QPaintEvent *e) Chris@127: { Chris@127: // Profiler profiler("Pane::paintEvent", true); Chris@127: Chris@127: QPainter paint; Chris@127: Chris@127: QRect r(rect()); Chris@127: Chris@127: if (e) { Chris@127: r = e->rect(); Chris@127: } Chris@127: /* Chris@127: paint.begin(this); Chris@127: paint.setClipRect(r); Chris@127: Chris@127: if (hasLightBackground()) { Chris@127: paint.setPen(Qt::white); Chris@127: paint.setBrush(Qt::white); Chris@127: } else { Chris@127: paint.setPen(Qt::black); Chris@127: paint.setBrush(Qt::black); Chris@127: } Chris@127: paint.drawRect(r); Chris@127: Chris@127: paint.end(); Chris@127: */ Chris@127: View::paintEvent(e); Chris@127: Chris@127: paint.begin(this); Chris@127: Chris@127: if (e) { Chris@127: paint.setClipRect(r); Chris@127: } Chris@127: Chris@127: const Model *waveformModel = 0; // just for reporting purposes Chris@127: Chris@127: int fontHeight = paint.fontMetrics().height(); Chris@127: int fontAscent = paint.fontMetrics().ascent(); Chris@127: Chris@127: if (m_manager && Chris@127: !m_manager->isPlaying() && Chris@127: m_manager->getToolMode() == ViewManager::SelectMode) { Chris@127: Chris@127: for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) { Chris@127: --vi; Chris@127: Chris@127: std::vector crosshairExtents; Chris@127: Chris@127: if ((*vi)->getCrosshairExtents(this, paint, m_identifyPoint, Chris@127: crosshairExtents)) { Chris@127: (*vi)->paintCrosshairs(this, paint, m_identifyPoint); Chris@127: break; Chris@127: } else if ((*vi)->isLayerOpaque()) { Chris@127: break; Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) { Chris@127: --vi; Chris@127: Chris@127: if (dynamic_cast(*vi)) { Chris@127: waveformModel = (*vi)->getModel(); Chris@127: } Chris@127: Chris@189: if (!m_manager || !m_manager->shouldShowVerticalScale()) { Chris@127: break; Chris@127: } Chris@127: Chris@222: m_scaleWidth = (*vi)->getVerticalScaleWidth(this, paint); Chris@127: Chris@222: if (m_scaleWidth > 0 && r.left() < m_scaleWidth) { Chris@127: Chris@127: // Profiler profiler("Pane::paintEvent - painting vertical scale", true); Chris@127: Chris@127: // std::cerr << "Pane::paintEvent: calling paint.save() in vertical scale block" << std::endl; Chris@127: paint.save(); Chris@127: Chris@127: paint.setPen(Qt::black); Chris@127: paint.setBrush(Qt::white); Chris@222: paint.drawRect(0, -1, m_scaleWidth, height()+1); Chris@127: Chris@127: paint.setBrush(Qt::NoBrush); Chris@127: (*vi)->paintVerticalScale Chris@222: (this, paint, QRect(0, 0, m_scaleWidth, height())); Chris@127: Chris@127: paint.restore(); Chris@127: } Chris@127: Chris@127: if (m_identifyFeatures) { Chris@127: Chris@127: QPoint pos = m_identifyPoint; Chris@127: QString desc = (*vi)->getFeatureDescription(this, pos); Chris@127: Chris@127: if (desc != "") { Chris@127: Chris@127: paint.save(); Chris@127: Chris@127: int tabStop = Chris@127: paint.fontMetrics().width(tr("Some lengthy prefix:")); Chris@127: Chris@127: QRect boundingRect = Chris@127: paint.fontMetrics().boundingRect Chris@127: (rect(), Chris@127: Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs, Chris@127: desc, tabStop); Chris@127: Chris@127: if (hasLightBackground()) { Chris@127: paint.setPen(Qt::NoPen); Chris@127: paint.setBrush(QColor(250, 250, 250, 200)); Chris@127: } else { Chris@127: paint.setPen(Qt::NoPen); Chris@127: paint.setBrush(QColor(50, 50, 50, 200)); Chris@127: } Chris@127: Chris@127: int extra = paint.fontMetrics().descent(); Chris@127: paint.drawRect(width() - boundingRect.width() - 10 - extra, Chris@127: 10 - extra, Chris@127: boundingRect.width() + 2 * extra, Chris@127: boundingRect.height() + extra); Chris@127: Chris@127: if (hasLightBackground()) { Chris@127: paint.setPen(QColor(150, 20, 0)); Chris@127: } else { Chris@127: paint.setPen(QColor(255, 150, 100)); Chris@127: } Chris@127: Chris@127: QTextOption option; Chris@127: option.setWrapMode(QTextOption::NoWrap); Chris@127: option.setAlignment(Qt::AlignRight | Qt::AlignTop); Chris@127: option.setTabStop(tabStop); Chris@127: paint.drawText(QRectF(width() - boundingRect.width() - 10, 10, Chris@127: boundingRect.width(), Chris@127: boundingRect.height()), Chris@127: desc, Chris@127: option); Chris@127: Chris@127: paint.restore(); Chris@127: } Chris@127: } Chris@127: Chris@127: break; Chris@127: } Chris@127: Chris@127: int sampleRate = getModelsSampleRate(); Chris@127: paint.setBrush(Qt::NoBrush); Chris@127: Chris@189: if (m_centreLineVisible && Chris@189: m_manager && Chris@189: m_manager->shouldShowCentreLine()) { Chris@127: Chris@189: QColor c = QColor(0, 0, 0); Chris@189: if (!hasLightBackground()) { Chris@189: c = QColor(240, 240, 240); Chris@189: } Chris@189: paint.setPen(c); Chris@189: int x = width() / 2 + 1; Chris@189: paint.drawLine(x, 0, x, height() - 1); Chris@189: paint.drawLine(x-1, 1, x+1, 1); Chris@189: paint.drawLine(x-2, 0, x+2, 0); Chris@189: paint.drawLine(x-1, height() - 2, x+1, height() - 2); Chris@189: paint.drawLine(x-2, height() - 1, x+2, height() - 1); Chris@127: Chris@127: paint.setPen(QColor(50, 50, 50)); Chris@127: Chris@127: int y = height() - fontHeight Chris@127: + fontAscent - 6; Chris@127: Chris@127: LayerList::iterator vi = m_layers.end(); Chris@127: Chris@127: if (vi != m_layers.begin()) { Chris@127: Chris@127: switch ((*--vi)->getPreferredFrameCountPosition()) { Chris@127: Chris@127: case Layer::PositionTop: Chris@127: y = fontAscent + 6; Chris@127: break; Chris@127: Chris@127: case Layer::PositionMiddle: Chris@127: y = (height() - fontHeight) / 2 Chris@127: + fontAscent; Chris@127: break; Chris@127: Chris@127: case Layer::PositionBottom: Chris@127: // y already set correctly Chris@127: break; Chris@127: } Chris@127: } Chris@127: Chris@189: if (m_manager && m_manager->shouldShowFrameCount()) { Chris@127: Chris@127: if (sampleRate) { Chris@127: Chris@127: QString text(QString::fromStdString Chris@127: (RealTime::frame2RealTime Chris@127: (m_centreFrame, sampleRate).toText(true))); Chris@127: Chris@127: int tw = paint.fontMetrics().width(text); Chris@127: int x = width()/2 - 4 - tw; Chris@127: Chris@127: drawVisibleText(paint, x, y, text, OutlinedText); Chris@127: } Chris@127: Chris@127: QString text = QString("%1").arg(m_centreFrame); Chris@127: Chris@127: int tw = paint.fontMetrics().width(text); Chris@127: int x = width()/2 + 4; Chris@127: Chris@127: drawVisibleText(paint, x, y, text, OutlinedText); Chris@127: } Chris@127: Chris@127: } else { Chris@127: Chris@127: paint.setPen(QColor(50, 50, 50)); Chris@127: } Chris@127: Chris@127: if (waveformModel && Chris@127: m_manager && Chris@189: m_manager->shouldShowDuration() && Chris@127: r.y() + r.height() >= height() - fontHeight - 6) { Chris@127: Chris@150: size_t modelRate = waveformModel->getSampleRate(); Chris@127: size_t playbackRate = m_manager->getPlaybackSampleRate(); Chris@224: size_t outputRate = m_manager->getOutputSampleRate(); Chris@224: Chris@127: QString srNote = ""; Chris@127: Chris@127: // Show (R) for waveform models that will be resampled on Chris@127: // playback, and (X) for waveform models that will be played Chris@224: // at the wrong rate because their rate differs from the Chris@224: // current playback rate (which is not necessarily that of the Chris@224: // main model). Chris@127: Chris@224: if (playbackRate != 0) { Chris@224: if (modelRate == playbackRate) { Chris@224: if (modelRate != outputRate) srNote = " " + tr("(R)"); Chris@224: } else { Chris@224: srNote = " " + tr("(X)"); Chris@224: } Chris@224: } Chris@127: Chris@127: QString desc = tr("%1 / %2Hz%3") Chris@127: .arg(RealTime::frame2RealTime(waveformModel->getEndFrame(), Chris@127: sampleRate) Chris@127: .toText(false).c_str()) Chris@150: .arg(modelRate) Chris@127: .arg(srNote); Chris@127: Chris@223: if (r.x() < m_scaleWidth + 5 + paint.fontMetrics().width(desc)) { Chris@223: drawVisibleText(paint, m_scaleWidth + 5, Chris@127: height() - fontHeight + fontAscent - 6, Chris@127: desc, OutlinedText); Chris@127: } Chris@127: } Chris@127: Chris@127: if (m_manager && Chris@189: m_manager->shouldShowLayerNames() && Chris@127: r.y() + r.height() >= height() - m_layers.size() * fontHeight - 6) { Chris@127: Chris@127: std::vector texts; Chris@127: int maxTextWidth = 0; Chris@127: Chris@127: for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@127: Chris@127: QString text = (*i)->getLayerPresentationName(); Chris@127: int tw = paint.fontMetrics().width(text); Chris@127: bool reduced = false; Chris@127: while (tw > width() / 3 && text.length() > 4) { Chris@127: if (!reduced && text.length() > 8) { Chris@127: text = text.left(text.length() - 4); Chris@127: } else { Chris@127: text = text.left(text.length() - 2); Chris@127: } Chris@127: reduced = true; Chris@127: tw = paint.fontMetrics().width(text + "..."); Chris@127: } Chris@127: if (reduced) { Chris@127: texts.push_back(text + "..."); Chris@127: } else { Chris@127: texts.push_back(text); Chris@127: } Chris@127: if (tw > maxTextWidth) maxTextWidth = tw; Chris@127: } Chris@127: Chris@127: int lly = height() - 6; Chris@133: int llx = width() - maxTextWidth - 5; Chris@127: Chris@133: if (m_manager->getZoomWheelsEnabled()) { Chris@133: lly -= 20; Chris@173: llx -= 36; Chris@133: } Chris@133: Chris@133: if (r.x() + r.width() >= llx) { Chris@127: Chris@127: for (int i = 0; i < texts.size(); ++i) { Chris@127: Chris@127: if (i == texts.size() - 1) { Chris@127: paint.setPen(Qt::black); Chris@127: } Chris@127: Chris@133: drawVisibleText(paint, llx, Chris@127: lly - fontHeight + fontAscent, Chris@127: texts[i], OutlinedText); Chris@127: Chris@127: lly -= fontHeight; Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: if (m_clickedInRange && m_shiftPressed) { Chris@127: if (m_manager && (m_manager->getToolMode() == ViewManager::NavigateMode)) { Chris@127: //!!! be nice if this looked a bit more in keeping with the Chris@127: //selection block Chris@127: paint.setPen(Qt::blue); Chris@127: paint.drawRect(m_clickPos.x(), m_clickPos.y(), Chris@127: m_mousePos.x() - m_clickPos.x(), Chris@127: m_mousePos.y() - m_clickPos.y()); Chris@127: } Chris@127: } Chris@127: Chris@127: if (selectionIsBeingEdited()) { Chris@127: Chris@127: int offset = m_mousePos.x() - m_clickPos.x(); Chris@127: int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset; Chris@127: int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset; Chris@127: Chris@127: if (m_editingSelectionEdge < 0) { Chris@127: p1 = getXForFrame(m_editingSelection.getEndFrame()); Chris@127: } else if (m_editingSelectionEdge > 0) { Chris@127: p0 = getXForFrame(m_editingSelection.getStartFrame()); Chris@127: } Chris@127: Chris@127: paint.save(); Chris@127: if (hasLightBackground()) { Chris@127: paint.setPen(QPen(Qt::black, 2)); Chris@127: } else { Chris@127: paint.setPen(QPen(Qt::white, 2)); Chris@127: } Chris@127: Chris@127: //!!! duplicating display policy with View::drawSelections Chris@127: Chris@127: if (m_editingSelectionEdge < 0) { Chris@127: paint.drawLine(p0, 1, p1, 1); Chris@127: paint.drawLine(p0, 0, p0, height()); Chris@127: paint.drawLine(p0, height() - 1, p1, height() - 1); Chris@127: } else if (m_editingSelectionEdge > 0) { Chris@127: paint.drawLine(p0, 1, p1, 1); Chris@127: paint.drawLine(p1, 0, p1, height()); Chris@127: paint.drawLine(p0, height() - 1, p1, height() - 1); Chris@127: } else { Chris@127: paint.setBrush(Qt::NoBrush); Chris@127: paint.drawRect(p0, 1, p1 - p0, height() - 2); Chris@127: } Chris@127: paint.restore(); Chris@127: } Chris@127: Chris@127: paint.end(); Chris@127: } Chris@127: Chris@222: size_t Chris@222: Pane::getFirstVisibleFrame() const Chris@222: { Chris@222: long f0 = getFrameForX(m_scaleWidth); Chris@222: size_t f = View::getFirstVisibleFrame(); Chris@222: if (f0 < 0 || f0 < long(f)) return f; Chris@222: return f0; Chris@222: } Chris@222: Chris@127: Selection Chris@127: Pane::getSelectionAt(int x, bool &closeToLeftEdge, bool &closeToRightEdge) const Chris@127: { Chris@127: closeToLeftEdge = closeToRightEdge = false; Chris@127: Chris@127: if (!m_manager) return Selection(); Chris@127: Chris@127: long testFrame = getFrameForX(x - 5); Chris@127: if (testFrame < 0) { Chris@127: testFrame = getFrameForX(x); Chris@127: if (testFrame < 0) return Selection(); Chris@127: } Chris@127: Chris@127: Selection selection = m_manager->getContainingSelection(testFrame, true); Chris@127: if (selection.isEmpty()) return selection; Chris@127: Chris@127: int lx = getXForFrame(selection.getStartFrame()); Chris@127: int rx = getXForFrame(selection.getEndFrame()); Chris@127: Chris@127: int fuzz = 2; Chris@127: if (x < lx - fuzz || x > rx + fuzz) return Selection(); Chris@127: Chris@127: int width = rx - lx; Chris@127: fuzz = 3; Chris@127: if (width < 12) fuzz = width / 4; Chris@127: if (fuzz < 1) fuzz = 1; Chris@127: Chris@127: if (x < lx + fuzz) closeToLeftEdge = true; Chris@127: if (x > rx - fuzz) closeToRightEdge = true; Chris@127: Chris@127: return selection; Chris@127: } Chris@127: Chris@174: bool Chris@174: Pane::canTopLayerMoveVertical() Chris@174: { Chris@174: float vmin, vmax, dmin, dmax; Chris@174: if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) return false; Chris@174: if (dmin <= vmin && dmax >= vmax) return false; Chris@174: return true; Chris@174: } Chris@174: Chris@174: bool Chris@174: Pane::getTopLayerDisplayExtents(float &vmin, float &vmax, Chris@188: float &dmin, float &dmax, Chris@188: QString *unit) Chris@174: { Chris@174: Layer *layer = 0; Chris@174: if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1); Chris@174: if (!layer) return false; Chris@174: bool vlog; Chris@174: QString vunit; Chris@188: bool rv = (layer->getValueExtents(vmin, vmax, vlog, vunit) && Chris@188: layer->getDisplayExtents(dmin, dmax)); Chris@188: if (unit) *unit = vunit; Chris@188: return rv; Chris@174: } Chris@174: Chris@174: bool Chris@174: Pane::setTopLayerDisplayExtents(float dmin, float dmax) Chris@174: { Chris@174: Layer *layer = 0; Chris@174: if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1); Chris@174: if (!layer) return false; Chris@174: return layer->setDisplayExtents(dmin, dmax); Chris@174: } Chris@174: Chris@127: void Chris@127: Pane::mousePressEvent(QMouseEvent *e) Chris@127: { Chris@127: if (e->buttons() & Qt::RightButton) { Chris@189: emit contextHelpChanged(""); Chris@127: emit rightButtonMenuRequested(mapToGlobal(e->pos())); Chris@127: return; Chris@127: } Chris@127: Chris@127: m_clickPos = e->pos(); Chris@127: m_clickedInRange = true; Chris@127: m_editingSelection = Selection(); Chris@127: m_editingSelectionEdge = 0; Chris@127: m_shiftPressed = (e->modifiers() & Qt::ShiftModifier); Chris@127: m_ctrlPressed = (e->modifiers() & Qt::ControlModifier); Chris@150: m_dragMode = UnresolvedDrag; Chris@127: Chris@127: ViewManager::ToolMode mode = ViewManager::NavigateMode; Chris@127: if (m_manager) mode = m_manager->getToolMode(); Chris@127: Chris@127: m_navigating = false; Chris@127: Chris@127: if (mode == ViewManager::NavigateMode || (e->buttons() & Qt::MidButton)) { Chris@127: Chris@127: if (mode != ViewManager::NavigateMode) { Chris@127: setCursor(Qt::PointingHandCursor); Chris@127: } Chris@127: Chris@127: m_navigating = true; Chris@127: m_dragCentreFrame = m_centreFrame; Chris@136: m_dragStartMinValue = 0; Chris@174: Chris@174: float vmin, vmax, dmin, dmax; Chris@174: if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) { Chris@174: m_dragStartMinValue = dmin; Chris@136: } Chris@136: Chris@127: } else if (mode == ViewManager::SelectMode) { Chris@127: Chris@217: if (!hasTopLayerTimeXAxis()) return; Chris@217: Chris@127: bool closeToLeft = false, closeToRight = false; Chris@127: Selection selection = getSelectionAt(e->x(), closeToLeft, closeToRight); Chris@127: Chris@127: if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) { Chris@127: Chris@127: m_manager->removeSelection(selection); Chris@127: Chris@127: if (closeToLeft) { Chris@127: m_selectionStartFrame = selection.getEndFrame(); Chris@127: } else { Chris@127: m_selectionStartFrame = selection.getStartFrame(); Chris@127: } Chris@127: Chris@127: m_manager->setInProgressSelection(selection, false); Chris@127: m_resizing = true; Chris@127: Chris@127: } else { Chris@127: Chris@127: int mouseFrame = getFrameForX(e->x()); Chris@127: size_t resolution = 1; Chris@127: int snapFrame = mouseFrame; Chris@127: Chris@127: Layer *layer = getSelectedLayer(); Chris@127: if (layer && !m_shiftPressed) { Chris@127: layer->snapToFeatureFrame(this, snapFrame, Chris@127: resolution, Layer::SnapLeft); Chris@127: } Chris@127: Chris@127: if (snapFrame < 0) snapFrame = 0; Chris@127: m_selectionStartFrame = snapFrame; Chris@127: if (m_manager) { Chris@127: m_manager->setInProgressSelection(Selection(snapFrame, Chris@127: snapFrame + resolution), Chris@127: !m_ctrlPressed); Chris@127: } Chris@127: Chris@127: m_resizing = false; Chris@127: } Chris@127: Chris@127: update(); Chris@127: Chris@127: } else if (mode == ViewManager::DrawMode) { Chris@127: Chris@127: Layer *layer = getSelectedLayer(); Chris@127: if (layer && layer->isLayerEditable()) { Chris@127: layer->drawStart(this, e); Chris@127: } Chris@127: Chris@127: } else if (mode == ViewManager::EditMode) { Chris@127: Chris@127: if (!editSelectionStart(e)) { Chris@127: Layer *layer = getSelectedLayer(); Chris@127: if (layer && layer->isLayerEditable()) { Chris@127: layer->editStart(this, e); Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: emit paneInteractedWith(); Chris@127: } Chris@127: Chris@127: void Chris@127: Pane::mouseReleaseEvent(QMouseEvent *e) Chris@127: { Chris@127: if (e->buttons() & Qt::RightButton) { Chris@127: return; Chris@127: } Chris@127: Chris@127: ViewManager::ToolMode mode = ViewManager::NavigateMode; Chris@127: if (m_manager) mode = m_manager->getToolMode(); Chris@127: Chris@127: if (m_clickedInRange) { Chris@127: mouseMoveEvent(e); Chris@127: } Chris@127: Chris@127: if (m_navigating || mode == ViewManager::NavigateMode) { Chris@127: Chris@127: m_navigating = false; Chris@127: Chris@127: if (mode != ViewManager::NavigateMode) { Chris@127: // restore cursor Chris@127: toolModeChanged(); Chris@127: } Chris@127: Chris@127: if (m_shiftPressed) { Chris@127: Chris@127: int x0 = std::min(m_clickPos.x(), m_mousePos.x()); Chris@127: int x1 = std::max(m_clickPos.x(), m_mousePos.x()); Chris@127: Chris@127: int y0 = std::min(m_clickPos.y(), m_mousePos.y()); Chris@127: int y1 = std::max(m_clickPos.y(), m_mousePos.y()); Chris@127: Chris@174: zoomToRegion(x0, y0, x1, y1); Chris@127: } Chris@127: Chris@127: } else if (mode == ViewManager::SelectMode) { Chris@127: Chris@217: if (!hasTopLayerTimeXAxis()) return; Chris@217: Chris@127: if (m_manager && m_manager->haveInProgressSelection()) { Chris@127: Chris@127: bool exclusive; Chris@127: Selection selection = m_manager->getInProgressSelection(exclusive); Chris@127: Chris@127: if (selection.getEndFrame() < selection.getStartFrame() + 2) { Chris@127: selection = Selection(); Chris@127: } Chris@127: Chris@127: m_manager->clearInProgressSelection(); Chris@127: Chris@127: if (exclusive) { Chris@127: m_manager->setSelection(selection); Chris@127: } else { Chris@127: m_manager->addSelection(selection); Chris@127: } Chris@127: } Chris@127: Chris@127: update(); Chris@127: Chris@127: } else if (mode == ViewManager::DrawMode) { Chris@127: Chris@127: Layer *layer = getSelectedLayer(); Chris@127: if (layer && layer->isLayerEditable()) { Chris@127: layer->drawEnd(this, e); Chris@127: update(); Chris@127: } Chris@127: Chris@127: } else if (mode == ViewManager::EditMode) { Chris@127: Chris@127: if (!editSelectionEnd(e)) { Chris@127: Layer *layer = getSelectedLayer(); Chris@127: if (layer && layer->isLayerEditable()) { Chris@127: layer->editEnd(this, e); Chris@127: update(); Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: m_clickedInRange = false; Chris@127: Chris@127: emit paneInteractedWith(); Chris@127: } Chris@127: Chris@127: void Chris@127: Pane::mouseMoveEvent(QMouseEvent *e) Chris@127: { Chris@127: if (e->buttons() & Qt::RightButton) { Chris@127: return; Chris@127: } Chris@127: Chris@189: updateContextHelp(&e->pos()); Chris@189: Chris@127: ViewManager::ToolMode mode = ViewManager::NavigateMode; Chris@127: if (m_manager) mode = m_manager->getToolMode(); Chris@127: Chris@127: QPoint prevPoint = m_identifyPoint; Chris@127: m_identifyPoint = e->pos(); Chris@127: Chris@127: if (!m_clickedInRange) { Chris@127: Chris@217: if (mode == ViewManager::SelectMode && hasTopLayerTimeXAxis()) { Chris@127: bool closeToLeft = false, closeToRight = false; Chris@127: getSelectionAt(e->x(), closeToLeft, closeToRight); Chris@127: if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) { Chris@127: setCursor(Qt::SizeHorCursor); Chris@127: } else { Chris@127: setCursor(Qt::ArrowCursor); Chris@127: } Chris@127: } Chris@127: Chris@127: if (!m_manager->isPlaying()) { Chris@127: Chris@174: if (getSelectedLayer()) { Chris@127: Chris@174: bool previouslyIdentifying = m_identifyFeatures; Chris@174: m_identifyFeatures = true; Chris@174: Chris@174: if (m_identifyFeatures != previouslyIdentifying || Chris@174: m_identifyPoint != prevPoint) { Chris@174: update(); Chris@174: } Chris@174: } Chris@127: } Chris@127: Chris@127: return; Chris@127: } Chris@127: Chris@127: if (m_navigating || mode == ViewManager::NavigateMode) { Chris@127: Chris@127: if (m_shiftPressed) { Chris@127: Chris@127: m_mousePos = e->pos(); Chris@127: update(); Chris@127: Chris@127: } else { Chris@127: Chris@174: dragTopLayer(e); Chris@150: } Chris@127: Chris@127: } else if (mode == ViewManager::SelectMode) { Chris@127: Chris@217: if (!hasTopLayerTimeXAxis()) return; Chris@217: Chris@174: dragExtendSelection(e); Chris@127: Chris@127: } else if (mode == ViewManager::DrawMode) { Chris@127: Chris@127: Layer *layer = getSelectedLayer(); Chris@127: if (layer && layer->isLayerEditable()) { Chris@127: layer->drawDrag(this, e); Chris@127: } Chris@127: Chris@127: } else if (mode == ViewManager::EditMode) { Chris@127: Chris@127: if (!editSelectionDrag(e)) { Chris@127: Layer *layer = getSelectedLayer(); Chris@127: if (layer && layer->isLayerEditable()) { Chris@127: layer->editDrag(this, e); Chris@127: } Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@174: Pane::zoomToRegion(int x0, int y0, int x1, int y1) Chris@174: { Chris@174: int w = x1 - x0; Chris@174: Chris@174: long newStartFrame = getFrameForX(x0); Chris@174: Chris@174: long visibleFrames = getEndFrame() - getStartFrame(); Chris@174: if (newStartFrame <= -visibleFrames) { Chris@174: newStartFrame = -visibleFrames + 1; Chris@174: } Chris@174: Chris@174: if (newStartFrame >= long(getModelsEndFrame())) { Chris@174: newStartFrame = getModelsEndFrame() - 1; Chris@174: } Chris@174: Chris@174: float ratio = float(w) / float(width()); Chris@174: // std::cerr << "ratio: " << ratio << std::endl; Chris@174: size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio); Chris@174: if (newZoomLevel < 1) newZoomLevel = 1; Chris@174: Chris@174: // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl; Chris@174: setZoomLevel(getZoomConstraintBlockSize(newZoomLevel)); Chris@174: setStartFrame(newStartFrame); Chris@174: Chris@174: QString unit; Chris@174: float min, max; Chris@174: bool log; Chris@174: Layer *layer = 0; Chris@174: for (LayerList::const_iterator i = m_layers.begin(); Chris@174: i != m_layers.end(); ++i) { Chris@174: if ((*i)->getValueExtents(min, max, log, unit) && Chris@174: (*i)->getDisplayExtents(min, max)) { Chris@174: layer = *i; Chris@174: break; Chris@174: } Chris@174: } Chris@174: Chris@174: if (layer) { Chris@174: if (log) { Chris@174: min = (min < 0.0) ? -log10f(-min) : (min == 0.0) ? 0.0 : log10f(min); Chris@174: max = (max < 0.0) ? -log10f(-max) : (max == 0.0) ? 0.0 : log10f(max); Chris@174: } Chris@174: float rmin = min + ((max - min) * (height() - y1)) / height(); Chris@174: float rmax = min + ((max - min) * (height() - y0)) / height(); Chris@174: std::cerr << "min: " << min << ", max: " << max << ", y0: " << y0 << ", y1: " << y1 << ", h: " << height() << ", rmin: " << rmin << ", rmax: " << rmax << std::endl; Chris@174: if (log) { Chris@174: rmin = powf(10, rmin); Chris@174: rmax = powf(10, rmax); Chris@174: } Chris@174: std::cerr << "finally: rmin: " << rmin << ", rmax: " << rmax << " " << unit.toStdString() << std::endl; Chris@174: Chris@174: layer->setDisplayExtents(rmin, rmax); Chris@174: updateVerticalPanner(); Chris@174: } Chris@174: } Chris@174: Chris@174: void Chris@174: Pane::dragTopLayer(QMouseEvent *e) Chris@174: { Chris@174: // We need to avoid making it too easy to drag both Chris@174: // horizontally and vertically, in the case where the Chris@174: // mouse is moved "mostly" in horizontal or vertical axis Chris@174: // with only a small variation in the other axis. This is Chris@174: // particularly important during playback (when we want to Chris@174: // avoid small horizontal motions) or in slow refresh Chris@174: // layers like spectrogram (when we want to avoid small Chris@174: // vertical motions). Chris@174: // Chris@174: // To this end we have horizontal and vertical thresholds Chris@174: // and a series of states: unresolved, horizontally or Chris@174: // vertically constrained, free. Chris@174: // Chris@174: // When the mouse first moves, we're unresolved: we Chris@174: // restrict ourselves to whichever direction seems safest, Chris@174: // until the mouse has passed a small threshold distance Chris@174: // from the click point. Then we lock in to one of the Chris@174: // constrained modes, based on which axis that distance Chris@174: // was measured in first. Finally, if it turns out we've Chris@174: // also moved more than a certain larger distance in the Chris@174: // other direction as well, we may switch into free mode. Chris@174: // Chris@174: // If the top layer is incapable of being dragged Chris@174: // vertically, the logic is short circuited. Chris@174: Chris@174: int xdiff = e->x() - m_clickPos.x(); Chris@174: int ydiff = e->y() - m_clickPos.y(); Chris@174: int smallThreshold = 10, bigThreshold = 50; Chris@174: Chris@174: bool canMoveVertical = canTopLayerMoveVertical(); Chris@174: bool canMoveHorizontal = true; Chris@174: Chris@174: if (!canMoveHorizontal) { Chris@174: m_dragMode = HorizontalDrag; Chris@174: } Chris@174: Chris@174: if (m_dragMode == UnresolvedDrag) { Chris@174: Chris@174: if (abs(ydiff) > smallThreshold && Chris@174: abs(ydiff) > abs(xdiff) * 2) { Chris@174: m_dragMode = VerticalDrag; Chris@174: } else if (abs(xdiff) > smallThreshold && Chris@174: abs(xdiff) > abs(ydiff) * 2) { Chris@174: m_dragMode = HorizontalDrag; Chris@174: } else if (abs(xdiff) > smallThreshold && Chris@174: abs(ydiff) > smallThreshold) { Chris@174: m_dragMode = FreeDrag; Chris@174: } else { Chris@174: // When playing, we don't want to disturb the play Chris@174: // position too easily; when not playing, we don't Chris@174: // want to move up/down too easily Chris@174: if (m_manager && m_manager->isPlaying()) { Chris@174: canMoveHorizontal = false; Chris@174: } else { Chris@174: canMoveVertical = false; Chris@174: } Chris@174: } Chris@174: } Chris@174: Chris@174: if (m_dragMode == VerticalDrag) { Chris@174: if (abs(xdiff) > bigThreshold) m_dragMode = FreeDrag; Chris@174: else canMoveHorizontal = false; Chris@174: } Chris@174: Chris@174: if (m_dragMode == HorizontalDrag && canMoveVertical) { Chris@174: if (abs(ydiff) > bigThreshold) m_dragMode = FreeDrag; Chris@174: else canMoveVertical = false; Chris@174: } Chris@174: Chris@174: if (canMoveHorizontal) { Chris@174: Chris@174: long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x()); Chris@174: Chris@174: size_t newCentreFrame = m_dragCentreFrame; Chris@174: Chris@174: if (frameOff < 0) { Chris@174: newCentreFrame -= frameOff; Chris@174: } else if (newCentreFrame >= size_t(frameOff)) { Chris@174: newCentreFrame -= frameOff; Chris@174: } else { Chris@174: newCentreFrame = 0; Chris@174: } Chris@174: Chris@174: if (newCentreFrame >= getModelsEndFrame()) { Chris@174: newCentreFrame = getModelsEndFrame(); Chris@174: if (newCentreFrame > 0) --newCentreFrame; Chris@174: } Chris@174: Chris@174: if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) { Chris@174: setCentreFrame(newCentreFrame); Chris@174: } Chris@174: } Chris@174: Chris@174: if (canMoveVertical) { Chris@174: Chris@174: float vmin = 0.f, vmax = 0.f; Chris@174: float dmin = 0.f, dmax = 0.f; Chris@174: Chris@174: if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) { Chris@174: Chris@174: std::cerr << "ydiff = " << ydiff << std::endl; Chris@174: Chris@174: float perpix = (dmax - dmin) / height(); Chris@174: float valdiff = ydiff * perpix; Chris@174: std::cerr << "valdiff = " << valdiff << std::endl; Chris@174: Chris@174: float newmin = m_dragStartMinValue + valdiff; Chris@174: float newmax = m_dragStartMinValue + (dmax - dmin) + valdiff; Chris@174: if (newmin < vmin) { Chris@174: newmax += vmin - newmin; Chris@174: newmin += vmin - newmin; Chris@174: } Chris@174: if (newmax > vmax) { Chris@174: newmin -= newmax - vmax; Chris@174: newmax -= newmax - vmax; Chris@174: } Chris@174: std::cerr << "(" << dmin << ", " << dmax << ") -> (" Chris@174: << newmin << ", " << newmax << ") (drag start " << m_dragStartMinValue << ")" << std::endl; Chris@174: Chris@174: setTopLayerDisplayExtents(newmin, newmax); Chris@174: updateVerticalPanner(); Chris@174: } Chris@174: } Chris@174: } Chris@174: Chris@174: void Chris@174: Pane::dragExtendSelection(QMouseEvent *e) Chris@174: { Chris@174: int mouseFrame = getFrameForX(e->x()); Chris@174: size_t resolution = 1; Chris@174: int snapFrameLeft = mouseFrame; Chris@174: int snapFrameRight = mouseFrame; Chris@174: Chris@174: Layer *layer = getSelectedLayer(); Chris@174: if (layer && !m_shiftPressed) { Chris@174: layer->snapToFeatureFrame(this, snapFrameLeft, Chris@174: resolution, Layer::SnapLeft); Chris@174: layer->snapToFeatureFrame(this, snapFrameRight, Chris@174: resolution, Layer::SnapRight); Chris@174: } Chris@174: Chris@174: // std::cerr << "snap: frame = " << mouseFrame << ", start frame = " << m_selectionStartFrame << ", left = " << snapFrameLeft << ", right = " << snapFrameRight << std::endl; Chris@174: Chris@174: if (snapFrameLeft < 0) snapFrameLeft = 0; Chris@174: if (snapFrameRight < 0) snapFrameRight = 0; Chris@174: Chris@174: size_t min, max; Chris@174: Chris@174: if (m_selectionStartFrame > snapFrameLeft) { Chris@174: min = snapFrameLeft; Chris@174: max = m_selectionStartFrame; Chris@174: } else if (snapFrameRight > m_selectionStartFrame) { Chris@174: min = m_selectionStartFrame; Chris@174: max = snapFrameRight; Chris@174: } else { Chris@174: min = snapFrameLeft; Chris@174: max = snapFrameRight; Chris@174: } Chris@174: Chris@174: if (m_manager) { Chris@174: m_manager->setInProgressSelection(Selection(min, max), Chris@174: !m_resizing && !m_ctrlPressed); Chris@174: } Chris@174: Chris@174: bool doScroll = false; Chris@174: if (!m_manager) doScroll = true; Chris@174: if (!m_manager->isPlaying()) doScroll = true; Chris@174: if (m_followPlay != PlaybackScrollContinuous) doScroll = true; Chris@174: Chris@174: if (doScroll) { Chris@174: int offset = mouseFrame - getStartFrame(); Chris@174: int available = getEndFrame() - getStartFrame(); Chris@174: if (offset >= available * 0.95) { Chris@174: int move = int(offset - available * 0.95) + 1; Chris@174: setCentreFrame(m_centreFrame + move); Chris@174: } else if (offset <= available * 0.10) { Chris@174: int move = int(available * 0.10 - offset) + 1; Chris@174: if (m_centreFrame > move) { Chris@174: setCentreFrame(m_centreFrame - move); Chris@174: } else { Chris@174: setCentreFrame(0); Chris@174: } Chris@174: } Chris@174: } Chris@174: Chris@174: update(); Chris@174: } Chris@174: Chris@174: void Chris@127: Pane::mouseDoubleClickEvent(QMouseEvent *e) Chris@127: { Chris@127: if (e->buttons() & Qt::RightButton) { Chris@127: return; Chris@127: } Chris@127: Chris@127: // std::cerr << "mouseDoubleClickEvent" << std::endl; Chris@127: Chris@127: m_clickPos = e->pos(); Chris@127: m_clickedInRange = true; Chris@127: m_shiftPressed = (e->modifiers() & Qt::ShiftModifier); Chris@127: m_ctrlPressed = (e->modifiers() & Qt::ControlModifier); Chris@127: Chris@127: ViewManager::ToolMode mode = ViewManager::NavigateMode; Chris@127: if (m_manager) mode = m_manager->getToolMode(); Chris@127: Chris@127: if (mode == ViewManager::NavigateMode || Chris@127: mode == ViewManager::EditMode) { Chris@127: Chris@127: Layer *layer = getSelectedLayer(); Chris@127: if (layer && layer->isLayerEditable()) { Chris@127: layer->editOpen(this, e); Chris@127: } Chris@127: } Chris@127: } Chris@127: Chris@127: void Chris@127: Pane::leaveEvent(QEvent *) Chris@127: { Chris@127: bool previouslyIdentifying = m_identifyFeatures; Chris@127: m_identifyFeatures = false; Chris@127: if (previouslyIdentifying) update(); Chris@189: emit contextHelpChanged(""); Chris@127: } Chris@127: Chris@127: void Chris@133: Pane::resizeEvent(QResizeEvent *) Chris@133: { Chris@133: updateHeadsUpDisplay(); Chris@133: } Chris@133: Chris@133: void Chris@127: Pane::wheelEvent(QWheelEvent *e) Chris@127: { Chris@127: //std::cerr << "wheelEvent, delta " << e->delta() << std::endl; Chris@127: Chris@127: int count = e->delta(); Chris@127: Chris@127: if (count > 0) { Chris@127: if (count >= 120) count /= 120; Chris@127: else count = 1; Chris@127: } Chris@127: Chris@127: if (count < 0) { Chris@127: if (count <= -120) count /= 120; Chris@127: else count = -1; Chris@127: } Chris@127: Chris@127: if (e->modifiers() & Qt::ControlModifier) { Chris@127: Chris@127: // Scroll left or right, rapidly Chris@127: Chris@127: if (getStartFrame() < 0 && Chris@127: getEndFrame() >= getModelsEndFrame()) return; Chris@127: Chris@127: long delta = ((width() / 2) * count * m_zoomLevel); Chris@127: Chris@127: if (int(m_centreFrame) < delta) { Chris@127: setCentreFrame(0); Chris@127: } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) { Chris@127: setCentreFrame(getModelsEndFrame()); Chris@127: } else { Chris@127: setCentreFrame(m_centreFrame - delta); Chris@127: } Chris@127: Chris@127: } else { Chris@127: Chris@127: // Zoom in or out Chris@127: Chris@127: int newZoomLevel = m_zoomLevel; Chris@127: Chris@127: while (count > 0) { Chris@127: if (newZoomLevel <= 2) { Chris@127: newZoomLevel = 1; Chris@127: break; Chris@127: } Chris@127: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1, Chris@127: ZoomConstraint::RoundDown); Chris@127: --count; Chris@127: } Chris@127: Chris@127: while (count < 0) { Chris@127: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1, Chris@127: ZoomConstraint::RoundUp); Chris@127: ++count; Chris@127: } Chris@127: Chris@127: if (newZoomLevel != m_zoomLevel) { Chris@127: setZoomLevel(newZoomLevel); Chris@127: } Chris@127: } Chris@127: Chris@127: emit paneInteractedWith(); Chris@127: } Chris@127: Chris@132: void Chris@132: Pane::horizontalThumbwheelMoved(int value) Chris@132: { Chris@137: //!!! dupe with updateHeadsUpDisplay Chris@137: Chris@132: int count = 0; Chris@132: int level = 1; Chris@137: Chris@137: Chris@137: //!!! pull out into function (presumably in View) Chris@137: bool haveConstraint = false; Chris@137: for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end(); Chris@137: ++i) { Chris@137: if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) { Chris@137: haveConstraint = true; Chris@137: break; Chris@137: } Chris@132: } Chris@132: Chris@137: if (haveConstraint) { Chris@137: while (true) { Chris@137: if (m_hthumb->getMaximumValue() - value == count) break; Chris@137: int newLevel = getZoomConstraintBlockSize(level + 1, Chris@137: ZoomConstraint::RoundUp); Chris@137: if (newLevel == level) break; Chris@137: level = newLevel; Chris@137: if (++count == 50) break; Chris@137: } Chris@137: } else { Chris@137: while (true) { Chris@137: if (m_hthumb->getMaximumValue() - value == count) break; Chris@137: int step = level / 10; Chris@137: int pwr = 0; Chris@137: while (step > 0) { Chris@137: ++pwr; Chris@137: step /= 2; Chris@137: } Chris@137: step = 1; Chris@137: while (pwr > 0) { Chris@137: step *= 2; Chris@137: --pwr; Chris@137: } Chris@137: // std::cerr << level << std::endl; Chris@137: level += step; Chris@137: if (++count == 100 || level > 262144) break; Chris@137: } Chris@137: } Chris@137: Chris@137: std::cerr << "new level is " << level << std::endl; Chris@132: setZoomLevel(level); Chris@132: } Chris@132: Chris@132: void Chris@132: Pane::verticalThumbwheelMoved(int value) Chris@132: { Chris@133: Layer *layer = 0; Chris@133: if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1); Chris@133: if (layer) { Chris@133: int defaultStep = 0; Chris@133: int max = layer->getVerticalZoomSteps(defaultStep); Chris@133: if (max == 0) { Chris@133: updateHeadsUpDisplay(); Chris@133: return; Chris@133: } Chris@133: if (value > max) { Chris@133: value = max; Chris@133: } Chris@133: layer->setVerticalZoomStep(value); Chris@174: updateVerticalPanner(); Chris@133: } Chris@132: } Chris@132: Chris@174: void Chris@174: Pane::verticalPannerMoved(float x0, float y0, float w, float h) Chris@174: { Chris@174: float vmin, vmax, dmin, dmax; Chris@174: if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) return; Chris@174: float y1 = y0 + h; Chris@174: float newmax = vmin + ((1.0 - y0) * (vmax - vmin)); Chris@174: float newmin = vmin + ((1.0 - y1) * (vmax - vmin)); Chris@174: std::cerr << "verticalPannerMoved: (" << x0 << "," << y0 << "," << w Chris@174: << "," << h << ") -> (" << newmin << "," << newmax << ")" << std::endl; Chris@174: setTopLayerDisplayExtents(newmin, newmax); Chris@174: } Chris@174: Chris@188: void Chris@188: Pane::editVerticalPannerExtents() Chris@188: { Chris@188: if (!m_vpan || !m_manager || !m_manager->getZoomWheelsEnabled()) return; Chris@188: Chris@188: float vmin, vmax, dmin, dmax; Chris@188: QString unit; Chris@188: if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax, &unit) Chris@188: || vmax == vmin) { Chris@188: return; Chris@188: } Chris@188: Chris@188: RangeInputDialog dialog(tr("Enter new range"), Chris@188: tr("New vertical display range, from %1 to %2 %4:") Chris@188: .arg(vmin).arg(vmax).arg(unit), Chris@188: unit, vmin, vmax, this); Chris@188: dialog.setRange(dmin, dmax); Chris@188: Chris@188: if (dialog.exec() == QDialog::Accepted) { Chris@188: dialog.getRange(dmin, dmax); Chris@188: setTopLayerDisplayExtents(dmin, dmax); Chris@188: updateVerticalPanner(); Chris@188: } Chris@188: } Chris@188: Chris@127: bool Chris@127: Pane::editSelectionStart(QMouseEvent *e) Chris@127: { Chris@127: if (!m_identifyFeatures || Chris@127: !m_manager || Chris@127: m_manager->getToolMode() != ViewManager::EditMode) { Chris@127: return false; Chris@127: } Chris@127: Chris@127: bool closeToLeft, closeToRight; Chris@127: Selection s(getSelectionAt(e->x(), closeToLeft, closeToRight)); Chris@127: if (s.isEmpty()) return false; Chris@127: m_editingSelection = s; Chris@127: m_editingSelectionEdge = (closeToLeft ? -1 : closeToRight ? 1 : 0); Chris@127: m_mousePos = e->pos(); Chris@127: return true; Chris@127: } Chris@127: Chris@127: bool Chris@127: Pane::editSelectionDrag(QMouseEvent *e) Chris@127: { Chris@127: if (m_editingSelection.isEmpty()) return false; Chris@127: m_mousePos = e->pos(); Chris@127: update(); Chris@127: return true; Chris@127: } Chris@127: Chris@127: bool Chris@127: Pane::editSelectionEnd(QMouseEvent *e) Chris@127: { Chris@127: if (m_editingSelection.isEmpty()) return false; Chris@127: Chris@127: int offset = m_mousePos.x() - m_clickPos.x(); Chris@127: Layer *layer = getSelectedLayer(); Chris@127: Chris@127: if (offset == 0 || !layer) { Chris@127: m_editingSelection = Selection(); Chris@127: return true; Chris@127: } Chris@127: Chris@127: int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset; Chris@127: int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset; Chris@127: Chris@127: long f0 = getFrameForX(p0); Chris@127: long f1 = getFrameForX(p1); Chris@127: Chris@127: Selection newSelection(f0, f1); Chris@127: Chris@127: if (m_editingSelectionEdge == 0) { Chris@127: Chris@127: CommandHistory::getInstance()->startCompoundOperation Chris@127: (tr("Drag Selection"), true); Chris@127: Chris@127: layer->moveSelection(m_editingSelection, f0); Chris@127: Chris@127: } else { Chris@127: Chris@127: CommandHistory::getInstance()->startCompoundOperation Chris@127: (tr("Resize Selection"), true); Chris@127: Chris@127: if (m_editingSelectionEdge < 0) { Chris@127: f1 = m_editingSelection.getEndFrame(); Chris@127: } else { Chris@127: f0 = m_editingSelection.getStartFrame(); Chris@127: } Chris@127: Chris@127: newSelection = Selection(f0, f1); Chris@127: layer->resizeSelection(m_editingSelection, newSelection); Chris@127: } Chris@127: Chris@127: m_manager->removeSelection(m_editingSelection); Chris@127: m_manager->addSelection(newSelection); Chris@127: Chris@127: CommandHistory::getInstance()->endCompoundOperation(); Chris@127: Chris@127: m_editingSelection = Selection(); Chris@127: return true; Chris@127: } Chris@127: Chris@127: void Chris@127: Pane::toolModeChanged() Chris@127: { Chris@127: ViewManager::ToolMode mode = m_manager->getToolMode(); Chris@127: // std::cerr << "Pane::toolModeChanged(" << mode << ")" << std::endl; Chris@127: Chris@127: switch (mode) { Chris@127: Chris@127: case ViewManager::NavigateMode: Chris@127: setCursor(Qt::PointingHandCursor); Chris@127: break; Chris@127: Chris@127: case ViewManager::SelectMode: Chris@127: setCursor(Qt::ArrowCursor); Chris@127: break; Chris@127: Chris@127: case ViewManager::EditMode: Chris@127: setCursor(Qt::UpArrowCursor); Chris@127: break; Chris@127: Chris@127: case ViewManager::DrawMode: Chris@127: setCursor(Qt::CrossCursor); Chris@127: break; Chris@127: /* Chris@127: case ViewManager::TextMode: Chris@127: setCursor(Qt::IBeamCursor); Chris@127: break; Chris@127: */ Chris@127: } Chris@127: } Chris@127: Chris@133: void Chris@133: Pane::zoomWheelsEnabledChanged() Chris@133: { Chris@133: updateHeadsUpDisplay(); Chris@133: update(); Chris@133: } Chris@133: Chris@133: void Chris@224: Pane::viewZoomLevelChanged(View *v, unsigned long z, bool locked) Chris@133: { Chris@212: // std::cerr << "Pane[" << this << "]::zoomLevelChanged (global now " Chris@212: // << (m_manager ? m_manager->getGlobalZoom() : 0) << ")" << std::endl; Chris@192: Chris@224: View::viewZoomLevelChanged(v, z, locked); Chris@224: Chris@224: if (!m_vthumb->isVisible()) return; Chris@224: Chris@222: if (v != this) { Chris@222: if (!locked || !m_followZoom) return; Chris@222: } Chris@222: Chris@133: if (m_manager && m_manager->getZoomWheelsEnabled()) { Chris@133: updateHeadsUpDisplay(); Chris@133: } Chris@133: } Chris@133: Chris@133: void Chris@133: Pane::propertyContainerSelected(View *v, PropertyContainer *pc) Chris@133: { Chris@133: Layer *layer = 0; Chris@133: Chris@133: if (getLayerCount() > 0) { Chris@133: layer = getLayer(getLayerCount() - 1); Chris@133: disconnect(layer, SIGNAL(verticalZoomChanged()), Chris@133: this, SLOT(verticalZoomChanged())); Chris@133: } Chris@133: Chris@133: View::propertyContainerSelected(v, pc); Chris@133: updateHeadsUpDisplay(); Chris@133: Chris@187: if (m_vthumb) { Chris@187: RangeMapper *rm = 0; Chris@187: if (layer) rm = layer->getNewVerticalZoomRangeMapper(); Chris@187: if (rm) m_vthumb->setRangeMapper(rm); Chris@187: } Chris@187: Chris@133: if (getLayerCount() > 0) { Chris@133: layer = getLayer(getLayerCount() - 1); Chris@133: connect(layer, SIGNAL(verticalZoomChanged()), Chris@133: this, SLOT(verticalZoomChanged())); Chris@133: } Chris@133: } Chris@133: Chris@133: void Chris@133: Pane::verticalZoomChanged() Chris@133: { Chris@133: Layer *layer = 0; Chris@133: Chris@133: if (getLayerCount() > 0) { Chris@133: Chris@133: layer = getLayer(getLayerCount() - 1); Chris@133: Chris@133: if (m_vthumb && m_vthumb->isVisible()) { Chris@133: m_vthumb->setValue(layer->getCurrentVerticalZoomStep()); Chris@133: } Chris@133: } Chris@133: } Chris@133: Chris@189: void Chris@189: Pane::updateContextHelp(const QPoint *pos) Chris@189: { Chris@189: QString help = ""; Chris@189: Chris@189: if (m_clickedInRange) { Chris@189: emit contextHelpChanged(""); Chris@189: return; Chris@189: } Chris@189: Chris@189: ViewManager::ToolMode mode = ViewManager::NavigateMode; Chris@189: if (m_manager) mode = m_manager->getToolMode(); Chris@189: Chris@189: bool editable = false; Chris@189: Layer *layer = getSelectedLayer(); Chris@189: if (layer && layer->isLayerEditable()) { Chris@189: editable = true; Chris@189: } Chris@189: Chris@189: if (mode == ViewManager::NavigateMode) { Chris@189: Chris@189: help = tr("Click and drag to navigate"); Chris@189: Chris@189: } else if (mode == ViewManager::SelectMode) { Chris@189: Chris@217: if (!hasTopLayerTimeXAxis()) return; Chris@217: Chris@189: bool haveSelection = (m_manager && !m_manager->getSelections().empty()); Chris@189: Chris@189: if (haveSelection) { Chris@189: if (editable) { Chris@189: 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: } else { Chris@189: help = tr("Click and drag to select a range; hold Ctrl for multi-select; middle-click and drag to navigate"); Chris@189: } Chris@189: Chris@189: if (pos) { Chris@189: bool closeToLeft = false, closeToRight = false; Chris@189: Selection selection = getSelectionAt(pos->x(), closeToLeft, closeToRight); Chris@189: if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) { Chris@189: Chris@189: help = tr("Click and drag to move the selection boundary"); Chris@189: } Chris@189: } Chris@189: } else { Chris@189: if (editable) { Chris@189: help = tr("Click and drag to select a range; hold Shift to avoid snapping to items; middle-click to navigate"); Chris@189: } else { Chris@189: help = tr("Click and drag to select a range; middle-click and drag to navigate"); Chris@189: } Chris@189: } Chris@189: Chris@189: } else if (mode == ViewManager::DrawMode) { Chris@189: Chris@189: //!!! could call through to a layer function to find out exact meaning Chris@189: if (editable) { Chris@189: help = tr("Click to add a new item in the active layer"); Chris@189: } Chris@189: Chris@189: } else if (mode == ViewManager::EditMode) { Chris@189: Chris@189: //!!! could call through to layer Chris@189: if (editable) { Chris@189: help = tr("Click and drag an item in the active layer to move it"); Chris@189: if (pos) { Chris@189: bool closeToLeft = false, closeToRight = false; Chris@189: Selection selection = getSelectionAt(pos->x(), closeToLeft, closeToRight); Chris@189: if (!selection.isEmpty()) { Chris@189: help = tr("Click and drag to move all items in the selected range"); Chris@189: } Chris@189: } Chris@189: } Chris@189: } Chris@189: Chris@189: emit contextHelpChanged(help); Chris@189: } Chris@189: Chris@189: void Chris@189: Pane::mouseEnteredWidget() Chris@189: { Chris@189: QWidget *w = dynamic_cast(sender()); Chris@189: if (!w) return; Chris@189: Chris@189: if (w == m_vpan) { Chris@189: emit contextHelpChanged(tr("Click and drag to adjust the visible range of the vertical scale")); Chris@189: } else if (w == m_vthumb) { Chris@189: emit contextHelpChanged(tr("Click and drag to adjust the vertical zoom level")); Chris@189: } else if (w == m_hthumb) { Chris@189: emit contextHelpChanged(tr("Click and drag to adjust the horizontal zoom level")); Chris@189: } else if (w == m_reset) { Chris@189: emit contextHelpChanged(tr("Reset horizontal and vertical zoom levels to their defaults")); Chris@189: } Chris@189: } Chris@189: Chris@189: void Chris@189: Pane::mouseLeftWidget() Chris@189: { Chris@189: emit contextHelpChanged(""); Chris@189: } Chris@189: Chris@127: QString Chris@127: Pane::toXmlString(QString indent, QString extraAttributes) const Chris@127: { Chris@127: return View::toXmlString Chris@127: (indent, Chris@127: QString("type=\"pane\" centreLineVisible=\"%1\" height=\"%2\" %3") Chris@127: .arg(m_centreLineVisible).arg(height()).arg(extraAttributes)); Chris@127: } Chris@127: Chris@127: