File indexing completed on 2023-05-30 11:30:53

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