viewmode.cpp 12.4 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>
#include <QDir>
#include <QDataStream>
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
                         QPainter *painter,
56
                         const QColorGroup &colorGroup,
57
                         int column, int width, int)
58
{
59
    if(width < item->pixmap(column)->width())
60
        return;
61

62
    if(m_needsRefresh)
63
        updateHeights();
64

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
        newPen.setColor(QPalette::Highlight);
80 81 82 83 84 85

        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,
86
                          colorGroup.brush(QPalette::Highlight));
Stephan Kulow's avatar
Stephan Kulow committed
87
        painter->setPen(colorGroup.color( QPalette::HighlightedText));
88 89
    }
    else
90
        painter->eraseRect(0, 0, width, item->height());
91 92 93

    if (!pm->isNull()) {
        int x = (width - pm->width()) / 2;
94
        x = qMax(x, item->listView()->itemMargin());
95 96 97
        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
        int x = (width - fm.width(*it)) / 2;
100
        x = qMax(x, item->listView()->itemMargin());
101 102 103
        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
    if(e->type() == QEvent::Hide)
118
        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
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);
175
    newPen.setStyle(Qt::DotLine);
176 177 178 179 180 181

    painter->setPen(newPen);
    painter->drawRect(border, border, width - border * 2, height - border * 2);
    painter->setPen(oldPen);
}

182
QStringList ViewMode::lines(const PlaylistBox::Item *item,
183 184
                            const QFontMetrics &fm,
                            int width)
185
{
186
    // Here 32 is a bit arbitrary, but that's the width of the icons in this
Dirk Mueller's avatar
Dirk Mueller committed
187
    // mode and seems to a reasonable toLower bound.
188 189

    if(width < 32)
190
        return QStringList();
191

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

    QStringList l;

    while(!line.isEmpty()) {
        int textLength = line.length();
198
        while(textLength > 0 &&
199
              fm.width(line.mid(0, textLength).stripWhiteSpace()) +
200
              item->listView()->itemMargin() * 2 > width)
201
        {
Stephan Kulow's avatar
Stephan Kulow committed
202
            int i = line.lastIndexOf(QRegExp( "\\W"), textLength - 1);
203 204 205 206 207
            if(i > 0)
                textLength = i;
            else
                textLength--;
        }
208

209 210 211 212 213 214
        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
                                QPainter *painter,
231 232 233
                                const QColorGroup &colorGroup,
                                int column, int width, int align)
{
Laurent Montel's avatar
Laurent Montel committed
234
    item->K3ListViewItem::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
        updateHeights();
246 247 248 249 250
    }
}

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
TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b),
260
    m_treeViewItems(5003, false), m_dynamicListsFrozen(false), m_setup(false)
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
            setupDynamicPlaylists();
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 290 291 292 293 294
        if(!m_setup) {
            m_setup = true;
            playlistBox()->setSorting(-1);
            CollectionList::instance()->setupTreeViewEntries(this);
            playlistBox()->setSorting(0);
            playlistBox()->sort();
        }
295 296
    }
    else {
Laurent Montel's avatar
Laurent Montel committed
297
        for(Q3DictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
298
            it.current()->setVisible(false);
299
    }
300
}
301

302
void TreeViewMode::removeItem(const QString &item, unsigned column)
303
{
304 305 306
    if(!m_setup)
        return;

307 308
    QString itemKey;
    if(column == PlaylistItem::ArtistColumn)
309
        itemKey = "artists" + item;
310
    else if(column == PlaylistItem::GenreColumn)
311
        itemKey = "genres" + item;
312
    else if(column == PlaylistItem::AlbumColumn)
313
        itemKey = "albums" + item;
314
    else {
Laurent Montel's avatar
Laurent Montel committed
315
        kWarning() << k_funcinfo << "Unhandled column type " << column << endl;
316 317 318
        return;
    }

319
    if(!m_treeViewItems.find(itemKey))
320 321 322
        return;

    TreeViewItemPlaylist *itemPlaylist = m_treeViewItems[itemKey];
323

324 325 326
    if(m_dynamicListsFrozen) {
        m_pendingItemsToRemove << itemKey;
        return;
327 328
    }

329 330 331 332 333
    m_treeViewItems.remove(itemKey);
    itemPlaylist->deleteLater();
    emit signalPlaylistDestroyed(itemPlaylist);
}

334
void TreeViewMode::addItems(const QStringList &items, unsigned column)
335
{
336 337 338
    if(!m_setup)
        return;

339
    QString searchCategory;
340 341 342 343 344 345 346
    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
347
        kWarning() << k_funcinfo << "Unhandled column type " << column << endl;
348 349
        return;
    }
350

Laurent Montel's avatar
Laurent Montel committed
351
    Q3ValueList<int> columns;
352
    columns.append(column);
353

354 355
    PlaylistSearch::Component::MatchMode mode = PlaylistSearch::Component::ContainsWord;
    if(column != PlaylistItem::ArtistColumn)
356
        mode = PlaylistSearch::Component::Exact;
357

358
    PlaylistSearch::ComponentList components;
359 360
    PlaylistList playlists;
    playlists.append(CollectionList::instance());
361

362 363 364 365
    QString itemKey, item;
    PlaylistBox::Item *itemParent = m_searchCategories[searchCategory];

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

369 370
        if(m_treeViewItems.find(itemKey))
            continue;
371

372 373
        components.clear();
        components.append(PlaylistSearch::Component(item, false, columns, mode));
374

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

377 378 379
        TreeViewItemPlaylist *p = new TreeViewItemPlaylist(playlistBox(), s, item);
        playlistBox()->setupPlaylist(p, "midi", itemParent);
        m_treeViewItems.insert(itemKey, p);
380
    }
381
}
382

383
void TreeViewMode::setDynamicListsFrozen(bool frozen)
384
{
385
    m_dynamicListsFrozen = frozen;
386

387 388
    if(frozen)
        return;
389 390 391 392

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

393 394 395 396 397 398
    for(QStringList::ConstIterator it = m_pendingItemsToRemove.begin();
        it != m_pendingItemsToRemove.end();
        ++it)
    {
        m_treeViewItems[*it]->deleteLater();
        m_treeViewItems.remove(*it);
399 400 401 402 403
    }

    m_pendingItemsToRemove.clear();
}

404
void TreeViewMode::setupDynamicPlaylists()
405 406 407
{
    PlaylistBox::Item *i;
    PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
408

409 410 411 412 413 414
    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);

415 416
    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Genres"));
    m_searchCategories.insert("genres", i);
417 418
}

419 420 421 422 423 424 425 426 427
////////////////////////////////////////////////////////////////////////////////
// CoverManagerMode
////////////////////////////////////////////////////////////////////////////////

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

}

428
#include "viewmode.moc"
429 430

// vim: set et sw=4 tw=0 sta: