Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "celldata.h"
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/txsheet.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshcolumn.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshleveltypes.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshzeraryfxcolumn.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshzeraryfxlevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcolumnfx.h"
Toshihiro Shimizu 890ddd
#include "toonz/fxdag.h"
shun-iwasawa fd55f6
#include "toonz/txshlevelcolumn.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TCellData::TCellData() : m_rowCount(0), m_colCount(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TCellData::TCellData(const TCellData *src)
Shinya Kitaoka 120a6e
    : m_cells(src->m_cells)
Shinya Kitaoka 120a6e
    , m_rowCount(src->m_rowCount)
Shinya Kitaoka 120a6e
    , m_colCount(src->m_colCount) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TCellData::~TCellData() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const TXshCell TCellData::getCell(int row, int col) const {
Shinya Kitaoka 120a6e
  assert(0 <= row && row < m_rowCount);
Shinya Kitaoka 120a6e
  assert(0 <= col && col < m_colCount);
Shinya Kitaoka 120a6e
  return m_cells[row + col * m_rowCount];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// data <- xsh
Shinya Kitaoka 120a6e
void TCellData::setCells(TXsheet *xsh, int r0, int c0, int r1, int c1) {
Shinya Kitaoka 120a6e
  assert(r0 <= r1 && c0 <= c1);
Shinya Kitaoka 120a6e
  m_rowCount = r1 - r0 + 1;
Shinya Kitaoka 120a6e
  m_colCount = c1 - c0 + 1;
Shinya Kitaoka 120a6e
  assert(m_rowCount > 0);
Shinya Kitaoka 120a6e
  assert(m_colCount > 0);
Shinya Kitaoka 120a6e
  m_cells.clear();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_cells.resize(m_rowCount * m_colCount);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int c;
Shinya Kitaoka 120a6e
  for (c = c0; c <= c1; c++) {
Shinya Kitaoka 120a6e
    int index          = c - c0;
Shinya Kitaoka 120a6e
    TXshColumn *column = xsh->getColumn(c);
Shinya Kitaoka 120a6e
    if (!column || column->isEmpty()) continue;
Shinya Kitaoka 120a6e
    xsh->getCells(r0, c, m_rowCount, &m_cells[index * m_rowCount]);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  setText("TCellData");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// data -> xsh
Shinya Kitaoka 120a6e
bool TCellData::getCells(TXsheet *xsh, int r0, int c0, int &r1, int &c1,
shun_iwasawa a174fa
                         bool insert, bool doZeraryClone,
shun_iwasawa a174fa
                         bool skipEmptyCells) const {
Shinya Kitaoka 120a6e
  int c;
Shinya Kitaoka 120a6e
  r1           = r0 + m_rowCount - 1;
Shinya Kitaoka 120a6e
  c1           = c0 + m_colCount - 1;
Shinya Kitaoka 120a6e
  bool cellSet = false;
Shinya Kitaoka 120a6e
  for (c = c0; c < c0 + m_colCount; c++) {
Shinya Kitaoka 120a6e
    TXshColumn *column = xsh->getColumn(c);
Shinya Kitaoka 120a6e
    /*- index:選択範囲左から何番目のカラムか(一番左は0) -*/
Shinya Kitaoka 120a6e
    int index                   = c - c0;
Shinya Kitaoka 120a6e
    std::vector<txshcell> cells = m_cells;</txshcell>
Shinya Kitaoka 120a6e
    bool isColumnEmpty          = true;
Shinya Kitaoka 120a6e
    if (column) {
Shinya Kitaoka 120a6e
      isColumnEmpty = column->isEmpty();
Shinya Kitaoka 120a6e
      /*- 各セルに左上→右下で順に割り振られるIndex -*/
Shinya Kitaoka 120a6e
      int cellIndex = index * m_rowCount;
shun_iwasawa a174fa
      // increment the cellIndex and skip empty cells
shun_iwasawa a174fa
      if (skipEmptyCells) {
shun_iwasawa a174fa
        while (cellIndex < (index + 1) * m_rowCount &&
shun_iwasawa a174fa
               m_cells[cellIndex].isEmpty())
shun_iwasawa a174fa
          ++cellIndex;
shun_iwasawa a174fa
      }
shun_iwasawa a174fa
      // if the cellIndex reaches the end of the selection
Shinya Kitaoka 120a6e
      if ((int)m_cells.size() <= cellIndex)  // Celle vuote.
Shinya Kitaoka 120a6e
        return cellSet;
Shinya Kitaoka 120a6e
      /*- カラムが変更不可なら次のカラムへ -*/
Shinya Kitaoka 120a6e
      if (!canChange(column, index)) continue;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (doZeraryClone && isColumnEmpty) cloneZeraryFx(index, cells);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // inserisco le celle
Shinya Kitaoka 120a6e
    if (insert) xsh->insertCells(r0, c, m_rowCount);
Shinya Kitaoka 120a6e
    cellSet = xsh->setCells(r0, c, m_rowCount, &cells[index * m_rowCount]);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return cellSet;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
shun-iwasawa fd55f6
// Paste only cell numbers.
shun-iwasawa fd55f6
// As a special behavior, enable to copy one column and paste into
shun-iwasawa fd55f6
// multiple columns.
shun-iwasawa fd55f6
shun-iwasawa fd55f6
bool TCellData::getNumbers(TXsheet *xsh, int r0, int c0, int &r1,
shun-iwasawa fd55f6
                           int &c1) const {
shun-iwasawa fd55f6
  r1                  = r0 + m_rowCount - 1;
shun-iwasawa fd55f6
  bool oneToMulti     = m_colCount == 1 && c0 < c1;
shun-iwasawa fd55f6
  if (!oneToMulti) c1 = c0 + m_colCount - 1;
shun-iwasawa fd55f6
shun-iwasawa fd55f6
  bool cellSet = false;
shun-iwasawa fd55f6
shun-iwasawa fd55f6
  for (int c = c0; c <= c1; c++) {
shun-iwasawa fd55f6
    TXshColumn *column = xsh->getColumn(c);
shun-iwasawa fd55f6
    if (!column || column->isEmpty()) continue;
shun-iwasawa fd55f6
    TXshLevelColumn *levelColumn = column->getLevelColumn();
shun-iwasawa fd55f6
    if (!levelColumn) continue;
shun-iwasawa fd55f6
shun-iwasawa fd55f6
    std::vector<txshcell> cells = m_cells;</txshcell>
shun-iwasawa fd55f6
shun-iwasawa fd55f6
    int sourceColIndex  = (oneToMulti) ? 0 : c - c0;
shun-iwasawa fd55f6
    int sourceCellIndex = sourceColIndex * m_rowCount;
shun-iwasawa fd55f6
shun-iwasawa fd55f6
    if (!canChange(column, sourceColIndex)) continue;
shun-iwasawa fd55f6
shun-iwasawa fd55f6
    bool isSet = levelColumn->setNumbers(r0, m_rowCount,
shun-iwasawa fd55f6
                                         &cells[sourceColIndex * m_rowCount]);
shun-iwasawa fd55f6
shun-iwasawa fd55f6
    cellSet = cellSet || isSet;
shun-iwasawa fd55f6
  }
shun-iwasawa fd55f6
  xsh->updateFrameCount();
shun-iwasawa fd55f6
shun-iwasawa fd55f6
  return cellSet;
shun-iwasawa fd55f6
}
shun-iwasawa fd55f6
shun-iwasawa fd55f6
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*-- c0 を起点に、TCellDataの選択範囲のカラム幅分、
Shinya Kitaoka 120a6e
        全てのカラムがcanChangeかどうか調べる。
Toshihiro Shimizu 890ddd
--*/
Shinya Kitaoka 120a6e
bool TCellData::canChange(TXsheet *xsh, int c0) const {
Shinya Kitaoka 120a6e
  int c;
Shinya Kitaoka 120a6e
  for (c = c0; c < c0 + m_colCount; c++) {
Shinya Kitaoka 120a6e
    int index          = c - c0;
Shinya Kitaoka 120a6e
    TXshColumn *column = xsh->getColumn(c);
Shinya Kitaoka 120a6e
    if (!canChange(column, index)) return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TCellData::canChange(TXshColumn *column, int index) const {
Shinya Kitaoka 120a6e
  /*- カラムが無い、又は空のときTrue(編集可) -*/
Shinya Kitaoka 120a6e
  if (!column || column->isEmpty()) return true;
Shinya Kitaoka 120a6e
  /*- カラムがロックされていたらFalse(編集不可) -*/
Shinya Kitaoka 120a6e
  if (column->isLocked()) return false;
Shinya Kitaoka 120a6e
  /*-- 全てのセルがcanSetCellかどうか調べる 今からペーストするセルが、
Shinya Kitaoka 120a6e
          ペースト先のカラムと同種ならばtrueとなる。
Shinya Kitaoka 120a6e
  --*/
Shinya Kitaoka 120a6e
  TXshCellColumn *cellColumn = column->getCellColumn();
Shinya Kitaoka 120a6e
  assert(cellColumn);
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = index * m_rowCount; i < (index * m_rowCount) + m_rowCount; i++)
Shinya Kitaoka 120a6e
    // for(i = 0; i < m_cells.size(); i++)
Shinya Kitaoka 120a6e
    if (!cellColumn->canSetCell(m_cells[i])) return false;
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TCellData::cloneZeraryFx(int index, std::vector<txshcell> &cells) const {</txshcell>
Shinya Kitaoka 120a6e
  int firstNotEmptyIndex = index * m_rowCount;
Shinya Kitaoka 120a6e
  while (firstNotEmptyIndex < (index + 1) * m_rowCount &&
Shinya Kitaoka 120a6e
         m_cells[firstNotEmptyIndex].isEmpty())
Shinya Kitaoka 120a6e
    firstNotEmptyIndex++;
Shinya Kitaoka 120a6e
  if (firstNotEmptyIndex >= (index + 1) * m_rowCount) return;
Shinya Kitaoka 120a6e
  TXshZeraryFxLevel *fxLevel =
Shinya Kitaoka 120a6e
      m_cells[firstNotEmptyIndex].m_level->getZeraryFxLevel();
Shinya Kitaoka 120a6e
  if (!fxLevel) return;
Shinya Kitaoka 120a6e
  TXshZeraryFxColumn *fxColumn = fxLevel->getColumn();
Shinya Kitaoka 120a6e
  assert(fxColumn);
Shinya Kitaoka 120a6e
  TFx *zeraryFx = fxColumn->getZeraryColumnFx()->getZeraryFx();
Shinya Kitaoka 120a6e
  if (zeraryFx) {
Shinya Kitaoka 120a6e
    std::wstring fxName = zeraryFx->getName();
Shinya Kitaoka 120a6e
    TFx *newZeraryFx    = zeraryFx->clone(false);
Shinya Kitaoka 120a6e
    fxColumn->getXsheet()->getFxDag()->assignUniqueId(newZeraryFx);
Shinya Kitaoka 120a6e
    newZeraryFx->setName(fxName);
Shinya Kitaoka 120a6e
    newZeraryFx->linkParams(zeraryFx);
Shinya Kitaoka 120a6e
    TXshZeraryFxLevel *newFxLevel   = new TXshZeraryFxLevel();
Shinya Kitaoka 120a6e
    TXshZeraryFxColumn *newFxColumn = new TXshZeraryFxColumn(0);
Shinya Kitaoka 120a6e
    newFxColumn->getZeraryColumnFx()->setZeraryFx(newZeraryFx);
Shinya Kitaoka 120a6e
    newFxLevel->setColumn(newFxColumn);
shun_iwasawa a174fa
    // replace the zerary fx cells by the new fx
Shinya Kitaoka 120a6e
    int r;
shun_iwasawa a174fa
    for (r     = firstNotEmptyIndex; r < (index + 1) * m_rowCount; r++)
shun_iwasawa a174fa
      cells[r] = TXshCell(newFxLevel, m_cells[r].getFrameId());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}