viewmode.cpp 10.2 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
    m_playlistBox(b),
35 36
    m_visible(false),
    m_needsRefresh(false)
37
{
38 39
    connect(this, SIGNAL(signalCreateSearchList(const PlaylistSearch &, const QString &, const QString &)),
            b, SIGNAL(signalCreateSearchList(const PlaylistSearch &, const QString &, const QString &)));
40 41

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

ViewMode::~ViewMode()
{

}

49
void ViewMode::paintCell(PlaylistBox::Item *item,
50 51
                         QPainter *painter, 
                         const QColorGroup &colorGroup,
52
                         int column, int width, int)
53
{
54
    if(width < item->pixmap(column)->width())
55 56
	return;

57 58 59
    if(m_needsRefresh)
	updateHeights();

60 61 62 63 64 65
    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[item].begin(); it != m_lines[item].end(); ++it) {
79 80 81 82 83
        int x = (width - fm.width(*it)) / 2;
        x = QMAX(x, item->listView()->itemMargin());
        painter->drawText(x, y, *it);
        y += fm.height() - fm.descent();
    }
84 85 86

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

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

95 96
bool ViewMode::eventFilter(QObject *watched, QEvent *e)
{
97
    if(m_visible && watched == m_playlistBox->viewport() && e->type() == QEvent::Resize) {
98 99
        QResizeEvent *re = static_cast<QResizeEvent *>(e);
        if(re->size().width() != re->oldSize().width())
100
            m_needsRefresh = true;
101
    }
102

103 104 105
    if(e->type() == QEvent::Hide)
	m_needsRefresh = true;
    
106 107 108
    return QObject::eventFilter(watched, e);
}

109 110 111
void ViewMode::setShown(bool shown)
{
    m_visible = shown;
112
    if(shown) {
113
        updateIcons(32);
114
	m_needsRefresh = true;
115
    }
116 117 118 119 120 121 122 123 124 125
}

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

126 127


128
void ViewMode::updateHeights()
129
{
130
    const int width = m_playlistBox->viewport()->width();
131 132 133 134 135 136 137 138 139 140 141

    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();
    }
142 143

    m_needsRefresh = false;
144 145
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
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);
}

162 163 164
QStringList ViewMode::lines(const PlaylistBox::Item *item,
			    const QFontMetrics &fm,
			    int width) const
165
{
166 167 168 169 170 171
    // 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();

172 173 174 175 176 177 178
    QString line = item->text();

    QStringList l;

    while(!line.isEmpty()) {
        int textLength = line.length();
        while(textLength > 0 && 
179 180
              fm.width(line.mid(0, textLength).stripWhiteSpace()) +
	      item->listView()->itemMargin() * 2 > width)
181 182 183 184 185 186 187 188 189 190 191 192 193 194
        {
            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;
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

209
void CompactViewMode::paintCell(PlaylistBox::Item *item,
210 211 212 213 214
                                QPainter *painter, 
                                const QColorGroup &colorGroup,
                                int column, int width, int align)
{
    item->KListViewItem::paintCell(painter, colorGroup, column, width, align);
215 216
    if(item == item->listView()->dropItem())
        paintDropIndicator(painter, width, item->height());
217 218 219 220
}

void CompactViewMode::setShown(bool shown)
{
221 222 223
    setVisible(shown);

    if(shown) {
224
        updateIcons(16);
225 226 227 228 229 230 231 232
	updateHeights();
    }
}

void CompactViewMode::updateHeights()
{
    for(QListViewItemIterator it(playlistBox()); it.current(); ++it)
	it.current()->setup();
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}

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

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

}

TreeViewMode::~TreeViewMode()
{

}

void TreeViewMode::setShown(bool show)
{
251 252
    CompactViewMode::setShown(show);

253 254
    playlistBox()->setRootIsDecorated(show);

255
    if(show) {
256
        PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
257

258 259 260 261
        if(!collectionItem) {
            connect(playlistBox(), SIGNAL(signalCollectionInitialized()),
                    this, SLOT(slotSetupCategories()));
            return;
262
        }
263 264 265

        if(collectionItem && m_searchCategories.isEmpty())
            slotSetupCategories();
266
        else {
267
            for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
268
                it.current()->setVisible(true);
269 270 271
	}
    }
    else {
272
        for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
273
            it.current()->setVisible(false);
274
    }
275
}
276

277 278 279 280 281 282
PlaylistBox::Item *TreeViewMode::createSearchItem(PlaylistBox *, SearchPlaylist *playlist,
						  const QString &searchCategory)
{
    return new PlaylistBox::Item(m_searchCategories[searchCategory], "midi", playlist->name(), playlist);
}

283
void TreeViewMode::setupCategory(const QString &searchCategory, const QStringList &members, int column, bool exact)
284 285
{
    CollectionList *collection = CollectionList::instance();
286 287
    QValueList<int> columns;
    columns.append(column);
288

289 290
    KApplication::setOverrideCursor(Qt::waitCursor);

291 292 293
    for(QStringList::ConstIterator it = members.begin(); it != members.end(); ++it) {
        
        PlaylistSearch::ComponentList components;
294 295 296 297 298 299 300 301

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

        components.append(PlaylistSearch::Component(*it, true, columns, mode));
302 303 304 305 306 307

        PlaylistList playlists;
        playlists.append(collection);

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

308
        emit signalCreateSearchList(s, searchCategory, *it);
309

310 311 312
        static int i = 0;
        if(++i % 5 == 0)
            kapp->processEvents();
313
    }
314 315

    KApplication::restoreOverrideCursor();
316
}
317

318 319 320 321 322 323 324 325
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);
326
    setupCategory("artists", CollectionList::instance()->uniqueSet(CollectionList::Artists),
327
		  PlaylistItem::ArtistColumn, false);
328 329 330

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

334 335
    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Genres"));
    m_searchCategories.insert("genres", i);
336
    setupCategory("genres", CollectionList::instance()->uniqueSet(CollectionList::Genres),
337
		  PlaylistItem::GenreColumn);
338

339 340 341 342
    for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
        it.current()->setSortedFirst(true);
}

343
#include "viewmode.moc"