Added playlist actions (insert, delete, move up, move down), with only insert working...
[kworship.git] / kworship / playlist / KwPlaylistList.cpp
blob031d4d381e7b8905526db03e1d58fb1550d6e0c0
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file KwPlaylistList.cpp
22 * @brief A list of playlist items.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwPlaylistList.h"
27 #include "KwPlaylistListNode.h"
29 #include <QDomElement>
31 #include <cassert>
33 KW_REGISTER_PLAYLIST_ITEM(KwPlaylistList, "list")
36 * Constructors + destructor.
39 /// Default constructor.
40 KwPlaylistList::KwPlaylistList()
41 : KwPlaylistItem()
42 , m_playlist()
46 /// Construct from a DOM element.
47 KwPlaylistList::KwPlaylistList(const QDomElement& element, KwResourceManager* resourceManager)
48 : KwPlaylistItem(element, resourceManager)
50 elementsHandled("playlist_items");
51 QDomElement list = element.firstChildElement("playlist_items");
52 while (!list.isNull())
54 QDomNodeList children = list.childNodes();
55 for (int i = 0; i < children.count(); ++i)
57 QDomElement child = children.item(i).toElement();
58 if (!child.isNull())
60 KwPlaylistItem* item = KwPlaylistItem::createFromDom(child, resourceManager);
61 if (0 != item)
63 addItem(item);
67 list = list.nextSiblingElement("playlist_items");
71 /// Destructor.
72 KwPlaylistList::~KwPlaylistList()
77 * Child management.
80 /// Add a child.
81 void KwPlaylistList::addItem(KwPlaylistItem* item, int position)
83 if (position == -1)
85 m_playlist.push_back(item);
87 else
89 m_playlist.insert(position, item);
91 item->setParentScope(this);
94 /// Get the number of children.
95 int KwPlaylistList::getItemCount() const
97 return m_playlist.size();
100 /// Get a specific child.
101 KwPlaylistItem* KwPlaylistList::getItem(int index)
103 assert(index < getItemCount());
104 Playlist::iterator it = m_playlist.begin();
105 while (index && it != m_playlist.end())
107 ++it;
108 --index;
110 assert(it != m_playlist.end());
111 if (it == m_playlist.end())
113 return 0;
115 else
117 return *it;
120 /// Get a specific child.
121 const KwPlaylistItem* KwPlaylistList::getItem(int index) const
123 assert(index < getItemCount());
124 Playlist::const_iterator it = m_playlist.constBegin();
125 while (index && it != m_playlist.constEnd())
127 ++it;
128 --index;
130 assert(it != m_playlist.constEnd());
131 if (it == m_playlist.constEnd())
133 return 0;
135 else
137 return *it;
142 * DOM Translation.
145 QString KwPlaylistList::itemType() const
147 return "list";
150 void KwPlaylistList::exportDetailsToDom(QDomDocument& document, QDomElement& element, KwResourceManager* resourceManager) const
152 QDomElement children = document.createElement("playlist_items");
153 element.appendChild(children);
155 foreach (const KwPlaylistItem* item, m_playlist)
157 item->exportToDom(document, children, resourceManager);
162 * Main interface.
165 KwPlaylistNode* KwPlaylistList::getNode(KwPlaylistNode* parent)
167 return new KwPlaylistListNode(parent, this);