Updating changelog
[impuzzle] / src / imageimporter.cpp
1 /*
2   Image Puzzle - A set your pieces straight game
3   Copyright (C) 2009  Timo Härkönen
4
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.
9
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.
14
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/>.
17  */
18
19 #include "imageimporter.h"
20 #include "puzzleitem.h"
21 #include "defines.h"
22
23 #include <QPixmap>
24
25 #include <QDebug>
26
27 ImageImporter *ImageImporter::instance_ = 0;
28
29 ImageImporter::ImageImporter(QObject *parent) :
30         QObject(parent)
31 {
32
33 }
34
35 ImageImporter *ImageImporter::instance()
36 {
37     if(!instance_) {
38         instance_ = new ImageImporter;
39     }
40
41     return instance_;
42 }
43
44 QList<PuzzleItem *> ImageImporter::newPieces(const QPixmap &pixmap, const int count)
45 {
46     QPixmap tmp;
47
48     if(pixmap.isNull()) {
49         tmp = QPixmap(":/images/default.jpg");
50     }
51     else {
52         tmp = pixmap;
53     }
54
55     if(tmp.size().height() != IMAGE_HEIGHT || tmp.size().width() != IMAGE_WIDTH) {
56         tmp = pixmap.scaled(QSize(IMAGE_WIDTH, IMAGE_HEIGHT), Qt::KeepAspectRatioByExpanding);
57     }
58
59     QList<PuzzleItem *> list;
60
61     int horizontalCount = 0;
62
63     if(count == 12) {
64         horizontalCount = 4;
65     }
66     else if(count == 20) {
67         horizontalCount = 5;
68     }
69     else {
70         return list;
71     }
72
73     int verticalCount = count / horizontalCount;
74     int verticalStep = IMAGE_HEIGHT / verticalCount;
75     int horizontalStep = IMAGE_WIDTH / horizontalCount;
76
77     for(int i = 0; i < verticalCount; ++i) {
78         for(int j = 0; j < horizontalCount; ++j) {
79             PuzzleItem *item = new PuzzleItem;
80             item->setPixmap(tmp.copy(QRect(QPoint(j * horizontalStep, i * verticalStep),
81                                            QPoint(horizontalStep + j * horizontalStep, verticalStep + i * verticalStep))));
82             list.append(item);
83         }
84     }
85
86     return list;
87 }