-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectrogram.cpp
139 lines (134 loc) · 4.7 KB
/
spectrogram.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
#include "spectrogram.h"
Spectrogram::Spectrogram(QString name,QWidget*parent):QCustomPlot(parent){
setInteractions(QCP::iRangeDrag|QCP::iRangeZoom);
map->setColorScale(scale);
map->setInterpolate(false);
map->setTightBoundary(true);
plotLayout()->addElement(0,1,scale);
plotLayout()->insertRow(0);
data->setRange(QCPRange(0,1),QCPRange(0,1));
QFont titleFont;
titleFont.setWeight(99);
titleFont.setPointSize(titleFont.pointSize()*1.5);
plotLayout()->addElement(0,0,new QCPTextElement(this,name,titleFont));
dialog->setWindowTitle(name);
QLineEdit*upper=new QLineEdit,*lower=new QLineEdit;
QComboBox*gradients=new QComboBox(dialog);
QCPAxis*axis=scale->axis();
connect(axis,(void(QCPAxis::*)(const QCPRange&))&QCPAxis::rangeChanged,[=](const QCPRange&range){
upper->setText(QString::number(range.upper));
lower->setText(QString::number(range.lower));
});
connect(upper,&QLineEdit::editingFinished,[=]{
autoscale->setChecked(false);
axis->setRangeUpper(upper->text().toDouble());
replot();
});
connect(lower,&QLineEdit::editingFinished,[=]{
autoscale->setChecked(false);
axis->setRangeLower(lower->text().toDouble());
replot();
});
QCheckBox*logBox=new QCheckBox("Log",dialog);
connect(logBox,&QCheckBox::stateChanged,[=](int state){
axis->setScaleType(state?QCPAxis::stLogarithmic:QCPAxis::stLinear);
replot();
});
logBox->setToolTip("Enables logarithmic scaling");
autoscale->setToolTip("Attempts to automatically detect reasonable max/min scale values");
autoscale->setChecked(true);
QFormLayout*layout=new QFormLayout(dialog);
layout->addRow("Scale Max:",upper);
layout->addRow("Scale Min:",lower);
QHBoxLayout*bottom=new QHBoxLayout();
layout->addRow(bottom);
bottom->addWidget(autoscale);
bottom->addWidget(logBox);
bottom->addWidget(gradients);
connect(gradients,(void(QComboBox::*)(int))(&QComboBox::currentIndexChanged),this,&Spectrogram::setGradient);
gradients->addItem("Grayscale");
gradients->addItem("Hot");
gradients->addItem("Cold");
gradients->addItem("Night");
gradients->addItem("Candy");
gradients->addItem("Geography");
gradients->addItem("Ion");
gradients->addItem("Thermal");
gradients->addItem("Polar");
gradients->addItem("Spectrum");
gradients->addItem("Jet");
gradients->addItem("Hues");
gradients->setMaxVisibleItems(gradients->count());
gradients->setCurrentIndex(8);
QMenu*menu=new QMenu(this);
menu->addAction("Show Controls",dialog,SLOT(show()));
menu->addAction("Reset View",this,SLOT(resetView()));
menu->addAction("Reset Scale",this,SLOT(resetScale()));
menu->addAction("Copy to Clipboard",this,SLOT(clipboard()));
menu->addAction("Export to TIFF...",this,SLOT(tiffExport()));
menu->addAction("Save Image As...",this,SLOT(saveImage()));
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this,&QWidget::customContextMenuRequested,[=](const QPoint&pos){menu->popup(mapToGlobal(pos));});
QLabel*label=new QLabel("0",this);
label->setMinimumWidth(127);
connect(this,&QCustomPlot::mouseMove,[=](QMouseEvent*event){
label->setText(QString::number(data->data(xAxis->pixelToCoord(event->x()),yAxis->pixelToCoord(event->y()))));
});
}
static int comp(const void*v1,const void*v2){
if(*(float*)v1 < *(float*)v2) return -1;
else if(*(float*)v1 > *(float*)v2) return 1;
else return 0;
}
void Spectrogram::adjustScale(){
const int keySize = data->keySize(), valueSize = data->valueSize();
float*values = (float*)malloc(keySize*valueSize*sizeof*values);
for(int key=0; key<keySize; ++key){
for(int value=0; value<valueSize; ++value){
values[key*valueSize+value] = data->cell(key,value);
}
}
qsort(values, keySize*valueSize, sizeof*values, &comp);
int n = 0;//filter out zeroes
while(n < keySize*valueSize && values[n] <= 0) ++n;
scale->axis()->setRange((values+n)[(keySize*valueSize-n)>>3],values[keySize*valueSize-1]);
free(values);
replot();
}
void Spectrogram::clear(){
data->clear();
replot();
}
bool Spectrogram::saveImage(){
QString formats="";
for(QByteArray format:QImageWriter::supportedImageFormats()){
formats += format.toUpper() + " (*."+format+");;";
}
formats.chop(2);
QString format="PNG (*.png)";
const QString name=QFileDialog::getSaveFileName(this,"Save Image","",formats,&format);
if(name.isNull())return false;
saveRastered(name,0,0,1,QFileInfo(name).suffix().toUtf8().data());
return true;
}
void Spectrogram::clipboard(){
QApplication::clipboard()->setPixmap(toPixmap());
}
void Spectrogram::resetScale(){
if(autoscale->isChecked())adjustScale();
else{
map->rescaleDataRange(true);
replot();
}
}
void Spectrogram::resetView(){
rescaleAxes();
replot();
}
void Spectrogram::setSize(int x,int y){
data->setSize(x,y);
}
void Spectrogram::setGradient(int gradient){
map->setGradient((QCPColorGradient::GradientPreset)gradient);
replot();
}