view.cpp 21 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 44
#include "view.h"
#include "viewport.h"

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

//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
70
View::View(SigSession &session, pv::toolbars::SamplingBar *sampling_bar, QWidget *parent) :
DreamSourceLab's avatar
DreamSourceLab committed
71 72
	QAbstractScrollArea(parent),
	_session(session),
DreamSourceLab's avatar
DreamSourceLab committed
73
    _sampling_bar(sampling_bar),
DreamSourceLab's avatar
DreamSourceLab committed
74 75 76
	_viewport(new Viewport(*this)),
	_ruler(new Ruler(*this)),
	_header(new Header(*this)),
DreamSourceLab's avatar
DreamSourceLab committed
77
    _devmode(new DevMode(*this)),
DreamSourceLab's avatar
DreamSourceLab committed
78 79 80 81 82 83 84 85 86 87
    _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
88
    _trig_pos(0),
DreamSourceLab's avatar
DreamSourceLab committed
89 90 91 92 93 94 95 96 97
	_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
98 99 100
    setViewportMargins(headerWidth(), RulerHeight, 0, 0);
    setViewport(_viewport);

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

DreamSourceLab's avatar
DreamSourceLab committed
108 109 110 111 112 113 114
    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
115 116 117 118 119 120
    connect(_header, SIGNAL(header_updated()),
        this, SLOT(header_updated()));

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

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

    _show_trig_cursor = false;
128
    _trig_cursor = new Cursor(*this, Trace::dsLightRed, 0);
DreamSourceLab's avatar
DreamSourceLab committed
129 130
    _show_search_cursor = false;
    _search_pos = 0;
131
    _search_cursor = new Cursor(*this, Trace::dsLightBlue, _search_pos);
DreamSourceLab's avatar
DreamSourceLab committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
}

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
166
    zoom(steps, get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
167 168 169 170 171 172 173 174 175 176 177 178
}

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

        if (_session.get_device()->dev_inst()->mode != DSO) {
            _offset = cursor_offset - _scale * offset;
            _offset = max(min(_offset, get_max_offset()), get_min_offset());
        }
DreamSourceLab's avatar
DreamSourceLab committed
234 235

        if (_scale != _preScale || _offset != _preOffset) {
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
236
            _header->update();
DreamSourceLab's avatar
DreamSourceLab committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250
            _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
251
        _scale = max(min(scale, _maxscale), _minscale);
252
        _offset = max(min(offset, get_max_offset()), get_min_offset());
DreamSourceLab's avatar
DreamSourceLab committed
253 254 255

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

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

    set_scale_offset(_preScale, _preOffset);
}

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

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

    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
    uint8_t max_height = MaxHeightUnit;
478 479
    const vector< boost::shared_ptr<Trace> > traces(get_traces());
    BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
480 481 482 483 484 485
    {
        assert(t);
        if (dynamic_pointer_cast<DsoSignal>(t) ||
            t->enabled())
            total_rows += t->rows_size();
    }
DreamSourceLab's avatar
DreamSourceLab committed
486

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

491 492 493 494 495 496 497 498 499 500
    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 {
        _signalHeight = (int)((height <= 0) ? 1 : height);
    }
DreamSourceLab's avatar
DreamSourceLab committed
501 502
    _spanY = _signalHeight + 2 * SignalMargin;
    int next_v_offset = SignalMargin;
503
    BOOST_FOREACH(boost::shared_ptr<Trace> t, traces) {
DreamSourceLab's avatar
DreamSourceLab committed
504 505 506 507 508
        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
509
	}
DreamSourceLab's avatar
DreamSourceLab committed
510

511
    _viewport->clear_measure();
DreamSourceLab's avatar
DreamSourceLab committed
512 513
    header_updated();
    normalize_layout();
DreamSourceLab's avatar
DreamSourceLab committed
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 554 555 556 557 558 559 560 561 562 563 564
}

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;
565
    int maxNameWidth = 25;
DreamSourceLab's avatar
DreamSourceLab committed
566 567
    int maxLeftWidth = 0;
    int maxRightWidth = 0;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
568

DreamSourceLab's avatar
v0.21  
DreamSourceLab committed
569 570 571
    QFont font = QApplication::font();
    QFontMetrics fm(font);

572
    const vector< boost::shared_ptr<Trace> > traces(get_traces());
DreamSourceLab's avatar
DreamSourceLab committed
573
    if (!traces.empty()){
574
        BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces) {
DreamSourceLab's avatar
DreamSourceLab committed
575 576 577
            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
578 579 580 581 582 583 584 585 586 587 588 589 590 591
        }
    }
    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
592
    if (_session.get_device()->dev_inst()->mode == DSO)
DreamSourceLab's avatar
DreamSourceLab committed
593
        _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
594

595 596 597 598 599
    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
600 601 602 603
    _scale = min(_scale, _maxscale);

    signals_changed();
    _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    _header->header_resize();
    _need_update = true;
}

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

624
    _offset = max(min(_offset, get_max_offset()), get_min_offset());
DreamSourceLab's avatar
DreamSourceLab committed
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

    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
655
        get_view_width(), _viewport->y());
DreamSourceLab's avatar
DreamSourceLab committed
656 657
    _header->setGeometry(0, _viewport->y(),
        _viewport->x(), _viewport->height());
DreamSourceLab's avatar
DreamSourceLab committed
658 659
    _devmode->setGeometry(0, 0,
        _viewport->x(), _viewport->y());
DreamSourceLab's avatar
DreamSourceLab committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
}

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
680
void View::on_traces_moved()
DreamSourceLab's avatar
DreamSourceLab committed
681 682
{
	update_scroll();
DreamSourceLab's avatar
DreamSourceLab committed
683
    _need_update = true;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
684
    _viewport->update();
DreamSourceLab's avatar
DreamSourceLab committed
685
    //traces_moved();
DreamSourceLab's avatar
DreamSourceLab committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
}

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

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

737
Viewport * View::get_viewport()
DreamSourceLab's avatar
DreamSourceLab committed
738
{
739
    return _viewport;
DreamSourceLab's avatar
DreamSourceLab committed
740 741 742 743
}

QString View::get_cm_time(int index)
{
744
    return _ruler->format_real_time(get_cursor_samples(index), _session.get_device()->get_sample_rate());
DreamSourceLab's avatar
DreamSourceLab committed
745 746 747 748 749 750 751
}

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

752 753 754 755
    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
756 757
}

DreamSourceLab's avatar
DreamSourceLab committed
758 759
uint64_t View::get_cursor_samples(int index)
{
760
    assert(index < (int)_cursorList.size());
DreamSourceLab's avatar
DreamSourceLab committed
761

762 763 764 765 766 767 768 769
    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
770 771
}

DreamSourceLab's avatar
DreamSourceLab committed
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
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
788
int View::get_view_width()
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
789
{
DreamSourceLab's avatar
DreamSourceLab committed
790 791
    int view_width = 0;
    if (_session.get_device()->dev_inst()->mode == DSO) {
792 793
        const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
        BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
DreamSourceLab's avatar
DreamSourceLab committed
794 795 796 797
            view_width = max((double)view_width, s->get_view_rect().width());
        }
    } else {
        view_width = _viewport->width();
DreamSourceLab's avatar
DreamSourceLab committed
798
    }
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
799

DreamSourceLab's avatar
DreamSourceLab committed
800
    return view_width;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
801 802
}

803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
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 {
        view_height = _viewport->width();
    }

    return view_height;
}

818 819 820 821 822 823 824 825 826 827 828
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);
}

DreamSourceLab's avatar
DreamSourceLab committed
829 830
} // namespace view
} // namespace pv