File indexing completed on 2024-05-05 04:45:45

0001 // SPDX-License-Identifier: BSD-3-Clause
0002 // SPDX-FileCopyrightText: 2016 The Qt Company Ltd. <https://www.qt.io/licensing/>
0003 // SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0004 
0005 #include <QtWidgets>
0006 
0007 #include <KUrlMimeData>
0008 
0009 #include "droparea.h"
0010 #include "dropsitewindow.h"
0011 
0012 DropSiteWindow::DropSiteWindow(QWidget *parent)
0013     : QWidget(parent)
0014 {
0015     abstractLabel =
0016         new QLabel(tr("This example accepts drags from other "
0017                       "applications and displays the MIME types "
0018                       "provided by the drag object."));
0019     abstractLabel->setWordWrap(true);
0020     abstractLabel->adjustSize();
0021 
0022     dropArea = new DropArea;
0023     connect(dropArea, &DropArea::changed, this, &DropSiteWindow::updateFormatsTable);
0024 
0025     QStringList labels;
0026     labels << tr("Format") << tr("Content");
0027 
0028     formatsTable = new QTableWidget;
0029     formatsTable->setColumnCount(2);
0030     formatsTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
0031     formatsTable->setHorizontalHeaderLabels(labels);
0032     formatsTable->horizontalHeader()->setStretchLastSection(true);
0033 
0034     clearButton = new QPushButton(tr("Clear"));
0035     copyButton = new QPushButton(tr("Copy"));
0036 
0037     buttonBox = new QDialogButtonBox;
0038     buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole);
0039     buttonBox->addButton(copyButton, QDialogButtonBox::ActionRole);
0040     copyButton->setVisible(false);
0041 
0042     connect(clearButton, &QAbstractButton::clicked, dropArea, &DropArea::clear);
0043     connect(copyButton, &QAbstractButton::clicked, this, &DropSiteWindow::copy);
0044 
0045     auto  mainLayout = new QVBoxLayout(this);
0046     mainLayout->addWidget(abstractLabel);
0047     mainLayout->addWidget(dropArea);
0048     mainLayout->addWidget(formatsTable);
0049     mainLayout->addWidget(buttonBox);
0050 }
0051 
0052 void DropSiteWindow::updateFormatsTable(const QMimeData *mimeData)
0053 {
0054     formatsTable->setRowCount(0);
0055     copyButton->setEnabled(false);
0056     if (!mimeData) {
0057         return;
0058     }
0059 
0060     const QStringList formats = mimeData->formats();
0061     for (const QString &format : formats) {
0062         auto formatItem = new QTableWidgetItem(format);
0063         formatItem->setFlags(Qt::ItemIsEnabled);
0064         formatItem->setTextAlignment(Qt::AlignTop | Qt::AlignLeft);
0065 
0066         QString text;
0067         if (format == QLatin1String("text/plain")) {
0068             text = mimeData->text().simplified();
0069         } else if (format == QLatin1String("text/html")) {
0070             text = mimeData->html().simplified();
0071         } else if (format == QLatin1String("text/uri-list")) {
0072             QList<QUrl> urlList = KUrlMimeData::urlsFromMimeData(mimeData);
0073             for (int i = 0; i < urlList.size() && i < 32; ++i) {
0074                 text.append(urlList.at(i).toString() + QLatin1Char(' '));
0075             }
0076         } else {
0077             QByteArray data = mimeData->data(format);
0078             for (int i = 0; i < data.size() && i < 32; ++i) {
0079                 text.append(QStringLiteral("%1 ").arg(uchar(data[i]), 2, 16, QLatin1Char('0')).toUpper());
0080             }
0081         }
0082 
0083         int row = formatsTable->rowCount();
0084         formatsTable->insertRow(row);
0085         formatsTable->setItem(row, 0, new QTableWidgetItem(format));
0086         formatsTable->setItem(row, 1, new QTableWidgetItem(text));
0087     }
0088 
0089     formatsTable->resizeColumnToContents(0);
0090     copyButton->setEnabled(formatsTable->rowCount() > 0);
0091 }
0092 
0093 void DropSiteWindow::copy()
0094 {
0095     QString text;
0096     for (int row = 0, rowCount = formatsTable->rowCount(); row < rowCount; ++row) {
0097         text += formatsTable->item(row, 0)->text() + ": " + formatsTable->item(row, 1)->text() + '\n';
0098     }
0099     QGuiApplication::clipboard()->setText(text);
0100 }