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@127: This file copyright 2006 Chris Cannam. 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@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@127: m_centreLineVisible(true) Chris@127: { Chris@127: setObjectName("Pane"); Chris@127: setMouseTracking(true); Chris@127: } Chris@127: 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: int verticalScaleWidth = 0; 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@127: if (!m_manager || Chris@127: m_manager->getOverlayMode() == ViewManager::NoOverlays) { Chris@127: break; Chris@127: } Chris@127: Chris@127: verticalScaleWidth = (*vi)->getVerticalScaleWidth(this, paint); Chris@127: Chris@127: if (verticalScaleWidth > 0 && r.left() < verticalScaleWidth) { 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@127: paint.drawRect(0, -1, verticalScaleWidth, height()+1); Chris@127: Chris@127: paint.setBrush(Qt::NoBrush); Chris@127: (*vi)->paintVerticalScale Chris@127: (this, paint, QRect(0, 0, verticalScaleWidth, 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@127: if (m_centreLineVisible) { Chris@127: Chris@127: if (hasLightBackground()) { Chris@127: paint.setPen(QColor(50, 50, 50)); Chris@127: } else { Chris@127: paint.setPen(QColor(200, 200, 200)); Chris@127: } Chris@127: paint.drawLine(width() / 2, 0, width() / 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@127: if (m_manager && Chris@127: m_manager->getOverlayMode() != ViewManager::NoOverlays) { 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@127: m_manager->getOverlayMode() != ViewManager::NoOverlays && Chris@127: r.y() + r.height() >= height() - fontHeight - 6) { Chris@127: Chris@127: size_t mainModelRate = m_manager->getMainModelSampleRate(); Chris@127: size_t playbackRate = m_manager->getPlaybackSampleRate(); Chris@127: 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@127: // at the wrong rate because their rate differs from that of Chris@127: // the main model. Chris@127: Chris@127: if (sampleRate == mainModelRate) { Chris@127: if (sampleRate != playbackRate) srNote = " " + tr("(R)"); Chris@127: } else { Chris@127: std::cerr << "Sample rate = " << sampleRate << ", main model rate = " << mainModelRate << std::endl; Chris@127: srNote = " " + tr("(X)"); Chris@127: } 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@127: .arg(sampleRate) Chris@127: .arg(srNote); Chris@127: Chris@127: if (r.x() < verticalScaleWidth + 5 + paint.fontMetrics().width(desc)) { Chris@127: drawVisibleText(paint, verticalScaleWidth + 5, Chris@127: height() - fontHeight + fontAscent - 6, Chris@127: desc, OutlinedText); Chris@127: } Chris@127: } Chris@127: Chris@127: if (m_manager && Chris@127: m_manager->getOverlayMode() == ViewManager::AllOverlays && 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@127: Chris@127: if (r.x() + r.width() >= width() - maxTextWidth - 5) { 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@127: drawVisibleText(paint, width() - maxTextWidth - 5, 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@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@127: void Chris@127: Pane::mousePressEvent(QMouseEvent *e) Chris@127: { Chris@127: if (e->buttons() & Qt::RightButton) { 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@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@127: Chris@127: } else if (mode == ViewManager::SelectMode) { Chris@127: 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: int w = x1 - x0; 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: // int h = y1 - y0; Chris@127: Chris@127: long newStartFrame = getFrameForX(x0); Chris@127: Chris@127: long visibleFrames = getEndFrame() - getStartFrame(); Chris@127: if (newStartFrame <= -visibleFrames) { Chris@127: newStartFrame = -visibleFrames + 1; Chris@127: } Chris@127: Chris@127: if (newStartFrame >= long(getModelsEndFrame())) { Chris@127: newStartFrame = getModelsEndFrame() - 1; Chris@127: } Chris@127: Chris@127: float ratio = float(w) / float(width()); Chris@127: // std::cerr << "ratio: " << ratio << std::endl; Chris@127: size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio); Chris@127: if (newZoomLevel < 1) newZoomLevel = 1; Chris@127: Chris@127: // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl; Chris@127: setZoomLevel(getZoomConstraintBlockSize(newZoomLevel)); Chris@127: setStartFrame(newStartFrame); Chris@127: Chris@127: //!!! lots of faff, shouldn't be here Chris@127: Chris@127: QString unit; Chris@127: float min, max; Chris@127: bool log; Chris@127: Layer *layer = 0; Chris@127: for (LayerList::const_iterator i = m_layers.begin(); Chris@127: i != m_layers.end(); ++i) { Chris@127: if ((*i)->getValueExtents(min, max, log, unit) && Chris@127: (*i)->getDisplayExtents(min, max)) { Chris@127: layer = *i; Chris@127: break; Chris@127: } Chris@127: } Chris@127: Chris@127: if (layer) { Chris@127: if (log) { Chris@127: min = (min < 0.0) ? -log10f(-min) : (min == 0.0) ? 0.0 : log10f(min); Chris@127: max = (max < 0.0) ? -log10f(-max) : (max == 0.0) ? 0.0 : log10f(max); Chris@127: } Chris@127: float rmin = min + ((max - min) * (height() - y1)) / height(); Chris@127: float rmax = min + ((max - min) * (height() - y0)) / height(); Chris@127: std::cerr << "min: " << min << ", max: " << max << ", y0: " << y0 << ", y1: " << y1 << ", h: " << height() << ", rmin: " << rmin << ", rmax: " << rmax << std::endl; Chris@127: if (log) { Chris@127: rmin = powf(10, rmin); Chris@127: rmax = powf(10, rmax); Chris@127: } Chris@127: std::cerr << "finally: rmin: " << rmin << ", rmax: " << rmax << " " << unit.toStdString() << std::endl; Chris@127: Chris@127: layer->setDisplayExtents(rmin, rmax); Chris@127: } Chris@127: Chris@127: //cerr << "mouseReleaseEvent: start frame now " << m_startFrame << endl; Chris@127: // update(); Chris@127: } Chris@127: Chris@127: } else if (mode == ViewManager::SelectMode) { Chris@127: 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@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@127: if (mode == ViewManager::SelectMode) { 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 (mode != ViewManager::DrawMode) { Chris@127: Chris@127: if (!m_manager->isPlaying()) { Chris@127: Chris@127: if (getSelectedLayer()) { Chris@127: Chris@127: bool previouslyIdentifying = m_identifyFeatures; Chris@127: m_identifyFeatures = true; Chris@127: Chris@127: if (m_identifyFeatures != previouslyIdentifying || Chris@127: m_identifyPoint != prevPoint) { Chris@127: update(); Chris@127: } Chris@127: } Chris@127: Chris@127: } Chris@127: 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@127: long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x()); Chris@127: Chris@127: size_t newCentreFrame = m_dragCentreFrame; Chris@127: Chris@127: if (frameOff < 0) { Chris@127: newCentreFrame -= frameOff; Chris@127: } else if (newCentreFrame >= size_t(frameOff)) { Chris@127: newCentreFrame -= frameOff; Chris@127: } else { Chris@127: newCentreFrame = 0; Chris@127: } Chris@127: Chris@127: if (newCentreFrame >= getModelsEndFrame()) { Chris@127: newCentreFrame = getModelsEndFrame(); Chris@127: if (newCentreFrame > 0) --newCentreFrame; Chris@127: } Chris@127: Chris@127: if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) { Chris@127: setCentreFrame(newCentreFrame); Chris@127: } Chris@127: } Chris@127: Chris@127: } else if (mode == ViewManager::SelectMode) { Chris@127: Chris@127: int mouseFrame = getFrameForX(e->x()); Chris@127: size_t resolution = 1; Chris@127: int snapFrameLeft = mouseFrame; Chris@127: int snapFrameRight = mouseFrame; Chris@127: Chris@127: Layer *layer = getSelectedLayer(); Chris@127: if (layer && !m_shiftPressed) { Chris@127: layer->snapToFeatureFrame(this, snapFrameLeft, Chris@127: resolution, Layer::SnapLeft); Chris@127: layer->snapToFeatureFrame(this, snapFrameRight, Chris@127: resolution, Layer::SnapRight); Chris@127: } Chris@127: Chris@127: // std::cerr << "snap: frame = " << mouseFrame << ", start frame = " << m_selectionStartFrame << ", left = " << snapFrameLeft << ", right = " << snapFrameRight << std::endl; Chris@127: Chris@127: if (snapFrameLeft < 0) snapFrameLeft = 0; Chris@127: if (snapFrameRight < 0) snapFrameRight = 0; Chris@127: Chris@127: size_t min, max; Chris@127: Chris@127: if (m_selectionStartFrame > snapFrameLeft) { Chris@127: min = snapFrameLeft; Chris@127: max = m_selectionStartFrame; Chris@127: } else if (snapFrameRight > m_selectionStartFrame) { Chris@127: min = m_selectionStartFrame; Chris@127: max = snapFrameRight; Chris@127: } else { Chris@127: min = snapFrameLeft; Chris@127: max = snapFrameRight; Chris@127: } Chris@127: Chris@127: if (m_manager) { Chris@127: m_manager->setInProgressSelection(Selection(min, max), Chris@127: !m_resizing && !m_ctrlPressed); Chris@127: } Chris@127: Chris@127: bool doScroll = false; Chris@127: if (!m_manager) doScroll = true; Chris@127: if (!m_manager->isPlaying()) doScroll = true; Chris@127: if (m_followPlay != PlaybackScrollContinuous) doScroll = true; Chris@127: Chris@127: if (doScroll) { Chris@127: int offset = mouseFrame - getStartFrame(); Chris@127: int available = getEndFrame() - getStartFrame(); Chris@127: if (offset >= available * 0.95) { Chris@127: int move = int(offset - available * 0.95) + 1; Chris@127: setCentreFrame(m_centreFrame + move); Chris@127: } else if (offset <= available * 0.10) { Chris@127: int move = int(available * 0.10 - offset) + 1; Chris@127: if (m_centreFrame > move) { Chris@127: setCentreFrame(m_centreFrame - move); Chris@127: } else { Chris@127: setCentreFrame(0); Chris@127: } 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->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@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@127: } Chris@127: Chris@127: 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@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@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: Chris@127: #ifdef INCLUDE_MOCFILES Chris@127: #include "Pane.moc.cpp" Chris@127: #endif Chris@127: