kplato

kptrequestresourcespanel.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 "kptrequestresourcespanel.h"
00021 #include "kpttask.h"
00022 #include "kptproject.h"
00023 #include "kptresource.h"
00024 #include "kptcalendar.h"
00025 
00026 #include <kdebug.h>
00027 #include <kmessagebox.h>
00028 #include <klocale.h>
00029 #include <kptcommand.h>
00030 
00031 #include <qlistbox.h>
00032 #include <qpushbutton.h>
00033 #include <qlineedit.h>
00034 #include <qstring.h>
00035 #include <qspinbox.h>
00036 #include <qvalidator.h>
00037 #include <qcombobox.h>
00038 #include <qdatetime.h>
00039 #include <qdatetimeedit.h>
00040 
00041 namespace KPlato
00042 {
00043 
00044 ResourceTableItem::ResourceTableItem(Resource *resource, ResourceRequest *request, bool check) {
00045     m_resource = resource;
00046     m_request = request;
00047     m_checked = check;
00048     m_origChecked = check;
00049     m_checkitem = 0;
00050     m_units = 100;
00051     m_origUnits = 100;
00052     //kdDebug()<<k_funcinfo<<"Added: '"<<resource->name()<<"' checked="<<m_checked<<endl;
00053 }
00054 
00055 ResourceTableItem::~ResourceTableItem() {
00056     //kdDebug()<<k_funcinfo<<m_resource->name()<<endl;
00057 }
00058 
00059 void ResourceTableItem::update() {
00060     if (m_checkitem)
00061         m_checked = m_checkitem->isChecked();
00062     //kdDebug()<<k_funcinfo<<m_resource->name()<<" checked="<<m_checked<<endl;
00063 }
00064 
00065 void ResourceTableItem::insert(QTable *table, int row) {
00066     //kdDebug()<<k_funcinfo<<endl;
00067     m_checkitem = new QCheckTableItem(table, m_resource->name());
00068     m_checkitem->setChecked(m_checked);
00069     table->setItem(row, 0, m_checkitem);
00070     
00071     //kdDebug()<<k_funcinfo<<"Added: '"<<m_resource->name()<<"' checked="<<m_checked<<endl;
00072 }
00073 
00074 GroupLVItem::GroupLVItem(QListView *parent, ResourceGroup *group, Task &task)
00075     : QListViewItem(parent, group->name(), QString("%1").arg(group->units())),
00076       m_group(group),
00077       m_units(0)
00078 {
00079 
00080     m_request = task.resourceGroupRequest(group);
00081     if (m_request) {
00082         m_units = m_request->units();
00083     }
00084     QPtrListIterator<Resource> it(group->resources());
00085     for (; it.current(); ++it) {
00086         //kdDebug()<<k_funcinfo<<"resource="<<it.current()->name()<<endl;
00087         ResourceRequest *req=0;
00088         if (m_request) {
00089             req = m_request->find(it.current());
00090         }
00091         m_resources.append(new ResourceTableItem(it.current(), req, (bool)req));
00092     }
00093     
00094     m_resources.setAutoDelete(true);
00095 }
00096 
00097 GroupLVItem::~GroupLVItem() {
00098     //kdDebug()<<k_funcinfo<<m_group->name()<<endl;
00099 }
00100 
00101 void GroupLVItem::update() {
00102     QPtrListIterator<ResourceTableItem> it(m_resources);
00103     for (; it.current(); ++it) {
00104         it.current()->update();
00105     }
00106 }
00107 
00108 void GroupLVItem::insert(QTable *table) {
00109 
00110     // clear the table, must be a better way!
00111     for (int i = table->numRows(); i > 0; --i)
00112         table->removeRow(i-1);
00113 
00114     if (m_group->numResources() == 0) {
00115         table->setNumRows(1);
00116         table->setItem(0, 0, new QCheckTableItem(table,i18n("None")));
00117         table->setItem(0, 1, new QComboTableItem(table,i18n("None")));
00118     } else {
00119         table->setNumRows(m_group->numResources());
00120         QPtrListIterator<ResourceTableItem> it(m_resources);
00121         for (int i = 0; it.current(); ++it, ++i) {
00122             it.current()->insert(table, i);
00123         }
00124     }
00125     table->adjustColumn(0);
00126 }
00127 
00128 int GroupLVItem::numRequests() {
00129     //kdDebug()<<k_funcinfo<<endl;
00130     int value = m_units;
00131     QPtrListIterator<ResourceTableItem> it(m_resources);
00132     for (; it.current(); ++it) {
00133         value += it.current()->numRequests();
00134     }
00135     return value;
00136 }
00137 
00138 bool GroupLVItem::isNull() const {
00139     //kdDebug()<<k_funcinfo<<endl;
00140     QPtrListIterator<ResourceTableItem> it(m_resources);
00141     for (; it.current(); ++it) {
00142         if (it.current()->isChecked())
00143             return false;
00144     }
00145     if (m_units > 0)
00146         return false;
00147     return true;
00148 }
00149 
00150 RequestResourcesPanel::RequestResourcesPanel(QWidget *parent, Task &task, bool baseline)
00151     : TaskResourcesPanelBase(parent),
00152       m_task(task),
00153       m_worktime(0),
00154       selectedGroup(0),
00155       m_blockChanged(false) {
00156 
00157     Project *p = dynamic_cast<Project*>(task.projectNode());
00158     if (p) {
00159         m_worktime = p->standardWorktime();
00160         
00161         QPtrListIterator<ResourceGroup> git(p->resourceGroups());
00162         for(int i=0; git.current(); ++git, ++i) {
00163             ResourceGroup *grp = git.current();
00164             GroupLVItem *grpitem = new GroupLVItem(groupList, grp, task);
00165             groupList->insertItem(grpitem);
00166             //kdDebug()<<k_funcinfo<<" Added group: "<<grp->name()<<endl;
00167         }
00168     }
00169     QListViewItem *item = groupList->firstChild();
00170     if (item) {
00171         groupList->setSelected(item, true);
00172         groupChanged(item);
00173     }
00174 
00175     resourceTable->setReadOnly(baseline);
00176     
00177     connect(groupList, SIGNAL(selectionChanged(QListViewItem*)),  SLOT(groupChanged(QListViewItem*)));
00178     connect(resourceTable, SIGNAL(valueChanged(int, int)), SLOT(resourceChanged(int, int)));
00179 //    connect(numUnits, SIGNAL(valueChanged(int)), SLOT(unitsChanged(int)));
00180 
00181 }
00182 
00183 void RequestResourcesPanel::groupChanged(QListViewItem *item) {
00184     //kdDebug()<<k_funcinfo<<endl;
00185     GroupLVItem *grp = dynamic_cast<GroupLVItem *>(item);
00186     if (grp == 0)
00187         return;
00188 
00189     if (selectedGroup) {
00190         selectedGroup->update();
00191     }
00192     selectedGroup = grp;
00193 
00194 /*    m_blockChanged = true;
00195     numUnits->setMaxValue(grp->m_group->units());
00196     numUnits->setValue(grp->m_units);
00197     m_blockChanged = false;*/
00198     grp->insert(resourceTable);
00199 }
00200 
00201 void RequestResourcesPanel::resourceChanged(int /*r*/, int /*c*/) {
00202     //kdDebug()<<k_funcinfo<<"("<<r<<","<<c<<")"<<endl;
00203     sendChanged();
00204 }
00205 
00206 void RequestResourcesPanel::unitsChanged(int units) {
00207     //kdDebug()<<k_funcinfo<<endl;
00208     if (selectedGroup) {
00209         selectedGroup->m_units = units;
00210         sendChanged();
00211     }
00212 }
00213 
00214 KCommand *RequestResourcesPanel::buildCommand(Part *part) {
00215     //kdDebug()<<k_funcinfo<<endl;
00216     KMacroCommand *cmd = 0;
00217     if (selectedGroup) {
00218         selectedGroup->update();
00219     }
00220     QListViewItem *item = groupList->firstChild();
00221     for (; item; item = item->nextSibling()) {
00222         GroupLVItem *grp = static_cast<GroupLVItem*>(item);
00223         QPtrListIterator<ResourceTableItem> it = grp->resources();
00224         for (; it.current(); ++it) {
00225             if (it.current()->isChecked() != it.current()->isOrigChecked()) {
00226                 if (!cmd) cmd = new KMacroCommand("");
00227                 if (it.current()->isChecked()) {
00228                     if (!grp->m_request) {
00229                         grp->m_request = new ResourceGroupRequest(grp->m_group, grp->m_units);
00230                         cmd->addCommand(new AddResourceGroupRequestCmd(part, m_task, grp->m_request));
00231                     }
00232                     cmd->addCommand(new AddResourceRequestCmd(part, grp->m_request, new ResourceRequest(it.current()->resource(), it.current()->units())));
00233                     
00234                     continue;
00235                 }
00236                 if (grp->m_request && it.current()->request()) {
00237                     cmd->addCommand(new RemoveResourceRequestCmd(part, grp->m_request, it.current()->request()));
00238                     if (grp->isNull()) {
00239                         cmd->addCommand(new RemoveResourceGroupRequestCmd(part, m_task, grp->m_request));
00240                     }
00241                 } else {
00242                     kdError()<<k_funcinfo<<"Remove failed"<<endl;
00243                 }
00244                 continue;
00245             }
00246             if (!it.current()->isChecked()) {
00247                 continue;
00248             }
00249         }
00250     }
00251     return cmd;
00252 }
00253 
00254 bool RequestResourcesPanel::ok() {
00255     if (selectedGroup)
00256         selectedGroup->update();
00257     return true;
00258 }
00259 
00260 void RequestResourcesPanel::sendChanged() {
00261     if (!m_blockChanged) emit changed();
00262 }
00263 
00264 }  //KPlato namespace
00265 
00266 #include "kptrequestresourcespanel.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys