SVN_SILENT made messages (.desktop file)
[kdepim.git] / knode / knhdrviewitem.cpp
blobe0fa77c94b486ed492fc1b7694f0432482cfaccb
1 /*
2 KNode, the KDE newsreader
3 Copyright (c) 1999-2006 the KNode authors.
4 See file AUTHORS for details
5 Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 You should have received a copy of the GNU General Public License
12 along with this program; if not, write to the Free Software Foundation,
13 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
16 #include <QDrag>
17 #include <QPainter>
18 #include <QPixmap>
20 #include <kdebug.h>
22 #include "knglobals.h"
23 #include "knconfigmanager.h"
24 #include "knhdrviewitem.h"
25 #include "knarticle.h"
26 #include "headerview.h"
27 #include "settings.h"
30 KNHdrViewItem::KNHdrViewItem( KNHeaderView *ref, KNArticle::Ptr a ) :
31 K3ListViewItem( ref )
33 init( a );
37 KNHdrViewItem::KNHdrViewItem( KNHdrViewItem *ref, KNArticle::Ptr a ) :
38 K3ListViewItem( ref )
40 init( a );
44 void KNHdrViewItem::init( KNArticle::Ptr a )
46 art = a;
47 mActive = false;
48 for ( int i = 0; i < 5; ++i) // FIXME hardcoded column count
49 mShowToolTip[i] = false;
53 KNHdrViewItem::~KNHdrViewItem()
55 if (mActive) {
56 Q3ListView *lv = listView();
57 if (lv)
58 static_cast<KNHeaderView*>( lv )->activeRemoved();
61 if (art) {
62 art->setListItem( 0, art );
67 void KNHdrViewItem::expandChildren()
69 Q3ListViewItemIterator it( firstChild() );
70 for ( ; it.current(); ++it) {
71 if (it.current()->depth() <= depth())
72 break;
73 it.current()->setOpen( true );
78 int KNHdrViewItem::compare( Q3ListViewItem *i, int col, bool ) const
80 KNArticle::Ptr otherArticle = static_cast<KNHdrViewItem*>( i )->art;
81 int diff = 0;
82 time_t date1 = 0, date2 = 0;
84 switch (col) {
85 case 0:
86 case 1:
87 return text( col ).localeAwareCompare( i->text(col) );
89 case 2:
90 if (art->type() == KNArticle::ATremote) {
91 diff = boost::static_pointer_cast<KNRemoteArticle>( art )->score() - boost::static_pointer_cast<KNRemoteArticle>( otherArticle )->score();
92 return (diff < 0 ? -1 : diff > 0 ? 1 : 0);
93 } else
94 return 0;
96 case 3:
97 diff = art->lines()->numberOfLines() - otherArticle->lines()->numberOfLines();
98 return (diff < 0 ? -1 : diff > 0 ? 1 : 0);
100 case 4:
101 date1 = art->date()->dateTime().toTime_t();
102 date2 = otherArticle->date()->dateTime().toTime_t();
103 if (art->type() == KNArticle::ATremote && static_cast<KNHeaderView*>( listView() )->sortByThreadChangeDate()) {
104 if ( boost::static_pointer_cast<KNRemoteArticle>( art )->subThreadChangeDate() > date1 )
105 date1 = boost::static_pointer_cast<KNRemoteArticle>( art )->subThreadChangeDate();
106 if ( boost::static_pointer_cast<KNRemoteArticle>( otherArticle )->subThreadChangeDate() > date2 ) {
107 date2 = boost::static_pointer_cast<KNRemoteArticle>( otherArticle )->subThreadChangeDate();
110 diff = date1 - date2;
111 return (diff < 0 ? -1 : diff > 0 ? 1 : 0);
113 default:
114 return 0;
119 void KNHdrViewItem::paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int alignment )
121 int xText = 0, xPM = 3, yPM = 0;
122 QColor base;
123 const KPaintInfo *paintInfo = static_cast<KNHeaderView*>( listView() )->paintInfo();
125 QPen pen = p->pen();
126 if (isSelected() || mActive) {
127 pen.setColor( cg.color( QPalette::HighlightedText ) );
128 base = cg.color( QPalette::Highlight );
129 } else {
130 if (greyOut())
131 pen.setColor( greyColor() );
132 else
133 pen.setColor( normalColor() );
134 base = backgroundColor( column );
137 p->setPen( pen );
139 p->fillRect( 0, 0, width, height(), QBrush(base) );
141 if ( column == paintInfo->subCol ) {
142 QFont font = p->font();
143 font.setBold( firstColBold() );
144 p->setFont( font );
145 const QPixmap *pm;
147 for (int i = 0; i < 4; ++i) {
148 pm = pixmap( i );
149 if (pm && !pm->isNull()) {
150 yPM = (height() - pm->height()) / 2;
151 p->drawPixmap( xPM, yPM, *pm );
152 xPM += pm->width() + 3;
156 xText = xPM;
159 if (width - xText - 5 > 0) {
160 int cntWidth = 0;
161 QString t2;
162 QFont f2;
163 if (countUnreadInThread() > 0 && column == paintInfo->subCol && !isOpen()) {
164 t2 = QString( " (%1)" ).arg( countUnreadInThread() );
165 f2 = p->font();
166 f2.setBold( true );
167 cntWidth = QFontMetrics( f2 ).width( t2, -1 );
169 QString t = p->fontMetrics().elidedText( text( column ), Qt::ElideRight, width - xText - cntWidth - 5 );
171 // show tooltip if we have to squeeze the text
172 if ( t != text( column ) )
173 mShowToolTip[column] = true;
174 else
175 mShowToolTip[column] = false;
177 p->drawText( xText, 0, width - xText - 5, height(), alignment | Qt::AlignVCenter, t );
178 if (cntWidth) {
179 QFont orig = p->font();
180 p->setFont( f2 );
181 QPen pen = p->pen();
182 if (isSelected() || mActive) {
183 pen.setColor( cg.color( QPalette::HighlightedText ) );
184 } else {
185 pen.setColor( cg.color( QPalette::Link ) );
187 p->setPen( pen );
188 p->drawText( xText + QFontMetrics( orig ).width( t, -1 ), 0, width - xText - 5, height(), alignment | Qt::AlignVCenter, t2 );
194 int KNHdrViewItem::width( const QFontMetrics &fm, const Q3ListView *, int column ) const
196 int ret = fm.boundingRect( text(column) ).width();
197 const KPaintInfo *paintInfo = static_cast<KNHeaderView*>( listView() )->paintInfo();
199 // all pixmaps are drawn in the first column
200 if ( column == paintInfo->subCol ) {
201 const QPixmap *pm;
202 for (int i = 0; i < 4; ++i) {
203 pm = pixmap( i );
204 if (pm && !pm->isNull())
205 ret += pm->width() + 3;
209 return ret;
213 QString KNHdrViewItem::text( int col ) const
215 if ( !art )
216 return QString();
217 KNHeaderView *hv = static_cast<KNHeaderView*>( listView() );
219 if ( col == hv->paintInfo()->subCol ) {
220 return art->subject()->asUnicodeString();
223 if ( col == hv->paintInfo()->sizeCol ) {
224 if ( art->lines()->numberOfLines() != 0 ) { // invalid values are read as '0' in KNGroup::insortHeaders()
225 return QString::number( art->lines()->numberOfLines() );
226 } else {
227 return QString();
231 if ( col == hv->paintInfo()->scoreCol ) {
232 if ( art->type() == KNArticle::ATremote )
233 return QString::number( boost::static_pointer_cast<KNRemoteArticle>( art )->score() );
234 else
235 return QString();
238 if ( col == hv->paintInfo()->dateCol ) {
239 return hv->mDateFormatter.dateString( art->date()->dateTime().toTime_t() );
240 } else
241 return K3ListViewItem::text( col );
245 Q3DragObject* KNHdrViewItem::dragObject()
247 #ifdef __GNUC__
248 #warning Enable this section again, once KNHdrView does not derive from K3ListView any more and can process QDrag (not Q3DragObject)
249 #endif
250 return 0;
251 #if 0
252 QDrag *drag = new QDrag( listView()->viewport() );
253 QMimeData *md = new QMimeData;
254 drag->setMimeData( md );
256 KUrl::List list;
257 QString mid = art->messageID()->asUnicodeString();
258 // for some obscure reason it returns messageid in <>s
259 mid = mid.mid( 1, mid.length() - 2 );
260 list.append( QLatin1String( "news:" ) + mid );
261 QMap<QString,QString> metadata;
262 metadata["labels"] = KUrl::toPercentEncoding( art->subject()->asUnicodeString() );
263 list.populateMimeData( md, metadata );
264 md->setData( "x-knode-drag/article" , QByteArray() );
266 drag->setPixmap( knGlobals.configManager()->appearance()->icon( KNode::Appearance::posting ) );
267 return drag;
268 #endif
272 int KNHdrViewItem::countUnreadInThread()
274 int count = 0;
275 if ( knGlobals.settings()->showUnread() ) {
276 if (art->type() == KNArticle::ATremote) {
277 count = boost::static_pointer_cast<KNRemoteArticle>( art )->unreadFollowUps();
280 return count;
284 bool KNHdrViewItem::greyOut()
286 if (art->type() == KNArticle::ATremote) {
287 return !boost::static_pointer_cast<KNRemoteArticle>( art )->hasUnreadFollowUps()
288 && boost::static_pointer_cast<KNRemoteArticle>( art )->isRead();
289 } else
290 return false;
294 bool KNHdrViewItem::firstColBold()
296 if(art->type() == KNArticle::ATremote)
297 return boost::static_pointer_cast<KNRemoteArticle>( art )->isNew();
298 else
299 return false;
303 QColor KNHdrViewItem::normalColor()
305 if (art->type()==KNArticle::ATremote)
306 return boost::static_pointer_cast<KNRemoteArticle>( art )->color();
307 else
308 return knGlobals.settings()->unreadThreadColor();
312 QColor KNHdrViewItem::greyColor()
314 return knGlobals.settings()->readThreadColor();