From c8cd9f1b9a3d338a12bdccce957d74d515eb9c32 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@digia.com>
Date: Wed, 7 Oct 2015 12:03:05 +0200
Subject: [PATCH] Polish DnD Examples.

- Remove class DragLabel in draggabletext and add a static creation
  function instead since it only has a constructor setting some
  properties.
- Use QRegularExpression instead of QRegExp
- Use new connection syntax.
- Ensure compilation with
  DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII
  demonstrating use of QLatin1String vs QStringLiteral.
- Streamline code.

Change-Id: I2e2ddeb40837dba379990836c77fb72ad7263e2d
Reviewed-by: Oliver Wolff <oliver.wolff@theqtcompany.com>
---
 .../draganddrop/draggableicons/main.cpp       |  3 +-
 .../draggabletext/draggabletext.pro           |  6 +-
 .../draganddrop/draggabletext/draglabel.cpp   | 49 ----------------
 .../draganddrop/draggabletext/draglabel.h     | 58 -------------------
 .../draganddrop/draggabletext/dragwidget.cpp  | 45 +++++++-------
 .../widgets/draganddrop/dropsite/droparea.cpp |  6 +-
 .../draganddrop/dropsite/dropsitewindow.cpp   | 27 ++++-----
 .../draganddrop/fridgemagnets/dragwidget.cpp  | 19 +++---
 .../draganddrop/fridgemagnets/main.cpp        |  2 +-
 examples/widgets/draganddrop/puzzle/main.cpp  |  2 +-
 .../widgets/draganddrop/puzzle/mainwindow.cpp | 47 +++++++--------
 .../widgets/draganddrop/puzzle/mainwindow.h   |  3 +-
 .../widgets/draganddrop/puzzle/pieceslist.cpp | 10 ++--
 .../widgets/draganddrop/puzzle/pieceslist.h   |  2 +
 .../draganddrop/puzzle/puzzlewidget.cpp       | 26 +++------
 .../widgets/draganddrop/puzzle/puzzlewidget.h |  1 -
 16 files changed, 91 insertions(+), 215 deletions(-)
 delete mode 100644 examples/widgets/draganddrop/draggabletext/draglabel.cpp
 delete mode 100644 examples/widgets/draganddrop/draggabletext/draglabel.h

diff --git a/examples/widgets/draganddrop/draggableicons/main.cpp b/examples/widgets/draganddrop/draggableicons/main.cpp
index 165f71dbf76..44b4f848bfe 100644
--- a/examples/widgets/draganddrop/draggableicons/main.cpp
+++ b/examples/widgets/draganddrop/draggableicons/main.cpp
@@ -50,11 +50,10 @@ int main(int argc, char *argv[])
     QApplication app(argc, argv);
 
     QWidget mainWidget;
-    QHBoxLayout *horizontalLayout = new QHBoxLayout;
+    QHBoxLayout *horizontalLayout = new QHBoxLayout(&mainWidget);
     horizontalLayout->addWidget(new DragWidget);
     horizontalLayout->addWidget(new DragWidget);
 
-    mainWidget.setLayout(horizontalLayout);
     mainWidget.setWindowTitle(QObject::tr("Draggable Icons"));
     mainWidget.show();
 
diff --git a/examples/widgets/draganddrop/draggabletext/draggabletext.pro b/examples/widgets/draganddrop/draggabletext/draggabletext.pro
index 2815be16131..1add2a270eb 100644
--- a/examples/widgets/draganddrop/draggabletext/draggabletext.pro
+++ b/examples/widgets/draganddrop/draggabletext/draggabletext.pro
@@ -1,10 +1,8 @@
 QT += widgets
 
-HEADERS     = draglabel.h \
-              dragwidget.h
+HEADERS     = dragwidget.h
 RESOURCES   = draggabletext.qrc
-SOURCES     = draglabel.cpp \
-              dragwidget.cpp \
+SOURCES     = dragwidget.cpp \
               main.cpp
 
 # install
diff --git a/examples/widgets/draganddrop/draggabletext/draglabel.cpp b/examples/widgets/draganddrop/draggabletext/draglabel.cpp
deleted file mode 100644
index 4e741fb6c82..00000000000
--- a/examples/widgets/draganddrop/draggabletext/draglabel.cpp
+++ /dev/null
@@ -1,49 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2015 The Qt Company Ltd.
-** Contact: http://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** 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 "draglabel.h"
-
-DragLabel::DragLabel(const QString &text, QWidget *parent)
-    : QLabel(text, parent)
-{
-    setAutoFillBackground(true);
-    setFrameShape(QFrame::Panel);
-    setFrameShadow(QFrame::Raised);
-}
diff --git a/examples/widgets/draganddrop/draggabletext/draglabel.h b/examples/widgets/draganddrop/draggabletext/draglabel.h
deleted file mode 100644
index 8d2c31aa1d3..00000000000
--- a/examples/widgets/draganddrop/draggabletext/draglabel.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2015 The Qt Company Ltd.
-** Contact: http://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** 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 DRAGLABEL_H
-#define DRAGLABEL_H
-
-#include <QLabel>
-
-QT_BEGIN_NAMESPACE
-class QDragEnterEvent;
-class QDragMoveEvent;
-class QFrame;
-QT_END_NAMESPACE
-
-class DragLabel : public QLabel
-{
-public:
-    DragLabel(const QString &text, QWidget *parent);
-};
-
-#endif // DRAGLABEL_H
diff --git a/examples/widgets/draganddrop/draggabletext/dragwidget.cpp b/examples/widgets/draganddrop/draggabletext/dragwidget.cpp
index 3b8bca32776..36c4df2e438 100644
--- a/examples/widgets/draganddrop/draggabletext/dragwidget.cpp
+++ b/examples/widgets/draganddrop/draggabletext/dragwidget.cpp
@@ -40,13 +40,23 @@
 
 #include <QtWidgets>
 
-#include "draglabel.h"
 #include "dragwidget.h"
 
+static QLabel *createDragLabel(const QString &text, QWidget *parent)
+{
+    QLabel *label = new QLabel(text, parent);
+    label->setAutoFillBackground(true);
+    label->setFrameShape(QFrame::Panel);
+    label->setFrameShadow(QFrame::Raised);
+    return label;
+}
+
+static QString hotSpotMimeDataKey() { return QStringLiteral("application/x-hotspot"); }
+
 DragWidget::DragWidget(QWidget *parent)
     : QWidget(parent)
 {
-    QFile dictionaryFile(":/dictionary/words.txt");
+    QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt"));
     dictionaryFile.open(QIODevice::ReadOnly);
     QTextStream inputStream(&dictionaryFile);
 
@@ -57,7 +67,7 @@ DragWidget::DragWidget(QWidget *parent)
         QString word;
         inputStream >> word;
         if (!word.isEmpty()) {
-            DragLabel *wordLabel = new DragLabel(word, this);
+            QLabel *wordLabel = createDragLabel(word, this);
             wordLabel->move(x, y);
             wordLabel->show();
             wordLabel->setAttribute(Qt::WA_DeleteOnClose);
@@ -69,12 +79,6 @@ DragWidget::DragWidget(QWidget *parent)
         }
     }
 
-    /*
-    QPalette newPalette = palette();
-    newPalette.setColor(QPalette::Window, Qt::white);
-    setPalette(newPalette);
-    */
-
     setAcceptDrops(true);
     setMinimumSize(400, qMax(200, y));
     setWindowTitle(tr("Draggable Text"));
@@ -98,19 +102,19 @@ void DragWidget::dropEvent(QDropEvent *event)
 {
     if (event->mimeData()->hasText()) {
         const QMimeData *mime = event->mimeData();
-        QStringList pieces = mime->text().split(QRegExp("\\s+"),
+        QStringList pieces = mime->text().split(QRegularExpression(QStringLiteral("\\s+")),
                              QString::SkipEmptyParts);
         QPoint position = event->pos();
         QPoint hotSpot;
 
-        QList<QByteArray> hotSpotPos = mime->data("application/x-hotspot").split(' ');
+        QByteArrayList hotSpotPos = mime->data(hotSpotMimeDataKey()).split(' ');
         if (hotSpotPos.size() == 2) {
             hotSpot.setX(hotSpotPos.first().toInt());
             hotSpot.setY(hotSpotPos.last().toInt());
         }
 
-        foreach (QString piece, pieces) {
-            DragLabel *newLabel = new DragLabel(piece, this);
+        foreach (const QString &piece, pieces) {
+            QLabel *newLabel = createDragLabel(piece, this);
             newLabel->move(position - hotSpot);
             newLabel->show();
             newLabel->setAttribute(Qt::WA_DeleteOnClose);
@@ -127,18 +131,15 @@ void DragWidget::dropEvent(QDropEvent *event)
     } else {
         event->ignore();
     }
-    foreach (QObject *child, children()) {
-        if (child->inherits("QWidget")) {
-            QWidget *widget = static_cast<QWidget *>(child);
-            if (!widget->isVisible())
-                widget->deleteLater();
-        }
+    foreach (QWidget *widget, findChildren<QWidget *>()) {
+        if (!widget->isVisible())
+            widget->deleteLater();
     }
 }
 
 void DragWidget::mousePressEvent(QMouseEvent *event)
 {
-    QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
+    QLabel *child = qobject_cast<QLabel*>(childAt(event->pos()));
     if (!child)
         return;
 
@@ -146,8 +147,8 @@ void DragWidget::mousePressEvent(QMouseEvent *event)
 
     QMimeData *mimeData = new QMimeData;
     mimeData->setText(child->text());
-    mimeData->setData("application/x-hotspot",
-                      QByteArray::number(hotSpot.x()) + " " + QByteArray::number(hotSpot.y()));
+    mimeData->setData(hotSpotMimeDataKey(),
+                      QByteArray::number(hotSpot.x()) + ' ' + QByteArray::number(hotSpot.y()));
 
     QPixmap pixmap(child->size());
     child->render(&pixmap);
diff --git a/examples/widgets/draganddrop/dropsite/droparea.cpp b/examples/widgets/draganddrop/dropsite/droparea.cpp
index d3d2bb10ca1..0016434cc3e 100644
--- a/examples/widgets/draganddrop/dropsite/droparea.cpp
+++ b/examples/widgets/draganddrop/dropsite/droparea.cpp
@@ -92,10 +92,8 @@ void DropArea::dropEvent(QDropEvent *event)
     } else if (mimeData->hasUrls()) {
         QList<QUrl> urlList = mimeData->urls();
         QString text;
-        for (int i = 0; i < urlList.size() && i < 32; ++i) {
-            QString url = urlList.at(i).path();
-            text += url + QString("\n");
-        }
+        for (int i = 0; i < urlList.size() && i < 32; ++i)
+            text += urlList.at(i).path() + QLatin1Char('\n');
         setText(text);
     } else {
         setText(tr("Cannot display data"));
diff --git a/examples/widgets/draganddrop/dropsite/dropsitewindow.cpp b/examples/widgets/draganddrop/dropsite/dropsitewindow.cpp
index 5f0e689fc9c..5d48be1c34e 100644
--- a/examples/widgets/draganddrop/dropsite/dropsitewindow.cpp
+++ b/examples/widgets/draganddrop/dropsite/dropsitewindow.cpp
@@ -55,8 +55,8 @@ DropSiteWindow::DropSiteWindow()
 
 //! [constructor part2]
     dropArea = new DropArea;
-    connect(dropArea, SIGNAL(changed(const QMimeData*)),
-            this, SLOT(updateFormatsTable(const QMimeData*)));
+    connect(dropArea, &DropArea::changed,
+            this, &DropSiteWindow::updateFormatsTable);
 //! [constructor part2]
 
 //! [constructor part3]
@@ -78,17 +78,16 @@ DropSiteWindow::DropSiteWindow()
     buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole);
     buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
 
-    connect(quitButton, SIGNAL(pressed()), this, SLOT(close()));
-    connect(clearButton, SIGNAL(pressed()), dropArea, SLOT(clear()));
+    connect(quitButton, &QAbstractButton::pressed, this, &QWidget::close);
+    connect(clearButton, &QAbstractButton::pressed, dropArea, &DropArea::clear);
 //! [constructor part4]
 
 //! [constructor part5]
-    QVBoxLayout *mainLayout = new QVBoxLayout;
+    QVBoxLayout *mainLayout = new QVBoxLayout(this);
     mainLayout->addWidget(abstractLabel);
     mainLayout->addWidget(dropArea);
     mainLayout->addWidget(formatsTable);
     mainLayout->addWidget(buttonBox);
-    setLayout(mainLayout);
 
     setWindowTitle(tr("Drop Site"));
     setMinimumSize(350, 500);
@@ -112,22 +111,18 @@ void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData)
 
 //! [updateFormatsTable() part3]
         QString text;
-        if (format == "text/plain") {
+        if (format == QLatin1String("text/plain")) {
             text = mimeData->text().simplified();
-        } else if (format == "text/html") {
+        } else if (format == QLatin1String("text/html")) {
             text = mimeData->html().simplified();
-        } else if (format == "text/uri-list") {
+        } else if (format == QLatin1String("text/uri-list")) {
             QList<QUrl> urlList = mimeData->urls();
             for (int i = 0; i < urlList.size() && i < 32; ++i)
-                text.append(urlList[i].toString() + " ");
+                text.append(urlList.at(i).toString() + QLatin1Char(' '));
         } else {
             QByteArray data = mimeData->data(format);
-            for (int i = 0; i < data.size() && i < 32; ++i) {
-                QString hex = QString("%1").arg(uchar(data[i]), 2, 16,
-                                                QChar('0'))
-                                           .toUpper();
-                text.append(hex + " ");
-            }
+            for (int i = 0; i < data.size() && i < 32; ++i)
+                text.append(QStringLiteral("%1 ").arg(uchar(data[i]), 2, 16, QLatin1Char('0')).toUpper());
         }
 //! [updateFormatsTable() part3]
 
diff --git a/examples/widgets/draganddrop/fridgemagnets/dragwidget.cpp b/examples/widgets/draganddrop/fridgemagnets/dragwidget.cpp
index a591a29994b..3a9ab5fa763 100644
--- a/examples/widgets/draganddrop/fridgemagnets/dragwidget.cpp
+++ b/examples/widgets/draganddrop/fridgemagnets/dragwidget.cpp
@@ -43,11 +43,13 @@
 
 #include <QtWidgets>
 
+static inline QString fridgetMagnetsMimeType() { return QStringLiteral("application/x-fridgemagnet"); }
+
 //! [0]
 DragWidget::DragWidget(QWidget *parent)
     : QWidget(parent)
 {
-    QFile dictionaryFile(":/dictionary/words.txt");
+    QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt"));
     dictionaryFile.open(QFile::ReadOnly);
     QTextStream inputStream(&dictionaryFile);
 //! [0]
@@ -74,7 +76,6 @@ DragWidget::DragWidget(QWidget *parent)
 //! [1]
 
 //! [2]
-    //Fridge magnets is used for demoing Qt on S60 and themed backgrounds look better than white
     QPalette newPalette = palette();
     newPalette.setColor(QPalette::Window, Qt::white);
     setPalette(newPalette);
@@ -90,7 +91,7 @@ DragWidget::DragWidget(QWidget *parent)
 void DragWidget::dragEnterEvent(QDragEnterEvent *event)
 {
 //! [4] //! [5]
-    if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
+    if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
         if (children().contains(event->source())) {
             event->setDropAction(Qt::MoveAction);
             event->accept();
@@ -110,7 +111,7 @@ void DragWidget::dragEnterEvent(QDragEnterEvent *event)
 //! [8]
 void DragWidget::dragMoveEvent(QDragMoveEvent *event)
 {
-    if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
+    if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
         if (children().contains(event->source())) {
             event->setDropAction(Qt::MoveAction);
             event->accept();
@@ -128,10 +129,10 @@ void DragWidget::dragMoveEvent(QDragMoveEvent *event)
 //! [9]
 void DragWidget::dropEvent(QDropEvent *event)
 {
-    if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
+    if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
         const QMimeData *mime = event->mimeData();
 //! [9] //! [10]
-        QByteArray itemData = mime->data("application/x-fridgemagnet");
+        QByteArray itemData = mime->data(fridgetMagnetsMimeType());
         QDataStream dataStream(&itemData, QIODevice::ReadOnly);
 
         QString text;
@@ -152,11 +153,11 @@ void DragWidget::dropEvent(QDropEvent *event)
         }
 //! [11] //! [12]
     } else if (event->mimeData()->hasText()) {
-        QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"),
+        QStringList pieces = event->mimeData()->text().split(QRegularExpression(QStringLiteral("\\s+")),
                              QString::SkipEmptyParts);
         QPoint position = event->pos();
 
-        foreach (QString piece, pieces) {
+        foreach (const QString &piece, pieces) {
             DragLabel *newLabel = new DragLabel(piece, this);
             newLabel->move(position);
             newLabel->show();
@@ -190,7 +191,7 @@ void DragWidget::mousePressEvent(QMouseEvent *event)
 
 //! [15]
     QMimeData *mimeData = new QMimeData;
-    mimeData->setData("application/x-fridgemagnet", itemData);
+    mimeData->setData(fridgetMagnetsMimeType(), itemData);
     mimeData->setText(child->labelText());
 //! [15]
 
diff --git a/examples/widgets/draganddrop/fridgemagnets/main.cpp b/examples/widgets/draganddrop/fridgemagnets/main.cpp
index edff4486d60..a52ff8cabd7 100644
--- a/examples/widgets/draganddrop/fridgemagnets/main.cpp
+++ b/examples/widgets/draganddrop/fridgemagnets/main.cpp
@@ -52,7 +52,7 @@ int main(int argc, char *argv[])
 #endif
     DragWidget window;
 
-    bool smallScreen = QApplication::arguments().contains("-small-screen");
+    bool smallScreen = QApplication::arguments().contains(QStringLiteral("-small-screen"));
     if (smallScreen)
         window.showFullScreen();
     else
diff --git a/examples/widgets/draganddrop/puzzle/main.cpp b/examples/widgets/draganddrop/puzzle/main.cpp
index 706ebe4d70a..e8ecbe37db6 100644
--- a/examples/widgets/draganddrop/puzzle/main.cpp
+++ b/examples/widgets/draganddrop/puzzle/main.cpp
@@ -48,7 +48,7 @@ int main(int argc, char *argv[])
 
     QApplication app(argc, argv);
     MainWindow window;
-    window.openImage(":/images/example.jpg");
+    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
index 0fbdfc3f8d4..48e79946e70 100644
--- a/examples/widgets/draganddrop/puzzle/mainwindow.cpp
+++ b/examples/widgets/draganddrop/puzzle/mainwindow.cpp
@@ -55,26 +55,27 @@ MainWindow::MainWindow(QWidget *parent)
     setWindowTitle(tr("Puzzle"));
 }
 
-void MainWindow::openImage(const QString &path)
+void MainWindow::openImage()
 {
-    QString fileName = path;
+    const QString fileName =
+        QFileDialog::getOpenFileName(this, tr("Open Image"), QString(),
+                                     tr("Image Files (*.png *.jpg *.bmp)"));
 
-    if (fileName.isNull()) {
-        fileName = QFileDialog::getOpenFileName(this,
-            tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)");
-    }
+    if (!fileName.isEmpty())
+        loadImage(fileName);
+}
 
-    if (!fileName.isEmpty()) {
-        QPixmap newImage;
-        if (!newImage.load(fileName)) {
-            QMessageBox::warning(this, tr("Open Image"),
-                                  tr("The image file could not be loaded."),
-                                  QMessageBox::Cancel);
-            return;
-        }
-        puzzleImage = newImage;
-        setupPuzzle();
+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()
@@ -120,19 +121,15 @@ void MainWindow::setupMenus()
 {
     QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
 
-    QAction *openAction = fileMenu->addAction(tr("&Open..."));
+    QAction *openAction = fileMenu->addAction(tr("&Open..."), this, &MainWindow::openImage);
     openAction->setShortcuts(QKeySequence::Open);
 
-    QAction *exitAction = fileMenu->addAction(tr("E&xit"));
+    QAction *exitAction = fileMenu->addAction(tr("E&xit"), qApp, &QCoreApplication::quit);
     exitAction->setShortcuts(QKeySequence::Quit);
 
     QMenu *gameMenu = menuBar()->addMenu(tr("&Game"));
 
-    QAction *restartAction = gameMenu->addAction(tr("&Restart"));
-
-    connect(openAction, SIGNAL(triggered()), this, SLOT(openImage()));
-    connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
-    connect(restartAction, SIGNAL(triggered()), this, SLOT(setupPuzzle()));
+    gameMenu->addAction(tr("&Restart"), this, &MainWindow::setupPuzzle);
 }
 
 void MainWindow::setupWidgets()
@@ -144,8 +141,8 @@ void MainWindow::setupWidgets()
     piecesList = new PiecesList(puzzleWidget->pieceSize(), this);
 
 
-    connect(puzzleWidget, SIGNAL(puzzleCompleted()),
-            this, SLOT(setCompleted()), Qt::QueuedConnection);
+    connect(puzzleWidget, &PuzzleWidget::puzzleCompleted,
+            this, &MainWindow::setCompleted, Qt::QueuedConnection);
 
     frameLayout->addWidget(piecesList);
     frameLayout->addWidget(puzzleWidget);
diff --git a/examples/widgets/draganddrop/puzzle/mainwindow.h b/examples/widgets/draganddrop/puzzle/mainwindow.h
index 093a14472e2..b5b7d0d9035 100644
--- a/examples/widgets/draganddrop/puzzle/mainwindow.h
+++ b/examples/widgets/draganddrop/puzzle/mainwindow.h
@@ -56,9 +56,10 @@ class MainWindow : public QMainWindow
 
 public:
     MainWindow(QWidget *parent = 0);
+    void loadImage(const QString &path);
 
 public slots:
-    void openImage(const QString &path = QString());
+    void openImage();
     void setupPuzzle();
 
 private slots:
diff --git a/examples/widgets/draganddrop/puzzle/pieceslist.cpp b/examples/widgets/draganddrop/puzzle/pieceslist.cpp
index e60fd0a9fff..fb27aa23047 100644
--- a/examples/widgets/draganddrop/puzzle/pieceslist.cpp
+++ b/examples/widgets/draganddrop/puzzle/pieceslist.cpp
@@ -57,7 +57,7 @@ PiecesList::PiecesList(int pieceSize, QWidget *parent)
 
 void PiecesList::dragEnterEvent(QDragEnterEvent *event)
 {
-    if (event->mimeData()->hasFormat("image/x-puzzle-piece"))
+    if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType()))
         event->accept();
     else
         event->ignore();
@@ -65,7 +65,7 @@ void PiecesList::dragEnterEvent(QDragEnterEvent *event)
 
 void PiecesList::dragMoveEvent(QDragMoveEvent *event)
 {
-    if (event->mimeData()->hasFormat("image/x-puzzle-piece")) {
+    if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())) {
         event->setDropAction(Qt::MoveAction);
         event->accept();
     } else {
@@ -75,8 +75,8 @@ void PiecesList::dragMoveEvent(QDragMoveEvent *event)
 
 void PiecesList::dropEvent(QDropEvent *event)
 {
-    if (event->mimeData()->hasFormat("image/x-puzzle-piece")) {
-        QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece");
+    if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())) {
+        QByteArray pieceData = event->mimeData()->data(PiecesList::puzzleMimeType());
         QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
         QPixmap pixmap;
         QPoint location;
@@ -112,7 +112,7 @@ void PiecesList::startDrag(Qt::DropActions /*supportedActions*/)
     dataStream << pixmap << location;
 
     QMimeData *mimeData = new QMimeData;
-    mimeData->setData("image/x-puzzle-piece", itemData);
+    mimeData->setData(PiecesList::puzzleMimeType(), itemData);
 
     QDrag *drag = new QDrag(this);
     drag->setMimeData(mimeData);
diff --git a/examples/widgets/draganddrop/puzzle/pieceslist.h b/examples/widgets/draganddrop/puzzle/pieceslist.h
index 83c1ad82852..07fa504f41d 100644
--- a/examples/widgets/draganddrop/puzzle/pieceslist.h
+++ b/examples/widgets/draganddrop/puzzle/pieceslist.h
@@ -51,6 +51,8 @@ public:
     explicit PiecesList(int pieceSize, QWidget *parent = 0);
     void addPiece(QPixmap pixmap, QPoint location);
 
+    static QString puzzleMimeType() { return QStringLiteral("image/x-puzzle-piece"); }
+
 protected:
     void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
     void dragMoveEvent(QDragMoveEvent *event) Q_DECL_OVERRIDE;
diff --git a/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp b/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp
index 69c0cbf0cc3..29052da4fbd 100644
--- a/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp
+++ b/examples/widgets/draganddrop/puzzle/puzzlewidget.cpp
@@ -39,6 +39,7 @@
 ****************************************************************************/
 
 #include "puzzlewidget.h"
+#include "pieceslist.h"
 
 #include <QDrag>
 #include <QDragEnterEvent>
@@ -65,7 +66,7 @@ void PuzzleWidget::clear()
 
 void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event)
 {
-    if (event->mimeData()->hasFormat("image/x-puzzle-piece"))
+    if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType()))
         event->accept();
     else
         event->ignore();
@@ -83,8 +84,8 @@ void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event)
 {
     QRect updateRect = highlightedRect.united(targetSquare(event->pos()));
 
-    if (event->mimeData()->hasFormat("image/x-puzzle-piece")
-        && findPiece(targetSquare(event->pos())) == -1) {
+    if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())
+        && pieceRects.indexOf(targetSquare(event->pos())) == -1) {
 
         highlightedRect = targetSquare(event->pos());
         event->setDropAction(Qt::MoveAction);
@@ -99,10 +100,10 @@ void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event)
 
 void PuzzleWidget::dropEvent(QDropEvent *event)
 {
-    if (event->mimeData()->hasFormat("image/x-puzzle-piece")
-        && findPiece(targetSquare(event->pos())) == -1) {
+    if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())
+        && pieceRects.indexOf(targetSquare(event->pos())) == -1) {
 
-        QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece");
+        QByteArray pieceData = event->mimeData()->data(PiecesList::puzzleMimeType());
         QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
         QRect square = targetSquare(event->pos());
         QPixmap pixmap;
@@ -130,19 +131,10 @@ void PuzzleWidget::dropEvent(QDropEvent *event)
     }
 }
 
-int PuzzleWidget::findPiece(const QRect &pieceRect) const
-{
-    for (int i = 0; i < pieceRects.size(); ++i) {
-        if (pieceRect == pieceRects[i])
-            return i;
-    }
-    return -1;
-}
-
 void PuzzleWidget::mousePressEvent(QMouseEvent *event)
 {
     QRect square = targetSquare(event->pos());
-    int found = findPiece(square);
+    int found = pieceRects.indexOf(square);
 
     if (found == -1)
         return;
@@ -164,7 +156,7 @@ void PuzzleWidget::mousePressEvent(QMouseEvent *event)
     dataStream << pixmap << location;
 
     QMimeData *mimeData = new QMimeData;
-    mimeData->setData("image/x-puzzle-piece", itemData);
+    mimeData->setData(PiecesList::puzzleMimeType(), itemData);
 
     QDrag *drag = new QDrag(this);
     drag->setMimeData(mimeData);
diff --git a/examples/widgets/draganddrop/puzzle/puzzlewidget.h b/examples/widgets/draganddrop/puzzle/puzzlewidget.h
index 6bf264b5e0b..eb8a3a29f93 100644
--- a/examples/widgets/draganddrop/puzzle/puzzlewidget.h
+++ b/examples/widgets/draganddrop/puzzle/puzzlewidget.h
@@ -75,7 +75,6 @@ protected:
     void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
 
 private:
-    int findPiece(const QRect &pieceRect) const;
     const QRect targetSquare(const QPoint &position) const;
 
     QList<QPixmap> piecePixmaps;
-- 
GitLab