-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrangeselectortablemodel.cpp
168 lines (148 loc) · 5.62 KB
/
rangeselectortablemodel.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
#include "include/ui/rangeselectortablemodel.h"
RangeSelectorTableModel::RangeSelectorTableModel(QStringList ranks,QString initial_board,QObject *parent,bool thumbnail):QAbstractItemModel(parent){
this->ranklist = ranks;
this->thumbnail = thumbnail;
this->grids_string = vector<vector<QString>>(this->ranklist.size());
for(int i = 0;i < this->ranklist.size();i ++){
this->grids_string[i] = vector<QString>(this->ranklist.size());
for(int j = 0;j < this->ranklist.size();j ++){
this->grids_string[i][j] = this->get_ij_text(i,j);
this->string2ij[this->get_ij_text(i,j)] = pair<int,int>(i,j);
}
}
this->grids_float = vector<vector<float>>(this->ranklist.size());
for(int i = 0;i < this->ranklist.size();i ++){
this->grids_float[i] = vector<float>(this->ranklist.size(),0.0);
}
this->setRangeText(initial_board);
}
float RangeSelectorTableModel::getRangeAt(int i, int j){
if(i < 0 || i >= this->ranklist.size()){
qDebug().noquote() << "RangeSelectorTableModel::getRangeAt i not valid: " << i;
return 0;
}
else if(j < 0 || j >= this->ranklist.size()){
qDebug().noquote() << "RangeSelectorTableModel::getRangeAt j not valid: " << j;
return 0;
}
else{
return this->grids_float[i][j];
}
}
void RangeSelectorTableModel::clear_range(){
for(int i = 0;i < this->ranklist.size();i ++){
for(int j = 0;j < this->ranklist.size();j ++){
this->grids_float[i][j] = 0;
}
}
}
void RangeSelectorTableModel::setRangeAt(int i, int j,float value){
if(i < 0 || i >= this->ranklist.size()){
qDebug().noquote() << "RangeSelectorTableModel::setRangeAt i not valid: " << i;
}
else if(j < 0 || j >= this->ranklist.size()){
qDebug().noquote() << "RangeSelectorTableModel::setRangeAt j not valid: " << j;
}
else{
this->grids_float[i][j] = value;
}
}
RangeSelectorTableModel::~RangeSelectorTableModel(){
}
QString RangeSelectorTableModel::getRangeText(){
QString retval = "";
for(int i = 0;i < this->ranklist.size();i ++){
for(int j = 0;j < this->ranklist.size();j ++){
if(this->grids_float[i][j] == 0)continue;
QString one_range_str = QString("%1:%2").arg(this->grids_string[i][j],QString::number(this->grids_float[i][j],'f',3));
if(retval == ""){
retval = one_range_str;
}else{
retval = retval + "," + one_range_str;
}
}
}
return retval;
}
void RangeSelectorTableModel::setRangeText(QString input_range){
this->clear_range();
for(QString one_range:input_range.split(",")){
if(one_range.trimmed() == "")continue;
QStringList one_range_list = one_range.split(":");
if(one_range_list.size() > 2 || one_range_list.size() < 1){
qDebug().noquote() << QString(tr("skipping range %1, format error, range list should contain two part seperate by ':'")).arg(one_range);
}
float range_float = 1.0;
QString key_range = one_range_list[0];
QStringList keys;
if(this->string2ij.count(one_range_list[0])){
keys.append(key_range);
}else{
keys.append(key_range + "o");
keys.append(key_range + "s");
}
for(QString one_key: keys){
if(one_key.count(" ") == one_key.length() || one_key == "")continue;
if(!this->string2ij.count(one_key)){
qDebug().noquote() << QString(tr("skipping range %1, format error")).arg(one_range);
continue;
}
pair<int,int> cord = this->string2ij[one_key];
if(one_range_list.size() > 1)range_float = one_range_list[1].toFloat();
this->grids_float[cord.first][cord.second] = range_float;
}
}
}
QString RangeSelectorTableModel::get_ij_text(int i,int j){
int row = i;
int col = j;
int larger = row > col?row:col;
int smaller = row > col?col:row;
QString retval = QString("%1%2%3")\
.arg(QString(this->ranklist[smaller]))\
.arg(QString(this->ranklist[larger]));
if(row > col){
retval = retval.arg("o");
}else if(row < col){
retval = retval.arg("s");
}else{
retval = retval.arg("");
}
return retval;
}
QVariant RangeSelectorTableModel::data(const QModelIndex &index, int role) const {
int row = index.row();
int col = index.column();
int larger = row > col?row:col;
int smaller = row > col?col:row;
QString retval = QString("<h4>%1%2<b>%3</b></h4>")\
.arg(QString(this->ranklist[smaller]))\
.arg(QString(this->ranklist[larger]));
if(row > col){
retval = retval.arg(tr("o"));
}else if(row < col){
retval = retval.arg(tr("s"));
}else{
retval = retval.arg(tr(" "));
}
retval += QString("%1").arg(QString::number(this->grids_float[row][col],'f',3));
return retval;
}
QModelIndex RangeSelectorTableModel::index(int row, int column,
const QModelIndex &parent) const {
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column, nullptr);
}
QVariant RangeSelectorTableModel::headerData(int section, Qt::Orientation orientation, int role){
return QString::fromStdString("");
}
QModelIndex RangeSelectorTableModel::parent(const QModelIndex &child) const {
return QModelIndex();
}
int RangeSelectorTableModel::rowCount(const QModelIndex &parent) const {
return this->ranklist.size();
}
int RangeSelectorTableModel::columnCount(const QModelIndex &parent) const {
return this->ranklist.size();
}