]> sigrok.org Git - pulseview.git/blob - pv/views/tabular_decoder/model.cpp
Add tabular decoder view
[pulseview.git] / pv / views / tabular_decoder / model.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 <QString>
21
22 #include "pv/views/tabular_decoder/view.hpp"
23
24 using std::make_shared;
25
26 namespace pv {
27 namespace views {
28 namespace tabular_decoder {
29
30 AnnotationCollectionModel::AnnotationCollectionModel(QObject* parent) :
31         QAbstractItemModel(parent)
32 {
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);
41 }
42
43 QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) const
44 {
45         if (!index.isValid())
46                 return QVariant();
47
48         if (role == Qt::DisplayRole)
49         {
50                 AnnotationCollectionItem* item =
51                         static_cast<AnnotationCollectionItem*>(index.internalPointer());
52
53                 return item->data(index.column());
54         }
55
56         if ((role == Qt::FontRole) && (index.parent().isValid()) && (index.column() == 0))
57         {
58                 QFont font;
59                 font.setItalic(true);
60                 return QVariant(font);
61         }
62
63         return QVariant();
64 }
65
66 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
67 {
68         if (!index.isValid())
69                 return nullptr;
70
71         return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
72 }
73
74 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
75         int role) const
76 {
77         if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
78                 return root_->data(section);
79
80         return QVariant();
81 }
82
83 QModelIndex AnnotationCollectionModel::index(int row, int column,
84         const QModelIndex& parent_idx) const
85 {
86         if (!hasIndex(row, column, parent_idx))
87                 return QModelIndex();
88
89         AnnotationCollectionItem* parent = root_.get();
90
91         if (parent_idx.isValid())
92                 parent = static_cast<AnnotationCollectionItem*>(parent_idx.internalPointer());
93
94         AnnotationCollectionItem* subItem = parent->subItem(row).get();
95
96         return subItem ? createIndex(row, column, subItem) : QModelIndex();
97 }
98
99 QModelIndex AnnotationCollectionModel::parent(const QModelIndex& index) const
100 {
101         if (!index.isValid())
102                 return QModelIndex();
103
104         AnnotationCollectionItem* subItem =
105                 static_cast<AnnotationCollectionItem*>(index.internalPointer());
106
107         shared_ptr<AnnotationCollectionItem> parent = subItem->parent();
108
109         return (parent == root_) ? QModelIndex() :
110                 createIndex(parent->row(), 0, parent.get());
111 }
112
113 int AnnotationCollectionModel::rowCount(const QModelIndex& parent_idx) const
114 {
115         AnnotationCollectionItem* parent = root_.get();
116
117         if (parent_idx.column() > 0)
118                 return 0;
119
120         if (parent_idx.isValid())
121                 parent = static_cast<AnnotationCollectionItem*>(parent_idx.internalPointer());
122
123         return parent->subItemCount();
124 }
125
126 int AnnotationCollectionModel::columnCount(const QModelIndex& parent_idx) const
127 {
128         if (parent_idx.isValid())
129                 return static_cast<AnnotationCollectionItem*>(
130                         parent_idx.internalPointer())->columnCount();
131         else
132                 return root_->columnCount();
133 }
134
135
136 } // namespace tabular_decoder
137 } // namespace views
138 } // namespace pv