editlistviewdialog.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
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; either
7 version 2 of the License, or (at your option) any later version.
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#include <tqheader.h>
20#include <tqlayout.h>
21
22#include <tdelistview.h>
23#include <ktabwidget.h>
24#include <tdelistbox.h>
25#include <kiconloader.h>
26#include <kdebug.h>
27#include <tdelocale.h>
28
29#include <koproperty/editor.h>
30#include <koproperty/set.h>
31#include <koproperty/property.h>
32
33#include "editlistviewdialog.h"
34
35namespace KFormDesigner {
36
40
42//js(trinity.2 dependent) : KDialogBase(Tabbed, 0/* WFlags */, parent, "editlistview_dialog", true, i18n("Edit listview contents"), Ok|Cancel, Ok, false)
43: KDialogBase(Tabbed, i18n("Edit Listview Contents"), Ok|Cancel, Ok, parent, "editlistview_dialog", true /* modal */, false)
44{
45 m_column = addPage(i18n("Columns"));
46 m_contents = addPage(i18n("Contents"));
47
49 TQHBoxLayout *layout = new TQHBoxLayout(m_contents, 0, 6);
50
52 TQVBoxLayout *vlayout = new TQVBoxLayout(layout, 3);
53 TQToolButton *newRow = new TQToolButton(m_contents);
54 newRow->setIconSet(BarIconSet("edit_add"));
55 newRow->setTextLabel(i18n("&Add Item"), true);
56 vlayout->addWidget(newRow);
57 m_buttons.insert(BNewRow, newRow);
58 connect(newRow, TQ_SIGNAL(clicked()), this, TQ_SLOT(newRow()));
59
60 TQToolButton *newChild = new TQToolButton(m_contents);
61 newChild->setIconSet(BarIconSet("1rightarrow"));
62 newChild->setTextLabel(i18n("New &Subitem"), true);
63 vlayout->addWidget(newChild);
64 m_buttons.insert(BNewChild, newChild);
65 connect(newChild, TQ_SIGNAL(clicked()), this, TQ_SLOT(newChildRow()));
66
67 TQToolButton *delRow = new TQToolButton(m_contents);
68 delRow->setIconSet(BarIconSet("edit_remove"));
69 delRow->setTextLabel(i18n("&Remove Item"), true);
70 vlayout->addWidget(delRow);
71 m_buttons.insert(BRemRow, delRow);
72 connect(delRow, TQ_SIGNAL(clicked()), this, TQ_SLOT(removeRow()));
73
74 TQToolButton *rowUp = new TQToolButton(m_contents);
75 rowUp->setIconSet(BarIconSet("1uparrow"));
76 rowUp->setTextLabel(i18n("Move Item &Up"), true);
77 vlayout->addWidget(rowUp);
78 m_buttons.insert(BRowUp, rowUp);
79 connect(rowUp, TQ_SIGNAL(clicked()), this, TQ_SLOT(MoveRowUp()));
80
81 TQToolButton *rowDown = new TQToolButton(m_contents);
82 rowDown->setIconSet(BarIconSet("1downarrow"));
83 rowDown->setTextLabel(i18n("Move Item &Down"), true);
84 vlayout->addWidget(rowDown);
85 m_buttons.insert(BRowDown, rowDown);
86 connect(rowDown, TQ_SIGNAL(clicked()), this, TQ_SLOT(MoveRowDown()));
87 vlayout->addStretch();
88
90 m_listview = new TDEListView(m_contents, "editlistview_listview");
91 m_listview->setItemsRenameable(true);
92 m_listview->setItemsMovable(true);
93 m_listview->setDragEnabled(true);
94 m_listview->setAllColumnsShowFocus(true);
95 m_listview->setRootIsDecorated(true);
96 m_listview->setDropVisualizer(true);
97 m_listview->setAcceptDrops(true);
98 m_listview->setSorting(-1);
99 layout->addWidget(m_listview);
100 m_listview->setFocus();
101 connect(m_listview, TQ_SIGNAL(currentChanged(TQListViewItem*)), this, TQ_SLOT(updateButtons(TQListViewItem*)));
102 connect(m_listview, TQ_SIGNAL(moved(TQListViewItem*, TQListViewItem*, TQListViewItem*)), this, TQ_SLOT(updateButtons(TQListViewItem*)));
103
105 TQHBoxLayout *hbox = new TQHBoxLayout(m_column, 0, 6);
106
107 // The "item properties" field
108 m_editor = new KoProperty::Editor(m_column, "editcolumn_propeditor");
109 m_propSet = new KoProperty::Set(this, "columns");
110 m_propSet->addProperty(new KoProperty::Property("caption", "Caption", i18n("Caption"),i18n("Caption")));
111 m_propSet->addProperty(new KoProperty::Property("width", 100, i18n("Width"), i18n("Width")));
112 m_propSet->addProperty(new KoProperty::Property("clickable", TQVariant(true), i18n("Clickable"), i18n("Clickable") ));
113 m_propSet->addProperty(new KoProperty::Property("resizable", TQVariant(true), i18n("Resizable"), i18n("Resizable") ));
114 m_propSet->addProperty(new KoProperty::Property("fullwidth", TQVariant(false), i18n("Full Width"), i18n("Full Width") ));
115 m_editor->changeSet(m_propSet);
116 connect(m_propSet, TQ_SIGNAL(propertyChanged(KoProperty::Set & KoProperty::Property&)),
117 this, TQ_SLOT(changeProperty(KoProperty::Set & KoProperty::Property&)));
118
119 // Setup the icon toolbar //////////
121 TQToolButton *add = new TQToolButton(m_column);
122 add->setIconSet(BarIconSet("edit_add"));
123 add->setTextLabel(i18n("&Add Item"), true);
124 vbox->addWidget(add);
125 m_buttons.insert(BColAdd, add);
126 connect(add, TQ_SIGNAL(clicked()), this, TQ_SLOT(newItem()));
127
128 TQToolButton *remove = new TQToolButton(m_column);
129 remove->setIconSet(BarIconSet("edit_remove"));
130 remove->setTextLabel(i18n("&Remove Item"), true);
131 vbox->addWidget(remove);
132 m_buttons.insert(BColRem, remove);
133 connect(remove, TQ_SIGNAL(clicked()), this, TQ_SLOT(removeItem()));
134
135 TQToolButton *up = new TQToolButton(m_column);
136 up->setIconSet(BarIconSet("1uparrow"));
137 up->setTextLabel(i18n("Move Item &Up"), true);
138 vbox->addWidget(up);
139 m_buttons.insert(BColUp, up);
140 connect(up, TQ_SIGNAL(clicked()), this, TQ_SLOT(MoveItemUp()));
141
142 TQToolButton *down = new TQToolButton(m_column);
143 down->setIconSet(BarIconSet("1downarrow"));
144 down->setTextLabel(i18n("Move Item &Down"), true);
145 vbox->addWidget(down);
146 m_buttons.insert(BColDown, down);
147 connect(down, TQ_SIGNAL(clicked()), this, TQ_SLOT(MoveItemDown()));
148 vbox->addStretch();
149
150 // The listbox with columns name /////
151 m_listbox = new TDEListBox(m_column, "editlistview_columns");
152 m_listbox->setFocus();
153 hbox->insertWidget(0, m_listbox);
154 hbox->addWidget(m_editor);
155 connect(m_listbox, TQ_SIGNAL(currentChanged(TQListBoxItem*)), this, TQ_SLOT(updateItemProperties(TQListBoxItem*)));
156
158 setInitialSize(TQSize(500, 300), true);
159}
160
161int
162EditListViewDialog::exec(TQListView *listview)
163{
164 if(!listview)
165 {
166 kdDebug() << "EditListViewDialog ERROR: no listview " << endl;
167 return 0;
168 }
169
170 // We copy the contents of the listview into our listview
171 for(int i = 0; i < listview->columns(); i++)
172 {
173 m_listview->addColumn(listview->columnText(i), listview->columnWidth(i));
174 m_listview->header()->setClickEnabled(listview->header()->isClickEnabled(i), i);
175 m_listview->header()->setResizeEnabled(listview->header()->isResizeEnabled(i), i);
176 m_listview->header()->setStretchEnabled(listview->header()->isStretchEnabled(i), i);
177 m_listview->setRenameable(i, true);
178 }
179 TQListViewItem *item = listview->firstChild();
180 while(item) {
181 loadChildNodes(m_listview, item, 0);
182 item = item->nextSibling();
183 }
184
185 m_listview->setSelected(m_listview->firstChild(), true);
186 if(!m_listview->firstChild())
187 updateButtons(0);
188
189 for(int i = 0; i < listview->columns(); i++)
190 m_listbox->insertItem(listview->columnText(i));
191 m_listbox->setSelected(0, true);
192
193 // and we exec the dialog
194 int r = KDialogBase::exec();
195 if(r == TQDialog::Accepted)
196 {
197 listview->clear();
198 // We copy the contents of our listview back in the listview
199 for(int i = 0; i < m_listview->columns(); i++)
200 {
201 if(listview->columns() <= i)
202 listview->addColumn(m_listview->columnText(i), m_listview->columnWidth(i));
203 else
204 {
205 listview->setColumnText(i, m_listview->columnText(i));
206 listview->setColumnWidth(i, m_listview->columnWidth(i));
207 }
208 listview->header()->setClickEnabled(m_listview->header()->isClickEnabled(i), i);
209 listview->header()->setResizeEnabled(m_listview->header()->isResizeEnabled(i), i);
210 listview->header()->setStretchEnabled(m_listview->header()->isStretchEnabled(i), i);
211 }
212
213 TQListViewItem *item = m_listview->firstChild();
214 while(item)
215 {
216 loadChildNodes(listview, item, 0);
217 item = item->nextSibling();
218 }
219 }
220 return r;
221}
222
224void
225EditListViewDialog::changeProperty(KoProperty::Set& set, KoProperty::Property& property)
226{
227 if(&set != m_propSet)
228 return;
229
230 TQString name = property.name();
231 TQVariant value = property.value();
232 if(name == "caption") {
233 m_propSet->blockSignals(true); // we need to block signals because changeItem will modify selection, and call updateItemProperties
234 m_listbox->changeItem(value.toString(), m_listbox->currentItem());
235 m_listview->setColumnText(m_listbox->currentItem(), value.toString());
236 m_propSet->blockSignals(false);
237 }
238 else if(name == "width")
239 m_listview->setColumnWidth(m_listbox->currentItem(), value.toInt());
240 else if(name == "resizable")
241 m_listview->header()->setResizeEnabled(value.toBool(), m_listbox->currentItem());
242 else if(name == "clickable")
243 m_listview->header()->setClickEnabled(value.toBool(), m_listbox->currentItem());
244 else if(name == "fullwidth")
245 m_listview->header()->setStretchEnabled(value.toBool(), m_listbox->currentItem());
246}
247
248void
249EditListViewDialog::updateItemProperties(TQListBoxItem *item)
250{
251 if(!item)
252 return;
253
254 int id = m_listbox->index(item);
255 if(m_propSet) {
256 m_propSet->blockSignals(true); // we don't want changeProperty to be called
257 (*m_propSet)["caption"].setValue(m_listview->columnText(id), false);
258 (*m_propSet)["width"].setValue(m_listview->columnWidth(id), false);
259 (*m_propSet)["clickable"].setValue(TQVariant(m_listview->header()->isClickEnabled(id)), false);
260 (*m_propSet)["resizable"].setValue(TQVariant(m_listview->header()->isResizeEnabled(id)), false);
261 (*m_propSet)["fullwidth"].setValue(TQVariant(m_listview->header()->isStretchEnabled(id)), false);
262 m_propSet->blockSignals(false);
263 m_editor->changeSet(m_propSet);
264 }
265
266 m_buttons[BColUp]->setEnabled(item->prev());
267 m_buttons[BColDown]->setEnabled(item->next());
268}
269
270void
271EditListViewDialog::newItem()
272{
273 m_listbox->insertItem(i18n("New Column"));
274 m_listview->addColumn(i18n("New Column"));
275 m_listview->setRenameable(m_listview->columns() - 1, true);
276 m_listbox->setCurrentItem(m_listbox->count() - 1);
277 m_buttons[BColRem]->setEnabled(true);
278}
279
280void
281EditListViewDialog::removeItem()
282{
283 int current = m_listbox->currentItem();
284 if(m_listbox->item(current + 1))
285 m_listbox->setCurrentItem(current +1);
286 else
287 m_listbox->setCurrentItem(current - 1);
288
289 m_listview->removeColumn(current);
290 m_listbox->removeItem(current);
291 if(m_listbox->count() == 0)
292 m_buttons[BColRem]->setEnabled(false);
293}
294
295void
296EditListViewDialog::MoveItemUp()
297{
298 int current = m_listbox->currentItem();
299 TQString text = m_listbox->text(current);
300 m_listbox->blockSignals(true);
301
302 m_listbox->changeItem(m_listbox->text(current - 1), current);
303 m_listview->setColumnText(current, m_listview->columnText(current - 1));
304 m_listview->setColumnWidth(current, m_listview->columnWidth(current - 1));
305 m_listview->header()->setClickEnabled(m_listview->header()->isClickEnabled(current - 1), current);
306 m_listview->header()->setResizeEnabled(m_listview->header()->isResizeEnabled(current - 1), current);
307 m_listview->header()->setStretchEnabled(m_listview->header()->isStretchEnabled(current - 1), current);
308
309 m_listbox->changeItem(text, current - 1);
310 m_listview->setColumnText(current - 1, (*m_propSet)["caption"].value().toString());
311 m_listview->setColumnWidth(current - 1,(*m_propSet)["width"].value().toBool());
312 m_listview->header()->setClickEnabled((*m_propSet)["clickable"].value().toBool(), current - 1);
313 m_listview->header()->setResizeEnabled((*m_propSet)["resizable"].value().toBool(), current - 1);
314 m_listview->header()->setStretchEnabled((*m_propSet)["fullwidth"].value().toBool(), current - 1);
315
316 m_listbox->blockSignals(false);
317 m_listbox->setCurrentItem(current - 1);
318}
319
320void
321EditListViewDialog::MoveItemDown()
322{
323 int current = m_listbox->currentItem();
324 TQString text = m_listbox->text(current);
325 m_listbox->blockSignals(true);
326
327 m_listbox->changeItem(m_listbox->text(current+1), current);
328 m_listview->setColumnText(current, m_listview->columnText(current + 1));
329 m_listview->setColumnWidth(current, m_listview->columnWidth(current + 1));
330 m_listview->header()->setClickEnabled(m_listview->header()->isClickEnabled(current + 1), current);
331 m_listview->header()->setResizeEnabled(m_listview->header()->isResizeEnabled(current + 1), current);
332 m_listview->header()->setStretchEnabled(m_listview->header()->isStretchEnabled(current + 1), current);
333
334 m_listbox->changeItem(text, current+1);
335 m_listview->setColumnText(current + 1, (*m_propSet)["caption"].value().toString());
336 m_listview->setColumnWidth(current + 1,(*m_propSet)["width"].value().toBool());
337 m_listview->header()->setClickEnabled((*m_propSet)["clickable"].value().toBool(), current + 1);
338 m_listview->header()->setResizeEnabled((*m_propSet)["resizable"].value().toBool(), current + 1);
339 m_listview->header()->setStretchEnabled((*m_propSet)["fullwidth"].value().toBool(), current + 1);
340
341 m_listbox->blockSignals(false);
342 m_listbox->setCurrentItem(current + 1);
343}
344
345
347void
349{
350 if(!item)
351 {
352 for(int i = BNewChild; i <= BRowDown; i++)
353 m_buttons[i]->setEnabled(false);
354 return;
355 }
356
357 m_buttons[BNewChild]->setEnabled(true);
358 m_buttons[BRemRow]->setEnabled(true);
359 m_buttons[BRowUp]->setEnabled( (item->itemAbove() && (item->itemAbove()->parent() == item->parent())) );
360 m_buttons[BRowDown]->setEnabled(item->nextSibling());
361}
362
363void
364EditListViewDialog::loadChildNodes(TQListView *listview, TQListViewItem *item, TQListViewItem *parent)
365{
366 TQListViewItem *newItem;
367 if(listview->inherits("TDEListView"))
368 {
369 if(parent)
370 newItem = new TDEListViewItem(parent);
371 else
372 newItem = new TDEListViewItem(listview);
373 }
374 else
375 {
376 if(parent)
377 newItem = new TQListViewItem(parent);
378 else
379 newItem = new TQListViewItem(listview);
380 }
381
382 // We need to move the item at the end, which is the expected behaviour (by default it is inserted at the beginning)
383 TQListViewItem *last;
384 if(parent)
385 last = parent->firstChild();
386 else
387 last = listview->firstChild();
388
389 while(last->nextSibling())
390 last = last->nextSibling();
391 newItem->moveItem(last);
392
393 // We copy the text of all the columns
394 for(int i = 0; i < listview->columns(); i++)
395 newItem->setText(i, item->text(i));
396
397 TQListViewItem *child = item->firstChild();
398 if(child)
399 newItem->setOpen(true);
400 while(child) {
401 loadChildNodes(listview, child, newItem);
402 child = child->nextSibling();
403 }
404}
405
406void
407EditListViewDialog::newRow()
408{
409 TDEListViewItem *parent = (TDEListViewItem*)m_listview->selectedItem();
410 if(parent)
411 parent = (TDEListViewItem*)parent->parent();
412 TDEListViewItem *item;
413 if(parent)
414 item = new TDEListViewItem(parent, m_listview->selectedItem());
415 else
416 item = new TDEListViewItem(m_listview, m_listview->selectedItem());
417 item->setText(0, i18n("New Item"));
418 m_listview->setCurrentItem(item);
419}
420
421void
422EditListViewDialog::newChildRow()
423{
424 TDEListViewItem *parent = (TDEListViewItem*)m_listview->currentItem();
425 TDEListViewItem *item;
426 if(parent)
427 item = new TDEListViewItem(parent);
428 else
429 item = new TDEListViewItem(m_listview, m_listview->currentItem());
430 item->setText(0, i18n("Sub Item"));
431
432 m_listview->setCurrentItem(item);
433 parent->setOpen(true);
434}
435
436void
437EditListViewDialog::removeRow()
438{
439 delete m_listview->currentItem();
440}
441
442void
443EditListViewDialog::MoveRowUp()
444{
445 TQListViewItem *item = m_listview->currentItem()->itemAbove();
446 item->moveItem(m_listview->currentItem());
447 updateButtons(m_listview->currentItem());
448}
449
450void
451EditListViewDialog::MoveRowDown()
452{
453 TQListViewItem *before = m_listview->currentItem();
454 before->moveItem(before->nextSibling());
456}
457
458}
459
460#include "editlistviewdialog.moc"
void changeProperty(KoProperty::Set &set, KoProperty::Property &property)
Columns page slots ///////.
void updateButtons(TQListViewItem *)
Contents page slots ////////.
void loadChildNodes(TQListView *listview, TQListViewItem *item, TQListViewItem *parent)
EditListViewDialog(TQWidget *parent)
A Dialog to edit the contents of a listview /////////////////////.
type * findParent(TQObject *o, const char *className, TQObject *&prevPrev)