-
Notifications
You must be signed in to change notification settings - Fork 0
/
DensityPlotWidget.cpp
175 lines (149 loc) · 4.2 KB
/
DensityPlotWidget.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
/*
* DensityPlotWidget.cpp
*
* Created on: Apr 25, 2010
* Author: Vincent
*/
#include "DensityPlotWidget.h"
#include <QPainter>
DensityPlotWidget::DensityPlotWidget(QWidget *parent) :
QLabel(parent) {
rangeLow = rangeHigh = 0;
margin = 10;
tickNumber = 3;
// setBackgroundRole(QPalette::Light);
this->setAutoFillBackground(true);
this->setStyleSheet("background-color: rgb(255, 255, 255)");
}
DensityPlotWidget::~DensityPlotWidget() {
}
void DensityPlotWidget::drawValue(double value) {
valueList.clear();
valueList << value;
update();
}
void DensityPlotWidget::drawHighlightedValue(double value) {
hlList.clear();
hlList << value;
update();
}
void DensityPlotWidget::setRange(int low, int high) {
rangeLow = low < high ? low : high;
rangeHigh = low > high ? low : high;
}
void DensityPlotWidget::drawValues(const QList<double> &values) {
valueList = values;
update();
}
void DensityPlotWidget::drawHighlightedValues(const QList<double> &values) {
hlList = values;
update();
}
void DensityPlotWidget::clear() {
valueList.clear();
hlList.clear();
selList.clear();
update();
}
void DensityPlotWidget::paintEvent(QPaintEvent *event) {
QLabel::paintEvent(event);
h = height();
w = width();
// Geometry is too small for drawing
if (h <= 20 || w <= 20) {
return;
}
QPainter painter(this);
QFontMetrics fm(painter.font());
margin = fm.height();
drawFrame(&painter);
drawLabels(&painter);
drawValues(&painter);
drawHighlightedValues(&painter);
drawSelectedValues(&painter);
}
void DensityPlotWidget::drawFrame(QPainter *painter) {
painter->save();
// painter->drawRect(margin, 0, w - 2 * margin, h - margin);
painter->drawLine(margin, h - margin, w - margin, h - margin);
painter->restore();
}
void DensityPlotWidget::drawValues(QPainter *painter, const QList<double> &list) {
painter->save();
if (rangeHigh == rangeLow) {
return;
}
double pixelsOverRange = (w - 2 * margin) / (rangeHigh - rangeLow);
painter->translate(margin, 0);
for (int i = 0; i < list.size(); ++i) {
painter->drawLine((list[i] - rangeLow) * pixelsOverRange, margin,
(list[i] - rangeLow) * pixelsOverRange, h - 2 * margin);
}
painter->restore();
}
void DensityPlotWidget::drawValues(QPainter *painter) {
painter->save();
painter->setPen(Qt::gray);
drawValues(painter, valueList);
painter->restore();
}
void DensityPlotWidget::drawHighlightedValues(QPainter *painter) {
painter->save();
painter->setPen(Qt::red);
if (rangeHigh == rangeLow) {
return;
}
double pixelsOverRange = (w - 2 * margin) / (rangeHigh - rangeLow);
for (int i = 0; i < hlList.size(); ++i) {
painter->drawLine((hlList[i] - rangeLow) * pixelsOverRange, margin / 2,
(hlList[i] - rangeLow) * pixelsOverRange, h - 1.5 * margin);
}
painter->restore();
}
void DensityPlotWidget::drawSelectedValues(QPainter *painter) {
painter->save();
painter->setPen(Qt::blue);
drawValues(painter, selList);
painter->restore();
}
void DensityPlotWidget::drawSelectedValues(const QList<double> & values) {
selList = values;
update();
}
void DensityPlotWidget::drawSelectedValue(double value) {
selList.clear();
selList << value;
update();
}
void DensityPlotWidget::drawLabels(QPainter *painter) {
double inc = (rangeHigh - rangeLow) / (tickNumber + 1);
double realInc = (w - 2 * margin) / (tickNumber + 1);
for (int i = 0; i <= tickNumber; ++i) {
painter->drawLine(margin + i * realInc, h - margin, margin + i
* realInc, h - margin / 2);
painter->drawText(margin + i * realInc, h - 1, QString::number(
int(rangeLow + i * inc)));
}
QFontMetrics fm(painter->font());
QString lastLabel = QString::number(int(rangeHigh));
int stringWidth = fm.width(lastLabel);
painter->drawLine(w - margin, h - margin, w - margin, h - margin / 2);
painter->drawText(w - margin - stringWidth, h - 1, lastLabel);
}
void DensityPlotWidget::setDimension(int width, int height) {
setFixedWidth(width);
setFixedHeight(height);
}
void DensityPlotWidget::setTickNumber(int numOfTicks) {
tickNumber = numOfTicks;
update();
}
void DensityPlotWidget::setPlotWidth(int width) {
setFixedWidth(width);
}
void DensityPlotWidget::setPlotHeight(int height) {
setFixedHeight(height);
}
void DensityPlotWidget::setColor(const QColor & color) {
this->color = color;
}