]> sigrok.org Git - pulseview.git/blob - pv/widgets/sweeptimingwidget.cpp
b1164891419898bd588c511edc3ab45e58edf70f
[pulseview.git] / pv / widgets / sweeptimingwidget.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include "sweeptimingwidget.h"
22
23 #include <assert.h>
24
25 namespace pv {
26 namespace widgets {
27
28 SweepTimingWidget::SweepTimingWidget(const char *suffix,
29         QWidget *parent) :
30         QWidget(parent),
31         _layout(this),
32         _read_only_value(this),
33         _value(this),
34         _list(this),
35         _value_type(None)
36 {
37         setContentsMargins(0, 0, 0, 0);
38
39         _value.setDecimals(0);
40         _value.setSuffix(QString::fromUtf8(suffix));
41
42         connect(&_list, SIGNAL(currentIndexChanged(int)),
43                 this, SIGNAL(value_changed()));
44         connect(&_value, SIGNAL(editingFinished()),
45                 this, SIGNAL(value_changed()));
46
47         setLayout(&_layout);
48         _layout.setMargin(0);
49         _layout.addWidget(&_read_only_value);
50         _layout.addWidget(&_list);
51         _layout.addWidget(&_value);
52
53         show_none();
54 }
55
56 void SweepTimingWidget::show_none()
57 {
58         _value_type = None;
59         _read_only_value.hide();
60         _value.hide();
61         _list.hide();
62 }
63
64 void SweepTimingWidget::show_read_only()
65 {
66         _value_type = ReadOnly;
67         _read_only_value.show();
68         _value.hide();
69         _list.hide();
70 }
71
72 void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
73         uint64_t step)
74 {
75         _value_type = MinMaxStep;
76
77         _value.setRange(min, max);
78         _value.setSingleStep(step);
79
80         _read_only_value.hide();
81         _value.show();
82         _list.hide();
83 }
84
85 void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
86 {
87         _value_type = List;
88
89         _list.clear();
90         for (size_t i = 0; i < count; i++)
91         {
92                 char *const s = sr_samplerate_string(vals[i]);
93                 _list.addItem(QString::fromUtf8(s),
94                         qVariantFromValue(vals[i]));
95                 g_free(s);
96         }
97
98         _read_only_value.hide();
99         _value.hide();
100         _list.show();
101 }
102
103 uint64_t SweepTimingWidget::value() const
104 {
105         switch(_value_type)
106         {
107         case None:
108         case ReadOnly:
109                 return 0;
110
111         case MinMaxStep:
112                 return (uint64_t)_value.value();
113
114         case List:
115         {
116                 const int index = _list.currentIndex();
117                 return (index >= 0) ? _list.itemData(
118                         index).value<uint64_t>() : 0;
119         }
120
121         default:
122                 // Unexpected value type
123                 assert(0);
124                 return 0;
125         }
126 }
127
128 void SweepTimingWidget::set_value(uint64_t value)
129 {
130         _read_only_value.setText(QString("%1").arg(value));
131
132         _value.setValue(value);
133
134         for (int i = 0; i < _list.count(); i++)
135                 if (value == _list.itemData(i).value<uint64_t>())
136                         _list.setCurrentIndex(i);
137 }
138
139 } // widgets
140 } // pv