Chris@58: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@59: Sonic Visualiser Chris@59: An audio file viewer and annotation editor. Chris@59: Centre for Digital Music, Queen Mary, University of London. Chris@59: This file copyright 2006 Chris Cannam. Chris@0: Chris@59: This program is free software; you can redistribute it and/or Chris@59: modify it under the terms of the GNU General Public License as Chris@59: published by the Free Software Foundation; either version 2 of the Chris@59: License, or (at your option) any later version. See the file Chris@59: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "widgets/Pane.h" Chris@0: #include "base/Layer.h" Chris@0: #include "base/Model.h" Chris@0: #include "base/ZoomConstraint.h" Chris@0: #include "base/RealTime.h" Chris@0: #include "base/Profiler.h" Chris@13: #include "base/ViewManager.h" Chris@99: #include "base/CommandHistory.h" Chris@41: #include "layer/WaveformLayer.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: using std::cerr; Chris@0: using std::endl; Chris@0: Chris@0: Pane::Pane(QWidget *w) : Chris@0: View(w, true), Chris@0: m_identifyFeatures(false), Chris@0: m_clickedInRange(false), Chris@0: m_shiftPressed(false), Chris@13: m_ctrlPressed(false), Chris@17: m_navigating(false), Chris@17: m_resizing(false), Chris@0: m_centreLineVisible(true) Chris@0: { Chris@0: setObjectName("Pane"); Chris@0: setMouseTracking(true); Chris@0: } Chris@0: Chris@0: bool Chris@44: Pane::shouldIlluminateLocalFeatures(const Layer *layer, QPoint &pos) const Chris@0: { Chris@42: QPoint discard; Chris@42: bool b0, b1; Chris@42: Chris@42: if (layer == getSelectedLayer() && Chris@42: !shouldIlluminateLocalSelection(discard, b0, b1)) { Chris@42: Chris@27: pos = m_identifyPoint; Chris@27: return m_identifyFeatures; Chris@27: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@42: bool Chris@42: Pane::shouldIlluminateLocalSelection(QPoint &pos, Chris@42: bool &closeToLeft, Chris@44: bool &closeToRight) const Chris@42: { Chris@42: if (m_identifyFeatures && Chris@42: m_manager && Chris@42: m_manager->getToolMode() == ViewManager::EditMode && Chris@42: !m_manager->getSelections().empty() && Chris@42: !selectionIsBeingEdited()) { Chris@42: Chris@42: Selection s(getSelectionAt(m_identifyPoint.x(), Chris@42: closeToLeft, closeToRight)); Chris@42: Chris@42: if (!s.isEmpty()) { Chris@42: if (getSelectedLayer() && getSelectedLayer()->isLayerEditable()) { Chris@42: Chris@42: pos = m_identifyPoint; Chris@42: return true; Chris@42: } Chris@42: } Chris@42: } Chris@42: Chris@42: return false; Chris@42: } Chris@42: Chris@42: bool Chris@42: Pane::selectionIsBeingEdited() const Chris@42: { Chris@42: if (!m_editingSelection.isEmpty()) { Chris@42: if (m_mousePos != m_clickPos && Chris@42: getFrameForX(m_mousePos.x()) != getFrameForX(m_clickPos.x())) { Chris@42: return true; Chris@42: } Chris@42: } Chris@42: return false; Chris@42: } Chris@42: Chris@0: void Chris@0: Pane::setCentreLineVisible(bool visible) Chris@0: { Chris@0: m_centreLineVisible = visible; Chris@0: update(); Chris@0: } Chris@0: Chris@0: void Chris@0: Pane::paintEvent(QPaintEvent *e) Chris@0: { Chris@0: QPainter paint; Chris@0: Chris@0: QRect r(rect()); Chris@0: Chris@0: if (e) { Chris@0: r = e->rect(); Chris@0: } Chris@0: /* Chris@0: paint.begin(this); Chris@0: paint.setClipRect(r); Chris@0: Chris@0: if (hasLightBackground()) { Chris@0: paint.setPen(Qt::white); Chris@0: paint.setBrush(Qt::white); Chris@0: } else { Chris@0: paint.setPen(Qt::black); Chris@0: paint.setBrush(Qt::black); Chris@0: } Chris@0: paint.drawRect(r); Chris@0: Chris@0: paint.end(); Chris@0: */ Chris@0: View::paintEvent(e); Chris@0: Chris@0: paint.begin(this); Chris@0: Chris@0: if (e) { Chris@0: paint.setClipRect(r); Chris@0: } Chris@41: Chris@41: const Model *waveformModel = 0; // just for reporting purposes Chris@51: int verticalScaleWidth = 0; Chris@41: Chris@55: int fontHeight = paint.fontMetrics().height(); Chris@55: int fontAscent = paint.fontMetrics().ascent(); Chris@55: Chris@99: if (m_manager && Chris@99: !m_manager->isPlaying() && Chris@99: m_manager->getToolMode() == ViewManager::SelectMode) { Chris@0: Chris@99: for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) { Chris@99: --vi; Chris@41: Chris@77: std::vector crosshairExtents; Chris@77: Chris@77: if ((*vi)->getCrosshairExtents(this, paint, m_identifyPoint, Chris@77: crosshairExtents)) { Chris@77: (*vi)->paintCrosshairs(this, paint, m_identifyPoint); Chris@99: break; Chris@99: } else if ((*vi)->isLayerOpaque()) { Chris@99: break; Chris@70: } Chris@77: } Chris@99: } Chris@99: Chris@99: for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) { Chris@99: --vi; Chris@99: Chris@99: if (dynamic_cast(*vi)) { Chris@99: waveformModel = (*vi)->getModel(); Chris@99: } Chris@0: Chris@77: if (!m_manager || Chris@77: m_manager->getOverlayMode() == ViewManager::NoOverlays) { Chris@77: break; Chris@77: } Chris@70: Chris@77: verticalScaleWidth = (*vi)->getVerticalScaleWidth(this, paint); Chris@77: Chris@77: if (verticalScaleWidth > 0 && r.left() < verticalScaleWidth) { Chris@0: Chris@0: // Profiler profiler("Pane::paintEvent - painting vertical scale", true); Chris@0: Chris@0: // std::cerr << "Pane::paintEvent: calling paint.save() in vertical scale block" << std::endl; Chris@77: paint.save(); Chris@77: Chris@77: paint.setPen(Qt::black); Chris@77: paint.setBrush(Qt::white); Chris@77: paint.drawRect(0, -1, verticalScaleWidth, height()+1); Chris@77: Chris@77: paint.setBrush(Qt::NoBrush); Chris@77: (*vi)->paintVerticalScale Chris@77: (this, paint, QRect(0, 0, verticalScaleWidth, height())); Chris@77: Chris@77: paint.restore(); Chris@77: } Chris@77: Chris@77: if (m_identifyFeatures) { Chris@77: Chris@77: QPoint pos = m_identifyPoint; Chris@77: QString desc = (*vi)->getFeatureDescription(this, pos); Chris@77: Chris@77: if (desc != "") { Chris@77: Chris@70: paint.save(); Chris@77: Chris@77: int tabStop = Chris@77: paint.fontMetrics().width(tr("Some lengthy prefix:")); Chris@77: Chris@77: QRect boundingRect = Chris@77: paint.fontMetrics().boundingRect Chris@77: (rect(), Chris@77: Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs, Chris@77: desc, tabStop); Chris@0: Chris@77: if (hasLightBackground()) { Chris@77: paint.setPen(Qt::NoPen); Chris@77: paint.setBrush(QColor(250, 250, 250, 200)); Chris@77: } else { Chris@77: paint.setPen(Qt::NoPen); Chris@77: paint.setBrush(QColor(50, 50, 50, 200)); Chris@77: } Chris@0: Chris@77: int extra = paint.fontMetrics().descent(); Chris@77: paint.drawRect(width() - boundingRect.width() - 10 - extra, Chris@77: 10 - extra, Chris@77: boundingRect.width() + 2 * extra, Chris@77: boundingRect.height() + extra); Chris@77: Chris@77: if (hasLightBackground()) { Chris@77: paint.setPen(QColor(150, 20, 0)); Chris@77: } else { Chris@77: paint.setPen(QColor(255, 150, 100)); Chris@77: } Chris@77: Chris@77: QTextOption option; Chris@77: option.setWrapMode(QTextOption::NoWrap); Chris@77: option.setAlignment(Qt::AlignRight | Qt::AlignTop); Chris@77: option.setTabStop(tabStop); Chris@77: paint.drawText(QRectF(width() - boundingRect.width() - 10, 10, Chris@77: boundingRect.width(), Chris@77: boundingRect.height()), Chris@77: desc, Chris@77: option); Chris@0: Chris@70: paint.restore(); Chris@70: } Chris@77: } Chris@25: Chris@77: break; Chris@0: } Chris@0: Chris@55: int sampleRate = getModelsSampleRate(); Chris@55: paint.setBrush(Qt::NoBrush); Chris@55: Chris@0: if (m_centreLineVisible) { Chris@0: Chris@0: if (hasLightBackground()) { Chris@0: paint.setPen(QColor(50, 50, 50)); Chris@0: } else { Chris@0: paint.setPen(QColor(200, 200, 200)); Chris@0: } Chris@0: paint.drawLine(width() / 2, 0, width() / 2, height() - 1); Chris@0: Chris@55: paint.setPen(QColor(50, 50, 50)); Chris@55: Chris@55: int y = height() - fontHeight Chris@55: + fontAscent - 6; Chris@0: Chris@0: LayerList::iterator vi = m_layers.end(); Chris@0: Chris@0: if (vi != m_layers.begin()) { Chris@0: Chris@0: switch ((*--vi)->getPreferredFrameCountPosition()) { Chris@0: Chris@0: case Layer::PositionTop: Chris@55: y = fontAscent + 6; Chris@0: break; Chris@0: Chris@0: case Layer::PositionMiddle: Chris@55: y = (height() - fontHeight) / 2 Chris@55: + fontAscent; Chris@0: break; Chris@0: Chris@0: case Layer::PositionBottom: Chris@0: // y already set correctly Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@70: if (m_manager && Chris@70: m_manager->getOverlayMode() != ViewManager::NoOverlays) { Chris@0: Chris@70: if (sampleRate) { Chris@0: Chris@70: QString text(QString::fromStdString Chris@70: (RealTime::frame2RealTime Chris@70: (m_centreFrame, sampleRate).toText(true))); Chris@70: Chris@70: int tw = paint.fontMetrics().width(text); Chris@70: int x = width()/2 - 4 - tw; Chris@70: Chris@70: drawVisibleText(paint, x, y, text, OutlinedText); Chris@70: } Chris@70: Chris@70: QString text = QString("%1").arg(m_centreFrame); Chris@70: Chris@70: int tw = paint.fontMetrics().width(text); Chris@70: int x = width()/2 + 4; Chris@70: Chris@70: drawVisibleText(paint, x, y, text, OutlinedText); Chris@70: } Chris@55: Chris@55: } else { Chris@55: Chris@55: paint.setPen(QColor(50, 50, 50)); Chris@55: } Chris@55: Chris@55: if (waveformModel && Chris@70: m_manager && Chris@70: m_manager->getOverlayMode() != ViewManager::NoOverlays && Chris@55: r.y() + r.height() >= height() - fontHeight - 6) { Chris@55: Chris@55: size_t mainModelRate = m_manager->getMainModelSampleRate(); Chris@55: size_t playbackRate = m_manager->getPlaybackSampleRate(); Chris@55: Chris@55: QString srNote = ""; Chris@55: Chris@55: // Show (R) for waveform models that will be resampled on Chris@55: // playback, and (X) for waveform models that will be played Chris@55: // at the wrong rate because their rate differs from that of Chris@55: // the main model. Chris@55: Chris@55: if (sampleRate == mainModelRate) { Chris@55: if (sampleRate != playbackRate) srNote = " " + tr("(R)"); Chris@0: } else { Chris@55: std::cerr << "Sample rate = " << sampleRate << ", main model rate = " << mainModelRate << std::endl; Chris@55: srNote = " " + tr("(X)"); Chris@0: } Chris@41: Chris@55: QString desc = tr("%1 / %2Hz%3") Chris@55: .arg(RealTime::frame2RealTime(waveformModel->getEndFrame(), Chris@55: sampleRate) Chris@55: .toText(false).c_str()) Chris@55: .arg(sampleRate) Chris@55: .arg(srNote); Chris@49: Chris@55: if (r.x() < verticalScaleWidth + 5 + paint.fontMetrics().width(desc)) { Chris@55: drawVisibleText(paint, verticalScaleWidth + 5, Chris@55: height() - fontHeight + fontAscent - 6, Chris@55: desc, OutlinedText); Chris@55: } Chris@55: } Chris@50: Chris@70: if (m_manager && Chris@70: m_manager->getOverlayMode() == ViewManager::AllOverlays && Chris@70: r.y() + r.height() >= height() - m_layers.size() * fontHeight - 6) { Chris@51: Chris@51: std::vector texts; Chris@51: int maxTextWidth = 0; Chris@51: Chris@51: for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { Chris@51: Chris@56: QString text = (*i)->getLayerPresentationName(); Chris@51: int tw = paint.fontMetrics().width(text); Chris@61: bool reduced = false; Chris@61: while (tw > width() / 3 && text.length() > 4) { Chris@61: if (!reduced && text.length() > 8) { Chris@61: text = text.left(text.length() - 4); Chris@61: } else { Chris@61: text = text.left(text.length() - 2); Chris@61: } Chris@61: reduced = true; Chris@61: tw = paint.fontMetrics().width(text + "..."); Chris@61: } Chris@61: if (reduced) { Chris@61: texts.push_back(text + "..."); Chris@61: } else { Chris@61: texts.push_back(text); Chris@61: } Chris@51: if (tw > maxTextWidth) maxTextWidth = tw; Chris@51: } Chris@55: Chris@51: int lly = height() - 6; Chris@51: Chris@55: if (r.x() + r.width() >= width() - maxTextWidth - 5) { Chris@55: Chris@55: for (int i = 0; i < texts.size(); ++i) { Chris@51: Chris@55: if (i == texts.size() - 1) { Chris@51: paint.setPen(Qt::black); Chris@51: } Chris@55: Chris@55: drawVisibleText(paint, width() - maxTextWidth - 5, Chris@55: lly - fontHeight + fontAscent, Chris@55: texts[i], OutlinedText); Chris@55: Chris@55: lly -= fontHeight; Chris@51: } Chris@51: } Chris@0: } Chris@0: Chris@0: if (m_clickedInRange && m_shiftPressed) { Chris@19: if (m_manager && (m_manager->getToolMode() == ViewManager::NavigateMode)) { Chris@19: //!!! be nice if this looked a bit more in keeping with the Chris@19: //selection block Chris@19: paint.setPen(Qt::blue); Chris@19: paint.drawRect(m_clickPos.x(), m_clickPos.y(), Chris@19: m_mousePos.x() - m_clickPos.x(), Chris@19: m_mousePos.y() - m_clickPos.y()); Chris@19: } Chris@0: } Chris@0: Chris@42: if (selectionIsBeingEdited()) { Chris@42: Chris@42: int offset = m_mousePos.x() - m_clickPos.x(); Chris@42: int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset; Chris@42: int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset; Chris@42: Chris@42: if (m_editingSelectionEdge < 0) { Chris@42: p1 = getXForFrame(m_editingSelection.getEndFrame()); Chris@42: } else if (m_editingSelectionEdge > 0) { Chris@42: p0 = getXForFrame(m_editingSelection.getStartFrame()); Chris@42: } Chris@42: Chris@42: paint.save(); Chris@42: if (hasLightBackground()) { Chris@42: paint.setPen(QPen(Qt::black, 2)); Chris@42: } else { Chris@42: paint.setPen(QPen(Qt::white, 2)); Chris@42: } Chris@42: Chris@42: //!!! duplicating display policy with View::drawSelections Chris@42: Chris@42: if (m_editingSelectionEdge < 0) { Chris@42: paint.drawLine(p0, 1, p1, 1); Chris@42: paint.drawLine(p0, 0, p0, height()); Chris@42: paint.drawLine(p0, height() - 1, p1, height() - 1); Chris@42: } else if (m_editingSelectionEdge > 0) { Chris@42: paint.drawLine(p0, 1, p1, 1); Chris@42: paint.drawLine(p1, 0, p1, height()); Chris@42: paint.drawLine(p0, height() - 1, p1, height() - 1); Chris@42: } else { Chris@42: paint.setBrush(Qt::NoBrush); Chris@42: paint.drawRect(p0, 1, p1 - p0, height() - 2); Chris@42: } Chris@42: paint.restore(); Chris@42: } Chris@42: Chris@0: paint.end(); Chris@0: } Chris@0: Chris@17: Selection Chris@44: Pane::getSelectionAt(int x, bool &closeToLeftEdge, bool &closeToRightEdge) const Chris@17: { Chris@17: closeToLeftEdge = closeToRightEdge = false; Chris@17: Chris@17: if (!m_manager) return Selection(); Chris@17: Chris@20: long testFrame = getFrameForX(x - 5); Chris@17: if (testFrame < 0) { Chris@20: testFrame = getFrameForX(x); Chris@17: if (testFrame < 0) return Selection(); Chris@17: } Chris@17: Chris@17: Selection selection = m_manager->getContainingSelection(testFrame, true); Chris@17: if (selection.isEmpty()) return selection; Chris@17: Chris@20: int lx = getXForFrame(selection.getStartFrame()); Chris@20: int rx = getXForFrame(selection.getEndFrame()); Chris@17: Chris@17: int fuzz = 2; Chris@17: if (x < lx - fuzz || x > rx + fuzz) return Selection(); Chris@17: Chris@17: int width = rx - lx; Chris@17: fuzz = 3; Chris@17: if (width < 12) fuzz = width / 4; Chris@17: if (fuzz < 1) fuzz = 1; Chris@17: Chris@17: if (x < lx + fuzz) closeToLeftEdge = true; Chris@17: if (x > rx - fuzz) closeToRightEdge = true; Chris@17: Chris@17: return selection; Chris@17: } Chris@17: Chris@0: void Chris@0: Pane::mousePressEvent(QMouseEvent *e) Chris@0: { Chris@89: if (e->buttons() & Qt::RightButton) { Chris@89: emit rightButtonMenuRequested(mapToGlobal(e->pos())); Chris@89: return; Chris@89: } Chris@89: Chris@0: m_clickPos = e->pos(); Chris@0: m_clickedInRange = true; Chris@42: m_editingSelection = Selection(); Chris@42: m_editingSelectionEdge = 0; Chris@0: m_shiftPressed = (e->modifiers() & Qt::ShiftModifier); Chris@13: m_ctrlPressed = (e->modifiers() & Qt::ControlModifier); Chris@13: Chris@13: ViewManager::ToolMode mode = ViewManager::NavigateMode; Chris@13: if (m_manager) mode = m_manager->getToolMode(); Chris@13: Chris@17: m_navigating = false; Chris@13: Chris@17: if (mode == ViewManager::NavigateMode || (e->buttons() & Qt::MidButton)) { Chris@17: Chris@17: if (mode != ViewManager::NavigateMode) { Chris@17: setCursor(Qt::PointingHandCursor); Chris@17: } Chris@17: Chris@17: m_navigating = true; Chris@13: m_dragCentreFrame = m_centreFrame; Chris@13: Chris@13: } else if (mode == ViewManager::SelectMode) { Chris@13: Chris@17: bool closeToLeft = false, closeToRight = false; Chris@17: Selection selection = getSelectionAt(e->x(), closeToLeft, closeToRight); Chris@17: Chris@17: if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) { Chris@17: Chris@17: m_manager->removeSelection(selection); Chris@17: Chris@17: if (closeToLeft) { Chris@17: m_selectionStartFrame = selection.getEndFrame(); Chris@17: } else { Chris@17: m_selectionStartFrame = selection.getStartFrame(); Chris@17: } Chris@17: Chris@17: m_manager->setInProgressSelection(selection, false); Chris@17: m_resizing = true; Chris@13: Chris@17: } else { Chris@17: Chris@20: int mouseFrame = getFrameForX(e->x()); Chris@17: size_t resolution = 1; Chris@17: int snapFrame = mouseFrame; Chris@17: Chris@17: Layer *layer = getSelectedLayer(); Chris@82: if (layer && !m_shiftPressed) { Chris@44: layer->snapToFeatureFrame(this, snapFrame, Chris@44: resolution, Layer::SnapLeft); Chris@17: } Chris@17: Chris@17: if (snapFrame < 0) snapFrame = 0; Chris@17: m_selectionStartFrame = snapFrame; Chris@17: if (m_manager) { Chris@17: m_manager->setInProgressSelection(Selection(snapFrame, Chris@17: snapFrame + resolution), Chris@17: !m_ctrlPressed); Chris@17: } Chris@17: Chris@17: m_resizing = false; Chris@17: } Chris@17: Chris@17: update(); Chris@17: Chris@17: } else if (mode == ViewManager::DrawMode) { Chris@17: Chris@13: Layer *layer = getSelectedLayer(); Chris@23: if (layer && layer->isLayerEditable()) { Chris@44: layer->drawStart(this, e); Chris@13: } Chris@18: Chris@18: } else if (mode == ViewManager::EditMode) { Chris@18: Chris@42: if (!editSelectionStart(e)) { Chris@42: Layer *layer = getSelectedLayer(); Chris@42: if (layer && layer->isLayerEditable()) { Chris@44: layer->editStart(this, e); Chris@42: } Chris@18: } Chris@13: } Chris@0: Chris@0: emit paneInteractedWith(); Chris@0: } Chris@0: Chris@0: void Chris@0: Pane::mouseReleaseEvent(QMouseEvent *e) Chris@0: { Chris@89: if (e->buttons() & Qt::RightButton) { Chris@89: return; Chris@89: } Chris@89: Chris@13: ViewManager::ToolMode mode = ViewManager::NavigateMode; Chris@13: if (m_manager) mode = m_manager->getToolMode(); Chris@13: Chris@0: if (m_clickedInRange) { Chris@0: mouseMoveEvent(e); Chris@0: } Chris@0: Chris@17: if (m_navigating || mode == ViewManager::NavigateMode) { Chris@17: Chris@17: m_navigating = false; Chris@17: Chris@17: if (mode != ViewManager::NavigateMode) { Chris@17: // restore cursor Chris@17: toolModeChanged(); Chris@17: } Chris@0: Chris@13: if (m_shiftPressed) { Chris@0: Chris@13: int x0 = std::min(m_clickPos.x(), m_mousePos.x()); Chris@13: int x1 = std::max(m_clickPos.x(), m_mousePos.x()); Chris@13: int w = x1 - x0; Chris@13: Chris@20: long newStartFrame = getFrameForX(x0); Chris@13: Chris@20: long visibleFrames = getEndFrame() - getStartFrame(); Chris@20: if (newStartFrame <= -visibleFrames) { Chris@20: newStartFrame = -visibleFrames + 1; Chris@13: } Chris@13: Chris@13: if (newStartFrame >= long(getModelsEndFrame())) { Chris@13: newStartFrame = getModelsEndFrame() - 1; Chris@13: } Chris@13: Chris@13: float ratio = float(w) / float(width()); Chris@13: // std::cerr << "ratio: " << ratio << std::endl; Chris@13: size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio); Chris@13: if (newZoomLevel < 1) newZoomLevel = 1; Chris@13: Chris@13: // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl; Chris@13: setZoomLevel(getZoomConstraintBlockSize(newZoomLevel)); Chris@13: setStartFrame(newStartFrame); Chris@13: Chris@13: //cerr << "mouseReleaseEvent: start frame now " << m_startFrame << endl; Chris@13: // update(); Chris@0: } Chris@0: Chris@13: } else if (mode == ViewManager::SelectMode) { Chris@13: Chris@13: if (m_manager && m_manager->haveInProgressSelection()) { Chris@13: Chris@13: bool exclusive; Chris@13: Selection selection = m_manager->getInProgressSelection(exclusive); Chris@13: Chris@13: if (selection.getEndFrame() < selection.getStartFrame() + 2) { Chris@13: selection = Selection(); Chris@13: } Chris@13: Chris@13: m_manager->clearInProgressSelection(); Chris@13: Chris@13: if (exclusive) { Chris@13: m_manager->setSelection(selection); Chris@13: } else { Chris@13: m_manager->addSelection(selection); Chris@13: } Chris@0: } Chris@13: Chris@13: update(); Chris@17: Chris@17: } else if (mode == ViewManager::DrawMode) { Chris@17: Chris@17: Layer *layer = getSelectedLayer(); Chris@23: if (layer && layer->isLayerEditable()) { Chris@44: layer->drawEnd(this, e); Chris@17: update(); Chris@17: } Chris@18: Chris@18: } else if (mode == ViewManager::EditMode) { Chris@18: Chris@42: if (!editSelectionEnd(e)) { Chris@42: Layer *layer = getSelectedLayer(); Chris@42: if (layer && layer->isLayerEditable()) { Chris@44: layer->editEnd(this, e); Chris@42: update(); Chris@42: } Chris@18: } Chris@17: } Chris@0: Chris@0: m_clickedInRange = false; Chris@0: Chris@0: emit paneInteractedWith(); Chris@0: } Chris@0: Chris@0: void Chris@0: Pane::mouseMoveEvent(QMouseEvent *e) Chris@0: { Chris@89: if (e->buttons() & Qt::RightButton) { Chris@89: return; Chris@89: } Chris@89: Chris@13: ViewManager::ToolMode mode = ViewManager::NavigateMode; Chris@13: if (m_manager) mode = m_manager->getToolMode(); Chris@13: Chris@28: QPoint prevPoint = m_identifyPoint; Chris@28: m_identifyPoint = e->pos(); Chris@28: Chris@0: if (!m_clickedInRange) { Chris@0: Chris@17: if (mode == ViewManager::SelectMode) { Chris@17: bool closeToLeft = false, closeToRight = false; Chris@17: getSelectionAt(e->x(), closeToLeft, closeToRight); Chris@17: if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) { Chris@17: setCursor(Qt::SizeHorCursor); Chris@17: } else { Chris@17: setCursor(Qt::ArrowCursor); Chris@17: } Chris@17: } Chris@0: Chris@35: //!!! if (mode != ViewManager::DrawMode) { Chris@0: Chris@67: if (!m_manager->isPlaying()) { Chris@67: Chris@55: if (getSelectedLayer()) { Chris@55: Chris@17: bool previouslyIdentifying = m_identifyFeatures; Chris@17: m_identifyFeatures = true; Chris@17: Chris@17: if (m_identifyFeatures != previouslyIdentifying || Chris@17: m_identifyPoint != prevPoint) { Chris@17: update(); Chris@17: } Chris@55: } Chris@55: Chris@67: } Chris@67: Chris@35: // } Chris@0: Chris@13: return; Chris@13: } Chris@0: Chris@17: if (m_navigating || mode == ViewManager::NavigateMode) { Chris@0: Chris@13: if (m_shiftPressed) { Chris@0: Chris@13: m_mousePos = e->pos(); Chris@13: update(); Chris@0: Chris@0: } else { Chris@13: Chris@20: long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x()); Chris@20: Chris@13: size_t newCentreFrame = m_dragCentreFrame; Chris@13: Chris@13: if (frameOff < 0) { Chris@13: newCentreFrame -= frameOff; Chris@13: } else if (newCentreFrame >= size_t(frameOff)) { Chris@13: newCentreFrame -= frameOff; Chris@13: } else { Chris@13: newCentreFrame = 0; Chris@13: } Chris@13: Chris@13: if (newCentreFrame >= getModelsEndFrame()) { Chris@13: newCentreFrame = getModelsEndFrame(); Chris@13: if (newCentreFrame > 0) --newCentreFrame; Chris@13: } Chris@20: Chris@20: if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) { Chris@13: setCentreFrame(newCentreFrame); Chris@13: } Chris@0: } Chris@0: Chris@13: } else if (mode == ViewManager::SelectMode) { Chris@13: Chris@20: int mouseFrame = getFrameForX(e->x()); Chris@13: size_t resolution = 1; Chris@13: int snapFrameLeft = mouseFrame; Chris@13: int snapFrameRight = mouseFrame; Chris@13: Chris@13: Layer *layer = getSelectedLayer(); Chris@82: if (layer && !m_shiftPressed) { Chris@44: layer->snapToFeatureFrame(this, snapFrameLeft, Chris@44: resolution, Layer::SnapLeft); Chris@44: layer->snapToFeatureFrame(this, snapFrameRight, Chris@44: resolution, Layer::SnapRight); Chris@13: } Chris@13: Chris@37: // std::cerr << "snap: frame = " << mouseFrame << ", start frame = " << m_selectionStartFrame << ", left = " << snapFrameLeft << ", right = " << snapFrameRight << std::endl; Chris@28: Chris@13: if (snapFrameLeft < 0) snapFrameLeft = 0; Chris@13: if (snapFrameRight < 0) snapFrameRight = 0; Chris@13: Chris@13: size_t min, max; Chris@13: Chris@13: if (m_selectionStartFrame > snapFrameLeft) { Chris@13: min = snapFrameLeft; Chris@13: max = m_selectionStartFrame; Chris@13: } else if (snapFrameRight > m_selectionStartFrame) { Chris@13: min = m_selectionStartFrame; Chris@13: max = snapFrameRight; Chris@13: } else { Chris@13: min = snapFrameLeft; Chris@13: max = snapFrameRight; Chris@0: } Chris@0: Chris@13: if (m_manager) { Chris@13: m_manager->setInProgressSelection(Selection(min, max), Chris@17: !m_resizing && !m_ctrlPressed); Chris@0: } Chris@15: Chris@15: bool doScroll = false; Chris@15: if (!m_manager) doScroll = true; Chris@15: if (!m_manager->isPlaying()) doScroll = true; Chris@15: if (m_followPlay != PlaybackScrollContinuous) doScroll = true; Chris@15: Chris@15: if (doScroll) { Chris@13: int offset = mouseFrame - getStartFrame(); Chris@13: int available = getEndFrame() - getStartFrame(); Chris@15: if (offset >= available * 0.95) { Chris@15: int move = int(offset - available * 0.95) + 1; Chris@14: setCentreFrame(m_centreFrame + move); Chris@15: } else if (offset <= available * 0.10) { Chris@15: int move = int(available * 0.10 - offset) + 1; Chris@14: if (m_centreFrame > move) { Chris@14: setCentreFrame(m_centreFrame - move); Chris@14: } else { Chris@14: setCentreFrame(0); Chris@14: } Chris@13: } Chris@13: } Chris@13: Chris@13: update(); Chris@17: Chris@17: } else if (mode == ViewManager::DrawMode) { Chris@17: Chris@17: Layer *layer = getSelectedLayer(); Chris@23: if (layer && layer->isLayerEditable()) { Chris@44: layer->drawDrag(this, e); Chris@17: } Chris@18: Chris@18: } else if (mode == ViewManager::EditMode) { Chris@18: Chris@42: if (!editSelectionDrag(e)) { Chris@42: Layer *layer = getSelectedLayer(); Chris@42: if (layer && layer->isLayerEditable()) { Chris@44: layer->editDrag(this, e); Chris@42: } Chris@18: } Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: Pane::mouseDoubleClickEvent(QMouseEvent *e) Chris@0: { Chris@89: if (e->buttons() & Qt::RightButton) { Chris@89: return; Chris@89: } Chris@89: Chris@101: // std::cerr << "mouseDoubleClickEvent" << std::endl; Chris@36: Chris@36: m_clickPos = e->pos(); Chris@36: m_clickedInRange = true; Chris@36: m_shiftPressed = (e->modifiers() & Qt::ShiftModifier); Chris@36: m_ctrlPressed = (e->modifiers() & Qt::ControlModifier); Chris@36: Chris@36: ViewManager::ToolMode mode = ViewManager::NavigateMode; Chris@36: if (m_manager) mode = m_manager->getToolMode(); Chris@36: Chris@101: if (mode == ViewManager::NavigateMode || Chris@101: mode == ViewManager::EditMode) { Chris@36: Chris@36: Layer *layer = getSelectedLayer(); Chris@36: if (layer && layer->isLayerEditable()) { Chris@44: layer->editOpen(this, e); Chris@36: } Chris@36: } Chris@0: } Chris@0: Chris@0: void Chris@0: Pane::leaveEvent(QEvent *) Chris@0: { Chris@0: bool previouslyIdentifying = m_identifyFeatures; Chris@0: m_identifyFeatures = false; Chris@0: if (previouslyIdentifying) update(); Chris@0: } Chris@0: Chris@0: void Chris@0: Pane::wheelEvent(QWheelEvent *e) Chris@0: { Chris@0: //std::cerr << "wheelEvent, delta " << e->delta() << std::endl; Chris@0: Chris@0: int count = e->delta(); Chris@0: Chris@0: if (count > 0) { Chris@0: if (count >= 120) count /= 120; Chris@0: else count = 1; Chris@0: } Chris@0: Chris@0: if (count < 0) { Chris@0: if (count <= -120) count /= 120; Chris@0: else count = -1; Chris@0: } Chris@17: Chris@17: if (e->modifiers() & Qt::ControlModifier) { Chris@17: Chris@20: // Scroll left or right, rapidly Chris@20: Chris@17: if (getStartFrame() < 0 && Chris@17: getEndFrame() >= getModelsEndFrame()) return; Chris@17: Chris@17: long delta = ((width() / 2) * count * m_zoomLevel); Chris@17: Chris@17: if (int(m_centreFrame) < delta) { Chris@17: setCentreFrame(0); Chris@17: } else if (int(m_centreFrame) - delta >= int(getModelsEndFrame())) { Chris@17: setCentreFrame(getModelsEndFrame()); Chris@17: } else { Chris@17: setCentreFrame(m_centreFrame - delta); Chris@17: } Chris@17: Chris@17: } else { Chris@17: Chris@20: // Zoom in or out Chris@20: Chris@17: int newZoomLevel = m_zoomLevel; Chris@0: Chris@17: while (count > 0) { Chris@17: if (newZoomLevel <= 2) { Chris@17: newZoomLevel = 1; Chris@17: break; Chris@17: } Chris@17: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1, Chris@17: ZoomConstraint::RoundDown); Chris@17: --count; Chris@0: } Chris@17: Chris@17: while (count < 0) { Chris@17: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1, Chris@17: ZoomConstraint::RoundUp); Chris@17: ++count; Chris@17: } Chris@17: Chris@17: if (newZoomLevel != m_zoomLevel) { Chris@17: setZoomLevel(newZoomLevel); Chris@17: } Chris@0: } Chris@0: Chris@0: emit paneInteractedWith(); Chris@0: } Chris@8: Chris@42: bool Chris@42: Pane::editSelectionStart(QMouseEvent *e) Chris@42: { Chris@43: if (!m_identifyFeatures || Chris@43: !m_manager || Chris@43: m_manager->getToolMode() != ViewManager::EditMode) { Chris@43: return false; Chris@43: } Chris@43: Chris@42: bool closeToLeft, closeToRight; Chris@42: Selection s(getSelectionAt(e->x(), closeToLeft, closeToRight)); Chris@42: if (s.isEmpty()) return false; Chris@42: m_editingSelection = s; Chris@42: m_editingSelectionEdge = (closeToLeft ? -1 : closeToRight ? 1 : 0); Chris@42: m_mousePos = e->pos(); Chris@42: return true; Chris@42: } Chris@42: Chris@42: bool Chris@42: Pane::editSelectionDrag(QMouseEvent *e) Chris@42: { Chris@42: if (m_editingSelection.isEmpty()) return false; Chris@42: m_mousePos = e->pos(); Chris@42: update(); Chris@42: return true; Chris@42: } Chris@42: Chris@42: bool Chris@42: Pane::editSelectionEnd(QMouseEvent *e) Chris@42: { Chris@42: if (m_editingSelection.isEmpty()) return false; Chris@43: Chris@43: int offset = m_mousePos.x() - m_clickPos.x(); Chris@43: Layer *layer = getSelectedLayer(); Chris@43: Chris@43: if (offset == 0 || !layer) { Chris@43: m_editingSelection = Selection(); Chris@43: return true; Chris@43: } Chris@43: Chris@43: int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset; Chris@43: int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset; Chris@43: Chris@43: long f0 = getFrameForX(p0); Chris@43: long f1 = getFrameForX(p1); Chris@43: Chris@43: Selection newSelection(f0, f1); Chris@43: Chris@43: if (m_editingSelectionEdge == 0) { Chris@43: Chris@99: CommandHistory::getInstance()->startCompoundOperation Chris@99: (tr("Drag Selection"), true); Chris@99: Chris@43: layer->moveSelection(m_editingSelection, f0); Chris@43: Chris@43: } else { Chris@43: Chris@99: CommandHistory::getInstance()->startCompoundOperation Chris@99: (tr("Resize Selection"), true); Chris@99: Chris@43: if (m_editingSelectionEdge < 0) { Chris@43: f1 = m_editingSelection.getEndFrame(); Chris@43: } else { Chris@43: f0 = m_editingSelection.getStartFrame(); Chris@43: } Chris@43: Chris@43: newSelection = Selection(f0, f1); Chris@43: layer->resizeSelection(m_editingSelection, newSelection); Chris@43: } Chris@43: Chris@43: m_manager->removeSelection(m_editingSelection); Chris@43: m_manager->addSelection(newSelection); Chris@43: Chris@99: CommandHistory::getInstance()->endCompoundOperation(); Chris@99: Chris@42: m_editingSelection = Selection(); Chris@42: return true; Chris@42: } Chris@42: Chris@13: void Chris@13: Pane::toolModeChanged() Chris@13: { Chris@13: ViewManager::ToolMode mode = m_manager->getToolMode(); Chris@101: // std::cerr << "Pane::toolModeChanged(" << mode << ")" << std::endl; Chris@13: Chris@13: switch (mode) { Chris@13: Chris@13: case ViewManager::NavigateMode: Chris@13: setCursor(Qt::PointingHandCursor); Chris@13: break; Chris@13: Chris@13: case ViewManager::SelectMode: Chris@13: setCursor(Qt::ArrowCursor); Chris@13: break; Chris@13: Chris@13: case ViewManager::EditMode: Chris@19: setCursor(Qt::UpArrowCursor); Chris@13: break; Chris@13: Chris@13: case ViewManager::DrawMode: Chris@13: setCursor(Qt::CrossCursor); Chris@13: break; Chris@36: /* Chris@13: case ViewManager::TextMode: Chris@13: setCursor(Qt::IBeamCursor); Chris@13: break; Chris@36: */ Chris@13: } Chris@13: } Chris@13: Chris@8: QString Chris@8: Pane::toXmlString(QString indent, QString extraAttributes) const Chris@8: { Chris@8: return View::toXmlString Chris@8: (indent, Chris@55: QString("type=\"pane\" centreLineVisible=\"%1\" height=\"%2\" %3") Chris@55: .arg(m_centreLineVisible).arg(height()).arg(extraAttributes)); Chris@8: } Chris@8: Chris@0: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "Pane.moc.cpp" Chris@0: #endif Chris@0: