diff options
Diffstat (limited to 'examples/widgets/draganddrop')
30 files changed, 79 insertions, 1084 deletions
diff --git a/examples/widgets/draganddrop/CMakeLists.txt b/examples/widgets/draganddrop/CMakeLists.txt index c75600f660..bdc6480790 100644 --- a/examples/widgets/draganddrop/CMakeLists.txt +++ b/examples/widgets/draganddrop/CMakeLists.txt @@ -1,8 +1,6 @@ # Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause 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..ad56f3f677 100644 --- a/examples/widgets/draganddrop/draganddrop.pro +++ b/examples/widgets/draganddrop/draganddrop.pro @@ -1,6 +1,4 @@ TEMPLATE = subdirs SUBDIRS = draggableicons \ draggabletext \ - dropsite \ - fridgemagnets \ - puzzle + dropsite diff --git a/examples/widgets/draganddrop/draggableicons/CMakeLists.txt b/examples/widgets/draganddrop/draggableicons/CMakeLists.txt index 44dbcc0fc9..c1fac8da6b 100644 --- a/examples/widgets/draganddrop/draggableicons/CMakeLists.txt +++ b/examples/widgets/draganddrop/draggableicons/CMakeLists.txt @@ -1,15 +1,9 @@ # Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(draggableicons LANGUAGES CXX) -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/draggableicons") - find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) qt_standard_project_setup() @@ -30,6 +24,11 @@ target_link_libraries(draggableicons PRIVATE Qt6::Widgets ) +if(EMSCRIPTEN) + target_link_options(draggableicons PUBLIC -sASYNCIFY -Os) +endif() + + # Resources: set(draggableicons_resource_files "images/boat.png" @@ -45,7 +44,14 @@ qt_add_resources(draggableicons "draggableicons" ) install(TARGETS draggableicons - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET draggableicons + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/widgets/draganddrop/draggableicons/draggableicons.pro b/examples/widgets/draganddrop/draggableicons/draggableicons.pro index f87ec4ad60..e43df790b4 100644 --- a/examples/widgets/draganddrop/draggableicons/draggableicons.pro +++ b/examples/widgets/draganddrop/draggableicons/draggableicons.pro @@ -4,7 +4,9 @@ HEADERS = dragwidget.h RESOURCES = draggableicons.qrc SOURCES = dragwidget.cpp \ main.cpp - +wasm { + QMAKE_LFLAGS += -sASYNCIFY -Os +} # install target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/draggableicons INSTALLS += target diff --git a/examples/widgets/draganddrop/draggableicons/main.cpp b/examples/widgets/draganddrop/draggableicons/main.cpp index cd22e4e394..fe92708594 100644 --- a/examples/widgets/draganddrop/draggableicons/main.cpp +++ b/examples/widgets/draganddrop/draggableicons/main.cpp @@ -8,8 +8,6 @@ int main(int argc, char *argv[]) { - Q_INIT_RESOURCE(draggableicons); - QApplication app(argc, argv); QWidget mainWidget; diff --git a/examples/widgets/draganddrop/draggabletext/CMakeLists.txt b/examples/widgets/draganddrop/draggabletext/CMakeLists.txt index a876f3fe8a..bc1e72db80 100644 --- a/examples/widgets/draganddrop/draggabletext/CMakeLists.txt +++ b/examples/widgets/draganddrop/draggabletext/CMakeLists.txt @@ -1,15 +1,9 @@ # Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(draggabletext LANGUAGES CXX) -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/draggabletext") - find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) qt_standard_project_setup() @@ -30,6 +24,10 @@ target_link_libraries(draggabletext PRIVATE Qt6::Widgets ) +if(EMSCRIPTEN) + target_link_options(draggabletext PUBLIC -sASYNCIFY -Os) +endif() + # Resources: set(draggabletext_resource_files "words.txt" @@ -43,7 +41,14 @@ qt_add_resources(draggabletext "draggabletext" ) install(TARGETS draggabletext - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET draggabletext + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/widgets/draganddrop/draggabletext/draggabletext.pro b/examples/widgets/draganddrop/draggabletext/draggabletext.pro index 1add2a270e..007b9d834e 100644 --- a/examples/widgets/draganddrop/draggabletext/draggabletext.pro +++ b/examples/widgets/draganddrop/draggabletext/draggabletext.pro @@ -4,6 +4,9 @@ HEADERS = dragwidget.h RESOURCES = draggabletext.qrc SOURCES = dragwidget.cpp \ main.cpp +wasm { + QMAKE_LFLAGS += -sASYNCIFY -Os +} # install target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/draggabletext diff --git a/examples/widgets/draganddrop/draggabletext/main.cpp b/examples/widgets/draganddrop/draggabletext/main.cpp index f6ab39aee1..ebfd481e62 100644 --- a/examples/widgets/draganddrop/draggabletext/main.cpp +++ b/examples/widgets/draganddrop/draggabletext/main.cpp @@ -7,8 +7,6 @@ int main(int argc, char *argv[]) { - Q_INIT_RESOURCE(draggabletext); - QApplication app(argc, argv); DragWidget window; window.show(); diff --git a/examples/widgets/draganddrop/draggabletext/words.txt b/examples/widgets/draganddrop/draggabletext/words.txt index 19b8b0325b..78bf304fa9 100644 --- a/examples/widgets/draganddrop/draggabletext/words.txt +++ b/examples/widgets/draganddrop/draggabletext/words.txt @@ -1,41 +1,35 @@ Qt -Quarterly +Academy is -a -paper -based -newsletter -exclusively -available -to +where +you +begin +your +journey +into Qt -customers -Every -quarter -we -mail -out -an -issue +It +is +a +learning +platform that -we -hope -will -bring -added -insight +can +teach +you +everything +from +the +basics +to +the +details and -pleasure +is +open to -your -Qt -programming -with -high -quality -technical -articles -written -by -Qt -experts +students +and +experienced +programmers +alike diff --git a/examples/widgets/draganddrop/dropsite/CMakeLists.txt b/examples/widgets/draganddrop/dropsite/CMakeLists.txt index dd46483c10..ae8d00eb91 100644 --- a/examples/widgets/draganddrop/dropsite/CMakeLists.txt +++ b/examples/widgets/draganddrop/dropsite/CMakeLists.txt @@ -1,15 +1,9 @@ # Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause cmake_minimum_required(VERSION 3.16) project(dropsite LANGUAGES CXX) -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/dropsite") - find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) qt_standard_project_setup() @@ -32,7 +26,14 @@ target_link_libraries(dropsite PRIVATE ) install(TARGETS dropsite - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET dropsite + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/widgets/draganddrop/fridgemagnets/CMakeLists.txt b/examples/widgets/draganddrop/fridgemagnets/CMakeLists.txt deleted file mode 100644 index e1b7eda112..0000000000 --- a/examples/widgets/draganddrop/fridgemagnets/CMakeLists.txt +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(fridgemagnets LANGUAGES CXX) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/draganddrop/fridgemagnets") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) - -qt_standard_project_setup() - -qt_add_executable(fridgemagnets - draglabel.cpp draglabel.h - dragwidget.cpp dragwidget.h - main.cpp -) - -set_target_properties(fridgemagnets PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(fridgemagnets PRIVATE - Qt6::Core - Qt6::Gui - Qt6::Widgets -) - -# Resources: -set(fridgemagnets_resource_files - "words.txt" -) - -qt_add_resources(fridgemagnets "fridgemagnets" - PREFIX - "/dictionary" - FILES - ${fridgemagnets_resource_files} -) - -install(TARGETS fridgemagnets - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/widgets/draganddrop/fridgemagnets/draglabel.cpp b/examples/widgets/draganddrop/fridgemagnets/draglabel.cpp deleted file mode 100644 index 952faae8fe..0000000000 --- a/examples/widgets/draganddrop/fridgemagnets/draglabel.cpp +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "draglabel.h" - -#include <QtWidgets> - -//! [0] -DragLabel::DragLabel(const QString &text, QWidget *parent) - : QLabel(parent) -{ - QFontMetrics metric(font()); - QSize size = metric.size(Qt::TextSingleLine, text); - - QImage image(size.width() + 12, size.height() + 12, QImage::Format_ARGB32_Premultiplied); - image.fill(qRgba(0, 0, 0, 0)); - - QFont font; - font.setStyleStrategy(QFont::ForceOutline); -//! [0] - -//! [1] - QLinearGradient gradient(0, 0, 0, image.height()-1); - gradient.setColorAt(0.0, Qt::white); - gradient.setColorAt(0.2, QColor(200, 200, 255)); - gradient.setColorAt(0.8, QColor(200, 200, 255)); - gradient.setColorAt(1.0, QColor(127, 127, 200)); - - QPainter painter; - painter.begin(&image); - painter.setRenderHint(QPainter::Antialiasing); - painter.setBrush(gradient); - painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1), - 25, 25, Qt::RelativeSize); - - painter.setFont(font); - painter.setBrush(Qt::black); - painter.drawText(QRect(QPoint(6, 6), size), Qt::AlignCenter, text); - painter.end(); -//! [1] - -//! [2] - setPixmap(QPixmap::fromImage(image)); - m_labelText = text; -} -//! [2] - -QString DragLabel::labelText() const -{ - return m_labelText; -} diff --git a/examples/widgets/draganddrop/fridgemagnets/draglabel.h b/examples/widgets/draganddrop/fridgemagnets/draglabel.h deleted file mode 100644 index e734a9ff78..0000000000 --- a/examples/widgets/draganddrop/fridgemagnets/draglabel.h +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef DRAGLABEL_H -#define DRAGLABEL_H - -#include <QLabel> - -QT_BEGIN_NAMESPACE -class QDragEnterEvent; -class QDragMoveEvent; -class QFrame; -QT_END_NAMESPACE - -//! [0] -class DragLabel : public QLabel -{ -public: - DragLabel(const QString &text, QWidget *parent); - QString labelText() const; - -private: - QString m_labelText; -}; -//! [0] - -#endif // DRAGLABEL_H diff --git a/examples/widgets/draganddrop/fridgemagnets/dragwidget.cpp b/examples/widgets/draganddrop/fridgemagnets/dragwidget.cpp deleted file mode 100644 index a883e7c58a..0000000000 --- a/examples/widgets/draganddrop/fridgemagnets/dragwidget.cpp +++ /dev/null @@ -1,176 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "draglabel.h" -#include "dragwidget.h" - -#include <QtWidgets> - -static inline QString fridgetMagnetsMimeType() { return QStringLiteral("application/x-fridgemagnet"); } - -//! [0] -DragWidget::DragWidget(QWidget *parent) - : QWidget(parent) -{ - QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt")); - dictionaryFile.open(QFile::ReadOnly); - QTextStream inputStream(&dictionaryFile); -//! [0] - -//! [1] - int x = 5; - int y = 5; - - while (!inputStream.atEnd()) { - QString word; - inputStream >> word; - if (!word.isEmpty()) { - DragLabel *wordLabel = new DragLabel(word, this); - wordLabel->move(x, y); - wordLabel->show(); - wordLabel->setAttribute(Qt::WA_DeleteOnClose); - x += wordLabel->width() + 2; - if (x >= 245) { - x = 5; - y += wordLabel->height() + 2; - } - } - } -//! [1] - -//! [2] - QPalette newPalette = palette(); - newPalette.setColor(QPalette::Window, Qt::white); - setPalette(newPalette); - - setMinimumSize(400, qMax(200, y)); - setWindowTitle(tr("Fridge Magnets")); -//! [2] //! [3] - setAcceptDrops(true); -} -//! [3] - -//! [4] -void DragWidget::dragEnterEvent(QDragEnterEvent *event) -{ -//! [4] //! [5] - if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) { - if (children().contains(event->source())) { - event->setDropAction(Qt::MoveAction); - event->accept(); - } else { - event->acceptProposedAction(); -//! [5] //! [6] - } -//! [6] //! [7] - } else if (event->mimeData()->hasText()) { - event->acceptProposedAction(); - } else { - event->ignore(); - } -} -//! [7] - -//! [8] -void DragWidget::dragMoveEvent(QDragMoveEvent *event) -{ - if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) { - if (children().contains(event->source())) { - event->setDropAction(Qt::MoveAction); - event->accept(); - } else { - event->acceptProposedAction(); - } - } else if (event->mimeData()->hasText()) { - event->acceptProposedAction(); - } else { - event->ignore(); - } -} -//! [8] - -//! [9] -void DragWidget::dropEvent(QDropEvent *event) -{ - if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) { - const QMimeData *mime = event->mimeData(); -//! [9] //! [10] - QByteArray itemData = mime->data(fridgetMagnetsMimeType()); - QDataStream dataStream(&itemData, QIODevice::ReadOnly); - - QString text; - QPoint offset; - dataStream >> text >> offset; -//! [10] -//! [11] - DragLabel *newLabel = new DragLabel(text, this); - newLabel->move(event->position().toPoint() - offset); - newLabel->show(); - newLabel->setAttribute(Qt::WA_DeleteOnClose); - - if (event->source() == this) { - event->setDropAction(Qt::MoveAction); - event->accept(); - } else { - event->acceptProposedAction(); - } -//! [11] //! [12] - } else if (event->mimeData()->hasText()) { - QStringList pieces = event->mimeData()->text().split( - QRegularExpression(QStringLiteral("\\s+")), Qt::SkipEmptyParts); - QPoint position = event->position().toPoint(); - - for (const QString &piece : pieces) { - DragLabel *newLabel = new DragLabel(piece, this); - newLabel->move(position); - newLabel->show(); - newLabel->setAttribute(Qt::WA_DeleteOnClose); - - position += QPoint(newLabel->width(), 0); - } - - event->acceptProposedAction(); - } else { - event->ignore(); - } -} -//! [12] - -//! [13] -void DragWidget::mousePressEvent(QMouseEvent *event) -{ -//! [13] -//! [14] - DragLabel *child = static_cast<DragLabel*>(childAt(event->position().toPoint())); - if (!child) - return; - - QPoint hotSpot = event->position().toPoint() - child->pos(); - - QByteArray itemData; - QDataStream dataStream(&itemData, QIODevice::WriteOnly); - dataStream << child->labelText() << QPoint(hotSpot); -//! [14] - -//! [15] - QMimeData *mimeData = new QMimeData; - mimeData->setData(fridgetMagnetsMimeType(), itemData); - mimeData->setText(child->labelText()); -//! [15] - -//! [16] - QDrag *drag = new QDrag(this); - drag->setMimeData(mimeData); - drag->setPixmap(child->pixmap()); - drag->setHotSpot(hotSpot); - - child->hide(); -//! [16] - -//! [17] - if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction) - child->close(); - else - child->show(); -} -//! [17] diff --git a/examples/widgets/draganddrop/fridgemagnets/dragwidget.h b/examples/widgets/draganddrop/fridgemagnets/dragwidget.h deleted file mode 100644 index 9614a3b76a..0000000000 --- a/examples/widgets/draganddrop/fridgemagnets/dragwidget.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 DRAGWIDGET_H -#define DRAGWIDGET_H - -#include <QWidget> - -QT_BEGIN_NAMESPACE -class QDragEnterEvent; -class QDropEvent; -QT_END_NAMESPACE - -//! [0] -class DragWidget : public QWidget -{ -public: - explicit DragWidget(QWidget *parent = nullptr); - -protected: - void dragEnterEvent(QDragEnterEvent *event) override; - void dragMoveEvent(QDragMoveEvent *event) override; - void dropEvent(QDropEvent *event) override; - void mousePressEvent(QMouseEvent *event) override; -}; -//! [0] - -#endif // DRAGWIDGET_H diff --git a/examples/widgets/draganddrop/fridgemagnets/fridgemagnets.pro b/examples/widgets/draganddrop/fridgemagnets/fridgemagnets.pro deleted file mode 100644 index 2c3165d1c5..0000000000 --- a/examples/widgets/draganddrop/fridgemagnets/fridgemagnets.pro +++ /dev/null @@ -1,12 +0,0 @@ -QT += widgets - -HEADERS = draglabel.h \ - dragwidget.h -RESOURCES = fridgemagnets.qrc -SOURCES = draglabel.cpp \ - dragwidget.cpp \ - main.cpp - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/widgets/draganddrop/fridgemagnets -INSTALLS += target diff --git a/examples/widgets/draganddrop/fridgemagnets/fridgemagnets.qrc b/examples/widgets/draganddrop/fridgemagnets/fridgemagnets.qrc deleted file mode 100644 index b72217d701..0000000000 --- a/examples/widgets/draganddrop/fridgemagnets/fridgemagnets.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource prefix="/dictionary"> - <file>words.txt</file> -</qresource> -</RCC> diff --git a/examples/widgets/draganddrop/fridgemagnets/main.cpp b/examples/widgets/draganddrop/fridgemagnets/main.cpp deleted file mode 100644 index d2b2e5dbed..0000000000 --- a/examples/widgets/draganddrop/fridgemagnets/main.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include <QApplication> - -#include "dragwidget.h" - -int main(int argc, char *argv[]) -{ - Q_INIT_RESOURCE(fridgemagnets); - - QApplication app(argc, argv); -#ifdef QT_KEYPAD_NAVIGATION - QApplication::setNavigationMode(Qt::NavigationModeCursorAuto); -#endif - DragWidget window; - - bool smallScreen = QApplication::arguments().contains(QStringLiteral("-small-screen")); - if (smallScreen) - window.showFullScreen(); - else - window.show(); - - return app.exec(); -} diff --git a/examples/widgets/draganddrop/fridgemagnets/words.txt b/examples/widgets/draganddrop/fridgemagnets/words.txt deleted file mode 100644 index a7e1632b09..0000000000 --- a/examples/widgets/draganddrop/fridgemagnets/words.txt +++ /dev/null @@ -1,48 +0,0 @@ -Colorless -green -ideas -sleep -furiously -A -colorless -green -idea -is -a -new -untried -idea -that -is -without -vividness -dull -and -unexciting -To -sleep -furiously -may -seem -a -puzzling -turn -of -phrase -but -the -mind -in -sleep -often -indeed -moves -furiously -with -ideas -and -images -flickering -in -and -out 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 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 73fdd6513b..0000000000 --- a/examples/widgets/draganddrop/puzzle/main.cpp +++ /dev/null @@ -1,17 +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[]) -{ - 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 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 |