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(s: "Puzzle" )); |
68 | } |
69 | |
70 | void MainWindow::openImage() |
71 | { |
72 | const QString directory = |
73 | QStandardPaths::standardLocations(type: QStandardPaths::PicturesLocation).value(i: 0, defaultValue: QDir::homePath()); |
74 | QFileDialog dialog(this, tr(s: "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(t: mimeTypeName); |
80 | mimeTypeFilters.sort(); |
81 | dialog.setMimeTypeFilters(mimeTypeFilters); |
82 | dialog.selectMimeTypeFilter(filter: "image/jpeg" ); |
83 | if (dialog.exec() == QDialog::Accepted) |
84 | loadImage(path: dialog.selectedFiles().constFirst()); |
85 | } |
86 | |
87 | void MainWindow::loadImage(const QString &fileName) |
88 | { |
89 | QPixmap newImage; |
90 | if (!newImage.load(fileName)) { |
91 | QMessageBox::warning(parent: this, title: tr(s: "Open Image" ), |
92 | text: tr(s: "The image file could not be loaded." ), |
93 | buttons: QMessageBox::Close); |
94 | return; |
95 | } |
96 | puzzleImage = newImage; |
97 | setupPuzzle(); |
98 | } |
99 | |
100 | void MainWindow::setCompleted() |
101 | { |
102 | QMessageBox::information(parent: this, title: tr(s: "Puzzle Completed" ), |
103 | text: tr(s: "Congratulations! You have completed the puzzle!\n" |
104 | "Click OK to start again." ), |
105 | button0: QMessageBox::Ok); |
106 | |
107 | setupPuzzle(); |
108 | } |
109 | |
110 | void MainWindow::setupPuzzle() |
111 | { |
112 | int size = qMin(a: puzzleImage.width(), b: puzzleImage.height()); |
113 | puzzleImage = puzzleImage.copy(ax: (puzzleImage.width() - size) / 2, |
114 | ay: (puzzleImage.height() - size) / 2, awidth: size, aheight: size).scaled(w: puzzleWidget->imageSize(), |
115 | h: puzzleWidget->imageSize(), aspectMode: Qt::IgnoreAspectRatio, mode: Qt::SmoothTransformation); |
116 | |
117 | model->addPieces(pixmap: puzzleImage); |
118 | puzzleWidget->clear(); |
119 | } |
120 | |
121 | void MainWindow::setupMenus() |
122 | { |
123 | QMenu * = menuBar()->addMenu(title: tr(s: "&File" )); |
124 | |
125 | QAction *openAction = fileMenu->addAction(text: tr(s: "&Open..." ), object: this, slot: &MainWindow::openImage); |
126 | openAction->setShortcuts(QKeySequence::Open); |
127 | |
128 | QAction *exitAction = fileMenu->addAction(text: tr(s: "E&xit" ), qApp, slot: &QCoreApplication::quit); |
129 | exitAction->setShortcuts(QKeySequence::Quit); |
130 | |
131 | QMenu * = menuBar()->addMenu(title: tr(s: "&Game" )); |
132 | |
133 | gameMenu->addAction(text: tr(s: "&Restart" ), object: this, slot: &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(sender: puzzleWidget, signal: &PuzzleWidget::puzzleCompleted, |
157 | receiver: this, slot: &MainWindow::setCompleted, type: Qt::QueuedConnection); |
158 | |
159 | frameLayout->addWidget(piecesList); |
160 | frameLayout->addWidget(puzzleWidget); |
161 | setCentralWidget(frame); |
162 | } |
163 | |