3 #include "kfoldertree.h" 5 #include <tdeio/global.h> 6 #include <kiconloader.h> 8 #include <kstringhandler.h> 10 #include <tqapplication.h> 15 KFolderTreeItem::KFolderTreeItem( KFolderTree *parent,
const TQString & label,
16 Protocol protocol, Type type )
17 : TDEListViewItem( parent, label ), mProtocol( protocol ), mType( type ),
18 mUnread(-1), mTotal(0), mSize(0), mFolderIsCloseToQuota( false )
23 KFolderTreeItem::KFolderTreeItem( KFolderTreeItem *parent,
24 const TQString & label, Protocol protocol, Type type,
25 int unread,
int total )
26 : TDEListViewItem( parent, label ), mProtocol( protocol ), mType( type ),
27 mUnread( unread ), mTotal( total ), mSize(0), mFolderIsCloseToQuota( false )
32 int KFolderTreeItem::protocolSortingKey()
const 36 switch ( mProtocol ) {
52 int KFolderTreeItem::typeSortingKey()
const 85 int KFolderTreeItem::compare( TQListViewItem * i,
int col,
bool )
const 87 KFolderTreeItem* other =
static_cast<KFolderTreeItem*
>( i );
94 if ( depth() == 0 && mProtocol == NONE )
96 if ( other->depth() == 0 && other->protocol() == NONE )
100 int thisKey = protocolSortingKey();
101 int thatKey = other->protocolSortingKey();
102 if ( thisKey < thatKey )
104 if ( thisKey > thatKey )
108 thisKey = typeSortingKey();
109 thatKey = other->typeSortingKey();
110 if ( thisKey < thatKey )
112 if ( thisKey > thatKey )
116 return text( 0 ).localeAwareCompare( other->text( 0 ) );
121 TQ_INT64 a = 0, b = 0;
122 if (col == static_cast<KFolderTree*>(listView())->unreadIndex())
125 b = other->unreadCount();
127 else if (col == static_cast<KFolderTree*>(listView())->totalIndex())
130 b = other->totalCount();
132 else if (col == static_cast<KFolderTree*>(listView())->sizeIndex())
135 b = other->folderSize();
141 return (a < b ? -1 : 1);
146 void KFolderTreeItem::setUnreadCount(
int aUnread )
148 if ( aUnread < 0 )
return;
152 TQString unread = TQString();
156 unread.setNum(mUnread);
160 setText( static_cast<KFolderTree*>(listView())->unreadIndex(),
165 void KFolderTreeItem::setTotalCount(
int aTotal )
167 if ( aTotal < 0 )
return;
171 TQString total = TQString();
175 total.setNum(mTotal);
179 setText( static_cast<KFolderTree*>(listView())->totalIndex(),
184 void KFolderTreeItem::setFolderSize( TQ_INT64 aSize )
186 if ( aSize < 0 )
return;
192 if (mSize == 0 && (childCount() == 0 || isOpen() ) )
195 size = TDEIO::convertSize(mSize);
197 if ( childCount() > 0 && !isOpen() ) {
198 TQ_INT64 recursiveSize = recursiveFolderSize();
199 if ( recursiveSize != mSize ) {
201 size += TQString::fromLatin1(
" + %1").arg( TDEIO::convertSize( recursiveSize - mSize ) );
203 size = TDEIO::convertSize( recursiveSize );
208 setText( static_cast<KFolderTree*>(listView())->sizeIndex(), size );
212 TQ_INT64 KFolderTreeItem::recursiveFolderSize()
const 214 TQ_INT64 size = mSize;
216 for ( TQListViewItem *item = firstChild() ;
217 item ; item = item->nextSibling() )
219 size +=
static_cast<KFolderTreeItem*
>(item)->recursiveFolderSize();
227 int KFolderTreeItem::countUnreadRecursive()
229 int count = (mUnread > 0) ? mUnread : 0;
231 for ( TQListViewItem *item = firstChild() ;
232 item ; item = item->nextSibling() )
234 count +=
static_cast<KFolderTreeItem*
>(item)->countUnreadRecursive();
241 void KFolderTreeItem::paintCell( TQPainter * p,
const TQColorGroup & cg,
242 int column,
int width,
int align )
244 KFolderTree *ft =
static_cast<KFolderTree*
>(listView());
246 const int unreadRecursiveCount = countUnreadRecursive();
247 const int unreadCount = ( mUnread > 0 ) ? mUnread : 0;
251 TQColorGroup mycg = cg;
252 if ( ( column == 0 || column == ft->sizeIndex() ) && folderIsCloseToQuota() )
254 mycg.setColor( TQColorGroup::Text, ft->paintInfo().colCloseToQuota );
258 if ( (column == 0 || column == ft->unreadIndex())
260 || ( !isOpen() && unreadRecursiveCount > 0 ) ) )
262 TQFont f = p->font();
263 f.setWeight(TQFont::Bold);
276 if ( ft->isUnreadActive() || column != 0 ) {
277 TDEListViewItem::paintCell( p, mycg, column, width, align );
279 TQListView *lv = listView();
280 TQString oldText = text(column);
284 setText( column,
"" );
286 TDEListViewItem::paintCell( p, mycg, column, width, align );
288 const TQPixmap *icon = pixmap( column );
289 int marg = lv ? lv->itemMargin() : 1;
292 setText( column, oldText );
294 p->setPen( mycg.highlightedText() );
296 p->setPen( mycg.color( TQColorGroup::Text ) );
299 r += icon->width() + marg;
301 TQString t = text( column );
308 if ( unreadCount > 0 || ( !isOpen() && unreadRecursiveCount > 0 ) ) {
310 unread =
" (" + TQString::number( unreadCount ) +
")";
311 else if ( unreadRecursiveCount == unreadCount || mType == Root )
312 unread =
" (" + TQString::number( unreadRecursiveCount ) +
")";
314 unread =
" (" + TQString::number( unreadCount ) +
" + " +
315 TQString::number( unreadRecursiveCount-unreadCount ) +
")";
319 TQFontMetrics fm( p->fontMetrics() );
320 int unreadWidth = fm.width( unread );
321 if ( fm.width( t ) + marg + r + unreadWidth > width )
322 t = squeezeFolderName( t, fm, width - marg - r - unreadWidth );
325 p->drawText( r, 0, width-marg-r, height(),
326 align | AlignVCenter, t, -1, &br );
328 if ( !unread.isEmpty() ) {
330 p->setPen( ft->paintInfo().colUnread );
331 p->drawText( br.right(), 0, width-marg-br.right(), height(),
332 align | AlignVCenter, unread );
338 TQString KFolderTreeItem::squeezeFolderName(
const TQString &text,
339 const TQFontMetrics &fm,
342 return KStringHandler::rPixelSqueeze( text, fm, width );
345 bool KFolderTreeItem::folderIsCloseToQuota()
const 347 return mFolderIsCloseToQuota;
350 void KFolderTreeItem::setFolderIsCloseToQuota(
bool v )
352 if ( mFolderIsCloseToQuota != v) {
353 mFolderIsCloseToQuota = v;
362 KFolderTree::KFolderTree( TQWidget *parent,
const char* name )
363 : TDEListView( parent, name ), mUnreadIndex(-1), mTotalIndex(-1), mSizeIndex(-1)
366 setStyleDependantFrameWidth();
367 setAcceptDrops(
true);
368 setDropVisualizer(
false);
369 setAllColumnsShowFocus(
true);
370 setShowSortIndicator(
true);
371 setUpdatesEnabled(
true);
372 setItemsRenameable(
false);
373 setRootIsDecorated(
true);
374 setSelectionModeExt(Extended);
375 setAlternateBackground(TQColor());
376 #if KDE_IS_VERSION( 3, 3, 90 ) 377 setShadeSortColumn (
false );
380 disableAutoSelection();
381 setColumnWidth( 0, 120 );
383 disconnect( header(), TQT_SIGNAL( sizeChange(
int,
int,
int ) ) );
384 connect( header(), TQT_SIGNAL( sizeChange(
int,
int,
int ) ),
385 TQT_SLOT( slotSizeChanged(
int,
int,
int ) ) );
389 void KFolderTree::setStyleDependantFrameWidth()
393 if( style().isA(
"KeramikStyle") )
394 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth ) - 1;
396 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth );
397 if ( frameWidth < 0 )
399 if ( frameWidth != lineWidth() )
400 setLineWidth( frameWidth );
404 void KFolderTree::styleChange( TQStyle& oldStyle )
406 setStyleDependantFrameWidth();
407 TDEListView::styleChange( oldStyle );
411 void KFolderTree::drawContentsOffset( TQPainter * p,
int ox,
int oy,
412 int cx,
int cy,
int cw,
int ch )
414 bool oldUpdatesEnabled = isUpdatesEnabled();
415 setUpdatesEnabled(
false);
416 TDEListView::drawContentsOffset( p, ox, oy, cx, cy, cw, ch );
417 setUpdatesEnabled(oldUpdatesEnabled);
421 void KFolderTree::contentsMousePressEvent( TQMouseEvent *e )
423 setSelectionModeExt(Single);
424 TDEListView::contentsMousePressEvent(e);
428 void KFolderTree::contentsMouseReleaseEvent( TQMouseEvent *e )
430 TDEListView::contentsMouseReleaseEvent(e);
431 setSelectionModeExt(Extended);
435 void KFolderTree::addAcceptableDropMimetype(
const char *mimeType,
bool outsideOk )
437 int oldSize = mAcceptableDropMimetypes.size();
438 mAcceptableDropMimetypes.resize(oldSize+1);
439 mAcceptOutside.resize(oldSize+1);
441 mAcceptableDropMimetypes.at(oldSize) = mimeType;
442 mAcceptOutside.setBit(oldSize, outsideOk);
446 bool KFolderTree::acceptDrag( TQDropEvent* event )
const 448 TQListViewItem* item = itemAt(contentsToViewport(event->pos()));
450 for (uint i = 0; i < mAcceptableDropMimetypes.size(); i++)
452 if (event->provides(mAcceptableDropMimetypes[i]))
455 return (static_cast<KFolderTreeItem*>(item))->acceptDrag(event);
457 return mAcceptOutside[i];
464 void KFolderTree::addUnreadColumn(
const TQString & name,
int width )
466 mUnreadIndex = addColumn( name, width );
467 setColumnAlignment( mUnreadIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight );
468 header()->adjustHeaderSize();
472 void KFolderTree::addTotalColumn(
const TQString & name,
int width )
474 mTotalIndex = addColumn( name, width );
475 setColumnAlignment( mTotalIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight );
476 header()->adjustHeaderSize();
480 void KFolderTree::removeUnreadColumn()
482 if ( !isUnreadActive() )
return;
483 removeColumn( mUnreadIndex );
484 if ( isTotalActive() && mTotalIndex > mUnreadIndex )
486 if ( isSizeActive() && mSizeIndex > mUnreadIndex )
490 header()->adjustHeaderSize();
494 void KFolderTree::removeTotalColumn()
496 if ( !isTotalActive() )
return;
497 removeColumn( mTotalIndex );
498 if ( isUnreadActive() && mTotalIndex < mUnreadIndex )
500 if ( isSizeActive() && mTotalIndex < mSizeIndex )
503 header()->adjustHeaderSize();
507 void KFolderTree::addSizeColumn(
const TQString & name,
int width )
509 mSizeIndex = addColumn( name, width );
510 setColumnAlignment( mSizeIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight );
511 header()->adjustHeaderSize();
515 void KFolderTree::removeSizeColumn()
517 if ( !isSizeActive() )
return;
518 removeColumn( mSizeIndex );
519 if ( isUnreadActive() && mSizeIndex < mUnreadIndex )
521 if ( isTotalActive() && mSizeIndex < mTotalIndex )
524 header()->adjustHeaderSize();
529 void KFolderTree::setFullWidth(
bool fullWidth )
532 header()->setStretchEnabled(
true, 0 );
536 void KFolderTree::slotSizeChanged(
int section,
int,
int newSize )
539 header()->sectionPos(section), 0, newSize, visibleHeight(),
false );
542 #include "kfoldertree.moc"