kplato

kptwbsdefinitionpanel.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 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 "kptwbsdefinitionpanel.h"
00021 #include "kptwbsdefinition.h"
00022 #include "kptcommand.h"
00023 #include "kptpart.h"
00024 
00025 #include <klocale.h>
00026 #include <kdebug.h>
00027 
00028 #include <qcombobox.h>
00029 #include <qlineedit.h>
00030 #include <qcheckbox.h>
00031 #include <qgroupbox.h>
00032 #include <qmemarray.h>
00033 #include <qpushbutton.h>
00034 #include <qstringlist.h>
00035 #include <qspinbox.h>
00036 #include <qtable.h>
00037 
00038 namespace KPlato
00039 {
00040 
00041 WBSDefinitionPanel::WBSDefinitionPanel(WBSDefinition &def, QWidget *p, const char *n)
00042     : WBSDefinitionPanelBase(p, n),
00043       m_def(def)
00044 {
00045     removeBtn->setEnabled(false);
00046     
00047     QStringList codeList = def.codeList();
00048     defaultSeparator->setText(def.defaultSeparator());
00049     defaultCode->insertStringList(codeList);
00050     defaultCode->setCurrentItem(def.defaultCodeIndex());
00051     defaultCode->setFocus();
00052     
00053     levelsGroup->setChecked(def.isLevelsDefEnabled());
00054     int i = 0;
00055     const QMap<int, WBSDefinition::CodeDef> &lev = def.levelsDef();
00056     levelsTable->setNumRows(lev.count());
00057     kdDebug()<<"Map size="<<lev.count()<<endl;
00058     QMap<int, WBSDefinition::CodeDef>::const_iterator it;
00059     for (it = lev.begin(); it != lev.end(); ++it) {
00060         levelsTable->verticalHeader()->setLabel(i, QString("%1").arg(it.key()));
00061         QComboTableItem *item = new QComboTableItem(levelsTable, codeList, true);
00062         item->setCurrentItem(it.data().code);
00063         levelsTable->setItem(i, 0, item);
00064         levelsTable->setText(i, 1, it.data().separator);
00065         i++;
00066     }
00067     levelsTable->setColumnStretchable(0, true);
00068     slotLevelChanged(level->value());
00069     
00070     connect(defaultCode, SIGNAL(activated(int)), SLOT(slotChanged()));
00071     connect(defaultSeparator, SIGNAL(textChanged(const QString&)), SLOT(slotChanged()));
00072     connect(levelsGroup, SIGNAL(toggled(bool)), SLOT(slotLevelsGroupToggled(bool)));
00073     connect(levelsTable, SIGNAL(valueChanged(int, int)), SLOT(slotChanged()));
00074     connect(levelsTable, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged()));
00075     connect(level, SIGNAL(valueChanged(int)), SLOT(slotLevelChanged(int)));
00076     connect(removeBtn, SIGNAL(clicked()), SLOT(slotRemoveBtnClicked()));
00077     connect(addBtn, SIGNAL(clicked()), SLOT(slotAddBtnClicked()));
00078 }
00079 
00080 void WBSDefinitionPanel::setStartValues(Part */*part*/) {
00081 }
00082 
00083 KMacroCommand *WBSDefinitionPanel::buildCommand(Part */*part*/) {
00084     KMacroCommand *cmd = new KMacroCommand(i18n("Modify WBS Definition"));
00085 
00086     return cmd;
00087 }
00088 
00089 bool WBSDefinitionPanel::ok() {
00090     m_def.setDefaultCode(defaultCode->currentItem());
00091     m_def.setDefaultSeparator(defaultSeparator->text());
00092     
00093     m_def.setLevelsDefEnabled(levelsGroup->isChecked());
00094 
00095     m_def.clearLevelsDef();
00096     for (int i = 0; i < levelsTable->numRows(); ++i) {
00097         m_def.setLevelsDef(levelsTable->verticalHeader()->label(i).toInt(), levelsTable->text(i, 0), levelsTable->text(i, 1));
00098     }
00099     return true;
00100 }
00101 
00102 void WBSDefinitionPanel::slotChanged() {
00103     emit changed(true);
00104 }
00105 
00106 void WBSDefinitionPanel::slotSelectionChanged() {
00107     QString s;
00108     bool rowSelected = false;
00109     for (int i=0; i < levelsTable->numRows(); ++i) {
00110         if (levelsTable->isRowSelected(i, true)) {
00111             s += QString("Row[%1]=selected ").arg(i);
00112             rowSelected = true;
00113         }
00114     }
00115     removeBtn->setEnabled(rowSelected);
00116     if (s.isEmpty()) s = "None selected";
00117     kdDebug()<<k_funcinfo<<s<<endl;
00118 }
00119 
00120 void WBSDefinitionPanel::slotRemoveBtnClicked() {
00121     QMemArray<int> rows;
00122     for (int i=0; i < levelsTable->numRows(); ++i) {
00123         if (levelsTable->isRowSelected(i)) {
00124             rows.resize(rows.size()+1);
00125             rows[rows.size()-1] = i;
00126         }
00127     }
00128     levelsTable->removeRows(rows);
00129     removeBtn->setEnabled(false);
00130     slotLevelChanged(level->value());
00131 }
00132 
00133 void WBSDefinitionPanel::slotAddBtnClicked() {
00134     kdDebug()<<k_funcinfo<<endl;
00135     int i=levelsTable->numRows()-1;
00136     for (; i >= 0; --i) {
00137         kdDebug()<<k_funcinfo<<"Checking row["<<i<<"]="<<levelsTable->verticalHeader()->label(i)<<" with "<<level->value()<<endl;
00138         if (level->value() > levelsTable->verticalHeader()->label(i).toInt()) {
00139             break;
00140         }
00141     }
00142     i++;
00143     levelsTable->insertRows(i);
00144     levelsTable->verticalHeader()->setLabel(i, QString("%1").arg(level->value()));
00145     QComboTableItem *item = new QComboTableItem(levelsTable, m_def.codeList(), true);
00146     levelsTable->setItem(i, 0, item);
00147     levelsTable->clearSelection();
00148     levelsTable->selectCells(i, 0, i, 0);
00149     levelsTable->setCurrentCell(i, 0);
00150     addBtn->setEnabled(false);
00151     slotChanged();
00152     
00153     kdDebug()<<k_funcinfo<<"Added row="<<i<<" level="<<level->value()<<endl;
00154 }
00155 
00156 void WBSDefinitionPanel::slotLevelChanged(int value) {
00157     for (int i=0; i < levelsTable->numRows(); ++i) {
00158         if (value == levelsTable->verticalHeader()->label(i).toInt()) {
00159             addBtn->setEnabled(false);
00160             return;
00161         }
00162     }
00163     addBtn->setEnabled(levelsGroup->isChecked());
00164     slotChanged();
00165 }
00166 void WBSDefinitionPanel::slotLevelsGroupToggled(bool /*on*/) {
00167     slotLevelChanged(level->value());
00168 }
00169 
00170 
00171 }  //KPlato namespace
00172 
00173 #include "kptwbsdefinitionpanel.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys