1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "mainwindow.h" |
52 | #include "piecesmodel.h" |
53 | #include "puzzlewidget.h" |
54 | |
55 | #include <QtWidgets> |
56 | #include <stdlib.h> |
57 | |
58 | MainWindow::MainWindow(QWidget *parent) |
59 | : QMainWindow(parent) |
60 | { |
61 | setupMenus(); |
62 | setupWidgets(); |
63 | model = new PiecesModel(puzzleWidget->pieceSize(), this); |
64 | piecesList->setModel(model); |
65 | |
66 | setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); |
67 | setWindowTitle(tr("Puzzle" )); |
68 | } |
69 | |
70 | void MainWindow::openImage() |
71 | { |
72 | const QString directory = |
73 | QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).value(0, QDir::homePath()); |
74 | QFileDialog dialog(this, tr("Open Image" ), directory); |
75 | dialog.setAcceptMode(QFileDialog::AcceptOpen); |
76 | dialog.setFileMode(QFileDialog::ExistingFile); |
77 | QStringList mimeTypeFilters; |
78 | for (const QByteArray &mimeTypeName : QImageReader::supportedMimeTypes()) |
79 | mimeTypeFilters.append(mimeTypeName); |
80 | mimeTypeFilters.sort(); |
81 | dialog.setMimeTypeFilters(mimeTypeFilters); |
82 | dialog.selectMimeTypeFilter("image/jpeg" ); |
83 | if (dialog.exec() == QDialog::Accepted) |
84 | loadImage(dialog.selectedFiles().constFirst()); |
85 | } |
86 | |
87 | void MainWindow::loadImage(const QString &fileName) |
88 | { |
89 | QPixmap newImage; |
90 | if (!newImage.load(fileName)) { |
91 | QMessageBox::warning(this, tr("Open Image" ), |
92 | tr("The image file could not be loaded." ), |
93 | QMessageBox::Close); |
94 | return; |
95 | } |
96 | puzzleImage = newImage; |
97 | setupPuzzle(); |
98 | } |
99 | |
100 | void MainWindow::setCompleted() |
101 | { |
102 | QMessageBox::information(this, tr("Puzzle Completed" ), |
103 | tr("Congratulations! You have completed the puzzle!\n" |
104 | "Click OK to start again." ), |
105 | QMessageBox::Ok); |
106 | |
107 | setupPuzzle(); |
108 | } |
109 | |
110 | void MainWindow::setupPuzzle() |
111 | { |
112 | int size = qMin(puzzleImage.width(), puzzleImage.height()); |
113 | puzzleImage = puzzleImage.copy((puzzleImage.width() - size) / 2, |
114 | (puzzleImage.height() - size) / 2, size, size).scaled(puzzleWidget->imageSize(), |
115 | puzzleWidget->imageSize(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); |
116 | |
117 | model->addPieces(puzzleImage); |
118 | puzzleWidget->clear(); |
119 | } |
120 | |
121 | void MainWindow::setupMenus() |
122 | { |
123 | QMenu * = menuBar()->addMenu(tr("&File" )); |
124 | |
125 | QAction *openAction = fileMenu->addAction(tr("&Open..." ), this, &MainWindow::openImage); |
126 | openAction->setShortcuts(QKeySequence::Open); |
127 | |
128 | QAction *exitAction = fileMenu->addAction(tr("E&xit" ), qApp, &QCoreApplication::quit); |
129 | exitAction->setShortcuts(QKeySequence::Quit); |
130 | |
131 | QMenu * = menuBar()->addMenu(tr("&Game" )); |
132 | |
133 | gameMenu->addAction(tr("&Restart" ), this, &MainWindow::setupPuzzle); |
134 | } |
135 | |
136 | void MainWindow::setupWidgets() |
137 | { |
138 | QFrame *frame = new QFrame; |
139 | QHBoxLayout *frameLayout = new QHBoxLayout(frame); |
140 | |
141 | puzzleWidget = new PuzzleWidget(400); |
142 | |
143 | piecesList = new QListView; |
144 | piecesList->setDragEnabled(true); |
145 | piecesList->setViewMode(QListView::IconMode); |
146 | piecesList->setIconSize(QSize(puzzleWidget->pieceSize() - 20, puzzleWidget->pieceSize() - 20)); |
147 | piecesList->setGridSize(QSize(puzzleWidget->pieceSize(), puzzleWidget->pieceSize())); |
148 | piecesList->setSpacing(10); |
149 | piecesList->setMovement(QListView::Snap); |
150 | piecesList->setAcceptDrops(true); |
151 | piecesList->setDropIndicatorShown(true); |
152 | |
153 | PiecesModel *model = new PiecesModel(puzzleWidget->pieceSize(), this); |
154 | piecesList->setModel(model); |
155 | |
156 | connect(puzzleWidget, &PuzzleWidget::puzzleCompleted, |
157 | this, &MainWindow::setCompleted, Qt::QueuedConnection); |
158 | |
159 | frameLayout->addWidget(piecesList); |
160 | frameLayout->addWidget(puzzleWidget); |
161 | setCentralWidget(frame); |
162 | } |
163 | |