kplato

kptstandardworktimedialog.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 "kptstandardworktimedialog.h"
21#include "kptduration.h"
22#include "kptproject.h"
23#include "kptcalendar.h"
24#include "kptcommand.h"
25#include "kptintervaledit.h"
26#include "kptpart.h"
27
28#include <tqgroupbox.h>
29#include <tqheader.h>
30#include <tqpushbutton.h>
31#include <tqspinbox.h>
32#include <tqcombobox.h>
33#include <tqlabel.h>
34#include <tqlayout.h>
35#include <tqlineedit.h>
36#include <tqdatetimeedit.h>
37#include <tqdatetime.h>
38
39#include <kdebug.h>
40#include <kcombobox.h>
41#include <kcalendarsystem.h>
42#include <tdeglobal.h>
43#include <tdelocale.h>
44#include <knuminput.h>
45#include <tdelistview.h>
46
47namespace KPlato
48{
49class WeekdayListItem : public TDEListViewItem
50{
51public:
52 WeekdayListItem(Calendar *cal, int wd, TDEListView *parent, TQString name, TDEListViewItem *after)
53 : TDEListViewItem(parent, after),
54 original(cal->weekday(wd)),
55 calendar(cal),
56 weekday(wd)
57 {
58 setText(0, name);
59 day = new CalendarDay(original);
60 if (day->state() == Map::NonWorking) {
61 setHours();
62 } else {
63 setText(1, TDEGlobal::locale()->formatNumber(day->duration().toDouble(Duration::Unit_h)));
64 }
65 }
66 ~WeekdayListItem() {
67 delete day;
68 }
69 void setHours() {
70 setText(1, "-");
71 day->clearIntervals();
72 }
73 void setIntervals(TQPtrList<TQPair<TQTime, TQTime> > intervals) {
74 day->setIntervals(intervals);
75 setText(1, TDEGlobal::locale()->formatNumber(day->duration().toDouble(Duration::Unit_h)));
76 }
77 void setState(int st) {
78 day->setState(st+1);
79 }
80
81 KCommand *save(Part *part) {
82 KCommand *cmd=0;
83 if (*original != *day) {
84 cmd = new CalendarModifyWeekdayCmd(part, calendar, weekday, day);
85 day = 0;
86 }
87 return cmd;
88 }
89 CalendarDay *day;
90 CalendarDay *original;
91 Calendar *calendar;
92 int weekday;
93};
94
95StandardWorktimeDialog::StandardWorktimeDialog(Project &p, TQWidget *parent, const char *name)
96 : KDialogBase( Swallow, i18n("Standard Worktime"), Ok|Cancel, Ok, parent, name, true, true),
97 project(p)
98{
99 //kdDebug()<<k_funcinfo<<&p<<endl;
100 m_original = p.standardWorktime();
101 dia = new StandardWorktimeDialogImpl(m_original, this);
102
103 setMainWidget(dia);
104 enableButtonOK(false);
105
106 connect(dia, TQ_SIGNAL(obligatedFieldsFilled(bool) ), TQ_SLOT(enableButtonOK(bool)));
107 connect(dia, TQ_SIGNAL(enableButtonOk(bool)), TQ_SLOT(enableButtonOK(bool)));
108}
109
110KMacroCommand *StandardWorktimeDialog::buildCommand(Part *part) {
111 //kdDebug()<<k_funcinfo<<endl;
112 TQString n = i18n("Modify Standard Worktime");
113 KMacroCommand *cmd = 0;
114 if (m_original->year() != dia->inYear()) {
115 if (cmd == 0) cmd = new KMacroCommand(n);
116 cmd->addCommand(new ModifyStandardWorktimeYearCmd(part, m_original, m_original->year(), dia->inYear()));
117 }
118 if (m_original->month() != dia->inMonth()) {
119 if (cmd == 0) cmd = new KMacroCommand(n);
120 cmd->addCommand(new ModifyStandardWorktimeMonthCmd(part, m_original, m_original->month(), dia->inMonth()));
121 }
122 if (m_original->week() != dia->inWeek()) {
123 if (cmd == 0) cmd = new KMacroCommand(n);
124 cmd->addCommand(new ModifyStandardWorktimeWeekCmd(part, m_original, m_original->week(), dia->inWeek()));
125 }
126 if (m_original->day() != dia->inDay()) {
127 if (cmd == 0) cmd = new KMacroCommand(n);
128 cmd->addCommand(new ModifyStandardWorktimeDayCmd(part, m_original, m_original->day(), dia->inDay()));
129 }
130 TQListViewItem *item = dia->weekdayList->firstChild();
131 for (; item; item = item->nextSibling()) {
132 KCommand *c = static_cast<WeekdayListItem*>(item)->save(part);
133 if (c) {
134 if (cmd == 0) cmd = new KMacroCommand(n);
135 cmd->addCommand(c);
136 }
137 }
138 return cmd;
139
140}
141
142void StandardWorktimeDialog::slotOk() {
143 accept();
144}
145
146
147StandardWorktimeDialogImpl::StandardWorktimeDialogImpl(StandardWorktime *std, TQWidget *parent)
148 : StandardWorktimeDialogBase(parent),
149 m_std(std) {
150 if (!std) {
151 m_std = new StandardWorktime();
152 }
153 TQBoxLayout *l = new TQVBoxLayout(intervalBox);
154 m_intervalEdit = new IntervalEdit(intervalBox);
155 l->addWidget(m_intervalEdit);
156
157 m_year = m_std->year();
158 m_month = m_std->month();
159 m_week = m_std->week();
160 m_day = m_std->day();
161
162 year->setValue(m_year);
163 month->setValue(m_month);
164 week->setValue(m_week);
165 day->setValue(m_day);
166
167 weekdayList->setSorting(-1);
168 weekdayList->header()->setStretchEnabled(true);
169 const KCalendarSystem * cs = TDEGlobal::locale()->calendar();
170 Calendar *cal = m_std->calendar();
171 if (cal) {
172 WeekdayListItem *item = 0;
173 for (int i = 0; i < 7; ++i) {
174 CalendarDay *day = cal->weekday(i);
175 if (day == 0) {
176 continue;
177 }
178 item = new WeekdayListItem(cal, i, weekdayList, cs->weekDayName(i+1), item);
179 weekdayList->insertItem(item);
180 }
181 }
182 connect(year, TQ_SIGNAL(valueChanged(double)), TQ_SLOT(slotYearChanged(double)));
183 connect(month, TQ_SIGNAL(valueChanged(double)), TQ_SLOT(slotMonthChanged(double)));
184 connect(week, TQ_SIGNAL(valueChanged(double)), TQ_SLOT(slotWeekChanged(double)));
185 connect(day, TQ_SIGNAL(valueChanged(double)), TQ_SLOT(slotDayChanged(double)));
186
187 connect(m_intervalEdit, TQ_SIGNAL(changed()), TQ_SLOT(slotIntervalChanged()));
188 connect(bApply, TQ_SIGNAL(clicked()), TQ_SLOT(slotApplyClicked()));
189 connect(weekdayList, TQ_SIGNAL(selectionChanged()), TQ_SLOT(slotWeekdaySelected()));
190 connect(state, TQ_SIGNAL(activated(int)), TQ_SLOT(slotStateChanged(int)));
191
192 if (weekdayList->firstChild()) {
193 weekdayList->setSelected(weekdayList->firstChild(), true);
194 weekdayList->setCurrentItem(weekdayList->firstChild());
195 }
196}
197
198
199void StandardWorktimeDialogImpl::slotEnableButtonApply(bool on) {
200 bApply->setEnabled(on);
201}
202
203void StandardWorktimeDialogImpl::slotEnableButtonOk(bool on) {
204 emit enableButtonOk(on);
205}
206
207void StandardWorktimeDialogImpl::slotCheckAllFieldsFilled() {
208 emit obligatedFieldsFilled(true);
209}
210
211void StandardWorktimeDialogImpl::slotYearChanged(double value) {
212 //kdDebug()<<k_funcinfo<<value<<endl;
213 m_year = value;
214 if (month->value() > value)
215 month->setValue(value);
216 slotEnableButtonOk(true);
217}
218
219void StandardWorktimeDialogImpl::slotMonthChanged(double value) {
220 m_month = value;
221 if (year->value() < value)
222 year->setValue(value);
223 if (week->value() > value)
224 week->setValue(value);
225 slotEnableButtonOk(true);
226}
227
228void StandardWorktimeDialogImpl::slotWeekChanged(double value) {
229 m_week = value;
230 if (month->value() < value)
231 month->setValue(value);
232 if (day->value() > value)
233 day->setValue(value);
234 slotEnableButtonOk(true);
235}
236
237void StandardWorktimeDialogImpl::slotDayChanged(double value) {
238 m_day = value;
239 if (week->value() < value)
240 week->setValue(value);
241 slotEnableButtonOk(true);
242}
243
244void StandardWorktimeDialogImpl::slotIntervalChanged() {
245 //kdDebug()<<k_funcinfo<<endl;
246 slotEnableButtonApply(true);
247}
248
249void StandardWorktimeDialogImpl::slotApplyClicked() {
250 //kdDebug()<<k_funcinfo<<"state="<<state->currentItem()<<endl;
251 TQListViewItem *item = weekdayList->firstChild();
252 for (; item; item = item->nextSibling()) {
253 if (item->isSelected()) {
254 //kdDebug()<<k_funcinfo<<item->text(0)<<" selected"<<endl;
255 WeekdayListItem *wd = static_cast<WeekdayListItem*>(item);
256 wd->setState(state->currentItem());
257 if (state->currentItem() == 0) {
258 wd->setHours();
259 } else {
260 wd->setIntervals(m_intervalEdit->intervals());
261 }
262 slotEnableButtonOk(true);
263 }
264 }
265}
266
267void StandardWorktimeDialogImpl::slotWeekdaySelected() {
268 //kdDebug()<<k_funcinfo<<"state="<<state->currentItem()<<endl;
269
270 TQListViewItem *item = weekdayList->firstChild();
271 for (; item; item = item->nextSibling()) {
272 if (item->isSelected()) {
273 //kdDebug()<<k_funcinfo<<item->text(0)<<" selected"<<endl;
274 WeekdayListItem *wd = static_cast<WeekdayListItem*>(item);
275 state->setCurrentItem(wd->day->state()-1);
276 m_intervalEdit->setIntervals(wd->day->workingIntervals());
277 slotStateChanged(state->currentItem());
278 break;
279 }
280 }
281 editBox->setEnabled(item != 0);
282}
283
284void StandardWorktimeDialogImpl::slotStateChanged(int st) {
285 //kdDebug()<<k_funcinfo<<"state="<<state->currentItem()<<endl;
286 intervalBox->setEnabled(st == 1); //Working
287 slotEnableButtonApply(st == 0);
288}
289
290} //KPlato namespace
291
292#include "kptstandardworktimedialog.moc"