File indexing completed on 2024-05-12 16:34:42
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2008 Peter Simonsson <peter.simonsson@gmail.com> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "CollectionItemModel.h" 0021 0022 #include "StencilBoxDebug.h" 0023 0024 #include <KoShapeFactoryBase.h> 0025 #include <KoProperties.h> 0026 0027 #include <QMimeData> 0028 0029 CollectionItemModel::CollectionItemModel(QObject* parent) 0030 : QAbstractListModel(parent), 0031 m_viewMode(QListView::IconMode) 0032 { 0033 setSupportedDragActions(Qt::CopyAction); 0034 } 0035 0036 QVariant CollectionItemModel::data(const QModelIndex& index, int role) const 0037 { 0038 if (!index.isValid() || index.row() > m_shapeTemplateList.count ()) 0039 return QVariant(); 0040 0041 switch(role) 0042 { 0043 case Qt::ToolTipRole: 0044 return m_shapeTemplateList[index.row()].toolTip; 0045 0046 case Qt::DecorationRole: 0047 return m_shapeTemplateList[index.row()].icon; 0048 0049 case Qt::UserRole: 0050 return m_shapeTemplateList[index.row()].id; 0051 0052 case Qt::DisplayRole: 0053 return m_viewMode == QListView::ListMode ? m_shapeTemplateList[index.row()].name : QString(); 0054 0055 case Qt::UserRole+1: 0056 return m_shapeTemplateList[index.row()].name; 0057 0058 default: 0059 return QVariant(); 0060 } 0061 0062 return QVariant(); 0063 } 0064 0065 int CollectionItemModel::rowCount(const QModelIndex& parent) const 0066 { 0067 Q_UNUSED(parent); 0068 return m_shapeTemplateList.count(); 0069 } 0070 0071 void CollectionItemModel::setShapeTemplateList(const QList<KoCollectionItem>& newlist) 0072 { 0073 m_shapeTemplateList = newlist; 0074 reset(); 0075 } 0076 0077 QListView::ViewMode CollectionItemModel::viewMode() const 0078 { 0079 return m_viewMode; 0080 } 0081 0082 void CollectionItemModel::setViewMode(QListView::ViewMode vm) 0083 { 0084 if(m_viewMode == vm) 0085 return; 0086 m_viewMode = vm; 0087 } 0088 0089 QMimeData* CollectionItemModel::mimeData(const QModelIndexList& indexes) const 0090 { 0091 if(indexes.isEmpty()) 0092 return 0; 0093 0094 QModelIndex index = indexes.first(); 0095 0096 if(!index.isValid()) 0097 return 0; 0098 0099 if(m_shapeTemplateList.isEmpty()) 0100 return 0; 0101 0102 QByteArray itemData; 0103 QDataStream dataStream(&itemData, QIODevice::WriteOnly); 0104 dataStream << m_shapeTemplateList[index.row()].id; 0105 const KoProperties *props = m_shapeTemplateList[index.row()].properties; 0106 0107 if(props) 0108 dataStream << props->store("shapes"); 0109 else 0110 dataStream << QString(); 0111 0112 QMimeData* mimeData = new QMimeData; 0113 mimeData->setData(SHAPETEMPLATE_MIMETYPE, itemData); 0114 0115 return mimeData; 0116 } 0117 0118 QStringList CollectionItemModel::mimeTypes() const 0119 { 0120 QStringList mimetypes; 0121 mimetypes << SHAPETEMPLATE_MIMETYPE; 0122 0123 return mimetypes; 0124 } 0125 0126 Qt::ItemFlags CollectionItemModel::flags(const QModelIndex& index) const 0127 { 0128 if(index.isValid()) 0129 return QAbstractListModel::flags(index) | Qt::ItemIsDragEnabled; 0130 0131 return QAbstractListModel::flags(index); 0132 } 0133 0134 const KoProperties* CollectionItemModel::properties(const QModelIndex& index) const 0135 { 0136 if (!index.isValid() || index.row() > m_shapeTemplateList.count()) 0137 return 0; 0138 0139 return m_shapeTemplateList[index.row()].properties; 0140 }