summaryrefslogtreecommitdiff
path: root/DSLogic-gui/pv/toolbars/devicebar.cpp
blob: 3d38a31cc426b2e58fe43dbe0cabbf069ccad4d6 (plain)
1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * This file is part of the DSLogic-gui project.
 * DSLogic-gui is based on PulseView.
 *
 * 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 <extdef.h>

#include <assert.h>

#include <boost/foreach.hpp>

#include <libsigrok4DSLogic/libsigrok.h>

#include <QAction>
#include <QDebug>
#include <QMessageBox>

#include "devicebar.h"

#include <pv/devicemanager.h>
#include <pv/dialogs/deviceoptions.h>

using namespace std;

namespace pv {
namespace toolbars {

DeviceBar::DeviceBar(QWidget *parent) :
    QToolBar("Device Bar", parent),
    _enable(true),
    _device_selector(this),
    _configure_button(this)
{
    setMovable(false);

    connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
        this, SLOT(on_device_selected()));
    connect(&_configure_button, SIGNAL(clicked()),
        this, SLOT(on_configure()));

    _configure_button.setIcon(QIcon::fromTheme("configure",
        QIcon(":/icons/params.png")));

    addWidget(&_device_selector);
    addWidget(&_configure_button);
}

void DeviceBar::set_device_list(
    const std::list<struct sr_dev_inst*> &devices)
{
    disconnect(&_device_selector, SIGNAL(currentIndexChanged (int)),
        this, SLOT(on_device_selected()));
    _device_selector.clear();

    BOOST_FOREACH (sr_dev_inst *sdi, devices) {
        const string title = DeviceManager::format_device_title(sdi);
        _device_selector.addItem(title.c_str(),
            qVariantFromValue((void*)sdi));
    }
    connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
        this, SLOT(on_device_selected()));
}

struct sr_dev_inst* DeviceBar::get_selected_device() const
{
    const int index = _device_selector.currentIndex();
    if (index < 0)
        return NULL;

    return (sr_dev_inst*)_device_selector.itemData(
        index).value<void*>();
}

void DeviceBar::set_selected_device(struct sr_dev_inst *const sdi, bool discon)
{
    if (discon)
        disconnect(&_device_selector, SIGNAL(currentIndexChanged (int)),
            this, SLOT(on_device_selected()));

    for (int i = 0; i < _device_selector.count(); i++)
        if (sdi == _device_selector.itemData(i).value<void*>()) {
            _device_selector.setCurrentIndex(i);
            if (!discon)
                device_selected();
            break;
        }

    if (discon)
        connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
            this, SLOT(on_device_selected()));
}

void DeviceBar::on_device_selected()
{
    device_selected();
}

void DeviceBar::on_configure()
{
    int  dev_mode, ret;
    sr_dev_inst *const sdi = get_selected_device();
    assert(sdi);

    dev_mode = sdi->mode;

    pv::dialogs::DeviceOptions dlg(this, sdi);
    ret = dlg.exec();
    if (ret == QDialog::Accepted) {
        if (dev_mode != sdi->mode) {
            device_selected();
        } else {
            device_updated();
        }
    }
}

void DeviceBar::enable_toggle(bool enable)
{
    _device_selector.setDisabled(!enable);
    _configure_button.setDisabled(!enable);
}

} // namespace toolbars
} // namespace pv