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 Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "treenode.h"
00026 #include "treenodeitem.h"
00027 #include "folderitem.h"
00028 #include <qfont.h>
00029 #include <qheader.h>
00030 #include <qpainter.h>
00031 #include <qstring.h>
00032 
00033 #include <kstringhandler.h>
00034 
00035 #include <kdebug.h>
00036 
00037 namespace Akregator {
00038 
00039 TreeNodeItem::TreeNodeItem(FolderItem* parent, TreeNode* node)
00040     : KListViewItem(parent), m_node(node)
00041 {
00042     initialize(node);
00043 }
00044 
00045 TreeNodeItem::TreeNodeItem(KListView* parent, TreeNode* node) 
00046     : KListViewItem(parent), m_node(node)
00047 {
00048     initialize(node);
00049 }
00050 
00051 TreeNodeItem::TreeNodeItem(KListView* parent, TreeNodeItem* after, TreeNode* node) : KListViewItem(parent, after), m_node(node)
00052 {
00053     initialize(node);
00054 }
00055 
00056 TreeNodeItem::TreeNodeItem(FolderItem* parent, TreeNodeItem* after, TreeNode* node)
00057     : KListViewItem(parent, after), m_node(node)
00058 {
00059     initialize(node);
00060 }
00061 
00062 void TreeNodeItem::initialize(TreeNode* node)
00063 {
00064     setRenameEnabled(0, true);
00065     if (node)
00066         setText(0, node->title() );
00067 }
00068 
00069 TreeNodeItem::~TreeNodeItem()
00070 {}
00071 
00072 QString TreeNodeItem::toolTip() const
00073 {
00074     return QString::null;
00075 }
00076 
00077 TreeNode* TreeNodeItem::node()
00078 { 
00079     return m_node; 
00080 }
00081 
00082 void TreeNodeItem::nodeChanged()
00083 {
00084 //    kdDebug() << "enter TreeNodeItem::nodeChanged item" << text(0) << endl; 
00085     if (!node())
00086         return;
00087     if (text(0) != node()->title())
00088         setText(0, node()->title());
00089 //    kdDebug() << "leave TreeNodeItem::nodeChanged item" << text(0) << endl; 
00090 }
00091 
00092 TreeNodeItem* TreeNodeItem::firstChild() const 
00093 { 
00094     return static_cast<TreeNodeItem*>(KListViewItem::firstChild()); 
00095 } 
00096 
00097 TreeNodeItem* TreeNodeItem::nextSibling() const 
00098 { 
00099     return static_cast<TreeNodeItem*>(KListViewItem::nextSibling()); 
00100 } 
00101 
00102 FolderItem* TreeNodeItem::parent() const 
00103 { 
00104     return static_cast<FolderItem*>(KListViewItem::parent()); 
00105 } 
00106     
00107 
00108 // TODO: reverse for reverse layout
00109 void TreeNodeItem::paintCell( QPainter * p, const QColorGroup & cg,
00110                                int column, int width, int align )
00111 
00112 {
00113     int u = node() ? node()->unread() : 0;
00114 
00115     if (u <= 0)
00116     {
00117         KListViewItem::paintCell(p,cg,column,width,align);
00118         return;
00119     }
00120 
00121     // from kfoldertree
00122     QString oldText = text(column);
00123     setText( column, " " );
00124 
00125     // draw bg
00126     KListViewItem::paintCell(p,cg,column,width,align);
00127 
00128     setText( column, oldText);
00129 
00130     // draw fg
00131     QFont f = p->font();
00132     f.setWeight(QFont::Bold);
00133     p->setFont(f);
00134 
00135     QFontMetrics fm( p->fontMetrics() );
00136     QListView *lv = listView();
00137     int x = lv ? lv->itemMargin() : 1;
00138     int m=x;
00139     const QPixmap *icon = pixmap( column );
00140     QRect br;
00141 
00142     if (icon)
00143         x += icon->width() + m;
00144 
00145     QString txt = " (" + QString::number(u) + ")";
00146     int txtW=fm.width( txt );
00147 
00148     if (fm.width( oldText ) + txtW + x > width)
00149         oldText=KStringHandler::rPixelSqueeze(oldText,fm, width - txtW - x);
00150 
00151     p->drawText( x, 0, width-m-x, height(), align | AlignVCenter, oldText, -1, &br );
00152 
00153     if ( !isSelected() )
00154         p->setPen( Qt::blue ); // TODO: configurable
00155 
00156     p->drawText( br.right(), 0, width-m-br.right(), height(),
00157                  align | AlignVCenter, txt );
00158 
00159     /*if ( isSelected() )
00160     p->setPen( cg.highlightedText() );
00161     else
00162     p->setPen( cg.text() );*/
00163 }
00164 
00165 } // namespace Akregator
KDE Home | KDE Accessibility Home | Description of Access Keys