Debian Codegraph @

examples/widgets/draganddrop/dropsite/dropsitewindow.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include <QPushButton>
5 #include <QDialogButtonBox>
6 #include <QHeaderView>
7 #include <QTableWidget>
8 #include <QTableWidgetItem>
9 #include <QVBoxLayout>
10
11 #include <QClipboard>
12 #include <QGuiApplication>
13
14 #include <QMimeData>
15
16 #include "droparea.h"
17 #include "dropsitewindow.h"
18
19 using namespace Qt::StringLiterals;
20
21 //! [constructor part1]
22 DropSiteWindow::DropSiteWindow()
23 {
24 abstractLabel = new QLabel(tr("This example accepts drags from other "
25 "applications and displays the MIME types "
26 "provided by the drag object."));
27 abstractLabel->setWordWrap(true);
28 abstractLabel->adjustSize();
29 //! [constructor part1]
30
31 //! [constructor part2]
32 dropArea = new DropArea;
33 connect(dropArea, &DropArea::changed,
34 this, &DropSiteWindow::updateFormatsTable);
35 //! [constructor part2]
36
37 //! [constructor part3]
38 formatsTable = new QTableWidget;
39 formatsTable->setColumnCount(2);
40 formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
41 formatsTable->setHorizontalHeaderLabels({tr("Format"), tr("Content")});
42 formatsTable->horizontalHeader()->setStretchLastSection(true);
43 //! [constructor part3]
44
45 //! [constructor part4]
46 clearButton = new QPushButton(tr("Clear"));
47 copyButton = new QPushButton(tr("Copy"));
48 quitButton = new QPushButton(tr("Quit"));
49
50 buttonBox = new QDialogButtonBox;
51 buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole);
52 buttonBox->addButton(copyButton, QDialogButtonBox::ActionRole);
53 #if !QT_CONFIG(clipboard)
54 copyButton->setVisible(false);
55 #endif
56
57 buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
58
59 connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
60 connect(clearButton, &QAbstractButton::clicked, dropArea, &DropArea::clear);
61 connect(copyButton, &QAbstractButton::clicked, this, &DropSiteWindow::copy);
62 //! [constructor part4]
63
64 //! [constructor part5]
65 QVBoxLayout *mainLayout = new QVBoxLayout(this);
66 mainLayout->addWidget(abstractLabel);
67 mainLayout->addWidget(dropArea);
68 mainLayout->addWidget(formatsTable);
69 mainLayout->addWidget(buttonBox);
70
71 setWindowTitle(tr("Drop Site"));
72 resize(700, 500);
73 }
74 //! [constructor part5]
75
76 //! [updateFormatsTable() part1]
77 void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData)
78 {
79 formatsTable->setRowCount(0);
80 copyButton->setEnabled(false);
81 if (!mimeData)
82 return;
83 //! [updateFormatsTable() part1]
84
85 //! [updateFormatsTable() part2]
86 const QStringList formats = mimeData->formats();
87 for (const QString &format : formats) {
88 QTableWidgetItem *formatItem = new QTableWidgetItem(format);
89 formatItem->setFlags(Qt::ItemIsEnabled);
90 formatItem->setTextAlignment(Qt::AlignTop | Qt::AlignLeft);
91 //! [updateFormatsTable() part2]
92
93 //! [updateFormatsTable() part3]
94 QString text;
95 if (format == u"text/plain") {
96 text = mimeData->text().simplified();
97 } else if (format == u"text/markdown") {
98 text = QString::fromUtf8(mimeData->data(u"text/markdown"_s));
99 } else if (format == u"text/html") {
100 text = mimeData->html().simplified();
101 } else if (format == u"text/uri-list") {
102 QList<QUrl> urlList = mimeData->urls();
103 for (qsizetype i = 0, count = qMin(urlList.size(), qsizetype(32)); i < count; ++i)
104 text.append(urlList.at(i).toString() + u' ');
105 } else {
106 QByteArray data = mimeData->data(format);
107 if (data.size() > 32)
108 data.truncate(32);
109 text = QString::fromLatin1(data.toHex(' ')).toUpper();
110 }
111 //! [updateFormatsTable() part3]
112
113 //! [updateFormatsTable() part4]
114 int row = formatsTable->rowCount();
115 formatsTable->insertRow(row);
116 formatsTable->setItem(row, 0, new QTableWidgetItem(format));
117 formatsTable->setItem(row, 1, new QTableWidgetItem(text));
118 }
119
120 formatsTable->resizeColumnToContents(0);
121 #if QT_CONFIG(clipboard)
122 copyButton->setEnabled(formatsTable->rowCount() > 0);
123 #endif
124 }
125 //! [updateFormatsTable() part4]
126
127 void DropSiteWindow::copy()
128 {
129 #if QT_CONFIG(clipboard)
130 QString text;
131 for (int row = 0, rowCount = formatsTable->rowCount(); row < rowCount; ++row)
132 text += formatsTable->item(row, 0)->text() + ": " + formatsTable->item(row, 1)->text() + '\n';
133 QGuiApplication::clipboard()->setText(text);
134 #endif
135 }