forked from bupticybee/TexasSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rangeselector.cpp
222 lines (198 loc) · 7.56 KB
/
rangeselector.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
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
#include "rangeselector.h"
#include "ui_rangeselector.h"
RangeSelector::RangeSelector(QTextEdit* rangeEdit,QWidget *parent,PokerMode mode) :
QDialog(parent),
ui(new Ui::RangeSelector)
{
QString ranks;
if(mode == PokerMode::HOLDEM){
ranks = "A,K,Q,J,T,9,8,7,6,5,4,3,2";
}else if(mode == PokerMode::SHORTDECK){
ranks = "A,K,Q,J,T,9,8,7,6";
}else{
throw runtime_error("mode not found in range selector");
}
this->rank_list = ranks.split(",");
this->rangeSelectorTableModel = new RangeSelectorTableModel(this->rank_list,rangeEdit->toPlainText(),this);
this->rangeSelectorTableDelegate = new RangeSelectorTableDelegate(this->rank_list,this->rangeSelectorTableModel,this);
ui->setupUi(this);
this->ui->rangeTableView->setModel(this->rangeSelectorTableModel);
this->ui->rangeTableView->setItemDelegate(this->rangeSelectorTableDelegate);
this->rangeEdit = rangeEdit;
this->ui->textEdit->setText(this->rangeEdit->toPlainText());
this->ui->rangeNumberSlider->setMaximum(this->max_val);
this->ui->rangeNumberSlider->setValue(this->max_val);
this->setWindowTitle(tr("RangeSelector"));
connect(this->ui->rangeTableView,SIGNAL(view_item_pressed(int,int)),this,SLOT(grid_pressed(int,int)));
connect(this->ui->rangeTableView,SIGNAL(view_item_area(int,int,int,int)),this,SLOT(grid_area(int,int,int,int)));
connect(this->ui->rangeTableView,SIGNAL(item_release(int,int)),this,SLOT(grid_release(int,int)));
QStringList filters;
filters << "*.txt";
qFileSystemModel = new QFileSystemModel(this);
QDir filedir = QDir::current().filePath("ranges");
qFileSystemModel->setRootPath(filedir.path());
#ifdef Q_OS_MAC
filedir = QDir("");
qFileSystemModel->setRootPath(filedir.path());
#endif
qFileSystemModel->setNameFilters(filters);
qFileSystemModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
this->ui->rangeFilesTreeView->setModel(this->qFileSystemModel);
this->ui->rangeFilesTreeView->setRootIndex(this->qFileSystemModel->index(filedir.path()));
for (int i=1; i<=4; i++)
{ this->ui->rangeFilesTreeView->hideColumn(i);}
connect(
this->ui->rangeFilesTreeView,
SIGNAL(clicked(const QModelIndex&)),
this,
SLOT(item_clicked(const QModelIndex&))
);
connect(ui->rangeFilesTreeView, SIGNAL(expanded(QModelIndex)), this, SLOT(onExpanded(QModelIndex)));
/*
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update_second()));
timer->start(1000);
*/
}
void RangeSelector::grid_area(int i1,int j1,int i2,int j2){
if(i1 <= i2 && j1 <= j2){
for(int i = i1;i <= i2;i ++){
for(int j = j1;j <= j2;j ++){
this->rangeSelectorTableModel->setRangeAt(i,j,this->range_num);
}
}
}
this->ui->textEdit->setText(this->rangeSelectorTableModel->getRangeText());
//this->ui->rangeTableView->update();
// DIRTY TRICK TO REPLACE update(), update() doesn't work here for some reason I dont know.
this->ui->textEdit->setFocus();
this->ui->rangeTableView->setFocus();
}
void RangeSelector::grid_release(int i,int j){
//this->ui->rangeTableView->update();
this->ui->textEdit->setFocus();
this->ui->rangeTableView->setFocus();
}
void RangeSelector::grid_pressed(int i,int j){
float range_float = this->rangeSelectorTableModel->getRangeAt(i,j);
if(this->range_num == range_float){
this->rangeSelectorTableModel->setRangeAt(i,j,0);
}else{
this->rangeSelectorTableModel->setRangeAt(i,j,this->range_num);
}
if(this->rangeSelectorTableModel->getRangeText() != this->ui->textEdit->toPlainText()){
this->ui->textEdit->setText(this->rangeSelectorTableModel->getRangeText());
}
//this->ui->rangeTableView->update();
}
void RangeSelector::update_second(){
this->ui->rangeTableView->update();
}
RangeSelector::~RangeSelector()
{
delete ui;
delete this->rangeSelectorTableModel;
}
void RangeSelector::on_confirmButtom_clicked()
{
this->rangeEdit->setText(this->ui->textEdit->toPlainText());
close();
}
void RangeSelector::on_rangeNumberSlider_valueChanged(int value)
{
this->range_num = float(this->ui->rangeNumberSlider->value()) / this->max_val;
if(this->range_num != this->ui->rangeNumberEdit->text().toFloat()){
this->ui->rangeNumberEdit->setText(QString("%2").arg(QString::number(this->range_num,'f',3)));
}
}
void RangeSelector::on_rangeNumberEdit_textEdited(const QString &arg1)
{
this->range_num = this->ui->rangeNumberEdit->text().toFloat();
this->ui->rangeNumberSlider->setValue((int)(this->max_val * this->range_num));
}
void RangeSelector::on_clearRangeButtom_clicked()
{
this->ui->textEdit->setText("");
this->rangeSelectorTableModel->clear_range();
// DIRTY TRICK TO REPLACE update(), update() doesn't work here for some reason I dont know.
this->ui->textEdit->setFocus();
this->ui->rangeTableView->setFocus();
this->ui->textEdit->setFocus();
}
void RangeSelector::on_textEdit_textChanged()
{
QString range_text = this->ui->textEdit->toPlainText();
this->rangeSelectorTableModel->setRangeText(range_text);
// DIRTY TRICK TO REPLACE update(), update() doesn't work here for some reason I dont know.
this->ui->rangeTableView->setFocus();
this->ui->textEdit->setFocus();
}
void RangeSelector::on_exportRangeButton_clicked()
{
QString range_text = this->ui->textEdit->toPlainText();
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Range"),
QDir::current().filePath("ranges/output_range.txt"),
tr("Text file (*.txt)"));
if(fileName.isNull())return;
setlocale(LC_ALL,"");
ofstream fileWriter;
fileWriter.open(fileName.toLocal8Bit());
QMessageBox msgBox;
QString message;
if(!fileWriter.fail()){
fileWriter << range_text.toStdString();
fileWriter.flush();
fileWriter.close();
message = QObject::tr("save success");
}else{
message = QObject::tr("save failed, file cannot be open");
}
qDebug().noquote() << message;
msgBox.setText(message);
setlocale(LC_CTYPE, "C");
msgBox.exec();
}
void RangeSelector::on_cancelButton_clicked()
{
close();
}
void RangeSelector::import_range(QString fileName){
if( fileName.isNull() )
{
qDebug().noquote() << tr("File selection invalid.");
return;
}
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly)){
qDebug().noquote() << tr("File open failed.");
return;
}
QString content;
QTextStream s1(&file);
content.append(s1.readAll());
this->ui->textEdit->setText(content);
}
void RangeSelector::on_importRangeButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(
this,
tr("Open range file"),
QDir::currentPath(),
tr("Text files (*.txt)"));
this->import_range(fileName);
}
void RangeSelector::item_clicked(const QModelIndex& index){
if(!this->qFileSystemModel->isDir(index)){
QFileInfo fileinfo = QFileInfo(this->qFileSystemModel->filePath(index));
if(fileinfo.suffix() == "txt"){
this->import_range(this->qFileSystemModel->filePath(index));
}
}
}
void RangeSelector::on_openRangeFolderButton_clicked()
{
QDesktopServices::openUrl( QUrl::fromLocalFile(QDir::current().filePath("ranges")) );
}
void RangeSelector::onExpanded(const QModelIndex& index){
ui->rangeFilesTreeView->resizeColumnToContents(0);
}