-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSelectDialog.cpp
More file actions
141 lines (118 loc) · 4.5 KB
/
FileSelectDialog.cpp
File metadata and controls
141 lines (118 loc) · 4.5 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
#include "FileSelectDialog.h"
FileSelectDialog::FileSelectDialog(MainWindow *window,QWidget *parent):
QDialog(parent),window(window){
setWindowTitle("Data Visualization System");
resize(400,300);
layout=new QVBoxLayout;
fieldboxlayout=new QVBoxLayout();
dateboxlayout=new QVBoxLayout();
checkboxlayout=new QHBoxLayout();
QFrame *framecheckbox=new QFrame;
framecheckbox->setLayout(checkboxlayout);
this->setLayout(layout);
QHBoxLayout *buttonlayout=new QHBoxLayout;
QFrame *framebutton=new QFrame;
framebutton->setLayout(buttonlayout);
layout->addWidget(framebutton,0);
layout->addWidget(framecheckbox,1);
//layout->addLayout(checkboxlayout,1);
QPushButton *button_file=new QPushButton();
buttonlayout->addWidget(button_file,0);
button_file->setText("Select file");
button_file->setEnabled(true);
connect(button_file, &QPushButton::clicked, this, &FileSelectDialog::selectFile);
connect(button_file, &QPushButton::clicked,[=](){
button_file->setEnabled(false);
});
QPushButton *button_ok=new QPushButton();
button_ok->setText("OK");
connect(button_ok, &QPushButton::clicked, [=](){
if(daterangeIsValid()) {
window->from_date=QDate::fromString(from->currentText(),"yyyy-MM-dd").toString("yyyyMMdd");
window->to_date=QDate::fromString(to->currentText(),"yyyy-MM-dd").toString("yyyyMMdd");
this->accept();
}
else {
QMessageBox msg;
msg.setText("To date must be after From date!");
msg.exec();
}
});
buttonlayout->addWidget(button_ok,0);
checkboxlayout->addLayout(fieldboxlayout,0);
checkboxlayout->addLayout(dateboxlayout,0);
}
void FileSelectDialog::selectFile() {
QString dirname=QFileDialog::getExistingDirectory();
QString absoluteFileName;
if(!folderIsValid(dirname, absoluteFileName)) {qDebug()<<"Dataset directory is invalid!";return;}
window->dirName=dirname;
QDir dir(dirname);
QFile file(absoluteFileName);
file.open(QIODevice::ReadOnly);
QTextStream in(&file);
QStringList dataFields=in.readLine().trimmed().split(',');
auto n_fields=dataFields.size();
fieldboxes=new QCheckBox *[n_fields];
for(int i=0;i<n_fields;i++) {
window->import_fields.push_back(dataFields[i]);
window->fields_onehot.push_back(false);
fieldboxes[i]=new QCheckBox(dataFields[i],this);
fieldboxlayout->addWidget(fieldboxes[i],0);
connect(fieldboxes[i], &QCheckBox::stateChanged,[=](int){
window->fields_onehot[i]=fieldboxes[i]->isChecked();
});
}
//checkboxlayout->addLayout(fieldboxlayout,0);
//checkboxlayout->addLayout(dateboxlayout,0);
QStringList date_list;
QString re=QString("order_")+QString("*_part*.csv");
QStringList filterlist;
filterlist.push_back(re);
dir.setNameFilters(filterlist);
auto entrylist=dir.entryList();
//qDebug()<<entrylist.size();
for(auto filename:entrylist){
QString date=filename.split('_')[1];
if(!date_list.contains(date)) date_list.push_back(date);
}
auto n_date=date_list.size();
from=new QComboBox;
to=new QComboBox;
QLabel *labelfrom=new QLabel("From:");
QLabel *labelto=new QLabel("To:");
dateboxlayout->addWidget(labelfrom,0);
dateboxlayout->addWidget(from,0);
dateboxlayout->addWidget(labelto,0);
dateboxlayout->addWidget(to,0);
for(int i=0;i<n_date;i++){
//window->import_dates.push_back(date_list[i]);
//window->dates_onehot.push_back(false);
from->addItem(QDate::fromString(date_list[i],"yyyyMMdd").toString("yyyy-MM-dd"));
to->addItem(QDate::fromString(date_list[i],"yyyyMMdd").toString("yyyy-MM-dd"));
}
//checkboxlayout->addLayout(fieldboxlayout,0);
//checkboxlayout->addLayout(dateboxlayout,0);
//this->setLayout(checkboxlayout);
}
bool FileSelectDialog::folderIsValid(QString dirname, QString &filename) {
QDir dir(dirname);
dir.setFilter(QDir::Files);
dir.setSorting(QDir::Name);
QStringList filelist=dir.entryList();
//qDebug()<<filelist[0];
if(filelist[0].contains("order")) {
//qDebug() << "Success!";
filename = dir.absoluteFilePath(filelist[0]);
//qDebug() << filename;
return true;
}else {
filename="";
return false;
}
}
bool FileSelectDialog::daterangeIsValid() {
int s=from->currentIndex();
int t=to->currentIndex();
return s<=t;
}