- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectrogram.cpp
More file actions
Latest commit
139 lines (134 loc) · 4.7 KB
/
Copy pathspectrogram.cpp
File metadata and controls
139 lines (134 loc) · 4.7 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
#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,newQCPTextElement(this,name,titleFont));
dialog->setWindowTitle(name);
QLineEdit*upper=new QLineEdit,*lower=new QLineEdit;
QComboBox*gradients=newQComboBox(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=newQCheckBox("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=newQFormLayout(dialog);
layout->addRow("Scale Max:",upper);
layout->addRow("Scale Min:",lower);
QHBoxLayout*bottom=newQHBoxLayout();
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=newQMenu(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=newQLabel("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()))));
});
}
staticintcomp(constvoid*v1,constvoid*v2){
if(*(float*)v1 < *(float*)v2) return -1;
elseif(*(float*)v1 > *(float*)v2) return1;
elsereturn0;
}
voidSpectrogram::adjustScale(){
constint 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();
}
voidSpectrogram::clear(){
data->clear();
replot();
}
boolSpectrogram::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())returnfalse;
saveRastered(name,0,0,1,QFileInfo(name).suffix().toUtf8().data());
returntrue;
}
voidSpectrogram::clipboard(){
QApplication::clipboard()->setPixmap(toPixmap());
}
voidSpectrogram::resetScale(){
if(autoscale->isChecked())adjustScale();
else{
map->rescaleDataRange(true);
replot();
}
}
voidSpectrogram::resetView(){
rescaleAxes();
replot();
}
voidSpectrogram::setSize(int x,int y){
data->setSize(x,y);
}
voidSpectrogram::setGradient(int gradient){
map->setGradient((QCPColorGradient::GradientPreset)gradient);
replot();
}