viewmode.cpp 13.3 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
    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.                                   *
 *                                                                         *
 ***************************************************************************/

16 17
#include "viewmode.h"

18
#include <kiconloader.h>
19
#include <kstandarddirs.h>
20
#include <kdebug.h>
21

22 23
#include <QPixmap>
#include <QPainter>
Laurent Montel's avatar
Laurent Montel committed
24
#include <QResizeEvent>
25 26

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

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

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

ViewMode::~ViewMode()
{

}

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

57
    if(m_needsRefresh)
58
        updateHeights();
59

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

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

    if(item->isSelected()) {
66
        painter->eraseRect(0, 0, width, item->height());
67
        painter->setRenderHint(QPainter::Antialiasing);
68 69 70 71

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

72
        painter->setPen(newPen);
Laurent Montel's avatar
Laurent Montel committed
73
        newPen.setJoinStyle(Qt::RoundJoin);
74

75 76 77 78
        newPen.setWidth(1);

        QColor background = m_playlistBox->palette().color(QPalette::Highlight);
        newPen.setColor(m_playlistBox->palette().color(QPalette::Text));
79
        painter->setPen(newPen);
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
        painter->drawRoundedRect(border, border, width - border * 2,
                                 item->height() - border * 2, 2, 2);

        QRect inner(border + 1, border + 1, width - border * 2 - 2,
                    item->height() - border * 2 - 2);

        painter->fillRect(inner, background);

        QPainterPath path(inner.bottomLeft());

        path.lineTo(QPoint(inner.topLeft().x(), inner.topLeft().y() - 3));
        const QPointF topLeft(inner.topLeft());
        QRectF arc(topLeft, QSizeF(4, 4));
        path.arcTo(arc, 180, -90);
        path.lineTo(inner.topRight());
        path.lineTo(inner.bottomRight());
        path.lineTo(inner.bottomLeft());

        QColor window(item->listView()->palette().window().color());
        const QColor base = background;

        window.setAlphaF(0.5);

        QLinearGradient decoGradient1;
        decoGradient1.setStart(inner.topLeft());
        decoGradient1.setFinalStop(inner.bottomLeft());
        decoGradient1.setColorAt(0, window);
        decoGradient1.setColorAt(1, Qt::transparent);

        QLinearGradient decoGradient2;
        decoGradient2.setStart(inner.topLeft());
        decoGradient2.setFinalStop(inner.topRight());
        decoGradient2.setColorAt(0, Qt::transparent);
        decoGradient2.setColorAt(1, base);

        painter->fillPath(path, decoGradient1);
        painter->fillPath(path, decoGradient2);
117

118
        painter->setPen(colorGroup.color(QPalette::HighlightedText));
119 120
    }
    else
121
        painter->eraseRect(0, 0, width, item->height());
122

123
    if(!pm->isNull()) {
124
        int x = (width - pm->width()) / 2;
125
        x = qMax(x, item->listView()->itemMargin());
126 127
        painter->drawPixmap(x, y, *pm);
    }
128

129
    y += pm->height() + fm.height() - fm.descent();
130

Albert Astals Cid's avatar
Albert Astals Cid committed
131
    foreach(const QString &line, m_lines[item]) {
132
        int x = (width - fm.width(line)) / 2;
133
        x = qMax(x, item->listView()->itemMargin());
134
        painter->drawText(x, y, line);
135 136
        y += fm.height() - fm.descent();
    }
137 138 139

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

142 143
bool ViewMode::eventFilter(QObject *watched, QEvent *e)
{
144
    if(m_visible && watched == m_playlistBox->viewport() && e->type() == QEvent::Resize) {
145 146
        QResizeEvent *re = static_cast<QResizeEvent *>(e);
        if(re->size().width() != re->oldSize().width())
147
            m_needsRefresh = true;
148
    }
149

150
    if(e->type() == QEvent::Hide)
151
        m_needsRefresh = true;
152

153 154 155
    return QObject::eventFilter(watched, e);
}

156 157
QString ViewMode::name() const
{
158
    return i18nc("the normal viewing mode", "Default");
159 160
}

161 162 163
void ViewMode::setShown(bool shown)
{
    m_visible = shown;
164
    if(shown) {
165
        updateIcons(32);
166
        m_needsRefresh = true;
167
    }
168 169 170 171
}

void ViewMode::updateIcons(int size)
{
Laurent Montel's avatar
Laurent Montel committed
172
    for(Q3ListViewItemIterator it(m_playlistBox); it.current(); ++it) {
173 174 175 176 177
        PlaylistBox::Item *i = static_cast<PlaylistBox::Item *>(*it);
        i->setPixmap(0, SmallIcon(i->iconName(), size));
    }
}

178 179 180 181 182 183 184 185 186
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());
}

187
void ViewMode::updateHeights()
188
{
189
    const int width = m_playlistBox->width() - m_playlistBox->verticalScrollBar()->width() - border * 2;
190

191 192
    const int baseHeight = 2 * m_playlistBox->itemMargin() + 32 +
                           border * 2 + 4;
193 194
    const QFontMetrics fm = m_playlistBox->fontMetrics();

Laurent Montel's avatar
Laurent Montel committed
195
    for(Q3ListViewItemIterator it(m_playlistBox); it.current(); ++it) {
196 197 198 199 200
        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);
    }
201 202

    m_needsRefresh = false;
203 204
}

205 206 207 208 209 210 211 212 213
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);
214
    newPen.setStyle(Qt::DotLine);
215 216 217 218 219 220

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

221
QStringList ViewMode::lines(const PlaylistBox::Item *item,
222 223
                            const QFontMetrics &fm,
                            int width)
224
{
225
    // Here 32 is a bit arbitrary, but that's the width of the icons in this
Dirk Mueller's avatar
Dirk Mueller committed
226
    // mode and seems to a reasonable toLower bound.
227 228

    if(width < 32)
229
        return QStringList();
230

231 232 233 234 235 236
    QString line = item->text();

    QStringList l;

    while(!line.isEmpty()) {
        int textLength = line.length();
237
        while(textLength > 0 &&
Tim Beaulen's avatar
Tim Beaulen committed
238
              fm.width(line.mid(0, textLength).trimmed()) +
239
              item->listView()->itemMargin() * 2 > width)
240
        {
Stephan Kulow's avatar
Stephan Kulow committed
241
            int i = line.lastIndexOf(QRegExp( "\\W"), textLength - 1);
242 243 244 245 246
            if(i > 0)
                textLength = i;
            else
                textLength--;
        }
247

Tim Beaulen's avatar
Tim Beaulen committed
248
        l.append(line.mid(0, textLength).trimmed());
249 250 251 252 253
        line = line.mid(textLength);
    }
    return l;
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

268
void CompactViewMode::paintCell(PlaylistBox::Item *item,
269
                                QPainter *painter,
270 271 272
                                const QColorGroup &colorGroup,
                                int column, int width, int align)
{
Laurent Montel's avatar
Laurent Montel committed
273
    item->K3ListViewItem::paintCell(painter, colorGroup, column, width, align);
274 275
    if(item == item->listView()->dropItem())
        paintDropIndicator(painter, width, item->height());
276 277
}

278 279
QString CompactViewMode::name() const
{
280
    return i18nc("compact viewing mode", "Compact");
281 282
}

283 284
void CompactViewMode::setShown(bool shown)
{
285 286 287
    setVisible(shown);

    if(shown) {
288
        updateIcons(16);
289
        updateHeights();
290 291 292 293 294
    }
}

void CompactViewMode::updateHeights()
{
Laurent Montel's avatar
Laurent Montel committed
295
    for(Q3ListViewItemIterator it(playlistBox()); it.current(); ++it)
296
        it.current()->setup();
297 298 299 300 301 302
}

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

303
TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b),
304
    m_dynamicListsFrozen(false), m_setup(false)
305 306 307 308 309 310 311 312 313
{

}

TreeViewMode::~TreeViewMode()
{

}

314 315 316 317 318
QString TreeViewMode::name() const
{
    return i18n("Tree");
}

319 320
void TreeViewMode::setShown(bool show)
{
321 322
    CompactViewMode::setShown(show);

323 324
    playlistBox()->setRootIsDecorated(show);

325
    if(show) {
326
        PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
327

328
        if(!collectionItem)
329 330 331
            return;

        if(collectionItem && m_searchCategories.isEmpty())
332
            setupDynamicPlaylists();
333
        else {
334 335
            foreach(PlaylistBox::Item *item, m_searchCategories)
                item->setVisible(true);
336
        }
337

338 339 340 341 342 343 344
        if(!m_setup) {
            m_setup = true;
            playlistBox()->setSorting(-1);
            CollectionList::instance()->setupTreeViewEntries(this);
            playlistBox()->setSorting(0);
            playlistBox()->sort();
        }
345 346
    }
    else {
347 348
        foreach(PlaylistBox::Item *item, m_searchCategories)
            item->setVisible(false);
349
    }
350
}
351

352
void TreeViewMode::removeItem(const QString &item, unsigned column)
353
{
354 355 356
    if(!m_setup)
        return;

357 358
    QString itemKey;
    if(column == PlaylistItem::ArtistColumn)
359
        itemKey = "artists" + item;
360
    else if(column == PlaylistItem::GenreColumn)
361
        itemKey = "genres" + item;
362
    else if(column == PlaylistItem::AlbumColumn)
363
        itemKey = "albums" + item;
364
    else {
Thiago Macieira's avatar
Thiago Macieira committed
365
        kWarning() << "Unhandled column type " << column;
366 367 368
        return;
    }

369
    if(!m_treeViewItems.contains(itemKey))
370 371
        return;

372
    TreeViewItemPlaylist *itemPlaylist = m_treeViewItems.value(itemKey, 0);
373

374 375 376
    if(m_dynamicListsFrozen) {
        m_pendingItemsToRemove << itemKey;
        return;
377 378
    }

379 380 381 382 383
    m_treeViewItems.remove(itemKey);
    itemPlaylist->deleteLater();
    emit signalPlaylistDestroyed(itemPlaylist);
}

384
void TreeViewMode::addItems(const QStringList &items, unsigned column)
385
{
386 387 388
    if(!m_setup)
        return;

389
    QString searchCategory;
390 391 392 393 394 395 396
    if(column == PlaylistItem::ArtistColumn)
        searchCategory = "artists";
    else if(column == PlaylistItem::GenreColumn)
        searchCategory = "genres";
    else if(column == PlaylistItem::AlbumColumn)
        searchCategory = "albums";
    else {
Thiago Macieira's avatar
Thiago Macieira committed
397
        kWarning() << "Unhandled column type " << column;
398 399
        return;
    }
400

401
    ColumnList columns;
402
    columns.append(column);
403

404 405
    PlaylistSearch::Component::MatchMode mode = PlaylistSearch::Component::ContainsWord;
    if(column != PlaylistItem::ArtistColumn)
406
        mode = PlaylistSearch::Component::Exact;
407

408
    PlaylistSearch::ComponentList components;
409 410
    PlaylistList playlists;
    playlists.append(CollectionList::instance());
411

Albert Astals Cid's avatar
Albert Astals Cid committed
412
    QString itemKey;
413
    PlaylistBox::Item *itemParent = m_searchCategories.value(searchCategory, 0);
414

Albert Astals Cid's avatar
Albert Astals Cid committed
415
    foreach(const QString &item, items) {
416
        itemKey = searchCategory + item;
417

418
        if(m_treeViewItems.contains(itemKey))
419
            continue;
420

421 422
        components.clear();
        components.append(PlaylistSearch::Component(item, false, columns, mode));
423

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

426
        TreeViewItemPlaylist *p = new TreeViewItemPlaylist(playlistBox(), s, item);
Michael Pyne's avatar
Michael Pyne committed
427
        playlistBox()->setupPlaylist(p, "audio-midi", itemParent);
428
        m_treeViewItems.insert(itemKey, p);
429
    }
430
}
431

432
void TreeViewMode::setDynamicListsFrozen(bool frozen)
433
{
434
    m_dynamicListsFrozen = frozen;
435

436 437
    if(frozen)
        return;
438

Albert Astals Cid's avatar
Albert Astals Cid committed
439
    foreach(const QString &pendingItem, m_pendingItemsToRemove) {
440 441
        m_treeViewItems[pendingItem]->deleteLater();
        m_treeViewItems.remove(pendingItem);
442 443 444 445 446
    }

    m_pendingItemsToRemove.clear();
}

447
void TreeViewMode::setupDynamicPlaylists()
448 449 450
{
    PlaylistBox::Item *i;
    PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
451

452
    i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Artists"));
453 454
    m_searchCategories.insert("artists", i);

455
    i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Albums"));
456 457
    m_searchCategories.insert("albums", i);

458
    i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Genres"));
459
    m_searchCategories.insert("genres", i);
460 461
}

462
#include "viewmode.moc"
463 464

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