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.

136 lines
4.0KB

  1. /*
  2. * resultlistviewtext.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 <tqregexp.h>
  21. #include <tqpainter.h>
  22. #include <tqfontmetrics.h>
  23. #include <tqfont.h>
  24. #include <tqpalette.h>
  25. #include "resultlistviewtext.h"
  26. using namespace ResultList;
  27. ResultListViewText::ResultListViewText(TDEListView *listView,
  28. const TQString &text,
  29. const TQString &result,
  30. ResultListViewText *after,
  31. bool isError)
  32. : TDEListViewItem(listView, after, text, result), m_text(text),
  33. m_result(result), m_wasError(isError), m_stackPosition(0)
  34. {
  35. // This is some kind of non-result answer, don't bother worrying about the
  36. // stack status, it hasn't changed.
  37. }
  38. ResultListViewText::ResultListViewText(TDEListView *listView,
  39. const TQString &text,
  40. const Abakus::number_t &result,
  41. ResultListViewText *after,
  42. bool isError)
  43. : TDEListViewItem(listView, after, text), m_text(text),
  44. m_result(result.toString()), m_wasError(isError), m_stackPosition(0),
  45. m_value(result)
  46. {
  47. if(after) {
  48. ResultListViewText *item = static_cast<ResultListViewText *>(listView->firstChild());
  49. for (; item && item != this; item = static_cast<ResultListViewText *>(item->itemBelow())) {
  50. if(!item->wasError()) {
  51. item->setStackPosition(item->stackPosition() + 1);
  52. item->repaint();
  53. }
  54. }
  55. }
  56. setStackPosition(0);
  57. // Call this manually to be rid of trailing zeroes.
  58. setText(ResultColumn, m_value.toString());
  59. }
  60. void ResultListViewText::setStackPosition(unsigned pos)
  61. {
  62. setText(ShortcutColumn, TQString("$%1").arg(pos));
  63. m_stackPosition = pos;
  64. }
  65. void ResultListViewText::precisionChanged()
  66. {
  67. if(m_wasError)
  68. return;
  69. m_result = m_value.toString();
  70. setText(ResultColumn, m_result);
  71. }
  72. void ResultListViewText::paintCell(TQPainter *p, const TQColorGroup &cg, int column, int width, int align)
  73. {
  74. TQColorGroup group(cg);
  75. // XXX: The TQt::red may not provide good contrast with weird color schemes.
  76. // If so I apologize.
  77. if(m_wasError && column == ResultColumn)
  78. group.setColor(TQColorGroup::Text, m_result == "OK" ? cg.link() : TQt::red);
  79. if(column == ResultColumn) {
  80. TQFont f = p->font();
  81. f.setBold(true);
  82. p->setFont(f);
  83. }
  84. if(column == ShortcutColumn) {
  85. TQFont f = p->font();
  86. f.setItalic(true);
  87. f.setPointSize(TQMIN(f.pointSize() * 9 / 11, 10));
  88. p->setFont(f);
  89. }
  90. TDEListViewItem::paintCell(p, group, column, width, align);
  91. }
  92. int ResultListViewText::width(const TQFontMetrics &fm, const TQListView *lv, int c) const
  93. {
  94. // Simulate painting the text to get accurate results.
  95. if(c == ResultColumn) {
  96. TQFont f = lv->font();
  97. f.setBold(true);
  98. return TDEListViewItem::width(TQFontMetrics(f), lv, c);
  99. }
  100. if(c == ShortcutColumn) {
  101. TQFont f = lv->font();
  102. f.setItalic(true);
  103. f.setPointSize(TQMIN(f.pointSize() * 9 / 11, 10));
  104. return TDEListViewItem::width(TQFontMetrics(f), lv, c);
  105. }
  106. return TDEListViewItem::width(fm, lv, c);
  107. }
  108. void ResultListViewText::setText(int column, const TQString &text)
  109. {
  110. if(!m_wasError && column == ResultColumn) {
  111. TDEListViewItem::setText(column, m_value.toString());
  112. return;
  113. }
  114. TDEListViewItem::setText(column, text);
  115. }