viewmode.cpp 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (C) 2003-2004 Scott Wheeler <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.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
16

17 18
#include "viewmode.h"

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

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

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

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

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

ViewMode::~ViewMode()
{

}

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

58
    if(m_needsRefresh)
59
        updateHeights();
60

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

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

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

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

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

76 77 78 79
        newPen.setWidth(1);

        QColor background = m_playlistBox->palette().color(QPalette::Highlight);
        newPen.setColor(m_playlistBox->palette().color(QPalette::Text));
80
        painter->setPen(newPen);
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 117
        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);
118

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

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

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

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

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

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

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

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

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

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

void ViewMode::updateIcons(int size)
{
173
    for(QTreeWidgetItemIterator it(m_playlistBox); *it; ++it) {
174
        PlaylistBox::Item *i = static_cast<PlaylistBox::Item *>(*it);
175
        i->setIcon(0, SmallIcon(i->iconName(), size));
176 177 178
    }
}

179 180
void ViewMode::setupItem(PlaylistBox::Item *item) const
{
181
    /*const PlaylistBox *box = item->listView();
182 183 184
    const int width = box->width() - box->verticalScrollBar()->width() - border * 2;
    const int baseHeight = 2 * box->itemMargin() + 32 + border * 2;
    const QFontMetrics fm = box->fontMetrics();
185
    item->setHeight(baseHeight + (fm.height() - fm.descent()) * lines(item, fm, width).count());*/
186 187
}

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

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

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

203
    m_needsRefresh = false;*/
204 205
}

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

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

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

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

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

    QStringList l;

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

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

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

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

}

CompactViewMode::~CompactViewMode()
{

}

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

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

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

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

void CompactViewMode::updateHeights()
{
296 297
    /*for(Q3ListViewItemIterator it(playlistBox()); it.current(); ++it)
        it.current()->setup();*/
298 299 300 301 302 303
}

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

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

}

TreeViewMode::~TreeViewMode()
{

}

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

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

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

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

329
        if(!collectionItem)
330 331 332
            return;

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

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

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

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

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

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

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

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

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

390
    QString searchCategory;
391 392 393 394 395 396 397
    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
398
        kWarning() << "Unhandled column type " << column;
399 400
        return;
    }
401

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

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

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

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

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

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

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

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

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

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

437 438
    if(frozen)
        return;
439

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

    m_pendingItemsToRemove.clear();
}

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

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

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

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

463
#include "viewmode.moc"
464 465

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