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
#include <kiconloader.h>
20
#include <kstandarddirs.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"
Michael Pyne's avatar
Michael Pyne committed
30
#include "juk_debug.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
        // FIXME
51
/*void ViewMode::paintCell(PlaylistBox::Item *item,
52
                         QPainter *painter,
53
                         const QColorGroup &colorGroup,
54
                         int column, int width, int)
55
{
56
    if(width < item->pixmap(column)->width())
57
        return;
58

59
    if(m_needsRefresh)
60
        updateHeights();
61

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

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

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

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

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

77 78 79 80
        newPen.setWidth(1);

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

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

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

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

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

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

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

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

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

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

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

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

180 181
void ViewMode::setupItem(PlaylistBox::Item *item) const
{
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 239 240
    QString line = item->text();

    QStringList l;

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

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

259 260 261 262 263 264 265 266 267 268 269 270 271 272
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

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

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

289 290
void CompactViewMode::setShown(bool shown)
{
291 292 293
    setVisible(shown);

    if(shown) {
294
        updateIcons(16);
295
        updateHeights();
296 297 298 299 300
    }
}

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

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

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

}

TreeViewMode::~TreeViewMode()
{

}

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

326 327
void TreeViewMode::setShown(bool show)
{
328 329
    CompactViewMode::setShown(show);

330 331
    playlistBox()->setRootIsDecorated(show);

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

336
        if(!collectionItem)
337 338 339
            return;

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

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

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

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

377
    if(!m_treeViewItems.contains(itemKey))
378 379
        return;

380
    TreeViewItemPlaylist *itemPlaylist = m_treeViewItems.value(itemKey, 0);
381

382 383 384
    if(m_dynamicListsFrozen) {
        m_pendingItemsToRemove << itemKey;
        return;
385 386
    }

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

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

397
    QString searchCategory;
398 399 400 401 402 403 404
    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
405
        qCWarning(JUK_LOG) << "Unhandled column type " << column;
406 407
        return;
    }
408

409
    ColumnList columns;
410
    columns.append(column);
411

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

416
    PlaylistSearch::ComponentList components;
417 418
    PlaylistList playlists;
    playlists.append(CollectionList::instance());
419

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

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

426
        if(m_treeViewItems.contains(itemKey))
427
            continue;
428

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

432
        PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny, false);
433

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

440
void TreeViewMode::setDynamicListsFrozen(bool frozen)
441
{
442
    m_dynamicListsFrozen = frozen;
443

444 445
    if(frozen)
        return;
446

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

    m_pendingItemsToRemove.clear();
}

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

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

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

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

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