akregator/src

treenodeitem.cpp
00001 /*
00002     This file is part of Akregator.
00003 
00004     Copyright (C) 2004 Frank Osterfeld <frank.osterfeld at kdemail.net>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of TQt, and distribute the resulting executable,
00022     without including the source code for TQt in the source distribution.
00023 */
00024 
00025 #include "akregatorconfig.h"
00026 
00027 #include "treenode.h"
00028 #include "treenodeitem.h"
00029 #include "folderitem.h"
00030 #include <tqfont.h>
00031 #include <tqheader.h>
00032 #include <tqpainter.h>
00033 #include <tqstring.h>
00034 
00035 #include <kstringhandler.h>
00036 
00037 #include <kdebug.h>
00038 
00039 namespace Akregator {
00040 
00041 TreeNodeItem::TreeNodeItem(FolderItem* parent, TreeNode* node)
00042     : TDEListViewItem(parent), m_node(node)
00043 {
00044     initialize(node);
00045 }
00046 
00047 TreeNodeItem::TreeNodeItem(TDEListView* parent, TreeNode* node) 
00048     : TDEListViewItem(parent), m_node(node)
00049 {
00050     initialize(node);
00051 }
00052 
00053 TreeNodeItem::TreeNodeItem(TDEListView* parent, TreeNodeItem* after, TreeNode* node) : TDEListViewItem(parent, after), m_node(node)
00054 {
00055     initialize(node);
00056 }
00057 
00058 TreeNodeItem::TreeNodeItem(FolderItem* parent, TreeNodeItem* after, TreeNode* node)
00059     : TDEListViewItem(parent, after), m_node(node)
00060 {
00061     initialize(node);
00062 }
00063 
00064 void TreeNodeItem::initialize(TreeNode* node)
00065 {
00066     setRenameEnabled(0, true);
00067     if (node)
00068         setText(0, node->title() );
00069 }
00070 
00071 TreeNodeItem::~TreeNodeItem()
00072 {}
00073 
00074 TQString TreeNodeItem::toolTip() const
00075 {
00076     return TQString();
00077 }
00078 
00079 TreeNode* TreeNodeItem::node()
00080 { 
00081     return m_node; 
00082 }
00083 
00084 void TreeNodeItem::nodeChanged()
00085 {
00086 //    kdDebug() << "enter TreeNodeItem::nodeChanged item" << text(0) << endl; 
00087     if (!node())
00088         return;
00089     if (text(0) != node()->title())
00090         setText(0, node()->title());
00091 //    kdDebug() << "leave TreeNodeItem::nodeChanged item" << text(0) << endl; 
00092 }
00093 
00094 TreeNodeItem* TreeNodeItem::firstChild() const 
00095 { 
00096     return static_cast<TreeNodeItem*>(TDEListViewItem::firstChild()); 
00097 } 
00098 
00099 TreeNodeItem* TreeNodeItem::nextSibling() const 
00100 { 
00101     return static_cast<TreeNodeItem*>(TDEListViewItem::nextSibling()); 
00102 } 
00103 
00104 FolderItem* TreeNodeItem::parent() const 
00105 { 
00106     return static_cast<FolderItem*>(TDEListViewItem::parent()); 
00107 } 
00108     
00109 
00110 // TODO: reverse for reverse layout
00111 void TreeNodeItem::paintCell( TQPainter * p, const TQColorGroup & cg,
00112                                int column, int width, int align )
00113 
00114 {
00115     int u = node() ? node()->unread() : 0;
00116 
00117     if (u <= 0)
00118     {
00119         TDEListViewItem::paintCell(p,cg,column,width,align);
00120         return;
00121     }
00122 
00123     // from kfoldertree
00124     TQString oldText = text(column);
00125     setText( column, " " );
00126 
00127     // draw bg
00128     TDEListViewItem::paintCell(p,cg,column,width,align);
00129 
00130     setText( column, oldText);
00131 
00132     // draw fg
00133     TQFont f = p->font();
00134     f.setWeight(TQFont::Bold);
00135     p->setFont(f);
00136 
00137     TQFontMetrics fm( p->fontMetrics() );
00138     TQListView *lv = listView();
00139     int x = lv ? lv->itemMargin() : 1;
00140     int m=x;
00141     const TQPixmap *icon = pixmap( column );
00142     TQRect br;
00143 
00144     if (icon)
00145         x += icon->width() + m;
00146 
00147     TQString txt = " (" + TQString::number(u) + ")";
00148     int txtW=fm.width( txt );
00149 
00150     if (fm.width( oldText ) + txtW + x > width)
00151         oldText=KStringHandler::rPixelSqueeze(oldText,fm, width - txtW - x);
00152 
00153     p->drawText( x, 0, width-m-x, height(), align | AlignVCenter, oldText, -1, &br );
00154 
00155     if ( !isSelected() )
00156         p->setPen( Settings::unreadTextColor() );
00157 
00158     p->drawText( br.right(), 0, width-m-br.right(), height(),
00159                  align | AlignVCenter, txt );
00160 
00161     /*if ( isSelected() )
00162     p->setPen( cg.highlightedText() );
00163     else
00164     p->setPen( cg.text() );*/
00165 }
00166 
00167 } // namespace Akregator