1 #include "kfoldertree.h" 3 #include <tdeio/global.h> 4 #include <kiconloader.h> 6 #include <kstringhandler.h> 8 #include <tqapplication.h> 13 KFolderTreeItem::KFolderTreeItem( KFolderTree *parent,
const TQString & label,
14 Protocol protocol, Type type )
15 : TDEListViewItem( parent, label ), mProtocol( protocol ), mType( type ),
16 mUnread(-1), mTotal(0), mSize(0), mFolderIsCloseToQuota( false )
21 KFolderTreeItem::KFolderTreeItem( KFolderTreeItem *parent,
22 const TQString & label, Protocol protocol, Type type,
23 int unread,
int total )
24 : TDEListViewItem( parent, label ), mProtocol( protocol ), mType( type ),
25 mUnread( unread ), mTotal( total ), mSize(0), mFolderIsCloseToQuota( false )
30 int KFolderTreeItem::protocolSortingKey()
const 34 switch ( mProtocol ) {
50 int KFolderTreeItem::typeSortingKey()
const 83 int KFolderTreeItem::compare( TQListViewItem * i,
int col,
bool )
const 85 KFolderTreeItem* other =
static_cast<KFolderTreeItem*
>( i );
92 if ( depth() == 0 && mProtocol == NONE )
94 if ( other->depth() == 0 && other->protocol() == NONE )
98 int thisKey = protocolSortingKey();
99 int thatKey = other->protocolSortingKey();
100 if ( thisKey < thatKey )
102 if ( thisKey > thatKey )
106 thisKey = typeSortingKey();
107 thatKey = other->typeSortingKey();
108 if ( thisKey < thatKey )
110 if ( thisKey > thatKey )
114 return text( 0 ).localeAwareCompare( other->text( 0 ) );
119 TQ_INT64 a = 0, b = 0;
120 if (col == static_cast<KFolderTree*>(listView())->unreadIndex())
123 b = other->unreadCount();
125 else if (col == static_cast<KFolderTree*>(listView())->totalIndex())
128 b = other->totalCount();
130 else if (col == static_cast<KFolderTree*>(listView())->sizeIndex())
133 b = other->folderSize();
139 return (a < b ? -1 : 1);
144 void KFolderTreeItem::setUnreadCount(
int aUnread )
146 if ( aUnread < 0 )
return;
150 TQString unread = TQString();
154 unread.setNum(mUnread);
158 setText( static_cast<KFolderTree*>(listView())->unreadIndex(),
163 void KFolderTreeItem::setTotalCount(
int aTotal )
165 if ( aTotal < 0 )
return;
169 TQString total = TQString();
173 total.setNum(mTotal);
177 setText( static_cast<KFolderTree*>(listView())->totalIndex(),
182 void KFolderTreeItem::setFolderSize( TQ_INT64 aSize )
184 if ( aSize < 0 )
return;
190 if (mSize == 0 && (childCount() == 0 || isOpen() ) )
193 size = TDEIO::convertSize(mSize);
195 if ( childCount() > 0 && !isOpen() ) {
196 TQ_INT64 recursiveSize = recursiveFolderSize();
197 if ( recursiveSize != mSize ) {
199 size += TQString::fromLatin1(
" + %1").arg( TDEIO::convertSize( recursiveSize - mSize ) );
201 size = TDEIO::convertSize( recursiveSize );
206 setText( static_cast<KFolderTree*>(listView())->sizeIndex(), size );
210 TQ_INT64 KFolderTreeItem::recursiveFolderSize()
const 212 TQ_INT64 size = mSize;
214 for ( TQListViewItem *item = firstChild() ;
215 item ; item = item->nextSibling() )
217 size +=
static_cast<KFolderTreeItem*
>(item)->recursiveFolderSize();
225 int KFolderTreeItem::countUnreadRecursive()
227 int count = (mUnread > 0) ? mUnread : 0;
229 for ( TQListViewItem *item = firstChild() ;
230 item ; item = item->nextSibling() )
232 count +=
static_cast<KFolderTreeItem*
>(item)->countUnreadRecursive();
239 void KFolderTreeItem::paintCell( TQPainter * p,
const TQColorGroup & cg,
240 int column,
int width,
int align )
242 KFolderTree *ft =
static_cast<KFolderTree*
>(listView());
244 const int unreadRecursiveCount = countUnreadRecursive();
245 const int unreadCount = ( mUnread > 0 ) ? mUnread : 0;
249 TQColorGroup mycg = cg;
250 if ( ( column == 0 || column == ft->sizeIndex() ) && folderIsCloseToQuota() )
252 mycg.setColor( TQColorGroup::Text, ft->paintInfo().colCloseToQuota );
256 if ( (column == 0 || column == ft->unreadIndex())
258 || ( !isOpen() && unreadRecursiveCount > 0 ) ) )
260 TQFont f = p->font();
261 f.setWeight(TQFont::Bold);
274 if ( ft->isUnreadActive() || column != 0 ) {
275 TDEListViewItem::paintCell( p, mycg, column, width, align );
277 TQListView *lv = listView();
278 TQString oldText = text(column);
282 setText( column,
"" );
284 TDEListViewItem::paintCell( p, mycg, column, width, align );
286 const TQPixmap *icon = pixmap( column );
287 int marg = lv ? lv->itemMargin() : 1;
290 setText( column, oldText );
292 p->setPen( mycg.highlightedText() );
294 p->setPen( mycg.color( TQColorGroup::Text ) );
297 r += icon->width() + marg;
299 TQString t = text( column );
306 if ( unreadCount > 0 || ( !isOpen() && unreadRecursiveCount > 0 ) ) {
308 unread =
" (" + TQString::number( unreadCount ) +
")";
309 else if ( unreadRecursiveCount == unreadCount || mType == Root )
310 unread =
" (" + TQString::number( unreadRecursiveCount ) +
")";
312 unread =
" (" + TQString::number( unreadCount ) +
" + " +
313 TQString::number( unreadRecursiveCount-unreadCount ) +
")";
317 TQFontMetrics fm( p->fontMetrics() );
318 int unreadWidth = fm.width( unread );
319 if ( fm.width( t ) + marg + r + unreadWidth > width )
320 t = squeezeFolderName( t, fm, width - marg - r - unreadWidth );
323 p->drawText( r, 0, width-marg-r, height(),
324 align | AlignVCenter, t, -1, &br );
326 if ( !unread.isEmpty() ) {
328 p->setPen( ft->paintInfo().colUnread );
329 p->drawText( br.right(), 0, width-marg-br.right(), height(),
330 align | AlignVCenter, unread );
336 TQString KFolderTreeItem::squeezeFolderName(
const TQString &text,
337 const TQFontMetrics &fm,
340 return KStringHandler::rPixelSqueeze( text, fm, width );
343 bool KFolderTreeItem::folderIsCloseToQuota()
const 345 return mFolderIsCloseToQuota;
348 void KFolderTreeItem::setFolderIsCloseToQuota(
bool v )
350 if ( mFolderIsCloseToQuota != v) {
351 mFolderIsCloseToQuota = v;
360 KFolderTree::KFolderTree( TQWidget *parent,
const char* name )
361 : TDEListView( parent, name ), mUnreadIndex(-1), mTotalIndex(-1), mSizeIndex(-1)
364 setStyleDependantFrameWidth();
365 setAcceptDrops(
true);
366 setDropVisualizer(
false);
367 setAllColumnsShowFocus(
true);
368 setShowSortIndicator(
true);
369 setUpdatesEnabled(
true);
370 setItemsRenameable(
false);
371 setRootIsDecorated(
true);
372 setSelectionModeExt(Extended);
373 setAlternateBackground(TQColor());
374 #if KDE_IS_VERSION( 3, 3, 90 ) 375 setShadeSortColumn (
false );
378 disableAutoSelection();
379 setColumnWidth( 0, 120 );
381 disconnect( header(), TQT_SIGNAL( sizeChange(
int,
int,
int ) ) );
382 connect( header(), TQT_SIGNAL( sizeChange(
int,
int,
int ) ),
383 TQT_SLOT( slotSizeChanged(
int,
int,
int ) ) );
387 void KFolderTree::setStyleDependantFrameWidth()
391 if( style().isA(
"KeramikStyle") )
392 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth ) - 1;
394 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth );
395 if ( frameWidth < 0 )
397 if ( frameWidth != lineWidth() )
398 setLineWidth( frameWidth );
402 void KFolderTree::styleChange( TQStyle& oldStyle )
404 setStyleDependantFrameWidth();
405 TDEListView::styleChange( oldStyle );
409 void KFolderTree::drawContentsOffset( TQPainter * p,
int ox,
int oy,
410 int cx,
int cy,
int cw,
int ch )
412 bool oldUpdatesEnabled = isUpdatesEnabled();
413 setUpdatesEnabled(
false);
414 TDEListView::drawContentsOffset( p, ox, oy, cx, cy, cw, ch );
415 setUpdatesEnabled(oldUpdatesEnabled);
419 void KFolderTree::contentsMousePressEvent( TQMouseEvent *e )
421 setSelectionModeExt(Single);
422 TDEListView::contentsMousePressEvent(e);
426 void KFolderTree::contentsMouseReleaseEvent( TQMouseEvent *e )
428 TDEListView::contentsMouseReleaseEvent(e);
429 setSelectionModeExt(Extended);
433 void KFolderTree::addAcceptableDropMimetype(
const char *mimeType,
bool outsideOk )
435 int oldSize = mAcceptableDropMimetypes.size();
436 mAcceptableDropMimetypes.resize(oldSize+1);
437 mAcceptOutside.resize(oldSize+1);
439 mAcceptableDropMimetypes.at(oldSize) = mimeType;
440 mAcceptOutside.setBit(oldSize, outsideOk);
444 bool KFolderTree::acceptDrag( TQDropEvent* event )
const 446 TQListViewItem* item = itemAt(contentsToViewport(event->pos()));
448 for (uint i = 0; i < mAcceptableDropMimetypes.size(); i++)
450 if (event->provides(mAcceptableDropMimetypes[i]))
453 return (static_cast<KFolderTreeItem*>(item))->acceptDrag(event);
455 return mAcceptOutside[i];
462 void KFolderTree::addUnreadColumn(
const TQString & name,
int width )
464 mUnreadIndex = addColumn( name, width );
465 setColumnAlignment( mUnreadIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight );
466 header()->adjustHeaderSize();
470 void KFolderTree::addTotalColumn(
const TQString & name,
int width )
472 mTotalIndex = addColumn( name, width );
473 setColumnAlignment( mTotalIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight );
474 header()->adjustHeaderSize();
478 void KFolderTree::removeUnreadColumn()
480 if ( !isUnreadActive() )
return;
481 removeColumn( mUnreadIndex );
482 if ( isTotalActive() && mTotalIndex > mUnreadIndex )
484 if ( isSizeActive() && mSizeIndex > mUnreadIndex )
488 header()->adjustHeaderSize();
492 void KFolderTree::removeTotalColumn()
494 if ( !isTotalActive() )
return;
495 removeColumn( mTotalIndex );
496 if ( isUnreadActive() && mTotalIndex < mUnreadIndex )
498 if ( isSizeActive() && mTotalIndex < mSizeIndex )
501 header()->adjustHeaderSize();
505 void KFolderTree::addSizeColumn(
const TQString & name,
int width )
507 mSizeIndex = addColumn( name, width );
508 setColumnAlignment( mSizeIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight );
509 header()->adjustHeaderSize();
513 void KFolderTree::removeSizeColumn()
515 if ( !isSizeActive() )
return;
516 removeColumn( mSizeIndex );
517 if ( isUnreadActive() && mSizeIndex < mUnreadIndex )
519 if ( isTotalActive() && mSizeIndex < mTotalIndex )
522 header()->adjustHeaderSize();
527 void KFolderTree::setFullWidth(
bool fullWidth )
530 header()->setStretchEnabled(
true, 0 );
534 void KFolderTree::slotSizeChanged(
int section,
int,
int newSize )
537 header()->sectionPos(section), 0, newSize, visibleHeight(),
false );
540 #include "kfoldertree.moc"