PulseView  unreleased development snapshot
A Qt-based sigrok GUI
signal.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012 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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <extdef.h>
21 
22 #include <cassert>
23 #include <cmath>
24 
25 #include <QApplication>
26 #include <QFormLayout>
27 #include <QKeyEvent>
28 #include <QLineEdit>
29 #include <QMenu>
30 
31 #include <libsigrokcxx/libsigrokcxx.hpp>
32 
33 #include "pv/data/signalbase.hpp"
34 
35 #include "signal.hpp"
36 #include "view.hpp"
37 
38 using std::shared_ptr;
39 
40 namespace pv {
41 namespace views {
42 namespace trace {
43 
44 const char *const ChannelNames[] = {
45  "CLK",
46  "DATA",
47  "EN",
48  "IN",
49  "OUT",
50  "RST",
51  "TX",
52  "RX",
53  "SDA",
54  "SCL",
55  "SCLK",
56  "MOSI",
57  "MISO",
58  "/CS",
59  "nCS",
60  "/SS",
61  "nSS",
62  "/RST",
63  "nRST",
64 };
65 
67  shared_ptr<data::SignalBase> signal) :
68  Trace(signal),
69  session_(session),
70  name_widget_(nullptr)
71 {
72  assert(base_);
73 
74  connect(base_.get(), SIGNAL(enabled_changed(bool)),
75  this, SLOT(on_enabled_changed(bool)));
76 }
77 
78 void Signal::set_name(QString name)
79 {
80  base_->set_name(name);
81 
82  if (name != name_widget_->currentText())
83  name_widget_->setEditText(name);
84 }
85 
86 bool Signal::enabled() const
87 {
88  return base_->enabled();
89 }
90 
91 shared_ptr<data::SignalBase> Signal::base() const
92 {
93  return base_;
94 }
95 
96 void Signal::save_settings(QSettings &settings) const
97 {
98  std::map<QString, QVariant> settings_map = save_settings();
99 
100  for (auto& entry : settings_map)
101  settings.setValue(entry.first, entry.second);
102 }
103 
104 std::map<QString, QVariant> Signal::save_settings() const
105 {
106  return std::map<QString, QVariant>();
107 }
108 
110 {
111  std::map<QString, QVariant> settings_map;
112 
113  QStringList keys = settings.allKeys();
114  for (int i = 0; i < keys.size(); i++)
115  settings_map[keys.at(i)] = settings.value(keys.at(i));
116 
117  restore_settings(settings_map);
118 }
119 
120 void Signal::restore_settings(std::map<QString, QVariant> settings)
121 {
122  (void)settings;
123 }
124 
125 
127 {
128  if (base_->enabled())
129  Trace::paint_back(p, pp);
130 }
131 
132 void Signal::populate_popup_form(QWidget *parent, QFormLayout *form)
133 {
134  name_widget_ = new QComboBox(parent);
135  name_widget_->setEditable(true);
136  name_widget_->setCompleter(nullptr);
137 
138  for (unsigned int i = 0; i < countof(ChannelNames); i++)
139  name_widget_->insertItem(i, ChannelNames[i]);
140 
141  const int index = name_widget_->findText(base_->name(), Qt::MatchExactly);
142 
143  if (index == -1) {
144  name_widget_->insertItem(0, base_->name());
145  name_widget_->setCurrentIndex(0);
146  } else {
147  name_widget_->setCurrentIndex(index);
148  }
149 
150  connect(name_widget_, SIGNAL(editTextChanged(const QString&)),
151  this, SLOT(on_nameedit_changed(const QString&)));
152 
153  form->addRow(tr("Name"), name_widget_);
154 
155  add_color_option(parent, form);
156 }
157 
158 QMenu* Signal::create_header_context_menu(QWidget *parent)
159 {
160  QMenu *const menu = Trace::create_header_context_menu(parent);
161 
162  menu->addSeparator();
163 
164  QString caption;
165 
166  if (base_->is_generated())
167  caption = tr("Remove");
168  else
169  caption = tr("Disable");
170 
171  QAction *const a = new QAction(caption, this);
172  a->setShortcuts(QKeySequence::Delete);
173  connect(a, SIGNAL(triggered()), this, SLOT(on_disable()));
174  menu->addAction(a);
175 
176  return menu;
177 }
178 
180 {
181  on_disable();
182 }
183 
184 void Signal::on_name_changed(const QString &text)
185 {
186  // On startup, this event is fired when a session restores signal
187  // names. However, the name widget hasn't yet been created.
188  if (!name_widget_)
189  return;
190 
191  if (text != name_widget_->currentText())
192  name_widget_->setEditText(text);
193 
195 }
196 
198 {
199  // For generated signals, "disable" means "remove"
200  if (base_->is_generated())
202  else
203  base_->set_enabled(false);
204 }
205 
207 {
208  (void)enabled;
209 
210  if (owner_)
211  owner_->extents_changed(true, true);
212 }
213 
214 } // namespace trace
215 } // namespace views
216 } // namespace pv
QComboBox * name_widget_
Definition: signal.hpp:113
void paint_back(QPainter &p, ViewItemPaintParams &pp)
Definition: signal.cpp:126
virtual void set_name(QString name)
Definition: signal.cpp:78
Signal(pv::Session &session, shared_ptr< data::SignalBase > signal)
Definition: signal.cpp:66
const char *const ChannelNames[]
Definition: signal.cpp:44
virtual void paint_back(QPainter &p, ViewItemPaintParams &pp)
Definition: trace.cpp:299
bool enabled() const
Definition: signal.cpp:86
virtual void extents_changed(bool horz, bool vert)=0
void on_nameedit_changed(const QString &name)
Definition: trace.cpp:442
QMenu * create_header_context_menu(QWidget *parent)
Definition: signal.cpp:158
virtual void on_name_changed(const QString &text)
Definition: trace.cpp:408
shared_ptr< data::SignalBase > base_
Definition: trace.hpp:208
void on_enabled_changed(bool enabled)
Definition: signal.cpp:206
virtual QMenu * create_header_context_menu(QWidget *parent)
Definition: trace.cpp:201
shared_ptr< data::SignalBase > base() const
Definition: signal.cpp:91
pv::Session & session_
Definition: signal.hpp:111
virtual void populate_popup_form(QWidget *parent, QFormLayout *form)
Definition: signal.cpp:132
#define countof(x)
Definition: extdef.h:23
virtual std::map< QString, QVariant > save_settings() const
Definition: signal.cpp:104
void add_color_option(QWidget *parent, QFormLayout *form)
Definition: trace.cpp:327
virtual void restore_settings(QSettings &settings)
Definition: signal.cpp:109
void remove_generated_signal(shared_ptr< data::SignalBase > signal)
Definition: session.cpp:984
virtual void on_name_changed(const QString &text)
Definition: signal.cpp:184