49 #include "ValenceHistogramWidget.hh" 53 ValenceHistogramWidget::ValenceHistogramWidget(QWidget *parent) :
58 void ValenceHistogramWidget::paintEvent(QPaintEvent *event) {
60 QWidget::paintEvent(event);
67 std::vector<size_t>::iterator nonzero_begin = histogram_->begin();
68 for (; nonzero_begin != histogram_->end() && *nonzero_begin == 0;
70 if (nonzero_begin == histogram_->end())
return;
72 std::vector<size_t>::iterator nonzero_end = histogram_->end();
73 for (; (nonzero_end-1) != nonzero_begin && *(nonzero_end-1) == 0;
76 const size_t hist_size = std::distance(nonzero_begin, nonzero_end);
77 const size_t hist_max = *std::max_element(nonzero_begin, nonzero_end);
78 const size_t ofs = std::distance(histogram_->begin(), nonzero_begin);
83 QRectF paint_rect = this->contentsRect();
84 QRectF bargraph_rect = paint_rect;
85 bargraph_rect.setBottom(bargraph_rect.bottom() - 16);
86 QRectF label_rect = paint_rect;
87 label_rect.setTop(bargraph_rect.bottom());
88 QPainter painter(
this);
93 painter.setRenderHint(QPainter::Antialiasing);
94 painter.setBrush(QColor::fromRgbF(0.518, 0.573, 0.643, 1.0));
95 painter.setFont(this->font());
98 static_cast<qreal
>(bargraph_rect.width()) / hist_size;
99 const qreal gap = (stride > 8) ? 1.0 : 0.0;
100 const qreal label_gap = 4;
101 QRectF barRect(0, 0, stride - gap, 0);
102 const qreal scale =
static_cast<qreal
>(bargraph_rect.height()) / hist_max;
108 qreal lastLabelX = label_rect.left();
109 for (std::vector<size_t>::iterator it = nonzero_begin;
110 it != nonzero_end; ++it, ++cnt) {
112 painter.setPen(Qt::NoPen);
113 barRect.setHeight(scale * (*it));
114 barRect.moveBottomLeft(
115 bargraph_rect.bottomLeft() + QPoint(stride * cnt, 0));
117 painter.drawRoundedRect(barRect, 3, 3, Qt::AbsoluteSize);
119 painter.drawRect(barRect);
122 painter.setPen(Qt::black);
123 QString labelText = QString::number(cnt + ofs);
125 painter.boundingRect(
126 QRectF(barRect.center().x()-50, label_rect.y(),
127 100.0, label_rect.height()),
128 Qt::AlignHCenter | Qt::AlignBottom, labelText);
129 if (labelBB.left() >= lastLabelX + label_gap) {
130 painter.drawText(labelBB, Qt::AlignHCenter | Qt::AlignBottom,
132 lastLabelX = labelBB.right();
137 void ValenceHistogramWidget::setHistogram(std::vector<size_t> *histogram) {
138 histogram_ = histogram;