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
22 #include "knglobals.h"
23 #include "knconfigmanager.h"
24 #include "knhdrviewitem.h"
25 #include "knarticle.h"
26 #include "headerview.h"
30 KNHdrViewItem::KNHdrViewItem( KNHeaderView
*ref
, KNArticle::Ptr a
) :
37 KNHdrViewItem::KNHdrViewItem( KNHdrViewItem
*ref
, KNArticle::Ptr a
) :
44 void KNHdrViewItem::init( KNArticle::Ptr a
)
48 for ( int i
= 0; i
< 5; ++i
) // FIXME hardcoded column count
49 mShowToolTip
[i
] = false;
53 KNHdrViewItem::~KNHdrViewItem()
56 Q3ListView
*lv
= listView();
58 static_cast<KNHeaderView
*>( lv
)->activeRemoved();
62 art
->setListItem( 0, art
);
67 void KNHdrViewItem::expandChildren()
69 Q3ListViewItemIterator
it( firstChild() );
70 for ( ; it
.current(); ++it
) {
71 if (it
.current()->depth() <= depth())
73 it
.current()->setOpen( true );
78 int KNHdrViewItem::compare( Q3ListViewItem
*i
, int col
, bool ) const
80 KNArticle::Ptr otherArticle
= static_cast<KNHdrViewItem
*>( i
)->art
;
82 time_t date1
= 0, date2
= 0;
87 return text( col
).localeAwareCompare( i
->text(col
) );
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);
97 diff
= art
->lines()->numberOfLines() - otherArticle
->lines()->numberOfLines();
98 return (diff
< 0 ? -1 : diff
> 0 ? 1 : 0);
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);
119 void KNHdrViewItem::paintCell( QPainter
*p
, const QColorGroup
&cg
, int column
, int width
, int alignment
)
121 int xText
= 0, xPM
= 3, yPM
= 0;
123 const KPaintInfo
*paintInfo
= static_cast<KNHeaderView
*>( listView() )->paintInfo();
126 if (isSelected() || mActive
) {
127 pen
.setColor( cg
.color( QPalette::HighlightedText
) );
128 base
= cg
.color( QPalette::Highlight
);
131 pen
.setColor( greyColor() );
133 pen
.setColor( normalColor() );
134 base
= backgroundColor( column
);
139 p
->fillRect( 0, 0, width
, height(), QBrush(base
) );
141 if ( column
== paintInfo
->subCol
) {
142 QFont font
= p
->font();
143 font
.setBold( firstColBold() );
147 for (int i
= 0; i
< 4; i
++) {
149 if (pm
&& !pm
->isNull()) {
150 yPM
= (height() - pm
->height()) / 2;
151 p
->drawPixmap( xPM
, yPM
, *pm
);
152 xPM
+= pm
->width() + 3;
159 if (width
- xText
- 5 > 0) {
163 if (countUnreadInThread() > 0 && column
== paintInfo
->subCol
&& !isOpen()) {
164 t2
= QString( " (%1)" ).arg( countUnreadInThread() );
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;
175 mShowToolTip
[column
] = false;
177 p
->drawText( xText
, 0, width
- xText
- 5, height(), alignment
| Qt::AlignVCenter
, t
);
179 QFont orig
= p
->font();
182 if (isSelected() || mActive
) {
183 pen
.setColor( cg
.color( QPalette::HighlightedText
) );
185 pen
.setColor( cg
.color( QPalette::Link
) );
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
) {
202 for (int i
= 0; i
< 4; ++i
) {
204 if (pm
&& !pm
->isNull())
205 ret
+= pm
->width() + 3;
213 QString
KNHdrViewItem::text( int col
) const
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() );
231 if ( col
== hv
->paintInfo()->scoreCol
) {
232 if ( art
->type() == KNArticle::ATremote
)
233 return QString::number( boost::static_pointer_cast
<KNRemoteArticle
>( art
)->score() );
238 if ( col
== hv
->paintInfo()->dateCol
) {
239 return hv
->mDateFormatter
.dateString( art
->date()->dateTime().toTime_t() );
241 return K3ListViewItem::text( col
);
245 Q3DragObject
* KNHdrViewItem::dragObject()
248 #warning Enable this section again, once KNHdrView does not derive from K3ListView any more and can process QDrag (not Q3DragObject)
252 QDrag
*drag
= new QDrag( listView()->viewport() );
253 QMimeData
*md
= new QMimeData
;
254 drag
->setMimeData( md
);
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
) );
272 int KNHdrViewItem::countUnreadInThread()
275 if ( knGlobals
.settings()->showUnread() ) {
276 if (art
->type() == KNArticle::ATremote
) {
277 count
= boost::static_pointer_cast
<KNRemoteArticle
>( art
)->unreadFollowUps();
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();
294 bool KNHdrViewItem::firstColBold()
296 if(art
->type() == KNArticle::ATremote
)
297 return boost::static_pointer_cast
<KNRemoteArticle
>( art
)->isNew();
303 QColor
KNHdrViewItem::normalColor()
305 if (art
->type()==KNArticle::ATremote
)
306 return boost::static_pointer_cast
<KNRemoteArticle
>( art
)->color();
308 return knGlobals
.settings()->unreadThreadColor();
312 QColor
KNHdrViewItem::greyColor()
314 return knGlobals
.settings()->readThreadColor();