Simple display preferences and song database code copied from playlist (will template...
[kworship.git] / kworship / playlist / KwPlaylistModel.cpp
blob2150fcd1218cbb86cf7736dcb3af04797ea03d1a
1 /**
2 * @file KwPlaylistModel.cpp
3 * @brief A Qt model for playlist items.
4 * @author James Hogan <james@albanarts.com>
5 */
7 #include "KwPlaylistModel.h"
8 #include "KwPlaylistNode.h"
10 #include <cassert>
13 * Constructors + destructor.
16 /// Default constructor.
17 KwPlaylistModel::KwPlaylistModel(QObject* parent)
18 : QAbstractItemModel(parent)
19 , m_root(0)
23 /// Destructor.
24 KwPlaylistModel::~KwPlaylistModel()
26 delete m_root;
30 * Main interface
33 /// Set the root node.
34 void KwPlaylistModel::setRootNode(KwPlaylistNode* root)
36 delete m_root;
37 m_root = root;
38 reset();
41 KwPlaylistNode* KwPlaylistModel::itemFromIndex(const QModelIndex& index) const
43 if (index.isValid()) {
44 return reinterpret_cast<KwPlaylistNode*>(index.internalPointer());
45 } else {
46 return m_root;
50 QModelIndex KwPlaylistModel::index(int row, int column, const QModelIndex& parent) const
52 if (0 == m_root)
54 return QModelIndex();
56 KwPlaylistNode* parentNode = itemFromIndex(parent);
57 assert(0 != parentNode);
58 return createIndex(row, column, parentNode->getChild(row));
61 QModelIndex KwPlaylistModel::parent(const QModelIndex& child) const
63 KwPlaylistNode* node = itemFromIndex(child);
64 assert(0 != node);
65 KwPlaylistNode* parentNode = node->getParent();
66 if (0 == parentNode)
68 return QModelIndex();
70 KwPlaylistNode* grandParentNode = parentNode->getParent();
71 if (0 == grandParentNode)
73 return QModelIndex();
75 int row = grandParentNode->getChildIndex(parentNode);
76 assert(row != -1);
77 return createIndex(row, child.column(), parentNode);
80 int KwPlaylistModel::rowCount(const QModelIndex& parent) const
82 KwPlaylistNode* parentNode = itemFromIndex(parent);
83 if (0 == parentNode)
85 return 0;
87 return parentNode->getChildCount();
90 int KwPlaylistModel::columnCount(const QModelIndex& parent) const
92 Q_UNUSED(parent)
93 return 2;
96 QVariant KwPlaylistModel::data(const QModelIndex& index, int role) const
98 KwPlaylistNode* item = itemFromIndex(index);
99 if (0 == item)
101 return QVariant();
103 return item->getData(role, index.column());
106 QVariant KwPlaylistModel::headerData(int section, Qt::Orientation orientation, int role) const
108 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
110 if (section == 0)
112 return tr("Node");
114 else if (section == 1)
116 return tr("Value");
119 return QVariant();