| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 |
|
|---|
| 44 | #include "droparea.h"
|
|---|
| 45 | #include "dropsitewindow.h"
|
|---|
| 46 |
|
|---|
| 47 | //! [constructor part1]
|
|---|
| 48 | DropSiteWindow::DropSiteWindow()
|
|---|
| 49 | {
|
|---|
| 50 | abstractLabel = new QLabel(tr("This example accepts drags from other "
|
|---|
| 51 | "applications and displays the MIME types "
|
|---|
| 52 | "provided by the drag object."));
|
|---|
| 53 | abstractLabel->setWordWrap(true);
|
|---|
| 54 | abstractLabel->adjustSize();
|
|---|
| 55 | //! [constructor part1]
|
|---|
| 56 |
|
|---|
| 57 | //! [constructor part2]
|
|---|
| 58 | dropArea = new DropArea;
|
|---|
| 59 | connect(dropArea, SIGNAL(changed(const QMimeData *)),
|
|---|
| 60 | this, SLOT(updateFormatsTable(const QMimeData *)));
|
|---|
| 61 | //! [constructor part2]
|
|---|
| 62 |
|
|---|
| 63 | //! [constructor part3]
|
|---|
| 64 | QStringList labels;
|
|---|
| 65 | labels << tr("Format") << tr("Content");
|
|---|
| 66 |
|
|---|
| 67 | formatsTable = new QTableWidget;
|
|---|
| 68 | formatsTable->setColumnCount(2);
|
|---|
| 69 | formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|---|
| 70 | formatsTable->setHorizontalHeaderLabels(labels);
|
|---|
| 71 | formatsTable->horizontalHeader()->setStretchLastSection(true);
|
|---|
| 72 | //! [constructor part3]
|
|---|
| 73 |
|
|---|
| 74 | //! [constructor part4]
|
|---|
| 75 | clearButton = new QPushButton(tr("Clear"));
|
|---|
| 76 | quitButton = new QPushButton(tr("Quit"));
|
|---|
| 77 |
|
|---|
| 78 | buttonBox = new QDialogButtonBox;
|
|---|
| 79 | buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole);
|
|---|
| 80 | buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
|---|
| 81 |
|
|---|
| 82 | connect(quitButton, SIGNAL(pressed()), this, SLOT(close()));
|
|---|
| 83 | connect(clearButton, SIGNAL(pressed()), dropArea, SLOT(clear()));
|
|---|
| 84 | //! [constructor part4]
|
|---|
| 85 |
|
|---|
| 86 | //! [constructor part5]
|
|---|
| 87 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
|---|
| 88 | mainLayout->addWidget(abstractLabel);
|
|---|
| 89 | mainLayout->addWidget(dropArea);
|
|---|
| 90 | mainLayout->addWidget(formatsTable);
|
|---|
| 91 | mainLayout->addWidget(buttonBox);
|
|---|
| 92 | setLayout(mainLayout);
|
|---|
| 93 |
|
|---|
| 94 | setWindowTitle(tr("Drop Site"));
|
|---|
| 95 | setMinimumSize(350, 500);
|
|---|
| 96 | }
|
|---|
| 97 | //! [constructor part5]
|
|---|
| 98 |
|
|---|
| 99 | //! [updateFormatsTable() part1]
|
|---|
| 100 | void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData)
|
|---|
| 101 | {
|
|---|
| 102 | formatsTable->setRowCount(0);
|
|---|
| 103 | if (!mimeData)
|
|---|
| 104 | return;
|
|---|
| 105 | //! [updateFormatsTable() part1]
|
|---|
| 106 |
|
|---|
| 107 | //! [updateFormatsTable() part2]
|
|---|
| 108 | foreach (QString format, mimeData->formats()) {
|
|---|
| 109 | QTableWidgetItem *formatItem = new QTableWidgetItem(format);
|
|---|
| 110 | formatItem->setFlags(Qt::ItemIsEnabled);
|
|---|
| 111 | formatItem->setTextAlignment(Qt::AlignTop | Qt::AlignLeft);
|
|---|
| 112 | //! [updateFormatsTable() part2]
|
|---|
| 113 |
|
|---|
| 114 | //! [updateFormatsTable() part3]
|
|---|
| 115 | QString text;
|
|---|
| 116 | if (format == "text/plain") {
|
|---|
| 117 | text = mimeData->text().simplified();
|
|---|
| 118 | } else if (format == "text/html") {
|
|---|
| 119 | text = mimeData->html().simplified();
|
|---|
| 120 | } else if (format == "text/uri-list") {
|
|---|
| 121 | QList<QUrl> urlList = mimeData->urls();
|
|---|
| 122 | for (int i = 0; i < urlList.size() && i < 32; ++i) {
|
|---|
| 123 | QString url = urlList.at(i).path();
|
|---|
| 124 | text.append(url + " ");
|
|---|
| 125 | }
|
|---|
| 126 | } else {
|
|---|
| 127 | QByteArray data = mimeData->data(format);
|
|---|
| 128 | for (int i = 0; i < data.size() && i < 32; ++i) {
|
|---|
| 129 | QString hex = QString("%1").arg(uchar(data[i]), 2, 16,
|
|---|
| 130 | QChar('0'))
|
|---|
| 131 | .toUpper();
|
|---|
| 132 | text.append(hex + " ");
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | //! [updateFormatsTable() part3]
|
|---|
| 136 |
|
|---|
| 137 | //! [updateFormatsTable() part4]
|
|---|
| 138 | int row = formatsTable->rowCount();
|
|---|
| 139 | formatsTable->insertRow(row);
|
|---|
| 140 | formatsTable->setItem(row, 0, new QTableWidgetItem(format));
|
|---|
| 141 | formatsTable->setItem(row, 1, new QTableWidgetItem(text));
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | formatsTable->resizeColumnToContents(0);
|
|---|
| 145 | }
|
|---|
| 146 | //! [updateFormatsTable() part4]
|
|---|