File indexing completed on 2024-04-28 04:05:13

0001 /*
0002     SPDX-FileCopyrightText: 2009 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "slicer-rect.h"
0008 
0009 #include <KPluginFactory>
0010 
0011 K_PLUGIN_CLASS_WITH_JSON(RectSlicer, "palapeli_rectslicer.json")
0012 
0013 RectSlicer::RectSlicer(QObject* parent, const QVariantList& args)
0014     : Pala::Slicer(parent, args)
0015     , Pala::SimpleGridPropertySet(this)
0016 {
0017 }
0018 
0019 bool RectSlicer::run(Pala::SlicerJob* job)
0020 {
0021     //read job
0022     const QSize pieceCount = Pala::SimpleGridPropertySet::pieceCount(job);
0023     const int xCount = pieceCount.width();
0024     const int yCount = pieceCount.height();
0025     const QImage image = job->image();
0026     //calculate some metrics
0027     const int pieceWidth = image.width() / xCount;
0028     const int pieceHeight = image.height() / yCount;
0029     const int roundingErrorX = image.width() - pieceWidth * xCount;
0030     const int roundingErrorY = image.height() - pieceHeight * yCount;
0031     //create pieces
0032     for (int x = 0; x < xCount; ++x)
0033     {
0034         for (int y = 0; y < yCount; ++y)
0035         {
0036             //calculate more metrics
0037             const QPoint offset(x * pieceWidth, y * pieceHeight);
0038             const int thisPieceWidth = (x == xCount) ? pieceWidth + roundingErrorX : pieceWidth;
0039             const int thisPieceHeight = (y == yCount) ? pieceHeight + roundingErrorY : pieceHeight;
0040             const QSize pieceSize(thisPieceWidth, thisPieceHeight);
0041             //copy image part to piece
0042             const QRect pieceBounds(offset, pieceSize);
0043             const QImage pieceImage = image.copy(pieceBounds);
0044             job->addPiece(x + y * xCount, pieceImage, offset);
0045         }
0046     }
0047     //create relations
0048     for (int x = 0; x < xCount; ++x)
0049     {
0050         for (int y = 0; y < yCount; ++y)
0051         {
0052             //along X axis (pointing left)
0053             if (x != 0)
0054                 job->addRelation(x + y * xCount, (x - 1) + y * xCount);
0055             //along Y axis (pointing up)
0056             if (y != 0)
0057                 job->addRelation(x + y * xCount, x + (y - 1) * xCount);
0058         }
0059     }
0060     return true;
0061 }
0062 
0063 #include "moc_slicer-rect.cpp"
0064 #include "slicer-rect.moc"