kplato

kptresourcedialog.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 "kptresourcedialog.h"
21#include "kptcommand.h"
22#include "kptpart.h"
23#include "kptproject.h"
24#include "kptresource.h"
25#include "kptcalendar.h"
26
27#include <tqpushbutton.h>
28#include <tqlabel.h>
29#include <tqlineedit.h>
30#include <tqcombobox.h>
31#include <tqdatetimeedit.h>
32#include <tqdatetime.h>
33#include <tqbuttongroup.h>
34#include <tqradiobutton.h>
35#include <tqspinbox.h>
36#include <tqptrlist.h>
37#include <tqstringlist.h>
38
39#include <tdeabc/addressee.h>
40#include <tdeabc/addresseedialog.h>
41
42#include <kcommand.h>
43#include <kdatetimewidget.h>
44#include <tdemessagebox.h>
45#include <tdelocale.h>
46#include <tdeglobal.h>
47#include <kdebug.h>
48
49namespace KPlato
50{
51
52ResourceDialogImpl::ResourceDialogImpl (TQWidget *parent)
53 : ResourceDialogBase(parent)
54{
55
56 connect(type, TQ_SIGNAL(activated(int)), TQ_SLOT(slotChanged()));
57 connect(units, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(slotChanged()));
58 connect(nameEdit, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotChanged()));
59 connect(initialsEdit, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotChanged()));
60 connect(emailEdit, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotChanged()));
61
62 connect(calendarList, TQ_SIGNAL(activated(int)), TQ_SLOT(slotChanged()));
63
64 connect(rateEdit, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotChanged()));
65 connect(overtimeEdit, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotChanged()));
66
67 connect(chooseBtn, TQ_SIGNAL(clicked()), TQ_SLOT(slotChooseResource()));
68
69 connect(availableFrom, TQ_SIGNAL(valueChanged(const TQDateTime&)), TQ_SLOT(slotChanged()));
70 connect(availableUntil, TQ_SIGNAL(valueChanged(const TQDateTime&)), TQ_SLOT(slotChanged()));
71 connect(availableFrom, TQ_SIGNAL(valueChanged(const TQDateTime&)), TQ_SLOT(slotAvailableFromChanged(const TQDateTime&)));
72 connect(availableUntil, TQ_SIGNAL(valueChanged(const TQDateTime&)), TQ_SLOT(slotAvailableUntilChanged(const TQDateTime&)));
73}
74
75
76void ResourceDialogImpl::slotChanged() {
77 emit changed();
78}
79
80void ResourceDialogImpl::slotAvailableFromChanged(const TQDateTime&) {
81 if (availableUntil->dateTime() < availableFrom->dateTime()) {
82 disconnect(availableUntil, TQ_SIGNAL(valueChanged(const TQDateTime&)), this, TQ_SLOT(slotAvailableUntilChanged(const TQDateTime&)));
83 //kdDebug()<<"From: "<<availableFrom->dateTime().toString()<<" until="<<availableUntil->dateTime().toString()<<endl;
84 availableUntil->setDateTime(availableFrom->dateTime());
85 connect(availableUntil, TQ_SIGNAL(valueChanged(const TQDateTime&)), TQ_SLOT(slotAvailableUntilChanged(const TQDateTime&)));
86 }
87}
88
89void ResourceDialogImpl::slotAvailableUntilChanged(const TQDateTime&) {
90 if (availableFrom->dateTime() > availableUntil->dateTime()) {
91 disconnect(availableFrom, TQ_SIGNAL(valueChanged(const TQDateTime&)), this, TQ_SLOT(slotAvailableFromChanged(const TQDateTime&)));
92 //kdDebug()<<"Until: "<<availableUntil->dateTime().toString()<<" from="<<availableFrom->dateTime().toString()<<endl;
93 availableFrom->setDateTime(availableUntil->dateTime());
94 connect(availableFrom, TQ_SIGNAL(valueChanged(const TQDateTime&)), TQ_SLOT(slotAvailableFromChanged(const TQDateTime&)));
95 }
96}
97
98void ResourceDialogImpl::slotCalculationNeeded(const TQString&) {
99 emit calculate();
100 emit changed();
101}
102
103void ResourceDialogImpl::slotChooseResource()
104{
105 TDEABC::Addressee a = TDEABC::AddresseeDialog::getAddressee(this);
106 if (!a.isEmpty()) {
107 nameEdit->setText(a.assembledName());
108 emailEdit->setText(a.preferredEmail());
109 TQStringList l = TQStringList::split(' ', a.assembledName());
110 TQString in;
111 TQStringList::Iterator it = l.begin();
112 for (/*int i = 0*/; it != l.end(); ++it) {
113 in += (*it)[0];
114 }
115 initialsEdit->setText(in);
116 }
117}
118
120
121ResourceDialog::ResourceDialog(Project &project, Resource *resource, TQWidget *parent, const char *name)
122 : KDialogBase( Swallow, i18n("Resource Settings"), Ok|Cancel, Ok, parent, name, true, true),
123 m_original(resource),
124 m_resource(resource),
125 m_calculationNeeded(false)
126{
127 dia = new ResourceDialogImpl(this);
128 setMainWidget(dia);
129 enableButtonOK(false);
130
131 dia->nameEdit->setText(resource->name());
132 dia->initialsEdit->setText(resource->initials());
133 dia->emailEdit->setText(resource->email());
134 dia->type->setCurrentItem((int)resource->type()); // NOTE: must match enum
135 dia->units->setValue(resource->units());
136 dia->availableFrom->setDateTime(resource->availableFrom());
137 dia->availableUntil->setDateTime(resource->availableUntil());
138 dia->rateEdit->setText(TDEGlobal::locale()->formatMoney(resource->normalRate()));
139 dia->overtimeEdit->setText(TDEGlobal::locale()->formatMoney(resource->overtimeRate()));
140
141 int cal = 0;
142 dia->calendarList->insertItem(i18n("None"));
143 m_calendars.insert(0, 0);
144 TQPtrList<Calendar> list = project.calendars();
145 TQPtrListIterator<Calendar> cit = list;
146 for(int i=1; cit.current(); ++cit, ++i) {
147 dia->calendarList->insertItem(cit.current()->name(), i);
148 m_calendars.insert(i, cit.current());
149 if (cit.current() == resource->calendar())
150 cal = i;
151 }
152 dia->calendarList->setCurrentItem(cal);
153
154 connect(dia, TQ_SIGNAL(changed()), TQ_SLOT(enableButtonOk()));
155 connect(dia, TQ_SIGNAL(calculate()), TQ_SLOT(slotCalculationNeeded()));
156 connect(dia->calendarList, TQ_SIGNAL(activated(int)), TQ_SLOT(slotCalendarChanged(int)));
157
158}
159
160
161void ResourceDialog::enableButtonOk() {
162 enableButtonOK(true);
163}
164
165void ResourceDialog::slotCalculationNeeded() {
166 m_calculationNeeded = true;
167}
168
169void ResourceDialog::slotOk() {
170 m_resource.setName(dia->nameEdit->text());
171 m_resource.setInitials(dia->initialsEdit->text());
172 m_resource.setEmail(dia->emailEdit->text());
173 m_resource.setType((Resource::Type)(dia->type->currentItem()));
174 m_resource.setUnits(dia->units->value());
175
176 m_resource.setNormalRate(TDEGlobal::locale()->readMoney(dia->rateEdit->text()));
177 m_resource.setOvertimeRate(TDEGlobal::locale()->readMoney(dia->overtimeEdit->text()));
178 m_resource.setCalendar(m_calendars[dia->calendarList->currentItem()]);
179 m_resource.setAvailableFrom(dia->availableFrom->dateTime());
180 m_resource.setAvailableUntil(dia->availableUntil->dateTime());
181 accept();
182}
183
184void ResourceDialog::slotCalendarChanged(int /*cal*/) {
185
186}
187
188KCommand *ResourceDialog::buildCommand(Part *part) {
189 return buildCommand(m_original, m_resource, part);
190}
191
192// static
193KCommand *ResourceDialog::buildCommand(Resource *original, Resource &resource, Part *part) {
194 KMacroCommand *m=0;
195 TQString n = i18n("Modify Resource");
196 if (resource.name() != original->name()) {
197 if (!m) m = new KMacroCommand(n);
198 m->addCommand(new ModifyResourceNameCmd(part, original, resource.name()));
199 }
200 if (resource.initials() != original->initials()) {
201 if (!m) m = new KMacroCommand(n);
202 m->addCommand(new ModifyResourceInitialsCmd(part, original, resource.initials()));
203 }
204 if (resource.email() != original->email()) {
205 if (!m) m = new KMacroCommand(n);
206 m->addCommand(new ModifyResourceEmailCmd(part, original, resource.email()));
207 }
208 if (resource.type() != original->type()) {
209 if (!m) m = new KMacroCommand(n);
210 m->addCommand(new ModifyResourceTypeCmd(part, original, resource.type()));
211 }
212 if (resource.units() != original->units()) {
213 if (!m) m = new KMacroCommand(n);
214 m->addCommand(new ModifyResourceUnitsCmd(part, original, resource.units()));
215 }
216 if (resource.availableFrom() != original->availableFrom()) {
217 if (!m) m = new KMacroCommand(n);
218 m->addCommand(new ModifyResourceAvailableFromCmd(part, original, resource.availableFrom()));
219 }
220 if (resource.availableUntil() != original->availableUntil()) {
221 if (!m) m = new KMacroCommand(n);
222 m->addCommand(new ModifyResourceAvailableUntilCmd(part, original, resource.availableUntil()));
223 }
224 if (resource.normalRate() != original->normalRate()) {
225 if (!m) m = new KMacroCommand(n);
226 m->addCommand(new ModifyResourceNormalRateCmd(part, original, resource.normalRate()));
227 }
228 if (resource.overtimeRate() != original->overtimeRate()) {
229 if (!m) m = new KMacroCommand(n);
230 m->addCommand(new ModifyResourceOvertimeRateCmd(part, original, resource.overtimeRate()));
231 }
232 if (resource.calendar(true) != original->calendar(true)) {
233 if (!m) m = new KMacroCommand(n);
234 m->addCommand(new ModifyResourceCalendarCmd(part, original, resource.calendar(true)));
235 }
236 return m;
237}
238
239} //KPlato namespace
240
241#include "kptresourcedialog.moc"