Files @ e14b76864ac2
Branch filter:

Location: tempo-plotter/src/scalepicker.cpp

Hasan Yavuz ÖZDERYA
added gain and offset columns to channelinfomodel
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
  Copyright © 2017 Hasan Yavuz Özderya

  This file is part of serialplot.

  serialplot is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  serialplot is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
#include <qwt_scale_widget.h>
#include <qwt_scale_map.h>
#include <qwt_scale_div.h>
#include <qwt_text.h>
#include <math.h>

#include "scalepicker.h"

// minimum size for pick (in pixels)
#define MIN_PICK_SIZE (2)

#define SNAP_DISTANCE (5)

class PlotOverlay : public QwtWidgetOverlay
{
public:
    PlotOverlay(QWidget* widget, ScalePicker* picker);

protected:
    virtual void drawOverlay(QPainter*) const;

private:
    ScalePicker* _picker;
};

PlotOverlay::PlotOverlay(QWidget* widget, ScalePicker* picker) :
    QwtWidgetOverlay(widget)
{
    _picker = picker;
}

void PlotOverlay::drawOverlay(QPainter* painter) const
{
    _picker->drawPlotOverlay(painter);
}

class ScaleOverlay : public QwtWidgetOverlay
{
public:
    ScaleOverlay(QWidget* widget, ScalePicker* picker);

protected:
    virtual void drawOverlay(QPainter*) const;

private:
    ScalePicker* _picker;
};

ScaleOverlay::ScaleOverlay(QWidget* widget, ScalePicker* picker) :
    QwtWidgetOverlay(widget)
{
    _picker = picker;
}

void ScaleOverlay::drawOverlay(QPainter* painter) const
{
    _picker->drawScaleOverlay(painter);
}

ScalePicker::ScalePicker(QwtScaleWidget* scaleWidget, QWidget* canvas) :
    QObject(scaleWidget)
{
    _scaleWidget = scaleWidget;
    _canvas = canvas;
    scaleWidget->installEventFilter(this);
    scaleWidget->setMouseTracking(true);
    pickerOverlay = new PlotOverlay(canvas, this);
    scaleOverlay = new ScaleOverlay(scaleWidget, this);
    started = false;
    pressed = false;
}

bool ScalePicker::eventFilter(QObject* object, QEvent* event)
{
    if (event->type() == QEvent::MouseButtonPress ||
        event->type() == QEvent::MouseButtonRelease ||
        event->type() == QEvent::MouseMove)
    {
        updateSnapPoints();

        QMouseEvent* mouseEvent = (QMouseEvent*) event;
        int posPx = this->positionPx(mouseEvent);

        // do snapping unless Shift is pressed
        if (! (mouseEvent->modifiers() & Qt::ShiftModifier))
        {
            for (auto sp : snapPoints)
            {
                if (std::abs(posPx-sp) <= SNAP_DISTANCE)
                {
                    posPx = sp;
                    break;
                }
            }
        }

        double pos = this->position(posPx);
        currentPosPx = posPx;

        if (event->type() == QEvent::MouseButtonPress &&
            mouseEvent->button() == Qt::LeftButton)
        {
            pressed = true; // not yet started
            firstPos = pos;
            firstPosPx = posPx;
        }
        else if (event->type() == QEvent::MouseMove)
        {
            // make sure pick size is big enough, so that just
            // clicking won't trigger pick
            if (!started && pressed && (fabs(posPx-firstPosPx) > MIN_PICK_SIZE))
            {
                started = true;
            }
            pickerOverlay->updateOverlay();
            scaleOverlay->updateOverlay();
        }
        else // event->type() == QEvent::MouseButtonRelease
        {
            pressed = false;
            if (started)
            {
                // finalize
                started = false;
                if (firstPos != pos) // ignore 0 width zoom
                {
                    emit picked(firstPos, pos);
                }
            }
            pickerOverlay->updateOverlay();
            scaleOverlay->updateOverlay();
        }
        return true;
    }
    else if (event->type() == QEvent::Leave)
    {
        scaleOverlay->updateOverlay();
        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 = trackerText();
        auto tSize = text.textSize(painter->font());

        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
        QwtText text = trackerText();
        auto tsize = text.textSize(painter->font());
        text.draw(painter, trackerTextRect(painter, currentPosPx, tsize));
    }
    painter->restore();
}

QwtText ScalePicker::trackerText() const
{
    double pos;
    // use stored value if snapped to restore precision
    if (snapPointMap.contains(currentPosPx))
    {
        pos = snapPointMap[currentPosPx];
    }
    else
    {
        pos = position(currentPosPx);
    }

    return QwtText(QString("%1").arg(pos));
}

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();
        }
        else
        {
            top = pickRect.bottom() - textSize.height();
        }

        // make sure text is not off the canvas
        if (top < 0)
        {
            top = std::max(0, pickRect.bottom());
        }
        else if (top + textSize.height() > canvasHeight)
        {
            top = std::min(canvasHeight, pickRect.bottom()) - textSize.height();
        }

        if (_scaleWidget->alignment() == QwtScaleDraw::LeftScale)
        {
            topLeft = QPointF(TEXT_MARGIN, top);
        }
        else                    // right scale
        {
            int canvasWidth = painter->device()->width();
            topLeft = QPointF(canvasWidth - textSize.width() - TEXT_MARGIN, top);
        }
    }
    return QRectF(topLeft, textSize);
}

void ScalePicker::drawScaleOverlay(QPainter* painter)
{
    painter->save();

    // rotate & adjust coordinate system for vertical drawing
    if (_scaleWidget->alignment() == QwtScaleDraw::LeftScale ||
        _scaleWidget->alignment() == QwtScaleDraw::RightScale) // vertical
    {
        int width = painter->device()->width();
        painter->rotate(90);
        painter->translate(0, -width);
    }

    // draw the indicators
    if (started) drawTriangle(painter, firstPosPx);
    if (started || _scaleWidget->underMouse())
    {
        drawTriangle(painter, currentPosPx);
    }

    painter->restore();
}

void ScalePicker::drawTriangle(QPainter* painter, int position)
{
    const double tan60 = 1.732;
    const double trsize = 10;
    const int TRIANGLE_NUM_POINTS = 3;
    const int MARGIN = 2;
    const QPointF points[TRIANGLE_NUM_POINTS] =
        {
            {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) const
{
    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) 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();
    snapPointMap.clear();
    for(auto t : allTicks)
    {
        // `round` is used because `allTicks` is double but `snapPoints` is int
        int p = round(_scaleWidget->scaleDraw()->scaleMap().transform(t));
        snapPoints << p;
        snapPointMap[p] = t;
    }
}