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

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

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

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

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

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

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

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

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

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

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

    set_scale_offset(_preScale, _preOffset);
}

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

281
    vector< boost::shared_ptr<Trace> >::iterator i = traces.begin();
DreamSourceLab's avatar
DreamSourceLab committed
282 283 284 285
    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
286
    i = copy(groups.begin(), groups.end(), i);
DreamSourceLab's avatar
DreamSourceLab committed
287 288 289 290 291

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

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

DreamSourceLab's avatar
DreamSourceLab committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
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)
{
341
    const double time = trig_pos * 1.0 / _session.get_device()->get_sample_rate();
DreamSourceLab's avatar
DreamSourceLab committed
342
    _trig_pos = trig_pos;
343
    _trig_cursor->set_index(trig_pos);
DreamSourceLab's avatar
DreamSourceLab committed
344
    _show_trig_cursor = true;
DreamSourceLab's avatar
DreamSourceLab committed
345
    set_scale_offset(_scale,  time - _scale * get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
346 347 348 349 350 351 352 353
    _ruler->update();
    _viewport->update();
}

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

354
    const double time = search_pos * 1.0 / _session.get_device()->get_sample_rate();
DreamSourceLab's avatar
DreamSourceLab committed
355
    _search_pos = search_pos;
356
    _search_cursor->set_index(search_pos);
DreamSourceLab's avatar
DreamSourceLab committed
357
    set_scale_offset(_scale,  time - _scale * get_view_width() / 2);
DreamSourceLab's avatar
DreamSourceLab committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    _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()
{
379
    const vector< boost::shared_ptr<Trace> > traces(get_traces());
DreamSourceLab's avatar
DreamSourceLab committed
380 381

	int v_min = INT_MAX;
382
    BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces)
DreamSourceLab's avatar
DreamSourceLab committed
383
        v_min = min(t->get_v_offset(), v_min);
DreamSourceLab's avatar
DreamSourceLab committed
384 385

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

	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
401
    return _signalHeight;
DreamSourceLab's avatar
DreamSourceLab committed
402 403 404 405
}

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

DreamSourceLab's avatar
DreamSourceLab committed
410
    length = _session.get_device()->get_sample_time() / _scale;
DreamSourceLab's avatar
DreamSourceLab committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	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
443
        _viewport->get_total_height() - areaSize.height());
DreamSourceLab's avatar
DreamSourceLab committed
444 445
}

DreamSourceLab's avatar
DreamSourceLab committed
446
void View::update_scale()
DreamSourceLab's avatar
DreamSourceLab committed
447
{
DreamSourceLab's avatar
DreamSourceLab committed
448 449 450 451
    const uint64_t sample_rate = _session.get_device()->get_sample_rate();
    assert(sample_rate > 0);

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

459
    _minscale = (1.0 / sample_rate) / MaxPixelsPerSample;
DreamSourceLab's avatar
DreamSourceLab committed
460 461 462 463
    _offset = 0;
    _preScale = _scale;
    _preOffset = _offset;

464
    _trig_cursor->set_index(_trig_pos);
DreamSourceLab's avatar
DreamSourceLab committed
465 466 467 468 469 470 471 472

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

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

DreamSourceLab's avatar
DreamSourceLab committed
482 483 484 485 486 487 488
    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;
489
    BOOST_FOREACH(boost::shared_ptr<Trace> t, traces) {
DreamSourceLab's avatar
DreamSourceLab committed
490 491 492 493 494
        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
495
	}
DreamSourceLab's avatar
DreamSourceLab committed
496 497 498

    header_updated();
    normalize_layout();
DreamSourceLab's avatar
DreamSourceLab committed
499 500 501 502 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
}

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;
550
    int maxNameWidth = 25;
DreamSourceLab's avatar
DreamSourceLab committed
551 552
    int maxLeftWidth = 0;
    int maxRightWidth = 0;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
553

DreamSourceLab's avatar
v0.21  
DreamSourceLab committed
554 555 556
    QFont font = QApplication::font();
    QFontMetrics fm(font);

557
    const vector< boost::shared_ptr<Trace> > traces(get_traces());
DreamSourceLab's avatar
DreamSourceLab committed
558
    if (!traces.empty()){
559
        BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces) {
DreamSourceLab's avatar
DreamSourceLab committed
560 561 562
            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
563 564 565 566 567 568 569 570 571 572 573 574 575 576
        }
    }
    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
577
    if (_session.get_device()->dev_inst()->mode == DSO)
DreamSourceLab's avatar
DreamSourceLab committed
578
        _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
579

580 581 582 583 584
    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
585 586 587 588
    _scale = min(_scale, _maxscale);

    signals_changed();
    _ruler->update();
DreamSourceLab's avatar
DreamSourceLab committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    _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;
	}

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

    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
640
        get_view_width(), _viewport->y());
DreamSourceLab's avatar
DreamSourceLab committed
641 642
    _header->setGeometry(0, _viewport->y(),
        _viewport->x(), _viewport->height());
DreamSourceLab's avatar
DreamSourceLab committed
643 644
    _devmode->setGeometry(0, 0,
        _viewport->x(), _viewport->y());
DreamSourceLab's avatar
DreamSourceLab committed
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
}

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
665
void View::on_traces_moved()
DreamSourceLab's avatar
DreamSourceLab committed
666 667
{
	update_scroll();
DreamSourceLab's avatar
DreamSourceLab committed
668
    _need_update = true;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
669
    _viewport->update();
DreamSourceLab's avatar
DreamSourceLab committed
670
    //traces_moved();
DreamSourceLab's avatar
DreamSourceLab committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
}

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

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

722
Viewport * View::get_viewport()
DreamSourceLab's avatar
DreamSourceLab committed
723
{
724
    return _viewport;
DreamSourceLab's avatar
DreamSourceLab committed
725 726 727 728
}

QString View::get_cm_time(int index)
{
729
    return _ruler->format_real_time(get_cursor_samples(index), _session.get_device()->get_sample_rate());
DreamSourceLab's avatar
DreamSourceLab committed
730 731 732 733 734 735 736
}

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

737 738 739 740
    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
741 742
}

DreamSourceLab's avatar
DreamSourceLab committed
743 744
uint64_t View::get_cursor_samples(int index)
{
745
    assert(index < (int)_cursorList.size());
DreamSourceLab's avatar
DreamSourceLab committed
746

747 748 749 750 751 752 753 754
    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
755 756
}

DreamSourceLab's avatar
DreamSourceLab committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
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
773
int View::get_view_width()
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
774
{
DreamSourceLab's avatar
DreamSourceLab committed
775 776
    int view_width = 0;
    if (_session.get_device()->dev_inst()->mode == DSO) {
777 778
        const vector< boost::shared_ptr<Signal> > sigs(_session.get_signals());
        BOOST_FOREACH(const boost::shared_ptr<Signal> s, sigs) {
DreamSourceLab's avatar
DreamSourceLab committed
779 780 781 782
            view_width = max((double)view_width, s->get_view_rect().width());
        }
    } else {
        view_width = _viewport->width();
DreamSourceLab's avatar
DreamSourceLab committed
783
    }
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
784

DreamSourceLab's avatar
DreamSourceLab committed
785
    return view_width;
DreamSourceLab's avatar
v0.3  
DreamSourceLab committed
786 787
}

788 789 790 791 792 793 794 795 796 797 798
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
799 800
} // namespace view
} // namespace pv