F:/KPlato/koffice/kplato/kptstandardworktimedialog.cc

Aller à la documentation de ce fichier.
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>
00029 #include <QHeaderView>
00030 #include <QPushButton>
00031 #include <QSpinBox>
00032 #include <QComboBox>
00033 #include <QLabel>
00034 #include <QLayout>
00035 #include <QLineEdit>
00036 
00037 #include <kcalendarsystem.h>
00038 #include <kdebug.h>
00039 #include <klocale.h>
00040 #include <knuminput.h>
00041 
00042 namespace KPlato
00043 {
00044 
00045 class WeekdayListItem : public QTreeWidgetItem
00046 {
00047 public:
00048     WeekdayListItem(Calendar *cal, int wd, QTreeWidget *parent, QString name, QTreeWidgetItem *after)
00049     : QTreeWidgetItem(parent, after),
00050       original(cal->weekday(wd)),
00051       calendar(cal),
00052       weekday(wd)
00053     {
00054         setText(0, name);
00055         day = new CalendarDay(original);
00056         if (day->state() == Map::NonWorking) {
00057             setHours();
00058         } else {
00059             setText(1, KGlobal::locale()->formatNumber(day->duration().toDouble(Duration::Unit_h)));
00060         }
00061     }
00062     ~WeekdayListItem() {
00063         delete day;
00064     }
00065     void setHours() {
00066         setText(1, "-");
00067         day->clearIntervals();
00068     }
00069     void setIntervals(QList<TimeInterval*> intervals) {
00070         day->setIntervals(intervals);
00071         setText(1, KGlobal::locale()->formatNumber(day->duration().toDouble(Duration::Unit_h)));
00072     }
00073     void setState(int st) {
00074         day->setState(st+1);
00075     }
00076     
00077     KCommand *save(Part *part) {
00078         KCommand *cmd=0;
00079         if (*original != *day) {
00080             cmd = new CalendarModifyWeekdayCmd(part, calendar, weekday, day);
00081             day = 0;
00082         }
00083         return cmd;
00084     }
00085     CalendarDay *day;
00086     CalendarDay *original;
00087     Calendar *calendar;
00088     int weekday;
00089 };
00090 
00091 StandardWorktimeDialog::StandardWorktimeDialog(Project &p, QWidget *parent, const char *name)
00092     : KDialog(parent),
00093       project(p)
00094 {
00095     setCaption( i18n("Standard Worktime") );
00096     setButtons( Ok|Cancel );
00097     setDefaultButton( Ok );
00098     showButtonSeparator( true );
00099     //kDebug()<<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     //kDebug()<<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     int cnt = dia->weekdayList->topLevelItemCount();
00131     for (int i=0; i < cnt; ++i) {
00132         KCommand *c = static_cast<WeekdayListItem*>(dia->weekdayList->topLevelItem(i))->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     : QWidget(parent),
00149       m_std(std) {
00150     
00151     setupUi(this);
00152     if (!std) {
00153         m_std = new StandardWorktime();
00154     }
00155     QBoxLayout *l = new QVBoxLayout(intervalBox);
00156     m_intervalEdit = new IntervalEdit(intervalBox);
00157     l->addWidget(m_intervalEdit);
00158     
00159     weekdayList->setRootIsDecorated(false);
00160     
00161     m_year = m_std->year();
00162     m_month = m_std->month();
00163     m_week = m_std->week();
00164     m_day = m_std->day();
00165 
00166     kDebug()<<k_funcinfo<<"y="<<m_year<<" m="<<m_month<<" w="<<m_week<<" d="<<m_day<<endl;
00167     year->setRange(1.0, 8784.0, 0.1, 1);
00168     year->setValue(m_year);
00169     month->setRange(1.0, 744.0, 0.1, 1);
00170     month->setValue(m_month);
00171     week->setRange(1.0, 168.0, 0.1, 1);
00172     week->setValue(m_week);
00173     day->setRange(1.0, 24.0, 0.1, 1);
00174     day->setValue(m_day);
00175     weekdayList->setHeaderLabel(i18n("Weekday"));
00176     weekdayList->setSortingEnabled(false);
00177     weekdayList->header()->setStretchLastSection(true);
00178     const KCalendarSystem * cs = KGlobal::locale()->calendar();
00179     Calendar *cal = m_std->calendar();
00180     if (cal) {
00181         WeekdayListItem *item = 0;
00182         for (int i = 0; i < 7; ++i) {
00183             CalendarDay *day = cal->weekday(i);
00184             if (day == 0) {
00185                 continue;
00186             }
00187             kDebug()<<k_funcinfo<<"Add day: "<<cs->weekDayName(i+1)<<endl;
00188             item = new WeekdayListItem(cal, i, weekdayList, cs->weekDayName(i+1), item);
00189             weekdayList->addTopLevelItem(item);
00190         }
00191     }
00192 
00193     connect(year, SIGNAL(valueChanged(double)), SLOT(slotYearChanged(double)));
00194     connect(month, SIGNAL(valueChanged(double)), SLOT(slotMonthChanged(double)));
00195     connect(week, SIGNAL(valueChanged(double)), SLOT(slotWeekChanged(double)));
00196     connect(day, SIGNAL(valueChanged(double)), SLOT(slotDayChanged(double)));
00197 
00198     connect(m_intervalEdit, SIGNAL(changed()), SLOT(slotIntervalChanged()));
00199     connect(bApply, SIGNAL(clicked()), SLOT(slotApplyClicked()));
00200     connect(weekdayList, SIGNAL(selectionChanged()), SLOT(slotWeekdaySelected()));
00201     connect(state, SIGNAL(activated(int)), SLOT(slotStateChanged(int)));
00202     
00203     if (weekdayList->topLevelItemCount() > 0) {
00204         weekdayList->topLevelItem(0)->setSelected(true);
00205         weekdayList->setCurrentItem(weekdayList->topLevelItem(0));
00206     }
00207 }
00208 
00209 
00210 void StandardWorktimeDialogImpl::slotEnableButtonApply(bool on) {
00211     bApply->setEnabled(on);
00212 }
00213 
00214 void StandardWorktimeDialogImpl::slotEnableButtonOk(bool on) {
00215     emit enableButtonOk(on);
00216 }
00217 
00218 void StandardWorktimeDialogImpl::slotCheckAllFieldsFilled() {
00219     emit obligatedFieldsFilled(true);
00220 }
00221 
00222 void StandardWorktimeDialogImpl::slotYearChanged(double value) {
00223     //kDebug()<<k_funcinfo<<value<<endl;
00224     m_year = value;
00225     if (month->value() > value)
00226         month->setValue(value);
00227     slotEnableButtonOk(true);
00228 }
00229 
00230 void StandardWorktimeDialogImpl::slotMonthChanged(double value) {
00231     m_month = value;
00232     if (year->value() < value)
00233         year->setValue(value);
00234     if (week->value() > value)
00235         week->setValue(value);
00236     slotEnableButtonOk(true);
00237 }
00238 
00239 void StandardWorktimeDialogImpl::slotWeekChanged(double value) {
00240     m_week = value;
00241     if (month->value() < value)
00242         month->setValue(value);
00243     if (day->value() > value)
00244         day->setValue(value);
00245     slotEnableButtonOk(true);
00246 }
00247 
00248 void StandardWorktimeDialogImpl::slotDayChanged(double value) {
00249     m_day = value;
00250     if (week->value() < value)
00251         week->setValue(value);
00252     slotEnableButtonOk(true);
00253 }
00254 
00255 void StandardWorktimeDialogImpl::slotIntervalChanged() {
00256     //kDebug()<<k_funcinfo<<endl;
00257     slotEnableButtonApply(true);
00258 }
00259 
00260 void StandardWorktimeDialogImpl::slotApplyClicked() {
00261     //kDebug()<<k_funcinfo<<"state="<<state->currentItem()<<endl;
00262     int c = weekdayList->topLevelItemCount();
00263     for (int i=0; i < c; ++i) {
00264         QTreeWidgetItem *item = weekdayList->topLevelItem(i);
00265         if (item->isSelected()) {
00266             //kDebug()<<k_funcinfo<<item->text(0)<<" selected"<<endl;
00267             WeekdayListItem *wd = static_cast<WeekdayListItem*>(item);
00268             wd->setState(state->currentItem());
00269             if (state->currentItem() == 0) {
00270                 wd->setHours();
00271             } else {
00272                 wd->setIntervals(m_intervalEdit->intervals());
00273             }
00274             slotEnableButtonOk(true);
00275         }
00276     }
00277 }
00278 
00279 void StandardWorktimeDialogImpl::slotWeekdaySelected() {
00280     //kDebug()<<k_funcinfo<<"state="<<state->currentItem()<<endl;
00281     
00282     QList<QTreeWidgetItem *> lst = weekdayList->selectedItems();
00283     if (lst.count() == 0) {
00284         editBox->setEnabled(false);
00285         return;
00286     }
00287     //kDebug()<<k_funcinfo<<item->text(0)<<" selected"<<endl;
00288     WeekdayListItem *wd = static_cast<WeekdayListItem*>(lst[0]);
00289     state->setCurrentIndex(wd->day->state()-1);
00290     m_intervalEdit->setIntervals(wd->day->workingIntervals());
00291     slotStateChanged(state->currentItem());
00292     editBox->setEnabled(true);
00293 }
00294 
00295 void StandardWorktimeDialogImpl::slotStateChanged(int st) {
00296     //kDebug()<<k_funcinfo<<"state="<<state->currentItem()<<endl;
00297     intervalBox->setEnabled(st == 1); //Working
00298     slotEnableButtonApply(st == 0);
00299 }
00300 
00301 }  //KPlato namespace
00302 
00303 
00304 #include "kptstandardworktimedialog.moc"

Généré le Wed Nov 22 23:40:57 2006 pour KPlato par  doxygen 1.5.1-p1