1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
32
#include "mainwindow.h"
33
#include "pieceslist.h"
34
#include "puzzlewidget.h"
36
MainWindow::MainWindow(QWidget *parent)
42
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
43
setWindowTitle(tr("Puzzle"));
46
void MainWindow::openImage(const QString &path)
48
QString fileName = path;
50
if (fileName.isNull())
51
fileName = QFileDialog::getOpenFileName(this,
52
tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)");
54
if (!fileName.isEmpty()) {
56
if (!newImage.load(fileName)) {
57
QMessageBox::warning(this, tr("Open Image"),
58
tr("The image file could not be loaded."),
59
QMessageBox::Cancel, QMessageBox::NoButton);
62
puzzleImage = newImage;
67
void MainWindow::setCompleted()
69
QMessageBox::information(this, tr("Puzzle Completed"),
70
tr("Congratulations! You have completed the puzzle!\n"
71
"Click OK to start again."),
77
void MainWindow::setupPuzzle()
79
int size = qMin(puzzleImage.width(), puzzleImage.height());
80
puzzleImage = puzzleImage.copy((puzzleImage.width() - size)/2,
81
(puzzleImage.height() - size)/2, size, size).scaled(400,
82
400, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
86
for (int y = 0; y < 5; ++y) {
87
for (int x = 0; x < 5; ++x) {
88
QPixmap pieceImage = puzzleImage.copy(x*80, y*80, 80, 80);
89
piecesList->addPiece(pieceImage, QPoint(x, y));
93
srand(QCursor::pos().x() ^ QCursor::pos().y());
95
for (int i = 0; i < piecesList->count(); ++i) {
96
if (int(2.0*rand()/(RAND_MAX+1.0)) == 1) {
97
QListWidgetItem *item = piecesList->takeItem(i);
98
piecesList->insertItem(0, item);
102
puzzleWidget->clear();
105
void MainWindow::setupMenus()
107
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
109
QAction *openAction = fileMenu->addAction(tr("&Open..."));
110
openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
112
QAction *exitAction = fileMenu->addAction(tr("E&xit"));
113
exitAction->setShortcut(QKeySequence(tr("Ctrl+Q")));
115
QMenu *gameMenu = menuBar()->addMenu(tr("&Game"));
117
QAction *restartAction = gameMenu->addAction(tr("&Restart"));
119
connect(openAction, SIGNAL(triggered()), this, SLOT(openImage()));
120
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
121
connect(restartAction, SIGNAL(triggered()), this, SLOT(setupPuzzle()));
124
void MainWindow::setupWidgets()
126
QFrame *frame = new QFrame;
127
QHBoxLayout *frameLayout = new QHBoxLayout(frame);
129
piecesList = new PiecesList;
130
puzzleWidget = new PuzzleWidget;
132
connect(puzzleWidget, SIGNAL(puzzleCompleted()),
133
this, SLOT(setCompleted()), Qt::QueuedConnection);
135
frameLayout->addWidget(piecesList);
136
frameLayout->addWidget(puzzleWidget);
137
setCentralWidget(frame);