kplato

kptstandardworktimedialog.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 - 2006 Dag Andersen <danders@get2net.dk>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation;
00007    version 2 of the License.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kptstandardworktimedialog.h"
00021 #include "kptduration.h"
00022 #include "kptproject.h"
00023 #include "kptcalendar.h"
00024 #include "kptcommand.h"
00025 #include "kptintervaledit.h"
00026 #include "kptpart.h"
00027 
00028 #include <qgroupbox.h>
00029 #include <qheader.h>
00030 #include <qpushbutton.h>
00031 #include <qspinbox.h>
00032 #include <qcombobox.h>
00033 #include <qlabel.h>
00034 #include <qlayout.h>
00035 #include <qlineedit.h>
00036 #include <qdatetimeedit.h>
00037 #include <qdatetime.h>
00038 
00039 #include <kdebug.h>
00040 #include <kcombobox.h>
00041 #include <kcalendarsystem.h>
00042 #include <kglobal.h>
00043 #include <klocale.h>
00044 #include <knuminput.h>
00045 #include <klistview.h>
00046 
00047 namespace KPlato
00048 {
00049 class WeekdayListItem : public KListViewItem
00050 {
00051 public:
00052     WeekdayListItem(Calendar *cal, int wd, KListView *parent, QString name, KListViewItem *after)
00053     : KListViewItem(parent, after),
00054       original(cal->weekday(wd)),
00055       calendar(cal),
00056       weekday(wd)
00057     {
00058         setText(0, name);
00059         day = new CalendarDay(original);
00060         if (day->state() == Map::NonWorking) {
00061             setHours();
00062         } else {
00063             setText(1, KGlobal::locale()->formatNumber(day->duration().toDouble(Duration::Unit_h)));
00064         }
00065     }
00066     ~WeekdayListItem() {
00067         delete day;
00068     }
00069     void setHours() {
00070         setText(1, "-");
00071         day->clearIntervals();
00072     }
00073     void setIntervals(QPtrList<QPair<QTime, QTime> > intervals) {
00074         day->setIntervals(intervals);
00075         setText(1, KGlobal::locale()->formatNumber(day->duration().toDouble(Duration::Unit_h)));
00076     }
00077     void setState(int st) {
00078         day->setState(st+1);
00079     }
00080     
00081     KCommand *save(Part *part) {
00082         KCommand *cmd=0;
00083         if (*original != *day) {
00084             cmd = new CalendarModifyWeekdayCmd(part, calendar, weekday, day);
00085             day = 0;
00086         }
00087         return cmd;
00088     }
00089     CalendarDay *day;
00090     CalendarDay *original;
00091     Calendar *calendar;
00092     int weekday;
00093 };
00094 
00095 StandardWorktimeDialog::StandardWorktimeDialog(Project &p, QWidget *parent, const char *name)
00096     : KDialogBase( Swallow, i18n("Standard Worktime"), Ok|Cancel, Ok, parent, name, true, true),
00097       project(p)
00098 {
00099     //kdDebug()<<k_funcinfo<<&p<<endl;
00100     m_original = p.standardWorktime();
00101     dia = new StandardWorktimeDialogImpl(m_original, this);
00102 
00103     setMainWidget(dia);
00104     enableButtonOK(false);
00105 
00106     connect(dia, SIGNAL(obligatedFieldsFilled(bool) ), SLOT(enableButtonOK(bool)));
00107     connect(dia, SIGNAL(enableButtonOk(bool)), SLOT(enableButtonOK(bool)));
00108 }
00109 
00110 KMacroCommand *StandardWorktimeDialog::buildCommand(Part *part) {
00111     //kdDebug()<<k_funcinfo<<endl;
00112     QString n = i18n("Modify Standard Worktime");
00113     KMacroCommand *cmd = 0;
00114     if (m_original->year() != dia->inYear()) {
00115         if (cmd == 0) cmd = new KMacroCommand(n);
00116         cmd->addCommand(new ModifyStandardWorktimeYearCmd(part, m_original, m_original->year(), dia->inYear()));
00117     }
00118     if (m_original->month() != dia->inMonth()) {
00119         if (cmd == 0) cmd = new KMacroCommand(n);
00120         cmd->addCommand(new ModifyStandardWorktimeMonthCmd(part, m_original, m_original->month(), dia->inMonth()));
00121     }
00122     if (m_original->week() != dia->inWeek()) {
00123         if (cmd == 0) cmd = new KMacroCommand(n);
00124         cmd->addCommand(new ModifyStandardWorktimeWeekCmd(part, m_original, m_original->week(), dia->inWeek()));
00125     }
00126     if (m_original->day() != dia->inDay()) {
00127         if (cmd == 0) cmd = new KMacroCommand(n);
00128         cmd->addCommand(new ModifyStandardWorktimeDayCmd(part, m_original, m_original->day(), dia->inDay()));
00129     }
00130     QListViewItem *item = dia->weekdayList->firstChild();
00131     for (; item; item = item->nextSibling()) {
00132         KCommand *c = static_cast<WeekdayListItem*>(item)->save(part);
00133         if (c) {
00134             if (cmd == 0) cmd = new KMacroCommand(n);
00135             cmd->addCommand(c);
00136         }
00137     }
00138     return cmd;
00139     
00140 }
00141 
00142 void StandardWorktimeDialog::slotOk() {
00143     accept();
00144 }
00145 
00146 
00147 StandardWorktimeDialogImpl::StandardWorktimeDialogImpl(StandardWorktime *std, QWidget *parent) 
00148     : StandardWorktimeDialogBase(parent),
00149       m_std(std) {
00150     if (!std) {
00151         m_std = new StandardWorktime();
00152     }
00153     QBoxLayout *l = new QVBoxLayout(intervalBox);
00154     m_intervalEdit = new IntervalEdit(intervalBox);
00155     l->addWidget(m_intervalEdit);
00156     
00157     m_year = m_std->year();
00158     m_month = m_std->month();
00159     m_week = m_std->week();
00160     m_day = m_std->day();
00161     
00162     year->setValue(m_year);
00163     month->setValue(m_month);
00164     week->setValue(m_week);
00165     day->setValue(m_day);
00166 
00167     weekdayList->setSorting(-1);
00168     weekdayList->header()->setStretchEnabled(true);
00169     const KCalendarSystem * cs = KGlobal::locale()->calendar();
00170     Calendar *cal = m_std->calendar();
00171     if (cal) {
00172         WeekdayListItem *item = 0;
00173         for (int i = 0; i < 7; ++i) {
00174             CalendarDay *day = cal->weekday(i);
00175             if (day == 0) {
00176                 continue;
00177             }
00178             item = new WeekdayListItem(cal, i, weekdayList, cs->weekDayName(i+1), item);
00179             weekdayList->insertItem(item);
00180         }
00181     }
00182     connect(year, SIGNAL(valueChanged(double)), SLOT(slotYearChanged(double)));
00183     connect(month, SIGNAL(valueChanged(double)), SLOT(slotMonthChanged(double)));
00184     connect(week, SIGNAL(valueChanged(double)), SLOT(slotWeekChanged(double)));
00185     connect(day, SIGNAL(valueChanged(double)), SLOT(slotDayChanged(double)));
00186     
00187     connect(m_intervalEdit, SIGNAL(changed()), SLOT(slotIntervalChanged()));
00188     connect(bApply, SIGNAL(clicked()), SLOT(slotApplyClicked()));
00189     connect(weekdayList, SIGNAL(selectionChanged()), SLOT(slotWeekdaySelected()));
00190     connect(state, SIGNAL(activated(int)), SLOT(slotStateChanged(int)));
00191     
00192     if (weekdayList->firstChild()) {
00193         weekdayList->setSelected(weekdayList->firstChild(), true);
00194         weekdayList->setCurrentItem(weekdayList->firstChild());
00195     }
00196 }
00197 
00198 
00199 void StandardWorktimeDialogImpl::slotEnableButtonApply(bool on) {
00200     bApply->setEnabled(on);
00201 }
00202 
00203 void StandardWorktimeDialogImpl::slotEnableButtonOk(bool on) {
00204     emit enableButtonOk(on);
00205 }
00206 
00207 void StandardWorktimeDialogImpl::slotCheckAllFieldsFilled() {
00208     emit obligatedFieldsFilled(true);
00209 }
00210 
00211 void StandardWorktimeDialogImpl::slotYearChanged(double value) {
00212     //kdDebug()<<k_funcinfo<<value<<endl;
00213     m_year = value;
00214     if (month->value() > value)
00215         month->setValue(value);
00216     slotEnableButtonOk(true);
00217 }
00218 
00219 void StandardWorktimeDialogImpl::slotMonthChanged(double value) {
00220     m_month = value;
00221     if (year->value() < value)
00222         year->setValue(value);
00223     if (week->value() > value)
00224         week->setValue(value);
00225     slotEnableButtonOk(true);
00226 }
00227 
00228 void StandardWorktimeDialogImpl::slotWeekChanged(double value) {
00229     m_week = value;
00230     if (month->value() < value)
00231         month->setValue(value);
00232     if (day->value() > value)
00233         day->setValue(value);
00234     slotEnableButtonOk(true);
00235 }
00236 
00237 void StandardWorktimeDialogImpl::slotDayChanged(double value) {
00238     m_day = value;
00239     if (week->value() < value)
00240         week->setValue(value);
00241     slotEnableButtonOk(true);
00242 }
00243 
00244 void StandardWorktimeDialogImpl::slotIntervalChanged() {
00245     //kdDebug()<<k_funcinfo<<endl;
00246     slotEnableButtonApply(true);
00247 }
00248 
00249 void StandardWorktimeDialogImpl::slotApplyClicked() {
00250     //kdDebug()<<k_funcinfo<<"state="<<state->currentItem()<<endl;
00251     QListViewItem *item = weekdayList->firstChild();
00252     for (; item; item = item->nextSibling()) {
00253         if (item->isSelected()) {
00254             //kdDebug()<<k_funcinfo<<item->text(0)<<" selected"<<endl;
00255             WeekdayListItem *wd = static_cast<WeekdayListItem*>(item);
00256             wd->setState(state->currentItem());
00257             if (state->currentItem() == 0) {
00258                 wd->setHours();
00259             } else {
00260                 wd->setIntervals(m_intervalEdit->intervals());
00261             }
00262             slotEnableButtonOk(true);
00263         }
00264     }
00265 }
00266 
00267 void StandardWorktimeDialogImpl::slotWeekdaySelected() {
00268     //kdDebug()<<k_funcinfo<<"state="<<state->currentItem()<<endl;
00269     
00270     QListViewItem *item = weekdayList->firstChild();
00271     for (; item; item = item->nextSibling()) {
00272         if (item->isSelected()) {
00273             //kdDebug()<<k_funcinfo<<item->text(0)<<" selected"<<endl;
00274             WeekdayListItem *wd = static_cast<WeekdayListItem*>(item);
00275             state->setCurrentItem(wd->day->state()-1);
00276             m_intervalEdit->setIntervals(wd->day->workingIntervals());
00277             slotStateChanged(state->currentItem());
00278             break;
00279         }
00280     }
00281     editBox->setEnabled(item != 0);
00282 }
00283 
00284 void StandardWorktimeDialogImpl::slotStateChanged(int st) {
00285     //kdDebug()<<k_funcinfo<<"state="<<state->currentItem()<<endl;
00286     intervalBox->setEnabled(st == 1); //Working
00287     slotEnableButtonApply(st == 0);
00288 }
00289 
00290 }  //KPlato namespace
00291 
00292 #include "kptstandardworktimedialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys