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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/***************************************************************************
kinvestmentlistitem.cpp - description
-------------------
begin : Wed Feb 6 2002
copyright : (C) 2000-2002 by Michael Edwardes
email : mte@users.sourceforge.net
Javier Campos Morales <javi_c@users.sourceforge.net>
Felix Rodriguez <frodriguez@users.sourceforge.net>
John C <thetacoturtle@users.sourceforge.net>
Thomas Baumgart <ipwizard@users.sourceforge.net>
Kevin Tambascio <ktambascio@users.sourceforge.net>
***************************************************************************/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// ----------------------------------------------------------------------------
// QT Includes
#include <qpainter.h>
// ----------------------------------------------------------------------------
// KDE Includes
#include <klistview.h>
// ----------------------------------------------------------------------------
// Project Includes
#include <kmymoney/kmymoneyglobalsettings.h>
#include "kinvestmentlistitem.h"
#include <kmymoney/mymoneysecurity.h>
#include <kmymoney/mymoneyfile.h>
KInvestmentListItem::KInvestmentListItem(KListView* parent, const MyMoneyAccount& account)
: KListViewItem(parent)
{
bColumn5Negative = false;
bColumn6Negative = false;
bColumn7Negative = false;
bColumn8Negative = false;
bColumn9Negative = false;
m_account = account;
m_listView = parent;
MyMoneySecurity security;
MyMoneyFile* file = MyMoneyFile::instance();
security = file->security(m_account.currencyId());
m_tradingCurrency = file->security(security.tradingCurrency());
int prec = MyMoneyMoney::denomToPrec(m_tradingCurrency.smallestAccountFraction());
QValueList<MyMoneyTransaction> transactionList;
// FIXME PRICE
// equity_price_history history = equity.priceHistory();
//column 0 (COLUMN_NAME_INDEX) is the name of the stock
setText(COLUMN_NAME_INDEX, m_account.name());
//column 1 (COLUMN_SYMBOL_INDEX) is the ticker symbol
setText(COLUMN_SYMBOL_INDEX, security.tradingSymbol());
//column 2 is the net value (price * quantity owned)
MyMoneyPrice price = file->price(m_account.currencyId(), m_tradingCurrency.id());
if(price.isValid()) {
setText(COLUMN_VALUE_INDEX, (file->balance(m_account.id()) * price.rate(m_tradingCurrency.id())).formatMoney(m_tradingCurrency.tradingSymbol(), prec));
} else {
setText(COLUMN_VALUE_INDEX, "---");
}
//column 3 (COLUMN_QUANTITY_INDEX) is the quantity of shares owned
prec = MyMoneyMoney::denomToPrec(security.smallestAccountFraction());
setText(COLUMN_QUANTITY_INDEX, file->balance(m_account.id()).formatMoney("", prec));
//column 4 is the current price
// Get the price precision from the configuration
prec = KMyMoneyGlobalSettings::pricePrecision();
// prec = MyMoneyMoney::denomToPrec(m_tradingCurrency.smallestAccountFraction());
if(price.isValid()) {
setText(COLUMN_PRICE_INDEX, price.rate(m_tradingCurrency.id()).formatMoney(m_tradingCurrency.tradingSymbol(), prec));
} else {
setText(COLUMN_PRICE_INDEX, "---");
}
}
KInvestmentListItem::~KInvestmentListItem()
{
}
// FIXME PRICE
#if 0
const QString KInvestmentListItem::calculate1WeekGain(const equity_price_history& history)
{
return calculateGain(history, -7, 0, false, bColumn6Negative);
}
const QString KInvestmentListItem::calculate4WeekGain(const equity_price_history& history)
{
return calculateGain(history, -28, 0, false, bColumn7Negative);
}
const QString KInvestmentListItem::calculate3MonthGain(const equity_price_history& history)
{
return calculateGain(history, 0, -3, false, bColumn8Negative);
}
const QString KInvestmentListItem::calculateYTDGain(const equity_price_history& history)
{
return calculateGain(history, 0, 0, true, bColumn9Negative);
}
const QString KInvestmentListItem::calculateGain(const equity_price_history& history, int dayDifference, int monthDifference, bool YTD, bool& bNegative)
{
bNegative = false;
if(history.isEmpty())
{
return QString("0.0%");
}
else
{
bool bFoundCurrent = false, bFoundComparison = false;
QDate tempDate, comparisonDate = QDate::currentDate();
if(YTD)
{
//if it is YTD, set the date to 01/01/<current year>
comparisonDate.setYMD(comparisonDate.year(), 1, 1);
}
else
{
comparisonDate = comparisonDate.addDays(dayDifference);
comparisonDate = comparisonDate.addMonths(monthDifference);
}
MyMoneyMoney comparisonValue, currentValue;
//find the current value, or closest to the current value.
equity_price_history::ConstIterator itToday = history.end();
for(tempDate = QDate::currentDate(); tempDate >= comparisonDate; )
{
itToday = history.find(tempDate);
if(itToday != history.end())
{
currentValue = itToday.data();
bFoundCurrent = true;
break;
}
tempDate = tempDate.addDays(-1);
}
if(!bFoundCurrent)
{
return QString("0.0%");
}
//find a date that is closest to a week old, not older, and not today's date. Because its a QMap, this map
//should already be sorted earliest to latest.
for(equity_price_history::ConstIterator it = history.begin(); it != history.end(); ++it)
{
if(it.key() >= comparisonDate && it.key() < QDate::currentDate())
{
comparisonDate = it.key();
comparisonValue = it.data();
bFoundComparison = true;
break;
}
}
if(!bFoundComparison)
{
return QString("0.0%");
}
//qDebug("Current date/value to use is %s/%s, Previous is %s/%s", tempDate.toString().data(), currentValue.toString().data(), comparisonDate.toString().data(), comparisonValue.toString().data());
//compute the percentage difference
if(comparisonValue != currentValue)
{
double result = (currentValue.toDouble() / comparisonValue.toDouble()) * 100.0;
result -= 100.0;
if(result < 0.0)
{
bNegative = true;
}
QString ds = QString("%1%").arg(result, 0, 'f', 3);
return ds;
/*MyMoneyMoney result = (currentValue / comparisonValue);
result = result * 100;
result = result - 100;
qDebug("final result = %s", result.toString().data());
return QString(result.formatMoney("", 3) + "%");*/
}
}
return QString("");
}
#endif
int KInvestmentListItem::compare(QListViewItem* i, int col, bool ascending) const
{
KInvestmentListItem* item = dynamic_cast<KInvestmentListItem*>(i);
// do special sorting only for numeric columns
// in all other cases use the standard sorting
if(item) {
switch(col) {
case COLUMN_VALUE_INDEX:
case COLUMN_QUANTITY_INDEX:
case COLUMN_PRICE_INDEX:
{
bool inv1 = text(col) == "---";
bool inv2 = item->text(col) == "---";
if(!inv1 && !inv2) {
MyMoneyMoney result = MyMoneyMoney(text(col)) - MyMoneyMoney(item->text(col));
if(result.isNegative())
return -1;
if(result.isZero())
return 0;
return 1;
} else if(inv1 && inv2) {
return 0;
} else if(inv1) {
return -1;
}
return 1;
}
break;
default:
break;
}
}
// do standard sorting here
return KListViewItem::compare(i, col, ascending);
}
void KInvestmentListItem::paintCell(QPainter * p, const QColorGroup & cg, int column, int width, int align)
{
bool bPaintRed = false;
if((column == COLUMN_RAWGAIN_INDEX && bColumn5Negative) ||
(column == COLUMN_1WEEKGAIN_INDEX && bColumn6Negative) ||
(column == COLUMN_4WEEKGAIN_INDEX && bColumn7Negative) ||
(column == COLUMN_3MONGAIN_INDEX && bColumn8Negative) ||
(column == COLUMN_YTDGAIN_INDEX && bColumn9Negative))
{
bPaintRed = true;
}
p->save();
QColorGroup cg2(cg);
if(isAlternate())
cg2.setColor(QColorGroup::Base, KMyMoneyGlobalSettings::listColor());
else
cg2.setColor(QColorGroup::Base, KMyMoneyGlobalSettings::listBGColor());
#ifndef KMM_DESIGNER
QFont font = KMyMoneyGlobalSettings::listCellFont();
// strike out closed accounts
if(m_account.isClosed())
font.setStrikeOut(true);
p->setFont(font);
#endif
if(bPaintRed)
{
QColorGroup _cg( cg2);
QColor c = _cg.text();
_cg.setColor(QColorGroup::Text, Qt::red);
QListViewItem::paintCell(p, _cg, column, width, align);
_cg.setColor(QColorGroup::Text, c);
}
else
{
QListViewItem::paintCell(p, cg2, column, width, align);
}
p->restore();
}
|