-
Notifications
You must be signed in to change notification settings - Fork 1
/
stockboxtablemodel.cpp
184 lines (174 loc) · 6.03 KB
/
stockboxtablemodel.cpp
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
#include "stockboxtablemodel.h"
StockBoxTableModel::StockBoxTableModel(QVector<StockBoxData*> stocks, QObject *parent)
: QAbstractTableModel(parent)
{
this->stocks = stocks;
}
QVariant StockBoxTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
switch (section) {
case 0:
return QString("Bidrate");
case 1:
return QString("Bidsize");
case 2:
return QString("Bid");
case 3:
return QString("Ask");
case 4:
return QString("Asksize");
case 5:
return QString("Askrate");
case 6:
return QString("C Ask");
case 7:
return QString("C Askrate");
case 8:
return QString("C Asksize");
case 9:
return QString("C Bid");
case 10:
return QString("C Bidrate");
case 11:
return QString("C Bidsize");
}
}
return QVariant();
}
bool StockBoxTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
/*if (value != headerData(section, orientation, role)) {
// FIXME: Implement me!
emit headerDataChanged(orientation, section, section);
return true;
}*/
return false;
}
int StockBoxTableModel::rowCount(const QModelIndex &parent) const
{
return stocks.size();
}
int StockBoxTableModel::columnCount(const QModelIndex &parent) const
{
return 12;
}
QVariant StockBoxTableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole){
StockBoxData* current_stock = stocks[index.row()];
switch(index.column()){
case 0:
return current_stock->data.bidrate;
case 1:
return QLocale(QLocale::English).toString(current_stock->data.bidsize);
case 2:
return QString::number(current_stock->data.bid, 'f', 3);
case 3:
return QString::number(current_stock->data.ask, 'f', 3);
case 4:
return QLocale(QLocale::English).toString(current_stock->data.asksize);
case 5:
return current_stock->data.askrate;
case 6:
return QString::number(current_stock->condition.ask, 'f', 3);
case 7:
return QLocale(QLocale::English).toString(current_stock->condition.askrate);
case 8:
return QLocale(QLocale::English).toString(current_stock->condition.asksize);
case 9:
return QString::number(current_stock->condition.bid, 'f', 3);
case 10:
return QLocale(QLocale::English).toString(current_stock->condition.bidrate);
case 11:
return QLocale(QLocale::English).toString(current_stock->condition.bidsize);
default:
return false;
}
}else if(role == Qt::EditRole){
StockBoxData* current_stock = stocks[index.row()];
switch(index.column()){
case 0:
return current_stock->data.bidrate;
case 1:
return current_stock->data.bidsize;
case 2:
return current_stock->data.bid;
case 3:
return current_stock->data.ask;
case 4:
return current_stock->data.asksize;
case 5:
return current_stock->data.askrate;
case 6:
return current_stock->condition.ask;
case 7:
return current_stock->condition.askrate;
case 8:
return current_stock->condition.asksize;
case 9:
return current_stock->condition.bid;
case 10:
return current_stock->condition.bidrate;
case 11:
return current_stock->condition.bidsize;
default:
return false;
}
}else if(role == Qt::BackgroundColorRole && index.row() == 1){
StockBoxData* current_stock = stocks[index.row()];
switch(index.column()){
case 6:
if(current_stock->data.ask > current_stock->condition.ask) return GREEN_COLOR;
else if(current_stock->data.ask < current_stock->condition.ask) return RED_COLOR;
return WHITE_COLOR;
case 9:
if(current_stock->data.bid > current_stock->condition.bid) return GREEN_COLOR;
else if(current_stock->data.bid < current_stock->condition.bid) return RED_COLOR;
return WHITE_COLOR;
}
}
return QVariant();
}
bool StockBoxTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (data(index, role) != value) {
StockBoxData* current_stock = stocks[index.row()];
switch(index.column()){
case 6:
current_stock->condition.ask = value.toDouble();
break;
case 7:
current_stock->condition.askrate = value.toInt();
break;
case 8:
current_stock->condition.asksize = value.toInt();
break;
case 9:
current_stock->condition.bid = value.toDouble();
break;
case 10:
current_stock->condition.bidrate = value.toInt();
break;
case 11:
current_stock->condition.bidsize = value.toInt();
break;
default:
return false;
}
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags StockBoxTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid() || index.column() <= 5)
return Qt::ItemIsEnabled;
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
}
void StockBoxTableModel::updateData(int top, int left, int bottom, int right){
emit dataChanged(createIndex(0, 0), createIndex(1, 5), QVector<int>() << Qt::DisplayRole);
// emit dataChanged(createIndex(1, 6), createIndex(1, 6), QVector<int>() << Qt::BackgroundColorRole);
// emit dataChanged(createIndex(1, 9), createIndex(1, 9), QVector<int>() << Qt::BackgroundColorRole);
}