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/dropsite | |
download | Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.zip Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.gz Qt-e5fcad302d86d316390c6b0f62759a067313e8a9.tar.bz2 |
Long live Qt 4.5!
Diffstat (limited to 'examples/draganddrop/dropsite')
-rw-r--r-- | examples/draganddrop/dropsite/droparea.cpp | 127 | ||||
-rw-r--r-- | examples/draganddrop/dropsite/droparea.h | 78 | ||||
-rw-r--r-- | examples/draganddrop/dropsite/dropsite.pro | 12 | ||||
-rw-r--r-- | examples/draganddrop/dropsite/dropsitewindow.cpp | 146 | ||||
-rw-r--r-- | examples/draganddrop/dropsite/dropsitewindow.h | 78 | ||||
-rw-r--r-- | examples/draganddrop/dropsite/main.cpp | 54 |
6 files changed, 495 insertions, 0 deletions
diff --git a/examples/draganddrop/dropsite/droparea.cpp b/examples/draganddrop/dropsite/droparea.cpp new file mode 100644 index 0000000..495e2bf --- /dev/null +++ b/examples/draganddrop/dropsite/droparea.cpp @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** 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 "droparea.h" + +//! [DropArea constructor] +DropArea::DropArea(QWidget *parent) + : QLabel(parent) +{ + setMinimumSize(200, 200); + setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); + setAlignment(Qt::AlignCenter); + setAcceptDrops(true); + setAutoFillBackground(true); + clear(); +} +//! [DropArea constructor] + +//! [dragEnterEvent() function] +void DropArea::dragEnterEvent(QDragEnterEvent *event) +{ + setText(tr("<drop content>")); + setBackgroundRole(QPalette::Highlight); + + event->acceptProposedAction(); + emit changed(event->mimeData()); +} +//! [dragEnterEvent() function] + +//! [dragMoveEvent() function] +void DropArea::dragMoveEvent(QDragMoveEvent *event) +{ + event->acceptProposedAction(); +} +//! [dragMoveEvent() function] + +//! [dropEvent() function part1] +void DropArea::dropEvent(QDropEvent *event) +{ + const QMimeData *mimeData = event->mimeData(); +//! [dropEvent() function part1] + +//! [dropEvent() function part2] + if (mimeData->hasImage()) { + setPixmap(qvariant_cast<QPixmap>(mimeData->imageData())); + } else if (mimeData->hasHtml()) { + setText(mimeData->html()); + setTextFormat(Qt::RichText); + } else if (mimeData->hasText()) { + setText(mimeData->text()); + setTextFormat(Qt::PlainText); + } 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"); + } + setText(text); + } else { + setText(tr("Cannot display data")); + } +//! [dropEvent() function part2] + +//! [dropEvent() function part3] + setBackgroundRole(QPalette::Dark); + event->acceptProposedAction(); +} +//! [dropEvent() function part3] + +//! [dragLeaveEvent() function] +void DropArea::dragLeaveEvent(QDragLeaveEvent *event) +{ + clear(); + event->accept(); +} +//! [dragLeaveEvent() function] + +//! [clear() function] +void DropArea::clear() +{ + setText(tr("<drop content>")); + setBackgroundRole(QPalette::Dark); + + emit changed(); +} +//! [clear() function] diff --git a/examples/draganddrop/dropsite/droparea.h b/examples/draganddrop/dropsite/droparea.h new file mode 100644 index 0000000..8a40acc --- /dev/null +++ b/examples/draganddrop/dropsite/droparea.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** 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 DROPAREA_H +#define DROPAREA_H + +#include <QLabel> + +QT_BEGIN_NAMESPACE +class QMimeData; +QT_END_NAMESPACE + +//! [DropArea header part1] +class DropArea : public QLabel +{ + Q_OBJECT + +public: + DropArea(QWidget *parent = 0); + +public slots: + void clear(); + +signals: + void changed(const QMimeData *mimeData = 0); +//! [DropArea header part1] + +//! [DropArea header part2] +protected: + void dragEnterEvent(QDragEnterEvent *event); + void dragMoveEvent(QDragMoveEvent *event); + void dragLeaveEvent(QDragLeaveEvent *event); + void dropEvent(QDropEvent *event); + +private: + QLabel *label; +}; +//! [DropArea header part2] + +#endif diff --git a/examples/draganddrop/dropsite/dropsite.pro b/examples/draganddrop/dropsite/dropsite.pro new file mode 100644 index 0000000..29dd0fd --- /dev/null +++ b/examples/draganddrop/dropsite/dropsite.pro @@ -0,0 +1,12 @@ +HEADERS = droparea.h \ + dropsitewindow.h +SOURCES = droparea.cpp \ + dropsitewindow.cpp \ + main.cpp + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/dropsite +sources.files = $$SOURCES $$HEADERS $$RESOURCES *.pro images +sources.path = $$[QT_INSTALL_EXAMPLES]/draganddrop/dropsite +INSTALLS += target sources + diff --git a/examples/draganddrop/dropsite/dropsitewindow.cpp b/examples/draganddrop/dropsite/dropsitewindow.cpp new file mode 100644 index 0000000..6e7055b --- /dev/null +++ b/examples/draganddrop/dropsite/dropsitewindow.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** 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 "droparea.h" +#include "dropsitewindow.h" + +//! [constructor part1] +DropSiteWindow::DropSiteWindow() +{ + abstractLabel = new QLabel(tr("This example accepts drags from other " + "applications and displays the MIME types " + "provided by the drag object.")); + abstractLabel->setWordWrap(true); + abstractLabel->adjustSize(); +//! [constructor part1] + +//! [constructor part2] + dropArea = new DropArea; + connect(dropArea, SIGNAL(changed(const QMimeData *)), + this, SLOT(updateFormatsTable(const QMimeData *))); +//! [constructor part2] + +//! [constructor part3] + QStringList labels; + labels << tr("Format") << tr("Content"); + + formatsTable = new QTableWidget; + formatsTable->setColumnCount(2); + formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers); + formatsTable->setHorizontalHeaderLabels(labels); + formatsTable->horizontalHeader()->setStretchLastSection(true); +//! [constructor part3] + +//! [constructor part4] + clearButton = new QPushButton(tr("Clear")); + quitButton = new QPushButton(tr("Quit")); + + buttonBox = new QDialogButtonBox; + buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole); + buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); + + connect(quitButton, SIGNAL(pressed()), this, SLOT(close())); + connect(clearButton, SIGNAL(pressed()), dropArea, SLOT(clear())); +//! [constructor part4] + +//! [constructor part5] + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(abstractLabel); + mainLayout->addWidget(dropArea); + mainLayout->addWidget(formatsTable); + mainLayout->addWidget(buttonBox); + setLayout(mainLayout); + + setWindowTitle(tr("Drop Site")); + setMinimumSize(350, 500); +} +//! [constructor part5] + +//! [updateFormatsTable() part1] +void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData) +{ + formatsTable->setRowCount(0); + if (!mimeData) + return; +//! [updateFormatsTable() part1] + +//! [updateFormatsTable() part2] + foreach (QString format, mimeData->formats()) { + QTableWidgetItem *formatItem = new QTableWidgetItem(format); + formatItem->setFlags(Qt::ItemIsEnabled); + formatItem->setTextAlignment(Qt::AlignTop | Qt::AlignLeft); +//! [updateFormatsTable() part2] + +//! [updateFormatsTable() part3] + QString text; + if (format == "text/plain") { + text = mimeData->text().simplified(); + } else if (format == "text/html") { + text = mimeData->html().simplified(); + } else if (format == "text/uri-list") { + QList<QUrl> urlList = mimeData->urls(); + for (int i = 0; i < urlList.size() && i < 32; ++i) { + QString url = urlList.at(i).path(); + text.append(url + " "); + } + } 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 + " "); + } + } +//! [updateFormatsTable() part3] + +//! [updateFormatsTable() part4] + int row = formatsTable->rowCount(); + formatsTable->insertRow(row); + formatsTable->setItem(row, 0, new QTableWidgetItem(format)); + formatsTable->setItem(row, 1, new QTableWidgetItem(text)); + } + + formatsTable->resizeColumnToContents(0); +} +//! [updateFormatsTable() part4] diff --git a/examples/draganddrop/dropsite/dropsitewindow.h b/examples/draganddrop/dropsite/dropsitewindow.h new file mode 100644 index 0000000..bbdc687 --- /dev/null +++ b/examples/draganddrop/dropsite/dropsitewindow.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** 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 DROPSITEWINDOW_H +#define DROPSITEWINDOW_H + +#include <QWidget> + +QT_BEGIN_NAMESPACE +class QDialogButtonBox; +class QLabel; +class QMimeData; +class QPushButton; +class QTableWidget; +QT_END_NAMESPACE +class DropArea; + +//! [DropSiteWindow header] +class DropSiteWindow : public QWidget +{ + Q_OBJECT + +public: + DropSiteWindow(); + +public slots: + void updateFormatsTable(const QMimeData *mimeData); + +private: + DropArea *dropArea; + QLabel *abstractLabel; + QTableWidget *formatsTable; + + QPushButton *clearButton; + QPushButton *quitButton; + QDialogButtonBox *buttonBox; +}; +//! [DropSiteWindow header] + +#endif diff --git a/examples/draganddrop/dropsite/main.cpp b/examples/draganddrop/dropsite/main.cpp new file mode 100644 index 0000000..62589ad --- /dev/null +++ b/examples/draganddrop/dropsite/main.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 "dropsitewindow.h" + +//! [main() function] +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + DropSiteWindow window; + window.show(); + return app.exec(); +} +//! [main() function] |