1 #include "kfoldertree.h"
2 #include <tdeversion.h>
4 #include <tdeio/global.h>
5 #include <kiconloader.h>
7 #include <kstringhandler.h>
9 #include <tqapplication.h>
14 KFolderTreeItem::KFolderTreeItem( KFolderTree *parent,
const TQString & label,
15 Protocol protocol, Type type )
16 : TDEListViewItem( parent, label ), mProtocol( protocol ), mType( type ),
17 mUnread(-1), mTotal(0), mSize(0), mFolderIsCloseToQuota( false )
22 KFolderTreeItem::KFolderTreeItem( KFolderTreeItem *parent,
23 const TQString & label, Protocol protocol, Type type,
24 int unread,
int total )
25 : TDEListViewItem( parent, label ), mProtocol( protocol ), mType( type ),
26 mUnread( unread ), mTotal( total ), mSize(0), mFolderIsCloseToQuota( false )
31 int KFolderTreeItem::protocolSortingKey()
const
35 switch ( mProtocol ) {
51 int KFolderTreeItem::typeSortingKey()
const
84 int KFolderTreeItem::compare( TQListViewItem * i,
int col,
bool )
const
86 KFolderTreeItem* other =
static_cast<KFolderTreeItem*
>( i );
93 if ( depth() == 0 && mProtocol == NONE )
95 if ( other->depth() == 0 && other->protocol() == NONE )
99 int thisKey = protocolSortingKey();
100 int thatKey = other->protocolSortingKey();
101 if ( thisKey < thatKey )
103 if ( thisKey > thatKey )
107 thisKey = typeSortingKey();
108 thatKey = other->typeSortingKey();
109 if ( thisKey < thatKey )
111 if ( thisKey > thatKey )
115 return text( 0 ).localeAwareCompare( other->text( 0 ) );
120 TQ_INT64 a = 0, b = 0;
121 if (col ==
static_cast<KFolderTree*
>(listView())->unreadIndex())
124 b = other->unreadCount();
126 else if (col ==
static_cast<KFolderTree*
>(listView())->totalIndex())
129 b = other->totalCount();
131 else if (col ==
static_cast<KFolderTree*
>(listView())->sizeIndex())
134 b = other->folderSize();
140 return (a < b ? -1 : 1);
145 void KFolderTreeItem::setUnreadCount(
int aUnread )
147 if ( aUnread < 0 )
return;
151 TQString unread = TQString();
155 unread.setNum(mUnread);
159 setText(
static_cast<KFolderTree*
>(listView())->unreadIndex(),
164 void KFolderTreeItem::setTotalCount(
int aTotal )
166 if ( aTotal < 0 )
return;
170 TQString total = TQString();
174 total.setNum(mTotal);
178 setText(
static_cast<KFolderTree*
>(listView())->totalIndex(),
183 void KFolderTreeItem::setFolderSize( TQ_INT64 aSize )
185 if ( aSize < 0 )
return;
191 if (mSize == 0 && (childCount() == 0 || isOpen() ) )
194 size = TDEIO::convertSize(mSize);
196 if ( childCount() > 0 && !isOpen() ) {
197 TQ_INT64 recursiveSize = recursiveFolderSize();
198 if ( recursiveSize != mSize ) {
200 size += TQString::fromLatin1(
" + %1").arg( TDEIO::convertSize( recursiveSize - mSize ) );
202 size = TDEIO::convertSize( recursiveSize );
207 setText(
static_cast<KFolderTree*
>(listView())->sizeIndex(), size );
211 TQ_INT64 KFolderTreeItem::recursiveFolderSize()
const
213 TQ_INT64 size = mSize;
215 for ( TQListViewItem *item = firstChild() ;
216 item ; item = item->nextSibling() )
218 size +=
static_cast<KFolderTreeItem*
>(item)->recursiveFolderSize();
226 int KFolderTreeItem::countUnreadRecursive()
228 int count = (mUnread > 0) ? mUnread : 0;
230 for ( TQListViewItem *item = firstChild() ;
231 item ; item = item->nextSibling() )
233 count +=
static_cast<KFolderTreeItem*
>(item)->countUnreadRecursive();
240 void KFolderTreeItem::paintCell( TQPainter * p,
const TQColorGroup & cg,
241 int column,
int width,
int align )
243 KFolderTree *ft =
static_cast<KFolderTree*
>(listView());
245 const int unreadRecursiveCount = countUnreadRecursive();
246 const int unreadCount = ( mUnread > 0 ) ? mUnread : 0;
250 TQColorGroup mycg = cg;
251 if ( ( column == 0 || column == ft->sizeIndex() ) && folderIsCloseToQuota() )
253 mycg.setColor( TQColorGroup::Text, ft->paintInfo().colCloseToQuota );
257 if ( (column == 0 || column == ft->unreadIndex())
259 || ( !isOpen() && unreadRecursiveCount > 0 ) ) )
261 TQFont f = p->font();
262 f.setWeight(TQFont::Bold);
275 if ( ft->isUnreadActive() || column != 0 ) {
276 TDEListViewItem::paintCell( p, mycg, column, width, align );
278 TQListView *lv = listView();
279 TQString oldText = text(column);
283 setText( column,
"" );
285 TDEListViewItem::paintCell( p, mycg, column, width, align );
287 const TQPixmap *icon = pixmap( column );
288 int marg = lv ? lv->itemMargin() : 1;
291 setText( column, oldText );
293 p->setPen( mycg.highlightedText() );
295 p->setPen( mycg.color( TQColorGroup::Text ) );
298 r += icon->width() + marg;
300 TQString t = text( column );
307 if ( unreadCount > 0 || ( !isOpen() && unreadRecursiveCount > 0 ) ) {
309 unread =
" (" + TQString::number( unreadCount ) +
")";
310 else if ( unreadRecursiveCount == unreadCount || mType == Root )
311 unread =
" (" + TQString::number( unreadRecursiveCount ) +
")";
313 unread =
" (" + TQString::number( unreadCount ) +
" + " +
314 TQString::number( unreadRecursiveCount-unreadCount ) +
")";
318 TQFontMetrics fm( p->fontMetrics() );
319 int unreadWidth = fm.width( unread );
320 if ( fm.width( t ) + marg + r + unreadWidth > width )
321 t = squeezeFolderName( t, fm, width - marg - r - unreadWidth );
324 p->drawText( r, 0, width-marg-r, height(),
325 align | AlignVCenter, t, -1, &br );
327 if ( !unread.isEmpty() ) {
329 p->setPen( ft->paintInfo().colUnread );
330 p->drawText( br.right(), 0, width-marg-br.right(), height(),
331 align | AlignVCenter, unread );
337 TQString KFolderTreeItem::squeezeFolderName(
const TQString &text,
338 const TQFontMetrics &fm,
341 return KStringHandler::rPixelSqueeze( text, fm, width );
344 bool KFolderTreeItem::folderIsCloseToQuota()
const
346 return mFolderIsCloseToQuota;
349 void KFolderTreeItem::setFolderIsCloseToQuota(
bool v )
351 if ( mFolderIsCloseToQuota != v) {
352 mFolderIsCloseToQuota = v;
361 KFolderTree::KFolderTree( TQWidget *parent,
const char* name )
362 : TDEListView( parent, name ), mUnreadIndex(-1), mTotalIndex(-1), mSizeIndex(-1)
365 setStyleDependantFrameWidth();
366 setAcceptDrops(
true);
367 setDropVisualizer(
false);
368 setAllColumnsShowFocus(
true);
369 setShowSortIndicator(
true);
370 setUpdatesEnabled(
true);
371 setItemsRenameable(
false);
372 setRootIsDecorated(
true);
373 setSelectionModeExt(Extended);
374 setAlternateBackground(TQColor());
375 #if KDE_IS_VERSION( 3, 3, 90 )
376 setShadeSortColumn (
false );
379 disableAutoSelection();
380 setColumnWidth( 0, 120 );
382 disconnect( header(), TQ_SIGNAL( sizeChange(
int,
int,
int ) ) );
383 connect( header(), TQ_SIGNAL( sizeChange(
int,
int,
int ) ),
384 TQ_SLOT( slotSizeChanged(
int,
int,
int ) ) );
388 void KFolderTree::setStyleDependantFrameWidth()
392 if( style().isA(
"KeramikStyle") )
393 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth ) - 1;
395 frameWidth = style().pixelMetric( TQStyle::PM_DefaultFrameWidth );
396 if ( frameWidth < 0 )
398 if ( frameWidth != lineWidth() )
399 setLineWidth( frameWidth );
403 void KFolderTree::styleChange( TQStyle& oldStyle )
405 setStyleDependantFrameWidth();
406 TDEListView::styleChange( oldStyle );
410 void KFolderTree::drawContentsOffset( TQPainter * p,
int ox,
int oy,
411 int cx,
int cy,
int cw,
int ch )
413 bool oldUpdatesEnabled = isUpdatesEnabled();
414 setUpdatesEnabled(
false);
415 TDEListView::drawContentsOffset( p, ox, oy, cx, cy, cw, ch );
416 setUpdatesEnabled(oldUpdatesEnabled);
420 void KFolderTree::contentsMousePressEvent( TQMouseEvent *e )
422 setSelectionModeExt(Single);
423 TDEListView::contentsMousePressEvent(e);
427 void KFolderTree::contentsMouseReleaseEvent( TQMouseEvent *e )
429 TDEListView::contentsMouseReleaseEvent(e);
430 setSelectionModeExt(Extended);
434 void KFolderTree::addAcceptableDropMimetype(
const char *mimeType,
bool outsideOk )
436 int oldSize = mAcceptableDropMimetypes.size();
437 mAcceptableDropMimetypes.resize(oldSize+1);
438 mAcceptOutside.resize(oldSize+1);
440 mAcceptableDropMimetypes.at(oldSize) = mimeType;
441 mAcceptOutside.setBit(oldSize, outsideOk);
445 bool KFolderTree::acceptDrag( TQDropEvent* event )
const
447 TQListViewItem* item = itemAt(contentsToViewport(event->pos()));
449 for (uint i = 0; i < mAcceptableDropMimetypes.size(); i++)
451 if (event->provides(mAcceptableDropMimetypes[i]))
454 return (
static_cast<KFolderTreeItem*
>(item))->acceptDrag(event);
456 return mAcceptOutside[i];
463 void KFolderTree::addUnreadColumn(
const TQString & name,
int width )
465 mUnreadIndex = addColumn( name, width );
466 setColumnAlignment( mUnreadIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight );
467 header()->adjustHeaderSize();
471 void KFolderTree::addTotalColumn(
const TQString & name,
int width )
473 mTotalIndex = addColumn( name, width );
474 setColumnAlignment( mTotalIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight );
475 header()->adjustHeaderSize();
479 void KFolderTree::removeUnreadColumn()
481 if ( !isUnreadActive() )
return;
482 removeColumn( mUnreadIndex );
483 if ( isTotalActive() && mTotalIndex > mUnreadIndex )
485 if ( isSizeActive() && mSizeIndex > mUnreadIndex )
489 header()->adjustHeaderSize();
493 void KFolderTree::removeTotalColumn()
495 if ( !isTotalActive() )
return;
496 removeColumn( mTotalIndex );
497 if ( isUnreadActive() && mTotalIndex < mUnreadIndex )
499 if ( isSizeActive() && mTotalIndex < mSizeIndex )
502 header()->adjustHeaderSize();
506 void KFolderTree::addSizeColumn(
const TQString & name,
int width )
508 mSizeIndex = addColumn( name, width );
509 setColumnAlignment( mSizeIndex, tqApp->reverseLayout() ? TQt::AlignLeft : TQt::AlignRight );
510 header()->adjustHeaderSize();
514 void KFolderTree::removeSizeColumn()
516 if ( !isSizeActive() )
return;
517 removeColumn( mSizeIndex );
518 if ( isUnreadActive() && mSizeIndex < mUnreadIndex )
520 if ( isTotalActive() && mSizeIndex < mTotalIndex )
523 header()->adjustHeaderSize();
528 void KFolderTree::setFullWidth(
bool fullWidth )
531 header()->setStretchEnabled(
true, 0 );
535 void KFolderTree::slotSizeChanged(
int section,
int,
int newSize )
538 header()->sectionPos(section), 0, newSize, visibleHeight(),
false );
541 #include "kfoldertree.moc"