forked from jfnavarro/st_viewer
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGeneItemModel.cpp
More file actions
238 lines (204 loc) · 5.98 KB
/
GeneItemModel.cpp
File metadata and controls
238 lines (204 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "GeneItemModel.h"
#include <set>
#include <QDebug>
#include <QModelIndex>
#include <QMimeData>
#include <QStringList>
#include <QItemSelection>
#include "data/Gene.h"
#include "data/Dataset.h"
static const int COLUMN_NUMBER = 5;
GeneItemModel::GeneItemModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
GeneItemModel::~GeneItemModel()
{
}
QVariant GeneItemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || m_items_reference.empty()) {
return QVariant(QVariant::Invalid);
}
const auto item = m_items_reference.at(index.row());
if ((role == Qt::DisplayRole || role == Qt::UserRole) && index.column() == Name) {
return item->name();
}
if (role == Qt::ForegroundRole && index.column() == Name) {
return QColor(0, 155, 60);
}
if ((role == Qt::CheckStateRole || role == Qt::UserRole) && index.column() == Show) {
return item->visible() ? Qt::Checked : Qt::Unchecked;
}
if (role == Qt::DecorationRole && index.column() == Color) {
return item->color();
}
if ((role == Qt::DisplayRole || role == Qt::UserRole) && index.column() == Count) {
return item->totalCount();
}
if ((role == Qt::DisplayRole || role == Qt::UserRole) && index.column() == CutOff) {
return item->cut_off();
}
if (role == Qt::TextAlignmentRole) {
switch (index.column()) {
case Show:
return Qt::AlignCenter;
case Color:
return Qt::AlignCenter;
case Name:
return Qt::AlignLeft;
case Count:
return Qt::AlignCenter;
case CutOff:
return Qt::AlignCenter;
default:
return QVariant(QVariant::Invalid);
}
}
return QVariant(QVariant::Invalid);
}
bool GeneItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole && index.column() == CutOff) {
auto item = m_items_reference.at(index.row());
const float new_cutoff = value.toFloat();
if (item->cut_off() != new_cutoff && new_cutoff >= 0.0) {
item->cut_off(new_cutoff);
emit dataChanged(index, index);
emit signalGeneCutOffChanged();
return true;
}
return false;
}
return false;
}
QVariant GeneItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case Name:
return tr("Gene");
case Show:
return tr("Show");
case Color:
return tr("Color");
case Count:
return tr("#Count");
case CutOff:
return tr("Cut-off");
default:
return QVariant(QVariant::Invalid);
}
}
if (orientation == Qt::Horizontal && role == Qt::ToolTipRole) {
switch (section) {
case Name:
return tr("The name of the gene");
case Show:
return tr("Indicates if the genes is visible on the screen");
case Color:
return tr("Indicates the color of the gene on the screen");
case Count:
return tr("The total number of transcritps of the gene");
case CutOff:
return tr("Numbers of reads from which this gene will be included");
default:
return QVariant(QVariant::Invalid);
}
}
if (role == Qt::TextAlignmentRole) {
switch (section) {
case Show:
return Qt::AlignCenter;
case Color:
return Qt::AlignLeft;
case Name:
return Qt::AlignLeft;
case Count:
return Qt::AlignCenter;
case CutOff:
return Qt::AlignCenter;
default:
return QVariant(QVariant::Invalid);
}
}
// return invalid value
return QVariant(QVariant::Invalid);
}
int GeneItemModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_items_reference.size();
}
int GeneItemModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : COLUMN_NUMBER;
}
Qt::ItemFlags GeneItemModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QAbstractTableModel::flags(index);
if (!index.isValid()) {
return defaultFlags;
}
switch (index.column()) {
case Name:
return defaultFlags;
case Show:
return Qt::ItemIsUserCheckable | defaultFlags;
case Color:
return defaultFlags;
case Count:
return defaultFlags;
case CutOff:
return Qt::ItemIsEditable | defaultFlags;
}
return defaultFlags;
}
void GeneItemModel::loadDataset(const Dataset &dataset)
{
Q_UNUSED(dataset)
beginResetModel();
m_items_reference = dataset.data()->genes();
endResetModel();
}
void GeneItemModel::clear()
{
beginResetModel();
m_items_reference.clear();
endResetModel();
}
void GeneItemModel::setVisibility(const QItemSelection &selection, bool visible)
{
if (m_items_reference.empty()) {
return;
}
// get unique indexes from the user selection
QSet<int> rows;
for (const auto &index : selection.indexes()) {
rows.insert(index.row());
}
// update the genes
for (const auto &row : rows) {
auto gene = m_items_reference.at(row);
if (gene->visible() != visible) {
gene->visible(visible);
}
}
}
void GeneItemModel::setColor(const QItemSelection &selection, const QColor &color)
{
if (m_items_reference.empty()) {
return;
}
// get unique indexes from the user selection
QSet<int> rows;
for (const auto &index : selection.indexes()) {
rows.insert(index.row());
}
// update the genes
for (const auto &row : rows) {
auto gene = m_items_reference.at(row);
if (color.isValid() && gene->color() != color) {
gene->color(color);
}
}
}