Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

Add external copy and paste

Rodney6b040d98c7ce
diff --git a/toonz/sources/include/toonzqt/strokesdata.h b/toonz/sources/include/toonzqt/strokesdata.h
index 687e515bf..8f9745fdb 100644
--- a/toonz/sources/include/toonzqt/strokesdata.h
+++ b/toonz/sources/include/toonzqt/strokesdata.h
@@ -38,9 +38,18 @@ public:
StrokesData *clone() const override {
TVectorImage *vi = 0;
if (m_image) vi = dynamic_cast<TVectorImage *>(m_image->cloneImage());
- return new StrokesData(vi);
+ StrokesData *copy = new StrokesData(vi);
+ copy->setImageData(imageData());
+ for (const QString &format : formats()) {
+ if (format != "application/x-qt-image")
+ copy->QMimeData::setData(format, data(format));
+ }
+ return copy;
}
+ void setClipboardFormats();
+ static StrokesData *fromClipboard(const QMimeData *mime);
+
// data <- image; copia gli stroke indicati da indices
void setImage(TVectorImageP image, const std::set<int> &indices);
diff --git a/toonz/sources/tnztools/rasterselection.cpp b/toonz/sources/tnztools/rasterselection.cpp
index bbc68f1ae..770082c2a 100644
--- a/toonz/sources/tnztools/rasterselection.cpp
+++ b/toonz/sources/tnztools/rasterselection.cpp
@@ -32,12 +32,164 @@
#include <QApplication>
#include <QClipboard>
+#include <QDataStream>
+#include <QFile>
+#include <QTemporaryDir>
#include "timage_io.h"
#include "tropcm.h"
+#include "tstream.h"
//=============================================================================
namespace {
+TStroke getStrokeByRect(TRectD r);
+
+const char *const DrawingClipboardFormat =
+ "application/x-opentoonz-raster-selection-v1";
+
+// Keep the palette in the payload, so a Toonz raster selection can be pasted
+// into another process without relying on pointers owned by the first one.
+QByteArray paletteBytes(const TPaletteP &palette) {
+ if (!palette) return QByteArray();
+ QTemporaryDir dir;
+ if (!dir.isValid()) return QByteArray();
+ QString name = dir.filePath("selection.tpl");
+ {
+ TOStream os(TFilePath(name.toStdWString()));
+ os << palette.getPointer();
+ }
+ QFile file(name);
+ return file.open(QIODevice::ReadOnly) ? file.readAll() : QByteArray();
+}
+
+TPaletteP paletteFromBytes(const QByteArray &bytes) {
+ if (bytes.isEmpty()) return TPaletteP();
+ QTemporaryDir dir;
+ if (!dir.isValid()) return TPaletteP();
+ QString name = dir.filePath("selection.tpl");
+ QFile file(name);
+ if (!file.open(QIODevice::WriteOnly) || file.write(bytes) != bytes.size())
+ return TPaletteP();
+ file.close();
+ TPersist *persist = nullptr;
+ try {
+ TIStream is(TFilePath(name.toStdWString()));
+ is >> persist;
+ } catch (...) {
+ delete persist;
+ return TPaletteP();
+ }
+ TPalette *palette = dynamic_cast<TPalette *>(persist);
+ if (!palette) delete persist;
+ return TPaletteP(palette);
+}
+
+QImage clipboardImage(const TRasterP &raster, const TPaletteP &palette) {
+ if (!raster) return QImage();
+ TRaster32P rgba = raster;
+ if (!rgba) {
+ rgba = TRaster32P(raster->getSize());
+ if (TRasterCM32P cm = raster)
+ TRop::convert(rgba, cm, palette);
+ else
+ TRop::convert(rgba, raster);
+ }
+ // The QImage returned by rasterToQImage borrows the raster's storage.
+ return rasterToQImage(rgba).copy();
+}
+
+QByteArray encodeDrawing(const TRasterP &raster, const TPaletteP &palette,
+ double dpiX, double dpiY, const TRectD &bounds,
+ const TAffine &affine) {
+ if (!raster || raster->getLx() <= 0 || raster->getLy() <= 0)
+ return QByteArray();
+ const bool cm = (TRasterCM32P)raster;
+ QByteArray paletteData = cm ? paletteBytes(palette) : QByteArray();
+ if (cm && paletteData.isEmpty()) return QByteArray();
+ QByteArray bytes;
+ QDataStream stream(&bytes, QIODevice::WriteOnly);
+ stream.setVersion(QDataStream::Qt_5_15);
+ stream << quint32(1) << cm << dpiX << dpiY << bounds.x0 << bounds.y0
+ << bounds.x1 << bounds.y1 << affine.a11 << affine.a12 << affine.a13
+ << affine.a21 << affine.a22 << affine.a23 << paletteData;
+ if (cm) {
+ TRasterCM32P ras = raster;
+ QByteArray pixels;
+ QDataStream pixelStream(&pixels, QIODevice::WriteOnly);
+ for (int y = 0; y < ras->getLy(); ++y)
+ for (int x = 0; x < ras->getLx(); ++x)
+ pixelStream << quint32(ras->pixels(y)[x].getValue());
+ stream << ras->getLx() << ras->getLy() << qCompress(pixels);
+ } else {
+ stream << clipboardImage(raster, palette);
+ }
+ return stream.status() == QDataStream::Ok ? bytes : QByteArray();
+}
+
+std::unique_ptr<RasterImageData> decodeDrawing(const QMimeData *mime) {
+ if (!mime || !mime->hasFormat(DrawingClipboardFormat)) return nullptr;
+ QByteArray bytes = mime->data(DrawingClipboardFormat);
+ if (bytes.isEmpty() || bytes.size() > 256 * 1024 * 1024) return nullptr;
+ QDataStream stream(&bytes, QIODevice::ReadOnly);
+ stream.setVersion(QDataStream::Qt_5_15);
+ quint32 version;
+ bool cm;
+ double dpiX, dpiY, x0, y0, x1, y1, a11, a12, a13, a21, a22, a23;
+ QByteArray paletteData;
+ stream >> version >> cm >> dpiX >> dpiY >> x0 >> y0 >> x1 >> y1 >> a11 >>
+ a12 >> a13 >> a21 >> a22 >> a23 >> paletteData;
+ if (stream.status() != QDataStream::Ok || version != 1) return nullptr;
+ TPaletteP palette =
+ cm ? paletteFromBytes(paletteData) : TPaletteP(new TPalette);
+ if (!palette) return nullptr;
+ TRasterP raster;
+ if (cm) {
+ int width, height;
+ QByteArray compressed;
+ stream >> width >> height >> compressed;
+ if (stream.status() != QDataStream::Ok || width <= 0 || height <= 0 ||
+ width > 16384 || height > 16384 ||
+ qint64(width) * height * 4 > 256 * 1024 * 1024)
+ return nullptr;
+ if (compressed.size() < 4) return nullptr;
+ const auto *header =
+ reinterpret_cast<const unsigned char *>(compressed.constData());
+ const quint32 expandedSize = (quint32(header[0]) << 24) |
+ (quint32(header[1]) << 16) |
+ (quint32(header[2]) << 8) | header[3];
+ if (expandedSize != qint64(width) * height * 4) return nullptr;
+ QByteArray pixels = qUncompress(compressed);
+ if (pixels.size() != qint64(width) * height * 4) return nullptr;
+ QDataStream pixelStream(&pixels, QIODevice::ReadOnly);
+ TRasterCM32P cmRaster(width, height);
+ for (int y = 0; y < height; ++y)
+ for (int x = 0; x < width; ++x) {
+ quint32 value;
+ pixelStream >> value;
+ cmRaster->pixels(y)[x] = TPixelCM32(value);
+ }
+ raster = cmRaster;
+ } else {
+ QImage image;
+ stream >> image;
+ if (image.isNull() || image.width() > 16384 || image.height() > 16384 ||
+ qint64(image.width()) * image.height() * 4 > 256 * 1024 * 1024)
+ return nullptr;
+ raster = rasterFromQImage(image.convertToFormat(QImage::Format_ARGB32));
+ }
+ if (stream.status() != QDataStream::Ok || !raster) return nullptr;
+ TRectD bounds(x0, y0, x1, y1);
+ if (bounds.isEmpty()) return nullptr;
+ std::vector<TStroke> strokes(1, getStrokeByRect(bounds));
+ std::vector<TRectD> rects;
+ TAffine affine(a11, a12, a13, a21, a22, a23);
+ std::unique_ptr<RasterImageData> result(
+ cm ? static_cast<RasterImageData *>(new ToonzImageData)
+ : static_cast<RasterImageData *>(new FullColorImageData));
+ result->setData(raster, palette, dpiX, dpiY, raster->getSize(), rects,
+ strokes, strokes, affine);
+ return result;
+}
//-----------------------------------------------------------------------------
TRasterP getRaster(const TImageP image) {
@@ -1149,18 +1301,33 @@ void RasterSelection::copySelection() {
double dpix, dpiy;
std::vector<TRectD> rect;
+ TRectD bounds;
+ for (const TStroke &stroke : m_strokes) bounds += stroke.getBBox();
+ if (bounds.isEmpty())
+ bounds = TRectD(-ras->getLx() / 2.0, -ras->getLy() / 2.0,
+ ras->getLx() / 2.0, ras->getLy() / 2.0);
if (TToonzImageP ti = (TToonzImageP)(m_currentImage)) {
- ToonzImageData *data = new ToonzImageData();
+ std::unique_ptr<ToonzImageData> data(new ToonzImageData());
ti->getDpi(dpix, dpiy);
data->setData(ras, ti->getPalette(), dpix, dpiy, ti->getSize(), rect,
m_strokes, m_originalStrokes, m_affine);
- QApplication::clipboard()->setMimeData(cloneData(data));
+ data->setImageData(clipboardImage(ras, ti->getPalette()));
+ QByteArray payload =
+ encodeDrawing(ras, ti->getPalette(), dpix, dpiy, bounds, m_affine);
+ if (!payload.isEmpty())
+ data->QMimeData::setData(DrawingClipboardFormat, payload);
+ QApplication::clipboard()->setMimeData(data.release());
} else if (TRasterImageP ri = (TRasterImageP)(m_currentImage)) {
- FullColorImageData *data = new FullColorImageData();
+ std::unique_ptr<FullColorImageData> data(new FullColorImageData());
ri->getDpi(dpix, dpiy);
data->setData(ras, ri->getPalette(), dpix, dpiy, ri->getRaster()->getSize(),
rect, m_strokes, m_originalStrokes, m_affine);
- QApplication::clipboard()->setMimeData(cloneData(data));
+ data->setImageData(clipboardImage(ras, ri->getPalette()));
+ QByteArray payload =
+ encodeDrawing(ras, ri->getPalette(), dpix, dpiy, bounds, m_affine);
+ if (!payload.isEmpty())
+ data->QMimeData::setData(DrawingClipboardFormat, payload);
+ QApplication::clipboard()->setMimeData(data.release());
}
}
@@ -1243,6 +1410,16 @@ void RasterSelection::pasteSelection() {
dynamic_cast<const RasterImageData *>(clipboard->mimeData());
const StrokesData *stData =
dynamic_cast<const StrokesData *>(clipboard->mimeData());
+ std::unique_ptr<StrokesData> transferredStrokes;
+ if (!stData && !riData) {
+ transferredStrokes.reset(StrokesData::fromClipboard(clipboard->mimeData()));
+ stData = transferredStrokes.get();
+ }
+ std::unique_ptr<RasterImageData> transferredData;
+ if (!riData && !stData) {
+ transferredData = decodeDrawing(clipboard->mimeData());
+ riData = transferredData.get();
+ }
QImage clipImage = clipboard->image();
if (!riData && !stData && clipImage.height() == 0) return;
if (isFloating()) pasteFloatingSelection();
@@ -1286,8 +1463,9 @@ void RasterSelection::pasteSelection() {
}
}
- if (clipImage.height() > 0 && (levelType == OVL_XSHLEVEL ||
- m_currentImage->getType() == OVL_XSHLEVEL)) {
+ if (!riData && clipImage.height() > 0 &&
+ (levelType == OVL_XSHLEVEL ||
+ m_currentImage->getType() == OVL_XSHLEVEL)) {
// An image was pasted from outside OpenToonz
// Set up variables
@@ -1314,7 +1492,7 @@ void RasterSelection::pasteSelection() {
m_originalStrokes.push_back(stroke);
}
// pack up the data to send to the next pasteSelection
- FullColorImageData *qimageData = new FullColorImageData();
+ std::unique_ptr<FullColorImageData> qimageData(new FullColorImageData());
qimageData->setData(ras, ri->getPalette(), 120.0, 120.0,
ri->getRaster()->getSize(), rects, m_strokes,
@@ -1323,7 +1501,8 @@ void RasterSelection::pasteSelection() {
0.0 - clipImage.height() / 2, clipImage.width() / 2,
clipImage.height() / 2));
- riData = qimageData;
+ riData = qimageData.get();
+ transferredData = std::move(qimageData);
}
if (!riData) return;
diff --git a/toonz/sources/tnztools/strokeselection.cpp b/toonz/sources/tnztools/strokeselection.cpp
index 189890594..c7170958b 100644
--- a/toonz/sources/tnztools/strokeselection.cpp
+++ b/toonz/sources/tnztools/strokeselection.cpp
@@ -162,6 +162,7 @@ void copyStrokesWithoutUndo(TVectorImageP image, std::set<int> &indexes) {
QClipboard *clipboard = QApplication::clipboard();
StrokesData *data = new StrokesData();
data->setImage(image, indexes);
+ data->setClipboardFormats();
clipboard->setMimeData(data, QClipboard::Clipboard);
}
@@ -173,6 +174,11 @@ bool pasteStrokesWithoutUndo(TVectorImageP image, std::set<int> &outIndexes,
QClipboard *clipboard = QApplication::clipboard();
const StrokesData *stData =
dynamic_cast<const StrokesData *>(clipboard->mimeData());
+ std::unique_ptr<StrokesData> transferredData;
+ if (!stData) {
+ transferredData.reset(StrokesData::fromClipboard(clipboard->mimeData()));
+ stData = transferredData.get();
+ }
const ToonzImageData *tiData =
dynamic_cast<const ToonzImageData *>(clipboard->mimeData());
const FullColorImageData *fciData =
@@ -916,6 +922,12 @@ void StrokeSelection::paste() {
if (TTool::getApplication()->getCurrentObject()->isSpline()) {
const StrokesData *stData = dynamic_cast<const StrokesData *>(
QApplication::clipboard()->mimeData());
+ std::unique_ptr<StrokesData> transferredData;
+ if (!stData) {
+ transferredData.reset(
+ StrokesData::fromClipboard(QApplication::clipboard()->mimeData()));
+ stData = transferredData.get();
+ }
if (!stData) return;
TVectorImageP splineImg = tool->getImage(true);
TVectorImageP img = stData->m_image;
diff --git a/toonz/sources/toonz/cellselection.cpp b/toonz/sources/toonz/cellselection.cpp
index dbcceb47b..483d26fb4 100644
--- a/toonz/sources/toonz/cellselection.cpp
+++ b/toonz/sources/toonz/cellselection.cpp
@@ -1830,10 +1830,12 @@ static void pasteRasterImageInCell(int row, int col,
//-----------------------------------------------------------------------------
// Choose pasting behavior by preference option
void TCellSelection::doPaste() {
- if (Preferences::instance()->getPasteCellsBehavior() ==
- 0) // insert paste whole contents of copied cells
+ // The numbers-only preference applies to copied cells, not drawing
+ // selections or images on the clipboard.
+ if (Preferences::instance()->getPasteCellsBehavior() == 0 ||
+ !dynamic_cast<const TCellData *>(QApplication::clipboard()->mimeData()))
pasteCells();
- else // overwrite paste numbers, consistent with QuickChecker
+ else
overwritePasteNumbers();
}
@@ -1844,6 +1846,12 @@ void TCellSelection::pasteCells() {
getSelectedCells(r0, c0, r1, c1);
QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
+ const StrokesData *strokesData = dynamic_cast<const StrokesData *>(mimeData);
+ std::unique_ptr<StrokesData> transferredStrokes;
+ if (!strokesData) {
+ transferredStrokes.reset(StrokesData::fromClipboard(mimeData));
+ strokesData = transferredStrokes.get();
+ }
TXsheet *xsh = TApp::instance()->getCurrentXsheet()->getXsheet();
XsheetViewer *viewer = TApp::instance()->getCurrentXsheetViewer();
ToolHandle *toolHandle = TApp::instance()->getCurrentTool();
@@ -2012,8 +2020,7 @@ void TCellSelection::pasteCells() {
TUndoManager::manager()->add(
new PasteDrawingsInCellUndo(level, frameIds, r0, c0));
}
- if (const StrokesData *strokesData =
- dynamic_cast<const StrokesData *>(mimeData)) {
+ if (strokesData) {
if (isEmpty()) // If the cell selection is empty, return.
return;
@@ -2078,7 +2085,9 @@ void TCellSelection::pasteCells() {
// See if the clipboard contains rasterData
const RasterImageData *rasterImageData =
dynamic_cast<const RasterImageData *>(mimeData);
- if (rasterImageData || clipImage.height() > 0) {
+ // StrokesData also carries an image for other applications. Do not paste
+ // that preview as a second raster drawing after pasting the vector strokes.
+ if (!strokesData && (rasterImageData || clipImage.height() > 0)) {
if (isEmpty()) return;
// Prevent pasting raster images into the camera column (c0 < 0)
if (c0 < 0) {
@@ -2217,7 +2226,7 @@ void TCellSelection::pasteCells() {
pasteRasterImageInCell(r0, c0, rasterImageData, newLevel);
} // end of full raster stuff
- } // end of raster stuff
+ } // end of raster stuff
if (!initUndo) {
DVGui::error(QObject::tr(
"It is not possible to paste data: there is nothing to paste."));
@@ -3320,6 +3329,9 @@ void TCellSelection::dPasteCells() {
TXsheet *xsh = TApp::instance()->getCurrentXsheet()->getXsheet();
QClipboard *clipboard = QApplication::clipboard();
const QMimeData *mimeData = clipboard->mimeData();
+ std::unique_ptr<StrokesData> transferredStrokes;
+ if (!dynamic_cast<const StrokesData *>(mimeData))
+ transferredStrokes.reset(StrokesData::fromClipboard(mimeData));
if (DYNAMIC_CAST(TCellData, cellData, mimeData)) {
if (!cellData->canChange(xsh, c0)) {
TUndoManager::manager()->endBlock();
@@ -3341,7 +3353,8 @@ void TCellSelection::dPasteCells() {
for (int i = 0; i < frameIds.size(); ++i)
createNewDrawing(xsh, r0 + i, c0, level->getType());
}
- } else if (DYNAMIC_CAST(StrokesData, strokesData, mimeData)) {
+ } else if (dynamic_cast<const StrokesData *>(mimeData) ||
+ transferredStrokes) {
createNewDrawing(xsh, r0, c0, PLI_XSHLEVEL);
} else if (DYNAMIC_CAST(ToonzImageData, toonzImageData, mimeData)) {
createNewDrawing(xsh, r0, c0, TZP_XSHLEVEL);
diff --git a/toonz/sources/toonzqt/rasterimagedata.cpp b/toonz/sources/toonzqt/rasterimagedata.cpp
index 96cdd4d20..d19e3bd8c 100644
--- a/toonz/sources/toonzqt/rasterimagedata.cpp
+++ b/toonz/sources/toonzqt/rasterimagedata.cpp
@@ -77,6 +77,11 @@ ToonzImageData::ToonzImageData(const ToonzImageData &src)
m_transformation = src.m_transformation;
m_originalStrokes = src.m_originalStrokes;
m_dim = src.m_dim;
+ setImageData(src.imageData());
+ for (const QString &format : src.formats()) {
+ if (format != "application/x-qt-image")
+ QMimeData::setData(format, src.data(format));
+ }
assert(m_palette);
}
@@ -219,6 +224,11 @@ FullColorImageData::FullColorImageData(const FullColorImageData &src)
m_transformation = src.m_transformation;
m_originalStrokes = src.m_originalStrokes;
m_dim = src.m_dim;
+ setImageData(src.imageData());
+ for (const QString &format : src.formats()) {
+ if (format != "application/x-qt-image")
+ QMimeData::setData(format, src.data(format));
+ }
}
//-------------------------------------------------------------------
diff --git a/toonz/sources/toonzqt/strokesdata.cpp b/toonz/sources/toonzqt/strokesdata.cpp
index 4e2577c9a..cfc9dbd2e 100644
--- a/toonz/sources/toonzqt/strokesdata.cpp
+++ b/toonz/sources/toonzqt/strokesdata.cpp
@@ -8,9 +8,135 @@
#include "toonz/toonzimageutils.h"
#include "toonz/trasterimageutils.h"
#include "toonz/stage.h"
+#include "toonzqt/gutil.h"
+#include "tlevel_io.h"
+
+#include <QCryptographicHash>
+#include <QFile>
+#include <QTemporaryDir>
+
+#include <cstring>
using namespace std;
+namespace {
+const char *const VectorClipboardFormat =
+ "application/x-opentoonz-vector-selection-v2";
+const char VectorClipboardMagic[] = {'O', 'T', 'V', '2'};
+constexpr int VectorClipboardHeaderSize = 4 + 4 + 32;
+constexpr int MaxVectorClipboardSize = 64 * 1024 * 1024;
+
+QByteArray makeVectorClipboardPayload(const QByteArray &pliData) {
+ QByteArray payload;
+ payload.reserve(VectorClipboardHeaderSize + pliData.size());
+ payload.append(VectorClipboardMagic, 4);
+ const quint32 size = static_cast<quint32>(pliData.size());
+ payload.append(static_cast<char>((size >> 24) & 0xff));
+ payload.append(static_cast<char>((size >> 16) & 0xff));
+ payload.append(static_cast<char>((size >> 8) & 0xff));
+ payload.append(static_cast<char>(size & 0xff));
+ payload.append(QCryptographicHash::hash(pliData, QCryptographicHash::Sha256));
+ payload.append(pliData);
+ return payload;
+}
+
+QByteArray vectorPliData(const QByteArray &payload) {
+ if (payload.size() < VectorClipboardHeaderSize ||
+ memcmp(payload.constData(), VectorClipboardMagic, 4) != 0)
+ return QByteArray();
+
+ const unsigned char *data =
+ reinterpret_cast<const unsigned char *>(payload.constData());
+ const quint32 size = (static_cast<quint32>(data[4]) << 24) |
+ (static_cast<quint32>(data[5]) << 16) |
+ (static_cast<quint32>(data[6]) << 8) |
+ static_cast<quint32>(data[7]);
+ if (size == 0 || size > MaxVectorClipboardSize ||
+ payload.size() != VectorClipboardHeaderSize + static_cast<int>(size))
+ return QByteArray();
+
+ const QByteArray pliData = payload.mid(VectorClipboardHeaderSize);
+ const QByteArray expectedHash = payload.mid(8, 32);
+ if (QCryptographicHash::hash(pliData, QCryptographicHash::Sha256) !=
+ expectedHash)
+ return QByteArray();
+ return pliData;
+}
+} // namespace
+
+void StrokesData::setClipboardFormats() {
+ if (!m_image || m_image->getStrokeCount() == 0) return;
+ try {
+ TRaster32P raster = m_image->render(false);
+ if (raster) setImageData(rasterToQImage(raster).copy());
+ } catch (...) {
+ // Native copy remains available when a rendering context cannot be made.
+ }
+
+ QTemporaryDir dir;
+ if (!dir.isValid()) return;
+ QString path = dir.filePath("selection.pli");
+ try {
+ // A split selection can retain internal state from its source image that
+ // is not suitable for a standalone PLI. Rebuild it just as the custom
+ // vector-brush exporter does before handing it to the PLI writer.
+ TPaletteP palette = new TPalette();
+ TVectorImageP sourceCopy = m_image->clone();
+ TVectorImageP transferImage = new TVectorImage();
+ transferImage->setPalette(palette.getPointer());
+ transferImage->setAutocloseTolerance(m_image->getAutocloseTolerance());
+ transferImage->mergeImage(sourceCopy, TAffine());
+ transferImage->findRegions();
+ if (transferImage->getStrokeCount() == 0) return;
+
+ TLevelP level = new TLevel();
+ level->setPalette(palette.getPointer());
+ level->setFrame(TFrameId(1), transferImage);
+ TLevelWriterP writer(TFilePath(path.toStdWString()));
+ writer->save(level);
+ writer = TLevelWriterP(); // The PLI writer finishes on destruction.
+ QFile file(path);
+ if (file.open(QIODevice::ReadOnly)) {
+ const QByteArray pliData = file.readAll();
+ if (!pliData.isEmpty() && pliData.size() <= MaxVectorClipboardSize)
+ QMimeData::setData(VectorClipboardFormat,
+ makeVectorClipboardPayload(pliData));
+ }
+ } catch (...) {
+ // The image representation and in-process vector data still work.
+ }
+}
+
+StrokesData *StrokesData::fromClipboard(const QMimeData *mime) {
+ if (!mime || !mime->hasFormat(VectorClipboardFormat)) return nullptr;
+ const QByteArray bytes = vectorPliData(mime->data(VectorClipboardFormat));
+ if (bytes.isEmpty()) return nullptr;
+ QTemporaryDir dir;
+ if (!dir.isValid()) return nullptr;
+ QString path = dir.filePath("selection.pli");
+ QFile file(path);
+ if (!file.open(QIODevice::WriteOnly) || file.write(bytes) != bytes.size())
+ return nullptr;
+ file.close();
+ try {
+ TLevelReaderP reader(TFilePath(path.toStdWString()));
+ // PLI frame readers need loadInfo() to initialize the stream and palette.
+ TLevelP level = reader->loadInfo();
+ if (!level || level->getFrameCount() != 1) return nullptr;
+ TImageReaderP frame = reader->getFrameReader(TFrameId(1));
+ TImageP loaded = frame ? frame->load() : TImageP();
+ TVectorImageP image = loaded;
+ if (!image || image->getStrokeCount() == 0 || !level->getPalette() ||
+ level->getPalette()->getPageCount() == 0 ||
+ level->getPalette()->getStyleCount() == 0)
+ return nullptr;
+ image->setPalette(level->getPalette());
+ return new StrokesData(image.getPointer());
+ } catch (...) {
+ return nullptr;
+ }
+}
+
//=============================================================================
namespace {
//-----------------------------------------------------------------------------