kplato

kptresourceappointmentsview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kptresourceappointmentsview.h"
21
22#include "kptappointment.h"
23#include "kptcalendar.h"
24#include "kptnode.h"
25#include "kptresource.h"
26#include "kptview.h"
27
28#include <tqapplication.h>
29#include <kcalendarsystem.h>
30#include <tdeglobal.h>
31#include <tdelocale.h>
32
33#include <tqdatetime.h>
34#include <tqheader.h>
35
36namespace KPlato
37{
38
39ResourceAppointmentsView::NodeItem::NodeItem(Node *t, TQListView *parent, bool highlight)
40 : DoubleListViewBase::MasterListItem(parent, t->name(), highlight),
41 node(t) {
42
43 setFormat(0, 'f', 1);
44 //kdDebug()<<k_funcinfo<<endl;
45}
46ResourceAppointmentsView::NodeItem::NodeItem(Node *t, TQListViewItem *p, bool highlight)
47 : DoubleListViewBase::MasterListItem(p, t->name(), highlight),
48 node(t) {
49
50 setFormat(0, 'f', 1);
51 //kdDebug()<<k_funcinfo<<endl;
52}
53
54ResourceAppointmentsView::NodeItem::NodeItem(TQString text, TQListViewItem *parent, bool highlight)
55 : DoubleListViewBase::MasterListItem(parent, text, highlight),
56 node(0) {
57
58 setFormat(0, 'f', 1);
59 //kdDebug()<<k_funcinfo<<endl;
60}
61
62ResourceAppointmentsView::NodeItem::NodeItem(TQString text, TQListView *parent, bool highlight)
63 : DoubleListViewBase::MasterListItem(parent, text, highlight),
64 node(0) {
65
66 setFormat(0, 'f', 1);
67 //kdDebug()<<k_funcinfo<<endl;
68}
69
70//-------------------------------------------
71ResourceAppointmentsView::ResourceAppointmentsView(View *view, TQWidget *parent)
72 : DoubleListViewBase(parent),
73 m_mainview(view),
74 m_resource(0),
75 m_availItem(0),
76 m_totalItem(0) {
77
78 setNameHeader(i18n("Task"));
79
80
81 TQValueList<int> list = sizes();
82 int tot = list[0] + list[1];
83 list[0] = TQMIN(35, tot);
84 list[1] = tot-list[0];
85 setSizes(list);
86}
87
88void ResourceAppointmentsView::zoom(double zoom) {
89 Q_UNUSED(zoom);
90}
91
92
93void ResourceAppointmentsView::draw(Resource *resource, const TQDate &start, const TQDate &end) {
94 m_resource = resource;
95 m_start = start;
96 m_end = end;
97 draw();
98}
99
100void ResourceAppointmentsView::draw() {
101 //kdDebug()<<k_funcinfo<<endl;
102 clear();
103 if (!m_resource)
104 return;
105
106 m_totalItem = new ResourceAppointmentsView::NodeItem(i18n("Total"), masterListView());
107 m_totalItem->setExpandable(true);
108 m_totalItem->setOpen(true);
109 m_availItem = new ResourceAppointmentsView::NodeItem(i18n("Available"), masterListView());
110 TQPtrList<Appointment> lst = m_resource->appointments();
111 //kdDebug()<<k_funcinfo<<lst.count()<<endl;
112 TQPtrListIterator<Appointment> it(lst);
113 for (; it.current(); ++it) {
114 //kdDebug()<<k_funcinfo<<endl;
115 Node *n = it.current()->node()->node();
116 ResourceAppointmentsView::NodeItem *item = new ResourceAppointmentsView::NodeItem(n, m_totalItem);
117
118 item->effortMap = it.current()->plannedPrDay(m_start, m_end);
119 }
120 slotUpdate();
121}
122
123void ResourceAppointmentsView::slotUpdate() {
124 //kdDebug()<<k_funcinfo<<endl;
125 if (!m_resource)
126 return;
127 TQApplication::setOverrideCursor(TQt::waitCursor);
128 createSlaveItems();
129 TDELocale *locale = TDEGlobal::locale();
130 const KCalendarSystem *cal = locale->calendar();
131 const Calendar *resCal = m_resource->calendar();
132 const TQDateTime availFrom = m_resource->availableFrom();
133 const TQDateTime availUntil = m_resource->availableUntil();
134 // Add columns for selected period/periods
135 //kdDebug()<<k_funcinfo<<start.toString()<<" - "<<end.toString()<<endl;
136 int c=0;
137 for (TQDate dt = m_start; dt <= m_end; dt = cal->addDays(dt, 1), ++c) {
138 TQString df = locale->formatDate(dt, true);
139 addSlaveColumn(df);
140 }
141 if (m_totalItem) {
142 m_totalItem->setHighlight(true);
143 m_totalItem->setSlaveHighlight(true);
144 }
145 TQListViewItemIterator it(masterListView());
146 for (;it.current(); ++it) {
147 ResourceAppointmentsView::NodeItem *item = static_cast<ResourceAppointmentsView::NodeItem*>(it.current());
148 if (!item || item->firstChild()) {
149 continue;
150 }
151 double eff;
152 double avail;
153 int col=0;
154 for (TQDate d=m_start; d <= m_end; d = cal->addDays(d, 1), ++col) {
155 if (item == m_availItem && resCal) {
156 TQDateTime f(d);
157 TQDateTime u(d, TQTime(23, 59, 59, 999));
158 if (f >= availUntil || u <= availFrom) {
159 avail = 0.0;
160 } else {
161 if (availFrom > f) {
162 f = availFrom;
163 }
164 if (availUntil < u) {
165 u = availUntil;
166 }
167 avail = ((double)(resCal->effort(f.date(), f.time(), u.time())*(double)(m_resource->units())/100.0).minutes()/60.0);
168 }
169 m_availItem->setSlaveItem(col, avail);
170 m_availItem->addToTotal(avail);
171 if (m_totalItem) {
172 m_totalItem->setSlaveLimit(col, avail);
173 }
174 }
175 if (item != m_availItem) {
176 eff = (double)(item->effortMap.effortOnDate(d).minutes())/60.0;
177 item->setSlaveItem(col, eff);
178 item->addToTotal(eff);
179 }
180 }
181 }
182 if (m_totalItem && m_availItem) {
183 m_totalItem->setLimit(m_availItem->value());
184 //kdDebug()<<k_funcinfo<<"avail="<<m_availItem->value()<<endl;
185 }
186 calculate();
187 TQApplication::restoreOverrideCursor();
188}
189
190
191void ResourceAppointmentsView::print(KPrinter &/*printer*/) {
192 kdDebug()<<k_funcinfo<<endl;
193}
194
195// bool ResourceAppointmentsView::setContext(Context::ResourceAppointmentsView &context) {
196// //kdDebug()<<k_funcinfo<<endl;
197//
198// TQValueList<int> list;
199// list << context.accountsviewsize << context.periodviewsize;
200// m_dlv->setSizes(list);
201// m_date = context.date;
202// if (!m_date.isValid())
203// m_date = TQDate::currentDate();
204// m_period = context.period;
205// m_cumulative = context.cumulative;
206//
207// return true;
208// }
209//
210// void ResourceAppointmentsView::getContext(Context::ResourceAppointmentsView &context) const {
211// //kdDebug()<<k_funcinfo<<endl;
212// context.accountsviewsize = m_dlv->sizes()[0];
213// context.periodviewsize = m_dlv->sizes()[1];
214// context.date = m_date;
215// context.period = m_period;
216// context.cumulative = m_cumulative;
217// //kdDebug()<<k_funcinfo<<"sizes="<<sizes()[0]<<","<<sizes()[1]<<endl;
218// }
219
220void ResourceAppointmentsView::clear() {
221 clearLists();
222 m_availItem = 0;
223 m_totalItem = 0;
224}
225
226void ResourceAppointmentsView::createSlaveItems() {
227 DoubleListViewBase::createSlaveItems();
228 setSlaveFormat(0, 'f', 1);
229}
230
231} //KPlato namespace
232
233#include "kptresourceappointmentsview.moc"