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(s: "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(sender: dropArea, signal: &DropArea::changed, |
69 | receiver: this, slot: &DropSiteWindow::updateFormatsTable); |
70 | //! [constructor part2] |
71 | |
72 | //! [constructor part3] |
73 | QStringList labels; |
74 | labels << tr(s: "Format" ) << tr(s: "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(s: "Clear" )); |
85 | copyButton = new QPushButton(tr(s: "Copy" )); |
86 | quitButton = new QPushButton(tr(s: "Quit" )); |
87 | |
88 | buttonBox = new QDialogButtonBox; |
89 | buttonBox->addButton(button: clearButton, role: QDialogButtonBox::ActionRole); |
90 | buttonBox->addButton(button: copyButton, role: QDialogButtonBox::ActionRole); |
91 | #if !QT_CONFIG(clipboard) |
92 | copyButton->setVisible(false); |
93 | #endif |
94 | |
95 | buttonBox->addButton(button: quitButton, role: QDialogButtonBox::RejectRole); |
96 | |
97 | connect(sender: quitButton, signal: &QAbstractButton::clicked, receiver: this, slot: &QWidget::close); |
98 | connect(sender: clearButton, signal: &QAbstractButton::clicked, receiver: dropArea, slot: &DropArea::clear); |
99 | connect(sender: copyButton, signal: &QAbstractButton::clicked, receiver: this, slot: &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(s: "Drop Site" )); |
110 | setMinimumSize(minw: 350, minh: 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/markdown" )) { |
136 | text = QString::fromUtf8(str: mimeData->data(mimetype: QLatin1String("text/markdown" ))); |
137 | } else if (format == QLatin1String("text/html" )) { |
138 | text = mimeData->html().simplified(); |
139 | } else if (format == QLatin1String("text/uri-list" )) { |
140 | QList<QUrl> urlList = mimeData->urls(); |
141 | for (int i = 0; i < urlList.size() && i < 32; ++i) |
142 | text.append(s: urlList.at(i).toString() + QLatin1Char(' ')); |
143 | } else { |
144 | QByteArray data = mimeData->data(mimetype: format); |
145 | for (int i = 0; i < data.size() && i < 32; ++i) |
146 | text.append(QStringLiteral("%1 " ).arg(a: uchar(data[i]), fieldWidth: 2, base: 16, fillChar: QLatin1Char('0')).toUpper()); |
147 | } |
148 | //! [updateFormatsTable() part3] |
149 | |
150 | //! [updateFormatsTable() part4] |
151 | int row = formatsTable->rowCount(); |
152 | formatsTable->insertRow(row); |
153 | formatsTable->setItem(row, column: 0, item: new QTableWidgetItem(format)); |
154 | formatsTable->setItem(row, column: 1, item: new QTableWidgetItem(text)); |
155 | } |
156 | |
157 | formatsTable->resizeColumnToContents(column: 0); |
158 | #if QT_CONFIG(clipboard) |
159 | copyButton->setEnabled(formatsTable->rowCount() > 0); |
160 | #endif |
161 | } |
162 | //! [updateFormatsTable() part4] |
163 | |
164 | void DropSiteWindow::copy() |
165 | { |
166 | #if QT_CONFIG(clipboard) |
167 | QString text; |
168 | for (int row = 0, rowCount = formatsTable->rowCount(); row < rowCount; ++row) |
169 | text += formatsTable->item(row, column: 0)->text() + ": " + formatsTable->item(row, column: 1)->text() + '\n'; |
170 | QGuiApplication::clipboard()->setText(text); |
171 | #endif |
172 | } |
173 | |