Image: enable pasting image data directly (#24711)
* Image: enable pasting image data directly * Update MainWindow.cpp
This commit is contained in:
@@ -71,6 +71,7 @@
|
||||
#include <App/Document.h>
|
||||
#include <App/DocumentObject.h>
|
||||
#include <App/DocumentObjectGroup.h>
|
||||
#include <App/ImagePlane.h>
|
||||
#include <App/SafeMode.h>
|
||||
#include <Base/ConsoleObserver.h>
|
||||
#include <Base/Parameter.h>
|
||||
@@ -2058,32 +2059,84 @@ bool MainWindow::canInsertFromMimeData (const QMimeData * source) const
|
||||
if (!source)
|
||||
return false;
|
||||
return source->hasUrls() ||
|
||||
source->hasImage() ||
|
||||
source->hasFormat(_MimeDocObj) || source->hasFormat(_MimeDocObjX) ||
|
||||
source->hasFormat(_MimeDocObjFile) || source->hasFormat(_MimeDocObjXFile);
|
||||
}
|
||||
|
||||
void MainWindow::insertFromMimeData (const QMimeData * mimeData)
|
||||
{
|
||||
if (!mimeData)
|
||||
if (!mimeData) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mimeData->hasImage()) {
|
||||
App::Document* doc = App::GetApplication().getActiveDocument();
|
||||
if (!doc) {
|
||||
doc = App::GetApplication().newDocument();
|
||||
}
|
||||
|
||||
if (!doc) {
|
||||
return;
|
||||
}
|
||||
|
||||
QImage image = qvariant_cast<QImage>(mimeData->imageData());
|
||||
if (image.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string tempPath = App::Application::getTempFileName("png");
|
||||
if (image.save(QString::fromStdString(tempPath), "PNG")) {
|
||||
WaitCursor wc;
|
||||
doc->openTransaction("Paste image");
|
||||
|
||||
try {
|
||||
std::string objName = doc->getUniqueObjectName("ImagePlane");
|
||||
App::DocumentObject* obj = doc->addObject("Image::ImagePlane", objName.c_str());
|
||||
if (obj) {
|
||||
obj->Label.setValue("PastedImage");
|
||||
static_cast<Image::ImagePlane*>(obj)->ImageFile.setValue(tempPath.c_str());
|
||||
doc->recompute();
|
||||
}
|
||||
}
|
||||
catch (const Base::Exception& e) {
|
||||
doc->abortTransaction();
|
||||
e.reportException();
|
||||
return;
|
||||
}
|
||||
|
||||
doc->commitTransaction();
|
||||
}
|
||||
else {
|
||||
Base::Console().error("Failed to save pasted image to temporary file: %s\n",
|
||||
tempPath.c_str());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
bool fromDoc = false;
|
||||
bool hasXLink = false;
|
||||
QString format;
|
||||
if(mimeData->hasFormat(_MimeDocObj))
|
||||
if (mimeData->hasFormat(_MimeDocObj)) {
|
||||
format = _MimeDocObj;
|
||||
}
|
||||
else if(mimeData->hasFormat(_MimeDocObjX)) {
|
||||
format = _MimeDocObjX;
|
||||
hasXLink = true;
|
||||
}else if(mimeData->hasFormat(_MimeDocObjFile)) {
|
||||
}
|
||||
else if(mimeData->hasFormat(_MimeDocObjFile)) {
|
||||
format = _MimeDocObjFile;
|
||||
fromDoc = true;
|
||||
}else if(mimeData->hasFormat(_MimeDocObjXFile)) {
|
||||
}
|
||||
else if(mimeData->hasFormat(_MimeDocObjXFile)) {
|
||||
format = _MimeDocObjXFile;
|
||||
fromDoc = true;
|
||||
hasXLink = true;
|
||||
}else {
|
||||
if (mimeData->hasUrls())
|
||||
}
|
||||
else {
|
||||
if (mimeData->hasUrls()) {
|
||||
loadUrls(App::GetApplication().getActiveDocument(), mimeData->urls());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user