viewmode.cpp 7.38 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 50
bool ViewMode::eventFilter(QObject *watched, QEvent *e)
{
51
    if(m_visible && watched == m_playlistBox->viewport() && e->type() == QEvent::Resize) {
52 53
        QResizeEvent *re = static_cast<QResizeEvent *>(e);
        if(re->size().width() != re->oldSize().width())
54
            m_needsRefresh = true;
55
    }
56

57
    if(e->type() == QEvent::Hide)
58
        m_needsRefresh = true;
59

60 61 62
    return QObject::eventFilter(watched, e);
}

63 64
QString ViewMode::name() const
{
65
    return i18nc("the normal viewing mode", "Default");
66 67
}

68 69 70
void ViewMode::setShown(bool shown)
{
    m_visible = shown;
71
    if(shown) {
72
        updateIcons();
73
        m_needsRefresh = true;
74
    }
75 76
}

77
void ViewMode::updateIcons()
78
{
79
    for(QTreeWidgetItemIterator it(m_playlistBox); *it; ++it) {
80
        PlaylistBox::Item *i = static_cast<PlaylistBox::Item *>(*it);
81
        i->setIcon(0, QIcon::fromTheme(i->iconName()));
82 83 84
    }
}

85 86
void ViewMode::setupItem(PlaylistBox::Item *item) const
{
87
    Q_UNUSED(item);
88 89
}

90 91 92 93 94 95 96 97 98
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);
99
    newPen.setStyle(Qt::DotLine);
100 101 102 103 104 105

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

106 107 108 109 110 111 112 113 114 115 116 117 118 119
///////////////////////////////////////////////////////////////////////////////
// CompactViewMode
////////////////////////////////////////////////////////////////////////////////

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

}

CompactViewMode::~CompactViewMode()
{

}

120 121
QString CompactViewMode::name() const
{
122
    return i18nc("compact viewing mode", "Compact");
123 124
}

125 126
void CompactViewMode::setShown(bool shown)
{
127 128 129
    setVisible(shown);

    if(shown) {
130
        updateIcons();
131 132 133
    }
}

134 135 136 137
////////////////////////////////////////////////////////////////////////////////
// TreeViewMode
////////////////////////////////////////////////////////////////////////////////

138
TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b),
139
    m_dynamicListsFrozen(false), m_setup(false)
140 141 142 143 144 145 146 147 148
{

}

TreeViewMode::~TreeViewMode()
{

}

149 150 151 152 153
QString TreeViewMode::name() const
{
    return i18n("Tree");
}

154 155
void TreeViewMode::setShown(bool show)
{
156 157
    CompactViewMode::setShown(show);

158 159
    playlistBox()->setRootIsDecorated(show);

160 161
    if(show) {
        if(m_searchCategories.isEmpty())
162
            setupDynamicPlaylists();
163
        else {
164 165
            for(auto &item : m_searchCategories)
                item->setHidden(false);
166
        }
167

168 169
        if(!m_setup) {
            m_setup = true;
170
            playlistBox()->setSortingEnabled(false);
171
            CollectionList::instance()->setupTreeViewEntries(this);
172
            playlistBox()->setSortingEnabled(true);
173
        }
174 175
    }
    else {
176 177 178
        for(auto &item : m_searchCategories)
            item->setHidden(true);
    }
179
}
180

181
void TreeViewMode::removeItem(const QString &item, unsigned column)
182
{
183 184 185
    if(!m_setup)
        return;

186 187
    QString itemKey;
    if(column == PlaylistItem::ArtistColumn)
188
        itemKey = "artists" + item;
189
    else if(column == PlaylistItem::GenreColumn)
190
        itemKey = "genres" + item;
191
    else if(column == PlaylistItem::AlbumColumn)
192
        itemKey = "albums" + item;
193
    else {
Michael Pyne's avatar
Michael Pyne committed
194
        qCWarning(JUK_LOG) << "Unhandled column type " << column;
195 196 197
        return;
    }

198
    if(!m_treeViewItems.contains(itemKey))
199 200
        return;

201
    TreeViewItemPlaylist *itemPlaylist = m_treeViewItems.value(itemKey, 0);
202

203 204 205
    if(m_dynamicListsFrozen) {
        m_pendingItemsToRemove << itemKey;
        return;
206 207
    }

208 209 210 211 212
    m_treeViewItems.remove(itemKey);
    itemPlaylist->deleteLater();
    emit signalPlaylistDestroyed(itemPlaylist);
}

213
void TreeViewMode::addItems(const QStringList &items, unsigned column)
214
{
215 216 217
    if(!m_setup)
        return;

218
    QString searchCategory;
219 220 221 222 223 224 225
    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
226
        qCWarning(JUK_LOG) << "Unhandled column type " << column;
227 228
        return;
    }
229

230
    ColumnList columns;
231
    columns.append(column);
232

233 234
    PlaylistSearch::Component::MatchMode mode = PlaylistSearch::Component::ContainsWord;
    if(column != PlaylistItem::ArtistColumn)
235
        mode = PlaylistSearch::Component::Exact;
236

237
    PlaylistSearch::ComponentList components;
238 239
    PlaylistList playlists;
    playlists.append(CollectionList::instance());
240

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

Albert Astals Cid's avatar
Albert Astals Cid committed
244
    foreach(const QString &item, items) {
245
        itemKey = searchCategory + item;
246

247
        if(m_treeViewItems.contains(itemKey))
248
            continue;
249

250 251
        components.clear();
        components.append(PlaylistSearch::Component(item, false, columns, mode));
252

253
        PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny);
254

255
        TreeViewItemPlaylist *p = new TreeViewItemPlaylist(playlistBox(), s, item);
Michael Pyne's avatar
Michael Pyne committed
256
        playlistBox()->setupPlaylist(p, "audio-midi", itemParent);
257
        m_treeViewItems.insert(itemKey, p);
258
    }
259
}
260

261
void TreeViewMode::setDynamicListsFrozen(bool frozen)
262
{
263
    m_dynamicListsFrozen = frozen;
264

265 266
    if(frozen)
        return;
267

Albert Astals Cid's avatar
Albert Astals Cid committed
268
    foreach(const QString &pendingItem, m_pendingItemsToRemove) {
269 270
        m_treeViewItems[pendingItem]->deleteLater();
        m_treeViewItems.remove(pendingItem);
271 272 273 274 275
    }

    m_pendingItemsToRemove.clear();
}

276
void TreeViewMode::setupDynamicPlaylists()
277 278 279
{
    PlaylistBox::Item *i;
    PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
280

281
    i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Artists"));
282 283
    m_searchCategories.insert("artists", i);

284
    i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Albums"));
285 286
    m_searchCategories.insert("albums", i);

287
    i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Genres"));
288
    m_searchCategories.insert("genres", i);
289 290
}

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