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