view.cpp 20.2 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 "decodetrace.h"
DreamSourceLab's avatar
DreamSourceLab committed
36
#include "header.h"
DreamSourceLab's avatar
DreamSourceLab committed
37
#include "devmode.h"
DreamSourceLab's avatar
DreamSourceLab committed
38 39
#include "ruler.h"
#include "signal.h"
DreamSourceLab's avatar
DreamSourceLab committed
40
#include "dsosignal.h"
DreamSourceLab's avatar
DreamSourceLab committed
41 42 43
#include "view.h"
#include "viewport.h"

DreamSourceLab's avatar
DreamSourceLab committed
44
#include "../device/devinst.h"
DreamSourceLab's avatar
DreamSourceLab committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#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;

//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
68
View::View(SigSession &session, pv::toolbars::SamplingBar *sampling_bar, QWidget *parent) :
DreamSourceLab's avatar
DreamSourceLab committed
69 70
	QAbstractScrollArea(parent),
	_session(session),
DreamSourceLab's avatar
DreamSourceLab committed
71
    _sampling_bar(sampling_bar),
DreamSourceLab's avatar
DreamSourceLab committed
72 73 74
	_viewport(new Viewport(*this)),
	_ruler(new Ruler(*this)),
	_header(new Header(*this)),
DreamSourceLab's avatar
DreamSourceLab committed
75
    _devmode(new DevMode(*this)),
DreamSourceLab's avatar
DreamSourceLab committed
76 77 78 79 80 81 82 83 84 85
    _scale(1e-8),
    _preScale(1e-6),
    _maxscale(1e9),
    _minscale(1e-15),
	_offset(0),
    _preOffset(0),
	_v_offset(0),
	_updating_scroll(false),
    _need_update(false),
	_show_cursors(false),
DreamSourceLab's avatar
DreamSourceLab committed
86
    _trig_pos(0),
DreamSourceLab's avatar
DreamSourceLab committed
87 88 89 90 91 92 93 94 95
	_hover_point(-1, -1)
{
    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
96 97 98
    setViewportMargins(headerWidth(), RulerHeight, 0, 0);
    setViewport(_viewport);

99 100 101 102
    connect(&_session, SIGNAL(signals_changed()),
        this, SLOT(signals_changed()));
    connect(&_session, SIGNAL(data_updated()),
        this, SLOT(data_updated()));
DreamSourceLab's avatar
DreamSourceLab committed
103 104 105
    connect(&_session, SIGNAL(receive_trigger(quint64)),
            this, SLOT(set_trig_pos(quint64)));

DreamSourceLab's avatar
DreamSourceLab committed
106 107 108 109 110 111 112
    connect(&_session, SIGNAL(device_setted()),
            _devmode, SLOT(set_device()));
    connect(_devmode, SIGNAL(mode_changed()),
            this, SIGNAL(mode_changed()));

    connect(_header, SIGNAL(traces_moved()),
        this, SLOT(on_traces_moved()));
DreamSourceLab's avatar
DreamSourceLab committed
113 114 115 116 117 118
    connect(_header, SIGNAL(header_updated()),
        this, SLOT(header_updated()));

	_viewport->installEventFilter(this);
	_ruler->installEventFilter(this);
	_header->installEventFilter(this);
DreamSourceLab's avatar
DreamSourceLab committed
119
    _devmode->installEventFilter(this);
DreamSourceLab's avatar
DreamSourceLab committed
120 121 122 123 124 125

    _viewport->setObjectName(tr("ViewArea_viewport"));
    _ruler->setObjectName(tr("ViewArea_ruler"));
    _header->setObjectName(tr("ViewArea_header"));

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

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

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

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

int View::v_offset() const
{
	return _v_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
164
    zoom(steps, get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
165 166 167 168 169 170 171 172 173 174 175 176
}

void View::set_need_update(bool need_update)
{
    _need_update = need_update;
}

bool View::need_update() const
{
    return _need_update;
}

DreamSourceLab's avatar
DreamSourceLab committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
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
201 202 203 204 205 206 207
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
208
        if (_session.get_device()->dev_inst()->mode != DSO) {
DreamSourceLab's avatar
DreamSourceLab committed
209
            _scale *= std::pow(3.0/2.0, -steps);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
210
            _scale = max(min(_scale, _maxscale), _minscale);
DreamSourceLab's avatar
DreamSourceLab committed
211
        }else {
212
            const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
DreamSourceLab's avatar
DreamSourceLab committed
213
            bool setted = false;
214 215
            BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
                boost::shared_ptr<DsoSignal> dsoSig;
DreamSourceLab's avatar
DreamSourceLab committed
216 217
                if (dsoSig = dynamic_pointer_cast<DsoSignal>(s)) {
                    if(steps > 0.5)
DreamSourceLab's avatar
DreamSourceLab committed
218
                        dsoSig->go_hDialPre(setted);
DreamSourceLab's avatar
DreamSourceLab committed
219
                    else if (steps < -0.5)
DreamSourceLab's avatar
DreamSourceLab committed
220 221 222 223
                        dsoSig->go_hDialNext(setted);
                    else
                        break;
                    setted = true;
DreamSourceLab's avatar
DreamSourceLab committed
224
                }
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
225 226
            }
        }
DreamSourceLab's avatar
DreamSourceLab committed
227
        _offset = cursor_offset - _scale * offset;
DreamSourceLab's avatar
DreamSourceLab committed
228
        const double MinOffset = -(_scale * (get_view_width() * (1 - MaxViewRate)));
DreamSourceLab's avatar
DreamSourceLab committed
229
        const double MaxOffset = _session.get_device()->get_sample_time() -
DreamSourceLab's avatar
DreamSourceLab committed
230
                _scale * (get_view_width() * MaxViewRate);
DreamSourceLab's avatar
DreamSourceLab committed
231 232 233
        _offset = max(min(_offset, MaxOffset), MinOffset);

        if (_scale != _preScale || _offset != _preOffset) {
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
234
            _header->update();
DreamSourceLab's avatar
DreamSourceLab committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248
            _ruler->update();
            _viewport->update();
            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
249
        _scale = max(min(scale, _maxscale), _minscale);
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
250

DreamSourceLab's avatar
DreamSourceLab committed
251
        const double MinOffset = -(_scale * (get_view_width() * (1 - MaxViewRate)));
DreamSourceLab's avatar
DreamSourceLab committed
252
        const double MaxOffset = _session.get_device()->get_sample_time()
DreamSourceLab's avatar
DreamSourceLab committed
253
                - _scale * (get_view_width() * MaxViewRate);
DreamSourceLab's avatar
DreamSourceLab committed
254 255 256 257
        _offset = max(min(offset, MaxOffset), MinOffset);

        if (_scale != _preScale || _offset != _preOffset) {
            update_scroll();
DreamSourceLab's avatar
DreamSourceLab committed
258
            _header->update();
DreamSourceLab's avatar
DreamSourceLab committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
            _ruler->update();
            _viewport->update();
        }
    //}
}

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

    set_scale_offset(_preScale, _preOffset);
}

274
vector< boost::shared_ptr<Trace> > View::get_traces() const
DreamSourceLab's avatar
DreamSourceLab committed
275
{
276
    const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
DreamSourceLab's avatar
DreamSourceLab committed
277
#ifdef ENABLE_DECODE
278
    const vector< boost::shared_ptr<DecodeTrace> > decode_sigs(
DreamSourceLab's avatar
DreamSourceLab committed
279
        _session.get_decode_signals());
280
    vector< boost::shared_ptr<Trace> > traces(
DreamSourceLab's avatar
DreamSourceLab committed
281 282
        sigs.size() + decode_sigs.size());
#else
283
    vector< boost::shared_ptr<Trace> > traces(sigs.size());
DreamSourceLab's avatar
DreamSourceLab committed
284 285
#endif

286
    vector< boost::shared_ptr<Trace> >::iterator i = traces.begin();
DreamSourceLab's avatar
DreamSourceLab committed
287 288 289 290 291 292 293 294 295
    i = copy(sigs.begin(), sigs.end(), i);
#ifdef ENABLE_DECODE
    i = copy(decode_sigs.begin(), decode_sigs.end(), i);
#endif

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

296 297
bool View::compare_trace_v_offsets(const boost::shared_ptr<Trace> &a,
    const boost::shared_ptr<Trace> &b)
DreamSourceLab's avatar
DreamSourceLab committed
298 299 300
{
    assert(a);
    assert(b);
301 302 303 304
    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
305 306
}

DreamSourceLab's avatar
DreamSourceLab committed
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
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();
	_viewport->update();
}

void View::show_trig_cursor(bool show)
{
    _show_trig_cursor = show;
    _ruler->update();
    _viewport->update();
}

void View::show_search_cursor(bool show)
{
    _show_search_cursor = show;
    _ruler->update();
    _viewport->update();
}

void View::set_trig_pos(quint64 trig_pos)
{
345
    const double time = trig_pos * 1.0 / _session.get_device()->get_sample_rate();
DreamSourceLab's avatar
DreamSourceLab committed
346
    _trig_pos = trig_pos;
347
    _trig_cursor->set_index(trig_pos);
DreamSourceLab's avatar
DreamSourceLab committed
348
    _show_trig_cursor = true;
DreamSourceLab's avatar
DreamSourceLab committed
349
    set_scale_offset(_scale,  time - _scale * get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
350 351 352 353 354 355 356 357
    _ruler->update();
    _viewport->update();
}

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

358
    const double time = search_pos * 1.0 / _session.get_device()->get_sample_rate();
DreamSourceLab's avatar
DreamSourceLab committed
359
    _search_pos = search_pos;
360
    _search_cursor->set_index(search_pos);
DreamSourceLab's avatar
DreamSourceLab committed
361
    set_scale_offset(_scale,  time - _scale * get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    _ruler->update();
    _viewport->update();
}

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()
{
383
    const vector< boost::shared_ptr<Trace> > traces(get_traces());
DreamSourceLab's avatar
DreamSourceLab committed
384 385

	int v_min = INT_MAX;
386
    BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
387
        v_min = min(t->get_v_offset(), v_min);
DreamSourceLab's avatar
DreamSourceLab committed
388 389

	const int delta = -min(v_min, 0);
390
    BOOST_FOREACH(boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
391
        t->set_v_offset(t->get_v_offset() + delta);
DreamSourceLab's avatar
DreamSourceLab committed
392 393 394 395 396 397 398 399 400 401 402 403 404

	verticalScrollBar()->setSliderPosition(_v_offset + delta);
	v_scroll_value_changed(verticalScrollBar()->sliderPosition());
}


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

int View::get_signalHeight()
{
DreamSourceLab's avatar
DreamSourceLab committed
405
    return _signalHeight;
DreamSourceLab's avatar
DreamSourceLab committed
406 407 408 409
}

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

DreamSourceLab's avatar
DreamSourceLab committed
414
    length = _session.get_device()->get_sample_time() / _scale;
DreamSourceLab's avatar
DreamSourceLab committed
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
	offset = _offset / _scale;
}

void View::update_scroll()
{
	assert(_viewport);

	const QSize areaSize = _viewport->size();

	// 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());
	verticalScrollBar()->setRange(0,
DreamSourceLab's avatar
DreamSourceLab committed
447
        _viewport->get_total_height() - areaSize.height());
DreamSourceLab's avatar
DreamSourceLab committed
448 449
}

DreamSourceLab's avatar
DreamSourceLab committed
450
void View::update_scale()
DreamSourceLab's avatar
DreamSourceLab committed
451
{
DreamSourceLab's avatar
DreamSourceLab committed
452 453 454 455
    const uint64_t sample_rate = _session.get_device()->get_sample_rate();
    assert(sample_rate > 0);

    if (_session.get_device()->dev_inst()->mode != DSO) {
456
        _scale = (1.0 / sample_rate) / WellPixelsPerSample;
DreamSourceLab's avatar
DreamSourceLab committed
457
        _maxscale = _session.get_device()->get_sample_time() / (get_view_width() * MaxViewRate);
DreamSourceLab's avatar
DreamSourceLab committed
458
    } else {
459
        _scale = _session.get_device()->get_time_base() * 10.0 / get_view_width() * std::pow(10.0, -9.0);
DreamSourceLab's avatar
DreamSourceLab committed
460 461
        _maxscale = 1e9;
    }
DreamSourceLab's avatar
DreamSourceLab committed
462

463
    _minscale = (1.0 / sample_rate) / MaxPixelsPerSample;
DreamSourceLab's avatar
DreamSourceLab committed
464 465 466 467
    _offset = 0;
    _preScale = _scale;
    _preOffset = _offset;

468
    _trig_cursor->set_index(_trig_pos);
DreamSourceLab's avatar
DreamSourceLab committed
469 470 471 472 473 474 475 476

    _ruler->update();
    _viewport->update();
}

void View::signals_changed()
{
    int total_rows = 0;
477 478
    const vector< boost::shared_ptr<Trace> > traces(get_traces());
    BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
479 480 481 482 483 484
    {
        assert(t);
        if (dynamic_pointer_cast<DsoSignal>(t) ||
            t->enabled())
            total_rows += t->rows_size();
    }
DreamSourceLab's avatar
DreamSourceLab committed
485

DreamSourceLab's avatar
DreamSourceLab committed
486 487 488 489 490 491 492
    const double height = (_viewport->height()
                           - horizontalScrollBar()->height()
                           - 2 * SignalMargin * traces.size()) * 1.0 / total_rows;

    _signalHeight = (int)((height <= 0) ? 1 : height);
    _spanY = _signalHeight + 2 * SignalMargin;
    int next_v_offset = SignalMargin;
493
    BOOST_FOREACH(boost::shared_ptr<Trace> t, traces) {
DreamSourceLab's avatar
DreamSourceLab committed
494 495 496 497 498
        t->set_view(this);
        const double traceHeight = _signalHeight*t->rows_size();
        t->set_signalHeight((int)traceHeight);
        t->set_v_offset(next_v_offset + 0.5 * traceHeight + SignalMargin);
        next_v_offset += traceHeight + 2 * SignalMargin;
DreamSourceLab's avatar
DreamSourceLab committed
499
	}
DreamSourceLab's avatar
DreamSourceLab committed
500 501 502

    header_updated();
    normalize_layout();
DreamSourceLab's avatar
DreamSourceLab committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
}

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

		const QMouseEvent *const mouse_event = (QMouseEvent*)event;
        if (object == _ruler || object == _viewport) {
            //_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;
554
    int maxNameWidth = 25;
DreamSourceLab's avatar
DreamSourceLab committed
555 556
    int maxLeftWidth = 0;
    int maxRightWidth = 0;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
557

DreamSourceLab's avatar
v0.21  
DreamSourceLab committed
558 559 560
    QFont font = QApplication::font();
    QFontMetrics fm(font);

561
    const vector< boost::shared_ptr<Trace> > traces(get_traces());
DreamSourceLab's avatar
DreamSourceLab committed
562
    if (!traces.empty()){
563
        BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces) {
DreamSourceLab's avatar
DreamSourceLab committed
564 565 566
            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
567 568 569 570 571 572 573 574 575 576 577 578 579 580
        }
    }
    maxNameWidth = max(_header->get_nameEditWidth(), maxNameWidth);
    headerWidth = maxLeftWidth + maxNameWidth + maxRightWidth;

    setViewportMargins(headerWidth, RulerHeight, 0, 0);

    return headerWidth;
}

void View::resizeEvent(QResizeEvent*)
{
    update_margins();
    update_scroll();
DreamSourceLab's avatar
DreamSourceLab committed
581
    if (_session.get_device()->dev_inst()->mode == DSO)
DreamSourceLab's avatar
DreamSourceLab committed
582
        _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
583

584 585 586 587 588
    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
589 590 591 592
    _scale = min(_scale, _maxscale);

    signals_changed();
    _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
593 594 595 596 597 598 599 600 601 602 603
    _header->header_resize();
    _need_update = true;
}

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

    _preOffset = _offset;

DreamSourceLab's avatar
DreamSourceLab committed
604
    const double MinOffset = -(_scale * (get_view_width() * (1 - MaxViewRate)));
DreamSourceLab's avatar
DreamSourceLab committed
605
    const double MaxOffset = _session.get_device()->get_sample_time()
DreamSourceLab's avatar
DreamSourceLab committed
606
            - _scale * (get_view_width() * MaxViewRate);
DreamSourceLab's avatar
DreamSourceLab committed
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 636 637 638 639 640 641 642 643 644 645 646 647

	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;
	}

    _offset = max(min(_offset, MaxOffset), MinOffset);

    if (_offset != _preOffset) {
        _ruler->update();
        _viewport->update();
    }
}

void View::v_scroll_value_changed(int value)
{
	_v_offset = value;
	_header->update();
	_viewport->update();
}

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

	// Update the scroll bars
	update_scroll();

	// Repaint the view
    _need_update = true;
	_viewport->update();
}

void View::update_margins()
{
    _ruler->setGeometry(_viewport->x(), 0,
DreamSourceLab's avatar
DreamSourceLab committed
648
        get_view_width(), _viewport->y());
DreamSourceLab's avatar
DreamSourceLab committed
649 650
    _header->setGeometry(0, _viewport->y(),
        _viewport->x(), _viewport->height());
DreamSourceLab's avatar
DreamSourceLab committed
651 652
    _devmode->setGeometry(0, 0,
        _viewport->x(), _viewport->y());
DreamSourceLab's avatar
DreamSourceLab committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
}

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

    // Update the scroll bars
    update_scroll();

    _viewport->update();
    _header->update();
}

void View::marker_time_changed()
{
	_ruler->update();
	_viewport->update();
}

DreamSourceLab's avatar
DreamSourceLab committed
673
void View::on_traces_moved()
DreamSourceLab's avatar
DreamSourceLab committed
674 675
{
	update_scroll();
DreamSourceLab's avatar
DreamSourceLab committed
676
    _need_update = true;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
677
    _viewport->update();
DreamSourceLab's avatar
DreamSourceLab committed
678
    //traces_moved();
DreamSourceLab's avatar
DreamSourceLab committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
}

/*
 * 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;
}

704
void View::add_cursor(QColor color, uint64_t index)
DreamSourceLab's avatar
DreamSourceLab committed
705
{
706
    Cursor *newCursor = new Cursor(*this, color, index);
DreamSourceLab's avatar
DreamSourceLab committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
    _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++;
727
    set_scale_offset(_scale, (*i)->index() * 1.0 / _session.get_device()->get_sample_rate() - _scale * get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
728 729
}

730
Viewport * View::get_viewport()
DreamSourceLab's avatar
DreamSourceLab committed
731
{
732
    return _viewport;
DreamSourceLab's avatar
DreamSourceLab committed
733 734 735 736
}

QString View::get_cm_time(int index)
{
737
    return _ruler->format_real_time(get_cursor_samples(index), _session.get_device()->get_sample_rate());
DreamSourceLab's avatar
DreamSourceLab committed
738 739 740 741 742 743 744
}

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

745 746 747 748
    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
749 750
}

DreamSourceLab's avatar
DreamSourceLab committed
751 752
uint64_t View::get_cursor_samples(int index)
{
753
    assert(index < (int)_cursorList.size());
DreamSourceLab's avatar
DreamSourceLab committed
754

755 756 757 758 759 760 761 762
    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
763 764
}

DreamSourceLab's avatar
DreamSourceLab committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
void View::on_cursor_moved()
{
    cursor_moved();
}

void View::set_measure_en(int enable)
{
    _viewport->set_measure_en(enable);
}

void View::on_state_changed(bool stop)
{
    if (stop)
        _viewport->stop_trigger_timer();
}

DreamSourceLab's avatar
DreamSourceLab committed
781
int View::get_view_width()
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
782
{
DreamSourceLab's avatar
DreamSourceLab committed
783 784
    int view_width = 0;
    if (_session.get_device()->dev_inst()->mode == DSO) {
785 786
        const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
        BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
DreamSourceLab's avatar
DreamSourceLab committed
787 788 789 790
            view_width = max((double)view_width, s->get_view_rect().width());
        }
    } else {
        view_width = _viewport->width();
DreamSourceLab's avatar
DreamSourceLab committed
791
    }
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
792

DreamSourceLab's avatar
DreamSourceLab committed
793
    return view_width;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
794 795
}

DreamSourceLab's avatar
DreamSourceLab committed
796 797
} // namespace view
} // namespace pv