protocoldock.cpp 9.62 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
 *
 * 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 "protocoldock.h"
#include "../sigsession.h"
DreamSourceLab's avatar
DreamSourceLab committed
26 27
#include "../view/decodetrace.h"
#include "../device/devinst.h"
DreamSourceLab's avatar
DreamSourceLab committed
28 29 30 31 32

#include <QObject>
#include <QHBoxLayout>
#include <QPainter>
#include <QMessageBox>
DreamSourceLab's avatar
DreamSourceLab committed
33 34 35
#include <QFormLayout>

#include <boost/shared_ptr.hpp>
DreamSourceLab's avatar
DreamSourceLab committed
36 37 38 39 40

namespace pv {
namespace dock {

ProtocolDock::ProtocolDock(QWidget *parent, SigSession &session) :
41
    QScrollArea(parent),
DreamSourceLab's avatar
DreamSourceLab committed
42 43
    _session(session)
{
44 45
    _widget = new QWidget(this);

DreamSourceLab's avatar
DreamSourceLab committed
46 47
    QHBoxLayout *hori_layout = new QHBoxLayout();

48
    _add_button = new QPushButton(_widget);
DreamSourceLab's avatar
DreamSourceLab committed
49 50 51
    _add_button->setFlat(true);
    _add_button->setIcon(QIcon::fromTheme("protocol",
                         QIcon(":/icons/add.png")));
52
    _del_all_button = new QPushButton(_widget);
DreamSourceLab's avatar
DreamSourceLab committed
53 54 55 56
    _del_all_button->setFlat(true);
    _del_all_button->setIcon(QIcon::fromTheme("protocol",
                             QIcon(":/icons/del.png")));
    _del_all_button->setCheckable(true);
57
    _protocol_combobox = new QComboBox(_widget);
DreamSourceLab's avatar
DreamSourceLab committed
58 59 60 61 62 63 64 65 66 67 68 69

    GSList *l = g_slist_sort(g_slist_copy(
        (GSList*)srd_decoder_list()), decoder_name_cmp);
    for(; l; l = l->next)
    {
        const srd_decoder *const d = (srd_decoder*)l->data;
        assert(d);

        const bool have_probes = (d->channels || d->opt_channels) != 0;
        if (true == have_probes) {
            _protocol_combobox->addItem(QString::fromUtf8(d->name), qVariantFromValue(l->data));
        }
DreamSourceLab's avatar
DreamSourceLab committed
70
    }
DreamSourceLab's avatar
DreamSourceLab committed
71 72
    g_slist_free(l);

DreamSourceLab's avatar
DreamSourceLab committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    hori_layout->addWidget(_add_button);
    hori_layout->addWidget(_del_all_button);
    hori_layout->addWidget(_protocol_combobox);
    hori_layout->addStretch(1);

    connect(_add_button, SIGNAL(clicked()),
            this, SLOT(add_protocol()));
    connect(_del_all_button, SIGNAL(clicked()),
            this, SLOT(del_protocol()));

    _layout = new QVBoxLayout();
    _layout->addLayout(hori_layout);
    _layout->addStretch(1);

87 88 89 90 91
    _widget->setLayout(_layout);

    this->setWidget(_widget);
    _widget->setGeometry(0, 0, sizeHint().width(), 500);
    _widget->setObjectName("protocolWidget");
DreamSourceLab's avatar
DreamSourceLab committed
92 93 94 95 96 97
}

ProtocolDock::~ProtocolDock()
{
}

DreamSourceLab's avatar
DreamSourceLab committed
98 99 100 101 102 103
int ProtocolDock::decoder_name_cmp(const void *a, const void *b)
{
    return strcmp(((const srd_decoder*)a)->name,
        ((const srd_decoder*)b)->name);
}

DreamSourceLab's avatar
DreamSourceLab committed
104 105 106 107 108 109 110 111 112 113
void ProtocolDock::paintEvent(QPaintEvent *)
{
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void ProtocolDock::add_protocol()
{
DreamSourceLab's avatar
DreamSourceLab committed
114
    if (_session.get_device()->dev_inst()->mode != LOGIC) {
DreamSourceLab's avatar
DreamSourceLab committed
115
        QMessageBox msg(this);
DreamSourceLab's avatar
DreamSourceLab committed
116 117
        msg.setText(tr("Protocol Analyzer"));
        msg.setInformativeText(tr("Protocol Analyzer is only valid in Digital Mode!"));
DreamSourceLab's avatar
DreamSourceLab committed
118 119 120 121
        msg.setStandardButtons(QMessageBox::Ok);
        msg.setIcon(QMessageBox::Warning);
        msg.exec();
    } else {
DreamSourceLab's avatar
DreamSourceLab committed
122 123 124 125 126 127
        srd_decoder *const decoder =
            (srd_decoder*)(_protocol_combobox->itemData(_protocol_combobox->currentIndex())).value<void*>();
        if (_session.add_decoder(decoder)) {
            //std::list <int > _sel_probes = dlg.get_sel_probes();
            //QMap <QString, QVariant>& _options = dlg.get_options();
            //QMap <QString, int> _options_index = dlg.get_options_index();
DreamSourceLab's avatar
DreamSourceLab committed
128

129 130
            QPushButton *_del_button = new QPushButton(_widget);
            QPushButton *_set_button = new QPushButton(_widget);
DreamSourceLab's avatar
DreamSourceLab committed
131 132 133 134 135
            _del_button->setFlat(true);
            _del_button->setIcon(QIcon::fromTheme("protocol",
                                 QIcon(":/icons/del.png")));
            _set_button->setFlat(true);
            _set_button->setIcon(QIcon::fromTheme("protocol",
DreamSourceLab's avatar
DreamSourceLab committed
136
                                 QIcon(":/icons/gear.png")));
137
            QLabel *_protocol_label = new QLabel(_widget);
DreamSourceLab's avatar
DreamSourceLab committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

            _del_button->setCheckable(true);
            _protocol_label->setText(_protocol_combobox->currentText());

            connect(_del_button, SIGNAL(clicked()),
                    this, SLOT(del_protocol()));
            connect(_set_button, SIGNAL(clicked()),
                    this, SLOT(rst_protocol()));

            _del_button_list.push_back(_del_button);
            _set_button_list.push_back(_set_button);
            _protocol_label_list.push_back(_protocol_label);
            _protocol_index_list.push_back(_protocol_combobox->currentIndex());

            QHBoxLayout *hori_layout = new QHBoxLayout();
            hori_layout->addWidget(_set_button);
            hori_layout->addWidget(_del_button);
            hori_layout->addWidget(_protocol_label);
            hori_layout->addStretch(1);
            _hori_layout_list.push_back(hori_layout);
            _layout->insertLayout(_del_button_list.size(), hori_layout);

DreamSourceLab's avatar
DreamSourceLab committed
160
            //_session.add_protocol_analyzer(_protocol_combobox->currentIndex(), _sel_probes, _options, _options_index);
DreamSourceLab's avatar
DreamSourceLab committed
161 162 163 164 165 166 167 168 169 170 171
        }
    }
}

void ProtocolDock::rst_protocol()
{
    int rst_index = 0;
    for (QVector <QPushButton *>::const_iterator i = _set_button_list.begin();
         i != _set_button_list.end(); i++) {
        QPushButton *button = qobject_cast<QPushButton *>(sender());
        if ((*i) == button) {
DreamSourceLab's avatar
DreamSourceLab committed
172 173 174 175 176 177 178 179 180 181
            //pv::decoder::DemoConfig dlg(this, _session.get_device(), _protocol_index_list.at(rst_index));
            //dlg.set_config(_session.get_decode_probes(rst_index), _session.get_decode_options_index(rst_index));
            //if (dlg.exec()) {
                //std::list <int > _sel_probes = dlg.get_sel_probes();
                //QMap <QString, QVariant>& _options = dlg.get_options();
                //QMap <QString, int> _options_index = dlg.get_options_index();

                //_session.rst_protocol_analyzer(rst_index, _sel_probes, _options, _options_index);
            //}
            _session.rst_decoder(rst_index);
DreamSourceLab's avatar
DreamSourceLab committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
            break;
        }
        rst_index++;
    }
}

void ProtocolDock::del_protocol()
{
    if (_del_all_button->isChecked()) {
        _del_all_button->setChecked(false);
        if (_hori_layout_list.size() > 0) {
            int del_index = 0;
            for (QVector <QHBoxLayout *>::const_iterator i = _hori_layout_list.begin();
                 i != _hori_layout_list.end(); i++) {
                _layout->removeItem((*i));
                delete (*i);
                delete _del_button_list.at(del_index);
                delete _set_button_list.at(del_index);
                delete _protocol_label_list.at(del_index);

DreamSourceLab's avatar
DreamSourceLab committed
202
                _session.remove_decode_signal(0);
DreamSourceLab's avatar
DreamSourceLab committed
203 204 205 206 207 208 209 210 211
                del_index++;
            }
            _hori_layout_list.clear();
            _del_button_list.clear();
            _set_button_list.clear();
            _protocol_label_list.clear();
            _protocol_index_list.clear();
        } else {
            QMessageBox msg(this);
DreamSourceLab's avatar
DreamSourceLab committed
212 213
            msg.setText(tr("Protocol Analyzer"));
            msg.setInformativeText(tr("No Protocol Analyzer to delete!"));
DreamSourceLab's avatar
DreamSourceLab committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
            msg.setStandardButtons(QMessageBox::Ok);
            msg.setIcon(QMessageBox::Warning);
            msg.exec();
        }
    } else {
       int del_index = 0;
       for (QVector <QPushButton *>::const_iterator i = _del_button_list.begin();
            i != _del_button_list.end(); i++) {
           if ((*i)->isChecked()) {
               _layout->removeItem(_hori_layout_list.at(del_index));

               delete _hori_layout_list.at(del_index);
               delete _del_button_list.at(del_index);
               delete _set_button_list.at(del_index);
               delete _protocol_label_list.at(del_index);

               _hori_layout_list.remove(del_index);
               _del_button_list.remove(del_index);
               _set_button_list.remove(del_index);
               _protocol_label_list.remove(del_index);
               _protocol_index_list.remove(del_index);

DreamSourceLab's avatar
DreamSourceLab committed
236
               _session.remove_decode_signal(del_index);
DreamSourceLab's avatar
DreamSourceLab committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

               break;
           }
           del_index++;
       }
    }
}

void ProtocolDock::del_all_protocol()
{
    if (_hori_layout_list.size() > 0) {
        int del_index = 0;
        for (QVector <QHBoxLayout *>::const_iterator i = _hori_layout_list.begin();
             i != _hori_layout_list.end(); i++) {
            _layout->removeItem((*i));
            delete (*i);
            delete _del_button_list.at(del_index);
            delete _set_button_list.at(del_index);
            delete _protocol_label_list.at(del_index);

DreamSourceLab's avatar
DreamSourceLab committed
257
            _session.remove_decode_signal(0);
DreamSourceLab's avatar
DreamSourceLab committed
258 259 260 261 262 263 264 265 266 267 268 269
            del_index++;
        }
        _hori_layout_list.clear();
        _del_button_list.clear();
        _set_button_list.clear();
        _protocol_label_list.clear();
        _protocol_index_list.clear();
    }
}

} // namespace dock
} // namespace pv