Merge pull request #1369 from bhangart/feature/script-imagecrop-map

Return qet.imageCrop() as {x, y, width, height}
This commit is contained in:
Laurent Trinques
2026-10-07 13:15:48 +02:00
committed by GitHub
3 changed files with 22 additions and 9 deletions
+10 -4
View File
@@ -3541,14 +3541,20 @@ bool QetScriptApi::cropImage(int folioIndex, int imageIndex, int x, int y, int w
/**
@brief QetScriptApi::imageCrop
@return the image's crop rectangle in its original's pixels, as
"x,y,width,height", or an empty string for no such image.
{x, y, width, height} like elementGeometry(), or an empty map for no
such image.
*/
QString QetScriptApi::imageCrop(int folioIndex, int imageIndex) const
QVariantMap QetScriptApi::imageCrop(int folioIndex, int imageIndex) const
{
QVariantMap crop;
const QList<DiagramImageItem *> list = sortedImages(folioIndex);
if (imageIndex < 0 || imageIndex >= list.count()) return QString();
if (imageIndex < 0 || imageIndex >= list.count()) return crop;
const QRect r = list.at(imageIndex)->cropRect();
return QStringLiteral("%1,%2,%3,%4").arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
crop.insert(QStringLiteral("x"), r.x());
crop.insert(QStringLiteral("y"), r.y());
crop.insert(QStringLiteral("width"), r.width());
crop.insert(QStringLiteral("height"), r.height());
return crop;
}
bool QetScriptApi::deleteImage(int folioIndex, int imageIndex)
+1 -1
View File
@@ -543,7 +543,7 @@ class QetScriptApi : public QObject
Q_INVOKABLE bool setImageScale(int folioIndex, int imageIndex, double factor);
Q_INVOKABLE bool setImageRotation(int folioIndex, int imageIndex, double angle);
Q_INVOKABLE bool cropImage(int folioIndex, int imageIndex, int x, int y, int width, int height);
Q_INVOKABLE QString imageCrop(int folioIndex, int imageIndex) const;
Q_INVOKABLE QVariantMap imageCrop(int folioIndex, int imageIndex) const;
Q_INVOKABLE bool deleteImage(int folioIndex, int imageIndex);
Q_INVOKABLE int addPdfPage(int folioIndex, const QString &pdfPath, int pageNumber,
int dpi, double x, double y);
+11 -4
View File
@@ -88,6 +88,13 @@ class tst_imagecropundo : public QObject
if (!m.hasMatch()) return {};
return QImage::fromData(QByteArray::fromBase64(m.captured(1).toLatin1())).size();
}
// qet.imageCrop()'s {x, y, width, height}, as logged by the script.
static QRect rect(const QJsonValue &crop)
{
const QJsonObject o = crop.toObject();
return QRect(o.value("x").toInt(-1), o.value("y").toInt(-1),
o.value("width").toInt(-1), o.value("height").toInt(-1));
}
private slots:
void initTestCase()
@@ -122,16 +129,16 @@ qet.log('PROBE ' + JSON.stringify(r));
const QJsonObject r = run(script, QStringLiteral(QET_EXAMPLES_DIR "/741.qet"));
QVERIFY2(!r.isEmpty(), "the script logged nothing");
QCOMPARE(r.value("full").toString(), QStringLiteral("0,0,40,30"));
QCOMPARE(rect(r.value("full")), QRect(0, 0, 40, 30));
QVERIFY(r.value("cropped_ok").toBool());
QCOMPARE(r.value("cropped").toString(), QStringLiteral("10,5,20,10"));
QCOMPARE(rect(r.value("cropped")), QRect(10, 5, 20, 10));
// Crops that change nothing report it, and push no undo step:
// the undo below still undoes the real crop.
QCOMPARE(r.value("same_ok").toBool(true), false);
QCOMPARE(r.value("empty_ok").toBool(true), false);
QCOMPARE(r.value("outside_ok").toBool(true), false);
QCOMPARE(r.value("undone").toString(), QStringLiteral("0,0,40,30"));
QCOMPARE(r.value("redone").toString(), QStringLiteral("10,5,20,10"));
QCOMPARE(rect(r.value("undone")), QRect(0, 0, 40, 30));
QCOMPARE(rect(r.value("redone")), QRect(10, 5, 20, 10));
const QByteArray undone_xml = read(undone);
QVERIFY(undone_xml.contains("<image "));