| 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 "piecesmodel.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 |
model = new PiecesModel(puzzleWidget->pieceSize(), this); |
| 17 |
piecesList->setModel(model); |
| 18 |
|
| 19 |
setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
| 20 |
setWindowTitle(tr("Puzzle")); |
| 21 |
} |
| 22 |
|
| 23 |
void MainWindow::openImage() |
| 24 |
{ |
| 25 |
const QString directory = |
| 26 |
QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).value(0, QDir::homePath()); |
| 27 |
QFileDialog dialog(this, tr("Open Image"), directory); |
| 28 |
dialog.setAcceptMode(QFileDialog::AcceptOpen); |
| 29 |
dialog.setFileMode(QFileDialog::ExistingFile); |
| 30 |
QStringList mimeTypeFilters; |
| 31 |
for (const QByteArray &mimeTypeName : QImageReader::supportedMimeTypes()) |
| 32 |
mimeTypeFilters.append(mimeTypeName); |
| 33 |
mimeTypeFilters.sort(); |
| 34 |
dialog.setMimeTypeFilters(mimeTypeFilters); |
| 35 |
dialog.selectMimeTypeFilter("image/jpeg"); |
| 36 |
if (dialog.exec() == QDialog::Accepted) |
| 37 |
loadImage(dialog.selectedFiles().constFirst()); |
| 38 |
} |
| 39 |
|
| 40 |
void MainWindow::loadImage(const QString &fileName) |
| 41 |
{ |
| 42 |
QPixmap newImage; |
| 43 |
if (!newImage.load(fileName)) { |
| 44 |
QMessageBox::warning(this, tr("Open Image"), |
| 45 |
tr("The image file could not be loaded."), |
| 46 |
QMessageBox::Close); |
| 47 |
return; |
| 48 |
} |
| 49 |
puzzleImage = newImage; |
| 50 |
setupPuzzle(); |
| 51 |
} |
| 52 |
|
| 53 |
void MainWindow::setCompleted() |
| 54 |
{ |
| 55 |
QMessageBox::information(this, tr("Puzzle Completed"), |
| 56 |
tr("Congratulations! You have completed the puzzle!\n" |
| 57 |
"Click OK to start again."), |
| 58 |
QMessageBox::Ok); |
| 59 |
|
| 60 |
setupPuzzle(); |
| 61 |
} |
| 62 |
|
| 63 |
void MainWindow::setupPuzzle() |
| 64 |
{ |
| 65 |
int size = qMin(puzzleImage.width(), puzzleImage.height()); |
| 66 |
puzzleImage = puzzleImage.copy((puzzleImage.width() - size) / 2, |
| 67 |
(puzzleImage.height() - size) / 2, size, size).scaled(puzzleWidget->imageSize(), |
| 68 |
puzzleWidget->imageSize(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); |
| 69 |
|
| 70 |
model->addPieces(puzzleImage); |
| 71 |
puzzleWidget->clear(); |
| 72 |
} |
| 73 |
|
| 74 |
void MainWindow::setupMenus() |
| 75 |
{ |
| 76 |
QMenu *fileMenu = menuBar()->addMenu(tr("&File")); |
| 77 |
|
| 78 |
QAction *openAction = fileMenu->addAction(tr("&Open..."), this, &MainWindow::openImage); |
| 79 |
openAction->setShortcuts(QKeySequence::Open); |
| 80 |
|
| 81 |
QAction *exitAction = fileMenu->addAction(tr("E&xit"), qApp, &QCoreApplication::quit); |
| 82 |
exitAction->setShortcuts(QKeySequence::Quit); |
| 83 |
|
| 84 |
QMenu *gameMenu = menuBar()->addMenu(tr("&Game")); |
| 85 |
|
| 86 |
gameMenu->addAction(tr("&Restart"), this, &MainWindow::setupPuzzle); |
| 87 |
} |
| 88 |
|
| 89 |
void MainWindow::setupWidgets() |
| 90 |
{ |
| 91 |
QFrame *frame = new QFrame; |
| 92 |
QHBoxLayout *frameLayout = new QHBoxLayout(frame); |
| 93 |
|
| 94 |
puzzleWidget = new PuzzleWidget(400); |
| 95 |
|
| 96 |
piecesList = new QListView; |
| 97 |
piecesList->setDragEnabled(true); |
| 98 |
piecesList->setViewMode(QListView::IconMode); |
| 99 |
piecesList->setIconSize(QSize(puzzleWidget->pieceSize() - 20, puzzleWidget->pieceSize() - 20)); |
| 100 |
piecesList->setGridSize(QSize(puzzleWidget->pieceSize(), puzzleWidget->pieceSize())); |
| 101 |
piecesList->setSpacing(10); |
| 102 |
piecesList->setMovement(QListView::Snap); |
| 103 |
piecesList->setAcceptDrops(true); |
| 104 |
piecesList->setDropIndicatorShown(true); |
| 105 |
|
| 106 |
PiecesModel *model = new PiecesModel(puzzleWidget->pieceSize(), this); |
| 107 |
piecesList->setModel(model); |
| 108 |
|
| 109 |
connect(puzzleWidget, &PuzzleWidget::puzzleCompleted, |
| 110 |
this, &MainWindow::setCompleted, Qt::QueuedConnection); |
| 111 |
|
| 112 |
frameLayout->addWidget(piecesList); |
| 113 |
frameLayout->addWidget(puzzleWidget); |
| 114 |
setCentralWidget(frame); |
| 115 |
} |