File indexing completed on 2024-05-05 05:40:29
0001 /*************************************************************************** 0002 * Copyright (C) 2014 by Renaud Guezennec * 0003 * https://rolisteam.org/contact * 0004 * * 0005 * rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "model/musicmodel.h" 0021 0022 #include <QFont> 0023 #include <QMimeData> 0024 #include <QNetworkRequest> 0025 #include <QSet> 0026 #include <QUrl> 0027 0028 MusicModel::MusicModel(QObject* parent) : QAbstractListModel(parent), m_header({tr("Title")}) {} 0029 0030 int MusicModel::rowCount(const QModelIndex& parent) const 0031 { 0032 if(parent.isValid()) 0033 return 0; 0034 return m_data.size(); 0035 } 0036 0037 QVariant MusicModel::headerData(int section, Qt::Orientation orientation, int role) const 0038 { 0039 if(orientation == Qt::Vertical) 0040 return {}; 0041 0042 if(Qt::DisplayRole == role) 0043 { 0044 return m_header[section]; 0045 } 0046 0047 return {}; 0048 } 0049 0050 namespace 0051 { 0052 QFont boldFont() 0053 { 0054 QFont font; 0055 font.setBold(true); 0056 return font; 0057 } 0058 0059 QString normalizeUrl(const QUrl& url) 0060 { 0061 if(url.isLocalFile() || url.host().contains("tabletopaudio.com") == false) 0062 return url.fileName(); 0063 0064 QString str= url.toString(); 0065 str= str.right(str.size() - (str.lastIndexOf("=") + 1)); 0066 return str.replace(".mp3", "").replace("_", " "); 0067 } 0068 } // namespace 0069 0070 QVariant MusicModel::data(const QModelIndex& index, int role) const 0071 { 0072 if(!index.isValid() || m_data.size() <= index.row()) 0073 return {}; 0074 0075 // Break early if role is not Diplay or Font. 0076 QSet<int> set({Qt::DisplayRole, Qt::FontRole, Qt::ToolTipRole, TITLE, URL}); 0077 if(!set.contains(role)) 0078 return {}; 0079 0080 auto url= m_data.at(index.row()); 0081 QVariant var; 0082 // qDebug() << "data song:" << m_currentSong << url; 0083 switch(role) 0084 { 0085 case Qt::DisplayRole: 0086 case TITLE: 0087 var= url.fileName(); 0088 break; 0089 case Qt::ToolTipRole: 0090 case URL: 0091 var= url; 0092 break; 0093 case Qt::FontRole: 0094 if(url.toString() == m_currentSong.toString()) 0095 { 0096 var= QVariant(boldFont()); 0097 } 0098 break; 0099 default: 0100 break; 0101 } 0102 0103 return var; 0104 } 0105 0106 void MusicModel::addSong(const QList<QUrl>& list) 0107 { 0108 if(list.isEmpty()) 0109 return; 0110 0111 beginInsertRows(QModelIndex(), m_data.size(), m_data.size() + list.size() - 1); 0112 m_data.reserve(m_data.size() + list.size()); 0113 0114 for(auto& url : list) 0115 { 0116 m_data.append(url); 0117 } 0118 endInsertRows(); 0119 } 0120 void MusicModel::insertSong(int i, QUrl url) 0121 { 0122 if(!url.isValid()) 0123 return; 0124 if(0 > i) 0125 { 0126 i= m_data.size(); 0127 } 0128 0129 beginInsertRows(QModelIndex(), i, i); 0130 m_data.insert(i, url); 0131 endInsertRows(); 0132 } 0133 0134 QUrl MusicModel::getMediaByModelIndex(const QModelIndex& index) const 0135 { 0136 return m_data.at(index.row()); 0137 } 0138 0139 void MusicModel::removeAll() 0140 { 0141 beginResetModel(); 0142 m_data.clear(); 0143 endResetModel(); 0144 } 0145 void MusicModel::removeSong(const QModelIndexList& list) 0146 { 0147 if(list.isEmpty()) 0148 return; 0149 0150 QModelIndex first= list[0]; 0151 QModelIndex end= list.last(); 0152 beginRemoveRows(first.parent(), first.row(), end.row()); 0153 for(int i= list.size() - 1; i >= 0; --i) 0154 { 0155 QModelIndex index= list[i]; 0156 m_data.removeAt(index.row()); 0157 } 0158 endRemoveRows(); 0159 } 0160 void MusicModel::setCurrentSong(const QModelIndex& p) 0161 { 0162 m_currentSong= data(p, URL).toUrl(); 0163 emit dataChanged(p, p); 0164 } 0165 0166 QStringList MusicModel::mimeTypes() const 0167 { 0168 return {"text/uri-list"}; 0169 } 0170 0171 int MusicModel::indexOfCurrent() const 0172 { 0173 return m_data.indexOf(m_currentSong); 0174 } 0175 0176 const QList<QUrl>& MusicModel::urls() const 0177 { 0178 return m_data; 0179 } 0180 0181 Qt::DropActions MusicModel::supportedDropActions() const 0182 { 0183 return Qt::CopyAction | Qt::MoveAction; 0184 } 0185 bool MusicModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int, const QModelIndex&) 0186 { 0187 // TODO 0188 /*if(action == Qt::IgnoreAction) 0189 return true; 0190 0191 if(!data->hasUrls()) 0192 return false; 0193 0194 QList<QUrl> list= data->urls(); 0195 QStringList filters= PreferencesManager::getInstance() 0196 ->value("AudioFileFilter", "*.wav *.mp2 *.mp3 *.ogg *.flac") 0197 .toString() 0198 .split(' '); 0199 0200 if(data->hasUrls()) 0201 { 0202 QList<QUrl> list= data->urls(); 0203 for(int i= 0; i < list.size(); ++i) 0204 { 0205 QString str= list[i].toLocalFile(); 0206 if(str.endsWith(".m3u")) 0207 { 0208 } 0209 else 0210 { 0211 QStringList filters= PreferencesManager::getInstance() 0212 ->value("AudioFileFilter", "*.wav *.mp2 *.mp3 *.ogg *.flac") 0213 .toString() 0214 .split(' '); 0215 for(auto filter : filters) 0216 { 0217 filter.replace("*", ""); 0218 if(str.endsWith(filter)) 0219 { 0220 insertSong(row, str); 0221 } 0222 } 0223 } 0224 } 0225 } 0226 */ 0227 return true; 0228 } 0229 Qt::ItemFlags MusicModel::flags(const QModelIndex& index) const 0230 { 0231 Qt::ItemFlags defaultFlags= QAbstractListModel::flags(index); 0232 if(!index.isValid()) 0233 defaultFlags|= Qt::ItemIsDropEnabled; 0234 0235 return defaultFlags; 0236 }