forked from jfnavarro/st_viewer
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSpotItemModel.cpp
More file actions
203 lines (173 loc) · 4.95 KB
/
SpotItemModel.cpp
File metadata and controls
203 lines (173 loc) · 4.95 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
#include "SpotItemModel.h"
#include <set>
#include <QDebug>
#include <QModelIndex>
#include <QMimeData>
#include <QStringList>
#include <QItemSelection>
#include "data/Spot.h"
#include "data/Dataset.h"
static const int COLUMN_NUMBER = 4;
SpotItemModel::SpotItemModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
QVariant SpotItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case Name:
return tr("Spot");
case Show:
return tr("Show");
case Count:
return tr("#Count");
case Color:
return tr("Color");
default:
return QVariant(QVariant::Invalid);
}
}
if (orientation == Qt::Horizontal && role == Qt::ToolTipRole) {
switch (section) {
case Name:
return tr("The coordinates of the spot");
case Show:
return tr("Indicates if the spot is visible on the screen");
case Count:
return tr("The total number of transcritps of the spot");
case Color:
return tr("Indicates the spot of the gene on the screen");
default:
return QVariant(QVariant::Invalid);
}
}
if (role == Qt::TextAlignmentRole) {
switch (section) {
case Show:
return Qt::AlignCenter;
case Color:
return Qt::AlignLeft;
case Count:
return Qt::AlignCenter;
case Name:
return Qt::AlignLeft;
default:
return QVariant(QVariant::Invalid);
}
}
// return invalid value
return QVariant(QVariant::Invalid);
}
int SpotItemModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_items_reference.size();
}
int SpotItemModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : COLUMN_NUMBER;
}
QVariant SpotItemModel::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::DisplayRole || role == Qt::UserRole) && index.column() == Count) {
return item->totalCount();
}
if (role == Qt::DecorationRole && index.column() == Color) {
return item->color();
}
if (role == Qt::TextAlignmentRole) {
switch (index.column()) {
case Show:
return Qt::AlignCenter;
case Color:
return Qt::AlignCenter;
case Count:
return Qt::AlignCenter;
case Name:
return Qt::AlignLeft;
default:
return QVariant(QVariant::Invalid);
}
}
return QVariant(QVariant::Invalid);
}
Qt::ItemFlags SpotItemModel::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 Count:
return defaultFlags;
case Color:
return defaultFlags;
}
return defaultFlags;
}
void SpotItemModel::loadDataset(const Dataset &dataset)
{
Q_UNUSED(dataset)
beginResetModel();
m_items_reference = dataset.data()->spots();
endResetModel();
}
void SpotItemModel::clear()
{
beginResetModel();
m_items_reference.clear();
endResetModel();
}
void SpotItemModel::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 spots
for (const auto &row : rows) {
auto spot = m_items_reference.at(row);
if (spot->visible() != visible) {
spot->visible(visible);
}
}
}
void SpotItemModel::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 spots
for (const auto &row : rows) {
auto spot = m_items_reference.at(row);
if (color.isValid() && spot->color() != color) {
spot->color(color);
}
}
}