From 2038b81df36cc44b63c44313a4fe39557c850760 Mon Sep 17 00:00:00 2001 From: am Date: Sat, 28 Nov 2009 16:00:04 +0000 Subject: -turned QualityStats into general purpose class GraphBarWidget git-svn-id: file:///var/svn/repos2/shemov/trunk@429 f440f766-f032-0410-8965-dc7d17de2ca0 --- graphbarwidget.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 graphbarwidget.cpp (limited to 'graphbarwidget.cpp') diff --git a/graphbarwidget.cpp b/graphbarwidget.cpp new file mode 100644 index 0000000..c6f9b0e --- /dev/null +++ b/graphbarwidget.cpp @@ -0,0 +1,125 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#include +#include + +#include "graphbarwidget.h" + +GraphBarWidget::GraphBarWidget(const QList > data, QWidget *parent) : QWidget(parent), mMargin(10), mData(data) { + mBarHeight = fontMetrics().height(); +} + +int GraphBarWidget::maxValue() const { + int retval = -1; + foreach(QList l, mData){ + if(l.at(1).toInt() > retval){ + retval = l.at(1).toInt(); + } + } + return retval; +} + +QSize GraphBarWidget::maxDescr() const { + QFont f = font(); + f.setBold(true); + QFontMetrics fm(f); + QSize retval(0,0); + foreach(QList l, mData){ + QString descr = l.at(0).toString(); + QSize curSize = fm.size(Qt::TextSingleLine, descr); + if(curSize.width() > retval.width()){ + retval = curSize; + } + } + return retval; +} + +QSize GraphBarWidget::maxCount() const { + QFontMetrics fm = fontMetrics(); + QSize retval(0,0); + foreach(QList l, mData){ + QString curCount = QString("(%1)").arg(l.at(2).toString()); + QSize curSize = fm.size(Qt::TextSingleLine, curCount); + if(curSize.width() > retval.width()){ + retval = curSize; + } + } + return retval; +} + +QSize GraphBarWidget::sizeHint() const { + int height = mData.count() * mBarHeight; + height += mData.count() * mMargin; + height += mBarHeight + mMargin; + return QSize(400, height); +} + +void GraphBarWidget::paintEvent(QPaintEvent *){ + QPainter p(this); + QHash length; //int + QList >::const_iterator it; + + //draw background + QPen pen(Qt::red, 2); + p.setPen(pen); + QLinearGradient bgGradient(QPointF(0.0, height() / 2), QPointF(width(), height() / 2)); + bgGradient.setColorAt(0, QColor(221, 107, 80)); + bgGradient.setColorAt(1, QColor(251, 179, 150)); + QBrush brush(bgGradient); + p.setBrush(brush); + p.drawRect(contentsRect()); + + //draw caption + QFont f = font(); + f.setUnderline(true); + f.setBold(true); + p.setFont(f); + QString cap(caption()); + QFontMetrics fm(f); + int captionStartx = (width() - fm.width(cap)) / 2; + int captionStarty = fm.height(); + p.drawText(captionStartx, captionStarty, cap); + + //prepare drawing data + int barsStarty = fm.height() + 2 * mMargin + 10; + f.setUnderline(false); + f.setBold(true); + p.setFont(f); + int maxdescr = maxDescr().width(); + int maxcount = maxCount().width(); + + int barsStartx = maxdescr + 2 * mMargin; + int marginRight = maxcount + 2 * mMargin; + int maxWidth = width() - barsStartx - marginRight; + p.setRenderHints(QPainter::Antialiasing); + p.setPen(QPen(Qt::red, 1)); + + //calculate real lengths based on widget width + for(it = mData.constBegin(); it != mData.constEnd(); ++it){ + qreal value = it->at(1).toDouble(); + value = value / maxValue() * maxWidth; + length[it->at(0).toString()] = value; + } + + //draw the bars and numbers + int ctr(0); + for(QList >::const_iterator itr = mData.constBegin(); itr != mData.constEnd() ; ++itr, ++ctr){ + int starty = barsStarty + ctr * p.fontMetrics().height() + ctr * mMargin; + p.drawText(mMargin, starty, itr->at(0).toString()); + QLinearGradient barGradient(QPointF(barsStartx, starty), QPointF(width(), starty)); + barGradient.setColorAt(0, QColor(0, 128, 170)); + barGradient.setColorAt(1, QColor(131, 205, 214)); + QBrush b(barGradient); + p.setBrush(b); + qreal len = length.value(itr->at(0).toString()); + QRect bar(barsStartx, starty - p.fontMetrics().height() + p.fontMetrics().descent(), len, p.fontMetrics().height()); + p.drawRect(bar); + int nStartx = barsStartx + len + mMargin; + p.drawText(nStartx, starty, QString(tr("(%1)")).arg(itr->at(2).toString())); + } +} -- cgit v1.2.3-70-g09d2