2 * This file is part of the PulseView project.
4 * Copyright (C) 2018 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
22 #include "subwindow.hpp"
24 #include <libsigrokdecode/libsigrokdecode.h>
26 using std::make_shared;
29 namespace subwindows {
30 namespace decoder_selector {
32 DecoderCollectionModel::DecoderCollectionModel(QObject* parent) :
33 QAbstractItemModel(parent)
35 vector<QVariant> header_data;
36 header_data.emplace_back(tr("Decoder")); // Column #0
37 header_data.emplace_back(tr("Name")); // Column #1
38 header_data.emplace_back(tr("ID")); // Column #2
39 root_ = make_shared<DecoderCollectionItem>(header_data);
41 // Note: the tag groups are sub-items of the root item
43 // Create "all decoders" group
44 vector<QVariant> item_data;
45 item_data.emplace_back(tr("All Decoders"));
46 // Add dummy entries to make the row count the same as the
47 // sub-item size, or else we can't query sub-item data
48 item_data.emplace_back();
49 item_data.emplace_back();
50 shared_ptr<DecoderCollectionItem> group_item_all =
51 make_shared<DecoderCollectionItem>(item_data, root_);
52 root_->appendSubItem(group_item_all);
54 for (GSList* li = (GSList*)srd_decoder_list(); li; li = li->next) {
55 const srd_decoder *const d = (srd_decoder*)li->data;
58 const QString id = QString::fromUtf8(d->id);
59 const QString name = QString::fromUtf8(d->name);
60 const QString long_name = QString::fromUtf8(d->longname);
62 // Add decoder to the "all decoders" group
64 item_data.emplace_back(name);
65 item_data.emplace_back(long_name);
66 item_data.emplace_back(id);
67 shared_ptr<DecoderCollectionItem> decoder_item_all =
68 make_shared<DecoderCollectionItem>(item_data, group_item_all);
69 group_item_all->appendSubItem(decoder_item_all);
71 // Add decoder to all relevant groups using the tag information
72 for (GSList* ti = (GSList*)d->tags; ti; ti = ti->next) {
73 const QString tag = tr((char*)ti->data);
74 const QVariant tag_var = QVariant(tag);
76 // Find tag group and create it if it doesn't exist yet
77 shared_ptr<DecoderCollectionItem> group_item =
78 root_->findSubItem(tag_var, 0);
82 item_data.emplace_back(tag);
83 // Add dummy entries to make the row count the same as the
84 // sub-item size, or else we can't query sub-item data
85 item_data.emplace_back();
86 item_data.emplace_back();
87 group_item = make_shared<DecoderCollectionItem>(item_data, root_);
88 root_->appendSubItem(group_item);
91 // Create decoder item
93 item_data.emplace_back(name);
94 item_data.emplace_back(long_name);
95 item_data.emplace_back(id);
96 shared_ptr<DecoderCollectionItem> decoder_item =
97 make_shared<DecoderCollectionItem>(item_data, group_item);
99 // Add decoder to tag group
100 group_item->appendSubItem(decoder_item);
105 QVariant DecoderCollectionModel::data(const QModelIndex& index, int role) const
107 if (!index.isValid())
110 if (role == Qt::DisplayRole)
112 DecoderCollectionItem* item =
113 static_cast<DecoderCollectionItem*>(index.internalPointer());
115 return item->data(index.column());
118 if ((role == Qt::FontRole) && (index.parent().isValid()) && (index.column() == 0))
121 font.setItalic(true);
122 return QVariant(font);
128 Qt::ItemFlags DecoderCollectionModel::flags(const QModelIndex& index) const
130 if (!index.isValid())
133 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
136 QVariant DecoderCollectionModel::headerData(int section, Qt::Orientation orientation,
139 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
140 return root_->data(section);
145 QModelIndex DecoderCollectionModel::index(int row, int column,
146 const QModelIndex& parent_idx) const
148 if (!hasIndex(row, column, parent_idx))
149 return QModelIndex();
151 DecoderCollectionItem* parent = root_.get();
153 if (parent_idx.isValid())
154 parent = static_cast<DecoderCollectionItem*>(parent_idx.internalPointer());
156 DecoderCollectionItem* subItem = parent->subItem(row).get();
158 return subItem ? createIndex(row, column, subItem) : QModelIndex();
161 QModelIndex DecoderCollectionModel::parent(const QModelIndex& index) const
163 if (!index.isValid())
164 return QModelIndex();
166 DecoderCollectionItem* subItem =
167 static_cast<DecoderCollectionItem*>(index.internalPointer());
169 shared_ptr<DecoderCollectionItem> parent = subItem->parent();
171 return (parent == root_) ? QModelIndex() :
172 createIndex(parent->row(), 0, parent.get());
175 int DecoderCollectionModel::rowCount(const QModelIndex& parent_idx) const
177 DecoderCollectionItem* parent = root_.get();
179 if (parent_idx.column() > 0)
182 if (parent_idx.isValid())
183 parent = static_cast<DecoderCollectionItem*>(parent_idx.internalPointer());
185 return parent->subItemCount();
188 int DecoderCollectionModel::columnCount(const QModelIndex& parent_idx) const
190 if (parent_idx.isValid())
191 return static_cast<DecoderCollectionItem*>(
192 parent_idx.internalPointer())->columnCount();
194 return root_->columnCount();
198 } // namespace decoder_selector
199 } // namespace subwindows