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@0: Chris Cannam, Queen Mary University of London, 2005 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@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@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@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@0: m_dragCentreFrame = m_centreFrame; Chris@0: Chris@0: emit paneInteractedWith(); Chris@0: } Chris@0: Chris@0: void Chris@0: Pane::mouseReleaseEvent(QMouseEvent *e) Chris@0: { Chris@0: if (m_clickedInRange) { Chris@0: mouseMoveEvent(e); Chris@0: } Chris@0: if (m_shiftPressed) { Chris@0: Chris@0: int x0 = std::min(m_clickPos.x(), m_mousePos.x()); Chris@0: int x1 = std::max(m_clickPos.x(), m_mousePos.x()); Chris@0: int w = x1 - x0; Chris@0: Chris@0: long newStartFrame = getStartFrame() + m_zoomLevel * x0; Chris@0: Chris@0: if (newStartFrame <= -long(width() * m_zoomLevel)) { Chris@0: newStartFrame = -long(width() * m_zoomLevel) + 1; Chris@0: } Chris@0: Chris@0: if (newStartFrame >= long(getModelsEndFrame())) { Chris@0: newStartFrame = getModelsEndFrame() - 1; Chris@0: } Chris@0: Chris@0: float ratio = float(w) / float(width()); Chris@0: // std::cerr << "ratio: " << ratio << std::endl; Chris@0: size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio); Chris@0: if (newZoomLevel < 1) newZoomLevel = 1; Chris@0: Chris@0: // std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl; Chris@0: setZoomLevel(getZoomConstraintBlockSize(newZoomLevel)); Chris@0: setStartFrame(newStartFrame); Chris@0: Chris@0: //cerr << "mouseReleaseEvent: start frame now " << m_startFrame << endl; Chris@0: // update(); 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@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@0: } else if (m_shiftPressed) { Chris@0: Chris@0: m_mousePos = e->pos(); Chris@0: update(); Chris@0: Chris@0: } else { Chris@0: Chris@0: long xoff = int(e->x()) - int(m_clickPos.x()); Chris@0: long frameOff = xoff * m_zoomLevel; Chris@0: Chris@0: size_t newCentreFrame = m_dragCentreFrame; Chris@0: Chris@0: if (frameOff < 0) { Chris@0: newCentreFrame -= frameOff; Chris@0: } else if (newCentreFrame >= size_t(frameOff)) { Chris@0: newCentreFrame -= frameOff; Chris@0: } else { Chris@0: newCentreFrame = 0; Chris@0: } Chris@0: Chris@0: if (newCentreFrame >= getModelsEndFrame()) { Chris@0: newCentreFrame = getModelsEndFrame(); Chris@0: if (newCentreFrame > 0) --newCentreFrame; Chris@0: } Chris@0: Chris@0: if (std::max(m_centreFrame, newCentreFrame) - Chris@0: std::min(m_centreFrame, newCentreFrame) > size_t(m_zoomLevel)) { Chris@0: setCentreFrame(newCentreFrame); Chris@0: } 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@0: Chris@0: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "Pane.moc.cpp" Chris@0: #endif Chris@0: