2 Image Puzzle - A set your pieces straight game
3 Copyright (C) 2009 Timo Härkönen
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "imageimporter.h"
20 #include "puzzleitem.h"
27 ImageImporter *ImageImporter::instance_ = 0;
29 ImageImporter::ImageImporter(QObject *parent) :
35 ImageImporter *ImageImporter::instance()
38 instance_ = new ImageImporter;
44 QList<PuzzleItem *> ImageImporter::newPieces(const QPixmap &pixmap, const int count)
49 qDebug() << "Got NULL image - using default.jpg";
50 tmp = QPixmap(":/images/default.jpg");
56 if(tmp.size().height() != IMAGE_HEIGHT || tmp.size().width() != IMAGE_WIDTH) {
57 tmp = pixmap.scaled(QSize(IMAGE_WIDTH, IMAGE_HEIGHT), Qt::KeepAspectRatioByExpanding);
60 QList<PuzzleItem *> list;
62 int horizontalCount = 0;
64 if(count == EASY_PIECE_COUNT) {
65 horizontalCount = EASY_HORIZONTAL_COUNT;
67 else if(count == HARD_PIECE_COUNT) {
68 horizontalCount = HARD_HORIZONTAL_COUNT;
71 qDebug() << QString("Bad piece count ( %1 ) @ ImageImporter::newPieces").arg(count);
75 int verticalCount = count / horizontalCount;
76 int verticalStep = IMAGE_HEIGHT / verticalCount;
77 int horizontalStep = IMAGE_WIDTH / horizontalCount;
81 for(int i = 0; i < verticalCount; ++i) {
82 for(int j = 0; j < horizontalCount; ++j) {
83 PuzzleItem *item = new PuzzleItem;
84 item->setPixmap(tmp.copy(QRect(QPoint(j * horizontalStep, i * verticalStep),
85 QPoint(horizontalStep + j * horizontalStep, verticalStep + i * verticalStep))));
86 item->setPieceNumber(pieceNo);