KissCount

KissCount Git Source Tree

Root/src/view/CostRepartitionBanner.cpp

1/*
2 Copyright 2010-2013 Grégory Soutadé
3
4 This file is part of KissCount.
5
6 KissCount is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 KissCount is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with KissCount. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include <KDChartLegend>
21#include <KDChartPieDiagram>
22
23#include "CostRepartitionBanner.hpp"
24
25CostRepartitionBanner::CostRepartitionBanner(KissCount* kiss, QFrame* parent, QString* categories) : QFrame(parent), _kiss(kiss), _categories(categories)
26{
27 User* user = _kiss->GetUser();
28 int nbCategories, i;
29 QVBoxLayout *vbox = new QVBoxLayout;
30
31 setLayout(vbox);
32
33 nbCategories = user->GetCategoriesNumber();
34
35 _pie = new KDChart::Widget();
36 _pie->setType( KDChart::Widget::Pie );
37 QPen pen;
38 pen.setWidth(2);
39 pen.setColor(Qt::black);
40 _pie->pieDiagram()->setPen(pen);
41
42 _pie->addLegend(KDChart::Position::South);
43 KDChart::Legend* legend = _pie->legend();
44 // legend->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
45 legend->setOrientation( Qt::Vertical );
46 legend->setTitleText( _("Cost repartition") );
47 QVector< double > vec;
48 vec << 0.0;
49 _dataset = new QStandardItemModel(nbCategories, 2, this);
50 for(i=0; i<nbCategories; i++)
51 {
52_pie->setDataset( i, vec, _categories[i] );
53_pie->pieDiagram()->setBrush(i, QBrush(wxUI::categoryColors[i]));
54legend->setText( i, _categories[i] );
55_dataset->setData(_dataset->index(i, 0, QModelIndex()), _categories[i]);
56_dataset->setData(_dataset->index(i, 1, QModelIndex()), 0.0);
57if (i < wxUI::MAX_CATEGORY)
58 _dataset->setData(_dataset->index(i, 0, QModelIndex()), wxUI::categoryColors[i], Qt::DecorationRole);
59 }
60
61 _pie->setMaximumSize( 200, 400 );
62
63 KDChart::TextAttributes legendTextAttr(legend->textAttributes());
64 legendTextAttr.setFontSize(64);
65 legendTextAttr.setAutoShrink(true);
66 legend->setTextAttributes(legendTextAttr);
67
68 legendTextAttr = KDChart::TextAttributes(legend->titleTextAttributes());
69 legendTextAttr.setFontSize(64);
70 legendTextAttr.setAutoShrink(true);
71 legend->setTitleTextAttributes(legendTextAttr);
72
73 _statsGrid = new QTableWidget(this);
74 _statsGrid->verticalHeader()->setHidden(true);
75 _statsGrid->horizontalHeader()->setHidden(true);
76 _statsGrid->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
77
78 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
79
80 QSize s = legend->sizeHint();
81 legend->setMaximumSize(s.width(), s.height()/3);
82
83 vbox->addWidget(_statsGrid);
84 vbox->addWidget(_pie);
85
86 setMaximumWidth(300);
87}
88
89void CostRepartitionBanner::Reset()
90{
91 int i;
92 User* user = _kiss->GetUser();
93 int nb_categories = user->GetCategoriesNumber();
94
95 if (!_statsGrid->rowCount())
96 {
97_statsGrid->setColumnCount(2);
98 //_statsGrid->EnableEditing(false);
99 }
100 else
101 {
102 _statsGrid->clear();
103 }
104 _statsGrid->setRowCount(nb_categories+1); // categories + non fix
105
106 _statsGrid->setItem(1, 0, new QTableWidgetItem(_("Non fix")));
107 _statsGrid->setItem(1, 1, new QTableWidgetItem(""));
108
109 for(i=0; i<nb_categories; i++)
110 {
111if (!i)
112{
113 _statsGrid->setItem(i, 0, new QTableWidgetItem(_categories[i]));
114 _statsGrid->setItem(i, 1, new QTableWidgetItem(""));
115 _statsGrid->item(i, 1)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
116}
117else
118{
119 _statsGrid->setItem(i+1, 0, new QTableWidgetItem(_categories[i]));
120 _statsGrid->setItem(i+1, 1, new QTableWidgetItem(""));
121 _statsGrid->item(i+1, 1)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
122}
123 }
124
125 _statsGrid->resizeColumnToContents(0);
126}
127
128void CostRepartitionBanner::UpdateCosts(int *categoriesValues, int totalDebit)
129{
130 int i;
131 User* user = _kiss->GetUser();
132 int value, percents;
133 QString v;
134
135 for(i=0; i<user->GetCategoriesNumber(); i++)
136 {
137 if (totalDebit != 0)
138 percents = (categoriesValues[i]*100)/totalDebit;
139 else
140 percents = 0.0;
141if (!i)
142 _statsGrid->item(i, 1)->setText(v.sprintf("%.2lf (%02d %%)", (double)categoriesValues[i]/100, (int)percents));
143else
144 _statsGrid->item(i+1, 1)->setText(v.sprintf("%.2lf (%02d %%)", (double)categoriesValues[i]/100, (int)percents));
145
146// KDChart::Legend* legend = _pie->legend();
147QVector< double > vec;
148vec << (double) categoriesValues[i] / 100;
149_pie->setDataset( i, vec, _categories[i] );
150// if (categoriesValues[i] == 0.0)
151// legend->setDatasetHidden(i, true);
152// else
153// legend->setDatasetHidden(i, false);
154// _dataset->setData(_dataset->index(i, 1, QModelIndex()), categoriesValues[i]);
155 }
156
157 value = totalDebit - categoriesValues[0];
158 if (totalDebit != 0)
159 percents = (value*100)/totalDebit;
160 else
161 percents = 0.0;
162 _statsGrid->item(1, 1)->setText(v.sprintf("%.2lf (%02d %%)", (double)value/100, (int)percents));
163
164 _statsGrid->resizeColumnToContents(1);
165}

Archive Download this file