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>
This commit is contained in:
parent
c55a36cb90
commit
c8cd9f1b9a
@ -50,11 +50,10 @@ int main(int argc, char *argv[])
|
|||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
QWidget mainWidget;
|
QWidget mainWidget;
|
||||||
QHBoxLayout *horizontalLayout = new QHBoxLayout;
|
QHBoxLayout *horizontalLayout = new QHBoxLayout(&mainWidget);
|
||||||
horizontalLayout->addWidget(new DragWidget);
|
horizontalLayout->addWidget(new DragWidget);
|
||||||
horizontalLayout->addWidget(new DragWidget);
|
horizontalLayout->addWidget(new DragWidget);
|
||||||
|
|
||||||
mainWidget.setLayout(horizontalLayout);
|
|
||||||
mainWidget.setWindowTitle(QObject::tr("Draggable Icons"));
|
mainWidget.setWindowTitle(QObject::tr("Draggable Icons"));
|
||||||
mainWidget.show();
|
mainWidget.show();
|
||||||
|
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
QT += widgets
|
QT += widgets
|
||||||
|
|
||||||
HEADERS = draglabel.h \
|
HEADERS = dragwidget.h
|
||||||
dragwidget.h
|
|
||||||
RESOURCES = draggabletext.qrc
|
RESOURCES = draggabletext.qrc
|
||||||
SOURCES = draglabel.cpp \
|
SOURCES = dragwidget.cpp \
|
||||||
dragwidget.cpp \
|
|
||||||
main.cpp
|
main.cpp
|
||||||
|
|
||||||
# install
|
# install
|
||||||
|
@ -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);
|
|
||||||
}
|
|
@ -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
|
|
@ -40,13 +40,23 @@
|
|||||||
|
|
||||||
#include <QtWidgets>
|
#include <QtWidgets>
|
||||||
|
|
||||||
#include "draglabel.h"
|
|
||||||
#include "dragwidget.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)
|
DragWidget::DragWidget(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
{
|
{
|
||||||
QFile dictionaryFile(":/dictionary/words.txt");
|
QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt"));
|
||||||
dictionaryFile.open(QIODevice::ReadOnly);
|
dictionaryFile.open(QIODevice::ReadOnly);
|
||||||
QTextStream inputStream(&dictionaryFile);
|
QTextStream inputStream(&dictionaryFile);
|
||||||
|
|
||||||
@ -57,7 +67,7 @@ DragWidget::DragWidget(QWidget *parent)
|
|||||||
QString word;
|
QString word;
|
||||||
inputStream >> word;
|
inputStream >> word;
|
||||||
if (!word.isEmpty()) {
|
if (!word.isEmpty()) {
|
||||||
DragLabel *wordLabel = new DragLabel(word, this);
|
QLabel *wordLabel = createDragLabel(word, this);
|
||||||
wordLabel->move(x, y);
|
wordLabel->move(x, y);
|
||||||
wordLabel->show();
|
wordLabel->show();
|
||||||
wordLabel->setAttribute(Qt::WA_DeleteOnClose);
|
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);
|
setAcceptDrops(true);
|
||||||
setMinimumSize(400, qMax(200, y));
|
setMinimumSize(400, qMax(200, y));
|
||||||
setWindowTitle(tr("Draggable Text"));
|
setWindowTitle(tr("Draggable Text"));
|
||||||
@ -98,19 +102,19 @@ void DragWidget::dropEvent(QDropEvent *event)
|
|||||||
{
|
{
|
||||||
if (event->mimeData()->hasText()) {
|
if (event->mimeData()->hasText()) {
|
||||||
const QMimeData *mime = event->mimeData();
|
const QMimeData *mime = event->mimeData();
|
||||||
QStringList pieces = mime->text().split(QRegExp("\\s+"),
|
QStringList pieces = mime->text().split(QRegularExpression(QStringLiteral("\\s+")),
|
||||||
QString::SkipEmptyParts);
|
QString::SkipEmptyParts);
|
||||||
QPoint position = event->pos();
|
QPoint position = event->pos();
|
||||||
QPoint hotSpot;
|
QPoint hotSpot;
|
||||||
|
|
||||||
QList<QByteArray> hotSpotPos = mime->data("application/x-hotspot").split(' ');
|
QByteArrayList hotSpotPos = mime->data(hotSpotMimeDataKey()).split(' ');
|
||||||
if (hotSpotPos.size() == 2) {
|
if (hotSpotPos.size() == 2) {
|
||||||
hotSpot.setX(hotSpotPos.first().toInt());
|
hotSpot.setX(hotSpotPos.first().toInt());
|
||||||
hotSpot.setY(hotSpotPos.last().toInt());
|
hotSpot.setY(hotSpotPos.last().toInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (QString piece, pieces) {
|
foreach (const QString &piece, pieces) {
|
||||||
DragLabel *newLabel = new DragLabel(piece, this);
|
QLabel *newLabel = createDragLabel(piece, this);
|
||||||
newLabel->move(position - hotSpot);
|
newLabel->move(position - hotSpot);
|
||||||
newLabel->show();
|
newLabel->show();
|
||||||
newLabel->setAttribute(Qt::WA_DeleteOnClose);
|
newLabel->setAttribute(Qt::WA_DeleteOnClose);
|
||||||
@ -127,18 +131,15 @@ void DragWidget::dropEvent(QDropEvent *event)
|
|||||||
} else {
|
} else {
|
||||||
event->ignore();
|
event->ignore();
|
||||||
}
|
}
|
||||||
foreach (QObject *child, children()) {
|
foreach (QWidget *widget, findChildren<QWidget *>()) {
|
||||||
if (child->inherits("QWidget")) {
|
|
||||||
QWidget *widget = static_cast<QWidget *>(child);
|
|
||||||
if (!widget->isVisible())
|
if (!widget->isVisible())
|
||||||
widget->deleteLater();
|
widget->deleteLater();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void DragWidget::mousePressEvent(QMouseEvent *event)
|
void DragWidget::mousePressEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
|
QLabel *child = qobject_cast<QLabel*>(childAt(event->pos()));
|
||||||
if (!child)
|
if (!child)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -146,8 +147,8 @@ void DragWidget::mousePressEvent(QMouseEvent *event)
|
|||||||
|
|
||||||
QMimeData *mimeData = new QMimeData;
|
QMimeData *mimeData = new QMimeData;
|
||||||
mimeData->setText(child->text());
|
mimeData->setText(child->text());
|
||||||
mimeData->setData("application/x-hotspot",
|
mimeData->setData(hotSpotMimeDataKey(),
|
||||||
QByteArray::number(hotSpot.x()) + " " + QByteArray::number(hotSpot.y()));
|
QByteArray::number(hotSpot.x()) + ' ' + QByteArray::number(hotSpot.y()));
|
||||||
|
|
||||||
QPixmap pixmap(child->size());
|
QPixmap pixmap(child->size());
|
||||||
child->render(&pixmap);
|
child->render(&pixmap);
|
||||||
|
@ -92,10 +92,8 @@ void DropArea::dropEvent(QDropEvent *event)
|
|||||||
} else if (mimeData->hasUrls()) {
|
} else if (mimeData->hasUrls()) {
|
||||||
QList<QUrl> urlList = mimeData->urls();
|
QList<QUrl> urlList = mimeData->urls();
|
||||||
QString text;
|
QString text;
|
||||||
for (int i = 0; i < urlList.size() && i < 32; ++i) {
|
for (int i = 0; i < urlList.size() && i < 32; ++i)
|
||||||
QString url = urlList.at(i).path();
|
text += urlList.at(i).path() + QLatin1Char('\n');
|
||||||
text += url + QString("\n");
|
|
||||||
}
|
|
||||||
setText(text);
|
setText(text);
|
||||||
} else {
|
} else {
|
||||||
setText(tr("Cannot display data"));
|
setText(tr("Cannot display data"));
|
||||||
|
@ -55,8 +55,8 @@ DropSiteWindow::DropSiteWindow()
|
|||||||
|
|
||||||
//! [constructor part2]
|
//! [constructor part2]
|
||||||
dropArea = new DropArea;
|
dropArea = new DropArea;
|
||||||
connect(dropArea, SIGNAL(changed(const QMimeData*)),
|
connect(dropArea, &DropArea::changed,
|
||||||
this, SLOT(updateFormatsTable(const QMimeData*)));
|
this, &DropSiteWindow::updateFormatsTable);
|
||||||
//! [constructor part2]
|
//! [constructor part2]
|
||||||
|
|
||||||
//! [constructor part3]
|
//! [constructor part3]
|
||||||
@ -78,17 +78,16 @@ DropSiteWindow::DropSiteWindow()
|
|||||||
buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole);
|
buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole);
|
||||||
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
||||||
|
|
||||||
connect(quitButton, SIGNAL(pressed()), this, SLOT(close()));
|
connect(quitButton, &QAbstractButton::pressed, this, &QWidget::close);
|
||||||
connect(clearButton, SIGNAL(pressed()), dropArea, SLOT(clear()));
|
connect(clearButton, &QAbstractButton::pressed, dropArea, &DropArea::clear);
|
||||||
//! [constructor part4]
|
//! [constructor part4]
|
||||||
|
|
||||||
//! [constructor part5]
|
//! [constructor part5]
|
||||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
||||||
mainLayout->addWidget(abstractLabel);
|
mainLayout->addWidget(abstractLabel);
|
||||||
mainLayout->addWidget(dropArea);
|
mainLayout->addWidget(dropArea);
|
||||||
mainLayout->addWidget(formatsTable);
|
mainLayout->addWidget(formatsTable);
|
||||||
mainLayout->addWidget(buttonBox);
|
mainLayout->addWidget(buttonBox);
|
||||||
setLayout(mainLayout);
|
|
||||||
|
|
||||||
setWindowTitle(tr("Drop Site"));
|
setWindowTitle(tr("Drop Site"));
|
||||||
setMinimumSize(350, 500);
|
setMinimumSize(350, 500);
|
||||||
@ -112,22 +111,18 @@ void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData)
|
|||||||
|
|
||||||
//! [updateFormatsTable() part3]
|
//! [updateFormatsTable() part3]
|
||||||
QString text;
|
QString text;
|
||||||
if (format == "text/plain") {
|
if (format == QLatin1String("text/plain")) {
|
||||||
text = mimeData->text().simplified();
|
text = mimeData->text().simplified();
|
||||||
} else if (format == "text/html") {
|
} else if (format == QLatin1String("text/html")) {
|
||||||
text = mimeData->html().simplified();
|
text = mimeData->html().simplified();
|
||||||
} else if (format == "text/uri-list") {
|
} else if (format == QLatin1String("text/uri-list")) {
|
||||||
QList<QUrl> urlList = mimeData->urls();
|
QList<QUrl> urlList = mimeData->urls();
|
||||||
for (int i = 0; i < urlList.size() && i < 32; ++i)
|
for (int i = 0; i < urlList.size() && i < 32; ++i)
|
||||||
text.append(urlList[i].toString() + " ");
|
text.append(urlList.at(i).toString() + QLatin1Char(' '));
|
||||||
} else {
|
} else {
|
||||||
QByteArray data = mimeData->data(format);
|
QByteArray data = mimeData->data(format);
|
||||||
for (int i = 0; i < data.size() && i < 32; ++i) {
|
for (int i = 0; i < data.size() && i < 32; ++i)
|
||||||
QString hex = QString("%1").arg(uchar(data[i]), 2, 16,
|
text.append(QStringLiteral("%1 ").arg(uchar(data[i]), 2, 16, QLatin1Char('0')).toUpper());
|
||||||
QChar('0'))
|
|
||||||
.toUpper();
|
|
||||||
text.append(hex + " ");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//! [updateFormatsTable() part3]
|
//! [updateFormatsTable() part3]
|
||||||
|
|
||||||
|
@ -43,11 +43,13 @@
|
|||||||
|
|
||||||
#include <QtWidgets>
|
#include <QtWidgets>
|
||||||
|
|
||||||
|
static inline QString fridgetMagnetsMimeType() { return QStringLiteral("application/x-fridgemagnet"); }
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
DragWidget::DragWidget(QWidget *parent)
|
DragWidget::DragWidget(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
{
|
{
|
||||||
QFile dictionaryFile(":/dictionary/words.txt");
|
QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt"));
|
||||||
dictionaryFile.open(QFile::ReadOnly);
|
dictionaryFile.open(QFile::ReadOnly);
|
||||||
QTextStream inputStream(&dictionaryFile);
|
QTextStream inputStream(&dictionaryFile);
|
||||||
//! [0]
|
//! [0]
|
||||||
@ -74,7 +76,6 @@ DragWidget::DragWidget(QWidget *parent)
|
|||||||
//! [1]
|
//! [1]
|
||||||
|
|
||||||
//! [2]
|
//! [2]
|
||||||
//Fridge magnets is used for demoing Qt on S60 and themed backgrounds look better than white
|
|
||||||
QPalette newPalette = palette();
|
QPalette newPalette = palette();
|
||||||
newPalette.setColor(QPalette::Window, Qt::white);
|
newPalette.setColor(QPalette::Window, Qt::white);
|
||||||
setPalette(newPalette);
|
setPalette(newPalette);
|
||||||
@ -90,7 +91,7 @@ DragWidget::DragWidget(QWidget *parent)
|
|||||||
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
|
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
|
||||||
{
|
{
|
||||||
//! [4] //! [5]
|
//! [4] //! [5]
|
||||||
if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
|
if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
|
||||||
if (children().contains(event->source())) {
|
if (children().contains(event->source())) {
|
||||||
event->setDropAction(Qt::MoveAction);
|
event->setDropAction(Qt::MoveAction);
|
||||||
event->accept();
|
event->accept();
|
||||||
@ -110,7 +111,7 @@ void DragWidget::dragEnterEvent(QDragEnterEvent *event)
|
|||||||
//! [8]
|
//! [8]
|
||||||
void DragWidget::dragMoveEvent(QDragMoveEvent *event)
|
void DragWidget::dragMoveEvent(QDragMoveEvent *event)
|
||||||
{
|
{
|
||||||
if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
|
if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
|
||||||
if (children().contains(event->source())) {
|
if (children().contains(event->source())) {
|
||||||
event->setDropAction(Qt::MoveAction);
|
event->setDropAction(Qt::MoveAction);
|
||||||
event->accept();
|
event->accept();
|
||||||
@ -128,10 +129,10 @@ void DragWidget::dragMoveEvent(QDragMoveEvent *event)
|
|||||||
//! [9]
|
//! [9]
|
||||||
void DragWidget::dropEvent(QDropEvent *event)
|
void DragWidget::dropEvent(QDropEvent *event)
|
||||||
{
|
{
|
||||||
if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
|
if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
|
||||||
const QMimeData *mime = event->mimeData();
|
const QMimeData *mime = event->mimeData();
|
||||||
//! [9] //! [10]
|
//! [9] //! [10]
|
||||||
QByteArray itemData = mime->data("application/x-fridgemagnet");
|
QByteArray itemData = mime->data(fridgetMagnetsMimeType());
|
||||||
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
|
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
|
||||||
|
|
||||||
QString text;
|
QString text;
|
||||||
@ -152,11 +153,11 @@ void DragWidget::dropEvent(QDropEvent *event)
|
|||||||
}
|
}
|
||||||
//! [11] //! [12]
|
//! [11] //! [12]
|
||||||
} else if (event->mimeData()->hasText()) {
|
} else if (event->mimeData()->hasText()) {
|
||||||
QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"),
|
QStringList pieces = event->mimeData()->text().split(QRegularExpression(QStringLiteral("\\s+")),
|
||||||
QString::SkipEmptyParts);
|
QString::SkipEmptyParts);
|
||||||
QPoint position = event->pos();
|
QPoint position = event->pos();
|
||||||
|
|
||||||
foreach (QString piece, pieces) {
|
foreach (const QString &piece, pieces) {
|
||||||
DragLabel *newLabel = new DragLabel(piece, this);
|
DragLabel *newLabel = new DragLabel(piece, this);
|
||||||
newLabel->move(position);
|
newLabel->move(position);
|
||||||
newLabel->show();
|
newLabel->show();
|
||||||
@ -190,7 +191,7 @@ void DragWidget::mousePressEvent(QMouseEvent *event)
|
|||||||
|
|
||||||
//! [15]
|
//! [15]
|
||||||
QMimeData *mimeData = new QMimeData;
|
QMimeData *mimeData = new QMimeData;
|
||||||
mimeData->setData("application/x-fridgemagnet", itemData);
|
mimeData->setData(fridgetMagnetsMimeType(), itemData);
|
||||||
mimeData->setText(child->labelText());
|
mimeData->setText(child->labelText());
|
||||||
//! [15]
|
//! [15]
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ int main(int argc, char *argv[])
|
|||||||
#endif
|
#endif
|
||||||
DragWidget window;
|
DragWidget window;
|
||||||
|
|
||||||
bool smallScreen = QApplication::arguments().contains("-small-screen");
|
bool smallScreen = QApplication::arguments().contains(QStringLiteral("-small-screen"));
|
||||||
if (smallScreen)
|
if (smallScreen)
|
||||||
window.showFullScreen();
|
window.showFullScreen();
|
||||||
else
|
else
|
||||||
|
@ -48,7 +48,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
MainWindow window;
|
MainWindow window;
|
||||||
window.openImage(":/images/example.jpg");
|
window.loadImage(QStringLiteral(":/images/example.jpg"));
|
||||||
window.show();
|
window.show();
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
@ -55,27 +55,28 @@ MainWindow::MainWindow(QWidget *parent)
|
|||||||
setWindowTitle(tr("Puzzle"));
|
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()) {
|
if (!fileName.isEmpty())
|
||||||
fileName = QFileDialog::getOpenFileName(this,
|
loadImage(fileName);
|
||||||
tr("Open Image"), "", "Image Files (*.png *.jpg *.bmp)");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fileName.isEmpty()) {
|
void MainWindow::loadImage(const QString &fileName)
|
||||||
|
{
|
||||||
QPixmap newImage;
|
QPixmap newImage;
|
||||||
if (!newImage.load(fileName)) {
|
if (!newImage.load(fileName)) {
|
||||||
QMessageBox::warning(this, tr("Open Image"),
|
QMessageBox::warning(this, tr("Open Image"),
|
||||||
tr("The image file could not be loaded."),
|
tr("The image file could not be loaded."),
|
||||||
QMessageBox::Cancel);
|
QMessageBox::Close);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
puzzleImage = newImage;
|
puzzleImage = newImage;
|
||||||
setupPuzzle();
|
setupPuzzle();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::setCompleted()
|
void MainWindow::setCompleted()
|
||||||
{
|
{
|
||||||
@ -120,19 +121,15 @@ void MainWindow::setupMenus()
|
|||||||
{
|
{
|
||||||
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
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);
|
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);
|
exitAction->setShortcuts(QKeySequence::Quit);
|
||||||
|
|
||||||
QMenu *gameMenu = menuBar()->addMenu(tr("&Game"));
|
QMenu *gameMenu = menuBar()->addMenu(tr("&Game"));
|
||||||
|
|
||||||
QAction *restartAction = gameMenu->addAction(tr("&Restart"));
|
gameMenu->addAction(tr("&Restart"), this, &MainWindow::setupPuzzle);
|
||||||
|
|
||||||
connect(openAction, SIGNAL(triggered()), this, SLOT(openImage()));
|
|
||||||
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
|
||||||
connect(restartAction, SIGNAL(triggered()), this, SLOT(setupPuzzle()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::setupWidgets()
|
void MainWindow::setupWidgets()
|
||||||
@ -144,8 +141,8 @@ void MainWindow::setupWidgets()
|
|||||||
piecesList = new PiecesList(puzzleWidget->pieceSize(), this);
|
piecesList = new PiecesList(puzzleWidget->pieceSize(), this);
|
||||||
|
|
||||||
|
|
||||||
connect(puzzleWidget, SIGNAL(puzzleCompleted()),
|
connect(puzzleWidget, &PuzzleWidget::puzzleCompleted,
|
||||||
this, SLOT(setCompleted()), Qt::QueuedConnection);
|
this, &MainWindow::setCompleted, Qt::QueuedConnection);
|
||||||
|
|
||||||
frameLayout->addWidget(piecesList);
|
frameLayout->addWidget(piecesList);
|
||||||
frameLayout->addWidget(puzzleWidget);
|
frameLayout->addWidget(puzzleWidget);
|
||||||
|
@ -56,9 +56,10 @@ class MainWindow : public QMainWindow
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
MainWindow(QWidget *parent = 0);
|
MainWindow(QWidget *parent = 0);
|
||||||
|
void loadImage(const QString &path);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void openImage(const QString &path = QString());
|
void openImage();
|
||||||
void setupPuzzle();
|
void setupPuzzle();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
@ -57,7 +57,7 @@ PiecesList::PiecesList(int pieceSize, QWidget *parent)
|
|||||||
|
|
||||||
void PiecesList::dragEnterEvent(QDragEnterEvent *event)
|
void PiecesList::dragEnterEvent(QDragEnterEvent *event)
|
||||||
{
|
{
|
||||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece"))
|
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType()))
|
||||||
event->accept();
|
event->accept();
|
||||||
else
|
else
|
||||||
event->ignore();
|
event->ignore();
|
||||||
@ -65,7 +65,7 @@ void PiecesList::dragEnterEvent(QDragEnterEvent *event)
|
|||||||
|
|
||||||
void PiecesList::dragMoveEvent(QDragMoveEvent *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->setDropAction(Qt::MoveAction);
|
||||||
event->accept();
|
event->accept();
|
||||||
} else {
|
} else {
|
||||||
@ -75,8 +75,8 @@ void PiecesList::dragMoveEvent(QDragMoveEvent *event)
|
|||||||
|
|
||||||
void PiecesList::dropEvent(QDropEvent *event)
|
void PiecesList::dropEvent(QDropEvent *event)
|
||||||
{
|
{
|
||||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece")) {
|
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())) {
|
||||||
QByteArray pieceData = event->mimeData()->data("image/x-puzzle-piece");
|
QByteArray pieceData = event->mimeData()->data(PiecesList::puzzleMimeType());
|
||||||
QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
|
QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
|
||||||
QPixmap pixmap;
|
QPixmap pixmap;
|
||||||
QPoint location;
|
QPoint location;
|
||||||
@ -112,7 +112,7 @@ void PiecesList::startDrag(Qt::DropActions /*supportedActions*/)
|
|||||||
dataStream << pixmap << location;
|
dataStream << pixmap << location;
|
||||||
|
|
||||||
QMimeData *mimeData = new QMimeData;
|
QMimeData *mimeData = new QMimeData;
|
||||||
mimeData->setData("image/x-puzzle-piece", itemData);
|
mimeData->setData(PiecesList::puzzleMimeType(), itemData);
|
||||||
|
|
||||||
QDrag *drag = new QDrag(this);
|
QDrag *drag = new QDrag(this);
|
||||||
drag->setMimeData(mimeData);
|
drag->setMimeData(mimeData);
|
||||||
|
@ -51,6 +51,8 @@ public:
|
|||||||
explicit PiecesList(int pieceSize, QWidget *parent = 0);
|
explicit PiecesList(int pieceSize, QWidget *parent = 0);
|
||||||
void addPiece(QPixmap pixmap, QPoint location);
|
void addPiece(QPixmap pixmap, QPoint location);
|
||||||
|
|
||||||
|
static QString puzzleMimeType() { return QStringLiteral("image/x-puzzle-piece"); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
|
void dragEnterEvent(QDragEnterEvent *event) Q_DECL_OVERRIDE;
|
||||||
void dragMoveEvent(QDragMoveEvent *event) Q_DECL_OVERRIDE;
|
void dragMoveEvent(QDragMoveEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
@ -39,6 +39,7 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include "puzzlewidget.h"
|
#include "puzzlewidget.h"
|
||||||
|
#include "pieceslist.h"
|
||||||
|
|
||||||
#include <QDrag>
|
#include <QDrag>
|
||||||
#include <QDragEnterEvent>
|
#include <QDragEnterEvent>
|
||||||
@ -65,7 +66,7 @@ void PuzzleWidget::clear()
|
|||||||
|
|
||||||
void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event)
|
void PuzzleWidget::dragEnterEvent(QDragEnterEvent *event)
|
||||||
{
|
{
|
||||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece"))
|
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType()))
|
||||||
event->accept();
|
event->accept();
|
||||||
else
|
else
|
||||||
event->ignore();
|
event->ignore();
|
||||||
@ -83,8 +84,8 @@ void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event)
|
|||||||
{
|
{
|
||||||
QRect updateRect = highlightedRect.united(targetSquare(event->pos()));
|
QRect updateRect = highlightedRect.united(targetSquare(event->pos()));
|
||||||
|
|
||||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece")
|
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())
|
||||||
&& findPiece(targetSquare(event->pos())) == -1) {
|
&& pieceRects.indexOf(targetSquare(event->pos())) == -1) {
|
||||||
|
|
||||||
highlightedRect = targetSquare(event->pos());
|
highlightedRect = targetSquare(event->pos());
|
||||||
event->setDropAction(Qt::MoveAction);
|
event->setDropAction(Qt::MoveAction);
|
||||||
@ -99,10 +100,10 @@ void PuzzleWidget::dragMoveEvent(QDragMoveEvent *event)
|
|||||||
|
|
||||||
void PuzzleWidget::dropEvent(QDropEvent *event)
|
void PuzzleWidget::dropEvent(QDropEvent *event)
|
||||||
{
|
{
|
||||||
if (event->mimeData()->hasFormat("image/x-puzzle-piece")
|
if (event->mimeData()->hasFormat(PiecesList::puzzleMimeType())
|
||||||
&& findPiece(targetSquare(event->pos())) == -1) {
|
&& 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);
|
QDataStream dataStream(&pieceData, QIODevice::ReadOnly);
|
||||||
QRect square = targetSquare(event->pos());
|
QRect square = targetSquare(event->pos());
|
||||||
QPixmap pixmap;
|
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)
|
void PuzzleWidget::mousePressEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
QRect square = targetSquare(event->pos());
|
QRect square = targetSquare(event->pos());
|
||||||
int found = findPiece(square);
|
int found = pieceRects.indexOf(square);
|
||||||
|
|
||||||
if (found == -1)
|
if (found == -1)
|
||||||
return;
|
return;
|
||||||
@ -164,7 +156,7 @@ void PuzzleWidget::mousePressEvent(QMouseEvent *event)
|
|||||||
dataStream << pixmap << location;
|
dataStream << pixmap << location;
|
||||||
|
|
||||||
QMimeData *mimeData = new QMimeData;
|
QMimeData *mimeData = new QMimeData;
|
||||||
mimeData->setData("image/x-puzzle-piece", itemData);
|
mimeData->setData(PiecesList::puzzleMimeType(), itemData);
|
||||||
|
|
||||||
QDrag *drag = new QDrag(this);
|
QDrag *drag = new QDrag(this);
|
||||||
drag->setMimeData(mimeData);
|
drag->setMimeData(mimeData);
|
||||||
|
@ -75,7 +75,6 @@ protected:
|
|||||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int findPiece(const QRect &pieceRect) const;
|
|
||||||
const QRect targetSquare(const QPoint &position) const;
|
const QRect targetSquare(const QPoint &position) const;
|
||||||
|
|
||||||
QList<QPixmap> piecePixmaps;
|
QList<QPixmap> piecePixmaps;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user