kplato

kptwbsdefinitionpanel.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Dag Andersen <danders@get2net.dk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation;
7 version 2 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kptwbsdefinitionpanel.h"
21#include "kptwbsdefinition.h"
22#include "kptcommand.h"
23#include "kptpart.h"
24
25#include <tdelocale.h>
26#include <kdebug.h>
27
28#include <tqcombobox.h>
29#include <tqlineedit.h>
30#include <tqcheckbox.h>
31#include <tqgroupbox.h>
32#include <tqmemarray.h>
33#include <tqpushbutton.h>
34#include <tqstringlist.h>
35#include <tqspinbox.h>
36#include <tqtable.h>
37
38namespace KPlato
39{
40
41WBSDefinitionPanel::WBSDefinitionPanel(WBSDefinition &def, TQWidget *p, const char *n)
42 : WBSDefinitionPanelBase(p, n),
43 m_def(def)
44{
45 removeBtn->setEnabled(false);
46
47 TQStringList codeList = def.codeList();
48 defaultSeparator->setText(def.defaultSeparator());
49 defaultCode->insertStringList(codeList);
50 defaultCode->setCurrentItem(def.defaultCodeIndex());
51 defaultCode->setFocus();
52
53 levelsGroup->setChecked(def.isLevelsDefEnabled());
54 int i = 0;
55 const TQMap<int, WBSDefinition::CodeDef> &lev = def.levelsDef();
56 levelsTable->setNumRows(lev.count());
57 kdDebug()<<"Map size="<<lev.count()<<endl;
58 TQMap<int, WBSDefinition::CodeDef>::const_iterator it;
59 for (it = lev.begin(); it != lev.end(); ++it) {
60 levelsTable->verticalHeader()->setLabel(i, TQString("%1").arg(it.key()));
61 TQComboTableItem *item = new TQComboTableItem(levelsTable, codeList, true);
62 item->setCurrentItem(it.data().code);
63 levelsTable->setItem(i, 0, item);
64 levelsTable->setText(i, 1, it.data().separator);
65 i++;
66 }
67 levelsTable->setColumnStretchable(0, true);
68 slotLevelChanged(level->value());
69
70 connect(defaultCode, TQ_SIGNAL(activated(int)), TQ_SLOT(slotChanged()));
71 connect(defaultSeparator, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotChanged()));
72 connect(levelsGroup, TQ_SIGNAL(toggled(bool)), TQ_SLOT(slotLevelsGroupToggled(bool)));
73 connect(levelsTable, TQ_SIGNAL(valueChanged(int, int)), TQ_SLOT(slotChanged()));
74 connect(levelsTable, TQ_SIGNAL(selectionChanged()), TQ_SLOT(slotSelectionChanged()));
75 connect(level, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(slotLevelChanged(int)));
76 connect(removeBtn, TQ_SIGNAL(clicked()), TQ_SLOT(slotRemoveBtnClicked()));
77 connect(addBtn, TQ_SIGNAL(clicked()), TQ_SLOT(slotAddBtnClicked()));
78}
79
80void WBSDefinitionPanel::setStartValues(Part */*part*/) {
81}
82
83KMacroCommand *WBSDefinitionPanel::buildCommand(Part */*part*/) {
84 KMacroCommand *cmd = new KMacroCommand(i18n("Modify WBS Definition"));
85
86 return cmd;
87}
88
89bool WBSDefinitionPanel::ok() {
90 m_def.setDefaultCode(defaultCode->currentItem());
91 m_def.setDefaultSeparator(defaultSeparator->text());
92
93 m_def.setLevelsDefEnabled(levelsGroup->isChecked());
94
95 m_def.clearLevelsDef();
96 for (int i = 0; i < levelsTable->numRows(); ++i) {
97 m_def.setLevelsDef(levelsTable->verticalHeader()->label(i).toInt(), levelsTable->text(i, 0), levelsTable->text(i, 1));
98 }
99 return true;
100}
101
102void WBSDefinitionPanel::slotChanged() {
103 emit changed(true);
104}
105
106void WBSDefinitionPanel::slotSelectionChanged() {
107 TQString s;
108 bool rowSelected = false;
109 for (int i=0; i < levelsTable->numRows(); ++i) {
110 if (levelsTable->isRowSelected(i, true)) {
111 s += TQString("Row[%1]=selected ").arg(i);
112 rowSelected = true;
113 }
114 }
115 removeBtn->setEnabled(rowSelected);
116 if (s.isEmpty()) s = "None selected";
117 kdDebug()<<k_funcinfo<<s<<endl;
118}
119
120void WBSDefinitionPanel::slotRemoveBtnClicked() {
121 TQMemArray<int> rows;
122 for (int i=0; i < levelsTable->numRows(); ++i) {
123 if (levelsTable->isRowSelected(i)) {
124 rows.resize(rows.size()+1);
125 rows[rows.size()-1] = i;
126 }
127 }
128 levelsTable->removeRows(rows);
129 removeBtn->setEnabled(false);
130 slotLevelChanged(level->value());
131}
132
133void WBSDefinitionPanel::slotAddBtnClicked() {
134 kdDebug()<<k_funcinfo<<endl;
135 int i=levelsTable->numRows()-1;
136 for (; i >= 0; --i) {
137 kdDebug()<<k_funcinfo<<"Checking row["<<i<<"]="<<levelsTable->verticalHeader()->label(i)<<" with "<<level->value()<<endl;
138 if (level->value() > levelsTable->verticalHeader()->label(i).toInt()) {
139 break;
140 }
141 }
142 i++;
143 levelsTable->insertRows(i);
144 levelsTable->verticalHeader()->setLabel(i, TQString("%1").arg(level->value()));
145 TQComboTableItem *item = new TQComboTableItem(levelsTable, m_def.codeList(), true);
146 levelsTable->setItem(i, 0, item);
147 levelsTable->clearSelection();
148 levelsTable->selectCells(i, 0, i, 0);
149 levelsTable->setCurrentCell(i, 0);
150 addBtn->setEnabled(false);
151 slotChanged();
152
153 kdDebug()<<k_funcinfo<<"Added row="<<i<<" level="<<level->value()<<endl;
154}
155
156void WBSDefinitionPanel::slotLevelChanged(int value) {
157 for (int i=0; i < levelsTable->numRows(); ++i) {
158 if (value == levelsTable->verticalHeader()->label(i).toInt()) {
159 addBtn->setEnabled(false);
160 return;
161 }
162 }
163 addBtn->setEnabled(levelsGroup->isChecked());
164 slotChanged();
165}
166void WBSDefinitionPanel::slotLevelsGroupToggled(bool /*on*/) {
167 slotLevelChanged(level->value());
168}
169
170
171} //KPlato namespace
172
173#include "kptwbsdefinitionpanel.moc"