20#include "kptaccountsview.h"
22#include "kptaccountsviewconfigdialog.h"
23#include "kptcontext.h"
24#include "kptdatetime.h"
25#include "kptproject.h"
27#include "kpteffortcostmap.h"
29#include <tqapplication.h>
30#include <tqcombobox.h>
31#include <tqdatetime.h>
32#include <tqdatetimeedit.h>
38#include <tqpushbutton.h>
39#include <tqvaluelist.h>
40#include <tqpopupmenu.h>
41#include <tqsizepolicy.h>
43#include <tqpaintdevicemetrics.h>
45#include <kcalendarsystem.h>
47#include <tdelistview.h>
56class Label :
public TQLabel
62 void paintContents(TQPainter *p) {
67AccountsView::AccountItem::AccountItem(
Account *a, TQListView *parent,
bool highlight)
70 if (parent->columns() >= 3) {
71 setText(2, a->description());
75AccountsView::AccountItem::AccountItem(Account *a, TQListViewItem *p,
bool highlight)
76 : DoubleListViewBase::MasterListItem(p, a->name(), highlight),
78 if (listView() && listView()->columns() >= 3) {
79 setText(2, a->description());
84AccountsView::AccountItem::AccountItem(TQString text, Account *a, TQListViewItem *parent,
bool highlight)
85 : DoubleListViewBase::MasterListItem(parent, text, highlight),
90void AccountsView::AccountItem::add(
int col,
const TQDate &date,
const EffortCost &ec) {
91 EffortCost &cm = costMap.add(date, ec);
93 m_slaveItem->setText(col, TDEGlobal::locale()->formatMoney(cm.cost(),
"", 0));
96AccountsView::AccountsView(Project &project, View *view, TQWidget *parent)
97 : TQWidget(parent,
"Accounts view"),
100 m_accounts(project.accounts()) {
102 m_date = TQDate::currentDate();
104 m_periodTexts<<i18n(
"Day")<<i18n(
"Week")<<i18n(
"Month");
105 m_cumulative =
false;
107 TQVBoxLayout *lay1 =
new TQVBoxLayout(
this, 0, KDialog::spacingHint());
109 TQHBoxLayout *lay2 =
new TQHBoxLayout(0, 0, KDialog::spacingHint());
110 m_label =
new Label(
this);
111 m_label->setFrameShape(TQLabel::StyledPanel);
112 m_label->setFrameShadow(TQLabel::Sunken);
113 m_label->setAlignment(
int(TQLabel::WordBreak | TQLabel::AlignVCenter));
114 lay2->addWidget(m_label);
115 m_changeBtn =
new TQPushButton(i18n(
"Configure..."),
this);
116 m_changeBtn->setSizePolicy(TQSizePolicy((TQSizePolicy::SizeType)0, (TQSizePolicy::SizeType)0, 0, 0, m_changeBtn->sizePolicy().hasHeightForWidth()));
117 lay2->addWidget(m_changeBtn);
118 lay1->addLayout(lay2);
120 m_dlv =
new DoubleListViewBase(
this,
true);
121 m_dlv->setNameHeader(i18n(
"Account"));
125 lay1->addWidget(m_dlv);
127 connect(
this, TQ_SIGNAL(update()), TQ_SLOT(slotUpdate()));
128 connect(m_changeBtn, TQ_SIGNAL(clicked()), TQ_SLOT(slotConfigure()));
130 TQValueList<int> list = m_dlv->sizes();
131 int tot = list[0] + list[1];
132 list[0] = TQMIN(35, tot);
133 list[1] = tot-list[0];
134 m_dlv->setSizes(list);
137void AccountsView::zoom(
double zoom) {
141void AccountsView::init() {
142 m_date = TQDate::currentDate();
144 initAccList(m_accounts.accountList());
147void AccountsView::draw() {
149 Context::Accountsview context;
150 getContextClosedItems(context, m_dlv->masterListView()->firstChild());
151 initAccList(m_accounts.accountList());
152 setContextClosedItems(context);
156void AccountsView::initAccList(
const AccountList &list) {
158 AccountListIterator it = list;
159 for (it.toLast(); it.current(); --it) {
160 AccountsView::AccountItem *a =
new AccountsView::AccountItem(it.current(), m_dlv->masterListView());
162 a->setExpandable(!it.current()->isElement());
163 initAccSubItems(it.current(), a);
168void AccountsView::initAccSubItems(Account *acc, AccountsView::AccountItem *parent) {
169 if (!acc->accountList().isEmpty()) {
174 initAccList(acc->accountList(), parent);
190void AccountsView::initAccList(
const AccountList &list, AccountsView::AccountItem *parent) {
191 AccountListIterator it = list;
192 for (it.toLast(); it.current(); --it) {
193 AccountsView::AccountItem *a =
new AccountsView::AccountItem(it.current(), parent);
195 a->setExpandable(!it.current()->isElement());
196 initAccSubItems(it.current(), a);
200void AccountsView::clearPeriods() {
201 m_dlv->clearSlaveList();
204void AccountsView::createPeriods() {
205 m_dlv->createSlaveItems();
208void AccountsView::slotUpdate() {
210 TQApplication::setOverrideCursor(TQt::waitCursor);
212 TDELocale *locale = TDEGlobal::locale();
213 const KCalendarSystem *cal = locale->calendar();
217 t +=
" <b>" + i18n(
"Cumulative") +
"</b> ";
219 t += i18n(
"Cut-off date:%1").arg(
"<b>" + locale->formatDate(m_date,
true) +
"</b>");
220 t +=
" " + i18n(
"Periodicity:%1").arg(
"<b>" + periodText(m_period) +
"</b>");
224 TQDate start = m_project.startTime().date();
229 for (TQDate dt = start; dt <= end; dt = cal->addDays(dt, 1), ++c) {
230 TQString df = locale->formatDate(dt,
true);
231 m_dlv->addSlaveColumn(df);
233 TQListViewItemIterator it(m_dlv->masterListView());
234 for (;it.current(); ++it) {
235 AccountsView::AccountItem *item =
dynamic_cast<AccountsView::AccountItem*
>(it.current());
236 if (!item || !item->account || !item->account->isElement()) {
239 item->costMap = m_accounts.plannedCost(*(item->account), start, end);
242 for (TQDate d=start; d <= end; d = cal->addDays(d, 1), ++col) {
243 EffortCost &ec = item->costMap.effortCostOnDate(d);
244 cost = (m_cumulative ? cost + ec.cost() : ec.cost());
245 item->setSlaveItem(col, cost);
246 m_cumulative ? item->setTotal(cost) : item->addToTotal(cost);
250 TQApplication::restoreOverrideCursor();
255 int weekStartDay = locale->weekStartDay();
258 TQDate pend = cal->addDays(dt, 7 + weekStartDay - 1 - cal->dayOfWeek(dt));
259 for (; pend <= end; ++c) {
262 int w = cal->weekNumber(dt, &y);
263 TQString t = i18n(
"<week>-<year>",
"%1-%2").arg(w).arg(y);
264 m_dlv->addSlaveColumn(t);
265 dt = pend.addDays(1);
266 pend = cal->addDays(pend, 7);
267 if ((pend.year() == end.year()) && (pend.weekNumber() == end.weekNumber())) {
272 TQApplication::restoreOverrideCursor();
275 TQListViewItemIterator it(m_dlv->masterListView());
276 for (;it.current(); ++it) {
277 AccountsView::AccountItem *item =
dynamic_cast<AccountsView::AccountItem*
>(it.current());
278 if (!item || !item->account || !item->account->isElement()) {
281 item->costMap = m_accounts.plannedCost(*(item->account), start, end);
284 TQDate pend = cal->addDays(d, 7 + weekStartDay - 1 - cal->dayOfWeek(d));
285 for (
int col=0; pend <= end; ++col) {
286 double cst = item->costMap.cost(d, d.daysTo(pend)+1);
287 cost = (m_cumulative ? cost + cst : cst);
288 item->setSlaveItem(col, cost);
289 m_cumulative ? item->setTotal(cost) : item->addToTotal(cost);
291 pend = cal->addDays(pend, 7);
292 if ((pend.year() == end.year()) && (pend.weekNumber() == end.weekNumber())) {
298 TQApplication::restoreOverrideCursor();
305 cal->setYMD(pend, dt.year(), dt.month(), dt.daysInMonth());
306 for (; pend <= end; ++c) {
308 TQString m = cal->monthName(dt,
true) + TQString(
" %1").arg( dt.year());
309 m_dlv->addSlaveColumn(m);
311 dt = pend.addDays(1);
312 pend = cal->addDays(pend, dt.daysInMonth());
313 if ((pend.year() == end.year()) && (pend.month() == end.month())) {
318 TQApplication::restoreOverrideCursor();
321 TQListViewItemIterator it(m_dlv->masterListView());
322 for (;it.current(); ++it) {
323 AccountsView::AccountItem *item =
dynamic_cast<AccountsView::AccountItem*
>(it.current());
324 if (!item || !item->account || !item->account->isElement()) {
327 item->costMap = m_accounts.plannedCost(*(item->account), start, end);
330 cal->setYMD(pend, d.year(), d.month(), d.daysInMonth());
331 for (
int col=0; pend <= end; ++col) {
332 double cst = item->costMap.cost(d, d.daysTo(pend)+1);
333 cost = (m_cumulative ? cost + cst : cst);
334 item->setSlaveItem(col, cost);
335 m_cumulative ? item->setTotal(cost) : item->addToTotal(cost);
337 pend = cal->addDays(pend, d.daysInMonth());
338 if ((pend.year() == end.year()) && (pend.month() == end.month())) {
344 TQApplication::restoreOverrideCursor();
347 TQApplication::restoreOverrideCursor();
350void AccountsView::print(KPrinter &printer) {
352 TQPaintDeviceMetrics m = TQPaintDeviceMetrics ( &printer );
353 uint top, left, bottom, right;
354 printer.margins(&top, &left, &bottom, &right);
358 p.setViewport(left, top, m.width()-left-right, m.height()-top-bottom);
359 p.setClipRect(left, top, m.width()-left-right, m.height()-top-bottom);
360 TQRect preg = p.clipRegion(TQPainter::CoordPainter).boundingRect();
363 double scale = TQMIN((
double)preg.width()/(
double)size().width(), (
double)preg.height()/(
double)(size().height()));
366 p.scale(scale, scale);
368 m_label->paintContents(&p);
369 p.translate(0, m_label->size().height());
370 m_dlv->paintContents(&p);
374bool AccountsView::setContext(Context::Accountsview &context) {
376 TQValueList<int> list;
377 list << context.accountsviewsize << context.periodviewsize;
379 m_date = context.date;
380 if (!m_date.isValid())
381 m_date = TQDate::currentDate();
382 m_period = context.period;
383 m_cumulative = context.cumulative;
384 setContextClosedItems(context);
389void AccountsView::setContextClosedItems(Context::Accountsview &context) {
390 for (TQStringList::ConstIterator it = context.closedItems.begin(); it != context.closedItems.end(); ++it) {
391 if (m_accounts.findAccount(*it)) {
392 TQListViewItemIterator lit(m_dlv->masterListView());
393 for (; lit.current(); ++lit) {
394 if (lit.current()->text(0) == (*it)) {
395 m_dlv->setOpen(lit.current(),
false);
403void AccountsView::getContext(Context::Accountsview &context)
const {
405 context.accountsviewsize = m_dlv->sizes()[0];
406 context.periodviewsize = m_dlv->sizes()[1];
407 context.date = m_date;
408 context.period = m_period;
409 context.cumulative = m_cumulative;
412 getContextClosedItems(context, m_dlv->masterListView()->firstChild());
416void AccountsView::getContextClosedItems(Context::Accountsview &context, TQListViewItem *item)
const {
419 for (TQListViewItem *i = item; i; i = i->nextSibling()) {
421 context.closedItems.append(i->text(0));
424 getContextClosedItems(context, i->firstChild());
428void AccountsView::slotConfigure() {
430 AccountsviewConfigDialog *dia =
new AccountsviewConfigDialog(m_date, m_period, m_periodTexts, m_cumulative,
this);
432 m_date = dia->date();
433 m_period = dia->period();
434 m_cumulative = dia->isCumulative();
440TQString AccountsView::periodText(
int offset) {
442 TQStringList::const_iterator it = m_periodTexts.at(offset);
443 if (it != m_periodTexts.constEnd()) {
451#include "kptaccountsview.moc"
Account holds one account.
The class DoubleListViewBase provides a double listview where the right listview (the slave) containe...