kplato

kptresourcedialog.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 - 2005 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 "kptresourcedialog.h"
00021 #include "kptcommand.h"
00022 #include "kptpart.h"
00023 #include "kptproject.h"
00024 #include "kptresource.h"
00025 #include "kptcalendar.h"
00026 
00027 #include <qpushbutton.h>
00028 #include <qlabel.h>
00029 #include <qlineedit.h>
00030 #include <qcombobox.h>
00031 #include <qdatetimeedit.h>
00032 #include <qdatetime.h>
00033 #include <qbuttongroup.h>
00034 #include <qradiobutton.h>
00035 #include <qspinbox.h>
00036 #include <qptrlist.h>
00037 #include <qstringlist.h>
00038 
00039 #include <kabc/addressee.h>
00040 #include <kabc/addresseedialog.h>
00041 
00042 #include <kcommand.h>
00043 #include <kdatetimewidget.h>
00044 #include <kmessagebox.h>
00045 #include <klocale.h>
00046 #include <kglobal.h>
00047 #include <kdebug.h>
00048 
00049 namespace KPlato
00050 {
00051 
00052 ResourceDialogImpl::ResourceDialogImpl (QWidget *parent)
00053     : ResourceDialogBase(parent)
00054 {
00055 
00056     connect(type, SIGNAL(activated(int)), SLOT(slotChanged()));
00057     connect(units, SIGNAL(valueChanged(int)), SLOT(slotChanged()));
00058     connect(nameEdit, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00059     connect(initialsEdit, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00060     connect(emailEdit, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00061 
00062     connect(calendarList, SIGNAL(activated(int)), SLOT(slotChanged()));
00063 
00064     connect(rateEdit, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00065     connect(overtimeEdit, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00066 
00067     connect(chooseBtn, SIGNAL(clicked()), SLOT(slotChooseResource()));
00068     
00069     connect(availableFrom, SIGNAL(valueChanged(const QDateTime&)), SLOT(slotChanged()));
00070     connect(availableUntil, SIGNAL(valueChanged(const QDateTime&)), SLOT(slotChanged()));
00071     connect(availableFrom, SIGNAL(valueChanged(const QDateTime&)), SLOT(slotAvailableFromChanged(const QDateTime&)));
00072     connect(availableUntil, SIGNAL(valueChanged(const QDateTime&)), SLOT(slotAvailableUntilChanged(const QDateTime&)));
00073 }
00074 
00075 
00076 void ResourceDialogImpl::slotChanged() {
00077     emit changed();
00078 }
00079 
00080 void ResourceDialogImpl::slotAvailableFromChanged(const QDateTime&) {
00081     if (availableUntil->dateTime() < availableFrom->dateTime()) {
00082         disconnect(availableUntil, SIGNAL(valueChanged(const QDateTime&)), this,  SLOT(slotAvailableUntilChanged(const QDateTime&)));
00083         //kdDebug()<<"From: "<<availableFrom->dateTime().toString()<<" until="<<availableUntil->dateTime().toString()<<endl;
00084         availableUntil->setDateTime(availableFrom->dateTime());
00085         connect(availableUntil, SIGNAL(valueChanged(const QDateTime&)), SLOT(slotAvailableUntilChanged(const QDateTime&)));
00086     }
00087 }
00088 
00089 void ResourceDialogImpl::slotAvailableUntilChanged(const QDateTime&) {
00090     if (availableFrom->dateTime() > availableUntil->dateTime()) {
00091         disconnect(availableFrom, SIGNAL(valueChanged(const QDateTime&)), this,  SLOT(slotAvailableFromChanged(const QDateTime&)));
00092         //kdDebug()<<"Until: "<<availableUntil->dateTime().toString()<<" from="<<availableFrom->dateTime().toString()<<endl;
00093         availableFrom->setDateTime(availableUntil->dateTime());
00094         connect(availableFrom, SIGNAL(valueChanged(const QDateTime&)), SLOT(slotAvailableFromChanged(const QDateTime&)));
00095     }
00096 }
00097 
00098 void ResourceDialogImpl::slotCalculationNeeded(const QString&) {
00099     emit calculate();
00100     emit changed();
00101 }
00102 
00103 void ResourceDialogImpl::slotChooseResource()
00104 {
00105     KABC::Addressee a = KABC::AddresseeDialog::getAddressee(this);
00106     if (!a.isEmpty()) {
00107         nameEdit->setText(a.assembledName());
00108         emailEdit->setText(a.preferredEmail());
00109         QStringList l = QStringList::split(' ', a.assembledName());
00110         QString in;
00111         QStringList::Iterator it = l.begin();
00112         for (/*int i = 0*/; it != l.end(); ++it) {
00113             in += (*it)[0];
00114         }
00115         initialsEdit->setText(in);
00116     }
00117 }
00118 
00120 
00121 ResourceDialog::ResourceDialog(Project &project, Resource *resource, QWidget *parent, const char *name)
00122     : KDialogBase( Swallow, i18n("Resource Settings"), Ok|Cancel, Ok, parent, name, true, true),
00123       m_original(resource),
00124       m_resource(resource),
00125       m_calculationNeeded(false)
00126 {
00127     dia = new ResourceDialogImpl(this);
00128     setMainWidget(dia);
00129     enableButtonOK(false);
00130 
00131     dia->nameEdit->setText(resource->name());
00132     dia->initialsEdit->setText(resource->initials());
00133     dia->emailEdit->setText(resource->email());
00134     dia->type->setCurrentItem((int)resource->type()); // NOTE: must match enum
00135     dia->units->setValue(resource->units());
00136     dia->availableFrom->setDateTime(resource->availableFrom());
00137     dia->availableUntil->setDateTime(resource->availableUntil());
00138     dia->rateEdit->setText(KGlobal::locale()->formatMoney(resource->normalRate()));
00139     dia->overtimeEdit->setText(KGlobal::locale()->formatMoney(resource->overtimeRate()));
00140 
00141     int cal = 0;
00142     dia->calendarList->insertItem(i18n("None"));
00143     m_calendars.insert(0, 0);      
00144     QPtrList<Calendar> list = project.calendars();
00145     QPtrListIterator<Calendar> cit = list;
00146     for(int i=1; cit.current(); ++cit, ++i) {
00147         dia->calendarList->insertItem(cit.current()->name(), i);
00148         m_calendars.insert(i, cit.current());
00149         if (cit.current() == resource->calendar())
00150             cal = i;
00151     }
00152     dia->calendarList->setCurrentItem(cal);
00153 
00154     connect(dia, SIGNAL(changed()), SLOT(enableButtonOk()));
00155     connect(dia, SIGNAL(calculate()), SLOT(slotCalculationNeeded()));
00156     connect(dia->calendarList, SIGNAL(activated(int)), SLOT(slotCalendarChanged(int)));
00157     
00158 }
00159 
00160 
00161 void ResourceDialog::enableButtonOk() {
00162     enableButtonOK(true);
00163 }
00164 
00165 void ResourceDialog::slotCalculationNeeded() {
00166     m_calculationNeeded = true;
00167 }
00168 
00169 void ResourceDialog::slotOk() {
00170     m_resource.setName(dia->nameEdit->text());
00171     m_resource.setInitials(dia->initialsEdit->text());
00172     m_resource.setEmail(dia->emailEdit->text());
00173     m_resource.setType((Resource::Type)(dia->type->currentItem()));
00174     m_resource.setUnits(dia->units->value());
00175 
00176     m_resource.setNormalRate(KGlobal::locale()->readMoney(dia->rateEdit->text()));
00177     m_resource.setOvertimeRate(KGlobal::locale()->readMoney(dia->overtimeEdit->text()));
00178     m_resource.setCalendar(m_calendars[dia->calendarList->currentItem()]);
00179     m_resource.setAvailableFrom(dia->availableFrom->dateTime());
00180     m_resource.setAvailableUntil(dia->availableUntil->dateTime());
00181     accept();
00182 }
00183 
00184 void ResourceDialog::slotCalendarChanged(int /*cal*/) {
00185     
00186 }
00187 
00188 KCommand *ResourceDialog::buildCommand(Part *part) {
00189     return buildCommand(m_original, m_resource, part);
00190 }
00191 
00192 // static
00193 KCommand *ResourceDialog::buildCommand(Resource *original, Resource &resource, Part *part) {
00194     KMacroCommand *m=0;
00195     QString n = i18n("Modify Resource");
00196     if (resource.name() != original->name()) {
00197         if (!m) m = new KMacroCommand(n);
00198         m->addCommand(new ModifyResourceNameCmd(part, original, resource.name()));
00199     }
00200     if (resource.initials() != original->initials()) {
00201         if (!m) m = new KMacroCommand(n);
00202         m->addCommand(new ModifyResourceInitialsCmd(part, original, resource.initials()));
00203     }
00204     if (resource.email() != original->email()) {
00205         if (!m) m = new KMacroCommand(n);
00206         m->addCommand(new ModifyResourceEmailCmd(part, original, resource.email()));
00207     }
00208     if (resource.type() != original->type()) {
00209         if (!m) m = new KMacroCommand(n);
00210         m->addCommand(new ModifyResourceTypeCmd(part, original, resource.type()));
00211     }
00212     if (resource.units() != original->units()) {
00213         if (!m) m = new KMacroCommand(n);
00214         m->addCommand(new ModifyResourceUnitsCmd(part, original, resource.units()));
00215     }
00216     if (resource.availableFrom() != original->availableFrom()) {
00217         if (!m) m = new KMacroCommand(n);
00218         m->addCommand(new ModifyResourceAvailableFromCmd(part, original, resource.availableFrom()));
00219     }
00220     if (resource.availableUntil() != original->availableUntil()) {
00221         if (!m) m = new KMacroCommand(n);
00222         m->addCommand(new ModifyResourceAvailableUntilCmd(part, original, resource.availableUntil()));
00223     }
00224     if (resource.normalRate() != original->normalRate()) {
00225         if (!m) m = new KMacroCommand(n);
00226         m->addCommand(new ModifyResourceNormalRateCmd(part, original, resource.normalRate()));
00227     }
00228     if (resource.overtimeRate() != original->overtimeRate()) {
00229         if (!m) m = new KMacroCommand(n);
00230         m->addCommand(new ModifyResourceOvertimeRateCmd(part, original, resource.overtimeRate()));
00231     }
00232     if (resource.calendar(true) != original->calendar(true)) {
00233         if (!m) m = new KMacroCommand(n);
00234         m->addCommand(new ModifyResourceCalendarCmd(part, original, resource.calendar(true)));
00235     }
00236     return m;
00237 }
00238 
00239 }  //KPlato namespace
00240 
00241 #include "kptresourcedialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys