/* This file is part of the KDE project Copyright (C) 2005 Dag Andersen kplato@kde.org> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; version 2 of the License. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kptdoublelistviewbase.h" #include "kptproject.h" #include "kptview.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace KPlato { void ListView::paintToPrinter(TQPainter * p, int cx, int cy, int cw, int ch) { //kdDebug()<save(); TQRegion r = p->clipRegion(TQPainter::CoordPainter); p->setClipRegion(r.intersect(TQRegion(cx, 0, cw, ch)), TQPainter::CoordPainter); TQColor bgc(193, 223, 255); TQBrush bg(bgc); p->setBackgroundMode(TQt::OpaqueMode); p->setBackgroundColor(bgc); TQHeader *h = header(); int hei = 0; for (int s = 0; s < h->count(); ++s) { TQRect r = h->sectionRect(s); //kdDebug()<label(s)<<" "<label(s)<<" "<= cx+cw) { //kdDebug()<label(s)<<" "<label(s)<<" "<eraseRect(tr); p->drawText(tr, columnAlignment(s)|TQt::AlignVCenter, h->label(s), -1); hei = TQMAX(tr.height(), hei); } r = p->clipRegion(TQPainter::CoordPainter); p->restore(); // p->drawRect(r.boundingRect()); p->save(); p->translate(0, hei+2); r = p->clipRegion(TQPainter::CoordPainter); // FIXME: Doesn't clip correctly, haven't figured out why p->setClipRegion(r.intersect(TQRegion(cx, cy, cw, ch)), TQPainter::CoordPainter); drawContentsOffset(p, 0, 0, cx, cy, cw, ch); // p->drawRect(r.boundingRect()); p->restore(); } DoubleListViewBase::SlaveListItem::SlaveListItem(DoubleListViewBase::MasterListItem *master, TQListView *parent, TQListViewItem *after, bool highlight) : TDEListViewItem(parent, after), m_masterItem(master), m_value(0.0), m_highlight(highlight), m_valueMap() { setFormat(); setExpandable(master->isExpandable()); setOpen(master->isOpen()); //kdDebug()<<"DoubleListViewBase::SlaveListItem "<text(0)<<" parent="<(parent)->m_masterItem->text(0)<isExpandable()); setOpen(master->isOpen()); //kdDebug()<<"DoubleListViewBase::SlaveListItem "<text(0)<<" parent="<(parent)->m_masterItem->text(0)<slaveItemDeleted(); } void DoubleListViewBase::SlaveListItem::clearColumn(int col) { if (col >= listView()->columns()) { return; } listView()->setColumnText(col, ""); setText(col, ""); m_valueMap[col] = 0; } void DoubleListViewBase::SlaveListItem::setColumn(int col, double value) { if (col < listView()->columns()) { //setText(col, TQString("%1").arg(value, m_fieldwidth, m_fmt, m_prec)); setText(col, TDEGlobal::locale()->formatNumber(value, m_prec)); m_valueMap.replace(col, value); //kdDebug()<text(0)<<": column["<(parent())->m_slaveItem, after); } } DoubleListViewBase::SlaveListItem *prev = 0; for (TQListViewItem *item = firstChild(); item; item = item->nextSibling()) { static_cast(item)->createSlaveItems(lv, prev); prev = static_cast(item)->m_slaveItem; } } void DoubleListViewBase::MasterListItem::setSlaveOpen(bool on) { if (m_slaveItem) m_slaveItem->setOpen(on); } void DoubleListViewBase::MasterListItem::slaveItemDeleted() { setTotal(0); m_slaveItem = 0; } void DoubleListViewBase::MasterListItem::setTotal(double tot) { m_value = tot; //setText(1, TQString("%1").arg(tot, m_fieldwidth, m_fmt, m_prec)); setText(1, TDEGlobal::locale()->formatNumber(tot, m_prec)); //kdDebug()<formatNumber(m_value, m_prec)); } double DoubleListViewBase::MasterListItem::calcTotal() { double tot=0.0; TQListViewItem *item=firstChild(); if (!item) { tot = m_value; } else { for (; item; item = item->nextSibling()) { tot += static_cast(item)->calcTotal(); } } setTotal(tot); return tot; } void DoubleListViewBase::MasterListItem::setSlaveItem(int col, double value) { if (m_slaveItem) { m_slaveItem->setColumn(col, value); } } void DoubleListViewBase::MasterListItem::clearColumn(int col) { for (TQListViewItem *item=firstChild(); item; item=item->nextSibling()) { static_cast(item)->clearColumn(col); } setTotal(0); if (m_slaveItem == 0) { kdError()<clearColumn(0); } void DoubleListViewBase::MasterListItem::calcSlaveItems() { if (m_slaveItem == 0 || m_slaveItem->listView() == 0) { kdError()<listView()"<listView()->columns(); for (int i = 0; i < cols; ++i) { calcSlaveItems(i); } } double DoubleListViewBase::MasterListItem::calcSlaveItems(int col) { if (m_slaveItem == 0) return 0.0; TQListViewItem *item=firstChild(); if (!item) { return m_slaveItem->value(col); } double tot=0.0; for (; item; item = item->nextSibling()) { tot += static_cast(item)->calcSlaveItems(col); } //kdDebug()<addColumn(i18n("Total")); m_masterList->setColumnAlignment(1, AlignRight); if (description) { m_masterList->addColumn(i18n("Description")); m_masterList->header()->moveSection(2, 1); m_masterList->header()->setStretchEnabled(true, 1); } else { m_masterList->header()->setStretchEnabled(true, 0); } m_masterList->setVScrollBarMode(TQScrollView::AlwaysOff); m_masterList->setHScrollBarMode(TQScrollView::AlwaysOn); m_slaveList = new ListView(this); m_slaveList->setSelectionMode(TQListView::NoSelection); m_slaveList->setItemMargin(2); m_slaveList->setSortColumn(-1); // Disable sort!! m_slaveList->setTreeStepSize(0); m_slaveList->setHScrollBarMode(TQScrollView::AlwaysOn); connect(m_slaveList->verticalScrollBar(), TQ_SIGNAL(valueChanged(int)), m_masterList->verticalScrollBar(), TQ_SLOT(setValue(int))); connect(m_masterList, TQ_SIGNAL(expanded(TQListViewItem*)), TQ_SLOT(slotExpanded(TQListViewItem*))); connect(m_masterList, TQ_SIGNAL(collapsed(TQListViewItem*)), TQ_SLOT(slotCollapsed(TQListViewItem*))); } TQSize DoubleListViewBase::sizeHint() const { //kdDebug()<columns() > 0) { m_slaveList->removeColumn(0); // removing the last one clears the list!!! } m_slaveList->clear(); // to be safe } void DoubleListViewBase::createSlaveItems() { clearSlaveList(); DoubleListViewBase::SlaveListItem *prev = 0; for (TQListViewItem *item = m_masterList->firstChild(); item; item = item->nextSibling()) { static_cast(item)->createSlaveItems(m_slaveList, prev); prev = static_cast(item)->slaveItem(); } } void DoubleListViewBase::print(KPrinter &printer) { kdDebug()<setOpen(item, open); } void DoubleListViewBase::slotExpanded(TQListViewItem* item) { //kdDebug()<(item)->setSlaveOpen(true); } } void DoubleListViewBase::slotCollapsed(TQListViewItem*item) { //kdDebug()<(item)->setSlaveOpen(false); } } void DoubleListViewBase::setDescriptionHeader(TQString text) { m_masterList->setColumnText(1, text); } void DoubleListViewBase::setNameHeader(TQString text) { m_masterList->setColumnText(0, text); } void DoubleListViewBase::setTotalHeader(TQString text) { m_masterList->setColumnText(2, text); } void DoubleListViewBase::addSlaveColumn(TQString text) { m_slaveList->addColumn(text); m_slaveList->setColumnAlignment(m_slaveList->columns()-1, AlignRight); } void DoubleListViewBase::calculate() { for (TQListViewItem *lvi=m_masterList->firstChild(); lvi; lvi = lvi->nextSibling()) { static_cast(lvi)->calcSlaveItems(); static_cast(lvi)->calcTotal(); } } void DoubleListViewBase::clearLists() { m_slaveList->clear(); m_masterList->clear(); } void DoubleListViewBase::setMasterFormat(int fieldwidth, char fmt, int prec) { TQListViewItemIterator it = m_masterList; for (; it.current(); ++it) { static_cast(it.current())->setFormat(fieldwidth, fmt, prec); } } void DoubleListViewBase::setSlaveFormat(int fieldwidth, char fmt, int prec) { TQListViewItemIterator it = m_slaveList; for (; it.current(); ++it) { static_cast(it.current())->setFormat(fieldwidth, fmt, prec); } } void DoubleListViewBase::setFormat(int fieldwidth, char fmt, int prec) { m_fieldwidth = fieldwidth; m_fmt = fmt; m_prec = prec; setMasterFormat(fieldwidth, fmt, prec); setSlaveFormat(fieldwidth, fmt, prec); } void DoubleListViewBase::paintContents(TQPainter *p) { //kdDebug()<contentsRect(); TQRect cs = m_slaveList->contentsRect(); int mx, my, sx, sy; m_masterList->contentsToViewport(cm.x(), cm.y(), mx, my); m_slaveList->contentsToViewport(cs.x(), cs.y(), sx, sy); if (sizes()[0] > 0) { p->save(); p->translate(mx, my); m_masterList->paintToPrinter(p, -mx, -my, cm.width(), cm.height()); p->restore(); } if (sizes()[1] > 0) { p->save(); p->translate(cm.width() + 8 + sx, sy); m_slaveList->paintToPrinter(p, -sx, -sy, cs.width(), cs.height()); //p->fillRect(-8, 0, 0, sy, TQt::white); p->restore(); } } } //KPlato namespace #include "kptdoublelistviewbase.moc"