File indexing completed on 2024-04-21 04:52:25
0001 /* 0002 Kdenlive TitleClip Pattern 0003 SPDX-FileCopyrightText: 2020 RafaĆ Lalik <rafallalik@gmail.com> 0004 0005 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "patternsmodel.h" 0009 #include "titledocument.h" 0010 0011 #include <QGraphicsItem> 0012 #include <QGraphicsScene> 0013 #include <QIODevice> 0014 #include <QPainter> 0015 0016 PatternsModel::PatternsModel(QObject *parent) 0017 : QAbstractListModel(parent) 0018 { 0019 } 0020 0021 QVariant PatternsModel::data(const QModelIndex &index, int role) const 0022 { 0023 if (!index.isValid()) return QVariant(); 0024 0025 if (role == Qt::DecorationRole) 0026 return pixmaps[index.row()].scaled(m_tileSize, Qt::KeepAspectRatio, Qt::SmoothTransformation); 0027 else if (role == Qt::SizeHintRole) 0028 return m_tileSize; 0029 else if (role == Qt::UserRole) 0030 return patterns.value(index.row()); 0031 0032 return QVariant(); 0033 } 0034 0035 int PatternsModel::rowCount(const QModelIndex &parent) const 0036 { 0037 return parent.isValid() ? 0 : patterns.size(); 0038 } 0039 0040 void PatternsModel::addScene(const QString &pattern) 0041 { 0042 int row = patterns.size(); 0043 beginInsertRows(QModelIndex(), row, row); 0044 0045 patterns.append(pattern); 0046 pixmaps.append(paintScene(pattern)); 0047 ++modified_counter; 0048 0049 endInsertRows(); 0050 } 0051 0052 QPixmap PatternsModel::paintScene(const QString &pattern) 0053 { 0054 QDomDocument doc; 0055 doc.setContent(pattern); 0056 0057 QList<QGraphicsItem *> items; 0058 int width, height, duration, missing; 0059 TitleDocument::loadFromXml(doc, items, width, height, nullptr, nullptr, nullptr, &duration, missing); 0060 0061 QGraphicsScene scene(0, 0, width, height); 0062 0063 if (bkg) { 0064 auto *bkg_frame = new QGraphicsPixmapItem(); 0065 bkg_frame->setTransform(bkg->transform()); 0066 bkg_frame->setZValue(bkg->zValue()); 0067 bkg_frame->setPixmap(bkg->pixmap()); 0068 scene.addItem(bkg_frame); 0069 } 0070 for (QGraphicsItem *item : qAsConst(items)) { 0071 scene.addItem(item); 0072 } 0073 0074 QPixmap pxm(width, height); 0075 QPainter painter(&pxm); 0076 painter.setRenderHint(QPainter::Antialiasing); 0077 scene.render(&painter); 0078 0079 return pxm; 0080 } 0081 0082 void PatternsModel::repaintScenes() 0083 { 0084 for (int i = 0; i < patterns.size(); ++i) { 0085 pixmaps[i] = paintScene(patterns[i]); 0086 } 0087 0088 Q_EMIT dataChanged(index(0), index(patterns.size() - 1)); 0089 } 0090 0091 void PatternsModel::removeScene(const QModelIndex &index) 0092 { 0093 beginRemoveRows(QModelIndex(), index.row(), index.row()); 0094 0095 patterns.remove(index.row()); 0096 pixmaps.remove(index.row()); 0097 0098 ++modified_counter; 0099 0100 endRemoveRows(); 0101 } 0102 0103 void PatternsModel::removeAll() 0104 { 0105 beginRemoveRows(QModelIndex(), 0, patterns.size() - 1); 0106 0107 patterns.clear(); 0108 pixmaps.clear(); 0109 0110 ++modified_counter; 0111 0112 endRemoveRows(); 0113 } 0114 0115 QByteArray PatternsModel::serialize() 0116 { 0117 QByteArray encodedData; 0118 QDataStream stream(&encodedData, QIODevice::WriteOnly); 0119 0120 for (const auto &p : qAsConst(patterns)) { 0121 stream << p; 0122 } 0123 modified_counter = 0; 0124 0125 return encodedData; 0126 } 0127 0128 void PatternsModel::deserialize(const QByteArray &data) 0129 { 0130 removeAll(); 0131 0132 QByteArray ba = data; 0133 QDataStream stream(&ba, QIODevice::ReadOnly); 0134 while (!stream.atEnd()) { 0135 QString p; 0136 stream >> p; 0137 addScene(p); 0138 } 0139 modified_counter = 0; 0140 }