-
Notifications
You must be signed in to change notification settings - Fork 0
/
budgethistorymodel.cpp
83 lines (72 loc) · 2.17 KB
/
budgethistorymodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "budgethistorymodel.h"
#include <QBrush>
#include <QDate>
#include <QPalette>
static const int DateColumn = 0;
static const int AmountColumn = 1;
static const int SubtotalColumn = 2;
/*---------------------------------------------------------------------
* BudgetHistoryModel::BudgetHistoryModel()
*/
/*!
* \brief Constructor.
*
* \param parent Parent
*/
BudgetHistoryModel::BudgetHistoryModel( QObject* parent )
: QSqlQueryModel( parent )
{
}
/*---------------------------------------------------------------------
* BudgetHistoryModel::data()
*/
/*!
* \brief Handles only the "Background Role" to get specialized
* behavior to give a visual clue of the separation of each
* month.
*
* \param index Index for the data
* \param role Role needed
*
* \return Returns the base or alternateBase of the data.
*
* \sa QAbstractItemModel::data()
*/
QVariant BudgetHistoryModel::data(const QModelIndex& index,
int role) const
{
const int row = index.row();
const int column = index.column();
QVariant datum;
if (role == Qt::BackgroundRole) {
QPalette palette;
QBrush brush( palette.base() );
if (row == 0) {
brush = palette.alternateBase();
} else {
QModelIndex myDateIndex = sibling( row, DateColumn, index );
QModelIndex compareDateIndex = sibling( row-1, DateColumn, index );
QDate myDate = myDateIndex.data(Qt::DisplayRole).toDate();
QDate compareDate = compareDateIndex.data(Qt::DisplayRole).toDate();
if (myDate.month() != compareDate.month() ||
myDate.year() != compareDate.year()) {
brush = palette.alternateBase();
}
}
datum = brush;
} else if (role == Qt::ForegroundRole &&
(column == AmountColumn || column == SubtotalColumn) ) {
QPalette palette;
QBrush brush( palette.text() );
QVariant amount = QSqlQueryModel::data( index, Qt::DisplayRole );
if (amount.toDouble() < 0.0) {
brush.setColor( Qt::darkRed );
} else {
brush.setColor( Qt::darkGreen );
}
datum = brush;
} else {
datum = QSqlQueryModel::data(index, role);
}
return datum;
}