-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatacanvas.cpp
353 lines (323 loc) · 11.5 KB
/
datacanvas.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "datacanvas.h"
#include "setcolorpanel.h"
#include "./ui_datacanvas.h"
// DataCanvas Initialize function
DataCanvas::DataCanvas(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::DataCanvas)
{
ui->setupUi(this);
// map to ui
// qcustomplot
dataCanvas = ui->Canvas;
// menu
fileMenu = ui->menuFile;
editMenu = ui->menuEdit;
// action
openFile = ui->actionOpen;
saveImage = ui->actionSaveImage;
// button
clearBtn = ui->clearBtn;
setColorBtn = ui->setColorBtn;
mathOpeBtn = ui->mathOpeBtn;
setVisualityBtn = ui->setVisualityBtn;
// Form
nameForm = ui->nameForm;
// connect
connect(openFile, SIGNAL(triggered()), this, SLOT(selectAndRead()));
connect(saveImage, SIGNAL(triggered()), this, SLOT(selectAndSaveImg()));
connect(clearBtn, SIGNAL(clicked()), this, SLOT(clearPlots()));
connect(setColorBtn, SIGNAL(clicked()), this, SLOT(setColor()));
connect(mathOpeBtn, SIGNAL(clicked()), this, SLOT(mathOpe()));
connect(setVisualityBtn, SIGNAL(clicked()), this, SLOT(setGraphVisuality()));
connect(nameForm, SIGNAL(currentIndexChanged(int)), this, SLOT(changeSetVisualityBtn(int)));
// init content config
dataCanvas->xAxis2->setVisible(true);
dataCanvas->xAxis2->setTickLabels(false);
dataCanvas->yAxis2->setVisible(true);
dataCanvas->yAxis2->setTickLabels(false);
dataCanvas->legend->setVisible(true);
// Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
dataCanvas->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
dataCanvas->axisRect()->setRangeDrag(Qt::Vertical);
dataCanvas->axisRect()->setRangeZoom(Qt::Vertical);
}
void DataCanvas::selectAndRead()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("DataFile (*.txt *.csv *.json)"));
readDataToQCP(fileName);
}
void DataCanvas::selectAndSaveImg()
{
QString path = QFileDialog::getSaveFileName(0,
tr("Save File"),
".");
dataCanvas->savePng(path,dataCanvas->width(),dataCanvas->height(),1,-1);
}
void DataCanvas::clearPlots()
{
QMessageBox msgBox(this);
msgBox.setWindowTitle("Warning");
msgBox.setText("Do you want to delete current plots?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Yes);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Yes:
dataCanvas->clearGraphs();
dataCanvas->replot();
// Save was clicked
break;
case QMessageBox::Cancel:
// Cancel was clicked
break;
default:
// should never be reached
break;
}
}
void DataCanvas::setColor()
{
QStringList nameList;
unsigned int size = dataCanvas->graphCount();
if(size==0){
QMessageBox::information(0,"graphs' number is 0","Currently there is no graph");
return; // null
}
for(int i=0;i<size;i++){
nameList.append(dataCanvas->graph(i)->name());
}
setColorPanel = new SetColorPanel(this,nameList);
connect(setColorPanel, SIGNAL(sendSetColorInfo(int,QColor)), this, SLOT(receiveSetColorInfo(int,QColor)));
setColorPanel->setFixedSize(620,510);
setColorPanel->exec();
delete setColorPanel;
}
//void DataCanvas::updateXAxisInterval()
//{
// xInterval = xIntervalEdit->text().toDouble();
// unsigned int size = dataCanvas->graphCount();
// for(int i=0;i<size;i++){
// double key;
// double value;
// auto graphData = dataCanvas->graph(i)->data();
// unsigned int dataSize = graphData->size();
// for(int j=0;j<dataSize;j++){
// key = graphData->at(j)->key;
// value = graphData->at(j)->value;
// graphData->remove(key);
// graphData->add(QCPGraphData(j*xInterval,value));
// }
// }
// dataCanvas->graph(maxCnt)->rescaleAxes();
// dataCanvas->replot();
//}
void DataCanvas::mathOpe()
{
unsigned int size = dataCanvas->graphCount();
if(size==0){
QMessageBox::information(0,"graphs' number is 0","Currently there is no graph");
return; // null
}
QStringList nameList;
for(int i=0;i<size;i++){
nameList.append(dataCanvas->graph(i)->name());
}
mathOpePanel = new MathOpePanel(this,nameList);
connect(mathOpePanel, SIGNAL(sendMathOpeInfo(int,int,QString,QString)), this, SLOT(receiveMathOpeInfo(int,int,QString,QString)));
mathOpePanel->setFixedSize(540,120);
mathOpePanel->exec();
delete mathOpePanel;
}
void DataCanvas::changeSetVisualityBtn(int idx)
{
if(idx<0) return;
if(dataCanvas->graph(idx)->visible()) setVisualityBtn->setText("Conceal");
else setVisualityBtn->setText("Reveal");
setVisualityBtn->repaint();
}
void DataCanvas::setGraphVisuality()
{
int idx = nameForm->currentIndex();
if(idx<0) return;
if(dataCanvas->graph(idx)->visible()) {
dataCanvas->graph(idx)->setVisible(false);
setVisualityBtn->setText("Reveal");
}
else {
dataCanvas->graph(idx)->setVisible(true);
setVisualityBtn->setText("Conceal");
}
dataCanvas->replot();
setVisualityBtn->repaint();
}
void DataCanvas::receiveSetColorInfo(int idx, QColor color)
{
dataCanvas->graph(idx)->setPen(QPen(color));
dataCanvas->replot();
}
void DataCanvas::receiveMathOpeInfo(int idxFst, int idxSnd, QString newPlotName, QString func)
{
std::function<double(double,double)>opeFunc;
if(func == "add") opeFunc = DataCanvas::add;
else if(func == "multiply") opeFunc = DataCanvas::multiply;
else if(func == "log") opeFunc = DataCanvas::log;
else {
QMessageBox::information(0,"input function error","not support "+func+" function");
return; // null
}
unsigned int fstSize = dataCanvas->graph(idxFst)->dataCount();
unsigned int sndSize = dataCanvas->graph(idxSnd)->dataCount();
unsigned int size = fstSize<=sndSize?fstSize:sndSize;
bool isFst = fstSize<=sndSize?true:false;
dataCanvas->addGraph();
nameForm->addItem(newPlotName);
auto graphDataFst = dataCanvas->graph(idxFst)->data();
auto graphDataSnd = dataCanvas->graph(idxSnd)->data();
auto graphData = dataCanvas->graph(dataCanvas->graphCount()-1)->data();
double key;
for(int i=0;i<size;i++){
if(isFst) key = graphDataFst->at(i)->key;
else key = graphDataSnd->at(i)->key;
graphData->add(QCPGraphData(key,opeFunc(graphDataFst->at(i)->value,graphDataSnd->at(i)->value)));
}
dataCanvas->graph(dataCanvas->graphCount()-1)->setName(newPlotName);
dataCanvas->graph(dataCanvas->graphCount()-1)->setPen(QPen(randomColor(dataCanvas->graphCount()-1)));
dataCanvas->replot();
}
void DataCanvas::readDataToQCP(QString fileName)
{
if(fileName == "") return;
dataCanvas->clearGraphs();
nameForm->clear();
// block global variables
QFile file(fileName);
QByteArray line;
QString plotName;
QVector<QVector<double>>plotsData;
QVector<double>tmpData;
unsigned int size;
unsigned int plotCnt = 0;
// file name split
QStringList pieces = fileName.split(".");
if(pieces.length()!=2){ // error data file name format
QMessageBox::information(0,"Read dataFile Error","File name format isn't \"fileName.suffixName\".");
return; // null
}
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0,"Read dataFile Error",file.errorString());
return; // null
}
auto suffixName = pieces[1]; // format:1.txt, 2.csv, 3.json, etc
if(suffixName=="txt"){
// if variables
QRegExp match("<PlotName:(.+),Separator:(space|comma|tab)>"); // regex match Name(can't be empty) & Separator
QString separatorName;
char splitChar;
bool isMatchedPlotName = false;
while (!file.atEnd()) {
line = file.readLine();
if(line[0]=='<' && !isMatchedPlotName){
line = line.simplified();
line.replace( " ", "" ); // delete whitespace
int pos = match.indexIn(line);
if (pos > -1) {
isMatchedPlotName = true;
plotName = match.cap(1); // plotName
separatorName = match.cap(2); // Separator
if(separatorName == "space") splitChar = ' ';
else if(separatorName == "comma") splitChar = ',';
else if(separatorName == "tab") splitChar = '\t';
}
else{ // miss match
QMessageBox::information(0,"dataFile Format Error","Can't match PlotName or Separator");
return; // null
}
}
else if(line!="\r\n" && isMatchedPlotName){
auto pieces = line.split(splitChar);
size = pieces.size();
for(int i=0;i<size;i++){
try{
tmpData.push_back((pieces.at(i)).toDouble());
}catch(std::exception e){
QMessageBox::information(0,"dataFile Format Error","Can't parse str to double due to the separator.");
return;
}
}
}
else if(line=="\r\n" && isMatchedPlotName){ // match over data
isMatchedPlotName = false;
dataCanvas->addGraph();
dataCanvas->graph(plotCnt)->setName(plotName);
nameForm->addItem(plotName);
plotsData.push_back(tmpData);
size = tmpData.size();
if(xSize < size) {
xSize = size;
maxCnt = plotCnt;
}
plotCnt++;
tmpData.clear();
}
}
}
else if(suffixName=="csv"){
}
else if(suffixName=="json"){
}
else{
QMessageBox::information(0,"dataFile suffix name error","fileName should be csv, json or txt.");
return;
}
// xAxisVal initialize
size = plotsData.size();
auto colors = randomColors(size);
for(int i=0;i<size;i++){
dataCanvas->graph(i)->setPen(QPen(colors[i]));
// dataCanvas->graph(i)->setData(xAxisVal,plotsData[i]);
auto graphData = dataCanvas->graph(i)->data();
unsigned int dataSize = plotsData[i].size();
for(int j=0;j<dataSize;j++){
graphData->add(QCPGraphData(j*xInterval,plotsData[i][j]));
}
}
dataCanvas->graph(maxCnt)->rescaleAxes();
dataCanvas->replot();
}
QVector<QColor> DataCanvas::randomColors(int count){
QVector<QColor> colors;
float currentHue = 0.0;
for (int i = 0; i < count; i++){
colors.push_back( QColor::fromHslF(currentHue, 1.0, 0.5) );
currentHue += 0.618033988749895f;
currentHue = std::fmod(currentHue, 1.0f);
}
return colors;
}
QColor DataCanvas::randomColor(int idx){
float currentHue = 0.0;
for (int i = 0; i < idx; i++){
currentHue += 0.618033988749895f;
currentHue = std::fmod(currentHue, 1.0f);
}
return QColor::fromHslF(currentHue, 1.0, 0.5);
}
double DataCanvas::add(double x,double y)
{
return x+y;
}
double DataCanvas::multiply(double x,double y)
{
return x*y;
}
double DataCanvas::log(double x,double y)
{
return ::log(x+y);
}
DataCanvas::~DataCanvas()
{
delete ui;
}