summaryrefslogtreecommitdiffstats
path: root/knode/kncollectionviewitem.cpp
blob: ace78a63a865a2734620c542fc752c6872aa3b7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
    kncollectionviewitem.cpp

    KNode, the KDE newsreader
    Copyright (c) 1999-2004 the KNode authors.
    See file AUTHORS for details

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <qdragobject.h>
#include <qpainter.h>

#include <kiconloader.h>
#include <kstringhandler.h>

#include "kncollectionviewitem.h"
#include "kncollectionview.h"
#include "kngroup.h"
#include "knfolder.h"
#include "knglobals.h"
#include "knconfigmanager.h"


KNCollectionViewItem::KNCollectionViewItem( KFolderTree *parent, Protocol protocol, Type type) :
  KFolderTreeItem(parent, QString::null, protocol, type), coll(0)
{
  setIcon();
}


KNCollectionViewItem::KNCollectionViewItem( KFolderTreeItem *it, Protocol protocol, Type type, int unread, int total ) :
  KFolderTreeItem(it, QString::null, protocol, type, unread, total), coll(0)
{
  setIcon();
}


KNCollectionViewItem::~KNCollectionViewItem()
{
  if(coll) coll->setListItem(0);
}


void KNCollectionViewItem::setIcon() {
  if ( protocol() == KFolderTreeItem::News ) {
    // news servers/groups
    switch ( type() ) {
      case KFolderTreeItem::Root:
        setPixmap( 0, SmallIcon("server") );
        break;
      default:
        setPixmap( 0, UserIcon("group") );
    }
  } else {
    // local folders
    switch ( type() ) {
      case KFolderTreeItem::Outbox:
        setPixmap( 0, SmallIcon("folder_outbox") );
        break;
      case KFolderTreeItem::Drafts:
        setPixmap( 0, SmallIcon("edit") );
        break;
      case KFolderTreeItem::SentMail:
        setPixmap( 0, SmallIcon("folder_sent_mail") );
        break;
      default:
        setPixmap( 0, SmallIcon("folder") );
    }
  }
}


int KNCollectionViewItem::compare(QListViewItem *i, int col, bool ascending) const
{
  KFolderTreeItem *other = static_cast<KFolderTreeItem*>(i);

  // folders should be always on the bottom
  if (protocol() == KFolderTreeItem::Local) {
    if (other && other->protocol() == KFolderTreeItem::News)
      return ascending ? 1 : -1;
  }

  // news servers should be always on top
  if (protocol() == KFolderTreeItem::News) {
    if (other && other->protocol() == KFolderTreeItem::Local)
      return ascending ? -1 : 1;
  }

  return KFolderTreeItem::compare(i, col, ascending);
}


bool KNCollectionViewItem::acceptDrag(QDropEvent* event) const
{
  if (event && coll && coll->type()==KNCollection::CTfolder) {
    if (event->provides("x-knode-drag/article"))
      return !(static_cast<KNFolder*>(coll)->isRootFolder());   // don't drop articles on the root folder
    else if (event->provides("x-knode-drag/folder"))
      return !isSelected();             // don't drop on itself
  }
  return false;
}


void KNCollectionViewItem::paintCell( QPainter * p, const QColorGroup & cg,int column, int width, int align )
{
  KFolderTree *ft = static_cast<KFolderTree*>( listView() );

  // we only need to deal with the case where we paint the folder/group name
  // and the unread count is displayed in a separate column
  if ( !ft->isUnreadActive() || column != 0 ) {
    KFolderTreeItem::paintCell( p, cg, column, width, align );
    return;
  }

  // find out if we will use bold font, necessary for the text squeezing
  if ( (column == 0 || column == ft->unreadIndex()) && ( mUnread > 0 ) ) {
    QFont f = p->font();
    f.setWeight(QFont::Bold);
    p->setFont(f);
  }

  // consider pixmap size for squeezing
  int pxWidth = 8;
  const QPixmap *px = pixmap(column);
  if (px)
    pxWidth += px->width();

  // temporary set the squeezed text and use the parent class to paint it
  QString curText = text( column );
  if ( p->fontMetrics().width( curText ) > width - pxWidth ) {
    setText( column, squeezeFolderName( curText, p->fontMetrics(), width - pxWidth ) );
    KFolderTreeItem::paintCell( p, cg, column, width, align );
    setText( column, curText );
  } else
    KFolderTreeItem::paintCell( p, cg, column, width, align );
}


QString KNCollectionViewItem::squeezeFolderName( const QString &text,
                                                 const QFontMetrics &fm,
                                                 uint width ) const
{
  if (protocol() == KFolderTreeItem::News && type() == KFolderTreeItem::Other) {
    QString t(text);
    int curPos = 0, nextPos = 0;
    QString temp;
    while ( (uint)fm.width(t) > width && nextPos != -1 ) {
      nextPos = t.find('.', curPos);
      if ( nextPos != -1 ) {
        temp = t[curPos];
        t.replace( curPos, nextPos - curPos, temp );
        curPos += 2;
      }
    }
    if ( (uint)fm.width( t ) > width )
      t = KStringHandler::rPixelSqueeze( t, fm, width );
    return t;
  } else
    return KFolderTreeItem::squeezeFolderName( text, fm, width );
}