Warning, file /plasma/kdeplasma-addons/applets/fifteenPuzzle/plugin/fifteenimageprovider.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Jeremy Whiting <jpwhiting@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "fifteenimageprovider.h"
0008 
0009 #include <QDebug>
0010 
0011 FifteenImageProvider::FifteenImageProvider()
0012     : QQuickImageProvider(QQuickImageProvider::Pixmap)
0013     , m_boardSize(4)
0014     , m_pieceWidth(30)
0015     , m_pieceHeight(30)
0016 {
0017 }
0018 
0019 QPixmap FifteenImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
0020 {
0021     Q_UNUSED(requestedSize); // wanted sizes are actually encoded in the id
0022 
0023     // id format is boardSize-imagenumber-pieceWidth-pieceHeight-imagePath
0024     qDebug() << "pixmap requested with id " << id;
0025     QStringList idParts = id.split(QLatin1Char('-'));
0026     if (idParts.size() < 4) {
0027         *size = QSize();
0028         return QPixmap();
0029     }
0030 
0031     bool update = false;
0032     int boardSize = idParts.at(0).toInt();
0033     int pieceWidth = idParts.at(2).toInt();
0034     int pieceHeight = idParts.at(3).toInt();
0035     QString path = idParts.at(4);
0036     if (path != m_imagePath && !path.isEmpty()) {
0037         m_imagePath = path;
0038         qDebug() << "loading pixmap from file " << path << m_pixmap.load(path);
0039         update = true;
0040     }
0041 
0042     if (idParts.at(1) == QLatin1String("all")) {
0043         return m_pixmap;
0044     } else {
0045         if (pieceWidth != m_pieceWidth || pieceHeight != m_pieceHeight) {
0046             m_pieceWidth = pieceWidth;
0047             m_pieceHeight = pieceHeight;
0048             update = true;
0049         }
0050 
0051         if (m_boardSize != boardSize) {
0052             m_boardSize = boardSize;
0053             update = true;
0054         }
0055 
0056         if (update) {
0057             updatePixmaps();
0058         }
0059 
0060         int number = idParts.at(1).toInt();
0061 
0062         qDebug() << "pixmap for piece " << number << " requested";
0063         if (number > 0 && number < m_pixmaps.size()) {
0064             *size = QSize(m_pieceWidth, m_pieceHeight);
0065             return m_pixmaps.at(number);
0066         }
0067     }
0068 
0069     *size = QSize();
0070     return QPixmap();
0071 }
0072 
0073 void FifteenImageProvider::updatePixmaps()
0074 {
0075     QSize size(m_pieceWidth * m_boardSize, m_pieceHeight * m_boardSize);
0076     QPixmap copyPixmap = m_pixmap.scaled(size);
0077 
0078     m_pixmaps.clear();
0079     m_pixmaps.resize(m_boardSize * m_boardSize);
0080 
0081     for (int i = 0; i < m_boardSize * m_boardSize; i++) {
0082         int posX = (i % m_boardSize) * m_pieceWidth;
0083         int posY = (i / m_boardSize) * m_pieceHeight;
0084 
0085         m_pixmaps[i] = copyPixmap.copy(posX, posY, m_pieceWidth, m_pieceHeight);
0086     }
0087 }