viewmode.cpp 7.12 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 33 34 35 36 37 38 39 40 41 42 43 44

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

ViewMode::ViewMode(PlaylistBox *b) :
    m_playlistBox(b),
    m_visible(false)
{

}

ViewMode::~ViewMode()
{

}

45
void ViewMode::paintCell(PlaylistBox::Item *i,
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
                         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();
    QString line = item->text();

    QStringList lines;
    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--;
        }
        
        lines.append(line.mid(0, textLength).stripWhiteSpace());
        line = line.mid(textLength);
    }

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

    int height = 3 * item->listView()->itemMargin() + pm->height() + 
        (fm.height() - fm.descent()) * lines.count();
	
    if(item->isSelected()) {
        painter->fillRect(0, 0, width, height, colorGroup.brush(QColorGroup::Highlight));
        painter->setPen(colorGroup.highlightedText());
    }
    else
        painter->eraseRect(0, 0, width, height);

    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();
    for(QStringList::Iterator it = lines.begin(); it != lines.end(); ++it) {
        int x = (width - fm.width(*it)) / 2;
        x = QMAX(x, item->listView()->itemMargin());
        painter->drawText(x, y, *it);
        y += fm.height() - fm.descent();
    }
    item->setHeight(height);
}

void ViewMode::setShown(bool shown)
{
    m_visible = shown;
    if(shown)
        updateIcons(32);
}

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

///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

134
void CompactViewMode::paintCell(PlaylistBox::Item *item,
135 136 137 138
                                QPainter *painter, 
                                const QColorGroup &colorGroup,
                                int column, int width, int align)
{
139
#if 0 // don't think this code is needed anymore
140

141 142
    if(width < item->pixmap(column)->width())
	return;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

    QFontMetrics fm = painter->fontMetrics();
    QString line = item->text();

    int baseWidth = item->pixmap(column)->width() + item->listView()->itemMargin() * 4;
    if(baseWidth + fm.width(line) > width) {
        int ellipsisLength = fm.width("...");
        if(width > baseWidth + ellipsisLength) {
            while(baseWidth + fm.width(line) + ellipsisLength > width)
                line.truncate(line.length() - 1);
            line = line.append("...");
        }
        else
            line = "...";
    }
    item->KListViewItem::setText(column, line);
159 160

#endif
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    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)
{
186 187
    if(show) {
	updateIcons(16);
188 189 190 191 192

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

	if(!collectionItem)
	    kdDebug(65432) << "TreeViewMode::setShown() - the CollectionList isn't initialized yet." << endl;
193
	
194 195 196 197 198 199
	if(collectionItem && m_categories.isEmpty()) {

	    PlaylistBox::Item *i;
	    
	    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Artists"));
	    m_categories.insert("artists", i);
200
            setupCategory(i, CollectionList::instance()->artists());
201 202 203

	    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Albums"));
	    m_categories.insert("albums", i);
204
            setupCategory(i, CollectionList::instance()->albums());
205 206 207

	    for(QDictIterator<PlaylistBox::Item> it(m_categories); it.current(); ++it)
		it.current()->setSortedFirst(true);
208 209
	}
	else {
210 211
	    for(QDictIterator<PlaylistBox::Item> it(m_categories); it.current(); ++it)
		it.current()->setVisible(true);
212 213 214
	}
    }
    else {
215 216
	for(QDictIterator<PlaylistBox::Item> it(m_categories); it.current(); ++it)
	    it.current()->setVisible(false);
217
    }
218 219

    playlistBox()->setRootIsDecorated(show);
220
}
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

void TreeViewMode::setupCategory(PlaylistBox::Item *parent, const QStringList &members) const
{
    CollectionList *collection = CollectionList::instance();

    for(QStringList::ConstIterator it = members.begin(); it != members.end(); ++it) {
        
        PlaylistSearch::ComponentList components;
        components.append(PlaylistSearch::Component(*it));

        PlaylistList playlists;
        playlists.append(collection);

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

        SearchPlaylist *p = new SearchPlaylist(s, static_cast<QWidget *>(collection->parent()), *it);
        new PlaylistBox::Item(parent, "midi", *it, p);
    }
}