summaryrefslogtreecommitdiffstats
path: root/examples/widgets
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2023-06-26 13:48:57 +0200
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2023-06-28 00:09:58 +0200
commitcefb1c9ca9af73999247678907c496c1a143d00f (patch)
tree9d77b10e151289ffd862f6ef7f42dfc510be0611 /examples/widgets
parent3a9526468c134b68b64b9a3bb6278d47c266e381 (diff)
Move drag and drop puzzle example to manual test
Pick-to: 6.5 6.6 Change-Id: I859cdb2cf74cd6272c29924c77dab26b4f4c0b6c Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'examples/widgets')
-rw-r--r--examples/widgets/doc/images/draganddroppuzzle-example.pngbin259104 -> 0 bytes
-rw-r--r--examples/widgets/doc/src/draganddroppuzzle.qdoc18
-rw-r--r--examples/widgets/doc/src/itemviewspuzzle.qdoc4
-rw-r--r--examples/widgets/draganddrop/CMakeLists.txt1
-rw-r--r--examples/widgets/draganddrop/draganddrop.pro3
-rw-r--r--examples/widgets/draganddrop/puzzle/CMakeLists.txt51
-rw-r--r--examples/widgets/draganddrop/puzzle/example.jpgbin42654 -> 0 bytes
-rw-r--r--examples/widgets/draganddrop/puzzle/main.cpp15
-rw-r--r--examples/widgets/draganddrop/puzzle/mainwindow.cpp118
-rw-r--r--examples/widgets/draganddrop/puzzle/mainwindow.h40
-rw-r--r--examples/widgets/draganddrop/puzzle/pieceslist.cpp87
-rw-r--r--examples/widgets/draganddrop/puzzle/pieceslist.h28
-rw-r--r--examples/widgets/draganddrop/puzzle/puzzle.pro17
-rw-r--r--examples/widgets/draganddrop/puzzle/puzzle.qrc5
-rw-r--r--examples/widgets/draganddrop/puzzle/puzzlewidget.cpp167
-rw-r--r--examples/widgets/draganddrop/puzzle/puzzlewidget.h56
16 files changed, 1 insertions, 609 deletions
diff --git a/examples/widgets/doc/images/draganddroppuzzle-example.png b/examples/widgets/doc/images/draganddroppuzzle-example.png
deleted file mode 100644
index ca6844581e..0000000000
--- a/examples/widgets/doc/images/draganddroppuzzle-example.png
+++ /dev/null
Binary files differ
diff --git a/examples/widgets/doc/src/draganddroppuzzle.qdoc b/examples/widgets/doc/src/draganddroppuzzle.qdoc
deleted file mode 100644
index c2504b245b..0000000000
--- a/examples/widgets/doc/src/draganddroppuzzle.qdoc
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
-
-/*!
- \example draganddrop/puzzle
- \title Drag and Drop Puzzle Example
-
- \brief The Drag and Drop Puzzle example demonstrates a way of using the drag and drop system with
- item view widgets.
-
- \image draganddroppuzzle-example.png
-
- This example is an implementation of a simple jigsaw puzzle game using Qt's
- drag and drop API.
- The \l{Item Views Puzzle Example}{Item View Puzzle} example shows
- many of the same features, but takes an alternative approach that uses Qt's
- model/view framework to manage drag and drop operations.
-*/
diff --git a/examples/widgets/doc/src/itemviewspuzzle.qdoc b/examples/widgets/doc/src/itemviewspuzzle.qdoc
index c1e0c361d6..9e77caf87e 100644
--- a/examples/widgets/doc/src/itemviewspuzzle.qdoc
+++ b/examples/widgets/doc/src/itemviewspuzzle.qdoc
@@ -12,8 +12,4 @@
This example is an implementation of a simple jigsaw puzzle game using the
built-in support for drag and drop provided by Qt's model/view framework.
- The \l{Drag and Drop Puzzle Example}{Drag and Drop Puzzle} example shows
- many of the same features, but takes an alternative approach that uses Qt's
- drag and drop API at the application level to handle drag and drop
- operations.
*/
diff --git a/examples/widgets/draganddrop/CMakeLists.txt b/examples/widgets/draganddrop/CMakeLists.txt
index c75600f660..88d571d31e 100644
--- a/examples/widgets/draganddrop/CMakeLists.txt
+++ b/examples/widgets/draganddrop/CMakeLists.txt
@@ -5,4 +5,3 @@ qt_internal_add_example(draggableicons)
qt_internal_add_example(draggabletext)
qt_internal_add_example(dropsite)
qt_internal_add_example(fridgemagnets)
-qt_internal_add_example(puzzle)
diff --git a/examples/widgets/draganddrop/draganddrop.pro b/examples/widgets/draganddrop/draganddrop.pro
index 1ecafd33a1..e62a0a59d0 100644
--- a/examples/widgets/draganddrop/draganddrop.pro
+++ b/examples/widgets/draganddrop/draganddrop.pro
@@ -2,5 +2,4 @@ TEMPLATE = subdirs
SUBDIRS = draggableicons \
draggabletext \
dropsite \
- fridgemagnets \
- puzzle
+ fridgemagnets
diff --git a/examples/widgets/draganddrop/puzzle/CMakeLists.txt b/examples/widgets/draganddrop/puzzle/CMakeLists.txt
deleted file mode 100644
index 13c6717551..0000000000
--- a/examples/widgets/draganddrop/puzzle/CMakeLists.txt
+++ /dev/null
@@ -1,51 +0,0 @@
-# Copyright (C) 2022 The Qt Company Ltd.
-# SPDX-License-Identifier: BSD-3-Clause
-
-cmake_minimum_required(VERSION 3.16)
-project(puzzle LANGUAGES CXX)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop_puzzle")
-
-find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
-
-qt_standard_project_setup()
-
-qt_add_executable(draganddrop_puzzle
- main.cpp
- mainwindow.cpp mainwindow.h
- pieceslist.cpp pieceslist.h
- puzzlewidget.cpp puzzlewidget.h
-)
-
-set_target_properties(draganddrop_puzzle PROPERTIES
- WIN32_EXECUTABLE TRUE
- MACOSX_BUNDLE TRUE
-)
-
-target_link_libraries(draganddrop_puzzle PRIVATE
- Qt6::Core
- Qt6::Gui
- Qt6::Widgets
-)
-
-# Resources:
-set(puzzle_resource_files
- "example.jpg"
-)
-
-qt_add_resources(draganddrop_puzzle "puzzle"
- PREFIX
- "/images"
- FILES
- ${puzzle_resource_files}
-)
-
-install(TARGETS draganddrop_puzzle
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
-)
diff --git a/examples/widgets/draganddrop/puzzle/example.jpg b/examples/widgets/draganddrop/puzzle/example.jpg
deleted file mode 100644
index 023203c57a..0000000000
--- a/examples/widgets/draganddrop/puzzle/example.jpg
+++ /dev/null
Binary files differ
diff --git a/examples/widgets/draganddrop/puzzle/main.cpp b/examples/widgets/draganddrop/puzzle/main.cpp
deleted file mode 100644
index 32e219256a..0000000000
--- a/examples/widgets/draganddrop/puzzle/main.cpp
+++ /dev/null
@@ -1,15 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "mainwindow.h"
-
-#include <QApplication>
-
-int main(int argc, char *argv[])
-{
- QApplication app(argc, argv);
- MainWindow window;
- window.loadImage(QStringLiteral(":/images/example.jpg"));
- window.show();
- return app.exec();
-}
diff --git a/examples/widgets/draganddrop/puzzle/mainwindow.cpp b/examples/widgets/draganddrop/puzzle/mainwindow.cpp
deleted file mode 100644
index b34bc24201..0000000000
--- a/examples/widgets/draganddrop/puzzle/mainwindow.cpp
+++ /dev/null
@@ -1,118 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "mainwindow.h"
-#include "pieceslist.h"
-#include "puzzlewidget.h"
-
-#include <QtWidgets>
-#include <stdlib.h>
-
-MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
-{
- setupMenus();
- setupWidgets();
-
- setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
- setWindowTitle(tr("Puzzle"));
-}
-
-void MainWindow::openImage()
-{
- const QString directory =
- QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).value(0, QDir::homePath());
- QFileDialog dialog(this, tr("Open Image"), directory);
- dialog.setAcceptMode(QFileDialog::AcceptOpen);
- dialog.setFileMode(QFileDialog::ExistingFile);
- QStringList mimeTypeFilters;
- for (const QByteArray &mimeTypeName : QImageReader::supportedMimeTypes())
- mimeTypeFilters.append(mimeTypeName);
- mimeTypeFilters.sort();
- dialog.setMimeTypeFilters(mimeTypeFilters);
- dialog.selectMimeTypeFilter("image/jpeg");
- if (dialog.exec() == QDialog::Accepted)
- loadImage(dialog.selectedFiles().constFirst());
-}
-
-void MainWindow::loadImage(const QString &fileName)
-{
- QPixmap newImage;
- if (!newImage.load(fileName)) {
- QMessageBox::warning(this, tr("Open Image"),
- tr("The image file could not be loaded."),
- QMessageBox::Close);
- return;
- }
- puzzleImage = newImage;
- setupPuzzle();
-}
-
-void MainWindow::setCompleted()
-{
- QMessageBox::information(this, tr("Puzzle Completed"),
- tr("Congratulations! You have completed the puzzle!\n"
- "Click OK to start again."),
- QMessageBox::Ok);
-
- setupPuzzle();
-}
-
-void MainWindow::setupPuzzle()
-{
- int size = qMin(puzzleImage.width(), puzzleImage.height());
- puzzleImage = puzzleImage.copy((puzzleImage.width() - size) / 2,
- (puzzleImage.height() - size) / 2, size, size).scaled(puzzleWidget->width(),
- puzzleWidget->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
-
- piecesList->clear();
-
- for (int y = 0; y < 5; ++y) {
- for (int x = 0; x < 5; ++x) {
- int pieceSize = puzzleWidget->pieceSize();
- QPixmap pieceImage = puzzleImage.copy(x * pieceSize, y * pieceSize, pieceSize, pieceSize);
- piecesList->addPiece(pieceImage, QPoint(x, y));
- }
- }
-
- for (int i = 0; i < piecesList->count(); ++i) {
- if (QRandomGenerator::global()->bounded(2) == 1) {
- QListWidgetItem *item = piecesList->takeItem(i);
- piecesList->insertItem(0, item);
- }
- }
-
- puzzleWidget->clear();
-}
-
-void MainWindow::setupMenus()
-{
- QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
-
- QAction *openAction = fileMenu->addAction(tr("&Open..."), this, &MainWindow::openImage);
- openAction->setShortcuts(QKeySequence::Open);
-
- QAction *exitAction = fileMenu->addAction(tr("E&xit"), qApp, &QCoreApplication::quit);
- exitAction->setShortcuts(QKeySequence::Quit);
-
- QMenu *gameMenu = menuBar()->addMenu(tr("&Game"));
-
- gameMenu->addAction(tr("&Restart"), this, &MainWindow::setupPuzzle);
-}
-
-void MainWindow::setupWidgets()
-{
- QFrame *frame = new QFrame;
- QHBoxLayout *frameLayout = new QHBoxLayout(frame);
- puzzleWidget = new PuzzleWidget(400);
-
- piecesList = new PiecesList(puzzleWidget->pieceSize(), this);
-
-
- connect(puzzleWidget, &PuzzleWidget::puzzleCompleted,
- this, &MainWindow::setCompleted, Qt::QueuedConnection);
-
- frameLayout->addWidget(piecesList);
- frameLayout->addWidget(puzzleWidget);
- setCentralWidget(frame);
-}
diff --git a/examples/widgets/draganddrop/puzzle/mainwindow.h b/examples/widgets/draganddrop/puzzle/mainwindow.h
deleted file mode 100644
index 83a441c722..0000000000
--- a/examples/widgets/draganddrop/puzzle/mainwindow.h
+++ /dev/null
@@ -1,40 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef MAINWINDOW_H
-#define MAINWINDOW_H
-
-#include <QMainWindow>
-#include <QPixmap>
-
-class PiecesList;
-class PuzzleWidget;
-QT_BEGIN_NAMESPACE
-class QListWidgetItem;
-QT_END_NAMESPACE
-
-class MainWindow : public QMainWindow
-{
- Q_OBJECT
-
-public:
- explicit MainWindow(QWidget *parent = nullptr);
- void loadImage(const QString &path);
-
-public slots:
- void openImage();
- void setupPuzzle();
-
-private slots:
- void setCompleted();
-
-private:
- void setupMenus();
- void setupWidgets();
-
- QPixmap puzzleImage;
- PiecesList *piecesList;
- PuzzleWidget *puzzleWidget;
-};
-
-#endif // MAINWINDOW_H
diff --git a/examples/widgets/draganddrop/puzzle/pieceslist.cpp b/examples/widgets/draganddrop/puzzle/pieceslist.cpp
deleted file mode 100644
index 0c7e771e42..0000000000
--- a/examples/widgets/draganddrop/puzzle/pieceslist.cpp
+++ /dev/null
@@ -1,87 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "pieceslist.h"
-
-#include <QDrag>
-#include <QDragEnterEvent>
-#include <QMimeData>
-
-PiecesList::PiecesList(int pieceSize, QWidget *parent)
- : QListWidget(parent), m_PieceSize(pieceSize)
-{
- setDragEnabled(true);
- setViewMode(QListView::IconMode);
- setIconSize(QSize(m_PieceSize, m_PieceSize));
- setSpacing(10);
- setAcceptDrops(true);
- setDropIndicatorShown(true);
-}
-
-void PiecesList::dragEnterEvent(QDragEnterEvent *event)
-{
- if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType()))
- event->accept();
- else
- event->ignore();
-}
-
-void PiecesList::dragMoveEvent(QDragMoveEvent *event)
-{
- if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())) {
- event->setDropAction(Qt::MoveAction);
- event->accept();
- } else {
- event->ignore();
- }
-}
-
-void PiecesList::dropEvent(QDropEvent *event)
-{
- if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())) {
- QByteArray pieceData = event->mimeData()->data(PiecesList::puzzleMimeType());
- QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
- QPixmap pixmap;
- QPoint location;
- dataStream >> pixmap >> location;
-
- addPiece(pixmap, location);
-
- event->setDropAction(Qt::MoveAction);
- event->accept();
- } else {
- event->ignore();
- }
-}
-
-void PiecesList::addPiece(const QPixmap &pixmap, const QPoint &location)
-{
- QListWidgetItem *pieceItem = new QListWidgetItem(this);
- pieceItem->setIcon(QIcon(pixmap));
- pieceItem->setData(Qt::UserRole, QVariant(pixmap));
- pieceItem->setData(Qt::UserRole+1, location);
- pieceItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled);
-}
-
-void PiecesList::startDrag(Qt::DropActions /*supportedActions*/)
-{
- QListWidgetItem *item = currentItem();
-
- QByteArray itemData;
- QDataStream dataStream(&itemData, QIODevice::WriteOnly);
- QPixmap pixmap = qvariant_cast<QPixmap>(item->data(Qt::UserRole));
- QPoint location = item->data(Qt::UserRole+1).toPoint();
-
- dataStream << pixmap << location;
-
- QMimeData *mimeData = new QMimeData;
- mimeData->setData(PiecesList::puzzleMimeType(), itemData);
-
- QDrag *drag = new QDrag(this);
- drag->setMimeData(mimeData);
- drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
- drag->setPixmap(pixmap);
-
- if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
- delete takeItem(row(item));
-}
diff --git a/examples/widgets/draganddrop/puzzle/pieceslist.h b/examples/widgets/draganddrop/puzzle/pieceslist.h
deleted file mode 100644
index 4c617e7006..0000000000
--- a/examples/widgets/draganddrop/puzzle/pieceslist.h
+++ /dev/null
@@ -1,28 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef PIECESLIST_H
-#define PIECESLIST_H
-
-#include <QListWidget>
-
-class PiecesList : public QListWidget
-{
- Q_OBJECT
-
-public:
- explicit PiecesList(int pieceSize, QWidget *parent = nullptr);
- void addPiece(const QPixmap &pixmap, const QPoint &location);
-
- static QString puzzleMimeType() { return QStringLiteral("image/x-puzzle-piece"); }
-
-protected:
- void dragEnterEvent(QDragEnterEvent *event) override;
- void dragMoveEvent(QDragMoveEvent *event) override;
- void dropEvent(QDropEvent *event) override;
- void startDrag(Qt::DropActions supportedActions) override;
-
- int m_PieceSize;
-};
-
-#endif // PIECESLIST_H
diff --git a/examples/widgets/draganddrop/puzzle/puzzle.pro b/examples/widgets/draganddrop/puzzle/puzzle.pro
deleted file mode 100644
index c462ba1bb8..0000000000
--- a/examples/widgets/draganddrop/puzzle/puzzle.pro
+++ /dev/null
@@ -1,17 +0,0 @@
-QT += widgets
-requires(qtConfig(filedialog))
-
-HEADERS = mainwindow.h \
- pieceslist.h \
- puzzlewidget.h
-RESOURCES = puzzle.qrc
-SOURCES = main.cpp \
- mainwindow.cpp \
- pieceslist.cpp \
- puzzlewidget.cpp
-
-QMAKE_PROJECT_NAME = dndpuzzle
-
-# install
-target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/puzzle
-INSTALLS += target
diff --git a/examples/widgets/draganddrop/puzzle/puzzle.qrc b/examples/widgets/draganddrop/puzzle/puzzle.qrc
deleted file mode 100644
index 4076cec026..0000000000
--- a/examples/widgets/draganddrop/puzzle/puzzle.qrc
+++ /dev/null
@@ -1,5 +0,0 @@
-<!DOCTYPE RCC><RCC version="1.0">
-<qresource prefix="/images">
- <file>example.jpg</file>
-</qresource>
-</RCC>
diff --git a/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp b/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp
deleted file mode 100644
index 7c3f12f7d8..0000000000
--- a/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp
+++ /dev/null
@@ -1,167 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#include "puzzlewidget.h"
-#include "pieceslist.h"
-
-#include <QDrag>
-#include <QDragEnterEvent>
-#include <QMimeData>
-#include <QPainter>
-
-PuzzleWidget::PuzzleWidget(int imageSize, QWidget *parent)
- : QWidget(parent), m_ImageSize(imageSize)
-{
- setAcceptDrops(true);
- setMinimumSize(m_ImageSize, m_ImageSize);
- setMaximumSize(m_ImageSize, m_ImageSize);
-}
-
-void PuzzleWidget::clear()
-{
- pieces.clear();
- highlightedRect = QRect();
- inPlace = 0;
- update();
-}
-
-void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event)
-{
- if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType()))
- event->accept();
- else
- event->ignore();
-}
-
-void PuzzleWidget::dragLeaveEvent(QDragLeaveEvent *event)
-{
- QRect updateRect = highlightedRect;
- highlightedRect = QRect();
- update(updateRect);
- event->accept();
-}
-
-void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event)
-{
- QRect updateRect = highlightedRect.united(targetSquare(event->position().toPoint()));
-
- if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())
- && findPiece(targetSquare(event->position().toPoint())) == -1) {
-
- highlightedRect = targetSquare(event->position().toPoint());
- event->setDropAction(Qt::MoveAction);
- event->accept();
- } else {
- highlightedRect = QRect();
- event->ignore();
- }
-
- update(updateRect);
-}
-
-void PuzzleWidget::dropEvent(QDropEvent *event)
-{
- if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())
- && findPiece(targetSquare(event->position().toPoint())) == -1) {
-
- QByteArray pieceData = event->mimeData()->data(PiecesList::puzzleMimeType());
- QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
- Piece piece;
- piece.rect = targetSquare(event->position().toPoint());
- dataStream >> piece.pixmap >> piece.location;
-
- pieces.append(piece);
-
- highlightedRect = QRect();
- update(piece.rect);
-
- event->setDropAction(Qt::MoveAction);
- event->accept();
-
- if (piece.location == piece.rect.topLeft() / pieceSize()) {
- inPlace++;
- if (inPlace == 25)
- emit puzzleCompleted();
- }
- } else {
- highlightedRect = QRect();
- event->ignore();
- }
-}
-
-int PuzzleWidget::findPiece(const QRect &pieceRect) const
-{
- for (int i = 0, size = pieces.size(); i < size; ++i) {
- if (pieces.at(i).rect == pieceRect)
- return i;
- }
- return -1;
-}
-
-void PuzzleWidget::mousePressEvent(QMouseEvent *event)
-{
- QRect square = targetSquare(event->position().toPoint());
- const int found = findPiece(square);
-
- if (found == -1)
- return;
-
- Piece piece = pieces.takeAt(found);
-
- if (piece.location == square.topLeft() / pieceSize())
- inPlace--;
-
- update(square);
-
- QByteArray itemData;
- QDataStream dataStream(&itemData, QIODevice::WriteOnly);
-
- dataStream << piece.pixmap << piece.location;
-
- QMimeData *mimeData = new QMimeData;
- mimeData->setData(PiecesList::puzzleMimeType(), itemData);
-
- QDrag *drag = new QDrag(this);
- drag->setMimeData(mimeData);
- drag->setHotSpot(event->position().toPoint() - square.topLeft());
- drag->setPixmap(piece.pixmap);
-
- if (drag->exec(Qt::MoveAction) != Qt::MoveAction) {
- pieces.insert(found, piece);
- update(targetSquare(event->position().toPoint()));
-
- if (piece.location == square.topLeft() / pieceSize())
- inPlace++;
- }
-}
-
-void PuzzleWidget::paintEvent(QPaintEvent *event)
-{
- QPainter painter(this);
- painter.fillRect(event->rect(), Qt::white);
-
- if (highlightedRect.isValid()) {
- painter.setBrush(QColor("#ffcccc"));
- painter.setPen(Qt::NoPen);
- painter.drawRect(highlightedRect.adjusted(0, 0, -1, -1));
- }
-
- for (const Piece &piece : pieces)
- painter.drawPixmap(piece.rect, piece.pixmap);
-}
-
-const QRect PuzzleWidget::targetSquare(const QPoint &position) const
-{
- QPoint topLeft = QPoint(position.x() / pieceSize(), position.y() / pieceSize()) * pieceSize();
- return QRect(topLeft, QSize(pieceSize(), pieceSize()));
-}
-
-int PuzzleWidget::pieceSize() const
-{
- return m_ImageSize / 5;
-}
-
-int PuzzleWidget::imageSize() const
-{
- return m_ImageSize;
-}
diff --git a/examples/widgets/draganddrop/puzzle/puzzlewidget.h b/examples/widgets/draganddrop/puzzle/puzzlewidget.h
deleted file mode 100644
index d1c00872ec..0000000000
--- a/examples/widgets/draganddrop/puzzle/puzzlewidget.h
+++ /dev/null
@@ -1,56 +0,0 @@
-// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-
-#ifndef PUZZLEWIDGET_H
-#define PUZZLEWIDGET_H
-
-#include <QPoint>
-#include <QPixmap>
-#include <QList>
-#include <QWidget>
-
-QT_BEGIN_NAMESPACE
-class QDragEnterEvent;
-class QDropEvent;
-class QMouseEvent;
-QT_END_NAMESPACE
-
-class PuzzleWidget : public QWidget
-{
- Q_OBJECT
-
-public:
- explicit PuzzleWidget(int imageSize, QWidget *parent = nullptr);
- void clear();
-
- int pieceSize() const;
- int imageSize() const;
-
-signals:
- void puzzleCompleted();
-
-protected:
- void dragEnterEvent(QDragEnterEvent *event) override;
- void dragLeaveEvent(QDragLeaveEvent *event) override;
- void dragMoveEvent(QDragMoveEvent *event) override;
- void dropEvent(QDropEvent *event) override;
- void mousePressEvent(QMouseEvent *event) override;
- void paintEvent(QPaintEvent *event) override;
-
-private:
- struct Piece {
- QPixmap pixmap;
- QRect rect;
- QPoint location;
- };
-
- int findPiece(const QRect &pieceRect) const;
- const QRect targetSquare(const QPoint &position) const;
-
- QList<Piece> pieces;
- QRect highlightedRect;
- int inPlace;
- int m_ImageSize;
-};
-
-#endif // PUZZLEWIDGET_H