1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QtWidgets> |
52 | |
53 | #include "droparea.h" |
54 | #include "dropsitewindow.h" |
55 | |
56 | //! [constructor part1] |
57 | DropSiteWindow::DropSiteWindow() |
58 | { |
59 | abstractLabel = new QLabel(tr("This example accepts drags from other " |
60 | "applications and displays the MIME types " |
61 | "provided by the drag object." )); |
62 | abstractLabel->setWordWrap(true); |
63 | abstractLabel->adjustSize(); |
64 | //! [constructor part1] |
65 | |
66 | //! [constructor part2] |
67 | dropArea = new DropArea; |
68 | connect(dropArea, &DropArea::changed, |
69 | this, &DropSiteWindow::updateFormatsTable); |
70 | //! [constructor part2] |
71 | |
72 | //! [constructor part3] |
73 | QStringList labels; |
74 | labels << tr("Format" ) << tr("Content" ); |
75 | |
76 | formatsTable = new QTableWidget; |
77 | formatsTable->setColumnCount(2); |
78 | formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers); |
79 | formatsTable->setHorizontalHeaderLabels(labels); |
80 | formatsTable->horizontalHeader()->setStretchLastSection(true); |
81 | //! [constructor part3] |
82 | |
83 | //! [constructor part4] |
84 | clearButton = new QPushButton(tr("Clear" )); |
85 | copyButton = new QPushButton(tr("Copy" )); |
86 | quitButton = new QPushButton(tr("Quit" )); |
87 | |
88 | buttonBox = new QDialogButtonBox; |
89 | buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole); |
90 | buttonBox->addButton(copyButton, QDialogButtonBox::ActionRole); |
91 | #if !QT_CONFIG(clipboard) |
92 | copyButton->setVisible(false); |
93 | #endif |
94 | |
95 | buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); |
96 | |
97 | connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close); |
98 | connect(clearButton, &QAbstractButton::clicked, dropArea, &DropArea::clear); |
99 | connect(copyButton, &QAbstractButton::clicked, this, &DropSiteWindow::copy); |
100 | //! [constructor part4] |
101 | |
102 | //! [constructor part5] |
103 | QVBoxLayout *mainLayout = new QVBoxLayout(this); |
104 | mainLayout->addWidget(abstractLabel); |
105 | mainLayout->addWidget(dropArea); |
106 | mainLayout->addWidget(formatsTable); |
107 | mainLayout->addWidget(buttonBox); |
108 | |
109 | setWindowTitle(tr("Drop Site" )); |
110 | setMinimumSize(350, 500); |
111 | } |
112 | //! [constructor part5] |
113 | |
114 | //! [updateFormatsTable() part1] |
115 | void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData) |
116 | { |
117 | formatsTable->setRowCount(0); |
118 | copyButton->setEnabled(false); |
119 | if (!mimeData) |
120 | return; |
121 | //! [updateFormatsTable() part1] |
122 | |
123 | //! [updateFormatsTable() part2] |
124 | const QStringList formats = mimeData->formats(); |
125 | for (const QString &format : formats) { |
126 | QTableWidgetItem *formatItem = new QTableWidgetItem(format); |
127 | formatItem->setFlags(Qt::ItemIsEnabled); |
128 | formatItem->setTextAlignment(Qt::AlignTop | Qt::AlignLeft); |
129 | //! [updateFormatsTable() part2] |
130 | |
131 | //! [updateFormatsTable() part3] |
132 | QString text; |
133 | if (format == QLatin1String("text/plain" )) { |
134 | text = mimeData->text().simplified(); |
135 | } else if (format == QLatin1String("text/html" )) { |
136 | text = mimeData->html().simplified(); |
137 | } else if (format == QLatin1String("text/uri-list" )) { |
138 | QList<QUrl> urlList = mimeData->urls(); |
139 | for (int i = 0; i < urlList.size() && i < 32; ++i) |
140 | text.append(urlList.at(i).toString() + QLatin1Char(' ')); |
141 | } else { |
142 | QByteArray data = mimeData->data(format); |
143 | for (int i = 0; i < data.size() && i < 32; ++i) |
144 | text.append(QStringLiteral("%1 " ).arg(uchar(data[i]), 2, 16, QLatin1Char('0')).toUpper()); |
145 | } |
146 | //! [updateFormatsTable() part3] |
147 | |
148 | //! [updateFormatsTable() part4] |
149 | int row = formatsTable->rowCount(); |
150 | formatsTable->insertRow(row); |
151 | formatsTable->setItem(row, 0, new QTableWidgetItem(format)); |
152 | formatsTable->setItem(row, 1, new QTableWidgetItem(text)); |
153 | } |
154 | |
155 | formatsTable->resizeColumnToContents(0); |
156 | #if QT_CONFIG(clipboard) |
157 | copyButton->setEnabled(formatsTable->rowCount() > 0); |
158 | #endif |
159 | } |
160 | //! [updateFormatsTable() part4] |
161 | |
162 | void DropSiteWindow::copy() |
163 | { |
164 | #if QT_CONFIG(clipboard) |
165 | QString text; |
166 | for (int row = 0, rowCount = formatsTable->rowCount(); row < rowCount; ++row) |
167 | text += formatsTable->item(row, 0)->text() + ": " + formatsTable->item(row, 1)->text() + '\n'; |
168 | QGuiApplication::clipboard()->setText(text); |
169 | #endif |
170 | } |
171 | |