viewmode.cpp 11.7 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>
Laurent Montel's avatar
Laurent Montel committed
25 26 27 28
//Added by qt3to4:
#include <Q3ValueList>
#include <QResizeEvent>
#include <QEvent>
29 30 31

#include "viewmode.h"
#include "playlistbox.h"
32
#include "searchplaylist.h"
33
#include "treeviewitemplaylist.h"
34
#include "collectionlist.h"
35 36 37 38 39

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

40 41
ViewMode::ViewMode(PlaylistBox *b) :
    QObject(b),
42
    m_playlistBox(b),
43 44
    m_visible(false),
    m_needsRefresh(false)
45
{
46
    m_playlistBox->viewport()->installEventFilter(this);
47 48 49 50 51 52 53
}

ViewMode::~ViewMode()
{

}

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

62 63 64
    if(m_needsRefresh)
	updateHeights();

65 66
    QFontMetrics fm = painter->fontMetrics();

67
    int y = item->listView()->itemMargin() + border;
68 69 70
    const QPixmap *pm = item->pixmap(column);

    if(item->isSelected()) {
71 72 73 74 75 76 77

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

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

        newPen.setWidth(5);
Laurent Montel's avatar
Laurent Montel committed
78
        newPen.setJoinStyle(Qt::RoundJoin);
79 80 81 82 83 84 85 86
        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));
87 88 89
        painter->setPen(colorGroup.highlightedText());
    }
    else
90
        painter->eraseRect(0, 0, width, item->height());
91 92 93 94 95 96 97

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

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

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

117 118
    if(e->type() == QEvent::Hide)
	m_needsRefresh = true;
119

120 121 122
    return QObject::eventFilter(watched, e);
}

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

void ViewMode::updateIcons(int size)
{
Laurent Montel's avatar
Laurent Montel committed
134
    for(Q3ListViewItemIterator it(m_playlistBox); it.current(); ++it) {
135 136 137 138 139
        PlaylistBox::Item *i = static_cast<PlaylistBox::Item *>(*it);
        i->setPixmap(0, SmallIcon(i->iconName(), size));
    }
}

140 141 142 143 144 145 146 147 148
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());
}

149
void ViewMode::updateHeights()
150
{
151
    const int width = m_playlistBox->width() - m_playlistBox->verticalScrollBar()->width() - border * 2;
152

153
    const int baseHeight = 2 * m_playlistBox->itemMargin() + 32 + border * 2;
154 155
    const QFontMetrics fm = m_playlistBox->fontMetrics();

Laurent Montel's avatar
Laurent Montel committed
156
    for(Q3ListViewItemIterator it(m_playlistBox); it.current(); ++it) {
157 158 159 160 161
        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);
    }
162 163

    m_needsRefresh = false;
164 165
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
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);
}

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

192 193 194 195 196 197 198
    QString line = item->text();

    QStringList l;

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

215 216 217 218 219 220 221 222 223 224 225 226 227 228
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

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

void CompactViewMode::setShown(bool shown)
{
241 242 243
    setVisible(shown);

    if(shown) {
244
        updateIcons(16);
245 246 247 248 249 250
	updateHeights();
    }
}

void CompactViewMode::updateHeights()
{
Laurent Montel's avatar
Laurent Montel committed
251
    for(Q3ListViewItemIterator it(playlistBox()); it.current(); ++it)
252
	it.current()->setup();
253 254 255 256 257 258
}

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

259 260
TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b),
    m_treeViewItems(5003, false), m_canDeletePlaylists(true)
261 262 263 264 265 266 267 268 269 270 271
{

}

TreeViewMode::~TreeViewMode()
{

}

void TreeViewMode::setShown(bool show)
{
272 273
    CompactViewMode::setShown(show);

274 275
    playlistBox()->setRootIsDecorated(show);

276
    if(show) {
277
        PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
278

279
        if(!collectionItem)
280 281 282
            return;

        if(collectionItem && m_searchCategories.isEmpty())
283
            setupCategories();
284
        else {
Laurent Montel's avatar
Laurent Montel committed
285
            for(Q3DictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
286
                it.current()->setVisible(true);
287
        }
288 289
    }
    else {
Laurent Montel's avatar
Laurent Montel committed
290
        for(Q3DictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
291
            it.current()->setVisible(false);
292
    }
293
}
294

295 296 297 298
void TreeViewMode::slotRemoveItem(const QString &item, unsigned column)
{
    QString itemKey;
    if(column == PlaylistItem::ArtistColumn)
299
        itemKey = "artists" + item;
300
    else if(column == PlaylistItem::GenreColumn)
301
        itemKey = "genres" + item;
302
    else if(column == PlaylistItem::AlbumColumn)
303
        itemKey = "albums" + item;
304
    else {
Laurent Montel's avatar
Laurent Montel committed
305
        kWarning() << k_funcinfo << "Unhandled column type " << column << endl;
306 307 308
        return;
    }

309
    if(!m_treeViewItems.find(itemKey))
310 311 312
        return;

    TreeViewItemPlaylist *itemPlaylist = m_treeViewItems[itemKey];
313 314 315 316 317 318

    if(!m_canDeletePlaylists) {
	m_pendingItemsToRemove << itemKey;
	return;
    }

319 320 321 322 323
    m_treeViewItems.remove(itemKey);
    itemPlaylist->deleteLater();
    emit signalPlaylistDestroyed(itemPlaylist);
}

324
void TreeViewMode::slotAddItems(const QStringList &items, unsigned column)
325
{
326
    QString searchCategory;
327 328 329 330 331 332 333
    if(column == PlaylistItem::ArtistColumn)
        searchCategory = "artists";
    else if(column == PlaylistItem::GenreColumn)
        searchCategory = "genres";
    else if(column == PlaylistItem::AlbumColumn)
        searchCategory = "albums";
    else {
Laurent Montel's avatar
Laurent Montel committed
334
        kWarning() << k_funcinfo << "Unhandled column type " << column << endl;
335 336
        return;
    }
337

Laurent Montel's avatar
Laurent Montel committed
338
    Q3ValueList<int> columns;
339
    columns.append(column);
340

341 342
    PlaylistSearch::Component::MatchMode mode = PlaylistSearch::Component::ContainsWord;
    if(column != PlaylistItem::ArtistColumn)
343
	mode = PlaylistSearch::Component::Exact;
344

345
    PlaylistSearch::ComponentList components;
346 347
    PlaylistList playlists;
    playlists.append(CollectionList::instance());
348

349 350 351 352 353 354 355
    QString itemKey, item;

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

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

357 358 359 360
	if(m_treeViewItems.find(itemKey))
	    continue;
	    
	components.clear();
361
	components.append(PlaylistSearch::Component(item, false, columns, mode));
362

363 364 365 366 367 368 369
	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);
    }
370
}
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
void TreeViewMode::slotCanDeletePlaylist(bool canDelete)
{
    m_canDeletePlaylists = canDelete;

    if(!canDelete)
	return;

    QStringList categories;
    categories << "artists" << "albums" << "genres";

    for(QStringList::ConstIterator it = m_pendingItemsToRemove.begin(); it != m_pendingItemsToRemove.end(); ++it) {
	m_treeViewItems[*it]->deleteLater();
	emit signalPlaylistDestroyed(m_treeViewItems[*it]);
	m_treeViewItems.remove(*it);
    }

    m_pendingItemsToRemove.clear();
}

391
void TreeViewMode::setupCategories()
392 393 394 395 396 397 398 399 400 401
{
    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);

402 403
    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Genres"));
    m_searchCategories.insert("genres", i);
404 405
}

406
#include "viewmode.moc"