File indexing completed on 2021-12-21 13:28:02

0001 /**
0002  * Copyright (C) 2003-2004 Scott Wheeler <wheeler@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under
0005  * the terms of the GNU General Public License as published by the Free Software
0006  * Foundation; either version 2 of the License, or (at your option) any later
0007  * version.
0008  *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0012  *
0013  * You should have received a copy of the GNU General Public License along with
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.
0015  */
0016 
0017 #include "viewmode.h"
0018 
0019 #include <kiconloader.h>
0020 
0021 #include <QPixmap>
0022 #include <QPainter>
0023 #include <QResizeEvent>
0024 
0025 #include "playlistbox.h"
0026 #include "searchplaylist.h"
0027 #include "treeviewitemplaylist.h"
0028 #include "collectionlist.h"
0029 #include "juk_debug.h"
0030 
0031 ////////////////////////////////////////////////////////////////////////////////
0032 // ViewMode
0033 ////////////////////////////////////////////////////////////////////////////////
0034 
0035 ViewMode::ViewMode(PlaylistBox *b) :
0036     QObject(b),
0037     m_playlistBox(b),
0038     m_visible(false),
0039     m_needsRefresh(false)
0040 {
0041     m_playlistBox->viewport()->installEventFilter(this);
0042 }
0043 
0044 ViewMode::~ViewMode()
0045 {
0046     removeEventFilter(m_playlistBox->viewport());
0047 }
0048 
0049 bool ViewMode::eventFilter(QObject *watched, QEvent *e)
0050 {
0051     if(m_visible && watched == m_playlistBox->viewport() && e->type() == QEvent::Resize) {
0052         QResizeEvent *re = static_cast<QResizeEvent *>(e);
0053         if(re->size().width() != re->oldSize().width())
0054             m_needsRefresh = true;
0055     }
0056 
0057     if(e->type() == QEvent::Hide)
0058         m_needsRefresh = true;
0059 
0060     return QObject::eventFilter(watched, e);
0061 }
0062 
0063 QString ViewMode::name() const
0064 {
0065     return i18nc("the normal viewing mode", "Default");
0066 }
0067 
0068 void ViewMode::setShown(bool shown)
0069 {
0070     m_visible = shown;
0071     if(shown) {
0072         updateIcons();
0073         m_needsRefresh = true;
0074     }
0075 }
0076 
0077 void ViewMode::updateIcons()
0078 {
0079     for(QTreeWidgetItemIterator it(m_playlistBox); *it; ++it) {
0080         PlaylistBox::Item *i = static_cast<PlaylistBox::Item *>(*it);
0081         i->setIcon(0, QIcon::fromTheme(i->iconName()));
0082     }
0083 }
0084 
0085 void ViewMode::setupItem(PlaylistBox::Item *item) const
0086 {
0087     Q_UNUSED(item);
0088 }
0089 
0090 void ViewMode::paintDropIndicator(QPainter *painter, int width, int height) // static
0091 {
0092     static const int border = 1;
0093     static const int lineWidth = 2;
0094 
0095     QPen oldPen = painter->pen();
0096     QPen newPen = oldPen;
0097 
0098     newPen.setWidth(lineWidth);
0099     newPen.setStyle(Qt::DotLine);
0100 
0101     painter->setPen(newPen);
0102     painter->drawRect(border, border, width - border * 2, height - border * 2);
0103     painter->setPen(oldPen);
0104 }
0105 
0106 ///////////////////////////////////////////////////////////////////////////////
0107 // CompactViewMode
0108 ////////////////////////////////////////////////////////////////////////////////
0109 
0110 CompactViewMode::CompactViewMode(PlaylistBox *b) : ViewMode(b)
0111 {
0112 
0113 }
0114 
0115 CompactViewMode::~CompactViewMode()
0116 {
0117 
0118 }
0119 
0120 QString CompactViewMode::name() const
0121 {
0122     return i18nc("compact viewing mode", "Compact");
0123 }
0124 
0125 void CompactViewMode::setShown(bool shown)
0126 {
0127     setVisible(shown);
0128 
0129     if(shown) {
0130         updateIcons();
0131     }
0132 }
0133 
0134 ////////////////////////////////////////////////////////////////////////////////
0135 // TreeViewMode
0136 ////////////////////////////////////////////////////////////////////////////////
0137 
0138 TreeViewMode::TreeViewMode(PlaylistBox *b) : CompactViewMode(b),
0139     m_dynamicListsFrozen(false), m_setup(false)
0140 {
0141 
0142 }
0143 
0144 TreeViewMode::~TreeViewMode()
0145 {
0146 
0147 }
0148 
0149 QString TreeViewMode::name() const
0150 {
0151     return i18n("Tree");
0152 }
0153 
0154 void TreeViewMode::setShown(bool show)
0155 {
0156     CompactViewMode::setShown(show);
0157 
0158     playlistBox()->setRootIsDecorated(show);
0159 
0160     if(show) {
0161         if(m_searchCategories.isEmpty())
0162             setupDynamicPlaylists();
0163         else {
0164             for(auto &item : m_searchCategories)
0165                 item->setHidden(false);
0166         }
0167 
0168         if(!m_setup) {
0169             m_setup = true;
0170             playlistBox()->setSortingEnabled(false);
0171             CollectionList::instance()->setupTreeViewEntries(this);
0172             playlistBox()->setSortingEnabled(true);
0173         }
0174     }
0175     else {
0176         for(auto &item : m_searchCategories)
0177             item->setHidden(true);
0178     }
0179 }
0180 
0181 void TreeViewMode::removeItem(const QString &item, unsigned column)
0182 {
0183     if(!m_setup)
0184         return;
0185 
0186     QString itemKey;
0187     if(column == PlaylistItem::ArtistColumn)
0188         itemKey = "artists" + item;
0189     else if(column == PlaylistItem::GenreColumn)
0190         itemKey = "genres" + item;
0191     else if(column == PlaylistItem::AlbumColumn)
0192         itemKey = "albums" + item;
0193     else {
0194         qCWarning(JUK_LOG) << "Unhandled column type " << column;
0195         return;
0196     }
0197 
0198     if(!m_treeViewItems.contains(itemKey))
0199         return;
0200 
0201     TreeViewItemPlaylist *itemPlaylist = m_treeViewItems.value(itemKey, 0);
0202 
0203     if(m_dynamicListsFrozen) {
0204         m_pendingItemsToRemove << itemKey;
0205         return;
0206     }
0207 
0208     m_treeViewItems.remove(itemKey);
0209     itemPlaylist->deleteLater();
0210     emit signalPlaylistDestroyed(itemPlaylist);
0211 }
0212 
0213 void TreeViewMode::addItems(const QStringList &items, unsigned column)
0214 {
0215     if(!m_setup)
0216         return;
0217 
0218     QString searchCategory;
0219     if(column == PlaylistItem::ArtistColumn)
0220         searchCategory = "artists";
0221     else if(column == PlaylistItem::GenreColumn)
0222         searchCategory = "genres";
0223     else if(column == PlaylistItem::AlbumColumn)
0224         searchCategory = "albums";
0225     else {
0226         qCWarning(JUK_LOG) << "Unhandled column type " << column;
0227         return;
0228     }
0229 
0230     ColumnList columns;
0231     columns.append(column);
0232 
0233     PlaylistSearch::Component::MatchMode mode = PlaylistSearch::Component::ContainsWord;
0234     if(column != PlaylistItem::ArtistColumn)
0235         mode = PlaylistSearch::Component::Exact;
0236 
0237     PlaylistSearch::ComponentList components;
0238     PlaylistList playlists;
0239     playlists.append(CollectionList::instance());
0240 
0241     QString itemKey;
0242     PlaylistBox::Item *itemParent = m_searchCategories.value(searchCategory, 0);
0243 
0244     foreach(const QString &item, items) {
0245         itemKey = searchCategory + item;
0246 
0247         if(m_treeViewItems.contains(itemKey))
0248             continue;
0249 
0250         components.clear();
0251         components.append(PlaylistSearch::Component(item, false, columns, mode));
0252 
0253         PlaylistSearch s(playlists, components, PlaylistSearch::MatchAny);
0254 
0255         TreeViewItemPlaylist *p = new TreeViewItemPlaylist(playlistBox(), s, item);
0256         playlistBox()->setupPlaylist(p, "audio-midi", itemParent);
0257         m_treeViewItems.insert(itemKey, p);
0258     }
0259 }
0260 
0261 void TreeViewMode::setDynamicListsFrozen(bool frozen)
0262 {
0263     m_dynamicListsFrozen = frozen;
0264 
0265     if(frozen)
0266         return;
0267 
0268     foreach(const QString &pendingItem, m_pendingItemsToRemove) {
0269         m_treeViewItems[pendingItem]->deleteLater();
0270         m_treeViewItems.remove(pendingItem);
0271     }
0272 
0273     m_pendingItemsToRemove.clear();
0274 }
0275 
0276 void TreeViewMode::setupDynamicPlaylists()
0277 {
0278     PlaylistBox::Item *i;
0279     PlaylistBox::Item *collectionItem = PlaylistBox::Item::collectionItem();
0280 
0281     i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Artists"));
0282     m_searchCategories.insert("artists", i);
0283 
0284     i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Albums"));
0285     m_searchCategories.insert("albums", i);
0286 
0287     i = new PlaylistBox::Item(collectionItem, "media-optical-audio", i18n("Genres"));
0288     m_searchCategories.insert("genres", i);
0289 }
0290 
0291 // vim: set et sw=4 tw=0 sta: