kplato

kptrequestresourcespanel.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 - 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;
7 version 2 of the License.
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 "kptrequestresourcespanel.h"
21#include "kpttask.h"
22#include "kptproject.h"
23#include "kptresource.h"
24#include "kptcalendar.h"
25
26#include <kdebug.h>
27#include <tdemessagebox.h>
28#include <tdelocale.h>
29#include <kptcommand.h>
30
31#include <tqlistbox.h>
32#include <tqpushbutton.h>
33#include <tqlineedit.h>
34#include <tqstring.h>
35#include <tqspinbox.h>
36#include <tqvalidator.h>
37#include <tqcombobox.h>
38#include <tqdatetime.h>
39#include <tqdatetimeedit.h>
40
41namespace KPlato
42{
43
44ResourceTableItem::ResourceTableItem(Resource *resource, ResourceRequest *request, bool check) {
45 m_resource = resource;
46 m_request = request;
47 m_checked = check;
48 m_origChecked = check;
49 m_checkitem = 0;
50 m_units = 100;
51 m_origUnits = 100;
52 //kdDebug()<<k_funcinfo<<"Added: '"<<resource->name()<<"' checked="<<m_checked<<endl;
53}
54
55ResourceTableItem::~ResourceTableItem() {
56 //kdDebug()<<k_funcinfo<<m_resource->name()<<endl;
57}
58
59void ResourceTableItem::update() {
60 if (m_checkitem)
61 m_checked = m_checkitem->isChecked();
62 //kdDebug()<<k_funcinfo<<m_resource->name()<<" checked="<<m_checked<<endl;
63}
64
65void ResourceTableItem::insert(TQTable *table, int row) {
66 //kdDebug()<<k_funcinfo<<endl;
67 m_checkitem = new TQCheckTableItem(table, m_resource->name());
68 m_checkitem->setChecked(m_checked);
69 table->setItem(row, 0, m_checkitem);
70
71 //kdDebug()<<k_funcinfo<<"Added: '"<<m_resource->name()<<"' checked="<<m_checked<<endl;
72}
73
74GroupLVItem::GroupLVItem(TQListView *parent, ResourceGroup *group, Task &task)
75 : TQListViewItem(parent, group->name(), TQString("%1").arg(group->units())),
76 m_group(group),
77 m_units(0)
78{
79
80 m_request = task.resourceGroupRequest(group);
81 if (m_request) {
82 m_units = m_request->units();
83 }
84 TQPtrListIterator<Resource> it(group->resources());
85 for (; it.current(); ++it) {
86 //kdDebug()<<k_funcinfo<<"resource="<<it.current()->name()<<endl;
87 ResourceRequest *req=0;
88 if (m_request) {
89 req = m_request->find(it.current());
90 }
91 m_resources.append(new ResourceTableItem(it.current(), req, (bool)req));
92 }
93
94 m_resources.setAutoDelete(true);
95}
96
97GroupLVItem::~GroupLVItem() {
98 //kdDebug()<<k_funcinfo<<m_group->name()<<endl;
99}
100
101void GroupLVItem::update() {
102 TQPtrListIterator<ResourceTableItem> it(m_resources);
103 for (; it.current(); ++it) {
104 it.current()->update();
105 }
106}
107
108void GroupLVItem::insert(TQTable *table) {
109
110 // clear the table, must be a better way!
111 for (int i = table->numRows(); i > 0; --i)
112 table->removeRow(i-1);
113
114 if (m_group->numResources() == 0) {
115 table->setNumRows(1);
116 table->setItem(0, 0, new TQCheckTableItem(table,i18n("None")));
117 table->setItem(0, 1, new TQComboTableItem(table,i18n("None")));
118 } else {
119 table->setNumRows(m_group->numResources());
120 TQPtrListIterator<ResourceTableItem> it(m_resources);
121 for (int i = 0; it.current(); ++it, ++i) {
122 it.current()->insert(table, i);
123 }
124 }
125 table->adjustColumn(0);
126}
127
128int GroupLVItem::numRequests() {
129 //kdDebug()<<k_funcinfo<<endl;
130 int value = m_units;
131 TQPtrListIterator<ResourceTableItem> it(m_resources);
132 for (; it.current(); ++it) {
133 value += it.current()->numRequests();
134 }
135 return value;
136}
137
138bool GroupLVItem::isNull() const {
139 //kdDebug()<<k_funcinfo<<endl;
140 TQPtrListIterator<ResourceTableItem> it(m_resources);
141 for (; it.current(); ++it) {
142 if (it.current()->isChecked())
143 return false;
144 }
145 if (m_units > 0)
146 return false;
147 return true;
148}
149
150RequestResourcesPanel::RequestResourcesPanel(TQWidget *parent, Task &task, bool baseline)
151 : TaskResourcesPanelBase(parent),
152 m_task(task),
153 m_worktime(0),
154 selectedGroup(0),
155 m_blockChanged(false) {
156
157 Project *p = dynamic_cast<Project*>(task.projectNode());
158 if (p) {
159 m_worktime = p->standardWorktime();
160
161 TQPtrListIterator<ResourceGroup> git(p->resourceGroups());
162 for(int i=0; git.current(); ++git, ++i) {
163 ResourceGroup *grp = git.current();
164 GroupLVItem *grpitem = new GroupLVItem(groupList, grp, task);
165 groupList->insertItem(grpitem);
166 //kdDebug()<<k_funcinfo<<" Added group: "<<grp->name()<<endl;
167 }
168 }
169 TQListViewItem *item = groupList->firstChild();
170 if (item) {
171 groupList->setSelected(item, true);
172 groupChanged(item);
173 }
174
175 resourceTable->setReadOnly(baseline);
176
177 connect(groupList, TQ_SIGNAL(selectionChanged(TQListViewItem*)), TQ_SLOT(groupChanged(TQListViewItem*)));
178 connect(resourceTable, TQ_SIGNAL(valueChanged(int, int)), TQ_SLOT(resourceChanged(int, int)));
179// connect(numUnits, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(unitsChanged(int)));
180
181}
182
183void RequestResourcesPanel::groupChanged(TQListViewItem *item) {
184 //kdDebug()<<k_funcinfo<<endl;
185 GroupLVItem *grp = dynamic_cast<GroupLVItem *>(item);
186 if (grp == 0)
187 return;
188
189 if (selectedGroup) {
190 selectedGroup->update();
191 }
192 selectedGroup = grp;
193
194/* m_blockChanged = true;
195 numUnits->setMaxValue(grp->m_group->units());
196 numUnits->setValue(grp->m_units);
197 m_blockChanged = false;*/
198 grp->insert(resourceTable);
199}
200
201void RequestResourcesPanel::resourceChanged(int /*r*/, int /*c*/) {
202 //kdDebug()<<k_funcinfo<<"("<<r<<","<<c<<")"<<endl;
203 sendChanged();
204}
205
206void RequestResourcesPanel::unitsChanged(int units) {
207 //kdDebug()<<k_funcinfo<<endl;
208 if (selectedGroup) {
209 selectedGroup->m_units = units;
210 sendChanged();
211 }
212}
213
214KCommand *RequestResourcesPanel::buildCommand(Part *part) {
215 //kdDebug()<<k_funcinfo<<endl;
216 KMacroCommand *cmd = 0;
217 if (selectedGroup) {
218 selectedGroup->update();
219 }
220 TQListViewItem *item = groupList->firstChild();
221 for (; item; item = item->nextSibling()) {
222 GroupLVItem *grp = static_cast<GroupLVItem*>(item);
223 TQPtrListIterator<ResourceTableItem> it = grp->resources();
224 for (; it.current(); ++it) {
225 if (it.current()->isChecked() != it.current()->isOrigChecked()) {
226 if (!cmd) cmd = new KMacroCommand("");
227 if (it.current()->isChecked()) {
228 if (!grp->m_request) {
229 grp->m_request = new ResourceGroupRequest(grp->m_group, grp->m_units);
230 cmd->addCommand(new AddResourceGroupRequestCmd(part, m_task, grp->m_request));
231 }
232 cmd->addCommand(new AddResourceRequestCmd(part, grp->m_request, new ResourceRequest(it.current()->resource(), it.current()->units())));
233
234 continue;
235 }
236 if (grp->m_request && it.current()->request()) {
237 cmd->addCommand(new RemoveResourceRequestCmd(part, grp->m_request, it.current()->request()));
238 if (grp->isNull()) {
239 cmd->addCommand(new RemoveResourceGroupRequestCmd(part, m_task, grp->m_request));
240 }
241 } else {
242 kdError()<<k_funcinfo<<"Remove failed"<<endl;
243 }
244 continue;
245 }
246 if (!it.current()->isChecked()) {
247 continue;
248 }
249 }
250 }
251 return cmd;
252}
253
254bool RequestResourcesPanel::ok() {
255 if (selectedGroup)
256 selectedGroup->update();
257 return true;
258}
259
260void RequestResourcesPanel::sendChanged() {
261 if (!m_blockChanged) emit changed();
262}
263
264} //KPlato namespace
265
266#include "kptrequestresourcespanel.moc"