]> sigrok.org Git - pulseview.git/blob - pv/views/trace/mathsignal.cpp
MathSignal: Add config dialog
[pulseview.git] / pv / views / trace / mathsignal.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net>
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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <extdef.h>
21
22 #include <QComboBox>
23 #include <QDebug>
24 #include <QDialogButtonBox>
25 #include <QFormLayout>
26 #include <QGridLayout>
27 #include <QLineEdit>
28 #include <QString>
29
30 #include "mathsignal.hpp"
31
32 #include "pv/data/signalbase.hpp"
33
34 using pv::data::SignalBase;
35
36 namespace pv {
37 namespace views {
38 namespace trace {
39
40 #define MATHSIGNAL_INPUT_TIMEOUT (2000)
41
42
43 MathEditDialog::MathEditDialog(pv::Session &session,
44         shared_ptr<pv::data::MathSignal> math_signal, QWidget *parent) :
45         QDialog(parent),
46         session_(session),
47         math_signal_(math_signal),
48         expression_(math_signal->get_expression()),
49         old_expression_(math_signal->get_expression())
50 {
51         setWindowTitle(tr("Math Expression Editor"));
52
53         // Create the rest of the dialog
54         QDialogButtonBox *button_box = new QDialogButtonBox(
55                 QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
56
57         QVBoxLayout* root_layout = new QVBoxLayout(this);
58 //      root_layout->addLayout(tab_layout);
59         root_layout->addWidget(button_box);
60
61         connect(button_box, SIGNAL(accepted()), this, SLOT(accept()));
62         connect(button_box, SIGNAL(rejected()), this, SLOT(reject()));
63 }
64
65 void MathEditDialog::accept()
66 {
67         math_signal_->set_expression(expression_);
68         QDialog::accept();
69 }
70
71 void MathEditDialog::reject()
72 {
73         math_signal_->set_expression(old_expression_);
74         QDialog::reject();
75 }
76
77
78 MathSignal::MathSignal(
79         pv::Session &session,
80         shared_ptr<data::SignalBase> base) :
81         AnalogSignal(session, base),
82         math_signal_(dynamic_pointer_cast<pv::data::MathSignal>(base))
83 {
84         delayed_expr_updater_.setSingleShot(true);
85         delayed_expr_updater_.setInterval(MATHSIGNAL_INPUT_TIMEOUT);
86         connect(&delayed_expr_updater_, &QTimer::timeout,
87                 this, [this]() { math_signal_->set_expression(expression_edit_->text()); });
88 }
89
90 void MathSignal::populate_popup_form(QWidget *parent, QFormLayout *form)
91 {
92         AnalogSignal::populate_popup_form(parent, form);
93
94         expression_edit_ = new QLineEdit();
95         expression_edit_->setText(math_signal_->get_expression());
96
97         const QIcon edit_icon(QIcon::fromTheme("edit", QIcon(":/icons/math.svg")));
98         QAction *edit_action =
99                 expression_edit_->addAction(edit_icon, QLineEdit::TrailingPosition);
100
101         connect(expression_edit_, SIGNAL(textEdited(QString)),
102                 this, SLOT(on_expression_changed(QString)));
103         connect(edit_action, SIGNAL(triggered(bool)),
104                 this, SLOT(on_edit_clicked()));
105         form->addRow(tr("Expression"), expression_edit_);
106
107         sample_count_cb_ = new QComboBox();
108         sample_count_cb_->setEditable(true);
109         sample_count_cb_->addItem(tr("same as session"));
110         sample_count_cb_->addItem(tr("100"));
111         sample_count_cb_->addItem(tr("10000"));
112         sample_count_cb_->addItem(tr("1000000"));
113         connect(sample_count_cb_, SIGNAL(editTextChanged(QString)),
114                 this, SLOT(on_sample_count_changed(QString)));
115         form->addRow(tr("Number of Samples"), sample_count_cb_);
116
117         sample_rate_cb_ = new QComboBox();
118         sample_rate_cb_->setEditable(true);
119         sample_rate_cb_->addItem(tr("same as session"));
120         sample_rate_cb_->addItem(tr("100"));
121         sample_rate_cb_->addItem(tr("10000"));
122         sample_rate_cb_->addItem(tr("1000000"));
123         form->addRow(tr("Sample rate"), sample_rate_cb_);
124 }
125
126 void MathSignal::on_expression_changed(const QString &text)
127 {
128         (void)text;
129
130         // Restart update timer
131         delayed_expr_updater_.start();
132 }
133
134 void MathSignal::on_sample_count_changed(const QString &text)
135 {
136         (void)text;
137 }
138
139 void MathSignal::on_edit_clicked()
140 {
141         MathEditDialog dlg(session_, math_signal_);
142
143         dlg.exec();
144 }
145
146 } // namespace trace
147 } // namespace views
148 } // namespace pv