|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:BSD$ |
|
10 ** You may use this file under the terms of the BSD license as follows: |
|
11 ** |
|
12 ** "Redistribution and use in source and binary forms, with or without |
|
13 ** modification, are permitted provided that the following conditions are |
|
14 ** met: |
|
15 ** * Redistributions of source code must retain the above copyright |
|
16 ** notice, this list of conditions and the following disclaimer. |
|
17 ** * Redistributions in binary form must reproduce the above copyright |
|
18 ** notice, this list of conditions and the following disclaimer in |
|
19 ** the documentation and/or other materials provided with the |
|
20 ** distribution. |
|
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
|
22 ** the names of its contributors may be used to endorse or promote |
|
23 ** products derived from this software without specific prior written |
|
24 ** permission. |
|
25 ** |
|
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
|
37 ** $QT_END_LICENSE$ |
|
38 ** |
|
39 ****************************************************************************/ |
|
40 |
|
41 #include "playlistmodel.h" |
|
42 |
|
43 #include <QtCore/qfileinfo.h> |
|
44 #include <QtCore/qurl.h> |
|
45 |
|
46 #include <qmediaplaylist.h> |
|
47 |
|
48 PlaylistModel::PlaylistModel(QObject *parent) |
|
49 : QAbstractItemModel(parent) |
|
50 , m_playlist(0) |
|
51 { |
|
52 } |
|
53 |
|
54 int PlaylistModel::rowCount(const QModelIndex &parent) const |
|
55 { |
|
56 return m_playlist && !parent.isValid() ? m_playlist->mediaCount() : 0; |
|
57 } |
|
58 |
|
59 int PlaylistModel::columnCount(const QModelIndex &parent) const |
|
60 { |
|
61 return !parent.isValid() ? ColumnCount : 0; |
|
62 } |
|
63 |
|
64 QModelIndex PlaylistModel::index(int row, int column, const QModelIndex &parent) const |
|
65 { |
|
66 return m_playlist && !parent.isValid() |
|
67 && row >= 0 && row < m_playlist->mediaCount() |
|
68 && column >= 0 && column < ColumnCount |
|
69 ? createIndex(row, column) |
|
70 : QModelIndex(); |
|
71 } |
|
72 |
|
73 QModelIndex PlaylistModel::parent(const QModelIndex &child) const |
|
74 { |
|
75 Q_UNUSED(child); |
|
76 |
|
77 return QModelIndex(); |
|
78 } |
|
79 |
|
80 QVariant PlaylistModel::data(const QModelIndex &index, int role) const |
|
81 { |
|
82 if (index.isValid() && role == Qt::DisplayRole) { |
|
83 QVariant value = m_data[index]; |
|
84 if (!value.isValid() && index.column() == Title) { |
|
85 QUrl location = m_playlist->media(index.row()).canonicalUrl(); |
|
86 return QFileInfo(location.path()).fileName(); |
|
87 } |
|
88 |
|
89 return value; |
|
90 } |
|
91 return QVariant(); |
|
92 } |
|
93 |
|
94 QMediaPlaylist *PlaylistModel::playlist() const |
|
95 { |
|
96 return m_playlist; |
|
97 } |
|
98 |
|
99 void PlaylistModel::setPlaylist(QMediaPlaylist *playlist) |
|
100 { |
|
101 if (m_playlist) { |
|
102 disconnect(m_playlist, SIGNAL(mediaAboutToBeInserted(int,int)), this, SLOT(beginInsertItems(int,int))); |
|
103 disconnect(m_playlist, SIGNAL(mediaInserted(int,int)), this, SLOT(endInsertItems())); |
|
104 disconnect(m_playlist, SIGNAL(mediaAboutToBeRemoved(int,int)), this, SLOT(beginRemoveItems(int,int))); |
|
105 disconnect(m_playlist, SIGNAL(mediaRemoved(int,int)), this, SLOT(endRemoveItems())); |
|
106 disconnect(m_playlist, SIGNAL(mediaChanged(int,int)), this, SLOT(changeItems(int,int))); |
|
107 } |
|
108 |
|
109 m_playlist = playlist; |
|
110 |
|
111 if (m_playlist) { |
|
112 connect(m_playlist, SIGNAL(mediaAboutToBeInserted(int,int)), this, SLOT(beginInsertItems(int,int))); |
|
113 connect(m_playlist, SIGNAL(mediaInserted(int,int)), this, SLOT(endInsertItems())); |
|
114 connect(m_playlist, SIGNAL(mediaAboutToBeRemoved(int,int)), this, SLOT(beginRemoveItems(int,int))); |
|
115 connect(m_playlist, SIGNAL(mediaRemoved(int,int)), this, SLOT(endRemoveItems())); |
|
116 connect(m_playlist, SIGNAL(mediaChanged(int,int)), this, SLOT(changeItems(int,int))); |
|
117 } |
|
118 |
|
119 |
|
120 reset(); |
|
121 } |
|
122 |
|
123 bool PlaylistModel::setData(const QModelIndex &index, const QVariant &value, int role) |
|
124 { |
|
125 Q_UNUSED(role); |
|
126 m_data[index] = value; |
|
127 emit dataChanged(index, index); |
|
128 return true; |
|
129 } |
|
130 |
|
131 void PlaylistModel::beginInsertItems(int start, int end) |
|
132 { |
|
133 m_data.clear(); |
|
134 beginInsertRows(QModelIndex(), start, end); |
|
135 } |
|
136 |
|
137 void PlaylistModel::endInsertItems() |
|
138 { |
|
139 endInsertRows(); |
|
140 } |
|
141 |
|
142 void PlaylistModel::beginRemoveItems(int start, int end) |
|
143 { |
|
144 m_data.clear(); |
|
145 beginRemoveRows(QModelIndex(), start, end); |
|
146 } |
|
147 |
|
148 void PlaylistModel::endRemoveItems() |
|
149 { |
|
150 endInsertRows(); |
|
151 } |
|
152 |
|
153 void PlaylistModel::changeItems(int start, int end) |
|
154 { |
|
155 m_data.clear(); |
|
156 emit dataChanged(index(start,0), index(end,ColumnCount)); |
|
157 } |
|
158 |
|
159 |