File indexing completed on 2024-05-05 05:40:27
0001 /*************************************************************************** 0002 * Copyright (C) 2020 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * This software 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/contentmodel.h" 0021 0022 #include <QDebug> 0023 #include <set> 0024 0025 #include "controller/view_controller/mediacontrollerbase.h" 0026 #include "controller/view_controller/sharednotecontroller.h" 0027 #include "controller/view_controller/vectorialmapcontroller.h" 0028 #include "worker/messagehelper.h" 0029 0030 ContentModel::ContentModel(QObject* parent) : QAbstractListModel(parent) {} 0031 0032 ContentModel::~ContentModel()= default; 0033 0034 int ContentModel::rowCount(const QModelIndex& parent) const 0035 { 0036 if(parent.isValid()) 0037 return 0; 0038 0039 return static_cast<int>(m_medias.size()); 0040 } 0041 0042 QVariant ContentModel::data(const QModelIndex& index, int role) const 0043 { 0044 if(!index.isValid()) 0045 return QVariant(); 0046 0047 std::set<int> acceptedRole({Qt::DisplayRole, Qt::EditRole, Qt::DecorationRole, NameRole, TitleRole, UuidRole, 0048 PathRole, ContentTypeRole, ActiveRole, ModifiedRole, OwnerIdRole, ControllerRole}); 0049 0050 if(acceptedRole.find(role) == acceptedRole.end()) 0051 return {}; 0052 0053 QVariant value; 0054 0055 auto medium= m_medias[static_cast<std::size_t>(index.row())].get(); 0056 0057 if(nullptr == medium) 0058 return {}; 0059 0060 switch(role) 0061 { 0062 case Qt::DisplayRole: 0063 case Qt::EditRole: 0064 case NameRole: 0065 value= medium->name(); 0066 break; 0067 case TitleRole: 0068 value= medium->title(); 0069 break; 0070 case UuidRole: 0071 value= medium->uuid(); 0072 break; 0073 case PathRole: 0074 value= medium->url(); 0075 break; 0076 case ContentTypeRole: 0077 value= QVariant::fromValue(medium->contentType()); 0078 break; 0079 case ActiveRole: 0080 value= medium->isActive(); 0081 break; 0082 case ModifiedRole: 0083 value= medium->modified(); 0084 break; 0085 case OwnerIdRole: 0086 value= medium->ownerId(); 0087 break; 0088 case ControllerRole: 0089 value= QVariant::fromValue(medium); 0090 break; 0091 } 0092 0093 return value; 0094 } 0095 0096 bool ContentModel::appendMedia(MediaControllerBase* media) 0097 { 0098 auto size= static_cast<int>(m_medias.size()); 0099 std::unique_ptr<MediaControllerBase> ctrl(media); 0100 beginInsertRows(QModelIndex(), size, size); 0101 m_medias.push_back(std::move(ctrl)); 0102 endInsertRows(); 0103 0104 emit mediaControllerAdded(media); 0105 return true; 0106 } 0107 0108 bool ContentModel::removeMedia(const QString& uuid) 0109 { 0110 auto it 0111 = std::find_if(std::begin(m_medias), std::end(m_medias), 0112 [uuid](const std::unique_ptr<MediaControllerBase>& medium) { return uuid == medium->uuid(); }); 0113 0114 if(it == std::end(m_medias)) 0115 return false; 0116 0117 (*it)->aboutToClose(); 0118 if((*it)->localIsOwner()) 0119 MessageHelper::closeMedia(uuid, (*it)->contentType()); 0120 0121 auto pos= static_cast<int>(std::distance(std::begin(m_medias), it)); 0122 0123 beginRemoveRows(QModelIndex(), pos, pos); 0124 m_medias.erase(it); 0125 endRemoveRows(); 0126 0127 return true; 0128 } 0129 0130 QString ContentModel::activeMediaId() const 0131 { 0132 auto it= std::find_if(std::begin(m_medias), std::end(m_medias), 0133 [](const std::unique_ptr<MediaControllerBase>& medium) { return medium->isActive(); }); 0134 0135 if(it == m_medias.end()) 0136 return {}; 0137 0138 return (*it)->uuid(); 0139 } 0140 0141 MediaControllerBase* ContentModel::media(const QString& mediaId) 0142 { 0143 auto it= std::find_if(std::begin(m_medias), std::end(m_medias), 0144 [mediaId](const std::unique_ptr<MediaControllerBase>& medium) 0145 { return mediaId == medium->uuid(); }); 0146 0147 if(it == m_medias.end()) 0148 return nullptr; 0149 0150 return it->get(); 0151 } 0152 0153 std::vector<MediaControllerBase*> ContentModel::controllers() const 0154 { 0155 std::vector<MediaControllerBase*> vec; 0156 std::transform(std::begin(m_medias), std::end(m_medias), std::back_inserter(vec), 0157 [](const std::unique_ptr<MediaControllerBase>& media) { return media.get(); }); 0158 return vec; 0159 } 0160 0161 QHash<int, QByteArray> ContentModel::roleNames() const 0162 { 0163 return QHash<int, QByteArray>({{NameRole, "name"}, 0164 {TitleRole, "title"}, 0165 {UuidRole, "uuid"}, 0166 {PathRole, "path"}, 0167 {ContentTypeRole, "type"}, 0168 {ActiveRole, "active"}, 0169 {ModifiedRole, "modified"}, 0170 {OwnerIdRole, "owner"}, 0171 {ControllerRole, "controller"}}); 0172 } 0173 0174 void ContentModel::clearData() 0175 { 0176 beginResetModel(); 0177 m_medias.clear(); 0178 endResetModel(); 0179 } 0180 0181 int ContentModel::mediaCount(Core::ContentType type) 0182 { 0183 return std::count_if(std::begin(m_medias), std::end(m_medias), 0184 [type](const std::unique_ptr<MediaControllerBase>& media) 0185 { return media->contentType() == type; }); 0186 } 0187 0188 /////////////////////////////////////////// 0189 // 0190 ////////////////////////////////////////// 0191 0192 FilteredContentModel::FilteredContentModel(Core::ContentType type) : m_type(type) 0193 { 0194 // contentController<VectorialMapController*>(); 0195 // contentController<SharedNoteController*>(); 0196 setFilterRole(ContentModel::ContentTypeRole); 0197 0198 setDynamicSortFilter(true); 0199 } 0200 0201 bool FilteredContentModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const 0202 { 0203 auto idx= sourceModel()->index(source_row, 0, source_parent); 0204 return idx.data(ContentModel::ContentTypeRole).value<Core::ContentType>() == m_type; 0205 }