| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include <QtGui>
|
|---|
| 42 | #include <stdlib.h>
|
|---|
| 43 |
|
|---|
| 44 | #include "mainwindow.h"
|
|---|
| 45 | #include "piecesmodel.h"
|
|---|
| 46 | #include "puzzlewidget.h"
|
|---|
| 47 |
|
|---|
| 48 | MainWindow::MainWindow(QWidget *parent)
|
|---|
| 49 | : QMainWindow(parent)
|
|---|
| 50 | {
|
|---|
| 51 | setupMenus();
|
|---|
| 52 | setupWidgets();
|
|---|
| 53 | model = new PiecesModel(this);
|
|---|
| 54 | piecesList->setModel(model);
|
|---|
| 55 |
|
|---|
| 56 | setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
|
|---|
| 57 | setWindowTitle(tr("Puzzle"));
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | void MainWindow::openImage(const QString &path)
|
|---|
| 61 | {
|
|---|
| 62 | QString fileName = path;
|
|---|
| 63 |
|
|---|
| 64 | if (fileName.isNull())
|
|---|
| 65 | fileName = QFileDialog::getOpenFileName(this,
|
|---|
| 66 | tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp)"));
|
|---|
| 67 |
|
|---|
| 68 | if (!fileName.isEmpty()) {
|
|---|
| 69 | QPixmap newImage;
|
|---|
| 70 | if (!newImage.load(fileName)) {
|
|---|
| 71 | QMessageBox::warning(this, tr("Open Image"),
|
|---|
| 72 | tr("The image file could not be loaded."),
|
|---|
| 73 | QMessageBox::Cancel);
|
|---|
| 74 | return;
|
|---|
| 75 | }
|
|---|
| 76 | puzzleImage = newImage;
|
|---|
| 77 | setupPuzzle();
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | void MainWindow::setCompleted()
|
|---|
| 82 | {
|
|---|
| 83 | QMessageBox::information(this, tr("Puzzle Completed"),
|
|---|
| 84 | tr("Congratulations! You have completed the puzzle!\n"
|
|---|
| 85 | "Click OK to start again."),
|
|---|
| 86 | QMessageBox::Ok);
|
|---|
| 87 |
|
|---|
| 88 | setupPuzzle();
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void MainWindow::setupPuzzle()
|
|---|
| 92 | {
|
|---|
| 93 | int size = qMin(puzzleImage.width(), puzzleImage.height());
|
|---|
| 94 | puzzleImage = puzzleImage.copy((puzzleImage.width() - size)/2,
|
|---|
| 95 | (puzzleImage.height() - size)/2, size, size).scaled(400,
|
|---|
| 96 | 400, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
|---|
| 97 |
|
|---|
| 98 | qsrand(QCursor::pos().x() ^ QCursor::pos().y());
|
|---|
| 99 |
|
|---|
| 100 | model->addPieces(puzzleImage);
|
|---|
| 101 | puzzleWidget->clear();
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void MainWindow::setupMenus()
|
|---|
| 105 | {
|
|---|
| 106 | QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
|---|
| 107 |
|
|---|
| 108 | QAction *openAction = fileMenu->addAction(tr("&Open..."));
|
|---|
| 109 | openAction->setShortcuts(QKeySequence::Open);
|
|---|
| 110 |
|
|---|
| 111 | QAction *exitAction = fileMenu->addAction(tr("E&xit"));
|
|---|
| 112 | exitAction->setShortcuts(QKeySequence::Quit);
|
|---|
| 113 |
|
|---|
| 114 | QMenu *gameMenu = menuBar()->addMenu(tr("&Game"));
|
|---|
| 115 |
|
|---|
| 116 | QAction *restartAction = gameMenu->addAction(tr("&Restart"));
|
|---|
| 117 |
|
|---|
| 118 | connect(openAction, SIGNAL(triggered()), this, SLOT(openImage()));
|
|---|
| 119 | connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
|---|
| 120 | connect(restartAction, SIGNAL(triggered()), this, SLOT(setupPuzzle()));
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void MainWindow::setupWidgets()
|
|---|
| 124 | {
|
|---|
| 125 | QFrame *frame = new QFrame;
|
|---|
| 126 | QHBoxLayout *frameLayout = new QHBoxLayout(frame);
|
|---|
| 127 |
|
|---|
| 128 | piecesList = new QListView;
|
|---|
| 129 | piecesList->setDragEnabled(true);
|
|---|
| 130 | piecesList->setViewMode(QListView::IconMode);
|
|---|
| 131 | piecesList->setIconSize(QSize(60, 60));
|
|---|
| 132 | piecesList->setGridSize(QSize(80, 80));
|
|---|
| 133 | piecesList->setSpacing(10);
|
|---|
| 134 | piecesList->setMovement(QListView::Snap);
|
|---|
| 135 | piecesList->setAcceptDrops(true);
|
|---|
| 136 | piecesList->setDropIndicatorShown(true);
|
|---|
| 137 |
|
|---|
| 138 | PiecesModel *model = new PiecesModel(this);
|
|---|
| 139 | piecesList->setModel(model);
|
|---|
| 140 |
|
|---|
| 141 | puzzleWidget = new PuzzleWidget;
|
|---|
| 142 |
|
|---|
| 143 | connect(puzzleWidget, SIGNAL(puzzleCompleted()),
|
|---|
| 144 | this, SLOT(setCompleted()), Qt::QueuedConnection);
|
|---|
| 145 |
|
|---|
| 146 | frameLayout->addWidget(piecesList);
|
|---|
| 147 | frameLayout->addWidget(puzzleWidget);
|
|---|
| 148 | setCentralWidget(frame);
|
|---|
| 149 | }
|
|---|