diff options
Diffstat (limited to 'examples/widgets/draganddrop/puzzle')
-rw-r--r-- | examples/widgets/draganddrop/puzzle/.prev_CMakeLists.txt | 55 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/CMakeLists.txt | 55 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/example.jpg | bin | 42654 -> 0 bytes | |||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/main.cpp | 64 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/mainwindow.cpp | 165 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/mainwindow.h | 87 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/pieceslist.cpp | 134 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/pieceslist.h | 75 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/puzzle.pro | 17 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/puzzle.qrc | 5 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/puzzlewidget.cpp | 214 | ||||
-rw-r--r-- | examples/widgets/draganddrop/puzzle/puzzlewidget.h | 103 |
12 files changed, 0 insertions, 974 deletions
diff --git a/examples/widgets/draganddrop/puzzle/.prev_CMakeLists.txt b/examples/widgets/draganddrop/puzzle/.prev_CMakeLists.txt deleted file mode 100644 index bcc7708662..0000000000 --- a/examples/widgets/draganddrop/puzzle/.prev_CMakeLists.txt +++ /dev/null @@ -1,55 +0,0 @@ -# Generated from puzzle.pro. - -cmake_minimum_required(VERSION 3.14) -project(puzzle LANGUAGES CXX) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/puzzle") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Widgets) - -qt_add_executable(puzzle - main.cpp - mainwindow.cpp mainwindow.h - pieceslist.cpp pieceslist.h - puzzlewidget.cpp puzzlewidget.h -) -set_target_properties(puzzle PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) -target_link_libraries(puzzle PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets -) - - -# Resources: -set(puzzle_resource_files - "example.jpg" -) - -qt6_add_resources(puzzle "puzzle" - PREFIX - "/images" - FILES - ${puzzle_resource_files} -) - -install(TARGETS puzzle - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/widgets/draganddrop/puzzle/CMakeLists.txt b/examples/widgets/draganddrop/puzzle/CMakeLists.txt deleted file mode 100644 index b7569fb0d2..0000000000 --- a/examples/widgets/draganddrop/puzzle/CMakeLists.txt +++ /dev/null @@ -1,55 +0,0 @@ -# Generated from puzzle.pro. - -cmake_minimum_required(VERSION 3.14) -project(puzzle LANGUAGES CXX) - -set(CMAKE_INCLUDE_CURRENT_DIR ON) - -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop_puzzle") # special case - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Widgets) - -qt_add_executable(draganddrop_puzzle # special case: renamed puzzle - main.cpp - mainwindow.cpp mainwindow.h - pieceslist.cpp pieceslist.h - puzzlewidget.cpp puzzlewidget.h -) -set_target_properties(draganddrop_puzzle PROPERTIES # special case - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) -target_link_libraries(draganddrop_puzzle PUBLIC # special case - Qt::Core - Qt::Gui - Qt::Widgets -) - - -# Resources: -set(puzzle_resource_files - "example.jpg" -) - -qt6_add_resources(draganddrop_puzzle "puzzle" # special case - PREFIX - "/images" - FILES - ${puzzle_resource_files} -) - -install(TARGETS draganddrop_puzzle # special case: renamed 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 Binary files differdeleted file mode 100644 index 023203c57a..0000000000 --- a/examples/widgets/draganddrop/puzzle/example.jpg +++ /dev/null diff --git a/examples/widgets/draganddrop/puzzle/main.cpp b/examples/widgets/draganddrop/puzzle/main.cpp deleted file mode 100644 index d013bf078d..0000000000 --- a/examples/widgets/draganddrop/puzzle/main.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "mainwindow.h" - -#include <QApplication> - -int main(int argc, char *argv[]) -{ - Q_INIT_RESOURCE(puzzle); - - 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 98a7cd4265..0000000000 --- a/examples/widgets/draganddrop/puzzle/mainwindow.cpp +++ /dev/null @@ -1,165 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#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 626612ebe8..0000000000 --- a/examples/widgets/draganddrop/puzzle/mainwindow.h +++ /dev/null @@ -1,87 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#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 7846d37ed2..0000000000 --- a/examples/widgets/draganddrop/puzzle/pieceslist.cpp +++ /dev/null @@ -1,134 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#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 a508d17d72..0000000000 --- a/examples/widgets/draganddrop/puzzle/pieceslist.h +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#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 06dda9c29f..0000000000 --- a/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp +++ /dev/null @@ -1,214 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#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 -{ - return QRect(position / pieceSize() * pieceSize(), - 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 0ac4719d74..0000000000 --- a/examples/widgets/draganddrop/puzzle/puzzlewidget.h +++ /dev/null @@ -1,103 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#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 |