File indexing completed on 2024-05-12 16:02:09
0001 /* This file is part of the KDE project 0002 * SPDX-FileCopyrightText: 2011 Thorsten Zachmann <zachmann@kde.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "KoMarkerModel.h" 0008 0009 // Calligra 0010 #include <KoMarker.h> 0011 // Qt 0012 #include <QSize> 0013 0014 0015 KoMarkerModel::KoMarkerModel(const QList<KoMarker*> markers, KoFlake::MarkerPosition position, QObject *parent) 0016 : QAbstractListModel(parent) 0017 , m_markerPosition(position) 0018 , m_temporaryMarkerPosition(-1) 0019 { 0020 Q_FOREACH (KoMarker *marker, markers) { 0021 m_markers.append(QExplicitlySharedDataPointer<KoMarker>(marker)); 0022 } 0023 } 0024 0025 KoMarkerModel::~KoMarkerModel() 0026 { 0027 } 0028 0029 int KoMarkerModel::rowCount(const QModelIndex &parent) const 0030 { 0031 Q_UNUSED(parent); 0032 return m_markers.count(); 0033 } 0034 0035 QVariant KoMarkerModel::data(const QModelIndex &index, int role) const 0036 { 0037 if (!index.isValid()) { 0038 return QVariant(); 0039 } 0040 0041 switch(role) { 0042 case Qt::DecorationRole: 0043 if (index.row() < m_markers.size()) { 0044 return QVariant::fromValue<KoMarker*>(m_markers.at(index.row()).data()); 0045 } 0046 return QVariant(); 0047 case Qt::SizeHintRole: 0048 return QSize(80,30); 0049 default: 0050 return QVariant(); 0051 } 0052 } 0053 0054 int KoMarkerModel::markerIndex(KoMarker *marker) const 0055 { 0056 for (int i = 0; i < m_markers.size(); i++) { 0057 if (m_markers[i] == marker) return i; 0058 if (m_markers[i] && marker && *m_markers[i] == *marker) return i; 0059 } 0060 0061 return false; 0062 } 0063 0064 int KoMarkerModel::addTemporaryMarker(KoMarker *marker) 0065 { 0066 if (m_temporaryMarkerPosition >= 0) { 0067 removeTemporaryMarker(); 0068 } 0069 0070 m_temporaryMarkerPosition = m_markers.size() > 0 ? 1 : 0; 0071 beginInsertRows(QModelIndex(), m_temporaryMarkerPosition, m_temporaryMarkerPosition); 0072 m_markers.prepend(QExplicitlySharedDataPointer<KoMarker>(marker)); 0073 0074 endInsertRows(); 0075 0076 return m_temporaryMarkerPosition; 0077 } 0078 0079 void KoMarkerModel::removeTemporaryMarker() 0080 { 0081 if (m_temporaryMarkerPosition >= 0) { 0082 beginRemoveRows(QModelIndex(), m_temporaryMarkerPosition, m_temporaryMarkerPosition); 0083 m_markers.removeAt(m_temporaryMarkerPosition); 0084 m_temporaryMarkerPosition = -1; 0085 endRemoveRows(); 0086 } 0087 } 0088 0089 int KoMarkerModel::temporaryMarkerPosition() const 0090 { 0091 return m_temporaryMarkerPosition; 0092 } 0093 0094 QVariant KoMarkerModel::marker(int index, int role) const 0095 { 0096 if (index < 0){ 0097 return QVariant(); 0098 } 0099 0100 switch(role) { 0101 case Qt::DecorationRole: 0102 if (index< m_markers.size()) { 0103 return QVariant::fromValue<KoMarker*>(m_markers.at(index).data()); 0104 } 0105 return QVariant(); 0106 case Qt::SizeHintRole: 0107 return QSize(80, 30); 0108 default: 0109 return QVariant(); 0110 } 0111 } 0112 0113 KoFlake::MarkerPosition KoMarkerModel::position() const 0114 { 0115 return m_markerPosition; 0116 }