]>
Commit | Line | Data |
---|---|---|
97378470 SA |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2018 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 <algorithm> | |
21 | ||
22 | #include <QDebug> | |
23 | #include <QInputDialog> | |
24 | #include <QLabel> | |
25 | #include <QPushButton> | |
26 | #include <QVBoxLayout> | |
27 | ||
28 | #include "pv/session.hpp" | |
29 | #include "pv/subwindows/decoder_selector/subwindow.hpp" | |
30 | ||
31 | using std::reverse; | |
32 | using std::shared_ptr; | |
33 | ||
34 | namespace pv { | |
35 | namespace subwindows { | |
36 | namespace decoder_selector { | |
37 | ||
38 | ||
e10848e8 SA |
39 | void QCustomTreeView::currentChanged(const QModelIndex& current, |
40 | const QModelIndex& previous) | |
41 | { | |
42 | QTreeView::currentChanged(current, previous); | |
43 | currentChanged(current); | |
44 | } | |
45 | ||
46 | ||
97378470 SA |
47 | SubWindow::SubWindow(Session& session, QWidget* parent) : |
48 | SubWindowBase(session, parent), | |
49 | splitter_(new QSplitter()), | |
e10848e8 | 50 | tree_view_(new QCustomTreeView()), |
c2b80ad9 SA |
51 | info_box_(new QWidget()), |
52 | info_label_header_(new QLabel()), | |
53 | info_label_body_(new QLabel()), | |
54 | info_label_footer_(new QLabel()), | |
97378470 SA |
55 | model_(new DecoderCollectionModel()) |
56 | { | |
57 | QVBoxLayout* root_layout = new QVBoxLayout(this); | |
58 | root_layout->setContentsMargins(0, 0, 0, 0); | |
59 | root_layout->addWidget(splitter_); | |
60 | ||
c2b80ad9 | 61 | splitter_->setOrientation(Qt::Vertical); |
97378470 | 62 | splitter_->addWidget(tree_view_); |
c2b80ad9 | 63 | splitter_->addWidget(info_box_); |
97378470 SA |
64 | |
65 | tree_view_->setModel(model_); | |
66 | tree_view_->setRootIsDecorated(true); | |
67 | ||
68 | // Hide the columns that hold the detailed item information | |
69 | tree_view_->hideColumn(2); // ID | |
70 | ||
c2b80ad9 SA |
71 | info_box_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
72 | QVBoxLayout* info_box_layout = new QVBoxLayout(info_box_); | |
73 | info_box_layout->addWidget(info_label_header_); | |
74 | info_box_layout->addWidget(info_label_body_); | |
75 | info_box_layout->addWidget(info_label_footer_); | |
76 | info_label_body_->setWordWrap(true); | |
77 | info_label_body_->setText(tr("Select a decoder to see its description here.")); | |
78 | ||
e10848e8 SA |
79 | connect(tree_view_, SIGNAL(currentChanged(const QModelIndex&)), |
80 | this, SLOT(on_item_changed(const QModelIndex&))); | |
81 | connect(tree_view_, SIGNAL(activated(const QModelIndex&)), | |
82 | this, SLOT(on_item_activated(const QModelIndex&))); | |
97378470 SA |
83 | |
84 | connect(this, SIGNAL(new_decoders_selected(vector<const srd_decoder*>)), | |
85 | &session, SLOT(on_new_decoders_selected(vector<const srd_decoder*>))); | |
86 | } | |
87 | ||
88 | bool SubWindow::has_toolbar() const | |
89 | { | |
90 | return true; | |
91 | } | |
92 | ||
93 | QToolBar* SubWindow::create_toolbar(QWidget *parent) const | |
94 | { | |
95 | QToolBar* toolbar = new QToolBar(parent); | |
96 | ||
97 | return toolbar; | |
98 | } | |
99 | ||
100 | const srd_decoder* SubWindow::get_srd_decoder_from_id(QString id) const | |
101 | { | |
102 | const srd_decoder* ret_val = nullptr; | |
103 | ||
104 | GSList* l = g_slist_copy((GSList*)srd_decoder_list()); | |
105 | for (GSList* li = l; li; li = li->next) { | |
106 | const srd_decoder* d = (srd_decoder*)li->data; | |
107 | assert(d); | |
108 | ||
109 | if (QString::fromUtf8(d->id) == id) | |
110 | ret_val = d; | |
111 | } | |
112 | g_slist_free(l); | |
113 | ||
114 | return ret_val; | |
115 | } | |
116 | ||
117 | vector<const char*> SubWindow::decoder_inputs(const srd_decoder* d) const | |
118 | { | |
119 | vector<const char*> ret_val; | |
120 | ||
121 | GSList* l = g_slist_copy(d->inputs); | |
122 | for (GSList* li = l; li; li = li->next) { | |
123 | const char* input = (const char*)li->data; | |
124 | ret_val.push_back(input); | |
125 | } | |
126 | g_slist_free(l); | |
127 | ||
128 | return ret_val; | |
129 | } | |
130 | ||
131 | vector<const srd_decoder*> SubWindow::decoders_providing(const char* output) const | |
132 | { | |
133 | vector<const srd_decoder*> ret_val; | |
134 | ||
135 | GSList* l = g_slist_copy((GSList*)srd_decoder_list()); | |
136 | for (GSList* li = l; li; li = li->next) { | |
137 | const srd_decoder* d = (srd_decoder*)li->data; | |
138 | assert(d); | |
139 | ||
140 | if (!d->outputs) | |
141 | continue; | |
142 | ||
143 | // TODO For now we ignore that d->outputs is actually a list | |
144 | if (strncmp((char*)(d->outputs->data), output, strlen(output)) == 0) | |
145 | ret_val.push_back(d); | |
146 | } | |
147 | g_slist_free(l); | |
148 | ||
149 | return ret_val; | |
150 | } | |
151 | ||
e10848e8 | 152 | void SubWindow::on_item_changed(const QModelIndex& index) |
c2b80ad9 SA |
153 | { |
154 | if (!index.isValid()) | |
155 | return; | |
156 | ||
157 | QModelIndex id_index = index.model()->index(index.row(), 2, index.parent()); | |
158 | QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString(); | |
159 | ||
160 | if (decoder_name.isEmpty()) | |
161 | return; | |
162 | ||
163 | const srd_decoder* d = get_srd_decoder_from_id(decoder_name); | |
164 | ||
165 | const QString id = QString::fromUtf8(d->id); | |
166 | const QString longname = QString::fromUtf8(d->longname); | |
167 | const QString desc = QString::fromUtf8(d->desc); | |
168 | const QString doc = QString::fromUtf8(srd_decoder_doc_get(d)); | |
169 | ||
170 | QString tags; | |
171 | GSList* l = g_slist_copy((GSList*)d->tags); | |
172 | for (GSList* li = l; li; li = li->next) { | |
173 | QString s = (li == l) ? | |
174 | tr((char*)li->data) : | |
175 | QString(tr(", %1")).arg(tr((char*)li->data)); | |
176 | tags.append(s); | |
177 | } | |
178 | g_slist_free(l); | |
179 | ||
180 | info_label_header_->setText(QString("<span style='font-size:large;font-weight:bold'>%1 (%2)</span><br>%3") | |
181 | .arg(longname, id, desc)); | |
182 | info_label_body_->setText(doc); | |
183 | info_label_footer_->setText(tr("<p align='right'>Tags: %1</p>").arg(tags)); | |
184 | } | |
185 | ||
e10848e8 | 186 | void SubWindow::on_item_activated(const QModelIndex& index) |
97378470 SA |
187 | { |
188 | if (!index.isValid()) | |
189 | return; | |
190 | ||
191 | QModelIndex id_index = index.model()->index(index.row(), 2, index.parent()); | |
192 | QString decoder_name = index.model()->data(id_index, Qt::DisplayRole).toString(); | |
193 | ||
194 | const srd_decoder* chosen_decoder = get_srd_decoder_from_id(decoder_name); | |
195 | if (chosen_decoder == nullptr) | |
196 | return; | |
197 | ||
198 | vector<const srd_decoder*> decoders; | |
199 | decoders.push_back(chosen_decoder); | |
200 | ||
201 | // If the decoder only depends on logic inputs, we add it and are done | |
202 | vector<const char*> inputs = decoder_inputs(decoders.front()); | |
203 | if (inputs.size() == 0) { | |
204 | qWarning() << "Protocol decoder" << decoder_name << "cannot have 0 inputs!"; | |
205 | return; | |
206 | } | |
207 | ||
208 | if (strncmp(inputs.at(0), "logic", 5) == 0) { | |
209 | new_decoders_selected(decoders); | |
210 | return; | |
211 | } | |
212 | ||
213 | // Check if we can automatically fulfill the stacking requirements | |
214 | while (strncmp(inputs.at(0), "logic", 5) != 0) { | |
215 | vector<const srd_decoder*> prov_decoders = decoders_providing(inputs.at(0)); | |
216 | ||
217 | if (prov_decoders.size() == 0) { | |
218 | // Emit warning and add the stack that we could gather so far | |
219 | qWarning() << "Protocol decoder" << QString::fromUtf8(decoders.back()->id) \ | |
220 | << "has input that no other decoder provides:" << QString::fromUtf8(inputs.at(0)); | |
221 | break; | |
222 | } | |
223 | ||
224 | if (prov_decoders.size() == 1) { | |
225 | decoders.push_back(prov_decoders.front()); | |
226 | } else { | |
227 | // Let user decide which one to use | |
228 | QString caption = QString(tr("Protocol decoder <b>%1</b> requires input type <b>%2</b> " \ | |
229 | "which several decoders provide.<br>Choose which one to use:<br>")) | |
230 | .arg(QString::fromUtf8(decoders.back()->id), QString::fromUtf8(inputs.at(0))); | |
231 | ||
232 | QStringList items; | |
233 | for (const srd_decoder* d : prov_decoders) | |
234 | items << QString::fromUtf8(d->id) + " (" + QString::fromUtf8(d->longname) + ")"; | |
235 | bool ok_clicked; | |
236 | QString item = QInputDialog::getItem(this, tr("Choose Decoder"), | |
237 | tr(caption.toUtf8()), items, 0, false, &ok_clicked); | |
238 | ||
239 | if ((!ok_clicked) || (item.isEmpty())) | |
240 | return; | |
241 | ||
242 | QString d = item.section(' ', 0, 0); | |
243 | decoders.push_back(get_srd_decoder_from_id(d)); | |
244 | } | |
245 | ||
246 | inputs = decoder_inputs(decoders.back()); | |
247 | } | |
248 | ||
249 | // Reverse decoder list and add the stack | |
250 | reverse(decoders.begin(), decoders.end()); | |
251 | new_decoders_selected(decoders); | |
252 | } | |
253 | ||
254 | } // namespace decoder_selector | |
255 | } // namespace subwindows | |
256 | } // namespace pv |