Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@5: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. 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@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@41: Chris@0: for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) { Chris@0: --vi; Chris@0: Chris@41: if (dynamic_cast(*vi)) { Chris@41: waveformModel = (*vi)->getModel(); Chris@41: } Chris@41: Chris@44: int sw = (*vi)->getVerticalScaleWidth(this, paint); Chris@0: Chris@0: if (sw > 0 && r.left() < sw) { 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@0: paint.save(); Chris@0: Chris@0: paint.setPen(Qt::black); Chris@0: paint.setBrush(Qt::white); Chris@0: paint.drawRect(0, 0, sw, height()); Chris@0: Chris@0: paint.setBrush(Qt::NoBrush); Chris@44: (*vi)->paintVerticalScale(this, paint, QRect(0, 0, sw, height())); Chris@0: Chris@0: paint.restore(); Chris@0: } Chris@41: Chris@0: if (m_identifyFeatures) { Chris@25: Chris@25: QPoint pos = m_identifyPoint; Chris@44: QString desc = (*vi)->getFeatureDescription(this, pos); Chris@25: Chris@25: if (desc != "") { Chris@25: Chris@25: paint.save(); Chris@25: Chris@25: int tabStop = Chris@25: paint.fontMetrics().width(tr("Some lengthy prefix:")); Chris@25: Chris@25: QRect boundingRect = Chris@25: paint.fontMetrics().boundingRect Chris@25: (rect(), Chris@25: Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs, Chris@25: desc, tabStop); Chris@25: Chris@25: if (hasLightBackground()) { Chris@25: paint.setPen(Qt::NoPen); Chris@25: paint.setBrush(QColor(250, 250, 250, 200)); Chris@25: } else { Chris@25: paint.setPen(Qt::NoPen); Chris@25: paint.setBrush(QColor(50, 50, 50, 200)); Chris@25: } Chris@25: Chris@25: int extra = paint.fontMetrics().descent(); Chris@25: paint.drawRect(width() - boundingRect.width() - 10 - extra, Chris@25: 10 - extra, Chris@25: boundingRect.width() + 2 * extra, Chris@25: boundingRect.height() + extra); Chris@25: Chris@25: if (hasLightBackground()) { Chris@25: paint.setPen(QColor(150, 20, 0)); Chris@25: } else { Chris@25: paint.setPen(QColor(255, 150, 100)); Chris@25: } Chris@25: Chris@25: QTextOption option; Chris@25: option.setWrapMode(QTextOption::NoWrap); Chris@25: option.setAlignment(Qt::AlignRight | Qt::AlignTop); Chris@25: option.setTabStop(tabStop); Chris@25: paint.drawText(QRectF(width() - boundingRect.width() - 10, 10, Chris@25: boundingRect.width(), Chris@25: boundingRect.height()), Chris@25: desc, Chris@25: option); Chris@25: Chris@25: paint.restore(); Chris@25: } Chris@0: } Chris@0: Chris@0: break; Chris@0: } Chris@0: 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.setBrush(Qt::NoBrush); Chris@0: paint.drawLine(width() / 2, 0, width() / 2, height() - 1); Chris@0: Chris@0: // QFont font(paint.font()); Chris@0: // font.setBold(true); Chris@0: // paint.setFont(font); Chris@0: Chris@0: int sampleRate = getModelsSampleRate(); Chris@0: int y = height() - paint.fontMetrics().height() Chris@0: + paint.fontMetrics().ascent() - 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@0: y = paint.fontMetrics().ascent() + 6; Chris@0: break; Chris@0: Chris@0: case Layer::PositionMiddle: Chris@0: y = (height() - paint.fontMetrics().height()) / 2 Chris@0: + paint.fontMetrics().ascent(); 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@0: if (sampleRate) { Chris@0: Chris@0: QString text(QString::fromStdString Chris@0: (RealTime::frame2RealTime Chris@0: (m_centreFrame, sampleRate).toText(true))); Chris@0: Chris@0: int tw = paint.fontMetrics().width(text); Chris@0: int x = width()/2 - 4 - tw; Chris@0: Chris@0: if (hasLightBackground()) { Chris@0: paint.setPen(palette().background().color()); Chris@0: for (int dx = -1; dx <= 1; ++dx) { Chris@0: for (int dy = -1; dy <= 1; ++dy) { Chris@0: if ((dx && dy) || !(dx || dy)) continue; Chris@0: paint.drawText(x + dx, y + dy, text); Chris@0: } Chris@0: } Chris@0: paint.setPen(QColor(50, 50, 50)); Chris@0: } else { Chris@0: paint.setPen(QColor(200, 200, 200)); Chris@0: } Chris@0: Chris@0: paint.drawText(x, y, text); Chris@0: } Chris@0: Chris@0: QString text = QString("%1").arg(m_centreFrame); Chris@0: Chris@0: int tw = paint.fontMetrics().width(text); Chris@0: int x = width()/2 + 4; Chris@0: Chris@0: if (hasLightBackground()) { Chris@0: paint.setPen(palette().background().color()); Chris@0: for (int dx = -1; dx <= 1; ++dx) { Chris@0: for (int dy = -1; dy <= 1; ++dy) { Chris@0: if ((dx && dy) || !(dx || dy)) continue; Chris@0: paint.drawText(x + dx, y + dy, text); Chris@0: } Chris@0: } Chris@0: paint.setPen(QColor(50, 50, 50)); Chris@0: } else { Chris@0: paint.setPen(QColor(200, 200, 200)); Chris@0: } Chris@0: paint.drawText(x, y, text); Chris@41: Chris@41: if (waveformModel) { Chris@49: Chris@49: // Show (R) for waveform models that will be resampled on Chris@49: // playback. Chris@49: Chris@49: //!!! Inadequate for auxiliary waveform layers, which are Chris@49: //played at the wrong rate if their rate differs from the Chris@49: //main model. Need to discover and label with (X) or Chris@49: //something. Chris@41: Chris@48: QString desc = tr("%1 / %2Hz%3") Chris@41: .arg(RealTime::frame2RealTime(waveformModel->getEndFrame(), Chris@48: sampleRate) Chris@41: .toText(false).c_str()) Chris@48: .arg(sampleRate) Chris@48: .arg((m_manager && Chris@48: sampleRate != m_manager->getPlaybackSampleRate()) ? " (R)" : ""); Chris@41: Chris@41: paint.drawText(width() - paint.fontMetrics().width(desc) - 5, Chris@41: height() - paint.fontMetrics().height() + Chris@41: paint.fontMetrics().ascent() - 6, Chris@41: desc); Chris@41: } 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@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@17: if (layer) { 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@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@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@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@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@13: if (layer) { 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@0: 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@36: if (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@43: layer->moveSelection(m_editingSelection, f0); Chris@43: Chris@43: } else { Chris@43: 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@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@13: 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@8: QString("type=\"pane\" centreLineVisible=\"%1\" %2") Chris@8: .arg(m_centreLineVisible).arg(extraAttributes)); Chris@8: } Chris@8: Chris@0: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "Pane.moc.cpp" Chris@0: #endif Chris@0: