kplato

kpttaskgeneralpanel.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 - 2006 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 "kpttaskgeneralpanel.h"
21#include "kpttaskdialog.h"
22#include "kpttask.h"
23#include "kptcommand.h"
24#include "kptduration.h"
25#include "kptdurationwidget.h"
26#include "kptcalendar.h"
27#include "kptdatetime.h"
28#include "kptconfig.h"
29#include "kptpart.h"
30
31#include <tdemessagebox.h>
32#include <klineedit.h>
33#include <ktextedit.h>
34#include <kcombobox.h>
35#include <kdatetimewidget.h>
36#include <tdelocale.h>
37#include <kcommand.h>
38#include <tdeabc/addressee.h>
39#include <tdeabc/addresseedialog.h>
40#include <kdatewidget.h>
41
42#include <tqlayout.h>
43#include <tqlabel.h>
44#include <tqdatetime.h>
45#include <tqdatetimeedit.h>
46#include <tqgroupbox.h>
47#include <tqpushbutton.h>
48#include <tqspinbox.h>
49
50#include <kdebug.h>
51
52namespace KPlato
53{
54
55TaskGeneralPanel::TaskGeneralPanel(Task &task, StandardWorktime *workTime, bool /*baseline*/, TQWidget *p, const char *n)
56 : TaskGeneralPanelImpl(p, n),
57 m_task(task),
58 m_dayLength(24)
59{
60 useTime = true;
61 setStartValues(task, workTime);
62/* Why is this done? Its useless (its not actually read only, but that may be a TQt thing) and I have to
63 edit these to actually be able to OK the dialog. TZ-8-2005
64 namefield->setReadOnly(baseline);
65 leaderfield->setReadOnly(baseline);
66 idfield->setReadOnly(baseline);
67 schedulingGroup->setEnabled(!baseline);
68*/
69}
70
71void TaskGeneralPanel::setStartValues(Task &task, StandardWorktime *workTime) {
72 m_effort = m_duration = task.effort()->expected();
73 namefield->setText(task.name());
74 leaderfield->setText(task.leader());
75 descriptionfield->setText(task.description());
76 idfield->setText(task.id());
77 wbsfield->setText(task.wbs());
78
79 setEstimateFields(DurationWidget::Days|DurationWidget::Hours|DurationWidget::Minutes);
80 if (workTime) {
81 //kdDebug()<<k_funcinfo<<"daylength="<<workTime->day()<<endl;
82 m_dayLength = workTime->day();
83 if (task.effort()->type() == Effort::Type_Effort) {
84 setEstimateScales(m_dayLength);
85 }
86 }
87 setEstimateFieldUnit(0, i18n("days", "d"));
88 setEstimateFieldUnit(1, i18n("hours", "h"));
89 setEstimateFieldUnit(2, i18n("minutes", "m"));
90 setEstimateType(task.effort()->type());
91
92 setSchedulingType(task.constraint());
93 if (task.constraintStartTime().isValid()) {
94 setStartDateTime(task.constraintStartTime());
95 } else {
96 TQDate date = TQDate::currentDate();
97 setStartDateTime(TQDateTime(date, TQTime()));
98 }
99 if (task.constraintEndTime().isValid()) {
100 setEndDateTime(task.constraintEndTime());
101 } else {
102 setEndDateTime(TQDateTime(startDate().addDays(1), TQTime()));
103 }
104 //kdDebug()<<k_funcinfo<<"Effort: "<<task.effort()->expected().toString()<<endl;
105 setEstimate(task.effort()->expected());
106 setOptimistic(task.effort()->optimisticRatio());
107 setPessimistic(task.effort()->pessimisticRatio());
108 setRisktype(task.effort()->risktype());
109 namefield->setFocus();
110}
111
112KMacroCommand *TaskGeneralPanel::buildCommand(Part *part) {
113 KMacroCommand *cmd = new KMacroCommand(i18n("Modify Task"));
114 bool modified = false;
115
116 Duration dt = Duration();
117
118 if (!namefield->isHidden() && m_task.name() != namefield->text()) {
119 cmd->addCommand(new NodeModifyNameCmd(part, m_task, namefield->text()));
120 modified = true;
121 }
122 if (!leaderfield->isHidden() && m_task.leader() != leaderfield->text()) {
123 cmd->addCommand(new NodeModifyLeaderCmd(part, m_task, leaderfield->text()));
124 modified = true;
125 }
126 if (!descriptionfield->isHidden() &&
127 m_task.description() != descriptionfield->text()) {
128 cmd->addCommand(new NodeModifyDescriptionCmd(part, m_task, descriptionfield->text()));
129 modified = true;
130 }
131 Node::ConstraintType c = (Node::ConstraintType)schedulingType();
132 if (c != m_task.constraint()) {
133 cmd->addCommand(new NodeModifyConstraintCmd(part, m_task, c));
134 modified = true;
135 }
136 if (startDateTime() != m_task.constraintStartTime() &&
137 (c == Node::FixedInterval || c == Node::StartNotEarlier || c == Node::MustStartOn)) {
138 cmd->addCommand(new NodeModifyConstraintStartTimeCmd(part, m_task, startDateTime()));
139 modified = true;
140 }
141 if (endDateTime() != m_task.constraintEndTime() &&
142 (c == Node::FinishNotLater || c == Node::FixedInterval || c == Node::MustFinishOn)) {
143 cmd->addCommand(new NodeModifyConstraintEndTimeCmd(part, m_task, endDateTime()));
144 modified = true;
145 }
146 if (!idfield->isHidden() && idfield->text() != m_task.id()) {
147
148 cmd->addCommand(new NodeModifyIdCmd(part, m_task, idfield->text()));
149 modified = true;
150 }
151 int et = estimationType();
152 if (et != m_task.effort()->type()) {
153 cmd->addCommand(new ModifyEffortTypeCmd(part, m_task, m_task.effort()->type(), et));
154 modified = true;
155 }
156 dt = estimationValue();
157 kdDebug()<<k_funcinfo<<"Estimate: "<<dt.toString()<<endl;
158 bool expchanged = dt != m_task.effort()->expected();
159 if ( expchanged ) {
160 cmd->addCommand(new ModifyEffortCmd(part, m_task, m_task.effort()->expected(), dt));
161 modified = true;
162 }
163 int x = optimistic();
164 if ( x != m_task.effort()->optimisticRatio() || expchanged) {
165 cmd->addCommand(new EffortModifyOptimisticRatioCmd(part, m_task, m_task.effort()->optimisticRatio(), x));
166 modified = true;
167 }
168 x = pessimistic();
169 if ( x != m_task.effort()->pessimisticRatio() || expchanged) {
170 cmd->addCommand(new EffortModifyPessimisticRatioCmd(part, m_task, m_task.effort()->pessimisticRatio(), x));
171 modified = true;
172 }
173 if (m_task.effort()->risktype() != risktype()) {
174 cmd->addCommand(new EffortModifyRiskCmd(part, m_task, m_task.effort()->risktype(), risktype()));
175 modified = true;
176 }
177 if (!modified) {
178 delete cmd;
179 return 0;
180 }
181 return cmd;
182}
183
184bool TaskGeneralPanel::ok() {
185 if (idfield->text() != m_task.id() && m_task.findNode(idfield->text())) {
186 KMessageBox::sorry(this, i18n("Task id must be unique"));
187 idfield->setFocus();
188 return false;
189 }
190 return true;
191}
192
193void TaskGeneralPanel::estimationTypeChanged(int type) {
194 if (type == 0 /*Effort*/) {
195 Duration d = estimationValue();
196 setEstimateScales(m_dayLength);
197 //setEstimate(d);
198 estimate->setEnabled(true);
199 } else {
200 Duration d = estimationValue();
201 setEstimateScales(24);
202 //setEstimate(d);
203 if (schedulingType() == 6) { /*Fixed interval*/
204 estimate->setEnabled(false);
205 } else {
206 estimate->setEnabled(true);
207 }
208
209 }
210 TaskGeneralPanelImpl::estimationTypeChanged(type);
211}
212
213void TaskGeneralPanel::scheduleTypeChanged(int value)
214{
215 if (value == 6 /*Fixed interval*/) {
216 if (estimateType->currentItem() == 1/*duration*/){
217 setEstimateScales(24);
218 estimate->setEnabled(false);
219 setEstimate(DateTime(endDateTime()) - DateTime(startDateTime()));
220 }
221 } else {
222 setEstimateScales(m_dayLength);
223 estimate->setEnabled(true);
224 }
225 TaskGeneralPanelImpl::scheduleTypeChanged(value);
226}
227
228//-----------------------------
229TaskGeneralPanelImpl::TaskGeneralPanelImpl(TQWidget *p, const char *n)
230 : TaskGeneralPanelBase(p, n) {
231
232 connect(idfield, TQ_SIGNAL(textChanged(const TQString &)), TQ_SLOT(checkAllFieldsFilled()));
233 connect(namefield, TQ_SIGNAL(textChanged(const TQString &)), TQ_SLOT(checkAllFieldsFilled()));
234 connect(leaderfield, TQ_SIGNAL(textChanged(const TQString &)), TQ_SLOT(checkAllFieldsFilled()));
235 connect(chooseLeader, TQ_SIGNAL(clicked()), TQ_SLOT(changeLeader()));
236 connect(estimateType, TQ_SIGNAL(activated(int)), TQ_SLOT(estimationTypeChanged(int)));
237 connect(scheduleType, TQ_SIGNAL(activated(int)), TQ_SLOT(scheduleTypeChanged(int)));
238 connect(scheduleStartDate, TQ_SIGNAL(changed(TQDate)), TQ_SLOT(startDateChanged()));
239 connect(scheduleStartTime, TQ_SIGNAL(valueChanged(const TQTime&)), TQ_SLOT(startTimeChanged(const TQTime&)));
240 connect(scheduleEndDate, TQ_SIGNAL(changed(TQDate)), TQ_SLOT(endDateChanged()));
241 connect(scheduleEndTime, TQ_SIGNAL(valueChanged(const TQTime&)), TQ_SLOT(endTimeChanged(const TQTime&)));
242 connect(estimate, TQ_SIGNAL(valueChanged()), TQ_SLOT(checkAllFieldsFilled()));
243 connect(optimisticValue, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(checkAllFieldsFilled()));
244 connect(pessimisticValue, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(checkAllFieldsFilled()));
245 connect(descriptionfield, TQ_SIGNAL(textChanged()), TQ_SLOT(checkAllFieldsFilled()));
246 connect(risk, TQ_SIGNAL(activated(int)), TQ_SLOT(checkAllFieldsFilled()));
247}
248
249void TaskGeneralPanelImpl::setSchedulingType(int type)
250{
251 enableDateTime(type);
252 scheduleType->setCurrentItem(type);
253 emit schedulingTypeChanged(type);
254}
255
256int TaskGeneralPanelImpl::schedulingType() const
257{
258 return scheduleType->currentItem();
259}
260
261void TaskGeneralPanelImpl::changeLeader()
262{
263 TDEABC::Addressee a = TDEABC::AddresseeDialog::getAddressee(this);
264 if (!a.isEmpty())
265 {
266 leaderfield->setText(a.fullEmail());
267 }
268}
269
270void TaskGeneralPanelImpl::setEstimationType( int type )
271{
272 estimateType->setCurrentItem(type);
273}
274
275int TaskGeneralPanelImpl::estimationType() const
276{
277 return estimateType->currentItem();
278}
279
280void TaskGeneralPanelImpl::setOptimistic( int value )
281{
282 optimisticValue->setValue(value);
283}
284
285void TaskGeneralPanelImpl::setPessimistic( int value )
286{
287 pessimisticValue->setValue(value);
288}
289
290int TaskGeneralPanelImpl::optimistic() const
291{
292 return optimisticValue->value();
293}
294
295int TaskGeneralPanelImpl::pessimistic()
296{
297 return pessimisticValue->value();
298}
299
300void TaskGeneralPanelImpl::enableDateTime( int scheduleType )
301{
302 scheduleStartTime->setEnabled(false);
303 scheduleEndTime->setEnabled(false);
304 scheduleStartDate->setEnabled(false);
305 scheduleEndDate->setEnabled(false);
306 switch (scheduleType)
307 {
308 case 0: //ASAP
309 case 1: //ALAP
310 break;
311 case 2: //Must start on
312 case 4: // Start not earlier
313 if (useTime) {
314 scheduleStartTime->setEnabled(true);
315 scheduleEndTime->setEnabled(false);
316 }
317 scheduleStartDate->setEnabled(true);
318 scheduleEndDate->setEnabled(false);
319 break;
320 case 3: //Must finish on
321 case 5: // Finish not later
322 if (useTime) {
323 scheduleStartTime->setEnabled(false);
324 scheduleEndTime->setEnabled(true);
325 }
326 scheduleStartDate->setEnabled(false);
327 scheduleEndDate->setEnabled(true);
328 break;
329 case 6: //Fixed interval
330 if (useTime) {
331 scheduleStartTime->setEnabled(true);
332 scheduleEndTime->setEnabled(true);
333 }
334 scheduleStartDate->setEnabled(true);
335 scheduleEndDate->setEnabled(true);
336 break;
337 default:
338 break;
339 }
340}
341
342
343void TaskGeneralPanelImpl::estimationTypeChanged( int /*type*/ )
344{
345 checkAllFieldsFilled();
346}
347
348
349
350void TaskGeneralPanelImpl::setEstimate( const Duration & duration)
351{
352 estimate->setValue( duration );
353}
354
355
356void TaskGeneralPanelImpl::setEstimateType( int type)
357{
358 estimateType->setCurrentItem(type);
359}
360
361
362void TaskGeneralPanelImpl::checkAllFieldsFilled()
363{
364 emit changed();
365 emit obligatedFieldsFilled(!namefield->text().isEmpty() && !idfield->text().isEmpty());
366}
367
368
369Duration TaskGeneralPanelImpl::estimationValue()
370{
371 return estimate->value();
372}
373
374
375void TaskGeneralPanelImpl::setEstimateFields( int mask )
376{
377 estimate->setVisibleFields(mask);
378}
379
380
381void TaskGeneralPanelImpl::setEstimateScales( double day )
382{
383 estimate->setFieldScale(0, day);
384 estimate->setFieldRightscale(0, day);
385
386 estimate->setFieldLeftscale(1, day);
387}
388
389
390void TaskGeneralPanelImpl::setEstimateFieldUnit( int field, TQString unit )
391{
392 estimate->setFieldUnit(field, unit);
393}
394
395void TaskGeneralPanelImpl::startDateChanged()
396{
397 if (!scheduleStartDate->isEnabled()) {
398 return;
399 }
400 TQDate date = startDate();
401 if (startDateTime() > endDateTime())
402 {
403 scheduleEndTime->blockSignals(true);
404 scheduleEndDate->blockSignals(true);
405 setEndDate(date);
406 setEndTime(startTime());
407 scheduleEndTime->blockSignals(false);
408 scheduleEndDate->blockSignals(false);
409 }
410 if (scheduleType->currentItem() == 6 /*FixedInterval*/)
411 {
412 estimationTypeChanged(estimateType->currentItem());
413 }
414 checkAllFieldsFilled();
415}
416
417void TaskGeneralPanelImpl::startTimeChanged( const TQTime &time )
418{
419 if (!scheduleStartTime->isEnabled()) {
420 return;
421 }
422 if (startDateTime() > endDateTime())
423 {
424 scheduleEndTime->blockSignals(true);
425 setEndTime(time);
426 scheduleEndTime->blockSignals(false);
427 }
428 if (scheduleType->currentItem() == 6 /*FixedInterval*/)
429 {
430 estimationTypeChanged(estimateType->currentItem());
431 }
432 checkAllFieldsFilled();
433}
434
435
436void TaskGeneralPanelImpl::endDateChanged()
437{
438 if (!scheduleEndDate->isEnabled()) {
439 return;
440 }
441 TQDate date = endDate();
442 if (endDateTime() < startDateTime())
443 {
444 scheduleStartTime->blockSignals(true);
445 scheduleStartDate->blockSignals(true);
446 setStartDate(date);
447 setStartTime(endTime());
448 scheduleStartTime->blockSignals(false);
449 scheduleStartDate->blockSignals(false);
450 }
451
452 if (scheduleType->currentItem() == 6 /*FixedInterval*/)
453 {
454 estimationTypeChanged(estimateType->currentItem());
455 }
456 checkAllFieldsFilled();
457}
458
459void TaskGeneralPanelImpl::endTimeChanged( const TQTime &time )
460{
461 if (!scheduleEndTime->isEnabled()) {
462 return;
463 }
464 if (endDateTime() < startDateTime())
465 {
466 scheduleStartTime->blockSignals(true);
467 setStartTime(time);
468 scheduleStartTime->blockSignals(false);
469 }
470
471 if (scheduleType->currentItem() == 6 /*FixedInterval*/)
472 {
473 estimationTypeChanged(estimateType->currentItem());
474 }
475 checkAllFieldsFilled();
476}
477
478void TaskGeneralPanelImpl::scheduleTypeChanged( int value )
479{
480 estimationTypeChanged(estimateType->currentItem());
481 enableDateTime(value);
482 checkAllFieldsFilled();
483}
484
485
486TQDateTime TaskGeneralPanelImpl::startDateTime()
487{
488 return TQDateTime(startDate(), startTime());
489}
490
491
492TQDateTime TaskGeneralPanelImpl::endDateTime()
493{
494 return TQDateTime(endDate(), endTime());
495}
496
497void TaskGeneralPanelImpl::setStartTime( const TQTime &time )
498{
499 scheduleStartTime->setTime(time);
500}
501
502void TaskGeneralPanelImpl::setEndTime( const TQTime &time )
503{
504 scheduleEndTime->setTime(time);
505}
506
507TQTime TaskGeneralPanelImpl::startTime() const
508{
509 return scheduleStartTime->time();
510}
511
512TQTime TaskGeneralPanelImpl::endTime()
513{
514 return scheduleEndTime->time();
515}
516
517TQDate TaskGeneralPanelImpl::startDate()
518{
519 return scheduleStartDate->date();
520}
521
522
523TQDate TaskGeneralPanelImpl::endDate()
524{
525 return scheduleEndDate->date();
526}
527
528void TaskGeneralPanelImpl::setStartDateTime( const TQDateTime &dt )
529{
530 setStartDate(dt.date());
531 setStartTime(dt.time());
532}
533
534
535void TaskGeneralPanelImpl::setEndDateTime( const TQDateTime &dt )
536{
537 setEndDate(dt.date());
538 setEndTime(dt.time());
539}
540
541void TaskGeneralPanelImpl::setStartDate( const TQDate &date )
542{
543 scheduleStartDate->setDate(date);
544}
545
546
547void TaskGeneralPanelImpl::setEndDate( const TQDate &date )
548{
549 scheduleEndDate->setDate(date);
550}
551
552void TaskGeneralPanelImpl::setRisktype( int r )
553{
554 risk->setCurrentItem(r);
555}
556
557int TaskGeneralPanelImpl::risktype() const
558{
559 return risk->currentItem();
560}
561
562
563
564} //KPlato namespace
565
566#include "kpttaskgeneralpanel.moc"