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
 
@@ -160,125 +160,139 @@ bool ScalePicker::eventFilter(QObject* o
 
        pickerOverlay->updateOverlay();
 
        return true;
 
    }
 
    else
 
    {
 
        return QObject::eventFilter(object, event);
 
    }
 
}
 

	
 
const int TEXT_MARGIN = 4;
 

	
 
void ScalePicker::drawPlotOverlay(QPainter* painter)
 
{
 
    const double FILL_ALPHA = 0.2;
 

	
 
    painter->save();
 
    painter->setPen(_pen);
 

	
 
    if (started)
 
    {
 
        QColor color = _pen.color();
 
        color.setAlphaF(FILL_ALPHA);
 
        painter->setBrush(color);
 

	
 
        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;
 
        }
 
        else if (left + textSize.width() + TEXT_MARGIN > canvasWidth)
 
        {
 
            left = std::min(pickRect.right(), canvasWidth) - (textSize.width() + TEXT_MARGIN);
 
        }
 

	
 
        if (_scaleWidget->alignment() == QwtScaleDraw::BottomScale)
 
        {
 
            int canvasHeight = painter->device()->height();
 
            topLeft = QPointF(left, canvasHeight - textSize.height());
 
        }
 
        else                // top scale
 
        {
 
            topLeft = QPointF(left, 0);
 
        }
 
    }
 
    else                        // left/right scale
 
    {
 
        int canvasHeight = painter->device()->height();
 

	
 
        int top = 0;
 
        if (pickLength > 0)
 
        {
 
            top = pickRect.bottom();
 
        }
 
@@ -344,76 +358,76 @@ void ScalePicker::drawTriangle(QPainter*
 
            {0, 0},
 
            {-trsize/tan60 , trsize},
 
            {trsize/tan60 , trsize}
 
        };
 

	
 
    painter->save();
 
    painter->setPen(Qt::NoPen);
 
    painter->setBrush(_scaleWidget->palette().windowText());
 
    painter->setRenderHint(QPainter::Antialiasing);
 

	
 
    painter->translate(position, MARGIN);
 
    painter->drawPolygon(points, TRIANGLE_NUM_POINTS);
 

	
 
    painter->restore();
 
}
 

	
 
void ScalePicker::setPen(QPen pen)
 
{
 
    _pen = pen;
 
}
 

	
 
// convert the position of the click to the plot coordinates
 
double ScalePicker::position(double posPx)
 
{
 
    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
 
        snapPoints << round(_scaleWidget->scaleDraw()->scaleMap().transform(t));
 
    }
 
}
src/scalepicker.h
Show inline comments
 
@@ -16,58 +16,60 @@
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef SCALEPICKER_H
 
#define SCALEPICKER_H
 

	
 
#include <QObject>
 
#include <QMouseEvent>
 
#include <QPen>
 
#include <QWidget>
 
#include <QList>
 
#include <qwt_scale_widget.h>
 
#include <qwt_widget_overlay.h>
 

	
 
class ScalePicker : public QObject
 
{
 
    Q_OBJECT
 

	
 
public:
 
    ScalePicker(QwtScaleWidget* scaleWidget, QWidget* canvas);
 
    virtual bool eventFilter(QObject*, QEvent*);
 

	
 
    void drawPlotOverlay(QPainter*); // called from ScalePickerOverlay
 
    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)