diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2009-03-23 09:18:55 (GMT) |
---|---|---|
committer | Simon Hausmann <simon.hausmann@nokia.com> | 2009-03-23 09:18:55 (GMT) |
commit | e5fcad302d86d316390c6b0f62759a067313e8a9 (patch) | |
tree | c2afbf6f1066b6ce261f14341cf6d310e5595bc1 /examples/draganddrop/draggabletext | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'examples/draganddrop/draggabletext')
-rw-r--r-- | examples/draganddrop/draggabletext/draggabletext.pro | 12 | ||||
-rw-r--r-- | examples/draganddrop/draggabletext/draggabletext.qrc | 5 | ||||
-rw-r--r-- | examples/draganddrop/draggabletext/draglabel.cpp | 52 | ||||
-rw-r--r-- | examples/draganddrop/draggabletext/draglabel.h | 59 | ||||
-rw-r--r-- | examples/draganddrop/draggabletext/dragwidget.cpp | 164 | ||||
-rw-r--r-- | examples/draganddrop/draggabletext/dragwidget.h | 63 | ||||
-rw-r--r-- | examples/draganddrop/draggabletext/main.cpp | 53 | ||||
-rw-r--r-- | examples/draganddrop/draggabletext/words.txt | 41 |
8 files changed, 449 insertions, 0 deletions
diff --git a/examples/draganddrop/draggabletext/draggabletext.pro b/examples/draganddrop/draggabletext/draggabletext.pro new file mode 100644 index 0000000..07c7c24 --- /dev/null +++ b/examples/draganddrop/draggabletext/draggabletext.pro @@ -0,0 +1,12 @@ +HEADERS = draglabel.h \ + dragwidget.h +RESOURCES = draggabletext.qrc +SOURCES = draglabel.cpp \ + dragwidget.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/draggabletext +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.txt *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/draggabletext +INSTALLS += target sources diff --git a/examples/draganddrop/draggabletext/draggabletext.qrc b/examples/draganddrop/draggabletext/draggabletext.qrc new file mode 100644 index 0000000..b72217d --- /dev/null +++ b/examples/draganddrop/draggabletext/draggabletext.qrc @@ -0,0 +1,5 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/dictionary"> + <file>words.txt</file> +</qresource> +</RCC> diff --git a/examples/draganddrop/draggabletext/draglabel.cpp b/examples/draganddrop/draggabletext/draglabel.cpp new file mode 100644 index 0000000..4dfa099 --- /dev/null +++ b/examples/draganddrop/draggabletext/draglabel.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#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/draganddrop/draggabletext/draglabel.h b/examples/draganddrop/draggabletext/draglabel.h new file mode 100644 index 0000000..8ba2778 --- /dev/null +++ b/examples/draganddrop/draggabletext/draglabel.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $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 diff --git a/examples/draganddrop/draggabletext/dragwidget.cpp b/examples/draganddrop/draggabletext/dragwidget.cpp new file mode 100644 index 0000000..2af7b29 --- /dev/null +++ b/examples/draganddrop/draggabletext/dragwidget.cpp @@ -0,0 +1,164 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "draglabel.h" +#include "dragwidget.h" + +DragWidget::DragWidget(QWidget *parent) + : QWidget(parent) +{ + QFile dictionaryFile(":/dictionary/words.txt"); + dictionaryFile.open(QIODevice::ReadOnly); + QTextStream inputStream(&dictionaryFile); + + 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 >= 195) { + x = 5; + y += wordLabel->height() + 2; + } + } + } + + QPalette newPalette = palette(); + newPalette.setColor(QPalette::Window, Qt::white); + setPalette(newPalette); + + setAcceptDrops(true); + setMinimumSize(400, qMax(200, y)); + setWindowTitle(tr("Draggable Text")); +} + +void DragWidget::dragEnterEvent(QDragEnterEvent *event) +{ + if (event->mimeData()->hasText()) { + if (children().contains(event->source())) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); + } + } else { + event->ignore(); + } +} + +void DragWidget::dropEvent(QDropEvent *event) +{ + if (event->mimeData()->hasText()) { + const QMimeData *mime = event->mimeData(); + QStringList pieces = mime->text().split(QRegExp("\\s+"), + QString::SkipEmptyParts); + QPoint position = event->pos(); + QPoint hotSpot; + + QList<QByteArray> hotSpotPos = mime->data("application/x-hotspot").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); + newLabel->move(position - hotSpot); + newLabel->show(); + newLabel->setAttribute(Qt::WA_DeleteOnClose); + + position += QPoint(newLabel->width(), 0); + } + + if (event->source() == this) { + event->setDropAction(Qt::MoveAction); + event->accept(); + } else { + event->acceptProposedAction(); + } + } else { + event->ignore(); + } + foreach (QObject *child, children()) { + if (child->inherits("QWidget")) { + QWidget *widget = static_cast<QWidget *>(child); + if (!widget->isVisible()) + widget->deleteLater(); + } + } +} + +void DragWidget::mousePressEvent(QMouseEvent *event) +{ + QLabel *child = static_cast<QLabel*>(childAt(event->pos())); + if (!child) + return; + + QPoint hotSpot = event->pos() - child->pos(); + + QMimeData *mimeData = new QMimeData; + mimeData->setText(child->text()); + mimeData->setData("application/x-hotspot", + QByteArray::number(hotSpot.x()) + + " " + QByteArray::number(hotSpot.y())); + + QPixmap pixmap(child->size()); + child->render(&pixmap); + + QDrag *drag = new QDrag(this); + drag->setMimeData(mimeData); + drag->setPixmap(pixmap); + drag->setHotSpot(hotSpot); + + Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction); + + if (dropAction == Qt::MoveAction) + child->close(); +} diff --git a/examples/draganddrop/draggabletext/dragwidget.h b/examples/draganddrop/draggabletext/dragwidget.h new file mode 100644 index 0000000..b51f05b --- /dev/null +++ b/examples/draganddrop/draggabletext/dragwidget.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DRAGWIDGET_H +#define DRAGWIDGET_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QDragEnterEvent; +class QDropEvent; +QT_END_NAMESPACE + +class DragWidget : public QWidget +{ +public: + DragWidget(QWidget *parent = 0); + +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dropEvent(QDropEvent *event); + void mousePressEvent(QMouseEvent *event); +}; + +#endif diff --git a/examples/draganddrop/draggabletext/main.cpp b/examples/draganddrop/draggabletext/main.cpp new file mode 100644 index 0000000..e66c8b7 --- /dev/null +++ b/examples/draganddrop/draggabletext/main.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> +#include "dragwidget.h" + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(draggabletext); + + QApplication app(argc, argv); + DragWidget window; + window.show(); + return app.exec(); +} diff --git a/examples/draganddrop/draggabletext/words.txt b/examples/draganddrop/draggabletext/words.txt new file mode 100644 index 0000000..19b8b03 --- /dev/null +++ b/examples/draganddrop/draggabletext/words.txt @@ -0,0 +1,41 @@ +Qt +Quarterly +is +a +paper +based +newsletter +exclusively +available +to +Qt +customers +Every +quarter +we +mail +out +an +issue +that +we +hope +will +bring +added +insight +and +pleasure +to +your +Qt +programming +with +high +quality +technical +articles +written +by +Qt +experts |