Skip to content

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

C

Regex migration and C++17 cleanup in `TXshLevelColumn`

andeonb6568d4eba07
diff --git a/toonz/sources/include/toonz/txshlevelcolumn.h b/toonz/sources/include/toonz/txshlevelcolumn.h
index 6b50090de..178d42f29 100644
--- a/toonz/sources/include/toonz/txshlevelcolumn.h
+++ b/toonz/sources/include/toonz/txshlevelcolumn.h
@@ -15,8 +15,7 @@
#define DVVAR DV_IMPORT_VAR
#endif
-//=============================================================================
-// forward declarations
+// Forward declarations
class TLevelColumnFx;
class TXshCell;
class TXshLevel;
@@ -24,82 +23,76 @@ class TXshLevel;
//=============================================================================
//! The TXshLevelColumn class provides a column of levels in xsheet and allows
//! its management.
-/*!Inherits \b TXshCellColumn.
-\n The class defines column of levels getLevelColumn(), more than \b
-TXshCellColumn has
- a pointer to \b TLevelColumnFx getLevelColumnFx() and a \b string to identify
-icon.
- The string is an icon identification helpful to level icon management,
-getIcon()
- updateIcon().
+/*!
+Inherits \b TXshCellColumn.
+
+The class defines column of levels getLevelColumn(), more than \b TXshCellColumn
+has a pointer to \b TLevelColumnFx getLevelColumnFx().
+
+Note: Previously had an icon identification string for level icon management,
+but it has been removed in the current implementation.
*/
//=============================================================================
class DVAPI TXshLevelColumn final : public TXshCellColumn {
PERSIST_DECLARATION(TXshLevelColumn)
- TLevelColumnFx *m_fx;
- std::string m_iconId;
+ TLevelColumnFx *m_fx;
bool m_iconVisible;
public:
- bool isIconVisible() { return m_iconVisible; }
+ // Icon visibility management
+ bool isIconVisible() const { return m_iconVisible; }
void setIconVisible(bool visible) { m_iconVisible = visible; }
- /*!
-Constructs a TXshLevelColumn with default value.
-*/
+ // Construction/Destruction
TXshLevelColumn();
- /*!
-Destroys the TXshLevelColumn object.
-*/
- ~TXshLevelColumn();
+ ~TXshLevelColumn() override;
+ // Type information
TXshColumn::ColumnType getColumnType() const override;
- /*!
-Return true if \b cell is empty or level of \b cell isn't a \b
-TXshZeraryFxLevel.
-*/
+ // Cell validation
bool canSetCell(const TXshCell &cell) const override;
- /*!
-Return \b TXshLevelColumn.
-*/
+ // Column type casting
TXshLevelColumn *getLevelColumn() override { return this; }
- /*!
-Clone column and return a pointer to the new \b TXshColumn cloned.
-*/
+ // Cloning
TXshColumn *clone() const override;
+ // Serialization
void loadData(TIStream &is) override;
void saveData(TOStream &os) override;
- /*!
-Return a pointer to \b TLevelColumnFx \b m_fx.
-*/
+ // Effects access
TLevelColumnFx *getLevelColumnFx() const;
-
- /*!
-Return \b TFx.
-*/
TFx *getFx() const override;
+ // Cell operations
// Used in TCellData::getNumbers
+ // reservedLevel can be nonzero if the preferences option
+ // "LinkColumnNameWithLevel" is ON and the column name is the same as some
+ // level in the scene cast.
bool setNumbers(int row, int rowCount, const TXshCell cells[],
- TXshLevel *reservedLevel);
+ TXshLevel *reservedLevel = nullptr);
+
+ // Disable copying and moving
+ TXshLevelColumn(const TXshLevelColumn &) = delete;
+ TXshLevelColumn &operator=(const TXshLevelColumn &) = delete;
+
+ TXshLevelColumn(TXshLevelColumn &&) = delete;
+ TXshLevelColumn &operator=(TXshLevelColumn &&) = delete;
private:
- // not implemented
- TXshLevelColumn(const TXshLevelColumn &);
- TXshLevelColumn &operator=(const TXshLevelColumn &);
+ // Private helper methods if needed
};
#ifdef _WIN32
template class DV_EXPORT_API TSmartPointerT<TXshLevelColumn>;
#endif
+// Smart pointer typedef for TXshLevelColumn
typedef TSmartPointerT<TXshLevelColumn> TXshLevelColumnP;
-#endif
+#endif // TXSHLEVELCOLUMN_INCLUDED
diff --git a/toonz/sources/toonzlib/txshlevelcolumn.cpp b/toonz/sources/toonzlib/txshlevelcolumn.cpp
index da2a46f29..4b8a1d066 100644
--- a/toonz/sources/toonzlib/txshlevelcolumn.cpp
+++ b/toonz/sources/toonzlib/txshlevelcolumn.cpp
@@ -1,31 +1,43 @@
#include "toonz/txshlevelcolumn.h"
+
+// TnzLib includes
#include "toonz/txshsimplelevel.h"
#include "toonz/txshcell.h"
#include "toonz/tcolumnfxset.h"
#include "toonz/tcolumnfx.h"
#include "toonz/txshleveltypes.h"
+// TnzCore includes
#include "tstream.h"
+// Qt includes
+#include <QRegularExpression>
+
//-----------------------------------------------------------------------------
namespace {
-TFrameId qstringToFrameId(QString str) {
- if (str.isEmpty() || str == "-1")
+TFrameId qstringToFrameId(const QString &str) {
+ if (str.isEmpty() || str == "-1") {
return TFrameId::EMPTY_FRAME;
- else if (str == "-" || str == "-2")
+ } else if (str == "-" || str == "-2") {
return TFrameId::NO_FRAME;
+ }
QString regExpStr = QString("^%1$").arg(TFilePath::fidRegExpStr());
- QRegExp rx(regExpStr);
- int pos = rx.indexIn(str);
- if (pos < 0) return TFrameId();
- if (rx.cap(2).isEmpty())
- return TFrameId(rx.cap(1).toInt());
- else
- return TFrameId(rx.cap(1).toInt(), rx.cap(2));
+ QRegularExpression rx(regExpStr);
+ QRegularExpressionMatch match = rx.match(str);
+
+ if (!match.hasMatch()) {
+ return TFrameId();
+ }
+
+ if (match.captured(2).isEmpty()) {
+ return TFrameId(match.captured(1).toInt());
+ } else {
+ return TFrameId(match.captured(1).toInt(), match.captured(2));
+ }
}
} // namespace
@@ -35,10 +47,7 @@ TFrameId qstringToFrameId(QString str) {
// TXshLevelColumn
TXshLevelColumn::TXshLevelColumn()
- : m_fx(new TLevelColumnFx())
- //, m_iconId("")
- , m_iconVisible(false) {
- // updateIcon();
+ : m_fx(new TLevelColumnFx()), m_iconVisible(false) {
m_fx->addRef();
m_fx->setColumn(this);
}
@@ -46,26 +55,30 @@ TXshLevelColumn::TXshLevelColumn()
//-----------------------------------------------------------------------------
TXshLevelColumn::~TXshLevelColumn() {
- m_fx->setColumn(0);
+ m_fx->setColumn(nullptr);
m_fx->release();
- m_fx = 0;
+ m_fx = nullptr;
}
//-----------------------------------------------------------------------------
TXshColumn::ColumnType TXshLevelColumn::getColumnType() const {
- return eLevelType;
+ return ColumnType::eLevelType;
}
//----------------------------------------------------------------------------
bool TXshLevelColumn::canSetCell(const TXshCell &cell) const {
- if (cell.isEmpty()) return true;
+ if (cell.isEmpty()) {
+ return true;
+ }
TXshSimpleLevel *sl = cell.getSimpleLevel();
- if (sl) return (sl->getType() & LEVELCOLUMN_XSHLEVEL);
+ if (sl) {
+ return (sl->getType() & LEVELCOLUMN_XSHLEVEL) != 0;
+ }
- return cell.getChildLevel();
+ return cell.getChildLevel() != nullptr;
}
//-----------------------------------------------------------------------------
@@ -87,7 +100,6 @@ TXshColumn *TXshLevelColumn::clone() const {
column->setColorTag(getColorTag());
column->setColorFilterId(getColorFilterId());
- // column->updateIcon();
return column;
}
@@ -97,64 +109,70 @@ void TXshLevelColumn::loadData(TIStream &is) {
std::string tagName;
while (is.openChild(tagName)) {
if (tagName == "status") {
- int status;
+ int status = 0;
is >> status;
setStatusWord(status);
- if (status & eCamstandTransparent43) {
+ if ((status & eCamstandTransparent43) != 0) {
setOpacity(128);
status = status & ~eCamstandTransparent43;
}
} else if (tagName == "camerastand_opacity") {
- int opacity;
+ int opacity = 0;
is >> opacity;
- setOpacity((UCHAR)opacity);
+ setOpacity(static_cast<UCHAR>(opacity));
} else if (tagName == "filter_color_id") {
- int id;
+ int id = 0;
is >> id;
setColorFilterId(id);
} else if (tagName == "cells") {
while (is.openChild(tagName)) {
if (tagName == "cell") {
- TPersist *p = 0;
+ TPersist *p = nullptr;
std::string str;
int row = 1, rowCount = 1, increment = 0;
TFilePath path;
is >> row >> rowCount >> p >> str >> increment;
+
TFrameId fid = qstringToFrameId(QString::fromStdString(str));
assert((fid.getLetter().isEmpty() && rowCount >= 0) ||
(!fid.getLetter().isEmpty() && rowCount == 1));
+
TXshLevel *xshLevel = dynamic_cast<TXshLevel *>(p);
if (xshLevel) {
int fidNumber = fid.getNumber();
for (int i = 0; i < rowCount; i++) {
TXshCell cell(xshLevel, fid);
setCell(row++, cell);
- // rowCount>1 => fid has not letter.
+ // rowCount > 1 => fid has no letter.
fidNumber += increment;
fid = TFrameId(fidNumber);
}
}
- } else
+ } else {
throw TException("TXshLevelColumn, unknown tag(2): " + tagName);
+ }
is.closeChild();
}
} else if (tagName == "fx") {
- TPersist *p = 0;
+ TPersist *p = nullptr;
is >> p;
if (TLevelColumnFx *lcf = dynamic_cast<TLevelColumnFx *>(p)) {
lcf->addRef();
- if (m_fx) m_fx->release();
+ if (m_fx) {
+ m_fx->release();
+ }
m_fx = lcf;
lcf->setColumn(this);
}
- } else if (tagName == "fxnodes") // per compatibilita' con 1.x e precedenti
- {
+ } else if (tagName ==
+ "fxnodes") { // For compatibility with 1.x and earlier
TFxSet fxSet;
fxSet.loadData(is);
} else if (loadCellMarks(tagName, is)) {
- // do nothing
- } else
+ // Do nothing
+ } else {
throw TException("TXshLevelColumn, unknown tag: " + tagName);
+ }
is.closeChild();
}
}
@@ -163,18 +181,28 @@ void TXshLevelColumn::loadData(TIStream &is) {
void TXshLevelColumn::saveData(TOStream &os) {
os.child("status") << getStatusWord();
- if (getOpacity() < 255) os.child("camerastand_opacity") << (int)getOpacity();
- if (getColorFilterId() != 0)
- os.child("filter_color_id") << (int)getColorFilterId();
- int r0, r1;
+
+ if (getOpacity() < 255) {
+ os.child("camerastand_opacity") << static_cast<int>(getOpacity());
+ }
+
+ if (getColorFilterId() != 0) {
+ os.child("filter_color_id") << static_cast<int>(getColorFilterId());
+ }
+
+ int r0 = 0, r1 = 0;
if (getRange(r0, r1)) {
os.openChild("cells");
for (int r = r0; r <= r1; r++) {
TXshCell cell = getCell(r);
- if (cell.isEmpty()) continue;
+ if (cell.isEmpty()) {
+ continue;
+ }
+
TFrameId fid = cell.m_frameId;
int n = 1, inc = 0, dr = fid.getNumber();
- // If fid has not letter save more than one cell and its incrementation;
+
+ // If fid has no letter, save more than one cell and its increment;
// otherwise save one cell.
if (r < r1 && fid.getLetter().isEmpty()) {
TXshCell cell2 = getCell(r + 1);
@@ -184,26 +212,33 @@ void TXshLevelColumn::saveData(TOStream &os) {
inc = cell2.m_frameId.getNumber() - dr;
n++;
for (;;) {
- if (r + n > r1) break;
+ if (r + n > r1) {
+ break;
+ }
cell2 = getCell(r + n);
TFrameId fid2 = cell2.m_frameId;
if (cell2.m_level.getPointer() != cell.m_level.getPointer() ||
- !fid2.getLetter().isEmpty())
+ !fid2.getLetter().isEmpty()) {
break;
- if (fid2.getNumber() != dr + n * inc) break;
+ }
+ if (fid2.getNumber() != dr + n * inc) {
+ break;
+ }
n++;
}
}
}
+
os.child("cell") << r << n << cell.m_level.getPointer() << fid.expand()
<< inc;
r += n - 1;
}
os.closeChild();
}
+
os.child("fx") << m_fx;
- // cell marks
+ // Save cell marks
saveCellMarks(os);
}
@@ -214,34 +249,45 @@ void TXshLevelColumn::saveData(TOStream &os) {
// in the scene cast.
bool TXshLevelColumn::setNumbers(int row, int rowCount, const TXshCell cells[],
TXshLevel *reservedLevel) {
- if (m_cells.empty() && !reservedLevel) return false;
+ if (m_cells.empty() && !reservedLevel) {
+ return false;
+ }
+
// Check availability.
// - if source cells are all empty, do nothing
// - also, if source or target cells contain NO_FRAME, do nothing
bool isSrcAllEmpty = true;
for (int i = 0; i < rowCount; i++) {
- // checking target cells
+ // Checking target cells
int currentTgtIndex = row + i - m_first;
- if (currentTgtIndex < m_cells.size()) {
+ if (currentTgtIndex < static_cast<int>(m_cells.size())) {
TXshCell tgtCell = m_cells[currentTgtIndex];
- if (!tgtCell.isEmpty() && tgtCell.m_frameId == TFrameId::NO_FRAME)
+ if (!tgtCell.isEmpty() && tgtCell.m_frameId == TFrameId::NO_FRAME) {
return false;
+ }
}
- // checking source cells
+
+ // Checking source cells
TXshCell srcCell = cells[i];
if (!srcCell.isEmpty()) {
- if (srcCell.m_frameId == TFrameId::NO_FRAME) return false;
+ if (srcCell.m_frameId == TFrameId::NO_FRAME) {
+ return false;
+ }
isSrcAllEmpty = false;
}
}
- if (isSrcAllEmpty) return false;
+
+ if (isSrcAllEmpty) {
+ return false;
+ }
// Find a level to input.
// If the first target cell is empty, search the upper cells, and lower cells
- // and use a level of firsty-found occupied neighbor cell.
+ // and use a level of first found occupied neighbor cell.
TXshLevelP currentLevel = reservedLevel;
- int tmpIndex = std::min(row - m_first, (int)m_cells.size() - 1);
- // search upper cells
+ int tmpIndex = std::min(row - m_first, static_cast<int>(m_cells.size()) - 1);
+
+ // Search upper cells
if (!currentLevel) {
while (tmpIndex >= 0) {
TXshCell tmpCell = m_cells[tmpIndex];
@@ -252,10 +298,11 @@ bool TXshLevelColumn::setNumbers(int row, int rowCount, const TXshCell cells[],
tmpIndex--;
}
}
- // if not found any level in upper cells, then search the lower cells
+
+ // If not found any level in upper cells, then search the lower cells
if (!currentLevel) {
tmpIndex = std::max(row - m_first, 0);
- while (tmpIndex < (int)m_cells.size()) {
+ while (tmpIndex < static_cast<int>(m_cells.size())) {
TXshCell tmpCell = m_cells[tmpIndex];
if (!tmpCell.isEmpty() && tmpCell.m_frameId != TFrameId::NO_FRAME) {
currentLevel = tmpCell.m_level;
@@ -263,14 +310,18 @@ bool TXshLevelColumn::setNumbers(int row, int rowCount, const TXshCell cells[],
}
tmpIndex++;
}
- // in the case any level for input could not be found
- if (!currentLevel) return false;
+
+ // In the case any level for input could not be found
+ if (!currentLevel) {
+ return false;
+ }
}
// Resize the cell container
int ra = row;
int rb = row + rowCount - 1;
- int c_rb = m_first + m_cells.size() - 1;
+ int c_rb = m_first + static_cast<int>(m_cells.size()) - 1;
+
if (row > c_rb) {
int newCellCount = row - m_first + rowCount;
m_cells.resize(newCellCount);
@@ -279,34 +330,43 @@ bool TXshLevelColumn::setNumbers(int row, int rowCount, const TXshCell cells[],
m_cells.insert(m_cells.begin(), delta, TXshCell());
m_first = row;
}
+
if (rb > c_rb) {
- for (int i = 0; i < rb - c_rb; ++i) m_cells.push_back(TXshCell());
+ for (int i = 0; i < rb - c_rb; ++i) {
+ m_cells.push_back(TXshCell());
+ }
}
- // Paste numbers.
+ // Paste numbers
for (int i = 0; i < rowCount; i++) {
int dstIndex = row - m_first + i;
TXshCell dstCell = m_cells[dstIndex];
TXshCell srcCell = cells[i];
+
if (srcCell.isEmpty()) {
m_cells[dstIndex] = TXshCell();
} else {
- if (!dstCell.isEmpty()) currentLevel = dstCell.m_level;
+ if (!dstCell.isEmpty()) {
+ currentLevel = dstCell.m_level;
+ }
m_cells[dstIndex] = TXshCell(currentLevel, srcCell.m_frameId);
}
}
- // Update the cell container.
+ // Update the cell container
while (!m_cells.empty() && m_cells.back().isEmpty()) {
m_cells.pop_back();
}
+
while (!m_cells.empty() && m_cells.front().isEmpty()) {
m_cells.erase(m_cells.begin());
m_first++;
}
+
if (m_cells.empty()) {
m_first = 0;
}
+
return true;
}