/*
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 .
*/
#include
#include
#include "CostRepartitionBanner.hpp"
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();
_pie = new KDChart::Widget();
_pie->setType( KDChart::Widget::Pie );
QPen pen;
pen.setWidth(2);
pen.setColor(Qt::black);
_pie->pieDiagram()->setPen(pen);
_pie->addLegend(KDChart::Position::South);
KDChart::Legend* legend = _pie->legend();
// legend->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
legend->setOrientation( Qt::Vertical );
legend->setTitleText( _("Cost repartition") );
QVector< double > vec;
vec << 0.0;
_dataset = new QStandardItemModel(nbCategories, 2, this);
for(i=0; isetDataset( i, vec, _categories[i] );
_pie->pieDiagram()->setBrush(i, QBrush(wxUI::categoryColors[i]));
legend->setText( i, _categories[i] );
_dataset->setData(_dataset->index(i, 0, QModelIndex()), _categories[i]);
_dataset->setData(_dataset->index(i, 1, QModelIndex()), 0.0);
if (i < wxUI::MAX_CATEGORY)
_dataset->setData(_dataset->index(i, 0, QModelIndex()), wxUI::categoryColors[i], Qt::DecorationRole);
}
_pie->setMaximumSize( 200, 400 );
KDChart::TextAttributes legendTextAttr(legend->textAttributes());
legendTextAttr.setFontSize(64);
legendTextAttr.setAutoShrink(true);
legend->setTextAttributes(legendTextAttr);
legendTextAttr = KDChart::TextAttributes(legend->titleTextAttributes());
legendTextAttr.setFontSize(64);
legendTextAttr.setAutoShrink(true);
legend->setTitleTextAttributes(legendTextAttr);
_statsGrid = new QTableWidget(this);
_statsGrid->verticalHeader()->setHidden(true);
_statsGrid->horizontalHeader()->setHidden(true);
_statsGrid->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
QSize s = legend->sizeHint();
legend->setMaximumSize(s.width(), s.height()/3);
vbox->addWidget(_statsGrid);
vbox->addWidget(_pie);
setMaximumWidth(300);
}
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(""));
_statsGrid->item(1, 1)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
for(i=0; isetItem(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);
}
void CostRepartitionBanner::UpdateCosts(int *categoriesValues, int totalDebit)
{
int i;
User* user = _kiss->GetUser();
int value, percents;
QString v;
for(i=0; iGetCategoriesNumber(); i++)
{
if (totalDebit != 0)
percents = (categoriesValues[i]*100)/totalDebit;
else
percents = 0.0;
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));
// KDChart::Legend* legend = _pie->legend();
QVector< double > vec;
vec << (double) categoriesValues[i] / 100;
_pie->setDataset( i, vec, _categories[i] );
// if (categoriesValues[i] == 0.0)
// legend->setDatasetHidden(i, true);
// else
// legend->setDatasetHidden(i, false);
// _dataset->setData(_dataset->index(i, 1, QModelIndex()), categoriesValues[i]);
}
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));
_statsGrid->resizeColumnToContents(1);
}