summaryrefslogtreecommitdiffstats
path: root/kmymoney2/dialogs/kbalancechartdlg.cpp
blob: ef86bee6e2d69892c43002dff28c51a84cf89b11 (plain)
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
/***************************************************************************
                          kbalancechartdlg  -  description
                             -------------------
    begin                : Mon Nov 26 2007
    copyright            : (C) 2007 by Thomas Baumgart
    email                : Thomas Baumgart <[email protected]>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// ----------------------------------------------------------------------------
// QT Includes

#include <tqlayout.h>
#include <tqframe.h>

// ----------------------------------------------------------------------------
// KDE Includes

#include <kpushbutton.h>
#include <tdelocale.h>

// ----------------------------------------------------------------------------
// Project Includes

#include "kbalancechartdlg.h"

#include <kmymoney/mymoneyreport.h>
#include "../reports/kreportchartview.h"
#include "../reports/pivottable.h"

#include <kmymoney/kmymoneyglobalsettings.h>


// in KOffice version < 1.5 KDCHART_PROPSET_NORMAL_DATA was a static const
// but in 1.5 this has been changed into a #define'd value. So we have to
// make sure, we use the right one.
#ifndef KDCHART_PROPSET_NORMAL_DATA
#define KMM_KDCHART_PROPSET_NORMAL_DATA KDChartParams::KDCHART_PROPSET_NORMAL_DATA
#else
#define KMM_KDCHART_PROPSET_NORMAL_DATA KDCHART_PROPSET_NORMAL_DATA
#endif

KBalanceChartDlg::KBalanceChartDlg(const MyMoneyAccount& account, TQWidget* parent, const char* name) :
  KDialog(parent, name)
{
#ifdef HAVE_KDCHART
  setCaption(i18n("Balance of %1").arg(account.name()));
  setSizeGripEnabled( TRUE );
  setModal( TRUE );

  TQVBoxLayout* KBalanceChartDlgLayout = new TQVBoxLayout( this, 11, 6, "KBalanceChartDlgLayout");

  MyMoneyReport reportCfg = MyMoneyReport(
                                          MyMoneyReport::eAssetLiability,
                                          MyMoneyReport::eMonths,
                                          MyMoneyTransactionFilter::userDefined, // overridden by the setDateFilter() call below
                                          MyMoneyReport::eDetailTotal,
                                          i18n("%1 Balance History").arg(account.name()),
                                               i18n("Generated Report")
                                         );
  reportCfg.setChartByDefault(true);
  reportCfg.setChartGridLines(false);
  reportCfg.setChartDataLabels(false);
  reportCfg.setChartType(MyMoneyReport::eChartLine);
  reportCfg.setIncludingSchedules( true );
  if(account.accountType() == MyMoneyAccount::Investment) {
    TQStringList::const_iterator it_a;
    for(it_a = account.accountList().begin(); it_a != account.accountList().end(); ++it_a)
      reportCfg.addAccount(*it_a);
  } else
    reportCfg.addAccount(account.id());
  reportCfg.setColumnsAreDays( true );
  reportCfg.setConvertCurrency( false );
  reportCfg.setDateFilter(TQDate::currentDate().addDays(-90),TQDate::currentDate().addDays(+90));

  reports::PivotTable table(reportCfg);

  reports::KReportChartView* chartWidget = new reports::KReportChartView(this, 0);

  table.drawChart(*chartWidget);

  chartWidget->params()->setLineMarker(false);
  chartWidget->params()->setLegendPosition(KDChartParams::NoLegend);
  chartWidget->params()->setLineWidth(2);
  chartWidget->params()->setDataColor(0, TDEGlobalSettings::textColor());

  // draw future values in a different line style
  KDChartPropertySet propSetFutureValue("future value", KMM_KDCHART_PROPSET_NORMAL_DATA);
  propSetFutureValue.setLineStyle(KDChartPropertySet::OwnID, Qt::DotLine);
  int m_idPropFutureValue = chartWidget->params()->registerProperties(propSetFutureValue);

  KDChartPropertySet propSetLastValue("last value", m_idPropFutureValue);
  propSetLastValue.setExtraLinesAlign(KDChartPropertySet::OwnID, TQt::AlignLeft | TQt::AlignBottom);
  propSetLastValue.setExtraLinesWidth(KDChartPropertySet::OwnID, -4);
  propSetLastValue.setExtraLinesColor(KDChartPropertySet::OwnID, KMyMoneyGlobalSettings::listGridColor());

  int m_idPropLastValue = chartWidget->params()->registerProperties(propSetLastValue);

  KDChartPropertySet propSetMinBalance("min balance", m_idPropFutureValue);
  propSetMinBalance.setLineStyle(KDChartPropertySet::OwnID, Qt::NoPen);
  propSetMinBalance.setExtraLinesAlign(KDChartPropertySet::OwnID, TQt::AlignLeft | TQt::AlignRight);
  int m_idPropMinBalance = chartWidget->params()->registerProperties(propSetMinBalance);

  KDChartPropertySet propSetMaxCredit("max credit", m_idPropMinBalance);
  propSetMaxCredit.setExtraLinesColor(KDChartPropertySet::OwnID, KMyMoneyGlobalSettings::listNegativeValueColor());
  propSetMaxCredit.setExtraLinesStyle(KDChartPropertySet::OwnID, Qt::DotLine);
  int m_idPropMaxCredit = chartWidget->params()->registerProperties(propSetMaxCredit);

  KBalanceChartDlgLayout->addWidget(chartWidget, 10);


  // add another row for markers if required or remove it if not necessary
  // see http://www.klaralvdalens-datakonsult.se/kdchart/ProgrammersManual/KDChart.pdf
  // Chapter 6, "Adding separate Lines/Markers".

  bool needRow = false;
  bool haveMinBalance = false;
  bool haveMaxCredit = false;
  MyMoneyMoney minBalance, maxCredit;
  MyMoneyMoney factor(1,1);
  if(account.accountGroup() == MyMoneyAccount::Asset)
    factor = -factor;

  if(account.value("maxCreditEarly").length() > 0) {
    needRow = true;
    haveMaxCredit = true;
    maxCredit = MyMoneyMoney(account.value("maxCreditEarly")) * factor;
  }
  if(account.value("maxCreditAbsolute").length() > 0) {
    needRow = true;
    haveMaxCredit = true;
    maxCredit = MyMoneyMoney(account.value("maxCreditAbsolute")) * factor;
  }

  if(account.value("minBalanceEarly").length() > 0) {
    needRow = true;
    haveMinBalance = true;
    minBalance = MyMoneyMoney(account.value("minBalanceEarly"));
  }
  if(account.value("minBalanceAbsolute").length() > 0) {
    needRow = true;
    haveMinBalance = true;
    minBalance = MyMoneyMoney(account.value("minBalanceAbsolute"));
  }

  KDChartTableDataBase* data = chartWidget->data();
  if(!needRow && data->usedRows() == 2) {
    data->expand( data->usedRows()-1, data->usedCols() );
  } else if(needRow && data->usedRows() == 1) {
    data->expand( data->usedRows()+1, data->usedCols() );
  }

  if(needRow) {
    if(haveMinBalance) {
      data->setCell(1, 0, minBalance.toDouble());
      chartWidget->setProperty(1, 0, m_idPropMinBalance);
    }
    if(haveMaxCredit) {
      data->setCell(1, 1, maxCredit.toDouble());
      chartWidget->setProperty(1, 1, m_idPropMaxCredit);
    }
  }

  for(int iCell = 90; iCell < 180; ++iCell) {
    chartWidget->setProperty(0, iCell, m_idPropFutureValue);
  }
  chartWidget->setProperty(0, 90, m_idPropLastValue);



  TQFrame* line1 = new TQFrame( this, "line1" );
  line1->setFrameShape( TQFrame::HLine );
  line1->setFrameShadow( TQFrame::Sunken );
  line1->setFrameShape( TQFrame::HLine );

  KBalanceChartDlgLayout->addWidget(line1);
  TQHBoxLayout* Layout1 = new TQHBoxLayout( KBalanceChartDlgLayout, 6, "Layout1");
#if 0
  KPushButton* buttonHelp = new KPushButton( this, "buttonHelp" );
  buttonHelp->setAutoDefault( TRUE );
  buttonHelp->setText(i18n("&Help"));
  Layout1->addWidget( buttonHelp );
#endif

  TQSpacerItem*Horizontal_Spacing2 = new TQSpacerItem( 20, 20, TQSizePolicy::Expanding, TQSizePolicy::Minimum );
  Layout1->addItem(Horizontal_Spacing2 );

#if 0
  KPushButton* buttonOk = new KPushButton( this, "buttonOk" );
  buttonOk->setAutoDefault( TRUE );
  buttonOk->setDefault( TRUE );
  buttonOk->setText(i18n("&OK"));
  Layout1->addWidget( buttonOk );
#endif
  KPushButton* buttonClose = new KPushButton( this, "buttonClose" );
  buttonClose->setEnabled( TRUE );
  buttonClose->setAutoDefault( TRUE );
  buttonClose->setGuiItem(KStdGuiItem::Close);
  Layout1->addWidget( buttonClose );

  // connect( buttonOk, TQT_SIGNAL( clicked() ), this, TQT_SLOT( accept() ) );
  connect( buttonClose, TQT_SIGNAL( clicked() ), this, TQT_SLOT( accept() ) );

  resize( TQSize(700, 500).expandedTo(minimumSizeHint()) );
  clearWState( WState_Polished );
#endif
}


KBalanceChartDlg::~KBalanceChartDlg()
{
}

#include "kbalancechartdlg.moc"