akregator/src

treenodeitem.cpp
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2004 Frank Osterfeld <frank.osterfeld at kdemail.net>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include "treenode.h"
26 #include "treenodeitem.h"
27 #include "folderitem.h"
28 #include <tqfont.h>
29 #include <tqheader.h>
30 #include <tqpainter.h>
31 #include <tqstring.h>
32 
33 #include <kstringhandler.h>
34 
35 #include <kdebug.h>
36 
37 namespace Akregator {
38 
39 TreeNodeItem::TreeNodeItem(FolderItem* parent, TreeNode* node)
40  : KListViewItem(parent), m_node(node)
41 {
42  initialize(node);
43 }
44 
45 TreeNodeItem::TreeNodeItem(KListView* parent, TreeNode* node)
46  : KListViewItem(parent), m_node(node)
47 {
48  initialize(node);
49 }
50 
51 TreeNodeItem::TreeNodeItem(KListView* parent, TreeNodeItem* after, TreeNode* node) : KListViewItem(parent, after), m_node(node)
52 {
53  initialize(node);
54 }
55 
56 TreeNodeItem::TreeNodeItem(FolderItem* parent, TreeNodeItem* after, TreeNode* node)
57  : KListViewItem(parent, after), m_node(node)
58 {
59  initialize(node);
60 }
61 
62 void TreeNodeItem::initialize(TreeNode* node)
63 {
64  setRenameEnabled(0, true);
65  if (node)
66  setText(0, node->title() );
67 }
68 
69 TreeNodeItem::~TreeNodeItem()
70 {}
71 
72 TQString TreeNodeItem::toolTip() const
73 {
74  return TQString();
75 }
76 
77 TreeNode* TreeNodeItem::node()
78 {
79  return m_node;
80 }
81 
82 void TreeNodeItem::nodeChanged()
83 {
84 // kdDebug() << "enter TreeNodeItem::nodeChanged item" << text(0) << endl;
85  if (!node())
86  return;
87  if (text(0) != node()->title())
88  setText(0, node()->title());
89 // kdDebug() << "leave TreeNodeItem::nodeChanged item" << text(0) << endl;
90 }
91 
92 TreeNodeItem* TreeNodeItem::firstChild() const
93 {
94  return static_cast<TreeNodeItem*>(KListViewItem::firstChild());
95 }
96 
97 TreeNodeItem* TreeNodeItem::nextSibling() const
98 {
99  return static_cast<TreeNodeItem*>(KListViewItem::nextSibling());
100 }
101 
102 FolderItem* TreeNodeItem::parent() const
103 {
104  return static_cast<FolderItem*>(KListViewItem::parent());
105 }
106 
107 
108 // TODO: reverse for reverse layout
109 void TreeNodeItem::paintCell( TQPainter * p, const TQColorGroup & cg,
110  int column, int width, int align )
111 
112 {
113  int u = node() ? node()->unread() : 0;
114 
115  if (u <= 0)
116  {
117  KListViewItem::paintCell(p,cg,column,width,align);
118  return;
119  }
120 
121  // from kfoldertree
122  TQString oldText = text(column);
123  setText( column, " " );
124 
125  // draw bg
126  KListViewItem::paintCell(p,cg,column,width,align);
127 
128  setText( column, oldText);
129 
130  // draw fg
131  TQFont f = p->font();
132  f.setWeight(TQFont::Bold);
133  p->setFont(f);
134 
135  TQFontMetrics fm( p->fontMetrics() );
136  TQListView *lv = listView();
137  int x = lv ? lv->itemMargin() : 1;
138  int m=x;
139  const TQPixmap *icon = pixmap( column );
140  TQRect br;
141 
142  if (icon)
143  x += icon->width() + m;
144 
145  TQString txt = " (" + TQString::number(u) + ")";
146  int txtW=fm.width( txt );
147 
148  if (fm.width( oldText ) + txtW + x > width)
149  oldText=KStringHandler::rPixelSqueeze(oldText,fm, width - txtW - x);
150 
151  p->drawText( x, 0, width-m-x, height(), align | AlignVCenter, oldText, -1, &br );
152 
153  if ( !isSelected() )
154  p->setPen( TQt::blue ); // TODO: configurable
155 
156  p->drawText( br.right(), 0, width-m-br.right(), height(),
157  align | AlignVCenter, txt );
158 
159  /*if ( isSelected() )
160  p->setPen( cg.highlightedText() );
161  else
162  p->setPen( cg.text() );*/
163 }
164 
165 } // namespace Akregator