2013-02-26 12:17:35 +01:00
|
|
|
/*
|
|
|
|
Copyright 2010-2013 Grégory Soutadé
|
|
|
|
|
|
|
|
This file is part of KissCount.
|
|
|
|
|
|
|
|
KissCount 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
KissCount is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
#include <QtCharts/QChartView>
|
|
|
|
#include <QtCharts/QPieSeries>
|
|
|
|
#include <QtCharts/QPieSlice>
|
2013-02-26 12:17:35 +01:00
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
#include <QHeaderView>
|
2013-02-26 12:17:35 +01:00
|
|
|
#include "CostRepartitionBanner.hpp"
|
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
using namespace QtCharts;
|
|
|
|
|
2013-02-26 12:17:35 +01:00
|
|
|
CostRepartitionBanner::CostRepartitionBanner(KissCount* kiss, QFrame* parent, QString* categories) : QFrame(parent), _kiss(kiss), _categories(categories)
|
|
|
|
{
|
|
|
|
User* user = _kiss->GetUser();
|
|
|
|
int nbCategories, i;
|
|
|
|
QVBoxLayout *vbox = new QVBoxLayout;
|
|
|
|
|
|
|
|
setLayout(vbox);
|
|
|
|
|
|
|
|
nbCategories = user->GetCategoriesNumber();
|
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
QPieSeries *series = new QPieSeries();
|
|
|
|
QPieSlice* slice;
|
|
|
|
|
|
|
|
_pie = new QChart();
|
|
|
|
|
2013-02-26 12:17:35 +01:00
|
|
|
QPen pen;
|
|
|
|
pen.setWidth(2);
|
|
|
|
pen.setColor(Qt::black);
|
2017-10-15 11:36:27 +02:00
|
|
|
//_pie->pieDiagram()->setPen(pen);
|
2013-02-26 12:17:35 +01:00
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
QLegend* legend = _pie->legend();
|
|
|
|
legend->detachFromChart();
|
|
|
|
legend->setAlignment(Qt::AlignTop);
|
|
|
|
legend->setVisible(false);
|
|
|
|
legend->setShowToolTips(true);
|
|
|
|
// legend->setAlignment(Qt::AlignBottom);
|
|
|
|
|
|
|
|
//_pie->addLegend(KDChart::Position::South);
|
|
|
|
// KDChart::Legend* legend = //_pie->legend();
|
2013-02-26 12:17:35 +01:00
|
|
|
// legend->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
|
2017-10-15 11:36:27 +02:00
|
|
|
// legend->setOrientation( Qt::Vertical );
|
|
|
|
|
|
|
|
_pie->setMargins(QMargins(5, 5, 5, 5));
|
|
|
|
|
2013-02-26 12:17:35 +01:00
|
|
|
for(i=0; i<nbCategories; i++)
|
|
|
|
{
|
2017-10-15 11:36:27 +02:00
|
|
|
slice = series->append(_categories[i], 0);
|
2013-02-26 12:17:35 +01:00
|
|
|
if (i < wxUI::MAX_CATEGORY)
|
2017-10-15 11:36:27 +02:00
|
|
|
slice->setBrush(QBrush(wxUI::categoryColors[i]));
|
2017-10-15 11:36:27 +02:00
|
|
|
slice->setLabelVisible(false);
|
2013-02-26 12:17:35 +01:00
|
|
|
}
|
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
_pie->addSeries(series);
|
|
|
|
//_pie->setTitle(_("Cost repartition"));
|
|
|
|
//_pie->setMaximumSize( 200, 400 );
|
2013-02-26 12:17:35 +01:00
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
// KDChart::TextAttributes legendTextAttr(legend->textAttributes());
|
|
|
|
// legendTextAttr.setFontSize(64);
|
|
|
|
// legendTextAttr.setAutoShrink(true);
|
|
|
|
// legend->setTextAttributes(legendTextAttr);
|
2013-02-26 12:17:35 +01:00
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
// legendTextAttr = KDChart::TextAttributes(legend->titleTextAttributes());
|
|
|
|
// legendTextAttr.setFontSize(64);
|
|
|
|
// legendTextAttr.setAutoShrink(true);
|
|
|
|
// legend->setTitleTextAttributes(legendTextAttr);
|
2013-02-26 12:17:35 +01:00
|
|
|
|
|
|
|
_statsGrid = new QTableWidget(this);
|
|
|
|
_statsGrid->verticalHeader()->setHidden(true);
|
|
|
|
_statsGrid->horizontalHeader()->setHidden(true);
|
|
|
|
_statsGrid->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
|
2017-10-15 11:36:27 +02:00
|
|
|
_statsGrid->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
|
|
|
|
_statsGrid->setMaximumHeight( 300 );
|
2013-02-26 12:17:35 +01:00
|
|
|
|
|
|
|
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
|
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
QChartView *chartView = new QChartView(_pie);
|
|
|
|
chartView->setRenderHint(QPainter::Antialiasing);
|
2017-10-15 11:36:27 +02:00
|
|
|
|
|
|
|
QHBoxLayout *hbox = new QHBoxLayout;
|
|
|
|
|
|
|
|
hbox->addStretch();
|
|
|
|
hbox->addWidget(_statsGrid);
|
|
|
|
hbox->addStretch();
|
|
|
|
|
|
|
|
vbox->addLayout(hbox);
|
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
vbox->addWidget(chartView);
|
2013-02-26 12:17:35 +01:00
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
setMaximumWidth(350);
|
2013-02-26 12:17:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CostRepartitionBanner::Reset()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
User* user = _kiss->GetUser();
|
|
|
|
int nb_categories = user->GetCategoriesNumber();
|
|
|
|
|
|
|
|
if (!_statsGrid->rowCount())
|
|
|
|
{
|
|
|
|
_statsGrid->setColumnCount(2);
|
|
|
|
//_statsGrid->EnableEditing(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_statsGrid->clear();
|
|
|
|
}
|
|
|
|
_statsGrid->setRowCount(nb_categories+1); // categories + non fix
|
|
|
|
|
|
|
|
_statsGrid->setItem(1, 0, new QTableWidgetItem(_("Non fix")));
|
|
|
|
_statsGrid->setItem(1, 1, new QTableWidgetItem(""));
|
2013-02-26 16:04:27 +01:00
|
|
|
_statsGrid->item(1, 1)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
|
2013-02-26 12:17:35 +01:00
|
|
|
|
|
|
|
for(i=0; i<nb_categories; i++)
|
|
|
|
{
|
|
|
|
if (!i)
|
|
|
|
{
|
|
|
|
_statsGrid->setItem(i, 0, new QTableWidgetItem(_categories[i]));
|
|
|
|
_statsGrid->setItem(i, 1, new QTableWidgetItem(""));
|
|
|
|
_statsGrid->item(i, 1)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_statsGrid->setItem(i+1, 0, new QTableWidgetItem(_categories[i]));
|
|
|
|
_statsGrid->setItem(i+1, 1, new QTableWidgetItem(""));
|
|
|
|
_statsGrid->item(i+1, 1)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_statsGrid->resizeColumnToContents(0);
|
|
|
|
}
|
|
|
|
|
2013-02-26 15:34:52 +01:00
|
|
|
void CostRepartitionBanner::UpdateCosts(int *categoriesValues, int totalDebit)
|
2013-02-26 12:17:35 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
User* user = _kiss->GetUser();
|
|
|
|
int value, percents;
|
|
|
|
QString v;
|
2017-10-15 11:36:27 +02:00
|
|
|
QPieSeries* series = (QPieSeries*) (_pie->series()[0]);
|
|
|
|
QList<QtCharts::QPieSlice*> slices = series->slices();
|
|
|
|
|
2013-02-26 12:17:35 +01:00
|
|
|
for(i=0; i<user->GetCategoriesNumber(); i++)
|
|
|
|
{
|
|
|
|
if (totalDebit != 0)
|
|
|
|
percents = (categoriesValues[i]*100)/totalDebit;
|
|
|
|
else
|
|
|
|
percents = 0.0;
|
2017-10-15 11:36:27 +02:00
|
|
|
|
2013-02-26 12:17:35 +01:00
|
|
|
if (!i)
|
|
|
|
_statsGrid->item(i, 1)->setText(v.sprintf("%.2lf (%02d %%)", (double)categoriesValues[i]/100, (int)percents));
|
|
|
|
else
|
|
|
|
_statsGrid->item(i+1, 1)->setText(v.sprintf("%.2lf (%02d %%)", (double)categoriesValues[i]/100, (int)percents));
|
2017-10-15 11:36:27 +02:00
|
|
|
|
|
|
|
slices[i]->setValue(categoriesValues[i] / 100);
|
|
|
|
|
|
|
|
slices[i]->setLabelVisible(categoriesValues[i] != 0.0);
|
2013-02-26 12:17:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
value = totalDebit - categoriesValues[0];
|
|
|
|
if (totalDebit != 0)
|
|
|
|
percents = (value*100)/totalDebit;
|
|
|
|
else
|
|
|
|
percents = 0.0;
|
|
|
|
_statsGrid->item(1, 1)->setText(v.sprintf("%.2lf (%02d %%)", (double)value/100, (int)percents));
|
|
|
|
|
2017-10-15 11:36:27 +02:00
|
|
|
_statsGrid->resizeColumnsToContents();
|
2013-02-26 12:17:35 +01:00
|
|
|
}
|