Commit 5ab2383de03415b763cfda58a98d90ccad53067f

Created ExpensesGraphModel class to act as the ExpensesGraphModel. SummaryData and MonthData are children of ExpensesGraphModel. Added an evolution graph to the summary option
  
22#include <QPainter>
33#include <QListIterator>
44#include <QPen>
5#include <QDate>
56
67#include "expensesgraph.h"
78#include "expense.h"
1919{
2020 _padding = 10;
2121 _accumulated = true;
22 _month = NULL;
22 _model = NULL;
2323}
2424
2525void ExpensesGraph::setPadding(int padding)
2828 repaint();
2929}
3030
31void ExpensesGraph::setMonthData(MonthData *month)
31void ExpensesGraph::setModel(ExpensesGraphModel *model)
3232{
33 _month = month;
33 _model = model;
3434}
3535
3636void ExpensesGraph::setAccumulated(bool accumulated)
8181
8282 drawScene(painter);
8383
84 if (_month) {
84 if (_model) {
8585 /* labels in x axis */
86 xStepsPerLabel = _month->days() / qMin(sceneWidth / X_LABEL_WIDTH, _month->days());
86 xStepsPerLabel = _model->xDivisions() / qMin(sceneWidth / X_LABEL_WIDTH, _model->xDivisions());
8787 /* labels in y axis. not more than 10 */
8888 yStepsPerLabel = Y_DIVISIONS / qMin(sceneHeight / Y_LABEL_HEIGHT, Y_DIVISIONS);
8989
90 dailyExpense = _month->budget() / _month->days();
90 dailyExpense = _model->budget() / _model->xDivisions();
9191 if (_accumulated) {
92 if (_month->budget() >= _month->totalExpense()) {
93 yMaxValue = _month->budget();
92 if (_model->budget() >= _model->totalExpense()) {
93 yMaxValue = _model->budget();
9494 } else {
95 aux = _month->totalExpense() / 100;
95 aux = _model->totalExpense() / 100;
9696 yMaxValue = (aux + 1) * 100;
9797 }
9898 } else {
99 aux = qMax(_month->maxExpense(), dailyExpense) / 100;
99 aux = qMax(_model->maxExpense(), dailyExpense) / 100;
100100 yMaxValue = (aux + 1) * 100;
101101 }
102102 yScale = sceneHeight / (double)yMaxValue;
103103
104 xStep = sceneWidth / (double)(_month->days()-1);
104 xStep = sceneWidth / (double)(_model->xDivisions()-1);
105105 yStep = sceneHeight / (double)Y_DIVISIONS;
106106
107107 painter.setRenderHint(QPainter::Antialiasing);
165165 QString("1"));
166166 painter.setPen(pen);
167167 x = xStart + (xStep *xStepsPerLabel);
168 for (i = xStepsPerLabel + 1; i<=_month->days(); i+=xStepsPerLabel) {
169 if (i != _month->days()) {
168 for (i = xStepsPerLabel + 1; i<=_model->xDivisions(); i+=xStepsPerLabel) {
169 if (i != _model->xDivisions()) {
170170 painter.drawLine (qRound(x), yStart, qRound(x), yEnd);
171171 }
172172 painter.setPen(Qt::white);
181181 }
182182
183183 /* current */
184 if (_month->isCurrent()) {
184 if (_model->isCurrent()) {
185185 i = QDate::currentDate().day() - 1;
186186 painter.setPen (Qt::yellow);
187187 painter.drawLine (qRound(xStart + (i * xStep)),
203203 painter.drawLine (xStart + 1,
204204 qRound(yStart - (dailyExpense * yScale)),
205205 xEnd,
206 qRound(yStart - (_month->budget() * yScale)));
206 qRound(yStart - (_model->budget() * yScale)));
207207 } else {
208208 painter.drawLine (xStart + 1,
209209 qRound(yStart - (dailyExpense * yScale)),
224224 y = yStart;
225225 path.moveTo (x, y);
226226
227 for (i=1; i<=_month->days(); i++) {
227 for (i=0; i<_model->xDivisions(); i++) {
228228 if (_accumulated) {
229 y -= _month->summary()[i] * yScale;
229 y -= _model->value(i) * yScale;
230230 } else {
231 y = yStart - (_month->summary()[i] * yScale);
231 y = yStart - (_model->value(i) * yScale);
232232 }
233 if (i == 1) {
233 if (i == 0) {
234234 path.moveTo (x, y);
235235 } else {
236236 path.lineTo (x, y);
  
33
44#include <QWidget>
55#include <QList>
6#include "monthdata.h"
6#include "expensesgraphmodel.h"
77
88class ExpensesGraph : public QWidget
99{
1515 QSize sizeHint() const;
1616
1717 void setPadding(int padding);
18 void setMonthData(MonthData *month);
18 void setModel(ExpensesGraphModel *model);
1919 void setAccumulated(bool accumulated);
2020 bool accumulated();
2121
3333
3434 int _padding;
3535 bool _accumulated;
36 MonthData *_month;
36 ExpensesGraphModel *_model;
3737 int xStart, yStart;
3838 int sceneWidth, sceneHeight;
3939 int xEnd, yEnd;
  
7979{
8080 month = data;
8181 /* set data to the graph as well */
82 graph->setMonthData(data);
82 graph->setModel(data);
8383 connect(month, SIGNAL(expensesChanged()), this, SLOT(expensesChanged()));
8484 connect(month, SIGNAL(budgetChanged()), this, SLOT(budgetChanged()));
8585
  
44MonthData::MonthData(QDate date,
55 Account *account,
66 QObject *parent)
7 : QObject(parent)
7 : ExpensesGraphModel(parent)
88{
99 _date = date;
1010 _account = account;
1111 _budget = account->budget();
1212 _maxExpense = 0;
1313 _totalExpense = 0;
14 _summary.resize(_date.daysInMonth() + 1);
14 _summary.resize(_date.daysInMonth());
1515 _summary.fill(0);
1616 _expenses = new QStandardItemModel(0,5);
1717}
5353
5454void MonthData::addExpense(Expense *e)
5555{
56 _summary[e->day()] += e->amount();
56 _summary[e->day() - 1] += e->amount();
5757 _totalExpense += e->amount();
58 if (_summary[e->day()] > _maxExpense) {
59 _maxExpense = _summary[e->day()];
58 if (_summary[e->day() - 1] > _maxExpense) {
59 _maxExpense = _summary[e->day() - 1];
6060 }
6161
6262 QStandardItem *item;
124124 return _date;
125125}
126126
127const QVector<double>& MonthData::summary()
127double MonthData::value(int index)
128128{
129 return _summary;
129 return _summary[index];
130130}
131131
132132void MonthData::clear()
137137 _totalExpense = 0;
138138
139139 emit expensesChanged();
140}
141
142int MonthData::xDivisions()
143{
144 return _date.daysInMonth();
140145}
  
77#include <QStandardItemModel>
88#include <QDate>
99#include <QDebug>
10#include "expensesgraphmodel.h"
1011#include "expense.h"
1112#include "global.h"
1213#include "account.h"
1314
14class MonthData : public QObject
15class MonthData : public ExpensesGraphModel
1516{
1617 Q_OBJECT
1718 public:
3131 double maxExpense();
3232 double totalExpense();
3333 QStandardItemModel* expenses();
34 const QVector<double>& summary();
34 double value(int i);
3535 QDate next();
3636 QDate previous();
3737 bool isCurrent();
3838 QDate date();
3939 void clear();
40 int xDivisions();
4041
4142 signals:
42 void expensesChanged();
43 void budgetChanged();
4443
4544 private:
4645
  
1515 monthdata.h \
1616 controller.h \
1717 expensesgraph.h \
18 expensesgraphmodel.h \
1819 expensedetailsdialog.h \
1920 budgetdialog.h \
2021 conceptsdialog.h \
4141 controller.cpp \
4242 mainwindow.cpp \
4343 expensesgraph.cpp \
44 expensesgraphmodel.cpp \
4445 expensedetailsdialog.cpp \
4546 budgetdialog.cpp \
4647 conceptsdialog.cpp \
  
33SummaryData::SummaryData(int startYear,
44 int startMonth,
55 int endYear,
6 int endMonth)
6 int endMonth,
7 QObject *parent)
8 : ExpensesGraphModel(parent)
79{
810 _startYear = startYear;
911 _startMonth = startMonth;
5555 _totalExpense += expense;
5656 int saved = budget - expense;
5757 _totalSaved += saved;
58 _monthCount ++;
5958
59 _values.append(expense);
60
6061 if (expense > _maxExpense) {
6162 _maxExpense = expense;
6263 }
7373 if (saved < _minSaved) {
7474 _minSaved = saved;
7575 }
76 _monthCount ++;
7677}
7778
7879double SummaryData::totalBudget()
129129const QList<Concept*>& SummaryData::concepts()
130130{
131131 return _concepts;
132}
133
134double SummaryData::budget()
135{
136 return _totalBudget;
137}
138
139bool SummaryData::isCurrent()
140{
141 return false;
142}
143
144int SummaryData::xDivisions()
145{
146 return _monthCount;
147}
148
149double SummaryData::value(int index)
150{
151 return _values.value(index);
132152}
  
33
44#include <QObject>
55#include <QList>
6#include "expensesgraphmodel.h"
67#include "concept.h"
78
8class SummaryData : public QObject
9class SummaryData : public ExpensesGraphModel
910{
1011 Q_OBJECT
1112 public:
1213 SummaryData(int startYear,
1314 int startMonth,
1415 int endYear,
15 int endMonth);
16 int endMonth,
17 QObject *parent = 0);
1618 ~SummaryData();
1719
1820 int startYear();
3535 void addMonthData(double budget, double expense);
3636 void addConcept(Concept *c);
3737
38 /* form ExpensesGraphModel */
39 double budget();
40 bool isCurrent();
41 int xDivisions();
42 double value(int i);
43
3844 const QList<Concept*>& concepts();
3945
4046 private:
6161 int _monthCount;
6262
6363 QList<Concept*> _concepts;
64 QList<double> _values;
6465};
6566
6667#endif
  
2121 connect(statistics, SIGNAL(triggered()), this, SLOT(statisticsSelected()));
2222 QAction *items = new QAction(tr("Items"), actionGroup);
2323 connect(items, SIGNAL(triggered()), this, SLOT(itemsSelected()));
24 QAction *evolution = new QAction(tr("Evolution"), actionGroup);
25 connect(evolution, SIGNAL(triggered()), this, SLOT(evolutionSelected()));
2426
2527 QMenuBar *menuBar = new QMenuBar(this);
2628 menuBar->addAction(statistics);
2729 menuBar->addAction(items);
30 menuBar->addAction(evolution);
2831
2932 summaryData = NULL;
3033
3134 statistics->setCheckable(true);
3235 items->setCheckable(true);
36 evolution->setCheckable(true);
3337
3438 /* statistics mode stuff */
3539 statsBox = new QFrame();
7979 chart = new ConceptsChart();
8080 vbox->addWidget(chart, 1);
8181
82 graph = new ExpensesGraph();
83 vbox->addWidget(graph, 1);
84
8285 /* set statistics mode by default */
8386 statistics->setChecked(true);
8487 chart->hide();
88 graph->hide();
8589}
8690
8791SummaryWindow::~SummaryWindow()
9999{
100100 statsBox->show();
101101 chart->hide();
102 graph->hide();
102103}
103104
104105void SummaryWindow::itemsSelected()
105106{
106107 statsBox->hide();
107108 chart->show();
109 graph->hide();
108110}
109111
112void SummaryWindow::evolutionSelected()
113{
114 statsBox->hide();
115 chart->hide();
116 graph->show();
117}
118
110119void SummaryWindow::setSummaryData(SummaryData *data)
111120{
112121 if (summaryData != NULL) {
155155 chart->repaint();
156156 }
157157
158 graph->setModel(summaryData);
159 if (graph->isVisible()) {
160 graph->repaint();
161 }
162
158163 if (summaryData->totalExpense() == 0) {
159164 QMaemo5InformationBox::information(this, tr("Warning: there are no expenses stored in the selected period"));
165 }
166}
167
168void SummaryWindow::mousePressEvent(QMouseEvent *event)
169{
170 if (graph->isVisible()) {
171 graph->changeView();
160172 }
161173}
  
77#include "periodwindow.h"
88#include "summarydata.h"
99#include "conceptschart.h"
10#include "expensesgraph.h"
1011#include <QAction>
1112#include <QFrame>
1213
2323 ~SummaryWindow();
2424 void setSummaryData(SummaryData *data);
2525
26 protected:
27 void mousePressEvent(QMouseEvent *event);
28
2629 private slots:
2730 void statisticsSelected();
2831 void itemsSelected();
32 void evolutionSelected();
2933
3034 private:
3135 QAction *statistics;
4949
5050 /* items stuff */
5151 ConceptsChart *chart;
52 ExpensesGraph *graph;
5253
5354 SummaryData *summaryData;
5455