viewport.cpp 27 KB
Newer Older
DreamSourceLab's avatar
DreamSourceLab committed
1
/*
DreamSourceLab's avatar
DreamSourceLab committed
2 3
 * This file is part of the DSView project.
 * DSView is based on PulseView.
DreamSourceLab's avatar
DreamSourceLab committed
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
 *
 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
 * Copyright (C) 2013 DreamSourceLab <dreamsourcelab@dreamsourcelab.com>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */


#include "view.h"
#include "viewport.h"
#include "ruler.h"

#include "signal.h"
DreamSourceLab's avatar
DreamSourceLab committed
29
#include "dsosignal.h"
30
#include "logicsignal.h"
DreamSourceLab's avatar
DreamSourceLab committed
31
#include "../device/devinst.h"
DreamSourceLab's avatar
DreamSourceLab committed
32 33 34 35 36 37 38
#include "../data/logic.h"
#include "../data/logicsnapshot.h"
#include "../sigsession.h"

#include <QMouseEvent>
#include <QStyleOption>

39 40
#include <math.h>

DreamSourceLab's avatar
DreamSourceLab committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <boost/foreach.hpp>

using namespace boost;
using namespace std;

namespace pv {
namespace view {

Viewport::Viewport(View &parent) :
	QWidget(&parent),
        _view(parent),
    _total_receive_len(0),
    _zoom_rect_visible(false),
    _measure_shown(false),
55
    _measure_type(LOGIC),
DreamSourceLab's avatar
DreamSourceLab committed
56 57 58 59
    _cur_sample(0),
    _nxt_sample(1),
    _cur_preX(0),
    _cur_aftX(1),
60 61 62
    _cur_midY(0),
    _hover_index(0),
    _hover_hit(false)
DreamSourceLab's avatar
DreamSourceLab committed
63 64 65 66 67 68 69 70 71
{
	setMouseTracking(true);
	setAutoFillBackground(true);
	setBackgroundRole(QPalette::Base);

    //setFixedSize(QSize(600, 400));
    _mm_width = "#####";
    _mm_period = "#####";
    _mm_freq = "#####";
72
    _mm_duty = "#####";
DreamSourceLab's avatar
DreamSourceLab committed
73 74 75 76
    _measure_en = true;
    triggered = false;
    timer_cnt = 0;

DreamSourceLab's avatar
DreamSourceLab committed
77 78
    connect(&_view, SIGNAL(traces_moved()),
        this, SLOT(on_traces_moved()));
DreamSourceLab's avatar
DreamSourceLab committed
79 80
    connect(&trigger_timer, SIGNAL(timeout()),
            this, SLOT(on_trigger_timer()));
81 82 83

    connect(&_view.session(), &SigSession::receive_data,
            this, &Viewport::set_receive_len);
DreamSourceLab's avatar
DreamSourceLab committed
84 85 86 87 88
}

int Viewport::get_total_height() const
{
	int h = 0;
DreamSourceLab's avatar
DreamSourceLab committed
89

90 91
    const vector< boost::shared_ptr<Trace> > traces(_view.get_traces());
    BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces) {
DreamSourceLab's avatar
DreamSourceLab committed
92 93 94 95
        assert(t);
        h += (int)(t->get_signalHeight());
    }
    h += 2 * View::SignalMargin;
DreamSourceLab's avatar
DreamSourceLab committed
96 97 98 99

	return h;
}

DreamSourceLab's avatar
DreamSourceLab committed
100 101 102 103 104
QPoint Viewport::get_mouse_point() const
{
    return _mouse_point;
}

DreamSourceLab's avatar
DreamSourceLab committed
105 106 107 108 109
void Viewport::paintEvent(QPaintEvent *event)
{
    (void)event;

    using pv::view::Signal;
DreamSourceLab's avatar
DreamSourceLab committed
110

DreamSourceLab's avatar
DreamSourceLab committed
111 112 113 114 115
    QStyleOption o;
    o.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);

116 117
    const vector< boost::shared_ptr<Trace> > traces(_view.get_traces());
    BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
118 119
    {
        assert(t);
DreamSourceLab's avatar
DreamSourceLab committed
120
        t->paint_back(p, 0, _view.get_view_width());
DreamSourceLab's avatar
DreamSourceLab committed
121
    }
DreamSourceLab's avatar
DreamSourceLab committed
122

123
    p.setRenderHint(QPainter::Antialiasing, false);
DreamSourceLab's avatar
DreamSourceLab committed
124 125
    if (_view.session().get_device()->dev_inst()->mode == LOGIC ||
        _view.session().get_instant()) {
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
126 127 128 129 130 131 132 133 134
        switch(_view.session().get_capture_state()) {
        case SigSession::Init:
            break;

        case SigSession::Stopped:
            paintSignals(p);
            break;

        case SigSession::Running:
DreamSourceLab's avatar
DreamSourceLab committed
135
            p.setRenderHint(QPainter::Antialiasing);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
136
            paintProgress(p);
DreamSourceLab's avatar
DreamSourceLab committed
137
            p.setRenderHint(QPainter::Antialiasing, false);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
138 139
            break;
        }
DreamSourceLab's avatar
DreamSourceLab committed
140
    } else {
DreamSourceLab's avatar
DreamSourceLab committed
141 142 143
        paintSignals(p);
    }

144
    BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
145 146 147
    {
        assert(t);
        if (t->enabled())
DreamSourceLab's avatar
DreamSourceLab committed
148
            t->paint_fore(p, 0, _view.get_view_width());
DreamSourceLab's avatar
DreamSourceLab committed
149 150
    }

151
    //p.setRenderHint(QPainter::Antialiasing, false);
DreamSourceLab's avatar
DreamSourceLab committed
152 153
    if (_view.get_signalHeight() != _curSignalHeight)
            _curSignalHeight = _view.get_signalHeight();
DreamSourceLab's avatar
DreamSourceLab committed
154

DreamSourceLab's avatar
DreamSourceLab committed
155 156 157 158 159
	p.end();
}

void Viewport::paintSignals(QPainter &p)
{
160
    const vector< boost::shared_ptr<Trace> > traces(_view.get_traces());
DreamSourceLab's avatar
DreamSourceLab committed
161 162 163 164 165 166 167 168 169 170 171 172
    if (_view.scale() != _curScale ||
        _view.offset() != _curOffset ||
        _view.get_signalHeight() != _curSignalHeight ||
        _view.need_update()) {
        _curScale = _view.scale();
        _curOffset = _view.offset();
        _curSignalHeight = _view.get_signalHeight();

        pixmap = QPixmap(size());
        pixmap.fill(Qt::transparent);
        QPainter dbp(&pixmap);
        dbp.initFrom(this);
173
        //p.setRenderHint(QPainter::Antialiasing, false);
174
        BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
175 176 177
        {
            assert(t);
            if (t->enabled())
DreamSourceLab's avatar
DreamSourceLab committed
178
                t->paint_mid(dbp, 0, _view.get_view_width());
DreamSourceLab's avatar
DreamSourceLab committed
179
        }
DreamSourceLab's avatar
DreamSourceLab committed
180

DreamSourceLab's avatar
DreamSourceLab committed
181 182 183 184 185 186 187 188
        _view.set_need_update(false);
    }
    p.drawPixmap(0, 0, pixmap);

    // plot cursors
    if (_view.cursors_shown()) {
        list<Cursor*>::iterator i = _view.get_cursorList().begin();
        double cursorX;
189
        const double samples_per_pixel = _view.session().get_device()->get_sample_rate() * _view.scale();
DreamSourceLab's avatar
DreamSourceLab committed
190
        while (i != _view.get_cursorList().end()) {
191
            cursorX = (*i)->index()/samples_per_pixel - (_view.offset() / _view.scale());
DreamSourceLab's avatar
DreamSourceLab committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
            if (rect().contains(_view.hover_point().x(), _view.hover_point().y()) &&
                    qAbs(cursorX - _view.hover_point().x()) <= HitCursorMargin)
                (*i)->paint(p, rect(), 1);
            else
                (*i)->paint(p, rect(), 0);
            i++;
        }
    }
    if (_view.trig_cursor_shown()) {
        _view.get_trig_cursor()->paint(p, rect(), 0);
    }
    if (_view.search_cursor_shown()) {
        _view.get_search_cursor()->paint(p, rect(), 0);
    }

    // plot zoom rect
    if (_zoom_rect_visible) {
        p.setPen(Qt::NoPen);
DreamSourceLab's avatar
DreamSourceLab committed
210
        p.setBrush(Trace::dsLightBlue);
DreamSourceLab's avatar
DreamSourceLab committed
211 212 213 214 215 216 217 218 219 220 221 222 223
        p.drawRect(_zoom_rect);
    }

    //plot measure arrow
    if (_measure_shown) {
        paintMeasure(p);
    }
}

void Viewport::paintProgress(QPainter &p)
{
    using pv::view::Signal;

224
    const uint64_t _total_sample_len = _view.session().get_device()->get_sample_limit();
225
    double progress = -(_total_receive_len * 1.0 / _total_sample_len * 360 * 16);
DreamSourceLab's avatar
DreamSourceLab committed
226
    int captured_progress = 0;
DreamSourceLab's avatar
DreamSourceLab committed
227 228

    p.setPen(Qt::gray);
DreamSourceLab's avatar
DreamSourceLab committed
229 230 231
    p.setBrush(Qt::NoBrush);
    const QPoint cenPos = QPoint(_view.get_view_width() / 2, height() / 2);
    const int radius = min(0.3 * _view.get_view_width(), 0.3 * height());
DreamSourceLab's avatar
DreamSourceLab committed
232
    p.drawEllipse(cenPos, radius - 2, radius - 2);
DreamSourceLab's avatar
DreamSourceLab committed
233
    p.setPen(QPen(Trace::dsGreen, 4, Qt::SolidLine));
DreamSourceLab's avatar
DreamSourceLab committed
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
    p.drawArc(cenPos.x() - radius, cenPos.y() - radius, 2* radius, 2 * radius, 180 * 16, progress);

    p.setPen(Qt::gray);
    const QPoint logoPoints[] = {
        QPoint(cenPos.x() - 0.75 * radius, cenPos.y()),
        QPoint(cenPos.x() - 0.75 * radius, cenPos.y() + 0.15 * radius),
        QPoint(cenPos.x() - 0.6 * radius, cenPos.y()),
        QPoint(cenPos.x() - 0.6 * radius, cenPos.y() + 0.3 * radius),
        QPoint(cenPos.x() - 0.45 * radius, cenPos.y()),
        QPoint(cenPos.x() - 0.45 * radius, cenPos.y() + 0.45 * radius),
        QPoint(cenPos.x() - 0.3 * radius, cenPos.y()),
        QPoint(cenPos.x() - 0.3 * radius, cenPos.y() + 0.3 * radius),
        QPoint(cenPos.x() - 0.15 * radius, cenPos.y()),
        QPoint(cenPos.x() - 0.15 * radius, cenPos.y() + 0.15 * radius),
        QPoint(cenPos.x() + 0.15 * radius, cenPos.y()),
        QPoint(cenPos.x() + 0.15 * radius, cenPos.y() - 0.15 * radius),
        QPoint(cenPos.x() + 0.3 * radius, cenPos.y()),
        QPoint(cenPos.x() + 0.3 * radius, cenPos.y() - 0.3 * radius),
        QPoint(cenPos.x() + 0.45 * radius, cenPos.y()),
        QPoint(cenPos.x() + 0.45 * radius, cenPos.y() - 0.45 * radius),
        QPoint(cenPos.x() + 0.6 * radius, cenPos.y()),
        QPoint(cenPos.x() + 0.6 * radius, cenPos.y() - 0.3 * radius),
        QPoint(cenPos.x() + 0.75 * radius, cenPos.y()),
        QPoint(cenPos.x() + 0.75 * radius, cenPos.y() - 0.15 * radius)
    };
    const int logoRadius = 10;
    p.drawLine(logoPoints[0], logoPoints[1]);
    p.drawLine(logoPoints[2], logoPoints[3]);
    p.drawLine(logoPoints[4], logoPoints[5]);
    p.drawLine(logoPoints[6], logoPoints[7]);
    p.drawLine(logoPoints[8], logoPoints[9]);
    p.drawLine(logoPoints[10], logoPoints[11]);
    p.drawLine(logoPoints[12], logoPoints[13]);
    p.drawLine(logoPoints[14], logoPoints[15]);
    p.drawLine(logoPoints[16], logoPoints[17]);
    p.drawLine(logoPoints[18], logoPoints[19]);
    p.drawEllipse(logoPoints[1].x() - 0.5 * logoRadius, logoPoints[1].y(),
            logoRadius, logoRadius);
    p.drawEllipse(logoPoints[3].x() - 0.5 * logoRadius, logoPoints[3].y(),
            logoRadius, logoRadius);
    p.drawEllipse(logoPoints[5].x() - 0.5 * logoRadius, logoPoints[5].y(),
            logoRadius, logoRadius);
    p.drawEllipse(logoPoints[7].x() - 0.5 * logoRadius, logoPoints[7].y(),
            logoRadius, logoRadius);
    p.drawEllipse(logoPoints[9].x() - 0.5 * logoRadius, logoPoints[9].y(),
            logoRadius, logoRadius);
    p.drawEllipse(logoPoints[11].x() - 0.5 * logoRadius, logoPoints[11].y() - logoRadius,
            logoRadius, logoRadius);
    p.drawEllipse(logoPoints[13].x() - 0.5 * logoRadius, logoPoints[13].y() - logoRadius,
            logoRadius, logoRadius);
    p.drawEllipse(logoPoints[15].x() - 0.5 * logoRadius, logoPoints[15].y() - logoRadius,
            logoRadius, logoRadius);
    p.drawEllipse(logoPoints[17].x() - 0.5 * logoRadius, logoPoints[17].y() - logoRadius,
            logoRadius, logoRadius);
    p.drawEllipse(logoPoints[19].x() - 0.5 * logoRadius, logoPoints[19].y() - logoRadius,
            logoRadius, logoRadius);

    if (!triggered) {
DreamSourceLab's avatar
DreamSourceLab committed
292 293 294 295
        const int width = _view.get_view_width();
        const QPoint cenLeftPos = QPoint(width / 2 - 0.05 * width, height() / 2);
        const QPoint cenRightPos = QPoint(width / 2 + 0.05 * width, height() / 2);
        const int trigger_radius = min(0.02 * width, 0.02 * height());
DreamSourceLab's avatar
DreamSourceLab committed
296 297

        p.setPen(Qt::NoPen);
DreamSourceLab's avatar
DreamSourceLab committed
298
        p.setBrush((timer_cnt % 3) == 0 ? Trace::dsLightBlue : Trace::dsGray);
DreamSourceLab's avatar
DreamSourceLab committed
299
        p.drawEllipse(cenLeftPos, trigger_radius, trigger_radius);
DreamSourceLab's avatar
DreamSourceLab committed
300
        p.setBrush((timer_cnt % 3) == 1 ? Trace::dsLightBlue : Trace::dsGray);
DreamSourceLab's avatar
DreamSourceLab committed
301
        p.drawEllipse(cenPos, trigger_radius, trigger_radius);
DreamSourceLab's avatar
DreamSourceLab committed
302
        p.setBrush((timer_cnt % 3) == 2 ? Trace::dsLightBlue : Trace::dsGray);
DreamSourceLab's avatar
DreamSourceLab committed
303
        p.drawEllipse(cenRightPos, trigger_radius, trigger_radius);
DreamSourceLab's avatar
DreamSourceLab committed
304 305

        sr_status status;
DreamSourceLab's avatar
DreamSourceLab committed
306
        if (sr_status_get(_view.session().get_device()->dev_inst(), &status, SR_STATUS_TRIG_BEGIN, SR_STATUS_TRIG_END) == SR_OK){
DreamSourceLab's avatar
DreamSourceLab committed
307 308 309 310 311
            const bool triggred = status.trig_hit & 0x01;
            const uint32_t captured_cnt = (status.captured_cnt0 +
                                          (status.captured_cnt1 << 8) +
                                          (status.captured_cnt2 << 16) +
                                          (status.captured_cnt3 << 24));
312 313 314 315
            if (triggred)
                captured_progress = (_total_sample_len - captured_cnt) * 100.0 / _total_sample_len;
            else
                captured_progress = captured_cnt * 100.0 / _total_sample_len;
DreamSourceLab's avatar
DreamSourceLab committed
316 317 318 319 320 321 322 323 324 325 326


            p.setPen(Trace::dsLightBlue);
            QFont font=p.font();
            font.setPointSize(10);
            font.setBold(true);
            p.setFont(font);
            QRect status_rect = QRect(cenPos.x() - radius, cenPos.y() + radius * 0.4, radius * 2, radius * 0.5);
            if (triggred)
                p.drawText(status_rect,
                           Qt::AlignCenter | Qt::AlignVCenter,
327
                           "Triggered! " + QString::number(captured_progress)+"% Captured");
DreamSourceLab's avatar
DreamSourceLab committed
328 329 330 331 332 333
            else
                p.drawText(status_rect,
                           Qt::AlignCenter | Qt::AlignVCenter,
                           "Waiting for Trigger! " + QString::number(captured_progress)+"% Captured");
        }

DreamSourceLab's avatar
DreamSourceLab committed
334 335
    } else {
        const int progress100 = ceil(progress / -3.6 / 16);
DreamSourceLab's avatar
DreamSourceLab committed
336
        p.setPen(Trace::dsGreen);
DreamSourceLab's avatar
DreamSourceLab committed
337 338 339 340 341 342
        QFont font=p.font();
        font.setPointSize(50);
        font.setBold(true);
        p.setFont(font);
        p.drawText(rect(), Qt::AlignCenter | Qt::AlignVCenter, QString::number(progress100)+"%");
    }
DreamSourceLab's avatar
DreamSourceLab committed
343 344 345 346 347

    p.setPen(QPen(Trace::dsLightBlue, 4, Qt::SolidLine));
    const int int_radius = max(radius - 4, 0);
    p.drawArc(cenPos.x() - int_radius, cenPos.y() - int_radius, 2* int_radius, 2 * int_radius, 180 * 16, -captured_progress*3.6*16);

DreamSourceLab's avatar
DreamSourceLab committed
348 349 350 351 352 353 354 355
}

void Viewport::mousePressEvent(QMouseEvent *event)
{
	assert(event);

	_mouse_down_point = event->pos();
	_mouse_down_offset = _view.offset();
DreamSourceLab's avatar
DreamSourceLab committed
356
    _measure_shown = false;
DreamSourceLab's avatar
DreamSourceLab committed
357 358 359 360 361

    if (event->buttons() & Qt::LeftButton) {
        if (_view.cursors_shown()) {
            list<Cursor*>::iterator i = _view.get_cursorList().begin();
            double cursorX;
362
            const double samples_per_pixel = _view.session().get_device()->get_sample_rate() * _view.scale();
DreamSourceLab's avatar
DreamSourceLab committed
363
            while (i != _view.get_cursorList().end()) {
364
                cursorX = (*i)->index()/samples_per_pixel - (_view.offset() / _view.scale());
DreamSourceLab's avatar
DreamSourceLab committed
365 366 367 368 369 370 371 372 373 374
                if ((*i)->grabbed())
                    _view.get_ruler()->rel_grabbed_cursor();
                else if (qAbs(cursorX - event->pos().x()) <= HitCursorMargin) {
                    _view.get_ruler()->set_grabbed_cursor(*i);
                    break;
                }
                i++;
            }

        }
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
375

376 377
        const vector< boost::shared_ptr<Signal> > sigs(_view.session().get_signals());
        BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
378
            assert(s);
379
            boost::shared_ptr<DsoSignal> dsoSig;
DreamSourceLab's avatar
DreamSourceLab committed
380
            if ((dsoSig = dynamic_pointer_cast<DsoSignal>(s)) &&
DreamSourceLab's avatar
DreamSourceLab committed
381
                 dsoSig->get_trig_rect(0, _view.get_view_width()).contains(_mouse_point)) {
DreamSourceLab's avatar
DreamSourceLab committed
382
                _drag_sig = s;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
383 384 385
                break;
            }
        }
DreamSourceLab's avatar
DreamSourceLab committed
386

DreamSourceLab's avatar
DreamSourceLab committed
387 388 389 390 391 392 393
        update();
    }
}

void Viewport::mouseMoveEvent(QMouseEvent *event)
{
	assert(event);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
394
    _mouse_point = event->pos();
395
    _hover_hit = false;
DreamSourceLab's avatar
DreamSourceLab committed
396 397 398 399 400 401
    if (event->buttons() & Qt::RightButton) {
        _zoom_rect = QRectF(_mouse_down_point, event->pos());
        _zoom_rect_visible = true;
	}

    if (event->buttons() & Qt::LeftButton) {
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
402
        if (_drag_sig) {
403
            boost::shared_ptr<view::DsoSignal> dsoSig;
DreamSourceLab's avatar
DreamSourceLab committed
404 405 406 407 408 409 410 411
            if (dsoSig = dynamic_pointer_cast<view::DsoSignal>(_drag_sig))
                dsoSig->set_trig_vpos(_mouse_point.y());
        } else {
            _view.set_scale_offset(_view.scale(),
                _mouse_down_offset +
                (_mouse_down_point - event->pos()).x() *
                _view.scale());
            measure();
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
412
        }
DreamSourceLab's avatar
DreamSourceLab committed
413
    }
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
414

DreamSourceLab's avatar
DreamSourceLab committed
415 416
    if (!(event->buttons() || Qt::NoButton)) {
        uint64_t sample_rate = _view.session().get_device()->get_sample_rate();
DreamSourceLab's avatar
DreamSourceLab committed
417 418
        TimeMarker* grabbed_marker = _view.get_ruler()->get_grabbed_cursor();
        if (_view.cursors_shown() && grabbed_marker) {
DreamSourceLab's avatar
DreamSourceLab committed
419 420
            const double cur_time = _view.offset() + _view.hover_point().x() * _view.scale();
            const double pos = cur_time * sample_rate;
421
            const double pos_delta = pos - (uint64_t)pos;
422
            if ( pos_delta < 0.5)
423
                grabbed_marker->set_index((uint64_t)floor(pos));
DreamSourceLab's avatar
DreamSourceLab committed
424
            else
425
                grabbed_marker->set_index((uint64_t)ceil(pos));
DreamSourceLab's avatar
DreamSourceLab committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439
        }
        measure();
    }

    update();
}

void Viewport::mouseReleaseEvent(QMouseEvent *event)
{
    assert(event);

    if (_zoom_rect_visible) {
        _zoom_rect_visible = false;
        const double newOffset = _view.offset() + (min(event->pos().x(), _mouse_down_point.x()) + 0.5) * _view.scale();
DreamSourceLab's avatar
DreamSourceLab committed
440
        const double newScale = max(min(_view.scale() * abs(event->pos().x() - _mouse_down_point.x()) / _view.get_view_width(),
DreamSourceLab's avatar
DreamSourceLab committed
441 442 443 444 445
                                        _view.get_maxscale()), _view.get_minscale());
        if (newScale != _view.scale())
            _view.set_scale_offset(newScale, newOffset);
    }

DreamSourceLab's avatar
DreamSourceLab committed
446 447 448
    if(_drag_sig)
        _drag_sig.reset();

449 450 451 452 453 454 455

    if (_hover_hit){
        _view.add_cursor(view::Ruler::CursorColor[_view.get_cursorList().size() % 8], _hover_index);
        _view.show_cursors(true);
        _hover_hit = false;
    }

DreamSourceLab's avatar
DreamSourceLab committed
456 457 458 459 460 461
    update();
}

void Viewport::mouseDoubleClickEvent(QMouseEvent *event)
{
    assert (event);
DreamSourceLab's avatar
DreamSourceLab committed
462 463
    (void)event;

DreamSourceLab's avatar
DreamSourceLab committed
464 465 466 467 468
    if (_view.session().get_device()->dev_inst()->mode == LOGIC) {
        if (_view.scale() == _view.get_maxscale())
            _view.set_preScale_preOffset();
        else
            _view.set_scale_offset(_view.get_maxscale(), 0);
DreamSourceLab's avatar
DreamSourceLab committed
469

DreamSourceLab's avatar
DreamSourceLab committed
470 471
        update();
    }
DreamSourceLab's avatar
DreamSourceLab committed
472 473 474 475 476 477 478 479
}

void Viewport::wheelEvent(QWheelEvent *event)
{
	assert(event);

	if (event->orientation() == Qt::Vertical) {
		// Vertical scrolling is interpreted as zooming in/out
DreamSourceLab's avatar
DreamSourceLab committed
480
        const double offset = event->x();
DreamSourceLab's avatar
DreamSourceLab committed
481
        _view.zoom(event->delta() / 80, offset);
DreamSourceLab's avatar
DreamSourceLab committed
482 483 484 485 486 487 488 489 490 491 492 493 494
	} else if (event->orientation() == Qt::Horizontal) {
		// Horizontal scrolling is interpreted as moving left/right
		_view.set_scale_offset(_view.scale(),
				       event->delta() * _view.scale()
				       + _view.offset());
	}

    measure();
}

void Viewport::leaveEvent(QEvent *)
{
    _measure_shown = false;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
495
    _mouse_point = QPoint(-1, -1);
DreamSourceLab's avatar
DreamSourceLab committed
496 497 498 499
    //_view.show_cursors(false);
    update();
}

DreamSourceLab's avatar
DreamSourceLab committed
500
void Viewport::on_traces_moved()
DreamSourceLab's avatar
DreamSourceLab committed
501 502 503 504 505 506 507 508 509 510 511
{
	update();
}

void Viewport::set_receive_len(quint64 length)
{
    if (length == 0) {
        _total_receive_len = 0;
        start_trigger_timer(333);
    } else {
        stop_trigger_timer();
DreamSourceLab's avatar
DreamSourceLab committed
512 513
        if (_total_receive_len + length > _view.session().get_device()->get_sample_limit())
            _total_receive_len = _view.session().get_device()->get_sample_limit();
DreamSourceLab's avatar
DreamSourceLab committed
514 515 516 517 518 519 520 521
        else
            _total_receive_len += length;
    }
    update();
}

void Viewport::measure()
{
522 523
   if (_view.session().get_capture_state() == SigSession::Running)
       return;
524
   _measure_shown = false;
525 526 527 528
   const uint64_t sample_rate = _view.session().get_device()->get_sample_rate();
   const vector< boost::shared_ptr<Signal> > sigs(_view.session().get_signals());
   BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
       assert(s);
529
       boost::shared_ptr<view::LogicSignal> logicSig;
530
       boost::shared_ptr<view::DsoSignal> dsoSig;
531 532 533
       if (logicSig = dynamic_pointer_cast<view::LogicSignal>(s)) {
           if (logicSig->measure(_view.hover_point(), _cur_sample, _nxt_sample, _thd_sample)) {
               _measure_shown = true;
534
               _measure_type = LOGIC;
535 536 537 538 539 540 541 542 543 544 545 546

               _mm_width = _view.get_ruler()->format_real_time(_nxt_sample - _cur_sample, sample_rate);
               _mm_period = _thd_sample != 0 ? _view.get_ruler()->format_real_time(_thd_sample - _cur_sample, sample_rate) : "#####";
               _mm_freq = _thd_sample != 0 ? _view.get_ruler()->format_real_freq(_thd_sample - _cur_sample, sample_rate) : "#####";

               const double pixels_offset =  _view.offset() / _view.scale();
               const double samples_per_pixel = sample_rate * _view.scale();
               _cur_preX = _cur_sample / samples_per_pixel - pixels_offset;
               _cur_aftX = _nxt_sample / samples_per_pixel - pixels_offset;
               _cur_thdX = _thd_sample / samples_per_pixel - pixels_offset;
               _cur_midY = logicSig->get_y();

547
               _mm_duty = _thd_sample != 0 ? QString::number((_nxt_sample - _cur_sample) * 100.0 / (_thd_sample - _cur_sample), 'f', 2)+"%" :
548 549
                                             "#####";
               mouse_measure();
550
               break;
551 552 553 554 555 556 557
           } else {
               _mm_width = "#####";
               _mm_period = "#####";
               _mm_freq = "#####";
               _mm_duty = "#####";
           }
           mouse_measure();
558 559 560 561 562
       } else if (dsoSig = dynamic_pointer_cast<view::DsoSignal>(s)) {
           if (_measure_en && dsoSig->measure(_view.hover_point())) {
               _measure_shown = true;
               _measure_type = DSO;
           }
563
       }
DreamSourceLab's avatar
DreamSourceLab committed
564 565 566 567 568
    }
}

void Viewport::paintMeasure(QPainter &p)
{
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    _hover_hit = false;
    if (_measure_type == LOGIC) {
        p.setPen(QColor(17, 133, 209,  255));
        p.drawLine(QLineF(_cur_preX, _cur_midY, _cur_aftX, _cur_midY));
        p.drawLine(QLineF(_cur_preX, _cur_midY, _cur_preX + 2, _cur_midY - 2));
        p.drawLine(QLineF(_cur_preX, _cur_midY, _cur_preX + 2, _cur_midY + 2));
        p.drawLine(QLineF(_cur_aftX - 2, _cur_midY - 2, _cur_aftX, _cur_midY));
        p.drawLine(QLineF(_cur_aftX - 2, _cur_midY + 2, _cur_aftX, _cur_midY));
        if (_thd_sample != 0) {
            p.drawLine(QLineF(_cur_aftX, _cur_midY, _cur_thdX, _cur_midY));
            p.drawLine(QLineF(_cur_aftX, _cur_midY, _cur_aftX + 2, _cur_midY - 2));
            p.drawLine(QLineF(_cur_aftX, _cur_midY, _cur_aftX + 2, _cur_midY + 2));
            p.drawLine(QLineF(_cur_thdX - 2, _cur_midY - 2, _cur_thdX, _cur_midY));
            p.drawLine(QLineF(_cur_thdX - 2, _cur_midY + 2, _cur_thdX, _cur_midY));
        }
DreamSourceLab's avatar
DreamSourceLab committed
584

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
        if (_measure_en) {
            int typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
                Qt::AlignLeft | Qt::AlignTop, _mm_width).width();
            typical_width = max(typical_width, p.boundingRect(0, 0, INT_MAX, INT_MAX,
                Qt::AlignLeft | Qt::AlignTop, _mm_period).width());
            typical_width = max(typical_width, p.boundingRect(0, 0, INT_MAX, INT_MAX,
                Qt::AlignLeft | Qt::AlignTop, _mm_freq).width());
            typical_width = max(typical_width, p.boundingRect(0, 0, INT_MAX, INT_MAX,
                Qt::AlignLeft | Qt::AlignTop, _mm_duty).width());
            typical_width = typical_width + 100;

            const double width = _view.get_view_width();
            const double height = _view.viewport()->height();
            const double left = _view.hover_point().x();
            const double top = _view.hover_point().y();
            const double right = left + typical_width;
            const double bottom = top + 80;
            QPointF org_pos = QPointF(right > width ? left - typical_width : left, bottom > height ? top - 80 : top);
            QRectF measure_rect = QRectF(org_pos.x(), org_pos.y(), (double)typical_width, 80.0);
            QRectF measure1_rect = QRectF(org_pos.x(), org_pos.y(), (double)typical_width, 20.0);
            QRectF measure2_rect = QRectF(org_pos.x(), org_pos.y()+20, (double)typical_width, 20.0);
            QRectF measure3_rect = QRectF(org_pos.x(), org_pos.y()+40, (double)typical_width, 20.0);
            QRectF measure4_rect = QRectF(org_pos.x(), org_pos.y()+60, (double)typical_width, 20.0);

            p.setPen(Qt::NoPen);
            p.setBrush(QColor(17, 133, 209,  150));
            p.drawRect(measure_rect);

            p.setPen(Qt::black);
            p.drawText(measure1_rect, Qt::AlignRight | Qt::AlignVCenter,
DreamSourceLab's avatar
DreamSourceLab committed
615
                       tr("Width: ") + _mm_width);
616
            p.drawText(measure2_rect, Qt::AlignRight | Qt::AlignVCenter,
DreamSourceLab's avatar
DreamSourceLab committed
617
                       tr("Period: ") + _mm_period);
618
            p.drawText(measure3_rect, Qt::AlignRight | Qt::AlignVCenter,
DreamSourceLab's avatar
DreamSourceLab committed
619
                       tr("Frequency: ") + _mm_freq);
620
            p.drawText(measure4_rect, Qt::AlignRight | Qt::AlignVCenter,
DreamSourceLab's avatar
DreamSourceLab committed
621
                       tr("Duty Cycle: ") + _mm_duty);
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
        }
    } else if (_measure_type == DSO) {
        const vector< boost::shared_ptr<Signal> > sigs(_view.session().get_signals());
        BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
            boost::shared_ptr<view::DsoSignal> dsoSig;
            if (dsoSig = dynamic_pointer_cast<view::DsoSignal>(s)) {
                uint64_t index;
                double value;
                QPointF hpoint;
                const int arrow_size = 5;
                const int mark_radius = 10;
                const int mark_width = 20;
                const int mark_cursor_height = 30;
                if (dsoSig->get_hover(index, hpoint, value)) {
                    p.setPen(dsoSig->get_colour());
                    const QRectF hpoint_rect = QRectF(hpoint.x()-mark_radius/2, hpoint.y()-mark_radius/2, mark_radius, mark_radius);
                    if (hpoint_rect.contains(_view.hover_point())) {
                        p.setBrush(dsoSig->get_colour());
                        const int cursor_up = hpoint.y()-mark_cursor_height;
                        const int cursor_dn = hpoint.y()+mark_cursor_height;
                        const int cursor_lf = hpoint.x()-arrow_size;
                        const int cursor_md = hpoint.x();
                        const int cursor_rt = hpoint.x()+arrow_size;

                        const QPointF up_arrow[3] = {
                            QPointF(cursor_lf, cursor_up+arrow_size),
                            QPointF(cursor_md, cursor_up),
                            QPointF(cursor_rt, cursor_up+arrow_size),
                        };
                        const QPointF dn_arrow[3] = {
                            QPointF(cursor_lf, cursor_dn-arrow_size),
                            QPointF(cursor_md, cursor_dn),
                            QPointF(cursor_rt, cursor_dn-arrow_size),
                        };
                        p.drawPolyline(up_arrow, 3);
                        p.drawPolyline(dn_arrow, 3);
                        p.drawLine(cursor_md, cursor_up, cursor_md, cursor_dn);
                        _hover_hit = true;
                        _hover_index = index;
                    } else {
                        p.setBrush(Qt::NoBrush);
                    }
                    p.drawEllipse(hpoint, mark_radius, mark_radius);
                    QString value_c = abs(value) > 1000 ? QString::number(value/1000.0, 'f', 2) + "V" : QString::number(value, 'f', 2) + "mV";
                    int value_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
                                                       Qt::AlignLeft | Qt::AlignTop, value_c).width();
                    const bool right = dsoSig->get_index()%2 ? hpoint.x() < value_width : hpoint.x() < _view.get_view_width() - value_width;
                    const bool up = hpoint.y() > 50;
                    const QPointF hpoint_sec = QPointF(hpoint.x() - (right ? -mark_width : mark_width), hpoint.y() - (up ? mark_width : -mark_width));
                    p.drawLine(hpoint, hpoint_sec);
                    p.drawLine(hpoint_sec, QPointF(hpoint_sec.x() + (right ? value_width : -value_width), hpoint_sec.y()));
                    p.drawText(QRectF(right ? hpoint_sec.x() : hpoint_sec.x() - value_width, hpoint_sec.y() - mark_width, value_width, mark_width),
                               Qt::AlignLeft | Qt::AlignBottom,
                               value_c);
                }
            }
        }
DreamSourceLab's avatar
DreamSourceLab committed
679 680 681
    }
}

682
QString Viewport::get_measure(QString option)
DreamSourceLab's avatar
DreamSourceLab committed
683
{
684 685 686 687 688 689 690 691 692 693
    if(option.compare("width") == 0)
        return _mm_width;
    else if (option.compare("period") == 0)
        return _mm_period;
    else if (option.compare("frequency") == 0)
        return _mm_freq;
    else if (option.compare("duty") == 0)
        return _mm_duty;
    else
        return "#####";
DreamSourceLab's avatar
DreamSourceLab committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
}

void Viewport::set_measure_en(int enable)
{
    if (enable == 0)
        _measure_en = false;
    else
        _measure_en = true;
}

void Viewport::start_trigger_timer(int msec)
{
    assert(msec > 0);
    triggered = false;
    timer_cnt = 0;
    trigger_timer.start(msec);
}

void Viewport::stop_trigger_timer()
{
    triggered = true;
    timer_cnt = 0;
    trigger_timer.stop();
}

void Viewport::on_trigger_timer()
{
    timer_cnt++;
    update();
}

} // namespace view
} // namespace pv