Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_files.h"
Toshihiro Shimizu 890ddd
#include <qscriptengine></qscriptengine>
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
#include "tlevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcenterlinevectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcamera.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
#include "tvectorrenderdata.h"
Toshihiro Shimizu 890ddd
#include "tgeometry.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshleveltypes.h"
Toshihiro Shimizu 890ddd
#include "toonz/levelproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshsimplelevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/levelset.h"
Toshihiro Shimizu 890ddd
#include "tfiletype.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include <qregexp></qregexp>
Toshihiro Shimizu 890ddd
#include <qcolor></qcolor>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace TScriptBinding {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Level::Level()
Shinya Kitaoka 120a6e
    : m_sl(0)
Shinya Kitaoka 120a6e
    , m_type(NO_XSHLEVEL)
Shinya Kitaoka 120a6e
    , m_scene(new ToonzScene())
Shinya Kitaoka 120a6e
    , m_sceneOwner(true) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Level::Level(TXshSimpleLevel *sl)
Shinya Kitaoka 120a6e
    : m_sl(sl)
Shinya Kitaoka 120a6e
    , m_type(sl->getType())
Shinya Kitaoka 120a6e
    , m_scene(sl->getScene())
Shinya Kitaoka 120a6e
    , m_sceneOwner(false) {
Shinya Kitaoka 120a6e
  sl->addRef();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Level::~Level() {
Shinya Kitaoka 120a6e
  if (m_sceneOwner) delete m_scene;
Shinya Kitaoka 120a6e
  if (m_sl) m_sl->release();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Level::ctor(QScriptContext *context, QScriptEngine *engine) {
Shinya Kitaoka 120a6e
  Level *level     = new Level();
Shinya Kitaoka 120a6e
  QScriptValue obj = engine->newQObject(level, QScriptEngine::AutoOwnership);
Shinya Kitaoka 120a6e
  if (context->argumentCount() == 1) {
Shinya Kitaoka 120a6e
    return obj.property("load").call(obj, context->argumentsObject());
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return obj;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Level::toString() {
Shinya Kitaoka 120a6e
  QString info  = "(";
Shinya Kitaoka 120a6e
  QString comma = "";
Shinya Kitaoka 120a6e
  if (getName() != "") {
Shinya Kitaoka 120a6e
    info.append(comma).append(getName());
Shinya Kitaoka 120a6e
    comma = ", ";
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  info.append(comma).append(tr("%1 frames").arg(getFrameCount()));
Shinya Kitaoka 120a6e
  info.append(")");
Shinya Kitaoka 120a6e
  if (m_type == PLI_XSHLEVEL)
Shinya Kitaoka 120a6e
    return QString("Vector level %1").arg(info);
Shinya Kitaoka 120a6e
  else if (m_type == TZP_XSHLEVEL)
Shinya Kitaoka 120a6e
    return QString("Toonz level %1").arg(info);
Shinya Kitaoka 120a6e
  else if (m_type == NO_XSHLEVEL)
Shinya Kitaoka 120a6e
    return QString("Empty level");
Shinya Kitaoka 120a6e
  else if (m_type == OVL_XSHLEVEL)
Shinya Kitaoka 120a6e
    return QString("Raster level %1").arg(info);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return QString("Level %1").arg(info);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QString Level::getType() const {
Shinya Kitaoka 120a6e
  if (m_type == NO_XSHLEVEL)
Shinya Kitaoka 120a6e
    return "Empty";
Shinya Kitaoka 120a6e
  else if (m_type == PLI_XSHLEVEL)
Shinya Kitaoka 120a6e
    return "Vector";
Shinya Kitaoka 120a6e
  else if (m_type == TZP_XSHLEVEL)
Shinya Kitaoka 120a6e
    return "ToonzRaster";
Shinya Kitaoka 120a6e
  else if (m_type == OVL_XSHLEVEL)
Shinya Kitaoka 120a6e
    return "Raster";
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return "Unknown";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int Level::getFrameCount() const { return m_sl ? m_sl->getFrameCount() : 0; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QString Level::getName() const {
Shinya Kitaoka 120a6e
  return m_sl ? QString::fromStdWString(m_sl->getName()) : "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Level::setName(const QString &name) {
Shinya Kitaoka 120a6e
  if (m_sl) m_sl->setName(name.toStdWString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Level::getPath() const {
Shinya Kitaoka 120a6e
  if (m_sl) {
Shinya Kitaoka 120a6e
    FilePath *result = new FilePath(m_sl->getPath());
Shinya Kitaoka 120a6e
    return result->create<filepath>(engine());</filepath>
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    return QScriptValue();
Shinya Kitaoka 120a6e
  return m_sl ? QString::fromStdWString(m_sl->getName()) : "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Level::setPath(const QScriptValue &pathArg) {
Shinya Kitaoka 120a6e
  TFilePath fp;
Shinya Kitaoka 120a6e
  FilePath *filePath = qscriptvalue_cast<filepath *="">(pathArg);</filepath>
Shinya Kitaoka 120a6e
  if (filePath)
Shinya Kitaoka 120a6e
    fp = filePath->getToonzFilePath();
Shinya Kitaoka 120a6e
  else if (pathArg.isString())
Shinya Kitaoka 120a6e
    fp = TFilePath(pathArg.toString().toStdString());
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    context()->throwError(
Shinya Kitaoka 120a6e
        tr("Bad argument (%1). It should be FilePath or string")
Shinya Kitaoka 120a6e
            .arg(pathArg.toString()));
Shinya Kitaoka 120a6e
  if (m_sl) {
Shinya Kitaoka 120a6e
    m_sl->setPath(fp);
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      m_sl->load();
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      context()->throwError(
Shinya Kitaoka 120a6e
          tr("Exception loading level (%1)")
Shinya Kitaoka 120a6e
              .arg(QString::fromStdWString(fp.getWideString())));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Level::load(const QScriptValue &fpArg) {
Shinya Kitaoka 120a6e
  if (m_sl) {
Shinya Kitaoka 120a6e
    m_scene->getLevelSet()->removeLevel(m_sl, true);
Shinya Kitaoka 120a6e
    m_sl->release();
Shinya Kitaoka 120a6e
    m_sl = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // get the path
Shinya Kitaoka 120a6e
  TFilePath fp;
Shinya Kitaoka 120a6e
  QScriptValue err = checkFilePath(context(), fpArg, fp);
Shinya Kitaoka 120a6e
  if (err.isError()) return err;
Shinya Kitaoka 120a6e
  QString fpStr = fpArg.toString();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    if (!TSystem::doesExistFileOrLevel(fp)) {
Shinya Kitaoka 120a6e
      return context()->throwError(tr("File %1 doesn't exist").arg(fpStr));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    TFileType::Type fileType = TFileType::getInfo(fp);
Shinya Kitaoka 120a6e
    if (TFileType::isVector(fileType))
Shinya Kitaoka 120a6e
      m_type = PLI_XSHLEVEL;
Shinya Kitaoka 120a6e
    else if (0 != (fileType & TFileType::CMAPPED_IMAGE))
Shinya Kitaoka 120a6e
      m_type = TZP_XSHLEVEL;
Shinya Kitaoka 120a6e
    else if (0 != (fileType & TFileType::RASTER_IMAGE))
Shinya Kitaoka 120a6e
      m_type = OVL_XSHLEVEL;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      return context()->throwError(tr("File %1 is unsupported").arg(fpStr));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    TXshLevel *xl = m_scene->loadLevel(fp);
Shinya Kitaoka 120a6e
    if (xl) {
Shinya Kitaoka 120a6e
      m_sl = xl->getSimpleLevel();
Shinya Kitaoka 120a6e
      m_sl->addRef();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    return context()->thisObject();
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    return context()->throwError(tr("Exception reading %1").arg(fpStr));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Level::save(const QScriptValue &fpArg) {
Shinya Kitaoka 120a6e
  if (getFrameCount() == 0) {
Shinya Kitaoka 120a6e
    return context()->throwError(tr("Can't save an empty level"));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // get the path
Shinya Kitaoka 120a6e
  TFilePath fp;
Shinya Kitaoka 120a6e
  QScriptValue err = checkFilePath(context(), fpArg, fp);
Shinya Kitaoka 120a6e
  if (err.isError()) return err;
Shinya Kitaoka 120a6e
  QString fpStr = fpArg.toString();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // handle conversion (if it is needed and possible)
Shinya Kitaoka 120a6e
  TFileType::Type fileType = TFileType::getInfo(fp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool isCompatible = false;
Shinya Kitaoka 120a6e
  if (TFileType::isFullColor(fileType)) {
Shinya Kitaoka 120a6e
    if (m_sl->getType() == OVL_XSHLEVEL) isCompatible = true;
Shinya Kitaoka 120a6e
  } else if (TFileType::isVector(fileType)) {
Shinya Kitaoka 120a6e
    if (m_sl->getType() == PLI_XSHLEVEL) isCompatible = true;
Shinya Kitaoka 120a6e
  } else if (fileType & TFileType::CMAPPED_IMAGE) {
Shinya Kitaoka 120a6e
    if (m_sl->getType() == TZP_XSHLEVEL) isCompatible = true;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    return context()->throwError(tr("Unrecognized file type :").arg(fpStr));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!isCompatible) {
Shinya Kitaoka 120a6e
    return context()->throwError(
Shinya Kitaoka 120a6e
        tr("Can't save a %1 level to this file type : %2")
Shinya Kitaoka 120a6e
            .arg(getType())
Shinya Kitaoka 120a6e
            .arg(fpStr));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    m_sl->save(fp);
Shinya Kitaoka 120a6e
  } catch (TSystemException se) {
Shinya Kitaoka 120a6e
    return context()->throwError(
Shinya Kitaoka 120a6e
        tr("Exception writing %1")
Shinya Kitaoka 120a6e
            .arg(QString::fromStdWString(se.getMessage())));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return context()->thisObject();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFrameId Level::getFid(const QScriptValue &arg, QString &err) {
Shinya Kitaoka 120a6e
  if (arg.isNumber() || arg.isString()) {
Shinya Kitaoka 120a6e
    QString s = arg.toString();
Shinya Kitaoka 120a6e
    QRegExp re("(-?\\d+)(\\w?)");
Shinya Kitaoka 120a6e
    if (re.exactMatch(s)) {
Shinya Kitaoka 120a6e
      int d     = re.cap(1).toInt();
Shinya Kitaoka 120a6e
      QString c = re.cap(2);
Shinya Kitaoka 120a6e
      TFrameId fid;
Shinya Kitaoka 120a6e
      if (c.length() == 1)
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050500
Shinya Kitaoka 120a6e
        fid = TFrameId(d, c[0].unicode());
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
        fid = TFrameId(d, c[0].toAscii());
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        fid = TFrameId(d);
Shinya Kitaoka 120a6e
      err   = "";
Shinya Kitaoka 120a6e
      return fid;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  err = QObject::tr("Argument '%1' does not look like a FrameId")
Shinya Kitaoka 120a6e
            .arg(arg.toString());
Shinya Kitaoka 120a6e
  return TFrameId();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageP Level::getImg(const TFrameId &fid) {
Shinya Kitaoka 120a6e
  if (m_sl)
Shinya Kitaoka 120a6e
    return m_sl->getFrame(fid, false);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return TImageP();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Level::getFrame(const QScriptValue &fidArg) {
Shinya Kitaoka 120a6e
  if (getFrameCount() == 0)
Shinya Kitaoka 120a6e
    return context()->throwError("An empty level has no frames");
Shinya Kitaoka 120a6e
  QString err;
Shinya Kitaoka 120a6e
  TFrameId fid = getFid(fidArg, err);
Shinya Kitaoka 120a6e
  if (err != "") return context()->throwError(err);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TImageP content = m_sl->getFrame(fid, false);
Shinya Kitaoka 120a6e
  if (content) {
Shinya Kitaoka 120a6e
    Image *img = new Image(content.getPointer());
Shinya Kitaoka 120a6e
    return create(img);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    return QScriptValue();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Level::getFrameByIndex(const QScriptValue &indexArg) {
Shinya Kitaoka 120a6e
  if (getFrameCount() == 0)
Shinya Kitaoka 120a6e
    return context()->throwError("An empty level has no frames");
Shinya Kitaoka 120a6e
  if (!indexArg.isNumber()) {
Shinya Kitaoka 120a6e
    return context()->throwError(
Shinya Kitaoka 120a6e
        tr("frame index (%1) must be a number").arg(indexArg.toString()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  int index = indexArg.toInteger();
Shinya Kitaoka 120a6e
  if (index < 0 || index >= getFrameCount()) {
Shinya Kitaoka 120a6e
    return context()->throwError(tr("frame index (%1) is out of range (0-%2)")
Shinya Kitaoka 120a6e
                                     .arg(index)
Shinya Kitaoka 120a6e
                                     .arg(getFrameCount() - 1));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TFrameId fid    = m_sl->index2fid(index);
Shinya Kitaoka 120a6e
  TImageP content = m_sl->getFrame(fid, false);
Shinya Kitaoka 120a6e
  if (content) {
Shinya Kitaoka 120a6e
    Image *img = new Image(content.getPointer());
Shinya Kitaoka 120a6e
    return create(img);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    return QScriptValue();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TODO: chiamare setFrame(const TFrameId &fid, const TImageP &img)
Shinya Kitaoka 120a6e
QScriptValue Level::setFrame(const QScriptValue &fidArg,
Shinya Kitaoka 120a6e
                             const QScriptValue &imageArg) {
Shinya Kitaoka 120a6e
  QString err;
Shinya Kitaoka 120a6e
  TFrameId fid = getFid(fidArg, err);
Shinya Kitaoka 120a6e
  if (err != "") return context()->throwError(err);
Shinya Kitaoka 120a6e
  Image *img = qscriptvalue_cast(imageArg);
Shinya Kitaoka 120a6e
  if (!img) {
Shinya Kitaoka 120a6e
    return context()->throwError(
Shinya Kitaoka 120a6e
        tr("second argument (%1) is not an image").arg(imageArg.toString()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QString imgType = img->getType();
Shinya Kitaoka 120a6e
  int levelType   = NO_XSHLEVEL;
Shinya Kitaoka 120a6e
  if (imgType == "ToonzRaster")
Shinya Kitaoka 120a6e
    levelType = TZP_XSHLEVEL;
Shinya Kitaoka 120a6e
  else if (imgType == "Raster")
Shinya Kitaoka 120a6e
    levelType = OVL_XSHLEVEL;
Shinya Kitaoka 120a6e
  else if (imgType == "Vector")
Shinya Kitaoka 120a6e
    levelType = PLI_XSHLEVEL;
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    return context()->throwError(
Shinya Kitaoka 120a6e
        tr("can not insert a %1 image into a level").arg(imgType));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_type == NO_XSHLEVEL) {
Shinya Kitaoka 120a6e
    m_type        = levelType;
Shinya Kitaoka 120a6e
    TXshLevel *xl = m_scene->createNewLevel(levelType);
Shinya Kitaoka 120a6e
    m_sl          = xl->getSimpleLevel();
Shinya Kitaoka 120a6e
    m_sl->addRef();
Shinya Kitaoka 120a6e
    m_sl->setPalette(img->getImg()->getPalette());
Shinya Kitaoka 120a6e
    if (levelType != PLI_XSHLEVEL) {
Shinya Kitaoka 120a6e
      LevelProperties *lprop = m_sl->getProperties();
Shinya Kitaoka 120a6e
      lprop->setDpiPolicy(LevelProperties::DP_ImageDpi);
Shinya Kitaoka 120a6e
      int xres   = img->getWidth();
Shinya Kitaoka 120a6e
      int yres   = img->getHeight();
Shinya Kitaoka 120a6e
      double dpi = img->getDpi();
Shinya Kitaoka 120a6e
      lprop->setDpi(dpi);
Shinya Kitaoka 120a6e
      lprop->setImageDpi(TPointD(dpi, dpi));
Shinya Kitaoka 120a6e
      lprop->setImageRes(TDimension(xres, yres));
Shinya Kitaoka 120a6e
      // lprop->setHasAlpha(true);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (m_type != levelType) {
Shinya Kitaoka 120a6e
    return context()->throwError(tr("can not insert a %1 image to a %2 level")
Shinya Kitaoka 120a6e
                                     .arg(imgType)
Shinya Kitaoka 120a6e
                                     .arg(getType()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_sl->getFrameCount() == 0) m_sl->setPalette(img->getImg()->getPalette());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_sl->setFrame(fid, img->getImg());
Shinya Kitaoka 120a6e
  m_sl->setDirtyFlag(true);
Shinya Kitaoka 120a6e
  return context()->thisObject();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue Level::getFrameIds() {
Shinya Kitaoka 120a6e
  QList<tframeid> fids;</tframeid>
Shinya Kitaoka 120a6e
  getFrameIds(fids);
Shinya Kitaoka 120a6e
  QScriptValue result = engine()->newArray();
Shinya Kitaoka 120a6e
  quint32 index       = 0;
otakuto dfe1cf
  for (const TFrameId &fid : fids) {
Shinya Kitaoka 120a6e
    QString fidStr = QString::fromStdString(fid.expand());
Shinya Kitaoka 120a6e
    result.setProperty(index++, fidStr);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return result;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Level::getFrameIds(QList<tframeid> &result) {</tframeid>
Shinya Kitaoka 120a6e
  if (getFrameCount() > 0) {
Shinya Kitaoka 120a6e
    std::vector<tframeid> fids;</tframeid>
Shinya Kitaoka 120a6e
    m_sl->getFids(fids);
Shinya Kitaoka 120a6e
    for (std::vector<tframeid>::iterator it = fids.begin(); it != fids.end();</tframeid>
Shinya Kitaoka 120a6e
         ++it) {
Shinya Kitaoka 120a6e
      result.append(*it);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int Level::setFrame(const TFrameId &fid, const TImageP &img) {
Shinya Kitaoka 120a6e
  TImage::Type imgType = img->getType();
Shinya Kitaoka 120a6e
  int levelType        = NO_XSHLEVEL;
Shinya Kitaoka 120a6e
  if (imgType == TImage::TOONZ_RASTER)
Shinya Kitaoka 120a6e
    levelType = TZP_XSHLEVEL;
Shinya Kitaoka 120a6e
  else if (imgType == TImage::RASTER)
Shinya Kitaoka 120a6e
    levelType = OVL_XSHLEVEL;
Shinya Kitaoka 120a6e
  else if (imgType == TImage::VECTOR)
Shinya Kitaoka 120a6e
    levelType = PLI_XSHLEVEL;
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    return -1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_type == NO_XSHLEVEL) {
Shinya Kitaoka 120a6e
    m_type        = levelType;
Shinya Kitaoka 120a6e
    TXshLevel *xl = m_scene->createNewLevel(levelType);
Shinya Kitaoka 120a6e
    m_sl          = xl->getSimpleLevel();
Shinya Kitaoka 120a6e
    m_sl->addRef();
Shinya Kitaoka 120a6e
    m_sl->setPalette(img->getPalette());
Shinya Kitaoka 120a6e
    if (levelType != PLI_XSHLEVEL) {
Shinya Kitaoka 120a6e
      LevelProperties *lprop = m_sl->getProperties();
Shinya Kitaoka 120a6e
      lprop->setDpiPolicy(LevelProperties::DP_ImageDpi);
Shinya Kitaoka 120a6e
      int xres = 0, yres = 0;
Shinya Kitaoka 120a6e
      double dpix = 0, dpiy = 0;
Shinya Kitaoka 120a6e
      if (TRasterImageP ri = img) {
Shinya Kitaoka 120a6e
        if (ri->getRaster()) {
Shinya Kitaoka 120a6e
          TDimension size = ri->getRaster()->getSize();
Shinya Kitaoka 120a6e
          xres            = size.lx;
Shinya Kitaoka 120a6e
          yres            = size.ly;
Shinya Kitaoka 120a6e
          ri->getDpi(dpix, dpiy);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      } else if (TToonzImageP ti = img) {
Shinya Kitaoka 120a6e
        if (ti->getRaster()) {
Shinya Kitaoka 120a6e
          TDimension size = ri->getRaster()->getSize();
Shinya Kitaoka 120a6e
          xres            = size.lx;
Shinya Kitaoka 120a6e
          yres            = size.ly;
Shinya Kitaoka 120a6e
          ri->getDpi(dpix, dpiy);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      lprop->setDpi(dpix);
Shinya Kitaoka 120a6e
      lprop->setImageDpi(TPointD(dpix, dpiy));
Shinya Kitaoka 120a6e
      lprop->setImageRes(TDimension(xres, yres));
Shinya Kitaoka 120a6e
      // lprop->setHasAlpha(true);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (m_type != levelType) {
Shinya Kitaoka 120a6e
    return -2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_sl->getFrameCount() == 0) m_sl->setPalette(img->getPalette());
Shinya Kitaoka 120a6e
  m_sl->setFrame(fid, img);
Shinya Kitaoka 120a6e
  m_sl->setDirtyFlag(true);
Shinya Kitaoka 120a6e
  return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace TScriptBinding