2 * @file KwPlaylistModel.cpp
3 * @brief A Qt model for playlist items.
4 * @author James Hogan <james@albanarts.com>
7 #include "KwPlaylistModel.h"
8 #include "KwPlaylistNode.h"
13 * Constructors + destructor.
16 /// Default constructor.
17 KwPlaylistModel::KwPlaylistModel(QObject
* parent
)
18 : QAbstractItemModel(parent
)
24 KwPlaylistModel::~KwPlaylistModel()
33 /// Set the root node.
34 void KwPlaylistModel::setRootNode(KwPlaylistNode
* root
)
41 KwPlaylistNode
* KwPlaylistModel::itemFromIndex(const QModelIndex
& index
) const
43 if (index
.isValid()) {
44 return reinterpret_cast<KwPlaylistNode
*>(index
.internalPointer());
50 QModelIndex
KwPlaylistModel::index(int row
, int column
, const QModelIndex
& parent
) const
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
);
65 KwPlaylistNode
* parentNode
= node
->getParent();
70 KwPlaylistNode
* grandParentNode
= parentNode
->getParent();
71 if (0 == grandParentNode
)
75 int row
= grandParentNode
->getChildIndex(parentNode
);
77 return createIndex(row
, child
.column(), parentNode
);
80 int KwPlaylistModel::rowCount(const QModelIndex
& parent
) const
82 KwPlaylistNode
* parentNode
= itemFromIndex(parent
);
87 return parentNode
->getChildCount();
90 int KwPlaylistModel::columnCount(const QModelIndex
& parent
) const
96 QVariant
KwPlaylistModel::data(const QModelIndex
& index
, int role
) const
98 KwPlaylistNode
* item
= itemFromIndex(index
);
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
)
114 else if (section
== 1)