fixes and improvements
[Sak.git] / piechart.h
blobc01ffa148f3f55735fe8d01bd0dff0243bcd6d19
1 #ifndef PIECHART_H_
2 #define PIECHART_H_
4 #include "task.h"
6 class TaskSummaryPieChart : public QGraphicsItem
8 public:
9 TaskSummaryPieChart(QGraphicsItem* parent = NULL) : QGraphicsItem(parent)
11 setAcceptsHoverEvents(true);
13 QRectF boundingRect() const { return QRectF(-0.61,-0.6,1.2,1.2);}
14 void setHits(const QList<HitElement>& hits)
16 qDeleteAll(m_subItems);
17 m_subItems.clear();
19 // compute total values per task
20 double total=0;
21 QMap<Task*, double> totals;
22 for(QList<HitElement>::const_iterator itr = hits.begin(); itr!= hits.end(); itr++) {
23 const HitElement& element = (*itr);
24 total += element.duration;
25 totals[element.task] = totals.value(element.task, 0) + element.duration;
27 // sort total values
28 QMap<double, Task*> sortedTotals;
29 for(QMap<Task*, double>::const_iterator titr = totals.begin(); titr!=totals.end(); titr++) {
30 sortedTotals.insertMulti(titr.value(), titr.key());
32 double startAngle = 90;
33 for(QMap<double, Task*>::const_iterator itr = sortedTotals.begin(); itr != sortedTotals.end(); itr++) {
34 Task* task = (*itr);
35 double spanAngle = itr.key() / (total) * 360;
36 qDebug() << "spanAngle" << spanAngle;
37 QGraphicsPathItem* pathItem = new QGraphicsPathItem(this);
38 scene()->addItem(pathItem);
39 pathItem->setToolTip(task->title);
40 QPainterPath path;
41 QPainterPath p;
42 p.moveTo(0,0);
43 p.arcTo(boundingRect(), startAngle, spanAngle);
44 p.closeSubpath();
45 pathItem->setPath(p);
46 pathItem->setBrush(task->bgColor);
47 pathItem->setPen(QPen(QBrush(task->fgColor),0));
48 startAngle += spanAngle;
49 m_subItems.insert(task , pathItem);
52 virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * /* option */, QWidget * /* widget */ )
55 // void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
57 protected:
58 QMap<Task*, QGraphicsPathItem*> m_subItems;
61 #endif