File indexing completed on 2024-12-08 13:28:01

0001 /***************************************************************************
0002  *  Copyright (C) 2021 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 "mindmap/model/imagemodel.h"
0021 
0022 #include <QSet>
0023 namespace mindmap
0024 {
0025 ImageModel::ImageModel() {}
0026 QVariant ImageModel::data(const QModelIndex& index, int role) const
0027 {
0028     QSet<int> allowedRole{Qt::DisplayRole, PixmapRole, IdRole, UrlRole, LocalFileRole};
0029 
0030     auto pos= index.row();
0031 
0032     if(!allowedRole.contains(role) || pos < 0 || pos >= static_cast<int>(m_data.size()))
0033         return {};
0034 
0035     auto info= m_data[pos];
0036 
0037     QVariant res;
0038     switch(role)
0039     {
0040     case Qt::DisplayRole:
0041         res= info.m_url.toString();
0042         break;
0043     case UrlRole:
0044         res= info.m_url;
0045         break;
0046     case IdRole:
0047         res= info.m_id;
0048         break;
0049     case LocalFileRole:
0050         res= info.m_url.isLocalFile();
0051         break;
0052     default:
0053         break;
0054     }
0055 
0056     return res;
0057 }
0058 
0059 int ImageModel::rowCount(const QModelIndex& index) const
0060 {
0061     if(index.isValid())
0062         return 0;
0063     return static_cast<int>(m_data.size());
0064 }
0065 
0066 void ImageModel::insertPixmap(const QString& id, const QPixmap& map, const QUrl& url, bool network)
0067 {
0068     if(map.isNull())
0069         return;
0070 
0071     removePixmap(id);
0072 
0073     auto pos= static_cast<int>(m_data.size());
0074     beginInsertRows(QModelIndex(), pos, pos);
0075     m_data.push_back(ImageInfo{map, id, url});
0076     endInsertRows();
0077     if(!network)
0078         emit imageAdded(id);
0079 }
0080 
0081 int ImageModel::removePixmap(const QString& id)
0082 {
0083     auto size= m_data.size();
0084 
0085     auto it
0086         = std::find_if(std::begin(m_data), std::end(m_data), [id](const ImageInfo& info) { return id == info.m_id; });
0087 
0088     if(it == std::end(m_data))
0089         return 0;
0090 
0091     auto pos= std::distance(std::begin(m_data), it);
0092     beginRemoveRows(QModelIndex(), pos, pos);
0093     m_data.erase(it);
0094     endRemoveRows();
0095     emit imageRemoved(id);
0096     return size - m_data.size();
0097 }
0098 
0099 bool ImageModel::hasPixmap(const QString& id) const
0100 {
0101     auto it
0102         = std::find_if(std::begin(m_data), std::end(m_data), [id](const ImageInfo& info) { return id == info.m_id; });
0103 
0104     return (it != std::end(m_data));
0105 }
0106 
0107 QPixmap ImageModel::pixmapFromId(const QString& id) const
0108 {
0109 
0110     auto it
0111         = std::find_if(std::begin(m_data), std::end(m_data), [id](const ImageInfo& info) {
0112               qDebug() << info.m_id << id;
0113               return id == info.m_id; });
0114 
0115     if(it == std::end(m_data))
0116         return {};
0117 
0118     qDebug() << "has image" << it->m_pixmap.isNull();
0119     return it->m_pixmap;
0120 }
0121 
0122 const ImageInfo ImageModel::imageInfoFromId(const QString& id) const
0123 {
0124     auto it
0125         = std::find_if(std::begin(m_data), std::end(m_data), [id](const ImageInfo& info) { return id == info.m_id; });
0126 
0127     if(it == std::end(m_data))
0128         return {};
0129 
0130     return *it;
0131 }
0132 
0133 const std::vector<ImageInfo>& ImageModel::imageInfos() const
0134 {
0135     return m_data;
0136 }
0137 } // namespace mindmap