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@264:     This file copyright 2006-2007 Chris Cannam and QMUL.
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@294: #include "base/TextAbbrev.h"
Chris@338: #include "base/Preferences.h"
Chris@127: #include "layer/WaveformLayer.h"
Chris@127: 
Chris@326: //!!! ugh
Chris@326: #include "data/model/WaveFileModel.h"
Chris@326: 
Chris@127: #include <QPaintEvent>
Chris@127: #include <QPainter>
Chris@257: #include <QBitmap>
Chris@312: #include <QDragEnterEvent>
Chris@312: #include <QDropEvent>
Chris@257: #include <QCursor>
Chris@316: #include <QTextStream>
Chris@316: 
Chris@127: #include <iostream>
Chris@127: #include <cmath>
Chris@127: 
Chris@133: //!!! for HUD -- pull out into a separate class
Chris@133: #include <QFrame>
Chris@133: #include <QGridLayout>
Chris@133: #include <QPushButton>
Chris@133: #include "widgets/Thumbwheel.h"
Chris@172: #include "widgets/Panner.h"
Chris@188: #include "widgets/RangeInputDialog.h"
Chris@189: #include "widgets/NotifyingPushButton.h"
Chris@133: 
Chris@282: #include "widgets/KeyReference.h" //!!! should probably split KeyReference into a data class in base and another that shows the widget
Chris@282: 
Chris@127: using std::cerr;
Chris@127: using std::endl;
Chris@127: 
Chris@267: QCursor *Pane::m_measureCursor1 = 0;
Chris@267: QCursor *Pane::m_measureCursor2 = 0;
Chris@262: 
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@133:     m_centreLineVisible(true),
Chris@222:     m_scaleWidth(0),
Chris@237:     m_headsUpDisplay(0),
Chris@237:     m_vpan(0),
Chris@237:     m_hthumb(0),
Chris@237:     m_vthumb(0),
Chris@290:     m_reset(0),
Chris@290:     m_mouseInWidget(false)
Chris@127: {
Chris@127:     setObjectName("Pane");
Chris@127:     setMouseTracking(true);
Chris@312:     setAcceptDrops(true);
Chris@133:     
Chris@133:     updateHeadsUpDisplay();
Chris@133: }
Chris@133: 
Chris@133: void
Chris@133: Pane::updateHeadsUpDisplay()
Chris@133: {
Chris@187:     Profiler profiler("Pane::updateHeadsUpDisplay", true);
Chris@187: 
Chris@192:     if (!isVisible()) return;
Chris@192: 
Chris@132: /*
Chris@132:     int count = 0;
Chris@132:     int currentLevel = 1;
Chris@132:     int level = 1;
Chris@132:     while (true) {
Chris@132:         if (getZoomLevel() == level) currentLevel = count;
Chris@132:         int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@132:                                                   ZoomConstraint::RoundUp);
Chris@132:         if (newLevel == level) break;
Chris@132:         if (newLevel == 131072) break; //!!! just because
Chris@132:         level = newLevel;
Chris@132:         ++count;
Chris@132:     }
Chris@132: 
Chris@132:     std::cerr << "Have " << count+1 << " zoom levels" << std::endl;
Chris@132: */
Chris@133: 
Chris@188:     Layer *layer = 0;
Chris@188:     if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@188: 
Chris@133:     if (!m_headsUpDisplay) {
Chris@133: 
Chris@133:         m_headsUpDisplay = new QFrame(this);
Chris@133: 
Chris@133:         QGridLayout *layout = new QGridLayout;
Chris@133:         layout->setMargin(0);
Chris@133:         layout->setSpacing(0);
Chris@133:         m_headsUpDisplay->setLayout(layout);
Chris@133:         
Chris@133:         m_hthumb = new Thumbwheel(Qt::Horizontal);
Chris@187:         m_hthumb->setObjectName(tr("Horizontal Zoom"));
Chris@260:         m_hthumb->setCursor(Qt::ArrowCursor);
Chris@173:         layout->addWidget(m_hthumb, 1, 0, 1, 2);
Chris@133:         m_hthumb->setFixedWidth(70);
Chris@133:         m_hthumb->setFixedHeight(16);
Chris@133:         m_hthumb->setDefaultValue(0);
Chris@165:         m_hthumb->setSpeed(0.6);
Chris@133:         connect(m_hthumb, SIGNAL(valueChanged(int)), this, 
Chris@133:                 SLOT(horizontalThumbwheelMoved(int)));
Chris@189:         connect(m_hthumb, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189:         connect(m_hthumb, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@172: 
Chris@172:         m_vpan = new Panner;
Chris@260:         m_vpan->setCursor(Qt::ArrowCursor);
Chris@172:         layout->addWidget(m_vpan, 0, 1);
Chris@173:         m_vpan->setFixedWidth(12);
Chris@172:         m_vpan->setFixedHeight(70);
Chris@174:         m_vpan->setAlpha(80, 130);
Chris@174:         connect(m_vpan, SIGNAL(rectExtentsChanged(float, float, float, float)),
Chris@174:                 this, SLOT(verticalPannerMoved(float, float, float, float)));
Chris@188:         connect(m_vpan, SIGNAL(doubleClicked()),
Chris@188:                 this, SLOT(editVerticalPannerExtents()));
Chris@189:         connect(m_vpan, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189:         connect(m_vpan, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@172: 
Chris@133:         m_vthumb = new Thumbwheel(Qt::Vertical);
Chris@187:         m_vthumb->setObjectName(tr("Vertical Zoom"));
Chris@260:         m_vthumb->setCursor(Qt::ArrowCursor);
Chris@172:         layout->addWidget(m_vthumb, 0, 2);
Chris@133:         m_vthumb->setFixedWidth(16);
Chris@133:         m_vthumb->setFixedHeight(70);
Chris@133:         connect(m_vthumb, SIGNAL(valueChanged(int)), this, 
Chris@133:                 SLOT(verticalThumbwheelMoved(int)));
Chris@189:         connect(m_vthumb, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189:         connect(m_vthumb, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@133: 
Chris@188:         if (layer) {
Chris@188:             RangeMapper *rm = layer->getNewVerticalZoomRangeMapper();
Chris@188:             if (rm) m_vthumb->setRangeMapper(rm);
Chris@188:         }
Chris@188: 
Chris@189:         m_reset = new NotifyingPushButton;
Chris@260:         m_reset->setCursor(Qt::ArrowCursor);
Chris@189:         m_reset->setFixedHeight(16);
Chris@189:         m_reset->setFixedWidth(16);
Chris@189:         layout->addWidget(m_reset, 1, 2);
Chris@189:         connect(m_reset, SIGNAL(clicked()), m_hthumb, SLOT(resetToDefault()));
Chris@189:         connect(m_reset, SIGNAL(clicked()), m_vthumb, SLOT(resetToDefault()));
Chris@189:         connect(m_reset, SIGNAL(clicked()), m_vpan, SLOT(resetToDefault()));
Chris@189:         connect(m_reset, SIGNAL(mouseEntered()), this, SLOT(mouseEnteredWidget()));
Chris@189:         connect(m_reset, SIGNAL(mouseLeft()), this, SLOT(mouseLeftWidget()));
Chris@133:     }
Chris@133: 
Chris@133:     int count = 0;
Chris@133:     int current = 0;
Chris@133:     int level = 1;
Chris@133: 
Chris@137:     //!!! pull out into function (presumably in View)
Chris@137:     bool haveConstraint = false;
Chris@137:     for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end();
Chris@137:          ++i) {
Chris@137:         if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) {
Chris@137:             haveConstraint = true;
Chris@137:             break;
Chris@137:         }
Chris@137:     }
Chris@137: 
Chris@137:     if (haveConstraint) {
Chris@137:         while (true) {
Chris@137:             if (getZoomLevel() == level) current = count;
Chris@137:             int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@137:                                                       ZoomConstraint::RoundUp);
Chris@137:             if (newLevel == level) break;
Chris@137:             level = newLevel;
Chris@137:             if (++count == 50) break;
Chris@137:         }
Chris@137:     } else {
Chris@137:         // if we have no particular constraints, we can really spread out
Chris@137:         while (true) {
Chris@137:             if (getZoomLevel() >= level) current = count;
Chris@137:             int step = level / 10;
Chris@137:             int pwr = 0;
Chris@137:             while (step > 0) {
Chris@137:                 ++pwr;
Chris@137:                 step /= 2;
Chris@137:             }
Chris@137:             step = 1;
Chris@137:             while (pwr > 0) {
Chris@137:                 step *= 2;
Chris@137:                 --pwr;
Chris@137:             }
Chris@154: //            std::cerr << level << std::endl;
Chris@137:             level += step;
Chris@137:             if (++count == 100 || level > 262144) break;
Chris@137:         }
Chris@133:     }
Chris@133: 
Chris@133: //    std::cerr << "Have " << count << " zoom levels" << std::endl;
Chris@133: 
Chris@133:     m_hthumb->setMinimumValue(0);
Chris@133:     m_hthumb->setMaximumValue(count);
Chris@133:     m_hthumb->setValue(count - current);
Chris@133: 
Chris@133: //    std::cerr << "set value to " << count-current << std::endl;
Chris@133: 
Chris@133: //    std::cerr << "default value is " << m_hthumb->getDefaultValue() << std::endl;
Chris@133: 
Chris@133:     if (count != 50 && m_hthumb->getDefaultValue() == 0) {
Chris@133:         m_hthumb->setDefaultValue(count - current);
Chris@133: //        std::cerr << "set default value to " << m_hthumb->getDefaultValue() << std::endl;
Chris@133:     }
Chris@133: 
Chris@204:     bool haveVThumb = false;
Chris@204: 
Chris@133:     if (layer) {
Chris@133:         int defaultStep = 0;
Chris@133:         int max = layer->getVerticalZoomSteps(defaultStep);
Chris@133:         if (max == 0) {
Chris@133:             m_vthumb->hide();
Chris@133:         } else {
Chris@204:             haveVThumb = true;
Chris@133:             m_vthumb->show();
Chris@187:             m_vthumb->blockSignals(true);
Chris@133:             m_vthumb->setMinimumValue(0);
Chris@133:             m_vthumb->setMaximumValue(max);
Chris@133:             m_vthumb->setDefaultValue(defaultStep);
Chris@133:             m_vthumb->setValue(layer->getCurrentVerticalZoomStep());
Chris@187:             m_vthumb->blockSignals(false);
Chris@135: 
Chris@205: //            std::cerr << "Vertical thumbwheel: min 0, max " << max
Chris@205: //                      << ", default " << defaultStep << ", value "
Chris@205: //                      << m_vthumb->getValue() << std::endl;
Chris@135: 
Chris@133:         }
Chris@133:     }
Chris@133: 
Chris@174:     updateVerticalPanner();
Chris@174: 
Chris@133:     if (m_manager && m_manager->getZoomWheelsEnabled() &&
Chris@133:         width() > 120 && height() > 100) {
Chris@165:         if (!m_headsUpDisplay->isVisible()) {
Chris@165:             m_headsUpDisplay->show();
Chris@165:         }
Chris@204:         if (haveVThumb) {
Chris@204:             m_headsUpDisplay->setFixedHeight(m_vthumb->height() + m_hthumb->height());
Chris@133:             m_headsUpDisplay->move(width() - 86, height() - 86);
Chris@133:         } else {
Chris@204:             m_headsUpDisplay->setFixedHeight(m_hthumb->height());
Chris@204:             m_headsUpDisplay->move(width() - 86, height() - 16);
Chris@133:         }
Chris@133:     } else {
Chris@133:         m_headsUpDisplay->hide();
Chris@133:     }
Chris@127: }
Chris@127: 
Chris@174: void
Chris@174: Pane::updateVerticalPanner()
Chris@174: {
Chris@174:     if (!m_vpan || !m_manager || !m_manager->getZoomWheelsEnabled()) return;
Chris@174: 
Chris@208:     // In principle we should show or hide the panner on the basis of
Chris@208:     // whether the top layer has adjustable display extents, and we do
Chris@208:     // that below.  However, we have no basis for layout of the panner
Chris@208:     // if the vertical scroll wheel is not also present.  So if we
Chris@208:     // have no vertical scroll wheel, we should remove the panner as
Chris@208:     // well.  Ideally any layer that implements display extents should
Chris@208:     // implement vertical zoom steps as well, but they don't all at
Chris@208:     // the moment.
Chris@208: 
Chris@208:     Layer *layer = 0;
Chris@208:     if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@208:     int discard;
Chris@208:     if (layer && layer->getVerticalZoomSteps(discard) == 0) {
Chris@208:         m_vpan->hide();
Chris@208:         return;
Chris@208:     }
Chris@208: 
Chris@174:     float vmin, vmax, dmin, dmax;
Chris@174:     if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax) && vmax != vmin) {
Chris@174:         float y0 = (dmin - vmin) / (vmax - vmin);
Chris@174:         float y1 = (dmax - vmin) / (vmax - vmin);
Chris@174:         m_vpan->blockSignals(true);
Chris@174:         m_vpan->setRectExtents(0, 1.0 - y1, 1, y1 - y0);
Chris@174:         m_vpan->blockSignals(false);
Chris@174:         m_vpan->show();
Chris@174:     } else {
Chris@174:         m_vpan->hide();
Chris@174:     }
Chris@174: }
Chris@174: 
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@262:     if (m_manager && m_manager->getToolMode() == ViewManager::MeasureMode) {
Chris@262:         return false;
Chris@262:     }
Chris@262: 
Chris@326:     if (m_manager && !m_manager->shouldIlluminateLocalFeatures()) {
Chris@326:         return false;
Chris@326:     }
Chris@326: 
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@261:     if (e) r = e->rect();
Chris@127: 
Chris@127:     View::paintEvent(e);
Chris@127: 
Chris@127:     paint.begin(this);
Chris@339:     setPaintFont(paint);
Chris@338: 
Chris@261:     if (e) paint.setClipRect(r);
Chris@127: 
Chris@259:     ViewManager::ToolMode toolMode = m_manager->getToolMode();
Chris@259: 
Chris@127:     if (m_manager &&
Chris@290: //        !m_manager->isPlaying() &&
Chris@290:         m_mouseInWidget &&
Chris@259:         toolMode == ViewManager::MeasureMode) {
Chris@127: 
Chris@127:         for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@127:             --vi;
Chris@127: 
Chris@127:             std::vector<QRect> 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@268:     Layer *topLayer = getTopLayer();
Chris@277:     bool haveSomeTimeXAxis = false;
Chris@268: 
Chris@258:     const Model *waveformModel = 0; // just for reporting purposes
Chris@326:     const Model *workModel = 0;
Chris@326: 
Chris@127:     for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@127:         --vi;
Chris@277:         if (!haveSomeTimeXAxis && (*vi)->hasTimeXAxis()) {
Chris@277:             haveSomeTimeXAxis = true;
Chris@277:         }
Chris@127:         if (dynamic_cast<WaveformLayer *>(*vi)) {
Chris@127:             waveformModel = (*vi)->getModel();
Chris@326:             workModel = waveformModel;
Chris@326:         } else {
Chris@326:             Model *m = (*vi)->getModel();
Chris@326:             if (dynamic_cast<WaveFileModel *>(m)) {
Chris@326:                 workModel = m;
Chris@326:             } else if (m && dynamic_cast<WaveFileModel *>(m->getSourceModel())) {
Chris@326:                 workModel = m->getSourceModel();
Chris@326:             }
Chris@127:         }
Chris@326:                 
Chris@326:         if (waveformModel && workModel && haveSomeTimeXAxis) break;
Chris@258:     }
Chris@127: 
Chris@261:     m_scaleWidth = 0;
Chris@261: 
Chris@261:     if (m_manager && m_manager->shouldShowVerticalScale() && topLayer) {
Chris@261:         drawVerticalScale(r, topLayer, paint);
Chris@261:     }
Chris@261: 
Chris@326:     if (m_identifyFeatures &&
Chris@326:         m_manager && m_manager->shouldIlluminateLocalFeatures() &&
Chris@326:         topLayer) {
Chris@261:         drawFeatureDescription(topLayer, paint);
Chris@261:     }
Chris@261:     
Chris@261:     int sampleRate = getModelsSampleRate();
Chris@261:     paint.setBrush(Qt::NoBrush);
Chris@261: 
Chris@261:     if (m_centreLineVisible &&
Chris@261:         m_manager &&
Chris@261:         m_manager->shouldShowCentreLine()) {
Chris@277:         drawCentreLine(sampleRate, paint, !haveSomeTimeXAxis);
Chris@261:     }
Chris@261:     
Chris@261:     paint.setPen(QColor(50, 50, 50));
Chris@261: 
Chris@261:     if (waveformModel &&
Chris@261:         m_manager &&
Chris@261:         m_manager->shouldShowDuration()) {
Chris@261:         drawDurationAndRate(r, waveformModel, sampleRate, paint);
Chris@261:     }
Chris@261: 
Chris@326:     bool haveWorkTitle = false;
Chris@326: 
Chris@326:     if (workModel &&
Chris@326:         m_manager &&
Chris@326:         m_manager->shouldShowWorkTitle()) {
Chris@326:         drawWorkTitle(r, paint, workModel);
Chris@326:         haveWorkTitle = true;
Chris@326:     }
Chris@326: 
Chris@326:     if (workModel &&
Chris@320:         m_manager &&
Chris@320:         m_manager->getAlignMode()) {
Chris@326:         drawAlignmentStatus(r, paint, workModel, haveWorkTitle);
Chris@320:     }
Chris@320: 
Chris@261:     if (m_manager &&
Chris@261:         m_manager->shouldShowLayerNames()) {
Chris@261:         drawLayerNames(r, paint);
Chris@261:     }
Chris@261: 
Chris@262:     if (m_shiftPressed && m_clickedInRange &&
Chris@283:         (toolMode == ViewManager::NavigateMode || m_navigating)) {
Chris@261: 
Chris@261:         //!!! be nice if this looked a bit more in keeping with the
Chris@261:         //selection block
Chris@262:         
Chris@262:         paint.setPen(Qt::blue);
Chris@262:         //!!! shouldn't use clickPos -- needs to use a clicked frame
Chris@262:         paint.drawRect(m_clickPos.x(), m_clickPos.y(),
Chris@262:                        m_mousePos.x() - m_clickPos.x(),
Chris@262:                        m_mousePos.y() - m_clickPos.y());
Chris@261: 
Chris@262:     }
Chris@261: 
Chris@266:     if (toolMode == ViewManager::MeasureMode && topLayer) {
Chris@272:         bool showFocus = false;
Chris@272:         if (!m_manager || !m_manager->isPlaying()) showFocus = true;
Chris@272:         topLayer->paintMeasurementRects(this, paint, showFocus, m_identifyPoint);
Chris@261:     }
Chris@261:     
Chris@261:     if (selectionIsBeingEdited()) {
Chris@261:         drawEditingSelection(paint);
Chris@261:     }
Chris@261: 
Chris@261:     paint.end();
Chris@261: }
Chris@261: 
Chris@276: size_t
Chris@276: Pane::getVerticalScaleWidth() const
Chris@276: {
Chris@276:     if (m_scaleWidth > 0) return m_scaleWidth;
Chris@276:     else return 0;
Chris@276: }
Chris@276: 
Chris@261: void
Chris@261: Pane::drawVerticalScale(QRect r, Layer *topLayer, QPainter &paint)
Chris@261: {
Chris@258:     Layer *scaleLayer = 0;
Chris@258: 
Chris@261:     float min, max;
Chris@261:     bool log;
Chris@261:     QString unit;
Chris@258: 
Chris@261:     // If the top layer has no scale and reports no display
Chris@261:     // extents, but does report a unit, then the scale should be
Chris@261:     // drawn from any underlying layer with a scale and that unit.
Chris@261:     // If the top layer has no scale and no value extents at all,
Chris@261:     // then the scale should be drawn from any underlying layer
Chris@261:     // with a scale regardless of unit.
Chris@258: 
Chris@261:     int sw = topLayer->getVerticalScaleWidth(this, paint);
Chris@258: 
Chris@261:     if (sw > 0) {
Chris@261:         scaleLayer = topLayer;
Chris@261:         m_scaleWidth = sw;
Chris@258: 
Chris@261:     } else {
Chris@258: 
Chris@261:         bool hasDisplayExtents = topLayer->getDisplayExtents(min, max);
Chris@261:         bool hasValueExtents = topLayer->getValueExtents(min, max, log, unit);
Chris@261:             
Chris@261:         if (!hasDisplayExtents) {
Chris@258: 
Chris@261:             if (!hasValueExtents) {
Chris@258: 
Chris@261:                 for (LayerList::iterator vi = m_layers.end();
Chris@261:                      vi != m_layers.begin(); ) {
Chris@261:                         
Chris@261:                     --vi;
Chris@261:                         
Chris@261:                     if ((*vi) == topLayer) continue;
Chris@261:                         
Chris@261:                     sw = (*vi)->getVerticalScaleWidth(this, paint);
Chris@261:                         
Chris@261:                     if (sw > 0) {
Chris@261:                         scaleLayer = *vi;
Chris@261:                         m_scaleWidth = sw;
Chris@261:                         break;
Chris@261:                     }
Chris@261:                 }
Chris@261:             } else if (unit != "") { // && hasValueExtents && !hasDisplayExtents
Chris@258: 
Chris@261:                 QString requireUnit = unit;
Chris@261: 
Chris@261:                 for (LayerList::iterator vi = m_layers.end();
Chris@261:                      vi != m_layers.begin(); ) {
Chris@258:                         
Chris@261:                     --vi;
Chris@258:                         
Chris@261:                     if ((*vi) == topLayer) continue;
Chris@258:                         
Chris@261:                     if ((*vi)->getDisplayExtents(min, max)) {
Chris@261:                             
Chris@261:                         // search no further than this: if the
Chris@261:                         // scale from this layer isn't suitable,
Chris@261:                         // we'll have to draw no scale (else we'd
Chris@261:                         // risk ending up with the wrong scale)
Chris@261:                             
Chris@261:                         if ((*vi)->getValueExtents(min, max, log, unit) &&
Chris@261:                             unit == requireUnit) {
Chris@261: 
Chris@261:                             sw = (*vi)->getVerticalScaleWidth(this, paint);
Chris@261:                             if (sw > 0) {
Chris@261:                                 scaleLayer = *vi;
Chris@261:                                 m_scaleWidth = sw;
Chris@261:                             }
Chris@258:                         }
Chris@261:                         break;
Chris@258:                     }
Chris@258:                 }
Chris@258:             }
Chris@127:         }
Chris@258:     }
Chris@127: 
Chris@258:     if (!scaleLayer) m_scaleWidth = 0;
Chris@258:         
Chris@258:     if (m_scaleWidth > 0 && r.left() < m_scaleWidth) {
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@258:         paint.save();
Chris@258:             
Chris@287:         paint.setPen(getForeground());
Chris@287:         paint.setBrush(getBackground());
Chris@258:         paint.drawRect(0, -1, m_scaleWidth, height()+1);
Chris@258:         
Chris@258:         paint.setBrush(Qt::NoBrush);
Chris@258:         scaleLayer->paintVerticalScale
Chris@258:             (this, paint, QRect(0, 0, m_scaleWidth, height()));
Chris@258:         
Chris@258:         paint.restore();
Chris@258:     }
Chris@261: }
Chris@261:             
Chris@261: void
Chris@261: Pane::drawFeatureDescription(Layer *topLayer, QPainter &paint)
Chris@261: {
Chris@261:     QPoint pos = m_identifyPoint;
Chris@261:     QString desc = topLayer->getFeatureDescription(this, pos);
Chris@261: 	    
Chris@261:     if (desc != "") {
Chris@261:         
Chris@261:         paint.save();
Chris@261:         
Chris@261:         int tabStop =
Chris@261:             paint.fontMetrics().width(tr("Some lengthy prefix:"));
Chris@261:         
Chris@261:         QRect boundingRect = 
Chris@261:             paint.fontMetrics().boundingRect
Chris@261:             (rect(),
Chris@261:              Qt::AlignRight | Qt::AlignTop | Qt::TextExpandTabs,
Chris@261:              desc, tabStop);
Chris@261:         
Chris@261:         if (hasLightBackground()) {
Chris@261:             paint.setPen(Qt::NoPen);
Chris@261:             paint.setBrush(QColor(250, 250, 250, 200));
Chris@261:         } else {
Chris@261:             paint.setPen(Qt::NoPen);
Chris@261:             paint.setBrush(QColor(50, 50, 50, 200));
Chris@261:         }
Chris@261:         
Chris@261:         int extra = paint.fontMetrics().descent();
Chris@261:         paint.drawRect(width() - boundingRect.width() - 10 - extra,
Chris@261:                        10 - extra,
Chris@261:                        boundingRect.width() + 2 * extra,
Chris@261:                        boundingRect.height() + extra);
Chris@261:         
Chris@261:         if (hasLightBackground()) {
Chris@261:             paint.setPen(QColor(150, 20, 0));
Chris@261:         } else {
Chris@261:             paint.setPen(QColor(255, 150, 100));
Chris@261:         }
Chris@261:         
Chris@261:         QTextOption option;
Chris@261:         option.setWrapMode(QTextOption::NoWrap);
Chris@261:         option.setAlignment(Qt::AlignRight | Qt::AlignTop);
Chris@261:         option.setTabStop(tabStop);
Chris@261:         paint.drawText(QRectF(width() - boundingRect.width() - 10, 10,
Chris@261:                               boundingRect.width(),
Chris@261:                               boundingRect.height()),
Chris@261:                        desc,
Chris@261:                        option);
Chris@261:         
Chris@261:         paint.restore();
Chris@261:     }
Chris@261: }
Chris@258: 
Chris@261: void
Chris@277: Pane::drawCentreLine(int sampleRate, QPainter &paint, bool omitLine)
Chris@261: {
Chris@261:     int fontHeight = paint.fontMetrics().height();
Chris@261:     int fontAscent = paint.fontMetrics().ascent();
Chris@261: 
Chris@261:     QColor c = QColor(0, 0, 0);
Chris@261:     if (!hasLightBackground()) {
Chris@261:         c = QColor(240, 240, 240);
Chris@261:     }
Chris@277: 
Chris@261:     paint.setPen(c);
Chris@274:     int x = width() / 2;
Chris@277: 
Chris@277:     if (!omitLine) {
Chris@277:         paint.drawLine(x, 0, x, height() - 1);
Chris@277:         paint.drawLine(x-1, 1, x+1, 1);
Chris@277:         paint.drawLine(x-2, 0, x+2, 0);
Chris@277:         paint.drawLine(x-1, height() - 2, x+1, height() - 2);
Chris@277:         paint.drawLine(x-2, height() - 1, x+2, height() - 1);
Chris@277:     }
Chris@261:     
Chris@261:     paint.setPen(QColor(50, 50, 50));
Chris@261:     
Chris@261:     int y = height() - fontHeight + fontAscent - 6;
Chris@261:     
Chris@261:     LayerList::iterator vi = m_layers.end();
Chris@261:     
Chris@261:     if (vi != m_layers.begin()) {
Chris@261: 	    
Chris@261:         switch ((*--vi)->getPreferredFrameCountPosition()) {
Chris@258:             
Chris@261:         case Layer::PositionTop:
Chris@261:             y = fontAscent + 6;
Chris@261:             break;
Chris@258:             
Chris@261:         case Layer::PositionMiddle:
Chris@261:             y = (height() - fontHeight) / 2
Chris@261:                 + fontAscent;
Chris@261:             break;
Chris@127:             
Chris@261:         case Layer::PositionBottom:
Chris@261:             // y already set correctly
Chris@261:             break;
Chris@127:         }
Chris@127:     }
Chris@127:     
Chris@261:     if (m_manager && m_manager->shouldShowFrameCount()) {
Chris@261:         
Chris@261:         if (sampleRate) {
Chris@127: 
Chris@261:             QString text(QString::fromStdString
Chris@261:                          (RealTime::frame2RealTime
Chris@261:                           (m_centreFrame, sampleRate).toText(true)));
Chris@127:             
Chris@261:             int tw = paint.fontMetrics().width(text);
Chris@261:             int x = width()/2 - 4 - tw;
Chris@127:             
Chris@127:             drawVisibleText(paint, x, y, text, OutlinedText);
Chris@127:         }
Chris@261:         
Chris@261:         QString text = QString("%1").arg(m_centreFrame);
Chris@261:         
Chris@261:         int x = width()/2 + 4;
Chris@261:         
Chris@261:         drawVisibleText(paint, x, y, text, OutlinedText);
Chris@261:     }
Chris@261: }
Chris@127: 
Chris@261: void
Chris@326: Pane::drawAlignmentStatus(QRect r, QPainter &paint, const Model *model,
Chris@326:                           bool down)
Chris@320: {
Chris@320:     const Model *reference = model->getAlignmentReference();
Chris@320: /*
Chris@320:     if (!reference) {
Chris@320:         std::cerr << "Pane[" << this << "]::drawAlignmentStatus: No reference" << std::endl;
Chris@320:     } else if (reference == model) {
Chris@320:         std::cerr << "Pane[" << this << "]::drawAlignmentStatus: This is the reference model" << std::endl;
Chris@320:     } else {
Chris@320:         std::cerr << "Pane[" << this << "]::drawAlignmentStatus: This is not the reference" << std::endl;
Chris@320:     }
Chris@320: */
Chris@320:     QString text;
Chris@320:     int completion = 100;
Chris@320: 
Chris@320:     if (reference == model) {
Chris@320:         text = tr("Reference");
Chris@320:     } else if (!reference) {
Chris@320:         text = tr("Unaligned");
Chris@320:     } else {
Chris@320:         completion = model->getAlignmentCompletion();
Chris@320:         if (completion == 0) {
Chris@320:             text = tr("Unaligned");
Chris@320:         } else if (completion < 100) {
Chris@320:             text = tr("Aligning: %1%").arg(completion);
Chris@320:         } else {
Chris@320:             text = tr("Aligned");
Chris@320:         }
Chris@320:     }
Chris@320: 
Chris@320:     paint.save();
Chris@320:     QFont font(paint.font());
Chris@320:     font.setBold(true);
Chris@320:     paint.setFont(font);
Chris@326:     if (completion < 100) paint.setBrush(Qt::red);
Chris@326: 
Chris@326:     int y = 5;
Chris@326:     if (down) y += paint.fontMetrics().height();
Chris@326:     int w = paint.fontMetrics().width(text);
Chris@326:     int h = paint.fontMetrics().height();
Chris@326:     if (r.top() > h + y || r.left() > w + m_scaleWidth + 5) {
Chris@326:         paint.restore();
Chris@326:         return;
Chris@326:     }
Chris@320:     
Chris@320:     drawVisibleText(paint, m_scaleWidth + 5,
Chris@326:                     paint.fontMetrics().ascent() + y, text, OutlinedText);
Chris@320: 
Chris@320:     paint.restore();
Chris@320: }
Chris@320: 
Chris@320: void
Chris@320: Pane::modelAlignmentCompletionChanged()
Chris@320: {
Chris@320:     View::modelAlignmentCompletionChanged();
Chris@320:     update(QRect(0, 0, 300, 100));
Chris@320: }
Chris@320: 
Chris@320: void
Chris@326: Pane::drawWorkTitle(QRect r, QPainter &paint, const Model *model)
Chris@326: {
Chris@326:     QString title = model->getTitle();
Chris@326:     QString maker = model->getMaker();
Chris@327: //std::cerr << "Pane::drawWorkTitle: title=\"" << title.toStdString()
Chris@327: //<< "\", maker=\"" << maker.toStdString() << "\"" << std::endl;
Chris@326:     if (title == "") return;
Chris@326: 
Chris@326:     QString text = title;
Chris@326:     if (maker != "") {
Chris@326:         text = tr("%1 - %2").arg(title).arg(maker);
Chris@326:     }
Chris@326:     
Chris@326:     paint.save();
Chris@326:     QFont font(paint.font());
Chris@326:     font.setItalic(true);
Chris@326:     paint.setFont(font);
Chris@326: 
Chris@326:     int y = 5;
Chris@326:     int w = paint.fontMetrics().width(text);
Chris@326:     int h = paint.fontMetrics().height();
Chris@326:     if (r.top() > h + y || r.left() > w + m_scaleWidth + 5) {
Chris@326:         paint.restore();
Chris@326:         return;
Chris@326:     }
Chris@326:     
Chris@326:     drawVisibleText(paint, m_scaleWidth + 5,
Chris@326:                     paint.fontMetrics().ascent() + y, text, OutlinedText);
Chris@326: 
Chris@326:     paint.restore();
Chris@326: }
Chris@326: 
Chris@326: void
Chris@261: Pane::drawLayerNames(QRect r, QPainter &paint)
Chris@261: {
Chris@261:     int fontHeight = paint.fontMetrics().height();
Chris@261:     int fontAscent = paint.fontMetrics().ascent();
Chris@127: 
Chris@300:     int lly = height() - 6;
Chris@300:     if (m_manager->getZoomWheelsEnabled()) {
Chris@300:         lly -= 20;
Chris@300:     }
Chris@300: 
Chris@300:     if (r.y() + r.height() < lly - int(m_layers.size()) * fontHeight) {
Chris@261:         return;
Chris@127:     }
Chris@127: 
Chris@294:     QStringList texts;
Chris@299:     std::vector<QPixmap> pixmaps;
Chris@294:     for (LayerList::iterator i = m_layers.begin(); i != m_layers.end(); ++i) {
Chris@294:         texts.push_back((*i)->getLayerPresentationName());
Chris@299: //        std::cerr << "Pane " << this << ": Layer presentation name for " << *i << ": "
Chris@299: //                  << texts[texts.size()-1].toStdString() << std::endl;
Chris@299:         pixmaps.push_back((*i)->getLayerPresentationPixmap
Chris@299:                           (QSize(fontAscent, fontAscent)));
Chris@294:     }
Chris@127: 
Chris@294:     int maxTextWidth = width() / 3;
Chris@294:     texts = TextAbbrev::abbreviate(texts, paint.fontMetrics(), maxTextWidth);
Chris@294: 
Chris@261:     int llx = width() - maxTextWidth - 5;
Chris@261:     if (m_manager->getZoomWheelsEnabled()) {
Chris@261:         llx -= 36;
Chris@261:     }
Chris@261:     
Chris@300:     if (r.x() + r.width() >= llx - fontAscent - 3) {
Chris@261: 	
Chris@261:         for (size_t i = 0; i < texts.size(); ++i) {
Chris@299: 
Chris@299: //            std::cerr << "Pane "<< this << ": text " << i << ": " << texts[i].toStdString() << std::endl;
Chris@261:             
Chris@261:             if (i + 1 == texts.size()) {
Chris@287:                 paint.setPen(getForeground());
Chris@261:             }
Chris@261:             
Chris@261:             drawVisibleText(paint, llx,
Chris@261:                             lly - fontHeight + fontAscent,
Chris@261:                             texts[i], OutlinedText);
Chris@299: 
Chris@299:             if (!pixmaps[i].isNull()) {
Chris@299:                 paint.drawPixmap(llx - fontAscent - 3,
Chris@299:                                  lly - fontHeight + (fontHeight-fontAscent)/2,
Chris@299:                                  pixmaps[i]);
Chris@299:             }
Chris@261:             
Chris@261:             lly -= fontHeight;
Chris@261:         }
Chris@261:     }
Chris@261: }
Chris@127: 
Chris@261: void
Chris@261: Pane::drawEditingSelection(QPainter &paint)
Chris@261: {
Chris@261:     int offset = m_mousePos.x() - m_clickPos.x();
Chris@261:     int p0 = getXForFrame(m_editingSelection.getStartFrame()) + offset;
Chris@261:     int p1 = getXForFrame(m_editingSelection.getEndFrame()) + offset;
Chris@261:     
Chris@261:     if (m_editingSelectionEdge < 0) {
Chris@261:         p1 = getXForFrame(m_editingSelection.getEndFrame());
Chris@261:     } else if (m_editingSelectionEdge > 0) {
Chris@261:         p0 = getXForFrame(m_editingSelection.getStartFrame());
Chris@127:     }
Chris@127:     
Chris@261:     paint.save();
Chris@287:     paint.setPen(QPen(getForeground(), 2));
Chris@261:     
Chris@261:     //!!! duplicating display policy with View::drawSelections
Chris@261:     
Chris@261:     if (m_editingSelectionEdge < 0) {
Chris@261:         paint.drawLine(p0, 1, p1, 1);
Chris@261:         paint.drawLine(p0, 0, p0, height());
Chris@261:         paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@261:     } else if (m_editingSelectionEdge > 0) {
Chris@261:         paint.drawLine(p0, 1, p1, 1);
Chris@261:         paint.drawLine(p1, 0, p1, height());
Chris@261:         paint.drawLine(p0, height() - 1, p1, height() - 1);
Chris@261:     } else {
Chris@261:         paint.setBrush(Qt::NoBrush);
Chris@261:         paint.drawRect(p0, 1, p1 - p0, height() - 2);
Chris@261:     }
Chris@261:     paint.restore();
Chris@261: }
Chris@127: 
Chris@261: void
Chris@261: Pane::drawDurationAndRate(QRect r, const Model *waveformModel,
Chris@261:                           int sampleRate, QPainter &paint)
Chris@261: {
Chris@261:     int fontHeight = paint.fontMetrics().height();
Chris@261:     int fontAscent = paint.fontMetrics().ascent();
Chris@127: 
Chris@261:     if (r.y() + r.height() < height() - fontHeight - 6) return;
Chris@127: 
Chris@261:     size_t modelRate = waveformModel->getSampleRate();
Chris@301:     size_t nativeRate = waveformModel->getNativeRate();
Chris@261:     size_t playbackRate = m_manager->getPlaybackSampleRate();
Chris@261:     size_t outputRate = m_manager->getOutputSampleRate();
Chris@261:         
Chris@261:     QString srNote = "";
Chris@127: 
Chris@301:     // Show (R) for waveform models that have been resampled or will
Chris@301:     // be resampled on playback, and (X) for waveform models that will
Chris@301:     // be played at the wrong rate because their rate differs from the
Chris@261:     // current playback rate (which is not necessarily that of the
Chris@261:     // main model).
Chris@127: 
Chris@261:     if (playbackRate != 0) {
Chris@261:         if (modelRate == playbackRate) {
Chris@301:             if (modelRate != outputRate || modelRate != nativeRate) {
Chris@301:                 srNote = " " + tr("(R)");
Chris@301:             }
Chris@261:         } else {
Chris@261:             srNote = " " + tr("(X)");
Chris@261:         }
Chris@127:     }
Chris@127: 
Chris@261:     QString desc = tr("%1 / %2Hz%3")
Chris@261:         .arg(RealTime::frame2RealTime(waveformModel->getEndFrame(),
Chris@261:                                       sampleRate)
Chris@261:              .toText(false).c_str())
Chris@301:         .arg(nativeRate)
Chris@261:         .arg(srNote);
Chris@261: 
Chris@261:     if (r.x() < m_scaleWidth + 5 + paint.fontMetrics().width(desc)) {
Chris@261:         drawVisibleText(paint, m_scaleWidth + 5,
Chris@261:                         height() - fontHeight + fontAscent - 6,
Chris@261:                         desc, OutlinedText);
Chris@261:     }
Chris@127: }
Chris@127: 
Chris@227: bool
Chris@229: Pane::render(QPainter &paint, int xorigin, size_t f0, size_t f1)
Chris@227: {
Chris@229:     if (!View::render(paint, xorigin + m_scaleWidth, f0, f1)) {
Chris@227:         return false;
Chris@227:     }
Chris@227: 
Chris@227:     if (m_scaleWidth > 0) {
Chris@227: 
Chris@227:         for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@227:             --vi;
Chris@227:             
Chris@227:             paint.save();
Chris@227:             
Chris@287:             paint.setPen(getForeground());
Chris@287:             paint.setBrush(getBackground());
Chris@229:             paint.drawRect(xorigin, -1, m_scaleWidth, height()+1);
Chris@227:             
Chris@227:             paint.setBrush(Qt::NoBrush);
Chris@227:             (*vi)->paintVerticalScale
Chris@229:                 (this, paint, QRect(xorigin, 0, m_scaleWidth, height()));
Chris@227:             
Chris@227:             paint.restore();
Chris@227:             break;
Chris@227:         }
Chris@227:     }
Chris@227: 
Chris@227:     return true;
Chris@227: }
Chris@227: 
Chris@227: QImage *
Chris@229: Pane::toNewImage(size_t f0, size_t f1)
Chris@227: {
Chris@227:     size_t x0 = f0 / getZoomLevel();
Chris@227:     size_t x1 = f1 / getZoomLevel();
Chris@227: 
Chris@227:     QImage *image = new QImage(x1 - x0 + m_scaleWidth,
Chris@227:                                height(), QImage::Format_RGB32);
Chris@227: 
Chris@227:     int formerScaleWidth = m_scaleWidth;
Chris@227:             
Chris@227:     if (m_manager && m_manager->shouldShowVerticalScale()) {
Chris@227:         for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@227:             --vi;
Chris@227:             QPainter paint(image);
Chris@227:             m_scaleWidth = (*vi)->getVerticalScaleWidth(this, paint);
Chris@227:             break;
Chris@227:         }
Chris@227:     } else {
Chris@227:         m_scaleWidth = 0;
Chris@227:     }
Chris@227: 
Chris@227:     if (m_scaleWidth != formerScaleWidth) {
Chris@227:         delete image;
Chris@227:         image = new QImage(x1 - x0 + m_scaleWidth,
Chris@227:                            height(), QImage::Format_RGB32);
Chris@227:     }        
Chris@227: 
Chris@227:     QPainter *paint = new QPainter(image);
Chris@229:     if (!render(*paint, 0, f0, f1)) {
Chris@227:         delete paint;
Chris@227:         delete image;
Chris@227:         return 0;
Chris@227:     } else {
Chris@227:         delete paint;
Chris@227:         return image;
Chris@227:     }
Chris@227: }
Chris@227: 
Chris@229: QSize
Chris@229: Pane::getImageSize(size_t f0, size_t f1)
Chris@229: {
Chris@229:     QSize s = View::getImageSize(f0, f1);
Chris@229:     QImage *image = new QImage(100, 100, QImage::Format_RGB32);
Chris@229:     QPainter paint(image);
Chris@229: 
Chris@229:     int sw = 0;
Chris@229:     if (m_manager && m_manager->shouldShowVerticalScale()) {
Chris@229:         for (LayerList::iterator vi = m_layers.end(); vi != m_layers.begin(); ) {
Chris@229:             --vi;
Chris@229:             QPainter paint(image);
Chris@229:             sw = (*vi)->getVerticalScaleWidth(this, paint);
Chris@229:             break;
Chris@229:         }
Chris@229:     }
Chris@229:     
Chris@229:     return QSize(sw + s.width(), s.height());
Chris@229: }
Chris@229: 
Chris@222: size_t
Chris@222: Pane::getFirstVisibleFrame() const
Chris@222: {
Chris@222:     long f0 = getFrameForX(m_scaleWidth);
Chris@222:     size_t f = View::getFirstVisibleFrame();
Chris@222:     if (f0 < 0 || f0 < long(f)) return f;
Chris@222:     return f0;
Chris@222: }
Chris@222: 
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@174: bool
Chris@174: Pane::canTopLayerMoveVertical()
Chris@174: {
Chris@174:     float vmin, vmax, dmin, dmax;
Chris@174:     if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) return false;
Chris@174:     if (dmin <= vmin && dmax >= vmax) return false;
Chris@174:     return true;
Chris@174: }
Chris@174: 
Chris@174: bool
Chris@174: Pane::getTopLayerDisplayExtents(float &vmin, float &vmax,
Chris@188:                                 float &dmin, float &dmax,
Chris@188:                                 QString *unit) 
Chris@174: {
Chris@268:     Layer *layer = getTopLayer();
Chris@174:     if (!layer) return false;
Chris@174:     bool vlog;
Chris@174:     QString vunit;
Chris@188:     bool rv = (layer->getValueExtents(vmin, vmax, vlog, vunit) &&
Chris@188:                layer->getDisplayExtents(dmin, dmax));
Chris@188:     if (unit) *unit = vunit;
Chris@188:     return rv;
Chris@174: }
Chris@174: 
Chris@174: bool
Chris@174: Pane::setTopLayerDisplayExtents(float dmin, float dmax)
Chris@174: {
Chris@268:     Layer *layer = getTopLayer();
Chris@174:     if (!layer) return false;
Chris@174:     return layer->setDisplayExtents(dmin, dmax);
Chris@174: }
Chris@174: 
Chris@127: void
Chris@282: Pane::registerShortcuts(KeyReference &kr)
Chris@282: {
Chris@282:     kr.setCategory(tr("Zoom"));
Chris@282:     kr.registerAlternativeShortcut(tr("Zoom In"), tr("Wheel Up"));
Chris@282:     kr.registerAlternativeShortcut(tr("Zoom Out"), tr("Wheel Down"));
Chris@282: 
Chris@282:     kr.setCategory(tr("General Pane Mouse Actions"));
Chris@282:     
Chris@282:     kr.registerShortcut(tr("Zoom"), tr("Wheel"),
Chris@282:                         tr("Zoom in or out in time axis"));
Chris@282:     kr.registerShortcut(tr("Ctrl+Wheel"), tr("Scroll"),
Chris@282:                         tr("Scroll rapidly left or right in time axis"));
Chris@282:     kr.registerShortcut(tr("Zoom Vertically"), tr("Shift+Wheel"), 
Chris@282:                         tr("Zoom in or out in the vertical axis"));
Chris@282:     kr.registerShortcut(tr("Scroll Vertically"), tr("Alt+Wheel"), 
Chris@282:                         tr("Scroll up or down in the vertical axis"));
Chris@282:     kr.registerShortcut(tr("Navigate"), tr("Middle"), 
Chris@282:                         tr("Click middle button and drag to navigate with any tool"));
Chris@282:     kr.registerShortcut(tr("Relocate"), tr("Double-Click Middle"), 
Chris@282:                         tr("Double-click middle button to relocate with any tool"));
Chris@282:     kr.registerShortcut(tr("Menu"), tr("Right"),
Chris@282:                         tr("Show pane context menu"));
Chris@282:     
Chris@282:     kr.setCategory(tr("Navigate Tool Mouse Actions"));
Chris@282:     
Chris@282:     kr.registerShortcut(tr("Navigate"), tr("Left"), 
Chris@282:                         tr("Click left button and drag to move around"));
Chris@282:     kr.registerShortcut(tr("Zoom to Area"), tr("Shift+Left"), 
Chris@282:                         tr("Shift-click left button and drag to zoom to a rectangular area"));
Chris@282:     kr.registerShortcut(tr("Relocate"), tr("Double-Click Left"), 
Chris@282:                         tr("Double-click left button to jump to clicked location"));
Chris@282:     kr.registerShortcut(tr("Edit"), tr("Double-Click Left"), 
Chris@282:                         tr("Double-click left button on an item to edit it"));
Chris@282:         
Chris@282:     kr.setCategory(tr("Select Tool Mouse Actions"));
Chris@282:     kr.registerShortcut(tr("Select"), tr("Left"), 
Chris@282:                         tr("Click left button and drag to select region; drag region edge to resize"));
Chris@282:     kr.registerShortcut(tr("Multi Select"), tr("Ctrl+Left"), 
Chris@282:                         tr("Ctrl-click left button and drag to select an additional region"));
Chris@283:     kr.registerShortcut(tr("Fine Select"), tr("Shift+Left"), 
Chris@283:                         tr("Shift-click left button and drag to select without snapping to items or grid"));
Chris@282:     
Chris@282:     kr.setCategory(tr("Edit Tool Mouse Actions"));
Chris@282:     kr.registerShortcut(tr("Move"), tr("Left"), 
Chris@282:                         tr("Click left button on an item or selected region and drag to move"));
Chris@282:     kr.registerShortcut(tr("Edit"), tr("Double-Click Left"), 
Chris@282:                         tr("Double-click left button on an item to edit it"));
Chris@282:     
Chris@282:     kr.setCategory(tr("Draw Tool Mouse Actions"));
Chris@282:     kr.registerShortcut(tr("Draw"), tr("Left"), 
Chris@282:                         tr("Click left button and drag to create new item"));
Chris@282: 
Chris@282:     kr.setCategory(tr("Measure Tool Mouse Actions"));
Chris@282:     kr.registerShortcut(tr("Measure Area"), tr("Left"), 
Chris@282:                         tr("Click left button and drag to measure a rectangular area"));
Chris@282:     kr.registerShortcut(tr("Measure Item"), tr("Double-Click Left"), 
Chris@282:                         tr("Click left button and drag to measure extents of an item or shape"));
Chris@283:     kr.registerShortcut(tr("Zoom to Area"), tr("Shift+Left"), 
Chris@283:                         tr("Shift-click left button and drag to zoom to a rectangular area"));
Chris@282: }
Chris@282: 
Chris@282: void
Chris@127: Pane::mousePressEvent(QMouseEvent *e)
Chris@127: {
Chris@127:     if (e->buttons() & Qt::RightButton) {
Chris@189:         emit contextHelpChanged("");
Chris@127:         emit rightButtonMenuRequested(mapToGlobal(e->pos()));
Chris@127:         return;
Chris@127:     }
Chris@127: 
Chris@341:     std::cerr << "mousePressEvent" << std::endl;
Chris@341: 
Chris@341:     
Chris@341:     //!!! need to avoid spurious mouse events when e.g. double-clicking.
Chris@341: 
Chris@341:     // when double clicking, we get:
Chris@341:     // mousePressEvent
Chris@341:     // mouseReleaseEvent
Chris@341:     // mouseMoveEvent <- called from mouseReleaseEvent
Chris@341:     // mouseDoubleClickEvent
Chris@341:     // mouseReleaseEvent
Chris@341:     // mouseMoveEvent <- called from mouseReleaseEvent
Chris@341: 
Chris@341:     // so we set a timer on the first press, if we're in a mode in
Chris@341:     // which moving the mouse will do something
Chris@341: 
Chris@341:     // the move method doesn't do anything unless that timer has
Chris@341:     // elapsed
Chris@341:     
Chris@341: 
Chris@127:     m_clickPos = e->pos();
Chris@262:     m_mousePos = m_clickPos;
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@150:     m_dragMode = UnresolvedDrag;
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@283:     if (mode == ViewManager::NavigateMode ||
Chris@283:         (e->buttons() & Qt::MidButton) ||
Chris@283:         (mode == ViewManager::MeasureMode &&
Chris@283:          (e->buttons() & Qt::LeftButton) && m_shiftPressed)) {
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@136:         m_dragStartMinValue = 0;
Chris@174:         
Chris@174:         float vmin, vmax, dmin, dmax;
Chris@174:         if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) {
Chris@174:             m_dragStartMinValue = dmin;
Chris@136:         }
Chris@136: 
Chris@127:     } else if (mode == ViewManager::SelectMode) {
Chris@127: 
Chris@217:         if (!hasTopLayerTimeXAxis()) return;
Chris@217: 
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@333: 		m_manager->setInProgressSelection
Chris@333:                     (Selection(alignToReference(snapFrame),
Chris@333:                                alignToReference(snapFrame + resolution)),
Chris@333:                      !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@335:     } else if (mode == ViewManager::EraseMode) {
Chris@335: 
Chris@335: 	Layer *layer = getSelectedLayer();
Chris@335: 	if (layer && layer->isLayerEditable()) {
Chris@335: 	    layer->eraseStart(this, e);
Chris@335: 	}
Chris@335: 
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@262: 
Chris@262:     } else if (mode == ViewManager::MeasureMode) {
Chris@262: 
Chris@268:         Layer *layer = getTopLayer();
Chris@267:         if (layer) layer->measureStart(this, e);
Chris@262:         update();
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@341:     std::cerr << "mouseReleaseEvent" << std::endl;
Chris@341: 
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: 
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: 
Chris@174:             zoomToRegion(x0, y0, x1, y1);
Chris@127: 	}
Chris@127: 
Chris@127:     } else if (mode == ViewManager::SelectMode) {
Chris@127: 
Chris@217:         if (!hasTopLayerTimeXAxis()) return;
Chris@217: 
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@335:     } else if (mode == ViewManager::EraseMode) {
Chris@335: 
Chris@335: 	Layer *layer = getSelectedLayer();
Chris@335: 	if (layer && layer->isLayerEditable()) {
Chris@335: 	    layer->eraseEnd(this, e);
Chris@335: 	    update();
Chris@335: 	}
Chris@335: 
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@262: 
Chris@262:     } else if (mode == ViewManager::MeasureMode) {
Chris@262: 
Chris@268:         Layer *layer = getTopLayer();
Chris@267:         if (layer) layer->measureEnd(this, e);
Chris@267:         if (m_measureCursor1) setCursor(*m_measureCursor1);
Chris@267:         update();
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@341:     std::cerr << "mouseMoveEvent" << std::endl;
Chris@341: 
Chris@341:     //!!! if no buttons pressed, and not called from
Chris@341:     //mouseReleaseEvent, we want to reset clicked-ness (to avoid
Chris@341:     //annoying continual drags when we moved the mouse outside the
Chris@341:     //window after pressing button first time).
Chris@341: 
Chris@189:     updateContextHelp(&e->pos());
Chris@189: 
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@217: 	if (mode == ViewManager::SelectMode && hasTopLayerTimeXAxis()) {
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 (!m_manager->isPlaying()) {
Chris@127: 
Chris@272:             bool updating = false;
Chris@272: 
Chris@326:             if (getSelectedLayer() &&
Chris@326:                 m_manager->shouldIlluminateLocalFeatures()) {
Chris@127: 
Chris@174:                 bool previouslyIdentifying = m_identifyFeatures;
Chris@174:                 m_identifyFeatures = true;
Chris@174:                 
Chris@174:                 if (m_identifyFeatures != previouslyIdentifying ||
Chris@174:                     m_identifyPoint != prevPoint) {
Chris@174:                     update();
Chris@272:                     updating = true;
Chris@272:                 }
Chris@272:             }
Chris@272: 
Chris@272:             if (!updating && mode == ViewManager::MeasureMode &&
Chris@272:                 m_manager && !m_manager->isPlaying()) {
Chris@272: 
Chris@272:                 Layer *layer = getTopLayer();
Chris@272:                 if (layer && layer->nearestMeasurementRectChanged
Chris@272:                     (this, prevPoint, m_identifyPoint)) {
Chris@272:                     update();
Chris@174:                 }
Chris@174:             }
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@174:             dragTopLayer(e);
Chris@150:         }
Chris@127: 
Chris@127:     } else if (mode == ViewManager::SelectMode) {
Chris@127: 
Chris@217:         if (!hasTopLayerTimeXAxis()) return;
Chris@217: 
Chris@174:         dragExtendSelection(e);
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@335:     } else if (mode == ViewManager::EraseMode) {
Chris@335: 
Chris@335: 	Layer *layer = getSelectedLayer();
Chris@335: 	if (layer && layer->isLayerEditable()) {
Chris@335: 	    layer->eraseDrag(this, e);
Chris@335: 	}
Chris@335: 
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@259: 
Chris@259:     } else if (mode == ViewManager::MeasureMode) {
Chris@259: 
Chris@267:         if (m_measureCursor2) setCursor(*m_measureCursor2);
Chris@266: 
Chris@268:         Layer *layer = getTopLayer();
Chris@290:         if (layer) {
Chris@290:             layer->measureDrag(this, e);
Chris@290:             if (layer->hasTimeXAxis()) edgeScrollMaybe(e->x());
Chris@290:         }
Chris@267: 
Chris@267:         update();
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@127: void
Chris@174: Pane::zoomToRegion(int x0, int y0, int x1, int y1)
Chris@174: {
Chris@174:     int w = x1 - x0;
Chris@174: 	    
Chris@174:     long newStartFrame = getFrameForX(x0);
Chris@174: 	    
Chris@174:     long visibleFrames = getEndFrame() - getStartFrame();
Chris@174:     if (newStartFrame <= -visibleFrames) {
Chris@174:         newStartFrame  = -visibleFrames + 1;
Chris@174:     }
Chris@174: 	    
Chris@174:     if (newStartFrame >= long(getModelsEndFrame())) {
Chris@174:         newStartFrame  = getModelsEndFrame() - 1;
Chris@174:     }
Chris@174: 	    
Chris@174:     float ratio = float(w) / float(width());
Chris@174: //	std::cerr << "ratio: " << ratio << std::endl;
Chris@174:     size_t newZoomLevel = (size_t)nearbyint(m_zoomLevel * ratio);
Chris@174:     if (newZoomLevel < 1) newZoomLevel = 1;
Chris@174: 
Chris@174: //	std::cerr << "start: " << m_startFrame << ", level " << m_zoomLevel << std::endl;
Chris@174:     setZoomLevel(getZoomConstraintBlockSize(newZoomLevel));
Chris@174:     setStartFrame(newStartFrame);
Chris@174: 
Chris@174:     QString unit;
Chris@174:     float min, max;
Chris@174:     bool log;
Chris@174:     Layer *layer = 0;
Chris@174:     for (LayerList::const_iterator i = m_layers.begin();
Chris@174:          i != m_layers.end(); ++i) { 
Chris@174:         if ((*i)->getValueExtents(min, max, log, unit) &&
Chris@174:             (*i)->getDisplayExtents(min, max)) {
Chris@174:             layer = *i;
Chris@174:             break;
Chris@174:         }
Chris@174:     }
Chris@174:             
Chris@174:     if (layer) {
Chris@174:         if (log) {
Chris@174:             min = (min < 0.0) ? -log10f(-min) : (min == 0.0) ? 0.0 : log10f(min);
Chris@174:             max = (max < 0.0) ? -log10f(-max) : (max == 0.0) ? 0.0 : log10f(max);
Chris@174:         }
Chris@174:         float rmin = min + ((max - min) * (height() - y1)) / height();
Chris@174:         float rmax = min + ((max - min) * (height() - y0)) / height();
Chris@174:         std::cerr << "min: " << min << ", max: " << max << ", y0: " << y0 << ", y1: " << y1 << ", h: " << height() << ", rmin: " << rmin << ", rmax: " << rmax << std::endl;
Chris@174:         if (log) {
Chris@174:             rmin = powf(10, rmin);
Chris@174:             rmax = powf(10, rmax);
Chris@174:         }
Chris@174:         std::cerr << "finally: rmin: " << rmin << ", rmax: " << rmax << " " << unit.toStdString() << std::endl;
Chris@174: 
Chris@174:         layer->setDisplayExtents(rmin, rmax);
Chris@174:         updateVerticalPanner();
Chris@174:     }
Chris@174: }
Chris@174: 
Chris@174: void
Chris@174: Pane::dragTopLayer(QMouseEvent *e)
Chris@174: {
Chris@174:     // We need to avoid making it too easy to drag both
Chris@174:     // horizontally and vertically, in the case where the
Chris@174:     // mouse is moved "mostly" in horizontal or vertical axis
Chris@174:     // with only a small variation in the other axis.  This is
Chris@174:     // particularly important during playback (when we want to
Chris@174:     // avoid small horizontal motions) or in slow refresh
Chris@174:     // layers like spectrogram (when we want to avoid small
Chris@174:     // vertical motions).
Chris@174:     // 
Chris@174:     // To this end we have horizontal and vertical thresholds
Chris@174:     // and a series of states: unresolved, horizontally or
Chris@174:     // vertically constrained, free.
Chris@174:     //
Chris@174:     // When the mouse first moves, we're unresolved: we
Chris@174:     // restrict ourselves to whichever direction seems safest,
Chris@174:     // until the mouse has passed a small threshold distance
Chris@174:     // from the click point.  Then we lock in to one of the
Chris@174:     // constrained modes, based on which axis that distance
Chris@174:     // was measured in first.  Finally, if it turns out we've
Chris@174:     // also moved more than a certain larger distance in the
Chris@174:     // other direction as well, we may switch into free mode.
Chris@174:     // 
Chris@174:     // If the top layer is incapable of being dragged
Chris@174:     // vertically, the logic is short circuited.
Chris@174: 
Chris@174:     int xdiff = e->x() - m_clickPos.x();
Chris@174:     int ydiff = e->y() - m_clickPos.y();
Chris@174:     int smallThreshold = 10, bigThreshold = 50;
Chris@174: 
Chris@174:     bool canMoveVertical = canTopLayerMoveVertical();
Chris@174:     bool canMoveHorizontal = true;
Chris@174: 
Chris@174:     if (!canMoveHorizontal) {
Chris@174:         m_dragMode = HorizontalDrag;
Chris@174:     }
Chris@174: 
Chris@174:     if (m_dragMode == UnresolvedDrag) {
Chris@174: 
Chris@174:         if (abs(ydiff) > smallThreshold &&
Chris@174:             abs(ydiff) > abs(xdiff) * 2) {
Chris@174:             m_dragMode = VerticalDrag;
Chris@174:         } else if (abs(xdiff) > smallThreshold &&
Chris@174:                    abs(xdiff) > abs(ydiff) * 2) {
Chris@174:             m_dragMode = HorizontalDrag;
Chris@174:         } else if (abs(xdiff) > smallThreshold &&
Chris@174:                    abs(ydiff) > smallThreshold) {
Chris@174:             m_dragMode = FreeDrag;
Chris@174:         } else {
Chris@174:             // When playing, we don't want to disturb the play
Chris@174:             // position too easily; when not playing, we don't
Chris@174:             // want to move up/down too easily
Chris@174:             if (m_manager && m_manager->isPlaying()) {
Chris@174:                 canMoveHorizontal = false;
Chris@174:             } else {
Chris@174:                 canMoveVertical = false;
Chris@174:             }
Chris@174:         }
Chris@174:     }
Chris@174: 
Chris@174:     if (m_dragMode == VerticalDrag) {
Chris@174:         if (abs(xdiff) > bigThreshold) m_dragMode = FreeDrag;
Chris@174:         else canMoveHorizontal = false;
Chris@174:     }
Chris@174: 
Chris@174:     if (m_dragMode == HorizontalDrag && canMoveVertical) {
Chris@174:         if (abs(ydiff) > bigThreshold) m_dragMode = FreeDrag;
Chris@174:         else canMoveVertical = false;
Chris@174:     }
Chris@174: 
Chris@174:     if (canMoveHorizontal) {
Chris@174: 
Chris@174:         long frameOff = getFrameForX(e->x()) - getFrameForX(m_clickPos.x());
Chris@174: 
Chris@174:         size_t newCentreFrame = m_dragCentreFrame;
Chris@174: 	    
Chris@174:         if (frameOff < 0) {
Chris@174:             newCentreFrame -= frameOff;
Chris@174:         } else if (newCentreFrame >= size_t(frameOff)) {
Chris@174:             newCentreFrame -= frameOff;
Chris@174:         } else {
Chris@174:             newCentreFrame = 0;
Chris@174:         }
Chris@174: 	    
Chris@339:         std::cerr << "Pane::dragTopLayer: newCentreFrame = " << newCentreFrame <<
Chris@339:             ", models end frame = " << getModelsEndFrame() << std::endl;
Chris@339: 
Chris@174:         if (newCentreFrame >= getModelsEndFrame()) {
Chris@174:             newCentreFrame = getModelsEndFrame();
Chris@174:             if (newCentreFrame > 0) --newCentreFrame;
Chris@174:         }
Chris@174:                 
Chris@174:         if (getXForFrame(m_centreFrame) != getXForFrame(newCentreFrame)) {
Chris@174:             setCentreFrame(newCentreFrame);
Chris@174:         }
Chris@174:     }
Chris@174: 
Chris@174:     if (canMoveVertical) {
Chris@174: 
Chris@174:         float vmin = 0.f, vmax = 0.f;
Chris@174:         float dmin = 0.f, dmax = 0.f;
Chris@174: 
Chris@174:         if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) {
Chris@174: 
Chris@248: //            std::cerr << "ydiff = " << ydiff << std::endl;
Chris@174: 
Chris@174:             float perpix = (dmax - dmin) / height();
Chris@174:             float valdiff = ydiff * perpix;
Chris@248: //            std::cerr << "valdiff = " << valdiff << std::endl;
Chris@174: 
Chris@174:             float newmin = m_dragStartMinValue + valdiff;
Chris@174:             float newmax = m_dragStartMinValue + (dmax - dmin) + valdiff;
Chris@174:             if (newmin < vmin) {
Chris@174:                 newmax += vmin - newmin;
Chris@174:                 newmin += vmin - newmin;
Chris@174:             }
Chris@174:             if (newmax > vmax) {
Chris@174:                 newmin -= newmax - vmax;
Chris@174:                 newmax -= newmax - vmax;
Chris@174:             }
Chris@248: //            std::cerr << "(" << dmin << ", " << dmax << ") -> ("
Chris@248: //                      << newmin << ", " << newmax << ") (drag start " << m_dragStartMinValue << ")" << std::endl;
Chris@174: 
Chris@174:             setTopLayerDisplayExtents(newmin, newmax);
Chris@174:             updateVerticalPanner();
Chris@174:         }
Chris@174:     }
Chris@174: }
Chris@174: 
Chris@174: void
Chris@174: Pane::dragExtendSelection(QMouseEvent *e)
Chris@174: {
Chris@174:     int mouseFrame = getFrameForX(e->x());
Chris@174:     size_t resolution = 1;
Chris@174:     int snapFrameLeft = mouseFrame;
Chris@174:     int snapFrameRight = mouseFrame;
Chris@174: 	
Chris@174:     Layer *layer = getSelectedLayer();
Chris@174:     if (layer && !m_shiftPressed) {
Chris@174:         layer->snapToFeatureFrame(this, snapFrameLeft,
Chris@174:                                   resolution, Layer::SnapLeft);
Chris@174:         layer->snapToFeatureFrame(this, snapFrameRight,
Chris@174:                                   resolution, Layer::SnapRight);
Chris@174:     }
Chris@174: 	
Chris@174: //	std::cerr << "snap: frame = " << mouseFrame << ", start frame = " << m_selectionStartFrame << ", left = " << snapFrameLeft << ", right = " << snapFrameRight << std::endl;
Chris@174: 
Chris@174:     if (snapFrameLeft < 0) snapFrameLeft = 0;
Chris@174:     if (snapFrameRight < 0) snapFrameRight = 0;
Chris@174: 	
Chris@174:     size_t min, max;
Chris@174: 	
Chris@248:     if (m_selectionStartFrame > size_t(snapFrameLeft)) {
Chris@174:         min = snapFrameLeft;
Chris@174:         max = m_selectionStartFrame;
Chris@248:     } else if (size_t(snapFrameRight) > m_selectionStartFrame) {
Chris@174:         min = m_selectionStartFrame;
Chris@174:         max = snapFrameRight;
Chris@174:     } else {
Chris@174:         min = snapFrameLeft;
Chris@174:         max = snapFrameRight;
Chris@174:     }
Chris@174: 
Chris@174:     if (m_manager) {
Chris@333:         m_manager->setInProgressSelection(Selection(alignToReference(min),
Chris@333:                                                     alignToReference(max)),
Chris@174:                                           !m_resizing && !m_ctrlPressed);
Chris@174:     }
Chris@174: 
Chris@259:     edgeScrollMaybe(e->x());
Chris@259: 
Chris@259:     update();
Chris@259: }
Chris@259: 
Chris@259: void
Chris@259: Pane::edgeScrollMaybe(int x)
Chris@259: {
Chris@259:     int mouseFrame = getFrameForX(x);
Chris@259: 
Chris@174:     bool doScroll = false;
Chris@174:     if (!m_manager) doScroll = true;
Chris@174:     if (!m_manager->isPlaying()) doScroll = true;
Chris@174:     if (m_followPlay != PlaybackScrollContinuous) doScroll = true;
Chris@174: 
Chris@174:     if (doScroll) {
Chris@174:         int offset = mouseFrame - getStartFrame();
Chris@174:         int available = getEndFrame() - getStartFrame();
Chris@259:         int move = 0;
Chris@174:         if (offset >= available * 0.95) {
Chris@259:             move = int(offset - available * 0.95) + 1;
Chris@259:         } else if (offset <= available * 0.10) {
Chris@259:              move = int(available * 0.10 - offset) + 1;
Chris@259:              move = -move;
Chris@259:         }
Chris@259:         if (move != 0) {
Chris@174:             setCentreFrame(m_centreFrame + move);
Chris@259:             update();
Chris@174:         }
Chris@174:     }
Chris@174: }
Chris@174: 
Chris@174: void
Chris@127: Pane::mouseDoubleClickEvent(QMouseEvent *e)
Chris@127: {
Chris@127:     if (e->buttons() & Qt::RightButton) {
Chris@127:         return;
Chris@127:     }
Chris@127: 
Chris@341:     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@255:     bool relocate = (mode == ViewManager::NavigateMode ||
Chris@255:                      (e->buttons() & Qt::MidButton));
Chris@255: 
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@255: 	    if (layer->editOpen(this, e)) relocate = false;
Chris@127: 	}
Chris@280: 
Chris@280:     } else if (mode == ViewManager::MeasureMode) {
Chris@280: 
Chris@280:         Layer *layer = getTopLayer();
Chris@280:         if (layer) layer->measureDoubleClick(this, e);
Chris@280:         update();
Chris@127:     }
Chris@255: 
Chris@255:     if (relocate) {
Chris@255: 
Chris@255:         long f = getFrameForX(e->x());
Chris@255: 
Chris@255:         setCentreFrame(f);
Chris@255: 
Chris@255:         m_dragCentreFrame = f;
Chris@255:         m_dragStartMinValue = 0;
Chris@255:         m_dragMode = UnresolvedDrag;
Chris@255: 
Chris@255:         float vmin, vmax, dmin, dmax;
Chris@255:         if (getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) {
Chris@255:             m_dragStartMinValue = dmin;
Chris@255:         }
Chris@255:     }
Chris@127: }
Chris@127: 
Chris@127: void
Chris@290: Pane::enterEvent(QEvent *)
Chris@290: {
Chris@290:     m_mouseInWidget = true;
Chris@290: }
Chris@290: 
Chris@290: void
Chris@127: Pane::leaveEvent(QEvent *)
Chris@127: {
Chris@290:     m_mouseInWidget = false;
Chris@127:     bool previouslyIdentifying = m_identifyFeatures;
Chris@127:     m_identifyFeatures = false;
Chris@127:     if (previouslyIdentifying) update();
Chris@189:     emit contextHelpChanged("");
Chris@127: }
Chris@127: 
Chris@127: void
Chris@133: Pane::resizeEvent(QResizeEvent *)
Chris@133: {
Chris@133:     updateHeadsUpDisplay();
Chris@133: }
Chris@133: 
Chris@133: 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@256:     } else if (e->modifiers() & Qt::ShiftModifier) {
Chris@256: 
Chris@256:         // Zoom vertically
Chris@256: 
Chris@256:         if (m_vpan) {
Chris@256:             m_vpan->scroll(e->delta() > 0);
Chris@256:         }
Chris@256: 
Chris@256:     } else if (e->modifiers() & Qt::AltModifier) {
Chris@256: 
Chris@256:         // Zoom vertically
Chris@256: 
Chris@256:         if (m_vthumb) {
Chris@256:             m_vthumb->scroll(e->delta() > 0);
Chris@256:         }
Chris@256: 
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@132: void
Chris@132: Pane::horizontalThumbwheelMoved(int value)
Chris@132: {
Chris@137:     //!!! dupe with updateHeadsUpDisplay
Chris@137: 
Chris@132:     int count = 0;
Chris@132:     int level = 1;
Chris@137: 
Chris@137: 
Chris@137:     //!!! pull out into function (presumably in View)
Chris@137:     bool haveConstraint = false;
Chris@137:     for (LayerList::const_iterator i = m_layers.begin(); i != m_layers.end();
Chris@137:          ++i) {
Chris@137:         if ((*i)->getZoomConstraint() && !(*i)->supportsOtherZoomLevels()) {
Chris@137:             haveConstraint = true;
Chris@137:             break;
Chris@137:         }
Chris@132:     }
Chris@132: 
Chris@137:     if (haveConstraint) {
Chris@137:         while (true) {
Chris@137:             if (m_hthumb->getMaximumValue() - value == count) break;
Chris@137:             int newLevel = getZoomConstraintBlockSize(level + 1,
Chris@137:                                                       ZoomConstraint::RoundUp);
Chris@137:             if (newLevel == level) break;
Chris@137:             level = newLevel;
Chris@137:             if (++count == 50) break;
Chris@137:         }
Chris@137:     } else {
Chris@137:         while (true) {
Chris@137:             if (m_hthumb->getMaximumValue() - value == count) break;
Chris@137:             int step = level / 10;
Chris@137:             int pwr = 0;
Chris@137:             while (step > 0) {
Chris@137:                 ++pwr;
Chris@137:                 step /= 2;
Chris@137:             }
Chris@137:             step = 1;
Chris@137:             while (pwr > 0) {
Chris@137:                 step *= 2;
Chris@137:                 --pwr;
Chris@137:             }
Chris@137: //            std::cerr << level << std::endl;
Chris@137:             level += step;
Chris@137:             if (++count == 100 || level > 262144) break;
Chris@137:         }
Chris@137:     }
Chris@137:         
Chris@244: //    std::cerr << "new level is " << level << std::endl;
Chris@132:     setZoomLevel(level);
Chris@132: }    
Chris@132: 
Chris@132: void
Chris@132: Pane::verticalThumbwheelMoved(int value)
Chris@132: {
Chris@133:     Layer *layer = 0;
Chris@133:     if (getLayerCount() > 0) layer = getLayer(getLayerCount() - 1);
Chris@133:     if (layer) {
Chris@133:         int defaultStep = 0;
Chris@133:         int max = layer->getVerticalZoomSteps(defaultStep);
Chris@133:         if (max == 0) {
Chris@133:             updateHeadsUpDisplay();
Chris@133:             return;
Chris@133:         }
Chris@133:         if (value > max) {
Chris@133:             value = max;
Chris@133:         }
Chris@133:         layer->setVerticalZoomStep(value);
Chris@174:         updateVerticalPanner();
Chris@133:     }
Chris@132: }    
Chris@132: 
Chris@174: void
Chris@174: Pane::verticalPannerMoved(float x0, float y0, float w, float h)
Chris@174: {
Chris@174:     float vmin, vmax, dmin, dmax;
Chris@174:     if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax)) return;
Chris@174:     float y1 = y0 + h;
Chris@174:     float newmax = vmin + ((1.0 - y0) * (vmax - vmin));
Chris@174:     float newmin = vmin + ((1.0 - y1) * (vmax - vmin));
Chris@174:     std::cerr << "verticalPannerMoved: (" << x0 << "," << y0 << "," << w
Chris@174:               << "," << h << ") -> (" << newmin << "," << newmax << ")" << std::endl;
Chris@174:     setTopLayerDisplayExtents(newmin, newmax);
Chris@174: }
Chris@174: 
Chris@188: void
Chris@188: Pane::editVerticalPannerExtents()
Chris@188: {
Chris@188:     if (!m_vpan || !m_manager || !m_manager->getZoomWheelsEnabled()) return;
Chris@188: 
Chris@188:     float vmin, vmax, dmin, dmax;
Chris@188:     QString unit;
Chris@188:     if (!getTopLayerDisplayExtents(vmin, vmax, dmin, dmax, &unit)
Chris@188:         || vmax == vmin) {
Chris@188:         return;
Chris@188:     }
Chris@188: 
Chris@188:     RangeInputDialog dialog(tr("Enter new range"),
Chris@188:                             tr("New vertical display range, from %1 to %2 %4:")
Chris@188:                             .arg(vmin).arg(vmax).arg(unit),
Chris@188:                             unit, vmin, vmax, this);
Chris@188:     dialog.setRange(dmin, dmax);
Chris@188: 
Chris@188:     if (dialog.exec() == QDialog::Accepted) {
Chris@188:         dialog.getRange(dmin, dmax);
Chris@188:         setTopLayerDisplayExtents(dmin, dmax);
Chris@188:         updateVerticalPanner();
Chris@188:     }
Chris@188: }
Chris@188: 
Chris@312: void
Chris@312: Pane::dragEnterEvent(QDragEnterEvent *e)
Chris@312: {
Chris@312:     QStringList formats(e->mimeData()->formats());
Chris@312:     std::cerr << "dragEnterEvent: format: "
Chris@312:               << formats.join(",").toStdString()
Chris@312:               << ", possibleActions: " << e->possibleActions()
Chris@312:               << ", proposedAction: " << e->proposedAction() << std::endl;
Chris@312:     
Chris@312:     if (e->provides("text/uri-list") || e->provides("text/plain")) {
Chris@312: 
Chris@312:         if (e->proposedAction() & Qt::CopyAction) {
Chris@312:             e->acceptProposedAction();
Chris@312:         } else {
Chris@312:             e->setDropAction(Qt::CopyAction);
Chris@312:             e->accept();
Chris@312:         }
Chris@312:     }
Chris@312: }
Chris@312: 
Chris@312: void
Chris@312: Pane::dropEvent(QDropEvent *e)
Chris@312: {
Chris@312:     std::cerr << "dropEvent: text: \"" << e->mimeData()->text().toStdString()
Chris@312:               << "\"" << std::endl;
Chris@312: 
Chris@312:     if (e->provides("text/uri-list") || e->provides("text/plain")) {
Chris@312: 
Chris@312:         if (e->proposedAction() & Qt::CopyAction) {
Chris@312:             e->acceptProposedAction();
Chris@312:         } else {
Chris@312:             e->setDropAction(Qt::CopyAction);
Chris@312:             e->accept();
Chris@312:         }
Chris@312: 
Chris@312:         if (e->provides("text/uri-list")) {
Chris@312: 
Chris@312:             std::cerr << "accepting... data is \"" << e->encodedData("text/uri-list").data() << "\"" << std::endl;
Chris@312:             emit dropAccepted(QString::fromLocal8Bit
Chris@312:                               (e->encodedData("text/uri-list").data())
Chris@312:                               .split(QRegExp("[\\r\\n]+"), 
Chris@312:                                      QString::SkipEmptyParts));
Chris@312:         } else {
Chris@312:             emit dropAccepted(QString::fromLocal8Bit
Chris@312:                               (e->encodedData("text/plain").data()));
Chris@312:         }
Chris@312:     }
Chris@312: }
Chris@312: 
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@248: Pane::editSelectionEnd(QMouseEvent *)
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@267:     if (mode == ViewManager::MeasureMode && !m_measureCursor1) {
Chris@267:         m_measureCursor1 = new QCursor(QBitmap(":/icons/measure1cursor.xbm"),
Chris@267:                                        QBitmap(":/icons/measure1mask.xbm"),
Chris@267:                                        15, 14);
Chris@267:         m_measureCursor2 = new QCursor(QBitmap(":/icons/measure2cursor.xbm"),
Chris@267:                                        QBitmap(":/icons/measure2mask.xbm"),
Chris@267:                                        16, 17);
Chris@257:     }
Chris@257: 
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@335: 	
Chris@335:     case ViewManager::EraseMode:
Chris@335: 	setCursor(Qt::CrossCursor);
Chris@335: 	break;
Chris@257: 
Chris@257:     case ViewManager::MeasureMode:
Chris@267:         if (m_measureCursor1) setCursor(*m_measureCursor1);
Chris@257: 	break;
Chris@257: 
Chris@127: /*	
Chris@127:     case ViewManager::TextMode:
Chris@127: 	setCursor(Qt::IBeamCursor);
Chris@127: 	break;
Chris@127: */
Chris@127:     }
Chris@127: }
Chris@127: 
Chris@133: void
Chris@133: Pane::zoomWheelsEnabledChanged()
Chris@133: {
Chris@133:     updateHeadsUpDisplay();
Chris@133:     update();
Chris@133: }
Chris@133: 
Chris@133: void
Chris@224: Pane::viewZoomLevelChanged(View *v, unsigned long z, bool locked)
Chris@133: {
Chris@212: //    std::cerr << "Pane[" << this << "]::zoomLevelChanged (global now "
Chris@212: //              << (m_manager ? m_manager->getGlobalZoom() : 0) << ")" << std::endl;
Chris@192: 
Chris@224:     View::viewZoomLevelChanged(v, z, locked);
Chris@224: 
Chris@232:     if (m_hthumb && !m_hthumb->isVisible()) return;
Chris@224: 
Chris@222:     if (v != this) {
Chris@222:         if (!locked || !m_followZoom) return;
Chris@222:     }
Chris@222: 
Chris@133:     if (m_manager && m_manager->getZoomWheelsEnabled()) {
Chris@133:         updateHeadsUpDisplay();
Chris@133:     }
Chris@133: }
Chris@133: 
Chris@133: void
Chris@133: Pane::propertyContainerSelected(View *v, PropertyContainer *pc)
Chris@133: {
Chris@133:     Layer *layer = 0;
Chris@133: 
Chris@133:     if (getLayerCount() > 0) {
Chris@133:         layer = getLayer(getLayerCount() - 1);
Chris@133:         disconnect(layer, SIGNAL(verticalZoomChanged()),
Chris@133:                    this, SLOT(verticalZoomChanged()));
Chris@133:     }
Chris@133: 
Chris@133:     View::propertyContainerSelected(v, pc);
Chris@133:     updateHeadsUpDisplay();
Chris@133: 
Chris@187:     if (m_vthumb) {
Chris@187:         RangeMapper *rm = 0;
Chris@187:         if (layer) rm = layer->getNewVerticalZoomRangeMapper();
Chris@187:         if (rm) m_vthumb->setRangeMapper(rm);
Chris@187:     }
Chris@187: 
Chris@133:     if (getLayerCount() > 0) {
Chris@133:         layer = getLayer(getLayerCount() - 1);
Chris@133:         connect(layer, SIGNAL(verticalZoomChanged()),
Chris@133:                 this, SLOT(verticalZoomChanged()));
Chris@133:     }
Chris@133: }
Chris@133: 
Chris@133: void
Chris@133: Pane::verticalZoomChanged()
Chris@133: {
Chris@133:     Layer *layer = 0;
Chris@133: 
Chris@133:     if (getLayerCount() > 0) {
Chris@133: 
Chris@133:         layer = getLayer(getLayerCount() - 1);
Chris@133: 
Chris@133:         if (m_vthumb && m_vthumb->isVisible()) {
Chris@133:             m_vthumb->setValue(layer->getCurrentVerticalZoomStep());
Chris@133:         }
Chris@133:     }
Chris@133: }
Chris@133: 
Chris@189: void
Chris@189: Pane::updateContextHelp(const QPoint *pos)
Chris@189: {
Chris@189:     QString help = "";
Chris@189: 
Chris@189:     if (m_clickedInRange) {
Chris@189:         emit contextHelpChanged("");
Chris@189:         return;
Chris@189:     }
Chris@189: 
Chris@189:     ViewManager::ToolMode mode = ViewManager::NavigateMode;
Chris@189:     if (m_manager) mode = m_manager->getToolMode();
Chris@189: 
Chris@189:     bool editable = false;
Chris@189:     Layer *layer = getSelectedLayer();
Chris@189:     if (layer && layer->isLayerEditable()) {
Chris@189:         editable = true;
Chris@189:     }
Chris@189:         
Chris@189:     if (mode == ViewManager::NavigateMode) {
Chris@189: 
Chris@189:         help = tr("Click and drag to navigate");
Chris@189:         
Chris@189:     } else if (mode == ViewManager::SelectMode) {
Chris@189: 
Chris@217:         if (!hasTopLayerTimeXAxis()) return;
Chris@217: 
Chris@189:         bool haveSelection = (m_manager && !m_manager->getSelections().empty());
Chris@189: 
Chris@189:         if (haveSelection) {
Chris@189:             if (editable) {
Chris@189:                 help = tr("Click and drag to select a range; hold Shift to avoid snapping to items; hold Ctrl for multi-select; middle-click and drag to navigate");
Chris@189:             } else {
Chris@189:                 help = tr("Click and drag to select a range; hold Ctrl for multi-select; middle-click and drag to navigate");
Chris@189:             }                
Chris@189: 
Chris@189:             if (pos) {
Chris@189:                 bool closeToLeft = false, closeToRight = false;
Chris@189:                 Selection selection = getSelectionAt(pos->x(), closeToLeft, closeToRight);
Chris@189:                 if ((closeToLeft || closeToRight) && !(closeToLeft && closeToRight)) {
Chris@189:                     
Chris@189:                     help = tr("Click and drag to move the selection boundary");
Chris@189:                 }
Chris@189:             }
Chris@189:         } else {
Chris@189:             if (editable) {
Chris@189:                 help = tr("Click and drag to select a range; hold Shift to avoid snapping to items; middle-click to navigate");
Chris@189:             } else {
Chris@189:                 help = tr("Click and drag to select a range; middle-click and drag to navigate");
Chris@189:             }
Chris@189:         }
Chris@189: 
Chris@189:     } else if (mode == ViewManager::DrawMode) {
Chris@189:         
Chris@189:         //!!! could call through to a layer function to find out exact meaning
Chris@189: 	if (editable) {
Chris@189:             help = tr("Click to add a new item in the active layer");
Chris@189:         }
Chris@335: 
Chris@335:     } else if (mode == ViewManager::EraseMode) {
Chris@335:         
Chris@335:         //!!! could call through to a layer function to find out exact meaning
Chris@335: 	if (editable) {
Chris@335:             help = tr("Click to erase an item from the active layer");
Chris@335:         }
Chris@189:         
Chris@189:     } else if (mode == ViewManager::EditMode) {
Chris@189:         
Chris@189:         //!!! could call through to layer
Chris@189: 	if (editable) {
Chris@189:             help = tr("Click and drag an item in the active layer to move it");
Chris@189:             if (pos) {
Chris@189:                 bool closeToLeft = false, closeToRight = false;
Chris@189:                 Selection selection = getSelectionAt(pos->x(), closeToLeft, closeToRight);
Chris@189:                 if (!selection.isEmpty()) {
Chris@189:                     help = tr("Click and drag to move all items in the selected range");
Chris@189:                 }
Chris@189:             }
Chris@189:         }
Chris@189:     }
Chris@189: 
Chris@189:     emit contextHelpChanged(help);
Chris@189: }
Chris@189: 
Chris@189: void
Chris@189: Pane::mouseEnteredWidget()
Chris@189: {
Chris@189:     QWidget *w = dynamic_cast<QWidget *>(sender());
Chris@189:     if (!w) return;
Chris@189: 
Chris@189:     if (w == m_vpan) {
Chris@189:         emit contextHelpChanged(tr("Click and drag to adjust the visible range of the vertical scale"));
Chris@189:     } else if (w == m_vthumb) {
Chris@189:         emit contextHelpChanged(tr("Click and drag to adjust the vertical zoom level"));
Chris@189:     } else if (w == m_hthumb) {
Chris@189:         emit contextHelpChanged(tr("Click and drag to adjust the horizontal zoom level"));
Chris@189:     } else if (w == m_reset) {
Chris@189:         emit contextHelpChanged(tr("Reset horizontal and vertical zoom levels to their defaults"));
Chris@189:     }
Chris@189: }
Chris@189: 
Chris@189: void
Chris@189: Pane::mouseLeftWidget()
Chris@189: {
Chris@189:     emit contextHelpChanged("");
Chris@189: }
Chris@189: 
Chris@316: void
Chris@316: Pane::toXml(QTextStream &stream,
Chris@316:             QString indent, QString extraAttributes) const
Chris@127: {
Chris@316:     View::toXml
Chris@316:         (stream, 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: