AbaKus – a complex calculator
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.

233 lines
5.7KB

  1. /*
  2. * abakuslistview.cpp - part of abakus
  3. * Copyright (C) 2004, 2005 Michael Pyne <michael.pyne@kdemail.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <klocale.h>
  20. #include <kpopupmenu.h>
  21. #include <kdebug.h>
  22. #include <tqdragobject.h>
  23. #include <tqcursor.h>
  24. #include <tqheader.h>
  25. #include "dragsupport.h"
  26. #include "abakuslistview.h"
  27. #include "valuemanager.h"
  28. #include "function.h"
  29. ListView::ListView(TQWidget *parent, const char *name) :
  30. TDEListView(parent, name), m_menu(0), m_usePopup(false), m_removeSingleId(0),
  31. m_removeAllId(0)
  32. {
  33. setResizeMode(LastColumn);
  34. setDragEnabled(true);
  35. connect(this, TQT_SIGNAL(contextMenuRequested(TQListViewItem *, const TQPoint &, int)),
  36. TQT_SLOT(rightClicked(TQListViewItem *, const TQPoint &)));
  37. }
  38. TQDragObject *ListView::dragObject()
  39. {
  40. TQPoint viewportPos = viewport()->mapFromGlobal(TQCursor::pos());
  41. TQListViewItem *item = itemAt(viewportPos);
  42. if(!item)
  43. return 0;
  44. int column = header()->sectionAt(viewportPos.x());
  45. TQString dragText = item->text(column);
  46. TQDragObject *drag = new TQTextDrag(dragText, this, "list item drag");
  47. drag->setPixmap(DragSupport::makePixmap(dragText, font()));
  48. return drag;
  49. }
  50. void ListView::enablePopupHandler(bool enable)
  51. {
  52. if(enable == m_usePopup)
  53. return;
  54. m_usePopup = enable;
  55. if(m_usePopup) {
  56. if(m_menu)
  57. kdError() << "ListView menu shouldn't exist here!\n";
  58. m_menu = new TDEPopupMenu(this);
  59. m_removeSingleId = m_menu->insertItem(removeItemString(), this, TQT_SLOT(removeSelected()));
  60. m_removeAllId = m_menu->insertItem("Placeholder", this, TQT_SLOT(removeAllItems()));
  61. }
  62. else {
  63. delete m_menu;
  64. m_menu = 0;
  65. }
  66. }
  67. TQString ListView::removeItemString() const
  68. {
  69. return TQString();
  70. }
  71. TQString ListView::removeAllItemsString(unsigned count) const
  72. {
  73. Q_UNUSED(count);
  74. return TQString();
  75. }
  76. void ListView::removeSelectedItem(TQListViewItem *item)
  77. {
  78. Q_UNUSED(item);
  79. }
  80. void ListView::removeAllItems()
  81. {
  82. }
  83. bool ListView::isItemRemovable(TQListViewItem *item) const
  84. {
  85. Q_UNUSED(item);
  86. return false;
  87. }
  88. void ListView::rightClicked(TQListViewItem *item, const TQPoint &pt)
  89. {
  90. if(!m_usePopup)
  91. return;
  92. m_menu->setItemEnabled(m_removeSingleId, item && isItemRemovable(item));
  93. m_menu->changeItem(m_removeAllId, removeAllItemsString(childCount()));
  94. m_menu->popup(pt);
  95. }
  96. void ListView::removeSelected()
  97. {
  98. removeSelectedItem(selectedItem());
  99. }
  100. ValueListViewItem::ValueListViewItem(TQListView *listView, const TQString &name,
  101. const Abakus::number_t &value) :
  102. TDEListViewItem(listView, name), m_value(value)
  103. {
  104. valueChanged();
  105. }
  106. void ValueListViewItem::valueChanged()
  107. {
  108. setText(1, m_value.toString());
  109. repaint();
  110. }
  111. void ValueListViewItem::valueChanged(const Abakus::number_t &newValue)
  112. {
  113. m_value = newValue;
  114. valueChanged();
  115. }
  116. Abakus::number_t ValueListViewItem::itemValue() const
  117. {
  118. return m_value;
  119. }
  120. VariableListView::VariableListView(TQWidget *parent, const char *name) :
  121. ListView(parent, name)
  122. {
  123. enablePopupHandler(true);
  124. }
  125. TQString VariableListView::removeItemString() const
  126. {
  127. return i18n("Remove selected variable");
  128. }
  129. TQString VariableListView::removeAllItemsString(unsigned count) const
  130. {
  131. // count is unreliable because not all of the elements in the list view
  132. // can be removed.
  133. count = 0;
  134. TQStringList values = ValueManager::instance()->valueNames();
  135. for(TQStringList::ConstIterator value = values.constBegin(); value != values.constEnd(); ++value)
  136. if(!ValueManager::instance()->isValueReadOnly(*value))
  137. ++count;
  138. return i18n("Remove all variables (1 variable)",
  139. "Remove all variables (%n variables)",
  140. count);
  141. }
  142. bool VariableListView::isItemRemovable(TQListViewItem *item) const
  143. {
  144. return !ValueManager::instance()->isValueReadOnly(item->text(0));
  145. }
  146. void VariableListView::removeSelectedItem(TQListViewItem *item)
  147. {
  148. ValueManager::instance()->removeValue(item->text(0));
  149. }
  150. void VariableListView::removeAllItems()
  151. {
  152. ValueManager::instance()->slotRemoveUserVariables();
  153. }
  154. FunctionListView::FunctionListView(TQWidget *parent, const char *name) :
  155. ListView(parent, name)
  156. {
  157. enablePopupHandler(true);
  158. }
  159. TQString FunctionListView::removeItemString() const
  160. {
  161. return i18n("Remove selected function");
  162. }
  163. TQString FunctionListView::removeAllItemsString(unsigned count) const
  164. {
  165. return i18n("Remove all functions (1 function)",
  166. "Remove all functions (%n functions)",
  167. count);
  168. }
  169. bool FunctionListView::isItemRemovable(TQListViewItem *item) const
  170. {
  171. return true;
  172. }
  173. void FunctionListView::removeSelectedItem(TQListViewItem *item)
  174. {
  175. // Use section to get the beginning of the string up to (and not
  176. // including) the first (
  177. TQString name = item->text(0).section('(', 0, 0);
  178. FunctionManager::instance()->removeFunction(name);
  179. }
  180. void FunctionListView::removeAllItems()
  181. {
  182. TQStringList fns = FunctionManager::instance()->functionList(FunctionManager::UserDefined);
  183. for(TQStringList::ConstIterator fn = fns.constBegin(); fn != fns.constEnd(); ++fn)
  184. FunctionManager::instance()->removeFunction(*fn);
  185. }
  186. #include "abakuslistview.moc"