viewmode.cpp 11 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 <kstandarddirs.h>
18
#include <kdebug.h>
19 20 21

#include <qpixmap.h>
#include <qpainter.h>
22 23 24
#include <qfile.h>
#include <qdir.h>
#include <qdatastream.h>
25 26 27

#include "viewmode.h"
#include "playlistbox.h"
28
#include "searchplaylist.h"
29
#include "treeviewitemplaylist.h"
30
#include "collectionlist.h"
31 32 33 34 35

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

36 37
ViewMode::ViewMode(PlaylistBox *b) :
    QObject(b),
38
    m_playlistBox(b),
39 40
    m_visible(false),
    m_needsRefresh(false)
41
{
42
    m_playlistBox->viewport()->installEventFilter(this);
43 44 45 46 47 48 49
}

ViewMode::~ViewMode()
{

}

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

58 59 60
    if(m_needsRefresh)
	updateHeights();

61 62
    QFontMetrics fm = painter->fontMetrics();

63
    int y = item->listView()->itemMargin() + border;
64 65 66
    const QPixmap *pm = item->pixmap(column);

    if(item->isSelected()) {
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

        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));
83 84 85
        painter->setPen(colorGroup.highlightedText());
    }
    else
86
        painter->eraseRect(0, 0, width, item->height());
87 88 89 90 91 92 93

    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();
94
    for(QStringList::Iterator it = m_lines[item].begin(); it != m_lines[item].end(); ++it) {
95 96 97 98 99
        int x = (width - fm.width(*it)) / 2;
        x = QMAX(x, item->listView()->itemMargin());
        painter->drawText(x, y, *it);
        y += fm.height() - fm.descent();
    }
100 101 102

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

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
    if(e->type() == QEvent::Hide)
	m_needsRefresh = true;
115

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 137 138 139 140 141 142 143 144
void ViewMode::setupItem(PlaylistBox::Item *item) const
{
    const PlaylistBox *box = item->listView();
    const int width = box->width() - box->verticalScrollBar()->width() - border * 2;
    const int baseHeight = 2 * box->itemMargin() + 32 + border * 2;
    const QFontMetrics fm = box->fontMetrics();
    item->setHeight(baseHeight + (fm.height() - fm.descent()) * lines(item, fm, width).count());
}

145
void ViewMode::updateHeights()
146
{
147
    const int width = m_playlistBox->width() - m_playlistBox->verticalScrollBar()->width() - border * 2;
148

149
    const int baseHeight = 2 * m_playlistBox->itemMargin() + 32 + border * 2;
150 151 152 153 154 155 156 157
    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);
    }
158 159

    m_needsRefresh = false;
160 161
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
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);
}

178 179
QStringList ViewMode::lines(const PlaylistBox::Item *item,
			    const QFontMetrics &fm,
180
			    int width)
181
{
182 183 184 185 186 187
    // 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();

188 189 190 191 192 193 194
    QString line = item->text();

    QStringList l;

    while(!line.isEmpty()) {
        int textLength = line.length();
        while(textLength > 0 && 
195 196
              fm.width(line.mid(0, textLength).stripWhiteSpace()) +
	      item->listView()->itemMargin() * 2 > width)
197 198 199 200 201 202 203 204 205 206 207 208 209 210
        {
            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;
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

225
void CompactViewMode::paintCell(PlaylistBox::Item *item,
226 227 228 229 230
                                QPainter *painter, 
                                const QColorGroup &colorGroup,
                                int column, int width, int align)
{
    item->KListViewItem::paintCell(painter, colorGroup, column, width, align);
231 232
    if(item == item->listView()->dropItem())
        paintDropIndicator(painter, width, item->height());
233 234 235 236
}

void CompactViewMode::setShown(bool shown)
{
237 238 239
    setVisible(shown);

    if(shown) {
240
        updateIcons(16);
241 242 243 244 245 246 247 248
	updateHeights();
    }
}

void CompactViewMode::updateHeights()
{
    for(QListViewItemIterator it(playlistBox()); it.current(); ++it)
	it.current()->setup();
249 250 251 252 253 254
}

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

255
TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b), m_treeViewItems(5003, false)
256 257 258 259 260 261 262 263 264 265 266
{

}

TreeViewMode::~TreeViewMode()
{

}

void TreeViewMode::setShown(bool show)
{
267 268
    CompactViewMode::setShown(show);

269 270
    playlistBox()->setRootIsDecorated(show);

271
    if(show) {
272
        PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
273

274
        if(!collectionItem)
275 276 277
            return;

        if(collectionItem && m_searchCategories.isEmpty())
278
            setupCategories();
279
        else {
280
            for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
281
                it.current()->setVisible(true);
282
        }
283 284
    }
    else {
285
        for(QDictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
286
            it.current()->setVisible(false);
287
    }
288
}
289

290 291 292 293
void TreeViewMode::slotRemoveItem(const QString &item, unsigned column)
{
    QString itemKey;
    if(column == PlaylistItem::ArtistColumn)
294
        itemKey = "artists" + item;
295
    else if(column == PlaylistItem::GenreColumn)
296
        itemKey = "genres" + item;
297
    else if(column == PlaylistItem::AlbumColumn)
298
        itemKey = "albums" + item;
299 300 301 302 303
    else {
        kdWarning() << k_funcinfo << "Unhandled column type " << column << endl;
        return;
    }

304
    if(!m_treeViewItems.find(itemKey))
305 306 307 308 309 310 311 312
        return;

    TreeViewItemPlaylist *itemPlaylist = m_treeViewItems[itemKey];
    m_treeViewItems.remove(itemKey);
    itemPlaylist->deleteLater();
    emit signalPlaylistDestroyed(itemPlaylist);
}

313
void TreeViewMode::slotAddItems(const QStringList &items, unsigned column)
314
{
315
    QString searchCategory;
316 317 318 319 320 321 322 323 324 325
    if(column == PlaylistItem::ArtistColumn)
        searchCategory = "artists";
    else if(column == PlaylistItem::GenreColumn)
        searchCategory = "genres";
    else if(column == PlaylistItem::AlbumColumn)
        searchCategory = "albums";
    else {
        kdWarning() << k_funcinfo << "Unhandled column type " << column << endl;
        return;
    }
326

327 328
    QValueList<int> columns;
    columns.append(column);
329

330 331
    PlaylistSearch::Component::MatchMode mode = PlaylistSearch::Component::ContainsWord;
    if(column != PlaylistItem::ArtistColumn)
332
	mode = PlaylistSearch::Component::Exact;
333

334
    PlaylistSearch::ComponentList components;
335 336
    PlaylistList playlists;
    playlists.append(CollectionList::instance());
337

338 339 340 341 342 343 344
    QString itemKey, item;

    PlaylistBox::Item *itemParent = m_searchCategories[searchCategory];

    for(QStringList::ConstIterator it = items.begin(); it != items.end(); ++it) {
	item = *it;
	itemKey = searchCategory + item;
345

346 347 348 349 350
	if(m_treeViewItems.find(itemKey))
	    continue;
	    
	components.clear();
	components.append(PlaylistSearch::Component(item, true, columns, mode));
351

352 353 354 355 356 357 358
	PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny, false);

	TreeViewItemPlaylist *p = new TreeViewItemPlaylist(playlistBox(), s, item, false);
	playlistBox()->setupPlaylist(p, "midi", itemParent);

	m_treeViewItems.insert(itemKey, p);
    }
359
}
360

361
void TreeViewMode::setupCategories()
362 363 364 365 366 367 368 369 370 371
{
    PlaylistBox::Item *i;
    PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
    
    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Artists"));
    m_searchCategories.insert("artists", i);

    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Albums"));
    m_searchCategories.insert("albums", i);

372 373
    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Genres"));
    m_searchCategories.insert("genres", i);
374 375
}

376
#include "viewmode.moc"