viewmode.cpp 13.2 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 20
#include <kiconloader.h>

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

#include "playlistbox.h"
26
#include "searchplaylist.h"
27
#include "treeviewitemplaylist.h"
28
#include "collectionlist.h"
Michael Pyne's avatar
Michael Pyne committed
29
#include "juk_debug.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
        // FIXME
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
    Q_UNUSED(item);
182
        // FIXME
183
    /*const PlaylistBox *box = item->listView();
184 185 186
    const int width = box->width() - box->verticalScrollBar()->width() - border * 2;
    const int baseHeight = 2 * box->itemMargin() + 32 + border * 2;
    const QFontMetrics fm = box->fontMetrics();
187
    item->setHeight(baseHeight + (fm.height() - fm.descent()) * lines(item, fm, width).count());*/
188 189
}

190
void ViewMode::updateHeights()
191
{
192
        // FIXME
193
    /*const int width = m_playlistBox->width() - m_playlistBox->verticalScrollBar()->width() - border * 2;
194

195 196
    const int baseHeight = 2 * m_playlistBox->itemMargin() + 32 +
                           border * 2 + 4;
197 198
    const QFontMetrics fm = m_playlistBox->fontMetrics();

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

206
    m_needsRefresh = false;*/
207 208
}

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

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

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

    if(width < 32)
233
        return QStringList();
234

235 236 237 238
    QString line = item->text();

    QStringList l;

239 240
    Q_UNUSED(fm);

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

Tim Beaulen's avatar
Tim Beaulen committed
255
        l.append(line.mid(0, textLength).trimmed());
256 257 258 259 260
        line = line.mid(textLength);
    }
    return l;
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

275
        // FIXME
276
/*void CompactViewMode::paintCell(PlaylistBox::Item *item,
277
                                QPainter *painter,
278 279 280
                                const QColorGroup &colorGroup,
                                int column, int width, int align)
{
Laurent Montel's avatar
Laurent Montel committed
281
    item->K3ListViewItem::paintCell(painter, colorGroup, column, width, align);
282 283
    if(item == item->listView()->dropItem())
        paintDropIndicator(painter, width, item->height());
284
}*/
285

286 287
QString CompactViewMode::name() const
{
288
    return i18nc("compact viewing mode", "Compact");
289 290
}

291 292
void CompactViewMode::setShown(bool shown)
{
293 294 295
    setVisible(shown);

    if(shown) {
296
        updateIcons(16);
297
        updateHeights();
298 299 300 301 302
    }
}

void CompactViewMode::updateHeights()
{
303
        // FIXME
304 305
    /*for(Q3ListViewItemIterator it(playlistBox()); it.current(); ++it)
        it.current()->setup();*/
306 307 308 309 310 311
}

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

312
TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b),
313
    m_dynamicListsFrozen(false), m_setup(false)
314 315 316 317 318 319 320 321 322
{

}

TreeViewMode::~TreeViewMode()
{

}

323 324 325 326 327
QString TreeViewMode::name() const
{
    return i18n("Tree");
}

328 329
void TreeViewMode::setShown(bool show)
{
330 331
    CompactViewMode::setShown(show);

332 333
    playlistBox()->setRootIsDecorated(show);

334
        // FIXME
335
    /*if(show) {
336
        PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
337

338
        if(!collectionItem)
339 340 341
            return;

        if(collectionItem && m_searchCategories.isEmpty())
342
            setupDynamicPlaylists();
343
        else {
344 345
            foreach(PlaylistBox::Item *item, m_searchCategories)
                item->setVisible(true);
346
        }
347

348 349 350 351 352 353 354
        if(!m_setup) {
            m_setup = true;
            playlistBox()->setSorting(-1);
            CollectionList::instance()->setupTreeViewEntries(this);
            playlistBox()->setSorting(0);
            playlistBox()->sort();
        }
355 356
    }
    else {
357 358
        foreach(PlaylistBox::Item *item, m_searchCategories)
            item->setVisible(false);
359
    }*/
360
}
361

362
void TreeViewMode::removeItem(const QString &item, unsigned column)
363
{
364 365 366
    if(!m_setup)
        return;

367 368
    QString itemKey;
    if(column == PlaylistItem::ArtistColumn)
369
        itemKey = "artists" + item;
370
    else if(column == PlaylistItem::GenreColumn)
371
        itemKey = "genres" + item;
372
    else if(column == PlaylistItem::AlbumColumn)
373
        itemKey = "albums" + item;
374
    else {
Michael Pyne's avatar
Michael Pyne committed
375
        qCWarning(JUK_LOG) << "Unhandled column type " << column;
376 377 378
        return;
    }

379
    if(!m_treeViewItems.contains(itemKey))
380 381
        return;

382
    TreeViewItemPlaylist *itemPlaylist = m_treeViewItems.value(itemKey, 0);
383

384 385 386
    if(m_dynamicListsFrozen) {
        m_pendingItemsToRemove << itemKey;
        return;
387 388
    }

389 390 391 392 393
    m_treeViewItems.remove(itemKey);
    itemPlaylist->deleteLater();
    emit signalPlaylistDestroyed(itemPlaylist);
}

394
void TreeViewMode::addItems(const QStringList &items, unsigned column)
395
{
396 397 398
    if(!m_setup)
        return;

399
    QString searchCategory;
400 401 402 403 404 405 406
    if(column == PlaylistItem::ArtistColumn)
        searchCategory = "artists";
    else if(column == PlaylistItem::GenreColumn)
        searchCategory = "genres";
    else if(column == PlaylistItem::AlbumColumn)
        searchCategory = "albums";
    else {
Michael Pyne's avatar
Michael Pyne committed
407
        qCWarning(JUK_LOG) << "Unhandled column type " << column;
408 409
        return;
    }
410

411
    ColumnList columns;
412
    columns.append(column);
413

414 415
    PlaylistSearch::Component::MatchMode mode = PlaylistSearch::Component::ContainsWord;
    if(column != PlaylistItem::ArtistColumn)
416
        mode = PlaylistSearch::Component::Exact;
417

418
    PlaylistSearch::ComponentList components;
419 420
    PlaylistList playlists;
    playlists.append(CollectionList::instance());
421

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

Albert Astals Cid's avatar
Albert Astals Cid committed
425
    foreach(const QString &item, items) {
426
        itemKey = searchCategory + item;
427

428
        if(m_treeViewItems.contains(itemKey))
429
            continue;
430

431 432
        components.clear();
        components.append(PlaylistSearch::Component(item, false, columns, mode));
433

434
        PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny, false);
435

436
        TreeViewItemPlaylist *p = new TreeViewItemPlaylist(playlistBox(), s, item);
Michael Pyne's avatar
Michael Pyne committed
437
        playlistBox()->setupPlaylist(p, "audio-midi", itemParent);
438
        m_treeViewItems.insert(itemKey, p);
439
    }
440
}
441

442
void TreeViewMode::setDynamicListsFrozen(bool frozen)
443
{
444
    m_dynamicListsFrozen = frozen;
445

446 447
    if(frozen)
        return;
448

Albert Astals Cid's avatar
Albert Astals Cid committed
449
    foreach(const QString &pendingItem, m_pendingItemsToRemove) {
450 451
        m_treeViewItems[pendingItem]->deleteLater();
        m_treeViewItems.remove(pendingItem);
452 453 454 455 456
    }

    m_pendingItemsToRemove.clear();
}

457
void TreeViewMode::setupDynamicPlaylists()
458 459 460
{
    PlaylistBox::Item *i;
    PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
461

462
    i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Artists"));
463 464
    m_searchCategories.insert("artists", i);

465
    i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Albums"));
466 467
    m_searchCategories.insert("albums", i);

468
    i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Genres"));
469
    m_searchCategories.insert("genres", i);
470 471
}

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