| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "mainwindow.h" |
| 5 |
#include "pieceslist.h" |
| 6 |
#include "puzzlewidget.h" |
| 7 |
|
| 8 |
#include <QtWidgets> |
| 9 |
#include <stdlib.h> |
| 10 |
|
| 11 |
MainWindow::MainWindow(QWidget *parent) |
| 12 |
: QMainWindow(parent) |
| 13 |
{ |
| 14 |
setupMenus(); |
| 15 |
setupWidgets(); |
| 16 |
|
| 17 |
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
| 18 |
setWindowTitle(tr("Puzzle")); |
| 19 |
} |
| 20 |
|
| 21 |
void MainWindow::openImage() |
| 22 |
{ |
| 23 |
const QString directory = |
| 24 |
QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).value(0, QDir::homePath()); |
| 25 |
QFileDialog dialog(this, tr("Open Image"), directory); |
| 26 |
dialog.setAcceptMode(QFileDialog::AcceptOpen); |
| 27 |
dialog.setFileMode(QFileDialog::ExistingFile); |
| 28 |
QStringList mimeTypeFilters; |
| 29 |
for (const QByteArray &mimeTypeName : QImageReader::supportedMimeTypes()) |
| 30 |
mimeTypeFilters.append(mimeTypeName); |
| 31 |
mimeTypeFilters.sort(); |
| 32 |
dialog.setMimeTypeFilters(mimeTypeFilters); |
| 33 |
dialog.selectMimeTypeFilter("image/jpeg"); |
| 34 |
if (dialog.exec() == QDialog::Accepted) |
| 35 |
loadImage(dialog.selectedFiles().constFirst()); |
| 36 |
} |
| 37 |
|
| 38 |
void MainWindow::loadImage(const QString &fileName) |
| 39 |
{ |
| 40 |
QPixmap newImage; |
| 41 |
if (!newImage.load(fileName)) { |
| 42 |
QMessageBox::warning(this, tr("Open Image"), |
| 43 |
tr("The image file could not be loaded."), |
| 44 |
QMessageBox::Close); |
| 45 |
return; |
| 46 |
} |
| 47 |
puzzleImage = newImage; |
| 48 |
setupPuzzle(); |
| 49 |
} |
| 50 |
|
| 51 |
void MainWindow::setCompleted() |
| 52 |
{ |
| 53 |
QMessageBox::information(this, tr("Puzzle Completed"), |
| 54 |
tr("Congratulations! You have completed the puzzle!\n" |
| 55 |
"Click OK to start again."), |
| 56 |
QMessageBox::Ok); |
| 57 |
|
| 58 |
setupPuzzle(); |
| 59 |
} |
| 60 |
|
| 61 |
void MainWindow::setupPuzzle() |
| 62 |
{ |
| 63 |
int size = qMin(puzzleImage.width(), puzzleImage.height()); |
| 64 |
puzzleImage = puzzleImage.copy((puzzleImage.width() - size) / 2, |
| 65 |
(puzzleImage.height() - size) / 2, size, size).scaled(puzzleWidget->width(), |
| 66 |
puzzleWidget->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); |
| 67 |
|
| 68 |
piecesList->clear(); |
| 69 |
|
| 70 |
for (int y = 0; y < 5; ++y) { |
| 71 |
for (int x = 0; x < 5; ++x) { |
| 72 |
int pieceSize = puzzleWidget->pieceSize(); |
| 73 |
QPixmap pieceImage = puzzleImage.copy(x * pieceSize, y * pieceSize, pieceSize, pieceSize); |
| 74 |
piecesList->addPiece(pieceImage, QPoint(x, y)); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
for (int i = 0; i < piecesList->count(); ++i) { |
| 79 |
if (QRandomGenerator::global()->bounded(2) == 1) { |
| 80 |
QListWidgetItem *item = piecesList->takeItem(i); |
| 81 |
piecesList->insertItem(0, item); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
puzzleWidget->clear(); |
| 86 |
} |
| 87 |
|
| 88 |
void MainWindow::setupMenus() |
| 89 |
{ |
| 90 |
QMenu *fileMenu = menuBar()->addMenu(tr("&File")); |
| 91 |
|
| 92 |
QAction *openAction = fileMenu->addAction(tr("&Open..."), this, &MainWindow::openImage); |
| 93 |
openAction->setShortcuts(QKeySequence::Open); |
| 94 |
|
| 95 |
QAction *exitAction = fileMenu->addAction(tr("E&xit"), qApp, &QCoreApplication::quit); |
| 96 |
exitAction->setShortcuts(QKeySequence::Quit); |
| 97 |
|
| 98 |
QMenu *gameMenu = menuBar()->addMenu(tr("&Game")); |
| 99 |
|
| 100 |
gameMenu->addAction(tr("&Restart"), this, &MainWindow::setupPuzzle); |
| 101 |
} |
| 102 |
|
| 103 |
void MainWindow::setupWidgets() |
| 104 |
{ |
| 105 |
QFrame *frame = new QFrame; |
| 106 |
QHBoxLayout *frameLayout = new QHBoxLayout(frame); |
| 107 |
puzzleWidget = new PuzzleWidget(400); |
| 108 |
|
| 109 |
piecesList = new PiecesList(puzzleWidget->pieceSize(), this); |
| 110 |
|
| 111 |
|
| 112 |
connect(puzzleWidget, &PuzzleWidget::puzzleCompleted, |
| 113 |
this, &MainWindow::setCompleted, Qt::QueuedConnection); |
| 114 |
|
| 115 |
frameLayout->addWidget(piecesList); |
| 116 |
frameLayout->addWidget(puzzleWidget); |
| 117 |
setCentralWidget(frame); |
| 118 |
} |