OpenToonz - An open-source full-featured 2D animation creation software
C
Fix WRAPPER_STD_METHODS macro and migrate Level class to QRegularExpression
andeoned73139266ed
diff --git a/toonz/sources/include/toonz/scriptbinding_level.h b/toonz/sources/include/toonz/scriptbinding_level.hindex 777b9c368..545e4f333 100644--- a/toonz/sources/include/toonz/scriptbinding_level.h+++ b/toonz/sources/include/toonz/scriptbinding_level.h@@ -6,13 +6,16 @@#include "toonz/scriptbinding.h"#include "toonz/scriptbinding_image.h"+// Forward declarationsclass ToonzScene;class TXshSimpleLevel;+class TFrameId;namespace TScriptBinding {class DVAPI Level final : public Wrapper {Q_OBJECT+TXshSimpleLevel *m_sl;ToonzScene *m_scene;bool m_sceneOwner;@@ -20,17 +23,13 @@ class DVAPI Level final : public Wrapper {public:Level();- Level(TXshSimpleLevel *);- ~Level();+ explicit Level(TXshSimpleLevel *sl);+ ~Level() override;- WRAPPER_STD_METHODS(Image)+ WRAPPER_STD_METHODS(Level)Q_INVOKABLE QScriptValue toString();- // QScriptValue toScriptValue(QScriptEngine *engine) { return create(engine,- // this); }- // static QScriptValue toScriptValue(QScriptEngine *engine, TXshSimpleLevel- // *sl) { return (new Level(sl))->toScriptValue(engine); }-+ // PropertiesQ_PROPERTY(QString type READ getType)QString getType() const;@@ -43,30 +42,37 @@ public:Q_PROPERTY(QScriptValue path READ getPath WRITE setPath)QScriptValue getPath() const;- void setPath(const QScriptValue &name);+ void setPath(const QScriptValue &pathArg);+ // Frame operationsQ_INVOKABLE QScriptValue getFrame(const QScriptValue &fid);Q_INVOKABLE QScriptValue getFrameByIndex(const QScriptValue &index);Q_INVOKABLE QScriptValue setFrame(const QScriptValue &fid,const QScriptValue &image);+ // Level operationsQ_INVOKABLE QScriptValue getFrameIds();-Q_INVOKABLE QScriptValue load(const QScriptValue &fp);Q_INVOKABLE QScriptValue save(const QScriptValue &fp);+ // Internal helper methodsvoid getFrameIds(QList<TFrameId> &fids);TImageP getImg(const TFrameId &fid);-int setFrame(const TFrameId &fid, const TImageP &img);+ // Accessor for underlying simple levelTXshSimpleLevel *getSimpleLevel() const { return m_sl; }+ // Static helper for parsing frame IDsstatic TFrameId getFid(const QScriptValue &arg, QString &err);++private:+ Q_DISABLE_COPY(Level)};} // namespace TScriptBinding+// Declare meta-type for script bindingQ_DECLARE_METATYPE(TScriptBinding::Level *)-#endif+#endif // SCRIPTBINDING_LEVEL_Hdiff --git a/toonz/sources/toonzlib/scriptbinding_level.cpp b/toonz/sources/toonzlib/scriptbinding_level.cppindex f5dade6fe..128efc25b 100644--- a/toonz/sources/toonzlib/scriptbinding_level.cpp+++ b/toonz/sources/toonzlib/scriptbinding_level.cpp@@ -1,13 +1,23 @@-#include "toonz/scriptbinding_level.h"++// Toonz script bindings#include "toonz/scriptbinding_files.h"-#include <QScriptEngine>-#include "timage_io.h"-#include "tlevel_io.h"-#include "tlevel.h"++// TnzLib includes#include "toonz/tcenterlinevectorizer.h"#include "toonz/tcamera.h"+#include "toonz/stage.h"+#include "toonz/txshleveltypes.h"+#include "toonz/levelproperties.h"+#include "toonz/toonzscene.h"+#include "toonz/txshsimplelevel.h"+#include "toonz/levelset.h"++// TnzCore / TnzBase includes+#include "tlevel_io.h"+#include "tlevel.h"+#include "timage_io.h"#include "trop.h"#include "trasterimage.h"#include "ttoonzimage.h"@@ -16,21 +26,18 @@#include "tofflinegl.h"#include "tvectorrenderdata.h"#include "tgeometry.h"-#include "toonz/stage.h"-#include "toonz/txshleveltypes.h"-#include "toonz/levelproperties.h"-#include "toonz/toonzscene.h"-#include "toonz/txshsimplelevel.h"-#include "toonz/levelset.h"#include "tfiletype.h"#include "tsystem.h"-#include <QRegExp>++// Qt includes+#include <QScriptEngine>+#include <QRegularExpression>#include <QColor>namespace TScriptBinding {Level::Level()- : m_sl(0)+ : m_sl(nullptr), m_type(NO_XSHLEVEL), m_scene(new ToonzScene()), m_sceneOwner(true) {}@@ -40,55 +47,72 @@ Level::Level(TXshSimpleLevel *sl), m_type(sl->getType()), m_scene(sl->getScene()), m_sceneOwner(false) {- sl->addRef();+ if (m_sl) {+ m_sl->addRef();+ }}Level::~Level() {- if (m_sceneOwner) delete m_scene;- if (m_sl) m_sl->release();+ if (m_sceneOwner) {+ delete m_scene;+ m_scene = nullptr;+ }+ if (m_sl) {+ m_sl->release();+ m_sl = nullptr;+ }}QScriptValue Level::ctor(QScriptContext *context, QScriptEngine *engine) {Level *level = new Level();QScriptValue obj = engine->newQObject(level, QScriptEngine::AutoOwnership);+if (context->argumentCount() == 1) {return obj.property("load").call(obj, context->argumentsObject());}+return obj;}QScriptValue Level::toString() {- QString info = "(";- QString comma = "";+ QString info = "(";+ QString comma;+if (getName() != "") {info.append(comma).append(getName());comma = ", ";}+info.append(comma).append(tr("%1 frames").arg(getFrameCount()));info.append(")");- if (m_type == PLI_XSHLEVEL)++ switch (m_type) {+ case PLI_XSHLEVEL:return QString("Vector level %1").arg(info);- else if (m_type == TZP_XSHLEVEL)+ case TZP_XSHLEVEL:return QString("Toonz level %1").arg(info);- else if (m_type == NO_XSHLEVEL)+ case NO_XSHLEVEL:return QString("Empty level");- else if (m_type == OVL_XSHLEVEL)+ case OVL_XSHLEVEL:return QString("Raster level %1").arg(info);- else+ default:return QString("Level %1").arg(info);+ }}QString Level::getType() const {- if (m_type == NO_XSHLEVEL)+ switch (m_type) {+ case NO_XSHLEVEL:return "Empty";- else if (m_type == PLI_XSHLEVEL)+ case PLI_XSHLEVEL:return "Vector";- else if (m_type == TZP_XSHLEVEL)+ case TZP_XSHLEVEL:return "ToonzRaster";- else if (m_type == OVL_XSHLEVEL)+ case OVL_XSHLEVEL:return "Raster";- else+ default:return "Unknown";+ }}int Level::getFrameCount() const { return m_sl ? m_sl->getFrameCount() : 0; }@@ -98,73 +122,90 @@ QString Level::getName() const {}void Level::setName(const QString &name) {- if (m_sl) m_sl->setName(name.toStdWString());+ if (m_sl) {+ m_sl->setName(name.toStdWString());+ }}QScriptValue Level::getPath() const {if (m_sl) {FilePath *result = new FilePath(m_sl->getPath());return result->create<FilePath>(engine());- } else- return QScriptValue();- return m_sl ? QString::fromStdWString(m_sl->getName()) : "";+ }+ return QScriptValue();}void Level::setPath(const QScriptValue &pathArg) {+ if (!m_sl) {+ context()->throwError(tr("Cannot set path on empty level"));+ return;+ }+TFilePath fp;FilePath *filePath = qscriptvalue_cast<FilePath *>(pathArg);- if (filePath)++ if (filePath) {fp = filePath->getToonzFilePath();- else if (pathArg.isString())+ } else if (pathArg.isString()) {fp = TFilePath(pathArg.toString().toStdString());- else+ } else {context()->throwError(tr("Bad argument (%1). It should be FilePath or string").arg(pathArg.toString()));- if (m_sl) {- m_sl->setPath(fp);- try {- m_sl->load();- } catch (...) {- context()->throwError(- tr("Exception loading level (%1)")- .arg(QString::fromStdWString(fp.getWideString())));- }+ return;+ }++ m_sl->setPath(fp);+ try {+ m_sl->load();+ } catch (...) {+ context()->throwError(+ tr("Exception loading level (%1)")+ .arg(QString::fromStdWString(fp.getWideString())));}}QScriptValue Level::load(const QScriptValue &fpArg) {+ // Clean up existing level if anyif (m_sl) {- m_scene->getLevelSet()->removeLevel(m_sl, true);+ if (m_scene) {+ m_scene->getLevelSet()->removeLevel(m_sl, true);+ }m_sl->release();- m_sl = 0;+ m_sl = nullptr;}- // get the path+ // Get the pathTFilePath fp;QScriptValue err = checkFilePath(context(), fpArg, fp);- if (err.isError()) return err;+ if (err.isError()) {+ return err;+ }+QString fpStr = fpArg.toString();try {if (!TSystem::doesExistFileOrLevel(fp)) {return context()->throwError(tr("File %1 doesn't exist").arg(fpStr));}+TFileType::Type fileType = TFileType::getInfo(fp);- if (TFileType::isVector(fileType))+ if (TFileType::isVector(fileType)) {m_type = PLI_XSHLEVEL;- else if (0 != (fileType & TFileType::CMAPPED_IMAGE))+ } else if (0 != (fileType & TFileType::CMAPPED_IMAGE)) {m_type = TZP_XSHLEVEL;- else if (0 != (fileType & TFileType::RASTER_IMAGE))+ } else if (0 != (fileType & TFileType::RASTER_IMAGE)) {m_type = OVL_XSHLEVEL;- else {+ } else {return context()->throwError(tr("File %1 is unsupported").arg(fpStr));}+TXshLevel *xl = m_scene->loadLevel(fp);if (xl) {m_sl = xl->getSimpleLevel();m_sl->addRef();}+return context()->thisObject();} catch (...) {return context()->throwError(tr("Exception reading %1").arg(fpStr));@@ -176,13 +217,16 @@ QScriptValue Level::save(const QScriptValue &fpArg) {return context()->throwError(tr("Can't save an empty level"));}- // get the path+ // Get the pathTFilePath fp;QScriptValue err = checkFilePath(context(), fpArg, fp);- if (err.isError()) return err;+ if (err.isError()) {+ return err;+ }+QString fpStr = fpArg.toString();- // handle conversion (if it is needed and possible)+ // Handle conversion (if it is needed and possible)TFileType::Type fileType = TFileType::getInfo(fp);bool isCompatible = false;@@ -193,98 +237,113 @@ QScriptValue Level::save(const QScriptValue &fpArg) {} else if (fileType & TFileType::CMAPPED_IMAGE) {if (m_sl->getType() == TZP_XSHLEVEL) isCompatible = true;} else {- return context()->throwError(tr("Unrecognized file type :").arg(fpStr));+ return context()->throwError(tr("Unrecognized file type: %1").arg(fpStr));}+if (!isCompatible) {return context()->throwError(- tr("Can't save a %1 level to this file type : %2")- .arg(getType())- .arg(fpStr));+ tr("Can't save a %1 level to this file type: %2")+ .arg(getType(), fpStr));}try {m_sl->save(fp);- } catch (TSystemException se) {+ } catch (TSystemException &se) {return context()->throwError(tr("Exception writing %1").arg(QString::fromStdWString(se.getMessage())));}+return context()->thisObject();}TFrameId Level::getFid(const QScriptValue &arg, QString &err) {if (arg.isNumber() || arg.isString()) {QString s = arg.toString();- QRegExp re("(-?\\d+)(\\w?)");- if (re.exactMatch(s)) {- int d = re.cap(1).toInt();- QString c = re.cap(2);+ QRegularExpression re(R"(^(-?\d+)(\w?)$)");+ QRegularExpressionMatch match = re.match(s);++ if (match.hasMatch()) {+ int d = match.captured(1).toInt();+ QString c = match.captured(2);TFrameId fid;- if (c.length() == 1)- fid = TFrameId(d, c[0].unicode());- else++ if (c.length() == 1) {+ fid = TFrameId(d, static_cast<wchar_t>(c[0].unicode()));+ } else {fid = TFrameId(d);- err = "";+ }++ err.clear();return fid;}}+err = QObject::tr("Argument '%1' does not look like a FrameId").arg(arg.toString());return TFrameId();}TImageP Level::getImg(const TFrameId &fid) {- if (m_sl)- return m_sl->getFrame(fid, false);- else- return TImageP();+ return m_sl ? m_sl->getFrame(fid, false) : TImageP();}QScriptValue Level::getFrame(const QScriptValue &fidArg) {- if (getFrameCount() == 0)+ if (getFrameCount() == 0) {return context()->throwError("An empty level has no frames");+ }+QString err;TFrameId fid = getFid(fidArg, err);- if (err != "") return context()->throwError(err);+ if (!err.isEmpty()) {+ return context()->throwError(err);+ }TImageP content = m_sl->getFrame(fid, false);if (content) {Image *img = new Image(content.getPointer());return create(img);- } else {- return QScriptValue();}++ return QScriptValue();}QScriptValue Level::getFrameByIndex(const QScriptValue &indexArg) {- if (getFrameCount() == 0)+ if (getFrameCount() == 0) {return context()->throwError("An empty level has no frames");+ }+if (!indexArg.isNumber()) {return context()->throwError(tr("frame index (%1) must be a number").arg(indexArg.toString()));}+int index = indexArg.toInteger();if (index < 0 || index >= getFrameCount()) {return context()->throwError(tr("frame index (%1) is out of range (0-%2)").arg(index).arg(getFrameCount() - 1));}+TFrameId fid = m_sl->index2fid(index);TImageP content = m_sl->getFrame(fid, false);+if (content) {Image *img = new Image(content.getPointer());return create(img);- } else {- return QScriptValue();}++ return QScriptValue();}-// TODO: chiamare setFrame(const TFrameId &fid, const TImageP &img)QScriptValue Level::setFrame(const QScriptValue &fidArg,const QScriptValue &imageArg) {QString err;TFrameId fid = getFid(fidArg, err);- if (err != "") return context()->throwError(err);+ if (!err.isEmpty()) {+ return context()->throwError(err);+ }+Image *img = qscriptvalue_cast<Image *>(imageArg);if (!img) {return context()->throwError(@@ -293,40 +352,46 @@ QScriptValue Level::setFrame(const QScriptValue &fidArg,QString imgType = img->getType();int levelType = NO_XSHLEVEL;- if (imgType == "ToonzRaster")++ if (imgType == "ToonzRaster") {levelType = TZP_XSHLEVEL;- else if (imgType == "Raster")+ } else if (imgType == "Raster") {levelType = OVL_XSHLEVEL;- else if (imgType == "Vector")+ } else if (imgType == "Vector") {levelType = PLI_XSHLEVEL;- else {+ } else {return context()->throwError(- tr("can not insert a %1 image into a level").arg(imgType));+ tr("cannot insert a %1 image into a level").arg(imgType));}+ // Handle empty level caseif (m_type == NO_XSHLEVEL) {m_type = levelType;TXshLevel *xl = m_scene->createNewLevel(levelType);m_sl = xl->getSimpleLevel();m_sl->addRef();m_sl->setPalette(img->getImg()->getPalette());+if (levelType != PLI_XSHLEVEL) {LevelProperties *lprop = m_sl->getProperties();lprop->setDpiPolicy(LevelProperties::DP_ImageDpi);+int xres = img->getWidth();int yres = img->getHeight();double dpi = img->getDpi();+lprop->setDpi(dpi);lprop->setImageDpi(TPointD(dpi, dpi));lprop->setImageRes(TDimension(xres, yres));- // lprop->setHasAlpha(true);}} else if (m_type != levelType) {- return context()->throwError(tr("can not insert a %1 image to a %2 level")- .arg(imgType)- .arg(getType()));+ return context()->throwError(+ tr("cannot insert a %1 image to a %2 level").arg(imgType, getType()));+ }++ if (m_sl->getFrameCount() == 0) {+ m_sl->setPalette(img->getImg()->getPalette());}- if (m_sl->getFrameCount() == 0) m_sl->setPalette(img->getImg()->getPalette());m_sl->setFrame(fid, img->getImg());m_sl->setDirtyFlag(true);@@ -336,22 +401,25 @@ QScriptValue Level::setFrame(const QScriptValue &fidArg,QScriptValue Level::getFrameIds() {QList<TFrameId> fids;getFrameIds(fids);+QScriptValue result = engine()->newArray();quint32 index = 0;+for (const TFrameId &fid : fids) {QString fidStr = QString::fromStdString(fid.expand());result.setProperty(index++, fidStr);}+return result;}void Level::getFrameIds(QList<TFrameId> &result) {- if (getFrameCount() > 0) {+ if (getFrameCount() > 0 && m_sl) {std::vector<TFrameId> fids;m_sl->getFids(fids);- for (std::vector<TFrameId>::iterator it = fids.begin(); it != fids.end();- ++it) {- result.append(*it);++ for (const TFrameId &fid : fids) {+ result.append(fid);}}}@@ -359,27 +427,32 @@ void Level::getFrameIds(QList<TFrameId> &result) {int Level::setFrame(const TFrameId &fid, const TImageP &img) {TImage::Type imgType = img->getType();int levelType = NO_XSHLEVEL;- if (imgType == TImage::TOONZ_RASTER)++ if (imgType == TImage::TOONZ_RASTER) {levelType = TZP_XSHLEVEL;- else if (imgType == TImage::RASTER)+ } else if (imgType == TImage::RASTER) {levelType = OVL_XSHLEVEL;- else if (imgType == TImage::VECTOR)+ } else if (imgType == TImage::VECTOR) {levelType = PLI_XSHLEVEL;- else {+ } else {return -1;}+ // Handle empty level caseif (m_type == NO_XSHLEVEL) {m_type = levelType;TXshLevel *xl = m_scene->createNewLevel(levelType);m_sl = xl->getSimpleLevel();m_sl->addRef();m_sl->setPalette(img->getPalette());+if (levelType != PLI_XSHLEVEL) {LevelProperties *lprop = m_sl->getProperties();lprop->setDpiPolicy(LevelProperties::DP_ImageDpi);+int xres = 0, yres = 0;double dpix = 0, dpiy = 0;+if (TRasterImageP ri = img) {if (ri->getRaster()) {TDimension size = ri->getRaster()->getSize();@@ -389,21 +462,25 @@ int Level::setFrame(const TFrameId &fid, const TImageP &img) {}} else if (TToonzImageP ti = img) {if (ti->getRaster()) {- TDimension size = ri->getRaster()->getSize();+ TDimension size = ti->getRaster()->getSize();xres = size.lx;yres = size.ly;- ri->getDpi(dpix, dpiy);+ ti->getDpi(dpix, dpiy);}}+lprop->setDpi(dpix);lprop->setImageDpi(TPointD(dpix, dpiy));lprop->setImageRes(TDimension(xres, yres));- // lprop->setHasAlpha(true);}} else if (m_type != levelType) {return -2;}- if (m_sl->getFrameCount() == 0) m_sl->setPalette(img->getPalette());++ if (m_sl->getFrameCount() == 0) {+ m_sl->setPalette(img->getPalette());+ }+m_sl->setFrame(fid, img);m_sl->setDirtyFlag(true);return 1;