KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

461 lines
16KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. * Boston, MA 02110-1301, USA.
  15. */
  16. #include <tqheader.h>
  17. #include <tqlayout.h>
  18. #include <klistview.h>
  19. #include <ktabwidget.h>
  20. #include <klistbox.h>
  21. #include <kiconloader.h>
  22. #include <kdebug.h>
  23. #include <klocale.h>
  24. #include <koproperty/editor.h>
  25. #include <koproperty/set.h>
  26. #include <koproperty/property.h>
  27. #include "editlistviewdialog.h"
  28. namespace KFormDesigner {
  29. //////////////////////////////////////////////////////////////////////////////////
  30. /// A Dialog to edit the contents of a listview /////////////////////
  31. /////////////////////////////////////////////////////////////////////////////////
  32. EditListViewDialog::EditListViewDialog(TQWidget *parent)
  33. //js(trinity.2 dependent) : KDialogBase(Tabbed, 0/* WFlags */, parent, "editlistview_dialog", true, i18n("Edit listview contents"), Ok|Cancel, Ok, false)
  34. : KDialogBase(Tabbed, i18n("Edit Listview Contents"), Ok|Cancel, Ok, parent, "editlistview_dialog", true /* modal */, false)
  35. {
  36. m_column = addPage(i18n("Columns"));
  37. m_contents = addPage(i18n("Contents"));
  38. ///////// Setup the "Contents" page /////////////////////////////
  39. TQHBoxLayout *tqlayout = new TQHBoxLayout(m_contents, 0, 6);
  40. //// Setup the icon toolbar /////////////////
  41. TQVBoxLayout *vtqlayout = new TQVBoxLayout(tqlayout, 3);
  42. TQToolButton *newRow = new TQToolButton(m_contents);
  43. newRow->setIconSet(BarIconSet("edit_add"));
  44. newRow->setTextLabel(i18n("&Add Item"), true);
  45. vtqlayout->addWidget(newRow);
  46. m_buttons.insert(BNewRow, newRow);
  47. connect(newRow, TQT_SIGNAL(clicked()), this, TQT_SLOT(newRow()));
  48. TQToolButton *newChild = new TQToolButton(m_contents);
  49. newChild->setIconSet(BarIconSet("1rightarrow"));
  50. newChild->setTextLabel(i18n("New &Subitem"), true);
  51. vtqlayout->addWidget(newChild);
  52. m_buttons.insert(BNewChild, newChild);
  53. connect(newChild, TQT_SIGNAL(clicked()), this, TQT_SLOT(newChildRow()));
  54. TQToolButton *delRow = new TQToolButton(m_contents);
  55. delRow->setIconSet(BarIconSet("edit_remove"));
  56. delRow->setTextLabel(i18n("&Remove Item"), true);
  57. vtqlayout->addWidget(delRow);
  58. m_buttons.insert(BRemRow, delRow);
  59. connect(delRow, TQT_SIGNAL(clicked()), this, TQT_SLOT(removeRow()));
  60. TQToolButton *rowUp = new TQToolButton(m_contents);
  61. rowUp->setIconSet(BarIconSet("1uparrow"));
  62. rowUp->setTextLabel(i18n("Move Item &Up"), true);
  63. vtqlayout->addWidget(rowUp);
  64. m_buttons.insert(BRowUp, rowUp);
  65. connect(rowUp, TQT_SIGNAL(clicked()), this, TQT_SLOT(MoveRowUp()));
  66. TQToolButton *rowDown = new TQToolButton(m_contents);
  67. rowDown->setIconSet(BarIconSet("1downarrow"));
  68. rowDown->setTextLabel(i18n("Move Item &Down"), true);
  69. vtqlayout->addWidget(rowDown);
  70. m_buttons.insert(BRowDown, rowDown);
  71. connect(rowDown, TQT_SIGNAL(clicked()), this, TQT_SLOT(MoveRowDown()));
  72. vtqlayout->addStretch();
  73. //// The listview ///////////
  74. m_listview = new KListView(m_contents, "editlistview_listview");
  75. m_listview->setItemsRenameable(true);
  76. m_listview->setItemsMovable(true);
  77. m_listview->setDragEnabled(true);
  78. m_listview->setAllColumnsShowFocus(true);
  79. m_listview->setRootIsDecorated(true);
  80. m_listview->setDropVisualizer(true);
  81. m_listview->setAcceptDrops(true);
  82. m_listview->setSorting(-1);
  83. tqlayout->addWidget(m_listview);
  84. m_listview->setFocus();
  85. connect(m_listview, TQT_SIGNAL(currentChanged(TQListViewItem*)), this, TQT_SLOT(updateButtons(TQListViewItem*)));
  86. connect(m_listview, TQT_SIGNAL(moved(TQListViewItem*, TQListViewItem*, TQListViewItem*)), this, TQT_SLOT(updateButtons(TQListViewItem*)));
  87. /////////////////// Setup the columns page ////////////////
  88. TQHBoxLayout *hbox = new TQHBoxLayout(m_column, 0, 6);
  89. // The "item properties" field
  90. m_editor = new KoProperty::Editor(m_column, "editcolumn_propeditor");
  91. m_propSet = new KoProperty::Set(TQT_TQOBJECT(this), "columns");
  92. m_propSet->addProperty(new KoProperty::Property("caption", "Caption", i18n("Caption"),i18n("Caption")));
  93. m_propSet->addProperty(new KoProperty::Property("width", 100, i18n("Width"), i18n("Width")));
  94. m_propSet->addProperty(new KoProperty::Property("clickable", TQVariant(true, 3), i18n("Clickable"), i18n("Clickable") ));
  95. m_propSet->addProperty(new KoProperty::Property("resizable", TQVariant(true, 3), i18n("Resizable"), i18n("Resizable") ));
  96. m_propSet->addProperty(new KoProperty::Property("fullwidth", TQVariant(false, 3), i18n("Full Width"), i18n("Full Width") ));
  97. m_editor->changeSet(m_propSet);
  98. connect(m_propSet, TQT_SIGNAL(propertyChanged(KoProperty::Set & KoProperty::Property&)),
  99. this, TQT_SLOT(changeProperty(KoProperty::Set & KoProperty::Property&)));
  100. // Setup the icon toolbar //////////
  101. TQVBoxLayout *vbox = new TQVBoxLayout(hbox, 3);
  102. TQToolButton *add = new TQToolButton(m_column);
  103. add->setIconSet(BarIconSet("edit_add"));
  104. add->setTextLabel(i18n("&Add Item"), true);
  105. vbox->addWidget(add);
  106. m_buttons.insert(BColAdd, add);
  107. connect(add, TQT_SIGNAL(clicked()), this, TQT_SLOT(newItem()));
  108. TQToolButton *remove = new TQToolButton(m_column);
  109. remove->setIconSet(BarIconSet("edit_remove"));
  110. remove->setTextLabel(i18n("&Remove Item"), true);
  111. vbox->addWidget(remove);
  112. m_buttons.insert(BColRem, remove);
  113. connect(remove, TQT_SIGNAL(clicked()), this, TQT_SLOT(removeItem()));
  114. TQToolButton *up = new TQToolButton(m_column);
  115. up->setIconSet(BarIconSet("1uparrow"));
  116. up->setTextLabel(i18n("Move Item &Up"), true);
  117. vbox->addWidget(up);
  118. m_buttons.insert(BColUp, up);
  119. connect(up, TQT_SIGNAL(clicked()), this, TQT_SLOT(MoveItemUp()));
  120. TQToolButton *down = new TQToolButton(m_column);
  121. down->setIconSet(BarIconSet("1downarrow"));
  122. down->setTextLabel(i18n("Move Item &Down"), true);
  123. vbox->addWidget(down);
  124. m_buttons.insert(BColDown, down);
  125. connect(down, TQT_SIGNAL(clicked()), this, TQT_SLOT(MoveItemDown()));
  126. vbox->addStretch();
  127. // The listbox with columns name /////
  128. m_listbox = new KListBox(m_column, "editlistview_columns");
  129. m_listbox->setFocus();
  130. hbox->insertWidget(0, m_listbox);
  131. hbox->addWidget(m_editor);
  132. connect(m_listbox, TQT_SIGNAL(currentChanged(TQListBoxItem*)), this, TQT_SLOT(updateItemProperties(TQListBoxItem*)));
  133. //// Init dialog and display it ////////////////////////
  134. setInitialSize(TQSize(500, 300), true);
  135. }
  136. int
  137. EditListViewDialog::exec(TQListView *listview)
  138. {
  139. if(!listview)
  140. {
  141. kdDebug() << "EditListViewDialog ERROR: no listview " << endl;
  142. return 0;
  143. }
  144. // We copy the contents of the listview into our listview
  145. for(int i = 0; i < listview->columns(); i++)
  146. {
  147. m_listview->addColumn(listview->columnText(i), listview->columnWidth(i));
  148. m_listview->header()->setClickEnabled(listview->header()->isClickEnabled(i), i);
  149. m_listview->header()->setResizeEnabled(listview->header()->isResizeEnabled(i), i);
  150. m_listview->header()->setStretchEnabled(listview->header()->isStretchEnabled(i), i);
  151. m_listview->setRenameable(i, true);
  152. }
  153. TQListViewItem *item = listview->firstChild();
  154. while(item) {
  155. loadChildNodes(m_listview, item, 0);
  156. item = item->nextSibling();
  157. }
  158. m_listview->setSelected(m_listview->firstChild(), true);
  159. if(!m_listview->firstChild())
  160. updateButtons(0);
  161. for(int i = 0; i < listview->columns(); i++)
  162. m_listbox->insertItem(listview->columnText(i));
  163. m_listbox->setSelected(0, true);
  164. // and we exec the dialog
  165. int r = KDialogBase::exec();
  166. if(r == TQDialog::Accepted)
  167. {
  168. listview->clear();
  169. // We copy the contents of our listview back in the listview
  170. for(int i = 0; i < m_listview->columns(); i++)
  171. {
  172. if(listview->columns() <= i)
  173. listview->addColumn(m_listview->columnText(i), m_listview->columnWidth(i));
  174. else
  175. {
  176. listview->setColumnText(i, m_listview->columnText(i));
  177. listview->setColumnWidth(i, m_listview->columnWidth(i));
  178. }
  179. listview->header()->setClickEnabled(m_listview->header()->isClickEnabled(i), i);
  180. listview->header()->setResizeEnabled(m_listview->header()->isResizeEnabled(i), i);
  181. listview->header()->setStretchEnabled(m_listview->header()->isStretchEnabled(i), i);
  182. }
  183. TQListViewItem *item = m_listview->firstChild();
  184. while(item)
  185. {
  186. loadChildNodes(listview, item, 0);
  187. item = item->nextSibling();
  188. }
  189. }
  190. return r;
  191. }
  192. /// Columns page slots ///////
  193. void
  194. EditListViewDialog::changeProperty(KoProperty::Set& set, KoProperty::Property& property)
  195. {
  196. if(&set != m_propSet)
  197. return;
  198. TQString name = property.name();
  199. TQVariant value = property.value();
  200. if(name == "caption") {
  201. m_propSet->blockSignals(true); // we need to block signals because changeItem will modify selection, and call updateItemProperties
  202. m_listbox->changeItem(value.toString(), m_listbox->currentItem());
  203. m_listview->setColumnText(m_listbox->currentItem(), value.toString());
  204. m_propSet->blockSignals(false);
  205. }
  206. else if(name == "width")
  207. m_listview->setColumnWidth(m_listbox->currentItem(), value.toInt());
  208. else if(name == "resizable")
  209. m_listview->header()->setResizeEnabled(value.toBool(), m_listbox->currentItem());
  210. else if(name == "clickable")
  211. m_listview->header()->setClickEnabled(value.toBool(), m_listbox->currentItem());
  212. else if(name == "fullwidth")
  213. m_listview->header()->setStretchEnabled(value.toBool(), m_listbox->currentItem());
  214. }
  215. void
  216. EditListViewDialog::updateItemProperties(TQListBoxItem *item)
  217. {
  218. if(!item)
  219. return;
  220. int id = m_listbox->index(item);
  221. if(m_propSet) {
  222. m_propSet->blockSignals(true); // we don't want changeProperty to be called
  223. (*m_propSet)["caption"].setValue(m_listview->columnText(id), false);
  224. (*m_propSet)["width"].setValue(m_listview->columnWidth(id), false);
  225. (*m_propSet)["clickable"].setValue(TQVariant(m_listview->header()->isClickEnabled(id), 4), false);
  226. (*m_propSet)["resizable"].setValue(TQVariant(m_listview->header()->isResizeEnabled(id), 4), false);
  227. (*m_propSet)["fullwidth"].setValue(TQVariant(m_listview->header()->isStretchEnabled(id), 4), false);
  228. m_propSet->blockSignals(false);
  229. m_editor->changeSet(m_propSet);
  230. }
  231. m_buttons[BColUp]->setEnabled(item->prev());
  232. m_buttons[BColDown]->setEnabled(item->next());
  233. }
  234. void
  235. EditListViewDialog::newItem()
  236. {
  237. m_listbox->insertItem(i18n("New Column"));
  238. m_listview->addColumn(i18n("New Column"));
  239. m_listview->setRenameable(m_listview->columns() - 1, true);
  240. m_listbox->setCurrentItem(m_listbox->count() - 1);
  241. m_buttons[BColRem]->setEnabled(true);
  242. }
  243. void
  244. EditListViewDialog::removeItem()
  245. {
  246. int current = m_listbox->currentItem();
  247. if(m_listbox->item(current + 1))
  248. m_listbox->setCurrentItem(current +1);
  249. else
  250. m_listbox->setCurrentItem(current - 1);
  251. m_listview->removeColumn(current);
  252. m_listbox->removeItem(current);
  253. if(m_listbox->count() == 0)
  254. m_buttons[BColRem]->setEnabled(false);
  255. }
  256. void
  257. EditListViewDialog::MoveItemUp()
  258. {
  259. int current = m_listbox->currentItem();
  260. TQString text = m_listbox->text(current);
  261. m_listbox->blockSignals(true);
  262. m_listbox->changeItem(m_listbox->text(current - 1), current);
  263. m_listview->setColumnText(current, m_listview->columnText(current - 1));
  264. m_listview->setColumnWidth(current, m_listview->columnWidth(current - 1));
  265. m_listview->header()->setClickEnabled(m_listview->header()->isClickEnabled(current - 1), current);
  266. m_listview->header()->setResizeEnabled(m_listview->header()->isResizeEnabled(current - 1), current);
  267. m_listview->header()->setStretchEnabled(m_listview->header()->isStretchEnabled(current - 1), current);
  268. m_listbox->changeItem(text, current - 1);
  269. m_listview->setColumnText(current - 1, (*m_propSet)["caption"].value().toString());
  270. m_listview->setColumnWidth(current - 1,(*m_propSet)["width"].value().toBool());
  271. m_listview->header()->setClickEnabled((*m_propSet)["clickable"].value().toBool(), current - 1);
  272. m_listview->header()->setResizeEnabled((*m_propSet)["resizable"].value().toBool(), current - 1);
  273. m_listview->header()->setStretchEnabled((*m_propSet)["fullwidth"].value().toBool(), current - 1);
  274. m_listbox->blockSignals(false);
  275. m_listbox->setCurrentItem(current - 1);
  276. }
  277. void
  278. EditListViewDialog::MoveItemDown()
  279. {
  280. int current = m_listbox->currentItem();
  281. TQString text = m_listbox->text(current);
  282. m_listbox->blockSignals(true);
  283. m_listbox->changeItem(m_listbox->text(current+1), current);
  284. m_listview->setColumnText(current, m_listview->columnText(current + 1));
  285. m_listview->setColumnWidth(current, m_listview->columnWidth(current + 1));
  286. m_listview->header()->setClickEnabled(m_listview->header()->isClickEnabled(current + 1), current);
  287. m_listview->header()->setResizeEnabled(m_listview->header()->isResizeEnabled(current + 1), current);
  288. m_listview->header()->setStretchEnabled(m_listview->header()->isStretchEnabled(current + 1), current);
  289. m_listbox->changeItem(text, current+1);
  290. m_listview->setColumnText(current + 1, (*m_propSet)["caption"].value().toString());
  291. m_listview->setColumnWidth(current + 1,(*m_propSet)["width"].value().toBool());
  292. m_listview->header()->setClickEnabled((*m_propSet)["clickable"].value().toBool(), current + 1);
  293. m_listview->header()->setResizeEnabled((*m_propSet)["resizable"].value().toBool(), current + 1);
  294. m_listview->header()->setStretchEnabled((*m_propSet)["fullwidth"].value().toBool(), current + 1);
  295. m_listbox->blockSignals(false);
  296. m_listbox->setCurrentItem(current + 1);
  297. }
  298. /// Contents page slots ////////
  299. void
  300. EditListViewDialog::updateButtons(TQListViewItem *item)
  301. {
  302. if(!item)
  303. {
  304. for(int i = BNewChild; i <= BRowDown; i++)
  305. m_buttons[i]->setEnabled(false);
  306. return;
  307. }
  308. m_buttons[BNewChild]->setEnabled(true);
  309. m_buttons[BRemRow]->setEnabled(true);
  310. m_buttons[BRowUp]->setEnabled( (item->itemAbove() && (item->itemAbove()->parent() == item->parent())) );
  311. m_buttons[BRowDown]->setEnabled(item->nextSibling());
  312. }
  313. void
  314. EditListViewDialog::loadChildNodes(TQListView *listview, TQListViewItem *item, TQListViewItem *parent)
  315. {
  316. TQListViewItem *newItem;
  317. if(listview->inherits("KListView"))
  318. {
  319. if(parent)
  320. newItem = new KListViewItem(parent);
  321. else
  322. newItem = new KListViewItem(listview);
  323. }
  324. else
  325. {
  326. if(parent)
  327. newItem = new TQListViewItem(parent);
  328. else
  329. newItem = new TQListViewItem(listview);
  330. }
  331. // We need to move the item at the end, which is the expected behaviour (by default it is inserted at the beginning)
  332. TQListViewItem *last;
  333. if(parent)
  334. last = parent->firstChild();
  335. else
  336. last = listview->firstChild();
  337. while(last->nextSibling())
  338. last = last->nextSibling();
  339. newItem->moveItem(last);
  340. // We copy the text of all the columns
  341. for(int i = 0; i < listview->columns(); i++)
  342. newItem->setText(i, item->text(i));
  343. TQListViewItem *child = item->firstChild();
  344. if(child)
  345. newItem->setOpen(true);
  346. while(child) {
  347. loadChildNodes(listview, child, newItem);
  348. child = child->nextSibling();
  349. }
  350. }
  351. void
  352. EditListViewDialog::newRow()
  353. {
  354. KListViewItem *parent = (KListViewItem*)m_listview->selectedItem();
  355. if(parent)
  356. parent = (KListViewItem*)parent->parent();
  357. KListViewItem *item;
  358. if(parent)
  359. item = new KListViewItem(parent, m_listview->selectedItem());
  360. else
  361. item = new KListViewItem(m_listview, m_listview->selectedItem());
  362. item->setText(0, i18n("New Item"));
  363. m_listview->setCurrentItem(item);
  364. }
  365. void
  366. EditListViewDialog::newChildRow()
  367. {
  368. KListViewItem *parent = (KListViewItem*)m_listview->currentItem();
  369. KListViewItem *item;
  370. if(parent)
  371. item = new KListViewItem(parent);
  372. else
  373. item = new KListViewItem(m_listview, m_listview->currentItem());
  374. item->setText(0, i18n("Sub Item"));
  375. m_listview->setCurrentItem(item);
  376. parent->setOpen(true);
  377. }
  378. void
  379. EditListViewDialog::removeRow()
  380. {
  381. delete m_listview->currentItem();
  382. }
  383. void
  384. EditListViewDialog::MoveRowUp()
  385. {
  386. TQListViewItem *item = m_listview->currentItem()->itemAbove();
  387. item->moveItem(m_listview->currentItem());
  388. updateButtons(m_listview->currentItem());
  389. }
  390. void
  391. EditListViewDialog::MoveRowDown()
  392. {
  393. TQListViewItem *before = m_listview->currentItem();
  394. before->moveItem(before->nextSibling());
  395. updateButtons(before);
  396. }
  397. }
  398. #include "editlistviewdialog.moc"