44 #include "QtHistogramWidget.hh" 51 QtHistogramWidget::QtHistogramWidget(QWidget *parent)
53 color_(QColor::fromRgbF(0.518, 0.573, 0.643, 1.0))
56 QtHistogramWidget::~QtHistogramWidget() =
default;
58 void QtHistogramWidget::setHistogram(std::unique_ptr<Histogram> histogram) {
59 histogram_ = std::move(histogram);
63 void QtHistogramWidget::setColorCoder(std::unique_ptr<IColorCoder> color_coder) {
64 color_coder_ = std::move(color_coder);
68 void QtHistogramWidget::paintEvent(QPaintEvent *event) {
70 QWidget::paintEvent(event);
77 const std::vector<size_t> &bins = histogram_->getBins();
78 const std::vector<double> &bin_widths = histogram_->getBinWidths();
79 const double total_width = histogram_->getTotalWidth();
82 size_t hist_max = bins.size() > 0 ? *std::max_element(bins.begin(), bins.end()) : 0;
87 const qreal labelHeight = 16;
88 QRectF paint_rect = this->contentsRect();
89 QRectF bargraph_rect = paint_rect;
90 bargraph_rect.setBottom(bargraph_rect.bottom() - labelHeight);
91 QRectF label_rect = paint_rect;
92 label_rect.setTop(bargraph_rect.bottom());
93 QPainter painter(
this);
98 painter.setRenderHint(QPainter::Antialiasing);
99 painter.setFont(this->font());
101 const qreal avg_width = bargraph_rect.width() / bins.size();
102 const qreal gap = (avg_width > 8) ? 1.0 : 0.0;
103 const qreal label_gap = 4;
104 const qreal y_scale = bargraph_rect.height() / hist_max;
106 const qreal total_gap = (bins.size() - 1) * gap;
107 const qreal total_barwidth = bargraph_rect.width() - total_gap;
113 double cumulative_width = 0.0;
115 qreal lastLabelX = label_rect.left();
116 const size_t n_bins = bins.size();
117 for (
size_t idx = 0; idx < n_bins; ++idx) {
118 const double bin_width = bin_widths[idx];
119 const qreal bar_width = total_barwidth * (bin_width / total_width);
121 painter.setPen(Qt::NoPen);
123 const double t = (cumulative_width + bin_width/2) / total_width;
124 cumulative_width += bin_width;
126 painter.setBrush(getColor(t));
128 barRect.setWidth(bar_width - gap);
129 barRect.setHeight(y_scale * bins[idx]);
130 barRect.moveBottomLeft(bargraph_rect.bottomLeft() + QPoint(xpos, 0));
133 painter.drawRoundedRect(barRect, 3, 3, Qt::AbsoluteSize);
135 painter.drawRect(barRect);
138 painter.setPen(Qt::black);
141 switch (histogram_->getLabelType()) {
142 case Histogram::LabelType::PerBin:
143 labelX = barRect.center().x();
144 labelText = histogram_->getBinLabel(idx);
146 case Histogram::LabelType::PerBoundary:
147 labelX = barRect.x();
148 labelText = histogram_->getBoundaryLabel(idx);
151 QRectF labelBB = painter.boundingRect(
152 QRectF(labelX - (label_distance_/2), label_rect.y(),
153 label_distance_, label_rect.height()),
154 Qt::AlignHCenter | Qt::AlignBottom, labelText);
156 if (labelBB.left() >= lastLabelX + label_gap) {
157 painter.drawText(labelBB, Qt::AlignHCenter | Qt::AlignBottom,
159 lastLabelX = labelBB.right();
168 QColor QtHistogramWidget::getColor(
double val)
171 return color_coder_->color_qcolor(val);
Namespace providing different geometric functions concerning angles.