view.cpp 27.8 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 29
 *
 * 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 <assert.h>
#include <limits.h>
#include <math.h>

#include <boost/foreach.hpp>

Diego F. Asanza's avatar
Diego F. Asanza committed
30
#include <QApplication>
DreamSourceLab's avatar
DreamSourceLab committed
31 32 33 34
#include <QEvent>
#include <QMouseEvent>
#include <QScrollBar>

DreamSourceLab's avatar
DreamSourceLab committed
35
#include "groupsignal.h"
DreamSourceLab's avatar
DreamSourceLab committed
36
#include "decodetrace.h"
DreamSourceLab's avatar
DreamSourceLab committed
37
#include "header.h"
DreamSourceLab's avatar
DreamSourceLab committed
38
#include "devmode.h"
DreamSourceLab's avatar
DreamSourceLab committed
39 40
#include "ruler.h"
#include "signal.h"
DreamSourceLab's avatar
DreamSourceLab committed
41
#include "dsosignal.h"
DreamSourceLab's avatar
DreamSourceLab committed
42 43
#include "view.h"
#include "viewport.h"
DreamSourceLab's avatar
DreamSourceLab committed
44
#include "mathtrace.h"
DreamSourceLab's avatar
DreamSourceLab committed
45

DreamSourceLab's avatar
DreamSourceLab committed
46
#include "../device/devinst.h"
DreamSourceLab's avatar
DreamSourceLab committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include "pv/sigsession.h"
#include "pv/data/logic.h"
#include "pv/data/logicsnapshot.h"

using namespace boost;
using namespace std;

namespace pv {
namespace view {

const int View::LabelMarginWidth = 70;
const int View::RulerHeight = 50;

const int View::MaxScrollValue = INT_MAX / 2;
61
const int View::MaxHeightUnit = 20;
DreamSourceLab's avatar
DreamSourceLab committed
62 63 64 65 66 67 68 69 70

//const int View::SignalHeight = 30;s
const int View::SignalMargin = 10;
const int View::SignalSnapGridSize = 10;

const QColor View::CursorAreaColour(220, 231, 243);

const QSizeF View::LabelPadding(4, 4);

DreamSourceLab's avatar
DreamSourceLab committed
71
View::View(SigSession &session, pv::toolbars::SamplingBar *sampling_bar, QWidget *parent) :
DreamSourceLab's avatar
DreamSourceLab committed
72
    QScrollArea(parent),
DreamSourceLab's avatar
DreamSourceLab committed
73
	_session(session),
DreamSourceLab's avatar
DreamSourceLab committed
74
    _sampling_bar(sampling_bar),
DreamSourceLab's avatar
DreamSourceLab committed
75 76 77 78 79 80 81 82
    _scale(1e-8),
    _preScale(1e-6),
    _maxscale(1e9),
    _minscale(1e-15),
	_offset(0),
    _preOffset(0),
	_updating_scroll(false),
	_show_cursors(false),
DreamSourceLab's avatar
DreamSourceLab committed
83
    _trig_pos(0),
DreamSourceLab's avatar
DreamSourceLab committed
84
    _hover_point(-1, -1)
DreamSourceLab's avatar
DreamSourceLab committed
85 86 87 88 89 90 91 92
{
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

	connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
		this, SLOT(h_scroll_value_changed(int)));
	connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
		this, SLOT(v_scroll_value_changed(int)));

DreamSourceLab's avatar
DreamSourceLab committed
93 94 95 96 97 98 99 100 101 102 103 104 105
    // trace viewport map
    _trace_view_map[SR_CHANNEL_LOGIC] = TIME_VIEW;
    _trace_view_map[SR_CHANNEL_GROUP] = TIME_VIEW;
    _trace_view_map[SR_CHANNEL_DECODER] = TIME_VIEW;
    _trace_view_map[SR_CHANNEL_ANALOG] = TIME_VIEW;
    _trace_view_map[SR_CHANNEL_DSO] = TIME_VIEW;
    _trace_view_map[SR_CHANNEL_FFT] = FFT_VIEW;

    _active_viewport = NULL;
    _ruler = new Ruler(*this);
    _header = new Header(*this);
    _devmode = new DevMode(this, session);

DreamSourceLab's avatar
DreamSourceLab committed
106
    setViewportMargins(headerWidth(), RulerHeight, 0, 0);
DreamSourceLab's avatar
DreamSourceLab committed
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
    //setViewport(_viewport);

    // windows splitter
    _time_viewport = new Viewport(*this, TIME_VIEW);
    _time_viewport->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    _time_viewport->setMinimumHeight(100);
    connect(_time_viewport, SIGNAL(measure_updated()),
            this, SLOT(on_measure_updated()));
    _fft_viewport = new Viewport(*this, FFT_VIEW);
    _fft_viewport->setVisible(false);
    _fft_viewport->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    _fft_viewport->setMinimumHeight(100);
    connect(_fft_viewport, SIGNAL(measure_updated()),
            this, SLOT(on_measure_updated()));

    _vsplitter = new QSplitter(this);
    _vsplitter->setOrientation(Qt::Vertical);
    _vsplitter->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    _viewport_list.push_back(_time_viewport);
    _vsplitter->addWidget(_time_viewport);
    _vsplitter->setCollapsible(0, false);
    _vsplitter->setStretchFactor(0, 2);
    _viewport_list.push_back(_fft_viewport);
    _vsplitter->addWidget(_fft_viewport);
    _vsplitter->setCollapsible(1, false);
    _vsplitter->setStretchFactor(1, 1);

    _viewcenter = new QWidget(this);
    _viewcenter->setContentsMargins(0,0,0,0);
    QGridLayout* layout = new QGridLayout(_viewcenter);
    layout->setContentsMargins(0,0,0,0);
    _viewcenter->setLayout(layout);
    layout->addWidget(_vsplitter, 0, 0);
    QWidget* bottom = new QWidget(this);
    bottom->setFixedHeight(10);
    layout->addWidget(bottom, 1, 0);
    setViewport(_viewcenter);
    connect(_vsplitter, SIGNAL(splitterMoved(int,int)),
        this, SLOT(splitterMoved(int, int)));
DreamSourceLab's avatar
DreamSourceLab committed
147

148 149
    connect(&_session, SIGNAL(device_setted()),
            _devmode, SLOT(set_device()));
150 151 152 153
    connect(&_session, SIGNAL(signals_changed()),
        this, SLOT(signals_changed()));
    connect(&_session, SIGNAL(data_updated()),
        this, SLOT(data_updated()));
DreamSourceLab's avatar
DreamSourceLab committed
154 155
    connect(&_session, SIGNAL(receive_trigger(quint64)),
            this, SLOT(set_trig_pos(quint64)));
156 157
    connect(&_session, SIGNAL(show_region(uint64_t,uint64_t)),
            this, SLOT(show_region(uint64_t, uint64_t)));
DreamSourceLab's avatar
DreamSourceLab committed
158

DreamSourceLab's avatar
DreamSourceLab committed
159 160 161 162 163
    connect(_devmode, SIGNAL(mode_changed()),
            this, SIGNAL(mode_changed()));

    connect(_header, SIGNAL(traces_moved()),
        this, SLOT(on_traces_moved()));
DreamSourceLab's avatar
DreamSourceLab committed
164 165 166
    connect(_header, SIGNAL(header_updated()),
        this, SLOT(header_updated()));

DreamSourceLab's avatar
DreamSourceLab committed
167 168
    _time_viewport->installEventFilter(this);
    _fft_viewport->installEventFilter(this);
DreamSourceLab's avatar
DreamSourceLab committed
169 170
	_ruler->installEventFilter(this);
	_header->installEventFilter(this);
DreamSourceLab's avatar
DreamSourceLab committed
171
    _devmode->installEventFilter(this);
DreamSourceLab's avatar
DreamSourceLab committed
172

DreamSourceLab's avatar
DreamSourceLab committed
173
    _viewcenter->setObjectName(tr("ViewArea_center"));
DreamSourceLab's avatar
DreamSourceLab committed
174 175 176 177
    _ruler->setObjectName(tr("ViewArea_ruler"));
    _header->setObjectName(tr("ViewArea_header"));

    _show_trig_cursor = false;
178
    _trig_cursor = new Cursor(*this, Trace::dsLightRed, 0);
DreamSourceLab's avatar
DreamSourceLab committed
179 180
    _show_search_cursor = false;
    _search_pos = 0;
181
    _search_cursor = new Cursor(*this, Trace::dsLightBlue, _search_pos);
DreamSourceLab's avatar
DreamSourceLab committed
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
}

SigSession& View::session()
{
	return _session;
}

double View::scale() const
{
	return _scale;
}

double View::offset() const
{
	return _offset;
}

double View::get_minscale() const
{
    return _minscale;
}

double View::get_maxscale() const
{
    return _maxscale;
}

void View::zoom(double steps)
{
DreamSourceLab's avatar
DreamSourceLab committed
211
    zoom(steps, get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
212 213
}

DreamSourceLab's avatar
DreamSourceLab committed
214
void View::set_update(Viewport *viewport, bool need_update)
DreamSourceLab's avatar
DreamSourceLab committed
215
{
DreamSourceLab's avatar
DreamSourceLab committed
216
    viewport->set_need_update(need_update);
DreamSourceLab's avatar
DreamSourceLab committed
217 218
}

DreamSourceLab's avatar
DreamSourceLab committed
219
void View::set_all_update(bool need_update)
DreamSourceLab's avatar
DreamSourceLab committed
220
{
DreamSourceLab's avatar
DreamSourceLab committed
221 222
    BOOST_FOREACH(Viewport *viewport, _viewport_list)
        viewport->set_need_update(need_update);
DreamSourceLab's avatar
DreamSourceLab committed
223 224
}

DreamSourceLab's avatar
DreamSourceLab committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
void View::update_sample(bool instant)
{
    _session.get_device()->set_config(NULL, NULL, SR_CONF_INSTANT, g_variant_new_boolean(instant));
    BOOST_FOREACH(const boost::shared_ptr<pv::view::Signal> s, _session.get_signals()) {
        boost::shared_ptr<pv::view::DsoSignal> dsoSig;
        if (dsoSig = dynamic_pointer_cast<pv::view::DsoSignal>(s)) {
            dsoSig->go_hDialCur();
            break;
        }
    }
}

void View::set_sample_rate(uint64_t sample_rate, bool force)
{
    if (_session.get_capture_state() != pv::SigSession::Stopped || force)
        _sampling_bar->set_sample_rate(sample_rate);
}

void View::set_sample_limit(uint64_t sample_limit, bool force)
{
    if (_session.get_capture_state() != pv::SigSession::Stopped || force)
        _sampling_bar->set_sample_limit(sample_limit);
}

DreamSourceLab's avatar
DreamSourceLab committed
249 250 251 252 253 254 255
void View::zoom(double steps, int offset)
{
    //if (_session.get_capture_state() == SigSession::Stopped) {
        _preScale = _scale;
        _preOffset = _offset;

        const double cursor_offset = _offset + _scale * offset;
DreamSourceLab's avatar
DreamSourceLab committed
256
        if (_session.get_device()->dev_inst()->mode != DSO) {
DreamSourceLab's avatar
DreamSourceLab committed
257
            _scale *= std::pow(3.0/2.0, -steps);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
258
            _scale = max(min(_scale, _maxscale), _minscale);
DreamSourceLab's avatar
DreamSourceLab committed
259
        }else {
260
            const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
DreamSourceLab's avatar
DreamSourceLab committed
261
            bool setted = false;
262 263
            BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
                boost::shared_ptr<DsoSignal> dsoSig;
DreamSourceLab's avatar
DreamSourceLab committed
264 265
                if (dsoSig = dynamic_pointer_cast<DsoSignal>(s)) {
                    if(steps > 0.5)
DreamSourceLab's avatar
DreamSourceLab committed
266
                        dsoSig->go_hDialPre(setted);
DreamSourceLab's avatar
DreamSourceLab committed
267
                    else if (steps < -0.5)
DreamSourceLab's avatar
DreamSourceLab committed
268 269 270 271
                        dsoSig->go_hDialNext(setted);
                    else
                        break;
                    setted = true;
DreamSourceLab's avatar
DreamSourceLab committed
272
                }
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
273 274
            }
        }
275

DreamSourceLab's avatar
DreamSourceLab committed
276 277
        _offset = cursor_offset - _scale * offset;
        _offset = max(min(_offset, get_max_offset()), get_min_offset());
DreamSourceLab's avatar
DreamSourceLab committed
278 279

        if (_scale != _preScale || _offset != _preOffset) {
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
280
            _header->update();
DreamSourceLab's avatar
DreamSourceLab committed
281
            _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
282
            viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
283 284 285 286 287 288 289 290 291 292 293 294
            update_scroll();
        }
    //}
}


void View::set_scale_offset(double scale, double offset)
{
    //if (_session.get_capture_state() == SigSession::Stopped) {
        _preScale = _scale;
        _preOffset = _offset;

DreamSourceLab's avatar
DreamSourceLab committed
295
        _scale = max(min(scale, _maxscale), _minscale);
296
        _offset = max(min(offset, get_max_offset()), get_min_offset());
DreamSourceLab's avatar
DreamSourceLab committed
297 298 299

        if (_scale != _preScale || _offset != _preOffset) {
            update_scroll();
DreamSourceLab's avatar
DreamSourceLab committed
300
            _header->update();
DreamSourceLab's avatar
DreamSourceLab committed
301
            _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
302
            viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
303 304 305 306 307 308 309 310 311 312 313 314 315
        }
    //}
}

void View::set_preScale_preOffset()
{
    //assert(_preScale <= _maxscale);
    //assert(_preScale >= _minscale);
    //assert(_preOffset >= 0);

    set_scale_offset(_preScale, _preOffset);
}

DreamSourceLab's avatar
DreamSourceLab committed
316
vector< boost::shared_ptr<Trace> > View::get_traces(int type)
DreamSourceLab's avatar
DreamSourceLab committed
317
{
DreamSourceLab's avatar
DreamSourceLab committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
//    const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
//    const vector< boost::shared_ptr<GroupSignal> > groups(_session.get_group_signals());
//#ifdef ENABLE_DECODE
//    const vector< boost::shared_ptr<DecodeTrace> > decode_sigs(
//        _session.get_decode_signals());
//    vector< boost::shared_ptr<Trace> > traces(
//        sigs.size() + groups.size() + decode_sigs.size());
//#else
//    vector< boost::shared_ptr<Trace> > traces(sigs.size() + groups.size());
//#endif

//    vector< boost::shared_ptr<Trace> >::iterator i = traces.begin();
//    i = copy(sigs.begin(), sigs.end(), i);
//#ifdef ENABLE_DECODE
//    i = copy(decode_sigs.begin(), decode_sigs.end(), i);
//#endif
//    i = copy(groups.begin(), groups.end(), i);

//    stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
//    return traces;

339
    const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
DreamSourceLab's avatar
DreamSourceLab committed
340
    const vector< boost::shared_ptr<GroupSignal> > groups(_session.get_group_signals());
DreamSourceLab's avatar
DreamSourceLab committed
341
#ifdef ENABLE_DECODE
342
    const vector< boost::shared_ptr<DecodeTrace> > decode_sigs(
DreamSourceLab's avatar
DreamSourceLab committed
343 344
        _session.get_decode_signals());
#endif
DreamSourceLab's avatar
DreamSourceLab committed
345
    const vector< boost::shared_ptr<MathTrace> > maths(_session.get_math_signals());
DreamSourceLab's avatar
DreamSourceLab committed
346

DreamSourceLab's avatar
DreamSourceLab committed
347 348 349 350 351
    vector< boost::shared_ptr<Trace> > traces;
    BOOST_FOREACH(boost::shared_ptr<Trace> t, sigs) {
        if (type == ALL_VIEW || _trace_view_map[t->get_type()] == type)
            traces.push_back(t);
    }
DreamSourceLab's avatar
DreamSourceLab committed
352
#ifdef ENABLE_DECODE
DreamSourceLab's avatar
DreamSourceLab committed
353 354 355 356
    BOOST_FOREACH(boost::shared_ptr<Trace> t, decode_sigs) {
        if (type == ALL_VIEW || _trace_view_map[t->get_type()] == type)
            traces.push_back(t);
    }
DreamSourceLab's avatar
DreamSourceLab committed
357
#endif
DreamSourceLab's avatar
DreamSourceLab committed
358 359 360 361 362 363 364 365 366
    BOOST_FOREACH(boost::shared_ptr<Trace> t, groups) {
        if (type == ALL_VIEW || _trace_view_map[t->get_type()] == type)
            traces.push_back(t);
    }

    BOOST_FOREACH(boost::shared_ptr<Trace> t, maths) {
        if (type == ALL_VIEW || _trace_view_map[t->get_type()] == type)
            traces.push_back(t);
    }
DreamSourceLab's avatar
DreamSourceLab committed
367 368 369 370 371

    stable_sort(traces.begin(), traces.end(), compare_trace_v_offsets);
    return traces;
}

372 373
bool View::compare_trace_v_offsets(const boost::shared_ptr<Trace> &a,
    const boost::shared_ptr<Trace> &b)
DreamSourceLab's avatar
DreamSourceLab committed
374 375 376
{
    assert(a);
    assert(b);
377 378 379 380
    if (a->get_type() != b->get_type())
        return a->get_type() > b->get_type();
    else
        return a->get_v_offset() < b->get_v_offset();
DreamSourceLab's avatar
DreamSourceLab committed
381 382
}

DreamSourceLab's avatar
DreamSourceLab committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
bool View::cursors_shown() const
{
	return _show_cursors;
}

bool View::trig_cursor_shown() const
{
    return _show_trig_cursor;
}

bool View::search_cursor_shown() const
{
    return _show_search_cursor;
}

void View::show_cursors(bool show)
{
	_show_cursors = show;
	_ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
402
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
403 404 405 406 407 408
}

void View::show_trig_cursor(bool show)
{
    _show_trig_cursor = show;
    _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
409
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
410 411 412 413 414 415
}

void View::show_search_cursor(bool show)
{
    _show_search_cursor = show;
    _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
416
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
417 418 419 420
}

void View::set_trig_pos(quint64 trig_pos)
{
421
    const double time = trig_pos * 1.0 / _session.get_device()->get_sample_rate();
DreamSourceLab's avatar
DreamSourceLab committed
422
    _trig_pos = trig_pos;
423
    _trig_cursor->set_index(trig_pos);
DreamSourceLab's avatar
DreamSourceLab committed
424
    _show_trig_cursor = true;
DreamSourceLab's avatar
DreamSourceLab committed
425
    set_scale_offset(_scale,  time - _scale * get_view_width() / 2);
426 427 428 429 430

    _trigger_time = QDateTime::currentDateTime();
    const int64_t secs = time - _session.get_device()->get_sample_time();
    _trigger_time = _trigger_time.addSecs(secs);

DreamSourceLab's avatar
DreamSourceLab committed
431
    _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
432
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
433 434 435 436 437 438
}

void View::set_search_pos(uint64_t search_pos)
{
    //assert(search_pos >= 0);

439
    const double time = search_pos * 1.0 / _session.get_device()->get_sample_rate();
DreamSourceLab's avatar
DreamSourceLab committed
440
    _search_pos = search_pos;
441
    _search_cursor->set_index(search_pos);
DreamSourceLab's avatar
DreamSourceLab committed
442
    set_scale_offset(_scale,  time - _scale * get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
443
    _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
444
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
}

uint64_t View::get_trig_pos()
{
    return _trig_pos;
}

uint64_t View::get_search_pos()
{
    return _search_pos;
}

const QPointF& View::hover_point() const
{
	return _hover_point;
}

void View::normalize_layout()
{
DreamSourceLab's avatar
DreamSourceLab committed
464
    const vector< boost::shared_ptr<Trace> > traces(get_traces(ALL_VIEW));
DreamSourceLab's avatar
DreamSourceLab committed
465 466

	int v_min = INT_MAX;
467
    BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
468
        v_min = min(t->get_v_offset(), v_min);
DreamSourceLab's avatar
DreamSourceLab committed
469 470

	const int delta = -min(v_min, 0);
471
    BOOST_FOREACH(boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
472
        t->set_v_offset(t->get_v_offset() + delta);
DreamSourceLab's avatar
DreamSourceLab committed
473

DreamSourceLab's avatar
DreamSourceLab committed
474
    verticalScrollBar()->setSliderPosition(delta);
DreamSourceLab's avatar
DreamSourceLab committed
475 476 477 478 479 480 481 482 483 484 485
	v_scroll_value_changed(verticalScrollBar()->sliderPosition());
}


int View::get_spanY()
{
    return _spanY;
}

int View::get_signalHeight()
{
DreamSourceLab's avatar
DreamSourceLab committed
486
    return _signalHeight;
DreamSourceLab's avatar
DreamSourceLab committed
487 488 489 490
}

void View::get_scroll_layout(double &length, double &offset) const
{
491
    const set< boost::shared_ptr<data::SignalData> > data_set = _session.get_data();
DreamSourceLab's avatar
DreamSourceLab committed
492
    if (data_set.empty())
DreamSourceLab's avatar
DreamSourceLab committed
493 494
		return;

DreamSourceLab's avatar
DreamSourceLab committed
495
    length = _session.get_device()->get_sample_time() / _scale;
DreamSourceLab's avatar
DreamSourceLab committed
496 497 498 499 500
	offset = _offset / _scale;
}

void View::update_scroll()
{
DreamSourceLab's avatar
DreamSourceLab committed
501
    assert(_viewcenter);
DreamSourceLab's avatar
DreamSourceLab committed
502

DreamSourceLab's avatar
DreamSourceLab committed
503
    const QSize areaSize = _viewcenter->size();
DreamSourceLab's avatar
DreamSourceLab committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

	// Set the horizontal scroll bar
	double length = 0, offset = 0;
	get_scroll_layout(length, offset);
	length = max(length - areaSize.width(), 0.0);

	horizontalScrollBar()->setPageStep(areaSize.width() / 2);

	_updating_scroll = true;

	if (length < MaxScrollValue) {
		horizontalScrollBar()->setRange(0, length);
		horizontalScrollBar()->setSliderPosition(offset);
	} else {
		horizontalScrollBar()->setRange(0, MaxScrollValue);
		horizontalScrollBar()->setSliderPosition(
			_offset * MaxScrollValue / (_scale * length));
	}

	_updating_scroll = false;

	// Set the vertical scrollbar
	verticalScrollBar()->setPageStep(areaSize.height());
DreamSourceLab's avatar
DreamSourceLab committed
527
    verticalScrollBar()->setRange(0,0);
DreamSourceLab's avatar
DreamSourceLab committed
528 529
}

DreamSourceLab's avatar
DreamSourceLab committed
530
void View::update_scale()
DreamSourceLab's avatar
DreamSourceLab committed
531
{
DreamSourceLab's avatar
DreamSourceLab committed
532 533 534 535
    const uint64_t sample_rate = _session.get_device()->get_sample_rate();
    assert(sample_rate > 0);

    if (_session.get_device()->dev_inst()->mode != DSO) {
536
        _scale = (1.0 / sample_rate) / WellPixelsPerSample;
DreamSourceLab's avatar
DreamSourceLab committed
537
        _maxscale = _session.get_device()->get_sample_time() / (get_view_width() * MaxViewRate);
DreamSourceLab's avatar
DreamSourceLab committed
538
    } else {
539
        _scale = _session.get_device()->get_time_base() * 10.0 / get_view_width() * std::pow(10.0, -9.0);
DreamSourceLab's avatar
DreamSourceLab committed
540 541
        _maxscale = 1e9;
    }
DreamSourceLab's avatar
DreamSourceLab committed
542

543
    _minscale = (1.0 / sample_rate) / MaxPixelsPerSample;
544
    //_offset = 0;
DreamSourceLab's avatar
DreamSourceLab committed
545
    _preScale = _scale;
546
    //_preOffset = _offset;
DreamSourceLab's avatar
DreamSourceLab committed
547

548
    _trig_cursor->set_index(_trig_pos);
DreamSourceLab's avatar
DreamSourceLab committed
549 550

    _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
551
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
552 553 554 555 556
}

void View::signals_changed()
{
    int total_rows = 0;
557
    uint8_t max_height = MaxHeightUnit;
DreamSourceLab's avatar
DreamSourceLab committed
558 559 560 561 562 563 564 565
    vector< boost::shared_ptr<Trace> > time_traces;
    vector< boost::shared_ptr<Trace> > fft_traces;
    BOOST_FOREACH(const boost::shared_ptr<Trace> t, get_traces(ALL_VIEW)) {
        if (_trace_view_map[t->get_type()] == TIME_VIEW)
            time_traces.push_back(t);
        else if (_trace_view_map[t->get_type()] == FFT_VIEW)
            if (t->enabled())
                fft_traces.push_back(t);
DreamSourceLab's avatar
DreamSourceLab committed
566
    }
DreamSourceLab's avatar
DreamSourceLab committed
567

DreamSourceLab's avatar
DreamSourceLab committed
568 569 570 571 572 573 574 575 576 577 578 579
    if (!fft_traces.empty()) {
        if (!_fft_viewport->isVisible()) {
            _fft_viewport->setVisible(true);
            _fft_viewport->clear_measure();
            _viewport_list.push_back(_fft_viewport);
            _vsplitter->refresh();
        }
        BOOST_FOREACH(boost::shared_ptr<Trace> t, fft_traces) {
            t->set_view(this);
            t->set_viewport(_fft_viewport);
            t->set_totalHeight(_fft_viewport->height());
            t->set_v_offset(_fft_viewport->geometry().bottom());
580 581
        }
    } else {
DreamSourceLab's avatar
DreamSourceLab committed
582 583 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 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
        _fft_viewport->setVisible(false);
        _vsplitter->refresh();

        // Find the decoder in the stack
        std::list< Viewport *>::iterator iter = _viewport_list.begin();
        for(int i = 0; i < _viewport_list.size(); i++, iter++)
            if ((*iter) == _fft_viewport)
                break;
        // Delete the element
        if (iter != _viewport_list.end())
            _viewport_list.erase(iter);
    }

    if (!time_traces.empty() && _time_viewport) {
        BOOST_FOREACH(const boost::shared_ptr<Trace> t, time_traces) {
            assert(t);
            if (dynamic_pointer_cast<DsoSignal>(t) ||
                t->enabled())
                total_rows += t->rows_size();
        }

        const double height = (_time_viewport->height()
                               - horizontalScrollBar()->height()
                               - 2 * SignalMargin * time_traces.size()) * 1.0 / total_rows;

        if (_session.get_device()->dev_inst()->mode == LOGIC) {
            GVariant* gvar = _session.get_device()->get_config(NULL, NULL, SR_CONF_MAX_HEIGHT_VALUE);
            if (gvar != NULL) {
                max_height = (g_variant_get_byte(gvar) + 1) * MaxHeightUnit;
                g_variant_unref(gvar);
            }
            _signalHeight = (int)((height <= 0) ? 1 : (height >= max_height) ? max_height : height);
        } else if (_session.get_device()->dev_inst()->mode == DSO) {
            _signalHeight = (_header->height()
                             - horizontalScrollBar()->height()
                             - 2 * SignalMargin * time_traces.size()) * 1.0 / total_rows;
        } else {
            _signalHeight = (int)((height <= 0) ? 1 : height);
        }
        _spanY = _signalHeight + 2 * SignalMargin;
        int next_v_offset = SignalMargin;
        BOOST_FOREACH(boost::shared_ptr<Trace> t, time_traces) {
            t->set_view(this);
            t->set_viewport(_time_viewport);
            const double traceHeight = _signalHeight*t->rows_size();
            t->set_totalHeight((int)traceHeight);
            t->set_v_offset(next_v_offset + 0.5 * traceHeight + SignalMargin);
            next_v_offset += traceHeight + 2 * SignalMargin;

            boost::shared_ptr<view::DsoSignal> dsoSig;
            if (dsoSig = dynamic_pointer_cast<view::DsoSignal>(t))
                dsoSig->set_scale(dsoSig->get_view_rect().height() / 256.0f);
        }
        _time_viewport->clear_measure();
636
    }
DreamSourceLab's avatar
DreamSourceLab committed
637 638 639

    header_updated();
    normalize_layout();
640
    data_updated();
DreamSourceLab's avatar
DreamSourceLab committed
641 642 643 644 645 646 647 648
}

bool View::eventFilter(QObject *object, QEvent *event)
{
	const QEvent::Type type = event->type();
	if (type == QEvent::MouseMove) {

		const QMouseEvent *const mouse_event = (QMouseEvent*)event;
DreamSourceLab's avatar
DreamSourceLab committed
649
        if (object == _ruler || object == _time_viewport || object == _fft_viewport) {
DreamSourceLab's avatar
DreamSourceLab committed
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 679 680 681 682 683 684 685 686 687 688 689 690 691
            //_hover_point = QPoint(mouse_event->x(), 0);
            double cur_periods = (mouse_event->pos().x() * _scale + _offset) / _ruler->get_min_period();
            double integer_x = (round(cur_periods) * _ruler->get_min_period() - _offset ) / _scale;
            double cur_deviate_x = qAbs(mouse_event->pos().x() - integer_x);
            if (cur_deviate_x < 10)
                _hover_point = QPointF(integer_x, mouse_event->pos().y());
            else
                _hover_point = mouse_event->pos();
        } else if (object == _header)
			_hover_point = QPoint(0, mouse_event->y());
		else
			_hover_point = QPoint(-1, -1);

		hover_point_changed();

	} else if (type == QEvent::Leave) {
		_hover_point = QPoint(-1, -1);
		hover_point_changed();
	}

	return QObject::eventFilter(object, event);
}

bool View::viewportEvent(QEvent *e)
{
	switch(e->type()) {
	case QEvent::Paint:
	case QEvent::MouseButtonPress:
	case QEvent::MouseButtonRelease:
	case QEvent::MouseButtonDblClick:
	case QEvent::MouseMove:
	case QEvent::Wheel:
		return false;

	default:
		return QAbstractScrollArea::viewportEvent(e);
	}
}

int View::headerWidth()
{
    int headerWidth;
692
    int maxNameWidth = 25;
DreamSourceLab's avatar
DreamSourceLab committed
693 694
    int maxLeftWidth = 0;
    int maxRightWidth = 0;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
695

DreamSourceLab's avatar
v0.21  
DreamSourceLab committed
696 697 698
    QFont font = QApplication::font();
    QFontMetrics fm(font);

DreamSourceLab's avatar
DreamSourceLab committed
699
    const vector< boost::shared_ptr<Trace> > traces(get_traces(ALL_VIEW));
DreamSourceLab's avatar
DreamSourceLab committed
700
    if (!traces.empty()){
701
        BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces) {
DreamSourceLab's avatar
DreamSourceLab committed
702 703 704
            maxNameWidth = max(fm.boundingRect(t->get_name()).width(), maxNameWidth);
            maxLeftWidth = max(t->get_leftWidth(), maxLeftWidth);
            maxRightWidth = max(t->get_rightWidth(), maxRightWidth);
DreamSourceLab's avatar
DreamSourceLab committed
705 706 707 708 709 710 711 712 713 714 715 716
        }
    }
    maxNameWidth = max(_header->get_nameEditWidth(), maxNameWidth);
    headerWidth = maxLeftWidth + maxNameWidth + maxRightWidth;

    setViewportMargins(headerWidth, RulerHeight, 0, 0);

    return headerWidth;
}

void View::resizeEvent(QResizeEvent*)
{
DreamSourceLab's avatar
DreamSourceLab committed
717
    setViewportMargins(headerWidth(), RulerHeight, 0, 0);
DreamSourceLab's avatar
DreamSourceLab committed
718 719
    update_margins();
    update_scroll();
DreamSourceLab's avatar
DreamSourceLab committed
720
    signals_changed();
DreamSourceLab's avatar
DreamSourceLab committed
721
    if (_session.get_device()->dev_inst()->mode == DSO)
DreamSourceLab's avatar
DreamSourceLab committed
722
        _scale = _session.get_device()->get_time_base() * std::pow(10.0, -9.0) * DS_CONF_DSO_HDIVS / get_view_width();
DreamSourceLab's avatar
DreamSourceLab committed
723

724 725 726 727 728
    if (_session.get_device()->dev_inst()->mode != DSO)
        _maxscale = _session.get_device()->get_sample_time() / (get_view_width() * MaxViewRate);
    else
        _maxscale = 1e9;

DreamSourceLab's avatar
DreamSourceLab committed
729 730 731
    _scale = min(_scale, _maxscale);

    _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
732
    _header->header_resize();
DreamSourceLab's avatar
DreamSourceLab committed
733 734
    set_update(_time_viewport, true);
    set_update(_fft_viewport, true);
DreamSourceLab's avatar
DreamSourceLab committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
}

void View::h_scroll_value_changed(int value)
{
	if (_updating_scroll)
		return;

    _preOffset = _offset;

	const int range = horizontalScrollBar()->maximum();
	if (range < MaxScrollValue)
		_offset = _scale * value;
	else {
		double length = 0, offset;
		get_scroll_layout(length, offset);
		_offset = _scale * length * value / MaxScrollValue;
	}

753
    _offset = max(min(_offset, get_max_offset()), get_min_offset());
DreamSourceLab's avatar
DreamSourceLab committed
754 755 756

    if (_offset != _preOffset) {
        _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
757
        viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
758 759 760 761 762 763
    }
}

void View::v_scroll_value_changed(int value)
{
	_header->update();
DreamSourceLab's avatar
DreamSourceLab committed
764
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
765 766 767 768 769 770 771 772 773 774 775
}

void View::data_updated()
{
    setViewportMargins(headerWidth(), RulerHeight, 0, 0);
    update_margins();

	// Update the scroll bars
	update_scroll();

	// Repaint the view
DreamSourceLab's avatar
DreamSourceLab committed
776 777 778
    set_update(_time_viewport, true);
    set_update(_fft_viewport, true);
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
779 780 781 782
}

void View::update_margins()
{
DreamSourceLab's avatar
DreamSourceLab committed
783 784 785 786
    _ruler->setGeometry(_viewcenter->x(), 0,
        get_view_width(), _viewcenter->y());
    _header->setGeometry(0, _viewcenter->y(),
        _viewcenter->x(), _viewcenter->height());
DreamSourceLab's avatar
DreamSourceLab committed
787
    _devmode->setGeometry(0, 0,
DreamSourceLab's avatar
DreamSourceLab committed
788
        _viewcenter->x(), _viewcenter->y());
DreamSourceLab's avatar
DreamSourceLab committed
789 790 791 792 793 794 795 796 797 798
}

void View::header_updated()
{
    headerWidth();
    update_margins();

    // Update the scroll bars
    update_scroll();

DreamSourceLab's avatar
DreamSourceLab committed
799
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
800 801 802 803 804 805
    _header->update();
}

void View::marker_time_changed()
{
	_ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
806
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
807 808
}

DreamSourceLab's avatar
DreamSourceLab committed
809
void View::on_traces_moved()
DreamSourceLab's avatar
DreamSourceLab committed
810 811
{
	update_scroll();
DreamSourceLab's avatar
DreamSourceLab committed
812 813
    set_update(_time_viewport, true);
    viewport_update();
DreamSourceLab's avatar
DreamSourceLab committed
814
    //traces_moved();
DreamSourceLab's avatar
DreamSourceLab committed
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
}

/*
 * cursorList
 */
std::list<Cursor*>& View::get_cursorList()
{
    return _cursorList;
}

Cursor* View::get_trig_cursor()
{
    return _trig_cursor;
}

Cursor* View::get_search_cursor()
{
    return _search_cursor;
}

Ruler* View::get_ruler()
{
    return _ruler;
}

840
void View::add_cursor(QColor color, uint64_t index)
DreamSourceLab's avatar
DreamSourceLab committed
841
{
842
    Cursor *newCursor = new Cursor(*this, color, index);
DreamSourceLab's avatar
DreamSourceLab committed
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    _cursorList.push_back(newCursor);
    cursor_update();
}

void View::del_cursor(Cursor* cursor)
{
    assert(cursor);

    _cursorList.remove(cursor);
    delete cursor;
    cursor_update();
}

void View::set_cursor_middle(int index)
{
    assert(index < (int)_cursorList.size());

    list<Cursor*>::iterator i = _cursorList.begin();
    while (index-- != 0)
            i++;
863
    set_scale_offset(_scale, (*i)->index() * 1.0 / _session.get_device()->get_sample_rate() - _scale * get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
864 865
}

DreamSourceLab's avatar
DreamSourceLab committed
866 867 868 869 870 871 872
void View::on_measure_updated()
{
    _active_viewport = dynamic_cast<Viewport *>(sender());
    measure_updated();
}

QString View::get_measure(QString option)
DreamSourceLab's avatar
DreamSourceLab committed
873
{
DreamSourceLab's avatar
DreamSourceLab committed
874 875 876 877
    if (_active_viewport) {
        return _active_viewport->get_measure(option);
    }
    return "#####";
DreamSourceLab's avatar
DreamSourceLab committed
878 879 880 881
}

QString View::get_cm_time(int index)
{
882
    return _ruler->format_real_time(get_cursor_samples(index), _session.get_device()->get_sample_rate());
DreamSourceLab's avatar
DreamSourceLab committed
883 884 885 886 887 888 889
}

QString View::get_cm_delta(int index1, int index2)
{
    if (index1 == index2)
        return "0";

890 891 892 893
    uint64_t samples1 = get_cursor_samples(index1);
    uint64_t samples2 = get_cursor_samples(index2);
    uint64_t delta_sample = (samples1 > samples2) ? samples1 - samples2 : samples2 - samples1;
    return _ruler->format_real_time(delta_sample, _session.get_device()->get_sample_rate());
DreamSourceLab's avatar
DreamSourceLab committed
894 895
}

DreamSourceLab's avatar
DreamSourceLab committed
896 897
uint64_t View::get_cursor_samples(int index)
{
898
    assert(index < (int)_cursorList.size());
DreamSourceLab's avatar
DreamSourceLab committed
899

900 901 902 903 904 905 906 907
    int curIndex = 0;
    for (list<Cursor*>::iterator i = _cursorList.begin();
         i != _cursorList.end(); i++) {
        if (index == curIndex) {
            return (*i)->index();
        }
        curIndex++;
    }
DreamSourceLab's avatar
DreamSourceLab committed
908 909
}

DreamSourceLab's avatar
DreamSourceLab committed
910 911 912 913 914 915 916
void View::on_cursor_moved()
{
    cursor_moved();
}

void View::set_measure_en(int enable)
{
DreamSourceLab's avatar
DreamSourceLab committed
917 918
    BOOST_FOREACH(Viewport *viewport, _viewport_list)
            viewport->set_measure_en(enable);
DreamSourceLab's avatar
DreamSourceLab committed
919 920 921 922
}

void View::on_state_changed(bool stop)
{
DreamSourceLab's avatar
DreamSourceLab committed
923 924 925 926
    if (stop) {
        BOOST_FOREACH(Viewport *viewport, _viewport_list)
            viewport->stop_trigger_timer();
    }
DreamSourceLab's avatar
DreamSourceLab committed
927 928
}

DreamSourceLab's avatar
DreamSourceLab committed
929
int View::get_view_width()
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
930
{
DreamSourceLab's avatar
DreamSourceLab committed
931 932
    int view_width = 0;
    if (_session.get_device()->dev_inst()->mode == DSO) {
933 934
        const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
        BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
DreamSourceLab's avatar
DreamSourceLab committed
935 936 937
            view_width = max((double)view_width, s->get_view_rect().width());
        }
    } else {
DreamSourceLab's avatar
DreamSourceLab committed
938
        view_width = _viewcenter->width();
DreamSourceLab's avatar
DreamSourceLab committed
939
    }
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
940

DreamSourceLab's avatar
DreamSourceLab committed
941
    return view_width;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
942 943
}

944 945 946 947 948 949 950 951 952
int View::get_view_height()
{
    int view_height = 0;
    if (_session.get_device()->dev_inst()->mode == DSO) {
        const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
        BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
            view_height = max((double)view_height, s->get_view_rect().height());
        }
    } else {
DreamSourceLab's avatar
DreamSourceLab committed
953
        view_height = _viewcenter->height();
954 955 956 957 958
    }

    return view_height;
}

959 960 961 962 963 964 965 966 967 968 969
double View::get_min_offset()
{
    return -(_scale * (get_view_width() * (1 - MaxViewRate)));
}

double View::get_max_offset()
{
    return _session.get_device()->get_sample_time()
            - _scale * (get_view_width() * MaxViewRate);
}

970 971 972 973 974
QString View::trigger_time()
{
    return _trigger_time.toString("yyyy-MM-dd hh:mm:ss ddd");
}

975 976 977 978 979 980 981 982 983
void View::show_region(uint64_t start, uint64_t end)
{
    assert(start <= end);
    const double ideal_scale = (end-start) * 2.0 / _session.get_device()->get_sample_rate() / get_view_width();
    const double new_scale = max(min(ideal_scale, _maxscale), _minscale);
    const double new_off = (start + end)  * 0.5 / _session.get_device()->get_sample_rate() - new_scale * get_view_width() / 2;
    set_scale_offset(new_scale, new_off);
}

DreamSourceLab's avatar
DreamSourceLab committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997
void View::viewport_update()
{
    _viewcenter->update();
    BOOST_FOREACH(Viewport *viewport, _viewport_list)
        viewport->update();
}

void View::splitterMoved(int pos, int index)
{
    (void)pos;
    (void)index;
    signals_changed();
}

DreamSourceLab's avatar
DreamSourceLab committed
998 999
} // namespace view
} // namespace pv