workingOn action
[Sak.git] / piechart.h
blobabf9d4bdbd2414ba64aa336b54d3b1eb1ee462fd
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 QGraphicsPathItem* pathItem = new QGraphicsPathItem(this);
37 scene()->addItem(pathItem);
38 pathItem->setToolTip(task->title);
39 QPainterPath path;
40 QPainterPath p;
41 p.moveTo(0,0);
42 p.arcTo(boundingRect(), startAngle, spanAngle);
43 p.closeSubpath();
44 pathItem->setPath(p);
45 pathItem->setBrush(task->bgColor);
46 pathItem->setPen(QPen(QBrush(task->fgColor),0));
47 startAngle += spanAngle;
48 m_subItems.insert(task , pathItem);
51 virtual void paint ( QPainter * painter, const QStyleOptionGraphicsItem * /* option */, QWidget * /* widget */ )
54 // void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
56 protected:
57 QMap<Task*, QGraphicsPathItem*> m_subItems;
60 #endif