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@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@0: m_centreLineVisible(true) Chris@0: { Chris@0: setObjectName("Pane"); Chris@0: setMouseTracking(true); Chris@0: } Chris@0: Chris@0: bool Chris@0: Pane::shouldIlluminateLocalFeatures(const Layer *layer, QPoint &pos) Chris@0: { Chris@0: for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) { Chris@0: --vi; Chris@0: if (layer != *vi) return false; Chris@0: pos = m_identifyPoint; Chris@0: return m_identifyFeatures; Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: 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@0: Chris@0: for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) { Chris@0: --vi; Chris@0: Chris@0: int sw = (*vi)->getVerticalScaleWidth(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@0: (*vi)->paintVerticalScale(paint, QRect(0, 0, sw, height())); Chris@0: Chris@0: paint.restore(); Chris@0: } Chris@0: Chris@0: if (m_identifyFeatures) { Chris@0: QRect descRect = (*vi)->getFeatureDescriptionRect(paint, Chris@0: m_identifyPoint); Chris@0: if (descRect.width() > 0 && descRect.height() > 0 && Chris@0: r.left() + r.width() >= width() - descRect.width() && Chris@0: r.top() < descRect.height()) { Chris@0: Chris@0: // Profiler profiler("Pane::paintEvent - painting local feature description", true); Chris@0: Chris@0: // std::cerr << "Pane::paintEvent: calling paint.save() in feature description block" << std::endl; Chris@0: paint.save(); Chris@0: Chris@0: paint.setPen(Qt::black); Chris@0: paint.setBrush(Qt::white); Chris@0: Chris@0: QRect rect(width() - descRect.width() - 1, 0, Chris@0: descRect.width(), descRect.height()); Chris@0: Chris@0: paint.drawRect(rect); Chris@0: Chris@0: paint.setBrush(Qt::NoBrush); Chris@0: (*vi)->paintLocalFeatureDescription(paint, rect, m_identifyPoint); Chris@0: Chris@0: paint.restore(); Chris@0: } 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@0: } Chris@0: Chris@0: if (m_clickedInRange && m_shiftPressed) { Chris@13: //!!! be nice if this looked a bit more in keeping with the Chris@13: //selection block Chris@0: paint.setPen(Qt::blue); Chris@0: paint.drawRect(m_clickPos.x(), m_clickPos.y(), Chris@0: m_mousePos.x() - m_clickPos.x(), Chris@0: m_mousePos.y() - m_clickPos.y()); Chris@0: } Chris@0: Chris@0: paint.end(); Chris@0: } Chris@0: Chris@0: void Chris@0: Pane::mousePressEvent(QMouseEvent *e) Chris@0: { Chris@0: m_clickPos = e->pos(); Chris@0: m_clickedInRange = true; 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@13: if (mode == ViewManager::NavigateMode) { Chris@13: Chris@13: m_dragCentreFrame = m_centreFrame; Chris@13: Chris@13: } else if (mode == ViewManager::SelectMode) { Chris@13: Chris@13: int mouseFrame = e->x() * m_zoomLevel + getStartFrame(); Chris@13: size_t resolution = 1; Chris@13: int snapFrame = mouseFrame; Chris@13: Chris@13: Layer *layer = getSelectedLayer(); Chris@13: if (layer) { Chris@13: snapFrame = layer->getNearestFeatureFrame(mouseFrame, resolution, Chris@13: false); Chris@13: } Chris@13: Chris@13: if (snapFrame < 0) snapFrame = 0; Chris@13: m_selectionStartFrame = snapFrame; Chris@13: if (m_manager) { Chris@13: m_manager->setInProgressSelection(Selection(snapFrame, Chris@13: snapFrame + resolution), Chris@13: !m_ctrlPressed); Chris@13: } Chris@13: update(); 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@13: if (mode == ViewManager::NavigateMode) { 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@13: long newStartFrame = getStartFrame() + m_zoomLevel * x0; Chris@13: Chris@13: if (newStartFrame <= -long(width() * m_zoomLevel)) { Chris@13: newStartFrame = -long(width() * m_zoomLevel) + 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@13: } 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@0: if (!m_clickedInRange) { Chris@0: Chris@0: // std::cerr << "Pane: calling identifyLocalFeatures" << std::endl; Chris@0: Chris@0: //!!! identifyLocalFeatures(true, e->x(), e->y()); Chris@0: Chris@0: bool previouslyIdentifying = m_identifyFeatures; Chris@0: QPoint prevPoint = m_identifyPoint; Chris@0: Chris@0: m_identifyFeatures = true; Chris@0: m_identifyPoint = e->pos(); Chris@0: Chris@0: if (m_identifyFeatures != previouslyIdentifying || Chris@0: m_identifyPoint != prevPoint) { Chris@0: update(); Chris@0: } Chris@0: Chris@13: return; Chris@13: Chris@13: } Chris@0: Chris@13: if (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@13: long xoff = int(e->x()) - int(m_clickPos.x()); Chris@13: long frameOff = xoff * m_zoomLevel; Chris@13: 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@13: Chris@13: if (std::max(m_centreFrame, newCentreFrame) - Chris@13: std::min(m_centreFrame, newCentreFrame) > size_t(m_zoomLevel)) { Chris@13: setCentreFrame(newCentreFrame); Chris@13: } Chris@0: } Chris@0: Chris@13: } else if (mode == ViewManager::SelectMode) { Chris@13: Chris@13: int mouseFrame = e->x() * m_zoomLevel + getStartFrame(); 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@13: snapFrameLeft = layer->getNearestFeatureFrame(mouseFrame, resolution, Chris@13: false); Chris@13: snapFrameRight = layer->getNearestFeatureFrame(mouseFrame, resolution, Chris@13: true); Chris@13: } Chris@13: 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@13: !m_ctrlPressed); Chris@0: } Chris@13: Chris@13: if (!m_manager || !m_manager->isPlaying()) { Chris@13: int offset = mouseFrame - getStartFrame(); Chris@13: int available = getEndFrame() - getStartFrame(); Chris@13: if (offset >= available * 0.9) { Chris@14: int move = int(offset - available * 0.9) + 1; Chris@14: setCentreFrame(m_centreFrame + move); Chris@13: } else if (offset <= available * 0.15) { Chris@14: int move = int(available * 0.15 - 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@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: Pane::mouseDoubleClickEvent(QMouseEvent *e) Chris@0: { Chris@0: std::cerr << "mouseDoubleClickEvent" << std::endl; 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 newZoomLevel = m_zoomLevel; 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@0: Chris@0: while (count > 0) { Chris@0: if (newZoomLevel <= 2) { Chris@0: newZoomLevel = 1; Chris@0: break; Chris@0: } Chris@0: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel - 1, Chris@0: ZoomConstraint::RoundDown); Chris@0: --count; Chris@0: } Chris@0: Chris@0: while (count < 0) { Chris@0: newZoomLevel = getZoomConstraintBlockSize(newZoomLevel + 1, Chris@0: ZoomConstraint::RoundUp); Chris@0: ++count; Chris@0: } Chris@0: Chris@0: if (newZoomLevel != m_zoomLevel) { Chris@0: setZoomLevel(newZoomLevel); Chris@0: } Chris@0: Chris@0: emit paneInteractedWith(); Chris@0: } Chris@8: 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@13: setCursor(Qt::SizeAllCursor); Chris@13: break; Chris@13: Chris@13: case ViewManager::DrawMode: Chris@13: setCursor(Qt::CrossCursor); Chris@13: break; Chris@13: Chris@13: case ViewManager::TextMode: Chris@13: setCursor(Qt::IBeamCursor); Chris@13: break; 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: