Changeset - 2b6c00b1d12e
[Not reviewed]
default
0 2 0
Hasan Yavuz Ă–ZDERYA - 9 years ago 2017-02-25 10:23:12
hy@ozderya.net
keep tracker text contained in canvas
2 files changed with 42 insertions and 26 deletions:
0 comments (0 inline, 0 general)
src/scalepicker.cpp
Show inline comments
 
@@ -184,77 +184,91 @@ void ScalePicker::drawPlotOverlay(QPaint
 
        QRect rect;
 
        QwtText text(QString("%1").arg(position(currentPosPx)));
 
        auto tSize = text.textSize(painter->font());
 
        QPointF tTopLeft;
 

	
 
        if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale ||
 
            _scaleWidget->alignment() == QwtScaleDraw::TopScale)
 
        {
 
            int canvasHeight = painter->device()->height();
 
            int pickWidth = currentPosPx-firstPosPx;
 
            rect = QRect(posCanvasPx(firstPosPx), 0, pickWidth, canvasHeight);
 
        }
 
        else // vertical
 
        {
 
            int canvasWidth = painter->device()->width();
 
            int pickHeight = currentPosPx-firstPosPx;
 
            rect = QRect(0, posCanvasPx(firstPosPx), canvasWidth, pickHeight);
 
        }
 
        painter->drawRect(rect);
 
        text.draw(painter, pickTrackerTextRect(painter, rect, tSize));
 
    }
 
    else if (_scaleWidget->underMouse())
 
    {
 
        // draw tracker text centered on cursor
 
        int canvasPosPx = posCanvasPx(currentPosPx);
 
        QwtText text(QString("%1").arg(position(currentPosPx)));
 
        auto tsize = text.textSize(painter->font());
 
        QPointF topLeft;
 

	
 
        if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale)
 
        {
 
            int height = painter->device()->height();
 
            topLeft = QPointF(canvasPosPx-tsize.width()/2, height-tsize.height());
 
        }
 
        else if (_scaleWidget->alignment() == QwtScaleDraw::TopScale)
 
        {
 
            topLeft = QPointF(canvasPosPx-tsize.width()/2, 0);
 
        }
 
        else if (_scaleWidget->alignment() == QwtScaleDraw::LeftScale)
 
        {
 
            topLeft = QPointF(TEXT_MARGIN, canvasPosPx-tsize.height()/2);
 
        }
 
        else                    // right scale
 
        {
 
            int width = painter->device()->width();
 
            topLeft = QPointF(width-tsize.width()-TEXT_MARGIN, canvasPosPx-tsize.height()/2);
 
        }
 
        text.draw(painter, QRectF(topLeft, tsize));
 
        text.draw(painter, trackerTextRect(painter, currentPosPx, tsize));
 
    }
 
    painter->restore();
 
}
 

	
 
QRectF ScalePicker::pickTrackerTextRect(QPainter* painter, QRect pickRect, QSizeF textSize)
 
QRectF ScalePicker::trackerTextRect(QPainter* painter, int posPx, QSizeF textSize) const
 
{
 
    int canvasPosPx = posCanvasPx(posPx);
 
    QPointF topLeft;
 

	
 
    if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale ||
 
        _scaleWidget->alignment() == QwtScaleDraw::TopScale)
 
    {
 
        int left = canvasPosPx - textSize.width() / 2;
 
        int canvasWidth = painter->device()->width();
 
        left = std::max(TEXT_MARGIN, left);
 
        left = std::min(double(left), canvasWidth - textSize.width() - TEXT_MARGIN);
 
        int top = 0;
 
        if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale)
 
        {
 
            top = painter->device()->height() - textSize.height();
 
        }
 
        topLeft = QPointF(left, top);
 
    }
 
    else                        // left/right scales
 
    {
 
        int top = canvasPosPx-textSize.height() / 2;
 
        int canvasHeight = painter->device()->height();
 
        top = std::max(0, top);
 
        top = std::min(double(top), canvasHeight - textSize.height());
 
        int left = TEXT_MARGIN;
 
        if (_scaleWidget->alignment() == QwtScaleDraw::RightScale)
 
        {
 
            left = painter->device()->width() - textSize.width();
 
        }
 
        topLeft = QPointF(left, top);
 
    }
 
    return QRectF(topLeft, textSize);
 
}
 

	
 
QRectF ScalePicker::pickTrackerTextRect(QPainter* painter, QRect pickRect, QSizeF textSize) const
 
{
 
    qreal left = 0;
 
    int pickLength = currentPosPx - firstPosPx;
 
    QPointF topLeft;
 

	
 
    if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale ||
 
        _scaleWidget->alignment() == QwtScaleDraw::TopScale)
 
    {
 
        int canvasWidth = painter->device()->width();
 

	
 
        if (pickLength > 0)
 
        {
 
            left = pickRect.right() + TEXT_MARGIN;
 
        }
 
        else
 
        {
 
            left = pickRect.right() - (textSize.width() + TEXT_MARGIN);
 
        }
 

	
 
        // make sure text is not off the canvas
 
        if (left < TEXT_MARGIN)
 
        {
 
            left = std::max(0, pickRect.right()) + TEXT_MARGIN;
 
        }
 
@@ -368,49 +382,49 @@ double ScalePicker::position(double posP
 
    return _scaleWidget->scaleDraw()->scaleMap().invTransform(posPx);
 
}
 

	
 
int ScalePicker::positionPx(QMouseEvent* mouseEvent)
 
{
 
    double pos;
 
    if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale ||
 
        _scaleWidget->alignment() == QwtScaleDraw::TopScale)
 
    {
 
        pos = mouseEvent->pos().x();
 
    }
 
    else // left or right scale
 
    {
 
        pos = mouseEvent->pos().y();
 
    }
 
    return pos;
 
}
 

	
 
/*
 
 * Scale widget and canvas widget is not always aligned. Especially
 
 * when zooming scaleWidget moves around. This causes irregularities
 
 * when drawing the tracker lines. This function maps scale widgets
 
 * pixel coordinate to canvas' coordinate.
 
 */
 
double ScalePicker::posCanvasPx(double pos)
 
double ScalePicker::posCanvasPx(double pos) const
 
{
 
    // assumption: scale.width < canvas.width && scale.x > canvas.x
 
    if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale ||
 
        _scaleWidget->alignment() == QwtScaleDraw::TopScale)
 
    {
 
        return pos + (_scaleWidget->x() - _canvas->x());
 
    }
 
    else // left or right scale
 
    {
 
        return pos + (_scaleWidget->y() - _canvas->y());
 
    }
 
    return pos;
 
}
 

	
 
void ScalePicker::updateSnapPoints()
 
{
 
    auto allTicks = _scaleWidget->scaleDraw()->scaleDiv().ticks(QwtScaleDiv::MajorTick) +
 
        _scaleWidget->scaleDraw()->scaleDiv().ticks(QwtScaleDiv::MediumTick) +
 
        _scaleWidget->scaleDraw()->scaleDiv().ticks(QwtScaleDiv::MinorTick);
 

	
 
    snapPoints.clear();
 
    for(auto t : allTicks)
 
    {
 
        // `round` is used because `allTicks` is double but `snapPoints` is int
src/scalepicker.h
Show inline comments
 
@@ -40,34 +40,36 @@ public:
 
    void drawScaleOverlay(QPainter*); // called from ScaleOverlay
 
    void setPen(QPen pen);
 

	
 
signals:
 
    void pickStarted(double pos);
 
    void picking(double firstPos, double lastPos);
 
    void picked(double firstPos, double lastPos);
 

	
 
private:
 
    QwtScaleWidget* _scaleWidget;
 
    QWidget* _canvas;
 
    QwtWidgetOverlay* pickerOverlay; // will be PlotOverlay
 
    QwtWidgetOverlay* scaleOverlay;  // will be ScaleOverlay
 
    QPen _pen;
 

	
 
    bool pressed;
 
    bool started;
 
    double firstPos; // converted to plot coordinates
 
    double firstPosPx; // pixel coordinates
 
    double currentPosPx; // current position in pixel coordinates
 
    QList<int> snapPoints;
 

	
 
    double position(double); // returns the axis mouse position relative to plot coordinates
 
    int positionPx(QMouseEvent*); // returns the axis mouse position in pixels
 
    double posCanvasPx(double pos); // returns the given position in canvas coordinates
 
    double posCanvasPx(double pos) const; // returns the given position in canvas coordinates
 
    void drawTriangle(QPainter* painter, int position);
 

	
 
private slots:
 
    /// Returns tracker text position
 
    QRectF trackerTextRect(QPainter* painter, int posPx, QSizeF textSize) const;
 
    /// Returns the text position for tracker text shown during picking
 
    QRectF pickTrackerTextRect(QPainter* painter, QRect pickRect, QSizeF textSize);
 
    QRectF pickTrackerTextRect(QPainter* painter, QRect pickRect, QSizeF textSize) const;
 
    void updateSnapPoints();
 
};
 

	
 
#endif // SCALEPICKER_H
0 comments (0 inline, 0 general)