viewmode.cpp 10.5 KB
Newer Older
1 2
/***************************************************************************
    begin                : Sat Jun 7 2003
3
    copyright            : (C) 2003 - 2004 by Scott Wheeler, 
4 5 6 7 8 9 10 11 12 13 14 15 16
    email                : wheeler@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kiconloader.h>
17
#include <kdebug.h>
18 19 20 21 22 23

#include <qpixmap.h>
#include <qpainter.h>

#include "viewmode.h"
#include "playlistbox.h"
24 25
#include "searchplaylist.h"
#include "collectionlist.h"
26 27 28 29 30

////////////////////////////////////////////////////////////////////////////////
// ViewMode
////////////////////////////////////////////////////////////////////////////////

31
ViewMode::ViewMode(PlaylistBox *b) : QObject(b),
32
    m_playlistBox(b),
33 34
    m_visible(false),
    m_needsRefresh(false)
35
{
36
    m_playlistBox->viewport()->installEventFilter(this);
37 38 39 40 41 42 43
}

ViewMode::~ViewMode()
{

}

44
void ViewMode::paintCell(PlaylistBox::Item *item,
45 46
                         QPainter *painter, 
                         const QColorGroup &colorGroup,
47
                         int column, int width, int)
48
{
49
    if(width < item->pixmap(column)->width())
50 51
	return;

52 53 54
    if(m_needsRefresh)
	updateHeights();

55 56
    QFontMetrics fm = painter->fontMetrics();

57
    int y = item->listView()->itemMargin() + border;
58 59 60
    const QPixmap *pm = item->pixmap(column);

    if(item->isSelected()) {
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

        painter->eraseRect(0, 0, width, item->height());

        QPen oldPen = painter->pen();
        QPen newPen = oldPen;

        newPen.setWidth(5);
        newPen.setJoinStyle(RoundJoin);
        newPen.setColor(QColorGroup::Highlight);

        painter->setPen(newPen);
        painter->drawRect(border, border, width - border * 2, item->height() - border * 2 + 1);
        painter->setPen(oldPen);

        painter->fillRect(border, border, width - border * 2, item->height() - border * 2 + 1,
                          colorGroup.brush(QColorGroup::Highlight));
77 78 79
        painter->setPen(colorGroup.highlightedText());
    }
    else
80
        painter->eraseRect(0, 0, width, item->height());
81 82 83 84 85 86 87

    if (!pm->isNull()) {
        int x = (width - pm->width()) / 2;
        x = QMAX(x, item->listView()->itemMargin());
        painter->drawPixmap(x, y, *pm);
    }
    y += pm->height() + fm.height() - fm.descent();
88
    for(QStringList::Iterator it = m_lines[item].begin(); it != m_lines[item].end(); ++it) {
89 90 91 92 93
        int x = (width - fm.width(*it)) / 2;
        x = QMAX(x, item->listView()->itemMargin());
        painter->drawText(x, y, *it);
        y += fm.height() - fm.descent();
    }
94 95 96

    if(item == item->listView()->dropItem())
        paintDropIndicator(painter, width, item->height());
97 98
}

99 100 101 102 103 104
PlaylistBox::Item *ViewMode::createSearchItem(PlaylistBox *box, SearchPlaylist *playlist,
                                              const QString &)
{
    return new PlaylistBox::Item(box, "midi", playlist->name(), playlist);
}

105 106
bool ViewMode::eventFilter(QObject *watched, QEvent *e)
{
107
    if(m_visible && watched == m_playlistBox->viewport() && e->type() == QEvent::Resize) {
108 109
        QResizeEvent *re = static_cast<QResizeEvent *>(e);
        if(re->size().width() != re->oldSize().width())
110
            m_needsRefresh = true;
111
    }
112

113 114 115
    if(e->type() == QEvent::Hide)
	m_needsRefresh = true;
    
116 117 118
    return QObject::eventFilter(watched, e);
}

119 120 121
void ViewMode::setShown(bool shown)
{
    m_visible = shown;
122
    if(shown) {
123
        updateIcons(32);
124
	m_needsRefresh = true;
125
    }
126 127 128 129 130 131 132 133 134 135
}

void ViewMode::updateIcons(int size)
{
    for(QListViewItemIterator it(m_playlistBox); it.current(); ++it) {
        PlaylistBox::Item *i = static_cast<PlaylistBox::Item *>(*it);
        i->setPixmap(0, SmallIcon(i->iconName(), size));
    }
}

136
void ViewMode::updateHeights()
137
{
138
    const int width = m_playlistBox->viewport()->width() - border * 2;
139

140
    const int baseHeight = 2 * m_playlistBox->itemMargin() + 32 + border * 2;
141 142 143 144 145 146 147 148 149
    const QFontMetrics fm = m_playlistBox->fontMetrics();

    for(QListViewItemIterator it(m_playlistBox); it.current(); ++it) {
        PlaylistBox::Item *i = static_cast<PlaylistBox::Item *>(it.current());
        m_lines[i] = lines(i, fm, width);
        const int height = baseHeight + (fm.height() - fm.descent()) * m_lines[i].count();
        i->setHeight(height);
        i->invalidateHeight();
    }
150 151

    m_needsRefresh = false;
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
void ViewMode::paintDropIndicator(QPainter *painter, int width, int height) // static
{
    static const int border = 1;
    static const int lineWidth = 2;

    QPen oldPen = painter->pen();
    QPen newPen = oldPen;

    newPen.setWidth(lineWidth);
    newPen.setStyle(DotLine);

    painter->setPen(newPen);
    painter->drawRect(border, border, width - border * 2, height - border * 2);
    painter->setPen(oldPen);
}

170 171 172
QStringList ViewMode::lines(const PlaylistBox::Item *item,
			    const QFontMetrics &fm,
			    int width) const
173
{
174 175 176 177 178 179
    // Here 32 is a bit arbitrary, but that's the width of the icons in this
    // mode and seems to a reasonable lower bound.

    if(width < 32)
	return QStringList();

180 181 182 183 184 185 186
    QString line = item->text();

    QStringList l;

    while(!line.isEmpty()) {
        int textLength = line.length();
        while(textLength > 0 && 
187 188
              fm.width(line.mid(0, textLength).stripWhiteSpace()) +
	      item->listView()->itemMargin() * 2 > width)
189 190 191 192 193 194 195 196 197 198 199 200 201 202
        {
            int i = line.findRev(QRegExp( "\\W"), textLength - 1);
            if(i > 0)
                textLength = i;
            else
                textLength--;
        }
        
        l.append(line.mid(0, textLength).stripWhiteSpace());
        line = line.mid(textLength);
    }
    return l;
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

CompactViewMode::CompactViewMode(PlaylistBox *b) : ViewMode(b)
{

}

CompactViewMode::~CompactViewMode()
{

}

217
void CompactViewMode::paintCell(PlaylistBox::Item *item,
218 219 220 221 222
                                QPainter *painter, 
                                const QColorGroup &colorGroup,
                                int column, int width, int align)
{
    item->KListViewItem::paintCell(painter, colorGroup, column, width, align);
223 224
    if(item == item->listView()->dropItem())
        paintDropIndicator(painter, width, item->height());
225 226 227 228
}

void CompactViewMode::setShown(bool shown)
{
229 230 231
    setVisible(shown);

    if(shown) {
232
        updateIcons(16);
233 234 235 236 237 238 239 240
	updateHeights();
    }
}

void CompactViewMode::updateHeights()
{
    for(QListViewItemIterator it(playlistBox()); it.current(); ++it)
	it.current()->setup();
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
}

////////////////////////////////////////////////////////////////////////////////
// TreeViewMode
////////////////////////////////////////////////////////////////////////////////

TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b)
{

}

TreeViewMode::~TreeViewMode()
{

}

void TreeViewMode::setShown(bool show)
{
259 260
    CompactViewMode::setShown(show);

261 262
    playlistBox()->setRootIsDecorated(show);

263
    if(show) {
264
        PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
265

266 267 268 269
        if(!collectionItem) {
            connect(playlistBox(), SIGNAL(signalCollectionInitialized()),
                    this, SLOT(slotSetupCategories()));
            return;
270
        }
271 272 273

        if(collectionItem && m_searchCategories.isEmpty())
            slotSetupCategories();
274
        else {
275
            for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
276
                it.current()->setVisible(true);
277 278 279
	}
    }
    else {
280
        for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
281
            it.current()->setVisible(false);
282
    }
283
}
284

285 286 287 288 289 290
PlaylistBox::Item *TreeViewMode::createSearchItem(PlaylistBox *, SearchPlaylist *playlist,
						  const QString &searchCategory)
{
    return new PlaylistBox::Item(m_searchCategories[searchCategory], "midi", playlist->name(), playlist);
}

291
void TreeViewMode::setupCategory(const QString &searchCategory, const QStringList &members, int column, bool exact)
292 293
{
    CollectionList *collection = CollectionList::instance();
294 295
    QValueList<int> columns;
    columns.append(column);
296

297 298
    KApplication::setOverrideCursor(Qt::waitCursor);

299 300 301
    for(QStringList::ConstIterator it = members.begin(); it != members.end(); ++it) {
        
        PlaylistSearch::ComponentList components;
302 303 304 305 306 307 308 309

	PlaylistSearch::Component::MatchMode mode;
	if(exact)
	    mode = PlaylistSearch::Component::Exact;
	else
	    mode = PlaylistSearch::Component::ContainsWord;

        components.append(PlaylistSearch::Component(*it, true, columns, mode));
310 311 312 313 314

        PlaylistList playlists;
        playlists.append(collection);

        PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny, false);
315 316
        SearchPlaylist *p = new SearchPlaylist(playlistBox(), s, *it, false);
	playlistBox()->setupPlaylist(p, "midi", m_searchCategories[searchCategory]);
317

318 319 320
        static int i = 0;
        if(++i % 5 == 0)
            kapp->processEvents();
321
    }
322 323

    KApplication::restoreOverrideCursor();
324
}
325

326 327 328 329 330 331 332 333
void TreeViewMode::slotSetupCategories()
{
    PlaylistBox::Item *i;
    
    PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
    
    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Artists"));
    m_searchCategories.insert("artists", i);
334
    setupCategory("artists", CollectionList::instance()->uniqueSet(CollectionList::Artists),
335
		  PlaylistItem::ArtistColumn, false);
336 337 338

    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Albums"));
    m_searchCategories.insert("albums", i);
339
    setupCategory("albums", CollectionList::instance()->uniqueSet(CollectionList::Albums),
340
		  PlaylistItem::AlbumColumn);
341

342 343
    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Genres"));
    m_searchCategories.insert("genres", i);
344
    setupCategory("genres", CollectionList::instance()->uniqueSet(CollectionList::Genres),
345
		  PlaylistItem::GenreColumn);
346

347 348 349 350
    for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
        it.current()->setSortedFirst(true);
}

351
#include "viewmode.moc"