File indexing completed on 2024-12-22 04:13:08

0001 /*
0002  * SPDX-FileCopyrightText: 2022 L. E. Segovia <amy@amyspark.me>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "kis_canvas_drop.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QAction>
0011 #include <QUrl>
0012 
0013 class KisCanvasDrop::Private
0014 {
0015 public:
0016     QAction *insertAsNewLayer;
0017     QAction *insertManyLayers;
0018     QAction *insertAsNewFileLayer;
0019     QAction *insertManyFileLayers;
0020     QAction *openInNewDocument;
0021     QAction *openManyDocuments;
0022     QAction *insertAsReferenceImage;
0023     QAction *insertAsReferenceImages;
0024     QAction *cancel;
0025 };
0026 
0027 KisCanvasDrop::KisCanvasDrop(QWidget *parent)
0028     : QMenu(parent)
0029     , d(new Private)
0030 {
0031     setObjectName("drop_popup");
0032 
0033     d->insertAsNewLayer = addAction(i18n("Insert as New Layer"));
0034     d->insertAsNewFileLayer = addAction(i18n("Insert as New File Layer"));
0035     d->openInNewDocument = addAction(i18n("Open in New Document"));
0036     d->insertAsReferenceImage = addAction(i18n("Insert as Reference Image"));
0037 
0038     d->insertManyLayers = addAction(i18n("Insert Many Layers"));
0039     d->insertManyFileLayers = addAction(i18n("Insert Many File Layers"));
0040     d->openManyDocuments = addAction(i18n("Open Many Documents"));
0041     d->insertAsReferenceImages = addAction(i18n("Insert as Reference Images"));
0042 
0043     addSeparator();
0044 
0045     d->cancel = addAction(i18n("Cancel"));
0046 }
0047 
0048 KisCanvasDrop::Action KisCanvasDrop::dropAs(const QMimeData &data, QPoint pos)
0049 {
0050     const auto &urls = data.urls();
0051 
0052     d->insertAsNewLayer->setEnabled(data.hasImage() || urls.count() == 1);
0053     d->insertAsNewFileLayer->setEnabled(urls.count() == 1);
0054     d->openInNewDocument->setEnabled(urls.count() == 1);
0055     d->insertAsReferenceImage->setEnabled(data.hasImage() || urls.count() == 1);
0056 
0057     d->insertManyLayers->setEnabled(urls.count() > 1);
0058     d->insertManyFileLayers->setEnabled(urls.count() > 1);
0059     d->openManyDocuments->setEnabled(urls.count() > 1);
0060     d->insertAsReferenceImages->setEnabled(urls.count() > 1);
0061 
0062     const QAction *action = exec(pos);
0063 
0064     if (action == d->insertAsNewLayer) {
0065         return KisCanvasDrop::INSERT_AS_NEW_LAYER;
0066     } else if (action == d->insertAsNewFileLayer) {
0067         return KisCanvasDrop::INSERT_AS_NEW_FILE_LAYER;
0068     } else if (action == d->openInNewDocument) {
0069         return KisCanvasDrop::OPEN_IN_NEW_DOCUMENT;
0070     } else if (action == d->insertAsReferenceImage) {
0071         return KisCanvasDrop::INSERT_AS_REFERENCE_IMAGE;
0072     } else if (action == d->insertManyLayers) {
0073         return KisCanvasDrop::INSERT_MANY_LAYERS;
0074     } else if (action == d->insertManyFileLayers) {
0075         return KisCanvasDrop::INSERT_MANY_FILE_LAYERS;
0076     } else if (action == d->openManyDocuments) {
0077         return KisCanvasDrop::OPEN_MANY_DOCUMENTS;
0078     } else if (action == d->insertAsReferenceImages) {
0079         return KisCanvasDrop::INSERT_AS_REFERENCE_IMAGES;
0080     }
0081 
0082     return KisCanvasDrop::NONE;
0083 }