viewmode.cpp 11.8 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 67 68 69 70 71 72

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

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

        newPen.setWidth(5);
Laurent Montel's avatar
Laurent Montel committed
73
        newPen.setJoinStyle(Qt::RoundJoin);
74
        newPen.setColor(QPalette::Highlight);
75 76 77 78 79 80

        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,
81
                          colorGroup.brush(QPalette::Highlight));
Stephan Kulow's avatar
Stephan Kulow committed
82
        painter->setPen(colorGroup.color( QPalette::HighlightedText));
83 84
    }
    else
85
        painter->eraseRect(0, 0, width, item->height());
86

87
    if(!pm->isNull()) {
88
        int x = (width - pm->width()) / 2;
89
        x = qMax(x, item->listView()->itemMargin());
90 91
        painter->drawPixmap(x, y, *pm);
    }
92

93
    y += pm->height() + fm.height() - fm.descent();
94 95 96

    foreach(QString line, m_lines[item]) {
        int x = (width - fm.width(line)) / 2;
97
        x = qMax(x, item->listView()->itemMargin());
98
        painter->drawText(x, y, line);
99 100
        y += fm.height() - fm.descent();
    }
101 102 103

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

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

114
    if(e->type() == QEvent::Hide)
115
        m_needsRefresh = true;
116

117 118 119
    return QObject::eventFilter(watched, e);
}

120 121 122 123 124
QString ViewMode::name() const
{
    return i18n("Default");
}

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

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

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

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

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

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

    m_needsRefresh = false;
166 167
}

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

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

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

    if(width < 32)
192
        return QStringList();
193

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

    QStringList l;

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

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

217 218 219 220 221 222 223 224 225 226 227 228 229 230
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

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

241 242 243 244 245
QString CompactViewMode::name() const
{
    return i18n("Compact");
}

246 247
void CompactViewMode::setShown(bool shown)
{
248 249 250
    setVisible(shown);

    if(shown) {
251
        updateIcons(16);
252
        updateHeights();
253 254 255 256 257
    }
}

void CompactViewMode::updateHeights()
{
Laurent Montel's avatar
Laurent Montel committed
258
    for(Q3ListViewItemIterator it(playlistBox()); it.current(); ++it)
259
        it.current()->setup();
260 261 262 263 264 265
}

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

266
TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b),
267
    m_dynamicListsFrozen(false), m_setup(false)
268 269 270 271 272 273 274 275 276
{

}

TreeViewMode::~TreeViewMode()
{

}

277 278 279 280 281
QString TreeViewMode::name() const
{
    return i18n("Tree");
}

282 283
void TreeViewMode::setShown(bool show)
{
284 285
    CompactViewMode::setShown(show);

286 287
    playlistBox()->setRootIsDecorated(show);

288
    if(show) {
289
        PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
290

291
        if(!collectionItem)
292 293 294
            return;

        if(collectionItem && m_searchCategories.isEmpty())
295
            setupDynamicPlaylists();
296
        else {
297 298
            foreach(PlaylistBox::Item *item, m_searchCategories)
                item->setVisible(true);
299
        }
300

301 302 303 304 305 306 307
        if(!m_setup) {
            m_setup = true;
            playlistBox()->setSorting(-1);
            CollectionList::instance()->setupTreeViewEntries(this);
            playlistBox()->setSorting(0);
            playlistBox()->sort();
        }
308 309
    }
    else {
310 311
        foreach(PlaylistBox::Item *item, m_searchCategories)
            item->setVisible(false);
312
    }
313
}
314

315
void TreeViewMode::removeItem(const QString &item, unsigned column)
316
{
317 318 319
    if(!m_setup)
        return;

320 321
    QString itemKey;
    if(column == PlaylistItem::ArtistColumn)
322
        itemKey = "artists" + item;
323
    else if(column == PlaylistItem::GenreColumn)
324
        itemKey = "genres" + item;
325
    else if(column == PlaylistItem::AlbumColumn)
326
        itemKey = "albums" + item;
327
    else {
Thiago Macieira's avatar
Thiago Macieira committed
328
        kWarning() << "Unhandled column type " << column;
329 330 331
        return;
    }

332
    if(!m_treeViewItems.contains(itemKey))
333 334
        return;

335
    TreeViewItemPlaylist *itemPlaylist = m_treeViewItems.value(itemKey, 0);
336

337 338 339
    if(m_dynamicListsFrozen) {
        m_pendingItemsToRemove << itemKey;
        return;
340 341
    }

342 343 344 345 346
    m_treeViewItems.remove(itemKey);
    itemPlaylist->deleteLater();
    emit signalPlaylistDestroyed(itemPlaylist);
}

347
void TreeViewMode::addItems(const QStringList &items, unsigned column)
348
{
349 350 351
    if(!m_setup)
        return;

352
    QString searchCategory;
353 354 355 356 357 358 359
    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
360
        kWarning() << "Unhandled column type " << column;
361 362
        return;
    }
363

364
    ColumnList columns;
365
    columns.append(column);
366

367 368
    PlaylistSearch::Component::MatchMode mode = PlaylistSearch::Component::ContainsWord;
    if(column != PlaylistItem::ArtistColumn)
369
        mode = PlaylistSearch::Component::Exact;
370

371
    PlaylistSearch::ComponentList components;
372 373
    PlaylistList playlists;
    playlists.append(CollectionList::instance());
374

375
    QString itemKey, item;
376
    PlaylistBox::Item *itemParent = m_searchCategories.value(searchCategory, 0);
377

378
    foreach(item, items) {
379
        itemKey = searchCategory + item;
380

381
        if(m_treeViewItems.contains(itemKey))
382
            continue;
383

384 385
        components.clear();
        components.append(PlaylistSearch::Component(item, false, columns, mode));
386

387
        PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny, false);
388

389 390 391
        TreeViewItemPlaylist *p = new TreeViewItemPlaylist(playlistBox(), s, item);
        playlistBox()->setupPlaylist(p, "midi", itemParent);
        m_treeViewItems.insert(itemKey, p);
392
    }
393
}
394

395
void TreeViewMode::setDynamicListsFrozen(bool frozen)
396
{
397
    m_dynamicListsFrozen = frozen;
398

399 400
    if(frozen)
        return;
401

402 403 404
    foreach(QString pendingItem, m_pendingItemsToRemove) {
        m_treeViewItems[pendingItem]->deleteLater();
        m_treeViewItems.remove(pendingItem);
405 406 407 408 409
    }

    m_pendingItemsToRemove.clear();
}

410
void TreeViewMode::setupDynamicPlaylists()
411 412 413
{
    PlaylistBox::Item *i;
    PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
414

415 416 417 418 419 420
    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);

421 422
    i = new PlaylistBox::Item(collectionItem, "cdimage", i18n("Genres"));
    m_searchCategories.insert("genres", i);
423 424
}

425
#include "viewmode.moc"
426 427

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