measuredock.cpp 11.4 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
 *
 * 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
 */

DreamSourceLab's avatar
DreamSourceLab committed
23
#include <math.h>
DreamSourceLab's avatar
DreamSourceLab committed
24 25 26 27 28

#include "measuredock.h"
#include "../sigsession.h"
#include "../view/cursor.h"
#include "../view/view.h"
29
#include "../view/viewport.h"
DreamSourceLab's avatar
DreamSourceLab committed
30 31
#include "../view/timemarker.h"
#include "../view/ruler.h"
DreamSourceLab's avatar
DreamSourceLab committed
32 33 34
#include "../view/logicsignal.h"
#include "../data/signaldata.h"
#include "../data/snapshot.h"
35 36 37
#include "../devicemanager.h"
#include "../device/device.h"
#include "../device/file.h"
DreamSourceLab's avatar
DreamSourceLab committed
38 39 40 41 42

#include <QObject>
#include <QPainter>
#include <QRegExpValidator>

DreamSourceLab's avatar
DreamSourceLab committed
43
#include "libsigrok4DSL/libsigrok.h"
DreamSourceLab's avatar
DreamSourceLab committed
44

DreamSourceLab's avatar
DreamSourceLab committed
45 46
using boost::shared_ptr;

DreamSourceLab's avatar
DreamSourceLab committed
47 48 49 50 51 52
namespace pv {
namespace dock {

using namespace pv::view;

MeasureDock::MeasureDock(QWidget *parent, View &view, SigSession &session) :
53
    QScrollArea(parent),
DreamSourceLab's avatar
DreamSourceLab committed
54 55 56 57
    _session(session),
    _view(view)
{

58 59 60 61 62
    _widget = new QWidget(this);
    //_widget->setSizePolicy();

    _mouse_groupBox = new QGroupBox(tr("Mouse measurement"), _widget);
    _fen_checkBox = new QCheckBox(tr("Enable floating measurement"), _widget);
DreamSourceLab's avatar
DreamSourceLab committed
63
    _fen_checkBox->setChecked(true);
64 65 66 67
    _width_label = new QLabel("#####", _widget);
    _period_label = new QLabel("#####", _widget);
    _freq_label = new QLabel("#####", _widget);
    _duty_label = new QLabel("#####", _widget);
DreamSourceLab's avatar
DreamSourceLab committed
68 69

    _mouse_layout = new QGridLayout();
70 71
    _mouse_layout->addWidget(_fen_checkBox, 0, 0, 1, 4);
    _mouse_layout->addWidget(new QLabel(tr("W: "), _widget), 1, 0);
DreamSourceLab's avatar
DreamSourceLab committed
72
    _mouse_layout->addWidget(_width_label, 1, 1);
73 74 75 76 77 78 79 80 81 82
    _mouse_layout->addWidget(new QLabel(tr("P: "), _widget), 1, 2);
    _mouse_layout->addWidget(_period_label, 1, 3);
    _mouse_layout->addWidget(new QLabel(tr("F: "), _widget), 2, 2);
    _mouse_layout->addWidget(_freq_label, 2, 3);
    _mouse_layout->addWidget(new QLabel(tr("D: "), _widget), 2, 0);
    _mouse_layout->addWidget(_duty_label, 2, 1);
    _mouse_layout->addWidget(new QLabel(_widget), 0, 4);
    _mouse_layout->addWidget(new QLabel(_widget), 1, 4);
    _mouse_layout->addWidget(new QLabel(_widget), 2, 4);
    _mouse_layout->setColumnStretch(5, 1);
DreamSourceLab's avatar
DreamSourceLab committed
83 84 85
    _mouse_groupBox->setLayout(_mouse_layout);


86 87 88 89
    _cursor_groupBox = new QGroupBox(tr("Cursor measurement"), _widget);
    _t1_comboBox = new QComboBox(_widget);
    _t2_comboBox = new QComboBox(_widget);
    _t3_comboBox = new QComboBox(_widget);
90 91 92
    _delta_label_t1t2 = new QLabel("##########/##########", _widget);
    _delta_label_t2t3 = new QLabel("##########/##########", _widget);
    _delta_label_t1t3 = new QLabel("##########/##########", _widget);
DreamSourceLab's avatar
DreamSourceLab committed
93 94 95
    _t1_last_index = 0;
    _t2_last_index = 0;
    _t3_last_index = 0;
DreamSourceLab's avatar
DreamSourceLab committed
96

97 98
    _cursor_layout = new QGridLayout(_widget);
    _cursor_layout->addWidget(new QLabel(tr("T1: "), _widget), 0, 0);
DreamSourceLab's avatar
DreamSourceLab committed
99
    _cursor_layout->addWidget(_t1_comboBox, 0, 1);
100
    _cursor_layout->addWidget(new QLabel(tr("T2: "), _widget), 1, 0);
DreamSourceLab's avatar
DreamSourceLab committed
101
    _cursor_layout->addWidget(_t2_comboBox, 1, 1);
102
    _cursor_layout->addWidget(new QLabel(tr("T3: "), _widget), 2, 0);
DreamSourceLab's avatar
DreamSourceLab committed
103 104
    _cursor_layout->addWidget(_t3_comboBox, 2, 1);

105 106 107
    _cursor_layout->addWidget(new QLabel(tr("Time/Samples"), _widget), 3, 1, 1, 2);
    _cursor_layout->addWidget(new QLabel(tr("|T2 - T1|: "), _widget), 4, 0);
    _cursor_layout->addWidget(_delta_label_t1t2, 4, 1, 1, 2);
DreamSourceLab's avatar
DreamSourceLab committed
108

109 110
    _cursor_layout->addWidget(new QLabel(tr("|T3 - T2|: "), _widget), 5, 0);
    _cursor_layout->addWidget(_delta_label_t2t3, 5, 1, 1, 2);
DreamSourceLab's avatar
DreamSourceLab committed
111

112 113
    _cursor_layout->addWidget(new QLabel(tr("|T3 - T1|: "), _widget), 6, 0);
    _cursor_layout->addWidget(_delta_label_t1t3, 6, 1, 1, 2);
DreamSourceLab's avatar
DreamSourceLab committed
114

115 116 117
    _cursor_layout->addWidget(new QLabel(_widget), 7, 0);
    _cursor_layout->addWidget(new QLabel(tr("Cursors"), _widget), 8, 0);
    _cursor_layout->addWidget(new QLabel(tr("Time/Samples"), _widget), 8, 1, 1, 2);
DreamSourceLab's avatar
DreamSourceLab committed
118

119 120 121 122
    _cursor_layout->addWidget(new QLabel(_widget), 0, 2);
    _cursor_layout->addWidget(new QLabel(_widget), 1, 2);
    _cursor_layout->addWidget(new QLabel(_widget), 2, 2);
    _cursor_layout->setColumnStretch(2, 1);
DreamSourceLab's avatar
DreamSourceLab committed
123 124 125

    _cursor_groupBox->setLayout(_cursor_layout);

126
    QVBoxLayout *layout = new QVBoxLayout(_widget);
DreamSourceLab's avatar
DreamSourceLab committed
127 128 129
    layout->addWidget(_mouse_groupBox);
    layout->addWidget(_cursor_groupBox);
    layout->addStretch(1);
130
    _widget->setLayout(layout);
DreamSourceLab's avatar
DreamSourceLab committed
131 132 133

    connect(_t1_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(delta_update()));
    connect(_t2_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(delta_update()));
DreamSourceLab's avatar
DreamSourceLab committed
134
    connect(_t3_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(delta_update()));
DreamSourceLab's avatar
DreamSourceLab committed
135 136

    connect(_fen_checkBox, SIGNAL(stateChanged(int)), &_view, SLOT(set_measure_en(int)));
137
    connect(_view.get_viewport(), SIGNAL(mouse_measure()), this, SLOT(mouse_measure()));
138 139 140 141

    this->setWidget(_widget);
    _widget->setGeometry(0, 0, sizeHint().width(), 2000);
    _widget->setObjectName("measureWidget");
DreamSourceLab's avatar
DreamSourceLab committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
}

MeasureDock::~MeasureDock()
{
}

void MeasureDock::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void MeasureDock::cursor_update()
{
DreamSourceLab's avatar
DreamSourceLab committed
158 159
    using namespace pv::data;

DreamSourceLab's avatar
DreamSourceLab committed
160 161
    int index = 1;

DreamSourceLab's avatar
DreamSourceLab committed
162 163 164
    disconnect(_t1_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(delta_update()));
    disconnect(_t2_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(delta_update()));
    disconnect(_t3_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(delta_update()));
DreamSourceLab's avatar
DreamSourceLab committed
165 166
    _t1_comboBox->clear();
    _t2_comboBox->clear();
DreamSourceLab's avatar
DreamSourceLab committed
167
    _t3_comboBox->clear();
DreamSourceLab's avatar
DreamSourceLab committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    if (!_cursor_pushButton_list.empty()) {
        for (QVector<QPushButton *>::Iterator i = _cursor_pushButton_list.begin();
             i != _cursor_pushButton_list.end(); i++)
            delete (*i);
        for (QVector<QLabel *>::Iterator i = _curpos_label_list.begin();
             i != _curpos_label_list.end(); i++)
            delete (*i);
        for (QVector<QLabel *>::Iterator i = _space_label_list.begin();
             i != _space_label_list.end(); i++)
            delete (*i);

        _cursor_pushButton_list.clear();
        _curpos_label_list.clear();
        _space_label_list.clear();
    }

    for(std::list<Cursor*>::iterator i = _view.get_cursorList().begin();
        i != _view.get_cursorList().end(); i++) {
DreamSourceLab's avatar
DreamSourceLab committed
187
        QString curCursor = tr("Cursor ")+QString::number(index);
DreamSourceLab's avatar
DreamSourceLab committed
188 189
        _t1_comboBox->addItem(curCursor);
        _t2_comboBox->addItem(curCursor);
DreamSourceLab's avatar
DreamSourceLab committed
190
        _t3_comboBox->addItem(curCursor);
DreamSourceLab's avatar
DreamSourceLab committed
191

192
        QPushButton *_cursor_pushButton = new QPushButton(curCursor, _widget);
DreamSourceLab's avatar
DreamSourceLab committed
193
        QString _cur_text = _view.get_cm_time(index - 1) + "/" + QString::number(_view.get_cursor_samples(index - 1));
194 195
        QLabel *_curpos_label = new QLabel(_cur_text, _widget);
        QLabel *_space_label = new QLabel(_widget);
DreamSourceLab's avatar
DreamSourceLab committed
196 197 198 199
        _cursor_pushButton_list.push_back(_cursor_pushButton);
        _curpos_label_list.push_back(_curpos_label);
        _space_label_list.push_back(_space_label);

200 201
        _cursor_layout->addWidget(_cursor_pushButton, 8 + index, 0);
        _cursor_layout->addWidget(_curpos_label, 8 + index, 1, 1, 2);
DreamSourceLab's avatar
DreamSourceLab committed
202 203 204 205 206 207

        connect(_cursor_pushButton, SIGNAL(clicked()), this, SLOT(goto_cursor()));

        index++;
    }

DreamSourceLab's avatar
DreamSourceLab committed
208 209 210 211 212 213 214 215 216 217 218 219
    if (_t1_last_index < _t1_comboBox->count())
        _t1_comboBox->setCurrentIndex(_t1_last_index);
    if (_t2_last_index < _t2_comboBox->count())
        _t2_comboBox->setCurrentIndex(_t2_last_index);
    if (_t3_last_index < _t3_comboBox->count())
        _t3_comboBox->setCurrentIndex(_t3_last_index);

    connect(_t1_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(delta_update()));
    connect(_t2_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(delta_update()));
    connect(_t3_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(delta_update()));

    delta_update();
DreamSourceLab's avatar
DreamSourceLab committed
220 221 222
    update();
}

223
void MeasureDock::mouse_measure()
DreamSourceLab's avatar
DreamSourceLab committed
224
{
225 226 227 228
    _width_label->setText(_view.get_viewport()->get_measure("width"));
    _period_label->setText(_view.get_viewport()->get_measure("period"));
    _freq_label->setText(_view.get_viewport()->get_measure("frequency"));
    _duty_label->setText(_view.get_viewport()->get_measure("duty"));
DreamSourceLab's avatar
DreamSourceLab committed
229 230 231 232 233 234 235 236 237
}

void MeasureDock::cursor_moved()
{
    //TimeMarker* grabbed_marker = _view.get_ruler()->get_grabbed_cursor();
    if (_view.cursors_shown()) {
        int index = 0;
        for(std::list<Cursor*>::iterator i = _view.get_cursorList().begin();
            i != _view.get_cursorList().end(); i++) {
DreamSourceLab's avatar
DreamSourceLab committed
238 239
            QString _cur_text = _view.get_cm_time(index) + "/" + QString::number(_view.get_cursor_samples(index));
            _curpos_label_list.at(index)->setText(_cur_text);
DreamSourceLab's avatar
DreamSourceLab committed
240 241 242 243 244 245 246 247 248
            //_curvalue_label_list.at(index)->setText(_view.get_cm_value(index));
            index++;
        }
    }
    delta_update();
}

void MeasureDock::delta_update()
{
249
    QString delta_text;
DreamSourceLab's avatar
DreamSourceLab committed
250 251 252
    _t1_last_index = std::max(_t1_comboBox->currentIndex(), 0);
    _t2_last_index = std::max(_t2_comboBox->currentIndex(), 0);
    _t3_last_index = std::max(_t3_comboBox->currentIndex(), 0);
DreamSourceLab's avatar
DreamSourceLab committed
253
    if (_t1_comboBox->count() != 0 && _t2_comboBox->count() != 0) {
DreamSourceLab's avatar
DreamSourceLab committed
254 255
        uint64_t delta = abs(_view.get_cursor_samples(_t1_last_index) -
                             _view.get_cursor_samples(_t2_last_index));
256 257 258
        delta_text = _view.get_cm_delta(_t1_last_index, _t2_last_index) +
                             "/" + QString::number(delta);
        _delta_label_t1t2->setText(delta_text);
DreamSourceLab's avatar
DreamSourceLab committed
259 260 261 262 263
    }

    if (_t2_comboBox->count() != 0 && _t2_comboBox->count() != 0) {
        uint64_t delta = abs(_view.get_cursor_samples(_t2_last_index) -
                             _view.get_cursor_samples(_t3_last_index));
264 265 266
        delta_text = _view.get_cm_delta(_t2_last_index, _t3_last_index) +
                             "/" + QString::number(delta);
        _delta_label_t2t3->setText(delta_text);
DreamSourceLab's avatar
DreamSourceLab committed
267 268 269 270 271
    }

    if (_t1_comboBox->count() != 0 && _t3_comboBox->count() != 0) {
        uint64_t delta = abs(_view.get_cursor_samples(_t1_last_index) -
                             _view.get_cursor_samples(_t3_last_index));
272 273 274
        delta_text = _view.get_cm_delta(_t1_last_index, _t3_last_index) +
                             "/" + QString::number(delta);
        _delta_label_t1t3->setText(delta_text);
DreamSourceLab's avatar
DreamSourceLab committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    }
}

void MeasureDock::goto_cursor()
{
    int index = 0;

    for (QVector<QPushButton *>::Iterator i = _cursor_pushButton_list.begin();
         i != _cursor_pushButton_list.end(); i++) {
        QPushButton *button = qobject_cast<QPushButton *>(sender());
        if ((*i) == button) {
            _view.set_cursor_middle(index);
            break;
        }
        index++;
    }
}

} // namespace dock
} // namespace pv