OpenToonz - An open-source full-featured 2D animation creation software
C
fix(level-strip): bound responsive cache and share layout geometry
Ahl76139b30350037
diff --git a/toonz/sources/include/toonzqt/icongenerator.h b/toonz/sources/include/toonzqt/icongenerator.hindex 0b09973f8..f6e286616 100644--- a/toonz/sources/include/toonzqt/icongenerator.h+++ b/toonz/sources/include/toonzqt/icongenerator.h@@ -113,6 +113,8 @@ public:bool onDemand = false);QPixmap getSizedIcon(TXshLevel *sl, const TFrameId &fid, std::string newId,TDimension dim = TDimension(0, 0));+ QPixmap getResponsiveIcon(TXshLevel *sl, const TFrameId &fid,+ const TDimension &dim);void invalidate(TXshLevel *sl, const TFrameId &fid,bool onlyFilmStrip = false);void remove(TXshLevel *sl, const TFrameId &fid, bool onlyFilmStrip = false);diff --git a/toonz/sources/toonz/filmstrip.cpp b/toonz/sources/toonz/filmstrip.cppindex 936064261..4f8d34f2a 100644--- a/toonz/sources/toonz/filmstrip.cpp+++ b/toonz/sources/toonz/filmstrip.cpp@@ -51,7 +51,6 @@#include <QDrag>#include <QTimer>#include <algorithm>-#include <string>namespace {QString fidToFrameNumberWithLetter(int f) {@@ -144,7 +143,7 @@ FilmstripFrames::FilmstripFrames(QScrollArea *parent, Qt::WindowFlags flags)setFrameStyle(QFrame::StyledPanel);setFocusPolicy(Qt::StrongFocus);- applyCrossAxisDimension();+ updateContentConstraints();if (m_isVertical) {setFixedHeight(parentWidget()->height());} else {@@ -185,13 +184,6 @@ TXshSimpleLevel *FilmstripFrames::getLevel() const {//------------------------------------------------------------------------------std::string FilmstripFrames::responsiveIconCacheSuffix(const QSize &size) {- return "_r_" + std::to_string(size.width()) + "x" +- std::to_string(size.height());-}--//------------------------------------------------------------------------------QSize FilmstripFrames::quantizeResponsiveRenderSize(const QSize &layout,const QSize &pref,bool vertical) {@@ -218,20 +210,32 @@ QSize FilmstripFrames::quantizeResponsiveRenderSize(const QSize &layout,//------------------------------------------------------------------------------void FilmstripFrames::applyCrossAxisDimension() {+void FilmstripFrames::updateContentConstraints() {if (!m_scrollArea) return;- QWidget *vp = m_scrollArea->viewport();- if (!vp) return;if (m_responsiveThumbnails) {- if (m_isVertical)- setFixedWidth(std::max(1, vp->width()));- else- setFixedHeight(std::max(1, vp->height()));+ m_scrollArea->setWidgetResizable(true);+ if (m_isVertical) {+ setMinimumWidth(0);+ setMaximumWidth(QWIDGETSIZE_MAX);+ setMinimumHeight(getFramesHeight());+ setMaximumHeight(QWIDGETSIZE_MAX);+ } else {+ setMinimumHeight(0);+ setMaximumHeight(QWIDGETSIZE_MAX);+ setMinimumWidth(getFramesWidth());+ setMaximumWidth(QWIDGETSIZE_MAX);+ }} else if (m_isVertical) {+ m_scrollArea->setWidgetResizable(false);setFixedWidth(m_iconSize.width() + fs_leftMargin + fs_rightMargin +fs_iconMarginLR * 2);+ setMinimumHeight(0);+ setMaximumHeight(QWIDGETSIZE_MAX);} else {+ m_scrollArea->setWidgetResizable(false);setFixedHeight(getOneFrameHeight());+ setMinimumWidth(0);+ setMaximumWidth(QWIDGETSIZE_MAX);}}@@ -289,7 +293,7 @@ void FilmstripFrames::updateIconLayout() {m_iconSize = effective;- applyCrossAxisDimension();+ updateContentConstraints();if (m_frameHeadGadget) m_frameHeadGadget->updateFrameMetrics();if (m_isVertical)updateContentHeight();@@ -395,14 +399,14 @@ int FilmstripFrames::getFramesWidth() const {//------------------------------------------------------------------------------int FilmstripFrames::getOneFrameHeight() {+int FilmstripFrames::getOneFrameHeight() const {return m_iconSize.height() + fs_frameSpacing + fs_iconMarginTop +fs_iconMarginBottom;}//------------------------------------------------------------------------------int FilmstripFrames::getOneFrameWidth() {+int FilmstripFrames::getOneFrameWidth() const {return m_iconSize.width() + fs_frameSpacing + fs_leftMargin +fs_iconMarginLR + fs_rightMargin;}@@ -415,41 +419,58 @@ void FilmstripFrames::updateContentHeight(int minimumHeight) {if (contentHeight < minimumHeight) contentHeight = minimumHeight;int parentHeight = parentWidget()->height();if (contentHeight < parentHeight) contentHeight = parentHeight;- if (contentHeight != height()) setFixedHeight(contentHeight);+ if (m_responsiveThumbnails) {+ setMinimumHeight(contentHeight);+ } else if (contentHeight != height()) {+ setFixedHeight(contentHeight);+ }}//-----------------------------------------------------------------------------void FilmstripFrames::updateContentWidth(int minimumWidth) {- setFixedHeight(getOneFrameHeight());+ if (!m_responsiveThumbnails) setFixedHeight(getOneFrameHeight());if (minimumWidth < 0) minimumWidth = visibleRegion().boundingRect().right();int contentWidth = getFramesWidth();if (contentWidth < minimumWidth) contentWidth = minimumWidth;int parentWidth = parentWidget()->width();if (contentWidth < parentWidth) contentWidth = parentWidth;- if (contentWidth != width()) setFixedWidth(contentWidth);+ if (m_responsiveThumbnails) {+ setMinimumWidth(contentWidth);+ } else if (contentWidth != width()) {+ setFixedWidth(contentWidth);+ }}//-----------------------------------------------------------------------------void FilmstripFrames::showFrame(int index) {TXshSimpleLevel *level = getLevel();+ if (!level) return;if (m_isVertical) {if (!level->isFid(index2fid(index))) {if (!level->isFid(index2fid(index))) return;}int y0 = index2y(index);- int y1 = y0 + m_iconSize.height() + fs_frameSpacing + fs_iconMarginTop +- fs_iconMarginBottom;- if (y1 > height()) setFixedHeight(y1);+ int y1 = y0 + getOneFrameHeight();+ if (y1 > height()) {+ if (m_responsiveThumbnails)+ setMinimumHeight(y1);+ else+ setFixedHeight(y1);+ }m_scrollArea->ensureVisible(0, (y0 + y1) / 2, 50, (y1 - y0) / 2);} else {if (!level->isFid(index2fid(index))) {if (!level->isFid(index2fid(index - 1))) return;}int x0 = index2x(index);- int x1 = x0 + m_iconSize.width() + fs_frameSpacing + fs_leftMargin +- fs_rightMargin + fs_iconMarginLR;- if (x1 > width()) setFixedWidth(x1);+ int x1 = x0 + getOneFrameWidth();+ if (x1 > width()) {+ if (m_responsiveThumbnails)+ setMinimumWidth(x1);+ else+ setFixedWidth(x1);+ }m_scrollArea->ensureVisible((x0 + x1) / 2, 0, (x1 - x0) / 2, 50);}}@@ -943,8 +964,7 @@ void FilmstripFrames::drawFrameIcon(QPainter &p, const QRect &r, int index,m_renderIconSize.height() > m_prefIconSize.height());if (needNativeSize) {const TDimension dim(m_renderIconSize.width(), m_renderIconSize.height());- pm = IconGenerator::instance()->getSizedIcon(- xl, fid, responsiveIconCacheSuffix(m_renderIconSize), dim);+ pm = IconGenerator::instance()->getResponsiveIcon(xl, fid, dim);}if (pm.isNull()) pm = IconGenerator::instance()->getIcon(xl, fid);}@@ -1544,8 +1564,8 @@ void FilmstripFrames::navigatorToggled(bool ignore) {//------------------------------------------------------------------------------void FilmstripFrames::responsiveThumbnailsToggled(bool ignore) {- emit(responsiveThumbnailsToggledSignal());+void FilmstripFrames::responsiveThumbnailsToggled(bool responsive) {+ emit(responsiveThumbnailsToggledSignal(responsive));}//-----------------------------------------------------------------------------@@ -1747,8 +1767,8 @@ Filmstrip::Filmstrip(QWidget *parent, Qt::WindowFlags flags) : QWidget(parent) {SLOT(comboBoxToggled()));connect(m_frames, SIGNAL(navigatorToggledSignal()), this,SLOT(navigatorToggled()));- connect(m_frames, SIGNAL(responsiveThumbnailsToggledSignal()), this,- SLOT(responsiveThumbnailsToggled()));+ connect(m_frames, SIGNAL(responsiveThumbnailsToggledSignal(bool)), this,+ SLOT(responsiveThumbnailsToggled(bool)));connect(m_frames, SIGNAL(levelSelectedSignal(int)), this,SLOT(onChooseLevelComboChanged(int)));@@ -1985,7 +2005,6 @@ void Filmstrip::hideEvent(QHideEvent *) {//-----------------------------------------------------------------------------void Filmstrip::resizeEvent(QResizeEvent *e) {- if (m_frames->isResponsiveThumbnails()) m_frames->updateIconLayout();if (m_isVertical) {m_frames->updateContentHeight();m_frameArea->verticalScrollBar()->setSingleStep(@@ -2049,10 +2068,8 @@ void Filmstrip::onLevelSwitched(TXshLevel *oldLevel) {//-----------------------------------------------------------------------------void Filmstrip::onSliderMoved(int val) {- int oneFrameHeight = m_frames->getIconSize().height() + fs_frameSpacing +- fs_iconMarginTop + fs_iconMarginBottom;- int oneFrameWidth =- m_frames->getIconSize().width() + fs_frameSpacing + fs_iconMarginLR;+ int oneFrameHeight = m_frames->getOneFrameHeight();+ int oneFrameWidth = m_frames->getOneFrameWidth();if (m_isVertical) {int tmpVal =(int)((float)val / (float)oneFrameHeight + 0.5f) * oneFrameHeight;@@ -2114,9 +2131,9 @@ void Filmstrip::navigatorToggled() {//------------------------------------------------------------------------------void Filmstrip::responsiveThumbnailsToggled() {- m_responsiveThumbnails = !m_responsiveThumbnails;- m_frames->setResponsiveThumbnails(m_responsiveThumbnails);+void Filmstrip::responsiveThumbnailsToggled(bool responsive) {+ m_responsiveThumbnails = responsive;+ m_frames->setResponsiveThumbnails(responsive);}//-----------------------------------------------------------------------------@@ -2173,7 +2190,7 @@ void Filmstrip::load(QSettings &settings) {}m_frames->setComboBox(m_showComboBox);- UINT responsive = settings.value("responsiveThumbnails", 0).toUInt();+ UINT responsive = settings.value("responsiveThumbnails", 1).toUInt();m_responsiveThumbnails = responsive == 1;m_frames->setResponsiveThumbnails(m_responsiveThumbnails);}diff --git a/toonz/sources/toonz/filmstrip.h b/toonz/sources/toonz/filmstrip.hindex 6cd3a0668..ff2217d88 100644--- a/toonz/sources/toonz/filmstrip.h+++ b/toonz/sources/toonz/filmstrip.h@@ -15,7 +15,6 @@// STD includes#include <vector>#include <map>-#include <string>// forward declarationclass TFrameId;@@ -51,7 +50,7 @@ public:bool m_isVertical = true;bool m_showNavigator = true;bool m_showComboBox = true;- bool m_responsiveThumbnails = false;+ bool m_responsiveThumbnails = true;void setBGColor(const QColor &color) { m_bgColor = color; }QColor getBGColor() const { return m_bgColor; }@@ -120,8 +119,8 @@ public:};void select(int index, SelectionMode mode = SIMPLE_SELECT);- int getOneFrameHeight();- int getOneFrameWidth();+ int getOneFrameHeight() const;+ int getOneFrameWidth() const;void setOrientation(bool isVertical);void setNavigator(bool showNavigator);void setComboBox(bool showComboBox);@@ -133,7 +132,7 @@ signals:void orientationToggledSignal(bool);void comboBoxToggledSignal();void navigatorToggledSignal();- void responsiveThumbnailsToggledSignal();+ void responsiveThumbnailsToggledSignal(bool responsive);void levelSelectedSignal(int);protected:@@ -217,9 +216,8 @@ private:QTimer *m_responsiveRenderTimer = nullptr;- void applyCrossAxisDimension();+ void updateContentConstraints();void scheduleResponsiveRenderCommit();- static std::string responsiveIconCacheSuffix(const QSize &size);static QSize quantizeResponsiveRenderSize(const QSize &layout,const QSize &pref, bool vertical);@@ -255,7 +253,7 @@ class Filmstrip final : public QWidget, public SaveLoadQSettings {bool m_isVertical = true;bool m_showNavigator = true;bool m_showComboBox = true;- bool m_responsiveThumbnails = false;+ bool m_responsiveThumbnails = true;public:Filmstrip(QWidget *parent = 0, Qt::WindowFlags flags = Qt::WindowFlags());@@ -291,7 +289,7 @@ public slots:void orientationToggled(bool);void comboBoxToggled();void navigatorToggled();- void responsiveThumbnailsToggled();+ void responsiveThumbnailsToggled(bool responsive);private:void updateWindowTitle();diff --git a/toonz/sources/toonz/frameheadgadget.cpp b/toonz/sources/toonz/frameheadgadget.cppindex 4d6a85b92..376e30aa0 100644--- a/toonz/sources/toonz/frameheadgadget.cpp+++ b/toonz/sources/toonz/frameheadgadget.cpp@@ -329,10 +329,8 @@ int FilmstripFrameHeadGadget::getCurrentFrame() const {//-----------------------------------------------------------------------------void FilmstripFrameHeadGadget::updateFrameMetrics() {- m_dy = m_filmstrip->getIconSize().height() + fs_frameSpacing +- fs_iconMarginTop + fs_iconMarginBottom;- m_dx = m_filmstrip->getIconSize().width() + fs_frameSpacing + fs_leftMargin +- fs_rightMargin + fs_iconMarginLR;+ m_dy = m_filmstrip->getOneFrameHeight();+ m_dx = m_filmstrip->getOneFrameWidth();}//-----------------------------------------------------------------------------diff --git a/toonz/sources/toonzqt/icongenerator.cpp b/toonz/sources/toonzqt/icongenerator.cppindex 8c2279dbe..e76b2a591 100644--- a/toonz/sources/toonzqt/icongenerator.cpp+++ b/toonz/sources/toonzqt/icongenerator.cpp@@ -47,6 +47,9 @@#include "toonzqt/icongenerator.h"#include <QCoreApplication>+#include <algorithm>+#include <deque>+#include <map>#include <vector>//=============================================================================@@ -64,6 +67,8 @@ TDimension FilmstripIconSize(0, 0);// Access name-based storagestd::set<std::string> iconsMap;typedef std::set<std::string>::iterator IconIterator;+std::map<std::string, std::deque<std::string>> responsiveIconKeys;+const int kMaxResponsiveIconSizes = 3;//-----------------------------------------------------------------------------@@ -166,15 +171,40 @@ void removeIcon(const std::string &iconName) {//-----------------------------------------------------------------------------+std::string responsiveIconKey(const std::string &baseId,+ const TDimension &size) {+ return baseId + "_r_" + std::to_string(size.lx) + "x" ++ std::to_string(size.ly);+}++std::string filmstripIconBaseId(TXshSimpleLevel *sl, const TFrameId &fid) {+ const int status = sl->getFrameStatus(fid);+ if (sl->getType() == TZP_XSHLEVEL &&+ status & TXshSimpleLevel::CleanupPreview) {+ sl->setFrameStatus(fid, status & ~TXshSimpleLevel::CleanupPreview);+ std::string id = sl->getIconId(fid);+ sl->setFrameStatus(fid, status);+ return id;+ }+ return sl->getIconId(fid);+}+void removeResponsiveSizedIcons(const std::string &baseId) {- const std::string prefix = baseId + "_r_";- std::vector<std::string> toRemove;- for (IconIterator it = iconsMap.lower_bound(prefix);- it != iconsMap.end() && it->compare(0, prefix.size(), prefix) == 0;- ++it) {- toRemove.push_back(*it);+ auto it = responsiveIconKeys.find(baseId);+ if (it == responsiveIconKeys.end()) return;+ for (const std::string &key : it->second) removeIcon(key);+ responsiveIconKeys.erase(it);+}++void retainResponsiveIcon(const std::string &baseId, const std::string &key) {+ std::deque<std::string> &keys = responsiveIconKeys[baseId];+ auto found = std::find(keys.begin(), keys.end(), key);+ if (found != keys.end()) keys.erase(found);+ keys.push_back(key);+ while ((int)keys.size() > kMaxResponsiveIconSizes) {+ removeIcon(keys.front());+ keys.pop_front();}- for (const std::string &key : toRemove) removeIcon(key);}//-----------------------------------------------------------------------------@@ -1601,13 +1631,27 @@ QPixmap IconGenerator::getSizedIcon(TXshLevel *xl, const TFrameId &fid,//-----------------------------------------------------------------------------+QPixmap IconGenerator::getResponsiveIcon(TXshLevel *xl, const TFrameId &fid,+ const TDimension &dim) {+ if (!xl) return QPixmap();+ TXshSimpleLevel *sl = xl->getSimpleLevel();+ if (!sl) return QPixmap();++ const std::string baseId = filmstripIconBaseId(sl, fid);+ const std::string key = responsiveIconKey(baseId, dim);+ retainResponsiveIcon(baseId, key);+ return getSizedIcon(xl, fid, key.substr(baseId.size()), dim);+}++//-----------------------------------------------------------------------------+void IconGenerator::invalidate(TXshLevel *xl, const TFrameId &fid,bool onlyFilmStrip) {if (!xl) return;if (TXshSimpleLevel *sl = xl->getSimpleLevel()) {std::string id = sl->getIconId(fid);- removeResponsiveSizedIcons(id);+ removeResponsiveSizedIcons(filmstripIconBaseId(sl, fid));int type = sl->getType();@@ -1695,7 +1739,7 @@ void IconGenerator::remove(TXshLevel *xl, const TFrameId &fid,std::string id(sl->getIconId(fid));removeIcon(id);- removeResponsiveSizedIcons(id);+ removeResponsiveSizedIcons(filmstripIconBaseId(sl, fid));if (!onlyFilmStrip) removeIcon(id + "_small");} else {TXshChildLevel *cl = xl->getChildLevel();