2 * This file is part of the PulseView project.
4 * Copyright (C) 2020 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 "pv/views/tabular_decoder/view.hpp"
24 using std::make_shared;
28 namespace tabular_decoder {
30 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
31 QAbstractItemModel(parent)
33 vector<QVariant> header_data;
34 header_data.emplace_back(tr("ID")); // Column #0
35 header_data.emplace_back(tr("Start Time")); // Column #1
36 header_data.emplace_back(tr("End Time")); // Column #2
37 header_data.emplace_back(tr("Ann Row Name")); // Column #3
38 header_data.emplace_back(tr("Class Row Name")); // Column #4
39 header_data.emplace_back(tr("Value")); // Column #5
40 root_ = make_shared<AnnotationCollectionItem>(header_data);
43 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
48 if (role == Qt::DisplayRole)
50 AnnotationCollectionItem* item =
51 static_cast<AnnotationCollectionItem*>(index.internalPointer());
53 return item->data(index.column());
56 if ((role == Qt::FontRole) && (index.parent().isValid()) && (index.column() == 0))
60 return QVariant(font);
66 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
71 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
74 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
77 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
78 return root_->data(section);
83 QModelIndex AnnotationCollectionModel::index(int row, int column,
84 const QModelIndex& parent_idx) const
86 if (!hasIndex(row, column, parent_idx))
89 AnnotationCollectionItem* parent = root_.get();
91 if (parent_idx.isValid())
92 parent = static_cast<AnnotationCollectionItem*>(parent_idx.internalPointer());
94 AnnotationCollectionItem* subItem = parent->subItem(row).get();
96 return subItem ? createIndex(row, column, subItem) : QModelIndex();
99 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
101 if (!index.isValid())
102 return QModelIndex();
104 AnnotationCollectionItem* subItem =
105 static_cast<AnnotationCollectionItem*>(index.internalPointer());
107 shared_ptr<AnnotationCollectionItem> parent = subItem->parent();
109 return (parent == root_) ? QModelIndex() :
110 createIndex(parent->row(), 0, parent.get());
113 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
115 AnnotationCollectionItem* parent = root_.get();
117 if (parent_idx.column() > 0)
120 if (parent_idx.isValid())
121 parent = static_cast<AnnotationCollectionItem*>(parent_idx.internalPointer());
123 return parent->subItemCount();
126 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
128 if (parent_idx.isValid())
129 return static_cast<AnnotationCollectionItem*>(
130 parent_idx.internalPointer())->columnCount();
132 return root_->columnCount();
136 } // namespace tabular_decoder