File indexing completed on 2024-12-22 04:13:11
0001 /* 0002 * SPDX-FileCopyrightText: 2005 Thomas Zander <zander@kde.org> 0003 * SPDX-FileCopyrightText: 2005 C. Boemann <cbo@boemann.dk> 0004 * SPDX-FileCopyrightText: 2007 Boudewijn Rempt <boud@valdyas.org> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "widgets/kis_image_from_clipboard_widget.h" 0010 #include "widgets/kis_custom_image_widget.h" 0011 0012 #include <QApplication> 0013 #include <QClipboard> 0014 #include <QComboBox> 0015 #include <QDesktopWidget> 0016 #include <QFile> 0017 #include <QMimeData> 0018 #include <QPushButton> 0019 #include <QRect> 0020 #include <QSlider> 0021 #include <QTimer> 0022 0023 #include <KisPart.h> 0024 #include <KoColor.h> 0025 #include <KoColorModelStandardIds.h> 0026 #include <KoColorProfile.h> 0027 #include <KoCompositeOpRegistry.h> 0028 #include <KoID.h> 0029 #include <KisSqueezedComboBox.h> 0030 0031 #include <kis_debug.h> 0032 #include <kis_fill_painter.h> 0033 #include <kis_group_layer.h> 0034 #include <kis_icon.h> 0035 #include <kis_image.h> 0036 #include <kis_layer.h> 0037 #include <kis_paint_device.h> 0038 #include <kis_paint_layer.h> 0039 #include <kis_painter.h> 0040 0041 #include <KisCursorOverrideLock.h> 0042 #include "KisDocument.h" 0043 #include "kis_clipboard.h" 0044 #include "kis_import_catcher.h" 0045 #include "widgets/kis_cmb_idlist.h" 0046 0047 KisImageFromClipboardWidget::KisImageFromClipboardWidget(QWidget* parent, qint32 defWidth, qint32 defHeight, double resolution, const QString& defColorModel, const QString& defColorDepth, const QString& defColorProfile, const QString& imageName) 0048 : KisCustomImageWidget(parent, defWidth, defHeight, resolution, defColorModel, defColorDepth, defColorProfile, imageName) 0049 { 0050 setObjectName("KisImageFromClipboard"); 0051 0052 lblPreview->hide(); 0053 grpClipboard->show(); 0054 0055 setNumberOfLayers(1); 0056 0057 disconnect(newDialogConfirmationButtonBox, &QDialogButtonBox::accepted, nullptr, 0058 nullptr); // disable normal signal 0059 connect(newDialogConfirmationButtonBox, &QDialogButtonBox::accepted, this, &KisImageFromClipboardWidget::createImage); 0060 } 0061 0062 KisImageFromClipboardWidget::~KisImageFromClipboardWidget() 0063 { 0064 } 0065 0066 void KisImageFromClipboardWidget::createImage() 0067 { 0068 newDialogConfirmationButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false); 0069 0070 KisPaintDeviceSP clip = KisClipboard::instance()->clip(QRect(), true); 0071 if (!clip) { 0072 enableImageCreation(QImage()); 0073 return; 0074 } 0075 0076 KisDocument *doc = createNewImage(); 0077 0078 if (doc) { 0079 KisImageSP image = doc->image(); 0080 if (image && image->root() && image->root()->firstChild()) { 0081 KisNodeSP node = image->root()->firstChild(); 0082 while (node && (!dynamic_cast<KisPaintLayer*>(node.data()) || node->userLocked())) { 0083 node = node->nextSibling(); 0084 } 0085 0086 if (!node) { 0087 KisPaintLayerSP newLayer = new KisPaintLayer(image, image->nextLayerName(), OPACITY_OPAQUE_U8); 0088 image->addNode(newLayer); 0089 node = newLayer; 0090 } 0091 0092 KIS_SAFE_ASSERT_RECOVER_RETURN(node); 0093 0094 KisPaintLayer * layer = dynamic_cast<KisPaintLayer*>(node.data()); 0095 KIS_SAFE_ASSERT_RECOVER_RETURN(layer); 0096 0097 layer->setOpacity(OPACITY_OPAQUE_U8); 0098 const QRect r = clip->exactBounds(); 0099 KisImportCatcher::adaptClipToImageColorSpace(clip, image); 0100 0101 KisPainter::copyAreaOptimized(QPoint(), clip, layer->paintDevice(), r); 0102 layer->setDirty(); 0103 } 0104 doc->setModified(true); 0105 emit m_openPane->documentSelected(doc); 0106 m_openPane->accept(); 0107 } 0108 0109 newDialogConfirmationButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true); 0110 } 0111 0112 void KisImageFromClipboardWidget::clipboardDataChanged() 0113 { 0114 createClipboardPreview(); 0115 } 0116 0117 void KisImageFromClipboardWidget::showEvent(QShowEvent *event) 0118 { 0119 KisCustomImageWidget::showEvent(event); 0120 0121 connect(KisClipboard::instance(), &KisClipboard::clipChanged, this, &KisImageFromClipboardWidget::clipboardDataChanged, Qt::UniqueConnection); 0122 0123 createClipboardPreview(); 0124 } 0125 0126 0127 void KisImageFromClipboardWidget::createClipboardPreview() 0128 { 0129 if (!KisClipboard::instance()->hasClip()) { 0130 enableImageCreation(QImage()); 0131 } 0132 0133 KisCursorOverrideLock cursorLock(Qt::BusyCursor); 0134 0135 QImage qimage = QApplication::clipboard()->image(); 0136 enableImageCreation(qimage); 0137 } 0138 0139 void KisImageFromClipboardWidget::enableImageCreation(const QImage &qimage) 0140 { 0141 if (qimage.isNull()) { 0142 doubleWidth->setValue(0); 0143 doubleHeight->setValue(0); 0144 newDialogConfirmationButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false); 0145 lblPreview->hide(); 0146 tabWidget->setEnabled(false); 0147 0148 lblDocumentInfo->setText(i18n("The clipboard is empty or does not have an image in it.")); 0149 } else { 0150 QSize previewSize = QSize(75, 75) * devicePixelRatioF(); 0151 QPixmap preview = QPixmap::fromImage(qimage.scaled(previewSize, Qt::KeepAspectRatio)); 0152 preview.setDevicePixelRatio(devicePixelRatioF()); 0153 lblPreview->setPixmap(preview); 0154 lblPreview->show(); 0155 newDialogConfirmationButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true); 0156 0157 doubleWidth->setValue(qimage.width()); 0158 doubleHeight->setValue(qimage.height()); 0159 0160 tabWidget->setEnabled(true); 0161 } 0162 }