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.

150 lines
4.3KB

  1. /*
  2. * resultlistview.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 <kdebug.h>
  20. #include <kpopupmenu.h>
  21. #include <klocale.h>
  22. #include <tqclipboard.h>
  23. #include <tqapplication.h>
  24. #include <tqevent.h>
  25. #include <tqcursor.h>
  26. #include <tqdragobject.h>
  27. #include <tqheader.h>
  28. #include "resultlistview.h"
  29. #include "resultlistviewtext.h"
  30. #include "dragsupport.h"
  31. using DragSupport::makePixmap;
  32. using namespace ResultList;
  33. ResultListView::ResultListView(TQWidget *parent, const char *name) :
  34. TDEListView(parent, name), m_itemRightClicked(0)
  35. {
  36. connect(this, TQT_SIGNAL(doubleClicked(TQListViewItem *, const TQPoint &, int)),
  37. TQT_SLOT(slotDoubleClicked(TQListViewItem *, const TQPoint &, int)));
  38. addColumn(i18n("Expression"));
  39. addColumn(i18n("Result"));
  40. addColumn(i18n("Shortcut"));
  41. header()->hide(); // I hate that header
  42. header()->setStretchEnabled(ResultColumn, true);
  43. setDragEnabled(true);
  44. setItemMargin(2);
  45. setColumnAlignment(ResultColumn, AlignLeft);
  46. setColumnAlignment(ShortcutColumn, AlignHCenter);
  47. setColumnWidthMode(ResultColumn, Maximum);
  48. setSortColumn(-1);
  49. }
  50. bool ResultListView::getStackValue(unsigned stackPosition, Abakus::number_t &result)
  51. {
  52. TQListViewItem *it = firstChild();
  53. for(; it; it = it->itemBelow()) {
  54. ResultListViewText *resultItem = dynamic_cast<ResultListViewText *>(it);
  55. if(!resultItem->wasError() && resultItem->stackPosition() == stackPosition) {
  56. result = Abakus::number_t(resultItem->resultText().latin1());
  57. return true;
  58. }
  59. }
  60. return false;
  61. }
  62. TQDragObject *ResultListView::dragObject()
  63. {
  64. TQPoint viewportPos = viewport()->mapFromGlobal(TQCursor::pos());
  65. ResultListViewText *item = itemUnderCursor();
  66. if(item) {
  67. TQString text = item->resultText();
  68. int column = header()->sectionAt(viewportPos.x());
  69. if(column == ExpressionColumn)
  70. text = item->expressionText();
  71. TQDragObject *drag = new TQTextDrag(text, this);
  72. drag->setPixmap(makePixmap(text, font()));
  73. return drag;
  74. }
  75. return 0;
  76. }
  77. void ResultListView::contextMenuEvent(TQContextMenuEvent *e)
  78. {
  79. m_itemRightClicked = itemUnderCursor();
  80. TDEPopupMenu *menu = constructPopupMenu(m_itemRightClicked);
  81. menu->popup(e->globalPos());
  82. }
  83. void ResultListView::slotDoubleClicked(TQListViewItem *item, const TQPoint &, int c)
  84. {
  85. ResultListViewText *textItem = dynamic_cast<ResultListViewText *>(item);
  86. if(!textItem)
  87. return;
  88. if(c == ExpressionColumn)
  89. emit signalEntrySelected(textItem->expressionText());
  90. else if(c == ResultColumn)
  91. emit signalResultSelected(textItem->resultText());
  92. }
  93. TDEPopupMenu *ResultListView::constructPopupMenu(const ResultListViewText *item)
  94. {
  95. TDEPopupMenu *menu = new TDEPopupMenu(this, "list view context menu");
  96. menu->insertItem(i18n("Clear &History"), this, TQT_SLOT(clear()), ALT+Key_R);
  97. int id = menu->insertItem(i18n("Copy Result to Clipboard"), this, TQT_SLOT(slotCopyResult()));
  98. if(!item || item->wasError())
  99. menu->setItemEnabled(id, false);
  100. return menu;
  101. }
  102. void ResultListView::slotCopyResult()
  103. {
  104. if(!m_itemRightClicked)
  105. return;
  106. TQClipboard *clipboard = TQApplication::clipboard();
  107. clipboard->setText(m_itemRightClicked->resultText(), TQClipboard::Clipboard);
  108. }
  109. ResultListViewText *ResultListView::lastItem() const
  110. {
  111. return static_cast<ResultListViewText *>(TDEListView::lastItem());
  112. }
  113. ResultListViewText *ResultListView::itemUnderCursor() const
  114. {
  115. TQPoint viewportPos = viewport()->mapFromGlobal(TQCursor::pos());
  116. TQListViewItem *underCursor = itemAt(viewportPos);
  117. return static_cast<ResultListViewText *>(underCursor);
  118. }
  119. #include "resultlistview.moc"