kplato

kptresourceappointmentsview.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Dag Andersen <danders@get2net.dk>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kptresourceappointmentsview.h"
00021 
00022 #include "kptappointment.h"
00023 #include "kptcalendar.h"
00024 #include "kptnode.h"
00025 #include "kptresource.h"
00026 #include "kptview.h"
00027 
00028 #include <qapplication.h>
00029 #include <kcalendarsystem.h>
00030 #include <kglobal.h>
00031 #include <klocale.h>
00032 
00033 #include <qdatetime.h>
00034 #include <qheader.h>
00035 
00036 namespace KPlato
00037 {
00038 
00039 ResourceAppointmentsView::NodeItem::NodeItem(Node *t, QListView *parent, bool highlight)
00040     : DoubleListViewBase::MasterListItem(parent, t->name(), highlight),
00041       node(t) {
00042       
00043       setFormat(0, 'f', 1);
00044     //kdDebug()<<k_funcinfo<<endl;
00045 }
00046 ResourceAppointmentsView::NodeItem::NodeItem(Node *t, QListViewItem *p, bool highlight)
00047     : DoubleListViewBase::MasterListItem(p, t->name(), highlight),
00048       node(t) {
00049       
00050       setFormat(0, 'f', 1);
00051     //kdDebug()<<k_funcinfo<<endl;
00052 }
00053 
00054 ResourceAppointmentsView::NodeItem::NodeItem(QString text, QListViewItem *parent, bool highlight)
00055     : DoubleListViewBase::MasterListItem(parent, text, highlight),
00056       node(0) {
00057       
00058       setFormat(0, 'f', 1);
00059     //kdDebug()<<k_funcinfo<<endl;
00060 }
00061 
00062 ResourceAppointmentsView::NodeItem::NodeItem(QString text, QListView *parent, bool highlight)
00063     : DoubleListViewBase::MasterListItem(parent, text, highlight),
00064       node(0) {
00065       
00066       setFormat(0, 'f', 1);
00067     //kdDebug()<<k_funcinfo<<endl;
00068 }
00069 
00070 //-------------------------------------------
00071 ResourceAppointmentsView::ResourceAppointmentsView(View *view, QWidget *parent)
00072     : DoubleListViewBase(parent),
00073       m_mainview(view),
00074       m_resource(0),
00075       m_availItem(0),
00076       m_totalItem(0) {
00077     
00078     setNameHeader(i18n("Task"));
00079     
00080     
00081     QValueList<int> list = sizes();
00082     int tot = list[0] + list[1];
00083     list[0] = QMIN(35, tot);
00084     list[1] = tot-list[0];
00085     setSizes(list);
00086 }
00087 
00088 void ResourceAppointmentsView::zoom(double zoom) {
00089     Q_UNUSED(zoom);
00090 }
00091 
00092 
00093 void ResourceAppointmentsView::draw(Resource *resource, const QDate &start, const QDate &end) {
00094     m_resource = resource;
00095     m_start = start;
00096     m_end = end;
00097     draw();
00098 }
00099 
00100 void ResourceAppointmentsView::draw() {
00101     //kdDebug()<<k_funcinfo<<endl;
00102     clear();
00103     if (!m_resource)
00104         return;
00105     
00106     m_totalItem = new ResourceAppointmentsView::NodeItem(i18n("Total"), masterListView());
00107     m_totalItem->setExpandable(true);
00108     m_totalItem->setOpen(true);
00109     m_availItem = new ResourceAppointmentsView::NodeItem(i18n("Available"), masterListView());
00110     QPtrList<Appointment> lst = m_resource->appointments();
00111     //kdDebug()<<k_funcinfo<<lst.count()<<endl;
00112     QPtrListIterator<Appointment> it(lst);
00113     for (; it.current(); ++it) {
00114         //kdDebug()<<k_funcinfo<<endl;
00115         Node *n = it.current()->node()->node();
00116         ResourceAppointmentsView::NodeItem *item = new ResourceAppointmentsView::NodeItem(n, m_totalItem);
00117         
00118         item->effortMap = it.current()->plannedPrDay(m_start, m_end);
00119     }
00120     slotUpdate();
00121 }
00122 
00123 void ResourceAppointmentsView::slotUpdate() {
00124     //kdDebug()<<k_funcinfo<<endl;
00125     if (!m_resource)
00126         return;
00127     QApplication::setOverrideCursor(Qt::waitCursor);
00128     createSlaveItems();
00129     KLocale *locale = KGlobal::locale();
00130     const KCalendarSystem *cal = locale->calendar();
00131     const Calendar *resCal = m_resource->calendar();
00132     const QDateTime availFrom = m_resource->availableFrom();
00133     const QDateTime availUntil = m_resource->availableUntil();
00134     // Add columns for selected period/periods
00135     //kdDebug()<<k_funcinfo<<start.toString()<<" - "<<end.toString()<<endl;
00136     int c=0;
00137     for (QDate dt = m_start; dt <= m_end; dt = cal->addDays(dt, 1), ++c) {
00138         QString df = locale->formatDate(dt, true);
00139         addSlaveColumn(df);
00140     }
00141     if (m_totalItem) {
00142         m_totalItem->setHighlight(true);
00143         m_totalItem->setSlaveHighlight(true);
00144     }
00145     QListViewItemIterator it(masterListView());
00146     for (;it.current(); ++it) {
00147         ResourceAppointmentsView::NodeItem *item = static_cast<ResourceAppointmentsView::NodeItem*>(it.current());
00148         if (!item || item->firstChild()) {
00149             continue;
00150         }
00151         double eff;
00152         double avail;
00153         int col=0;
00154         for (QDate d=m_start; d <= m_end; d = cal->addDays(d, 1), ++col) {
00155             if (item == m_availItem && resCal) {
00156                 QDateTime f(d);
00157                 QDateTime u(d, QTime(23, 59, 59, 999));
00158                 if (f >= availUntil || u <= availFrom) {
00159                     avail = 0.0;
00160                 } else {
00161                     if (availFrom > f) {
00162                         f = availFrom;
00163                     }
00164                     if (availUntil < u) {
00165                         u = availUntil;
00166                     }
00167                     avail = ((double)(resCal->effort(f.date(), f.time(), u.time())*(double)(m_resource->units())/100.0).minutes()/60.0);
00168                 }
00169                 m_availItem->setSlaveItem(col, avail);
00170                 m_availItem->addToTotal(avail);
00171                 if (m_totalItem) {
00172                     m_totalItem->setSlaveLimit(col, avail);
00173                 }
00174             }
00175             if (item != m_availItem) {
00176                 eff = (double)(item->effortMap.effortOnDate(d).minutes())/60.0;
00177                 item->setSlaveItem(col, eff);
00178                 item->addToTotal(eff);
00179             }
00180         }
00181     }
00182     if (m_totalItem && m_availItem) {
00183         m_totalItem->setLimit(m_availItem->value());
00184         //kdDebug()<<k_funcinfo<<"avail="<<m_availItem->value()<<endl;
00185     }
00186     calculate();
00187     QApplication::restoreOverrideCursor();
00188 }
00189 
00190 
00191 void ResourceAppointmentsView::print(KPrinter &/*printer*/) {
00192     kdDebug()<<k_funcinfo<<endl;
00193 }
00194 
00195 // bool ResourceAppointmentsView::setContext(Context::ResourceAppointmentsView &context) {
00196 //     //kdDebug()<<k_funcinfo<<endl;
00197 //     
00198 //     QValueList<int> list;
00199 //     list << context.accountsviewsize << context.periodviewsize;
00200 //     m_dlv->setSizes(list);
00201 //     m_date = context.date;
00202 //     if (!m_date.isValid())
00203 //         m_date = QDate::currentDate();
00204 //     m_period = context.period;
00205 //     m_cumulative = context.cumulative;
00206 //     
00207 //     return true;
00208 // }
00209 // 
00210 // void ResourceAppointmentsView::getContext(Context::ResourceAppointmentsView &context) const {
00211 //     //kdDebug()<<k_funcinfo<<endl;
00212 //     context.accountsviewsize = m_dlv->sizes()[0];
00213 //     context.periodviewsize = m_dlv->sizes()[1];
00214 //     context.date = m_date;
00215 //     context.period = m_period;
00216 //     context.cumulative = m_cumulative;
00217 //     //kdDebug()<<k_funcinfo<<"sizes="<<sizes()[0]<<","<<sizes()[1]<<endl;
00218 // }
00219 
00220 void ResourceAppointmentsView::clear() {
00221     clearLists();
00222     m_availItem = 0;
00223     m_totalItem = 0;
00224 }
00225 
00226 void ResourceAppointmentsView::createSlaveItems() {
00227     DoubleListViewBase::createSlaveItems();
00228     setSlaveFormat(0, 'f', 1);
00229 }
00230 
00231 }  //KPlato namespace
00232 
00233 #include "kptresourceappointmentsview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys