viewmode.cpp 9.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/***************************************************************************
                          viewmode.cpp
                             -------------------
    begin                : Sat Jun 7 2003
    copyright            : (C) 2003 by Scott Wheeler, 
    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>
19
#include <kdebug.h>
20 21 22 23 24 25

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

#include "viewmode.h"
#include "playlistbox.h"
26 27
#include "searchplaylist.h"
#include "collectionlist.h"
28 29 30 31 32

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

33
ViewMode::ViewMode(PlaylistBox *b) : QObject(b),
34 35 36
    m_playlistBox(b),
    m_visible(false)
{
37 38
    connect(this, SIGNAL(signalCreateSearchList(const PlaylistSearch &, const QString &, const QString &)),
            b, SIGNAL(signalCreateSearchList(const PlaylistSearch &, const QString &, const QString &)));
39 40

    m_playlistBox->viewport()->installEventFilter(this);
41 42 43 44 45 46 47
}

ViewMode::~ViewMode()
{

}

48
void ViewMode::paintCell(PlaylistBox::Item *i,
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
                         QPainter *painter, 
                         const QColorGroup &colorGroup,
                         int column, int width, int align)
{
    Q_UNUSED(align);

    if(width < i->pixmap(column)->width())
	return;

    PlaylistBox::Item *item = static_cast<PlaylistBox::Item *>(i);

    QFontMetrics fm = painter->fontMetrics();

    int y = item->listView()->itemMargin();
    const QPixmap *pm = item->pixmap(column);

    if(item->isSelected()) {
66
        painter->fillRect(0, 0, width, item->height(), colorGroup.brush(QColorGroup::Highlight));
67 68 69
        painter->setPen(colorGroup.highlightedText());
    }
    else
70
        painter->eraseRect(0, 0, width, item->height());
71 72 73 74 75 76 77

    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();
78
    for(QStringList::Iterator it = m_lines[i].begin(); it != m_lines[i].end(); ++it) {
79 80 81 82 83 84 85
        int x = (width - fm.width(*it)) / 2;
        x = QMAX(x, item->listView()->itemMargin());
        painter->drawText(x, y, *it);
        y += fm.height() - fm.descent();
    }
}

86 87 88 89 90 91
PlaylistBox::Item *ViewMode::createSearchItem(PlaylistBox *box, SearchPlaylist *playlist,
                                              const QString &)
{
    return new PlaylistBox::Item(box, "midi", playlist->name(), playlist);
}

92 93 94 95 96 97 98 99
bool ViewMode::eventFilter(QObject *watched, QEvent *e)
{
    if(watched == m_playlistBox->viewport() && e->type() == QEvent::Resize)
        updateHeights(static_cast<QResizeEvent *>(e)->size().width());

    return QObject::eventFilter(watched, e);
}

100 101 102
void ViewMode::setShown(bool shown)
{
    m_visible = shown;
103
    if(shown) {
104
        updateIcons(32);
105 106
        updateHeights();
    }
107 108 109 110 111 112 113 114 115 116
}

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));
    }
}

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
void ViewMode::updateHeights(int width)
{
    static int oldWidth = 0;
    if(width == 0)
        width = m_playlistBox->visibleWidth();

    if(oldWidth == width)
        return;
    oldWidth = width;

    const int baseHeight = 3 * m_playlistBox->itemMargin() + 32;
    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();
    }
}

QStringList ViewMode::lines(const PlaylistBox::Item *item, const QFontMetrics &fm, int width) const
{
    QString line = item->text();

    QStringList l;

    while(!line.isEmpty()) {
        int textLength = line.length();
        while(textLength > 0 && 
              fm.width(line.mid(0, textLength).stripWhiteSpace()) + item->listView()->itemMargin() * 2 > width)
        {
            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;
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

177
void CompactViewMode::paintCell(PlaylistBox::Item *item,
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
                                QPainter *painter, 
                                const QColorGroup &colorGroup,
                                int column, int width, int align)
{
    item->KListViewItem::paintCell(painter, colorGroup, column, width, align);
}

void CompactViewMode::setShown(bool shown)
{
    if(shown)
        updateIcons(16);
}

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

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

}

TreeViewMode::~TreeViewMode()
{

}

void TreeViewMode::setShown(bool show)
{
207 208
    playlistBox()->setRootIsDecorated(show);

209
    if(show) {
210
        updateIcons(16);
211

212
        PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
213

214 215 216 217
        if(!collectionItem) {
            connect(playlistBox(), SIGNAL(signalCollectionInitialized()),
                    this, SLOT(slotSetupCategories()));
            return;
218
        }
219 220 221

        if(collectionItem && m_searchCategories.isEmpty())
            slotSetupCategories();
222
        else {
223
            for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
224
                it.current()->setVisible(true);
225 226 227
	}
    }
    else {
228
        for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
229
            it.current()->setVisible(false);
230
    }
231
}
232

233 234 235 236 237 238
PlaylistBox::Item *TreeViewMode::createSearchItem(PlaylistBox *, SearchPlaylist *playlist,
						  const QString &searchCategory)
{
    return new PlaylistBox::Item(m_searchCategories[searchCategory], "midi", playlist->name(), playlist);
}

239
void TreeViewMode::setupCategory(const QString &searchCategory, const QStringList &members, int column, bool exact)
240 241
{
    CollectionList *collection = CollectionList::instance();
242 243
    QValueList<int> columns;
    columns.append(column);
244

245 246
    KApplication::setOverrideCursor(Qt::waitCursor);

247 248 249
    for(QStringList::ConstIterator it = members.begin(); it != members.end(); ++it) {
        
        PlaylistSearch::ComponentList components;
250 251 252 253 254 255 256 257

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

        components.append(PlaylistSearch::Component(*it, true, columns, mode));
258 259 260 261 262 263

        PlaylistList playlists;
        playlists.append(collection);

        PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny, false);

264
        emit signalCreateSearchList(s, searchCategory, *it);
265

266 267 268
        static int i = 0;
        if(++i % 5 == 0)
            kapp->processEvents();
269
    }
270 271

    KApplication::restoreOverrideCursor();
272
}
273

274 275 276 277 278 279 280 281
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);
282 283
    setupCategory("artists", CollectionList::instance()->viewModeItems(CollectionList::Artists),
		  PlaylistItem::ArtistColumn, false);
284 285 286

    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Albums"));
    m_searchCategories.insert("albums", i);
287 288
    setupCategory("albums", CollectionList::instance()->viewModeItems(CollectionList::Albums),
		  PlaylistItem::AlbumColumn);
289

290 291
    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Genres"));
    m_searchCategories.insert("genres", i);
292 293
    setupCategory("genres", CollectionList::instance()->viewModeItems(CollectionList::Genres),
		  PlaylistItem::GenreColumn);
294

295 296 297 298
    for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
        it.current()->setSortedFirst(true);
}

299
#include "viewmode.moc"