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
#include <Q3ValueList>
#include <QResizeEvent>
#include <QEvent>
28 29 30

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

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

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

ViewMode::~ViewMode()
{

}

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

61
    if(m_needsRefresh)
62
        updateHeights();
63

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

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

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

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

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

        newPen.setWidth(5);
Laurent Montel's avatar
Laurent Montel committed
77
        newPen.setJoinStyle(Qt::RoundJoin);
78
        newPen.setColor(QPalette::Highlight);
79 80 81 82 83 84

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

    if (!pm->isNull()) {
        int x = (width - pm->width()) / 2;
93
        x = qMax(x, item->listView()->itemMargin());
94 95 96
        painter->drawPixmap(x, y, *pm);
    }
    y += pm->height() + fm.height() - fm.descent();
97
    for(QStringList::Iterator it = m_lines[item].begin(); it != m_lines[item].end(); ++it) {
98
        int x = (width - fm.width(*it)) / 2;
99
        x = qMax(x, item->listView()->itemMargin());
100 101 102
        painter->drawText(x, y, *it);
        y += fm.height() - fm.descent();
    }
103 104 105

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

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

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

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

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

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

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

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

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

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

    m_needsRefresh = false;
163 164
}

165 166 167 168 169 170 171 172 173
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);
174
    newPen.setStyle(Qt::DotLine);
175 176 177 178 179 180

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

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

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

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

    QStringList l;

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

Tim Beaulen's avatar
Tim Beaulen committed
208
        l.append(line.mid(0, textLength).trimmed());
209 210 211 212 213
        line = line.mid(textLength);
    }
    return l;
}

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

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

}

CompactViewMode::~CompactViewMode()
{

}

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

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

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

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

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

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

}

TreeViewMode::~TreeViewMode()
{

}

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

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

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

278
        if(!collectionItem)
279 280 281
            return;

        if(collectionItem && m_searchCategories.isEmpty())
282
            setupDynamicPlaylists();
283
        else {
Laurent Montel's avatar
Laurent Montel committed
284
            for(Q3DictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
285
                it.current()->setVisible(true);
286
        }
287 288 289 290 291 292 293
        if(!m_setup) {
            m_setup = true;
            playlistBox()->setSorting(-1);
            CollectionList::instance()->setupTreeViewEntries(this);
            playlistBox()->setSorting(0);
            playlistBox()->sort();
        }
294 295
    }
    else {
Laurent Montel's avatar
Laurent Montel committed
296
        for(Q3DictIterator<PlaylistBox::Item> it(m_searchCategories); it.current(); ++it)
297
            it.current()->setVisible(false);
298
    }
299
}
300

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

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

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

    TreeViewItemPlaylist *itemPlaylist = m_treeViewItems[itemKey];
322

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    m_pendingItemsToRemove.clear();
}

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

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

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

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

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

}

427
#include "viewmode.moc"
428 429

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