Skip to content

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

C

Feat: lazy load rooms

Abel You0e2cf3599d24
diff --git a/toonz/sources/include/toonz/preferences.h b/toonz/sources/include/toonz/preferences.h
index f2a2a65f2..246f396ef 100644
--- a/toonz/sources/include/toonz/preferences.h
+++ b/toonz/sources/include/toonz/preferences.h
@@ -190,6 +190,9 @@ public:
PathAliasPriority getPathAliasPriority() const {
return PathAliasPriority(getIntValue(pathAliasPriority));
}
+ bool isLazyLoadRoomsEnabled() {
+ return getBoolValue(lazyLoadRooms);
+ }
// Interface tab
QStringList getStyleSheetList() const { return m_styleSheetList; }
diff --git a/toonz/sources/include/toonz/preferencesitemids.h b/toonz/sources/include/toonz/preferencesitemids.h
index 0b3102de6..ad64f3d87 100644
--- a/toonz/sources/include/toonz/preferencesitemids.h
+++ b/toonz/sources/include/toonz/preferencesitemids.h
@@ -13,6 +13,7 @@ enum PreferencesItemId {
projectRoot,
customProjectRoot,
pathAliasPriority,
+ lazyLoadRooms,
//----------
// Interface
diff --git a/toonz/sources/toonz/cleanuppaletteviewer.cpp b/toonz/sources/toonz/cleanuppaletteviewer.cpp
index 150965ac1..5b758a18f 100644
--- a/toonz/sources/toonz/cleanuppaletteviewer.cpp
+++ b/toonz/sources/toonz/cleanuppaletteviewer.cpp
@@ -40,7 +40,7 @@ int search(TPalette *plt, TCleanupStyle *style) {
assert(false);
return -1;
}
-}
+} // namespace
//********************************************************************************
// CleanupPaletteViewer implementation
@@ -88,7 +88,7 @@ CleanupPaletteViewer::CleanupPaletteViewer(QWidget *parent)
ret = ret && connect(m_ph, SIGNAL(paletteSwitched()), SLOT(buildGUI()));
ret = ret && connect(m_ph, SIGNAL(paletteChanged()), SLOT(buildGUI()));
ret = ret && connect(m_ph, SIGNAL(colorStyleChanged(bool)),
- SLOT(onColorStyleChanged()));
+ SLOT(onColorStyleChanged()));
ret = ret && connect(m_add, SIGNAL(clicked(bool)), SLOT(onAddClicked(bool)));
ret = ret &&
@@ -116,9 +116,9 @@ void CleanupPaletteViewer::buildGUI() {
->getCurrentCleanupPalette()
->getPalette();
assert(palette);
- bool ret = true;
-
- int i, stylesCount = palette->getPage(0)->getStyleCount();
+ bool ret = true;
+ TPalette::Page *page = palette->getPage(0);
+ int i, stylesCount = palette ? page ? page->getStyleCount() : 0 : 0;
for (i = 0; i < stylesCount; ++i) {
TCleanupStyle *cs =
dynamic_cast<TCleanupStyle *>(palette->getPage(0)->getStyle(i));
diff --git a/toonz/sources/toonz/mainwindow.cpp b/toonz/sources/toonz/mainwindow.cpp
index d61b136a3..fee80935f 100644
--- a/toonz/sources/toonz/mainwindow.cpp
+++ b/toonz/sources/toonz/mainwindow.cpp
@@ -59,6 +59,7 @@
#ifdef _WIN32
#include <QtPlatformHeaders/QWindowsWindowFunctions>
#endif
+#include <docklayout.h>
TEnv::IntVar ViewCameraToggleAction("ViewCameraToggleAction", 1);
TEnv::IntVar ViewTableToggleAction("ViewTableToggleAction", 1);
@@ -241,8 +242,48 @@ int get_version_code_from(std::string ver) {
//=============================================================================
// Room
//-----------------------------------------------------------------------------
+void copyQSettings(QSettings &source, QSettings &destination,
+ bool commit = true) {
+ // 1. Get all keys in the current group
+ QStringList keys = source.allKeys();
+
+ // 2. Iterate and copy all simple key-value pairs
+ for (const QString &key : keys) {
+ destination.setValue(key, source.value(key));
+ }
+
+ // 3. Get all sub-groups
+ QStringList groups = source.childGroups();
+
+ // 4. Recursively process all groups (sections)
+ for (const QString &group : groups) {
+ // Move into the source group
+ source.beginGroup(group);
+ // Move into the destination group
+ destination.beginGroup(group);
+
+ // Recursively call the copy function for the subgroup
+ copyQSettings(source, destination, false); // Don't sync yet
+
+ // Go back up for both
+ destination.endGroup();
+ source.endGroup();
+ }
+
+ // 5. Save the changes if commit is requested
+ if (commit) {
+ destination.sync();
+ }
+}
void Room::save() {
+ if (!m_initialized && m_settings) {
+ QSettings *newSettings =
+ new QSettings(getPath().getQString(), QSettings::Format::IniFormat);
+ copyQSettings(*m_settings, *newSettings, true);
+ m_settings.reset(newSettings);
+ return;
+ }
DockLayout *layout = dockLayout();
// Now save layout state
@@ -282,16 +323,26 @@ void Room::save() {
}
//-----------------------------------------------------------------------------
-
-void Room::load(const TFilePath &fp) {
- QSettings settings(toQString(fp), QSettings::IniFormat);
+void Room::load(const TFilePath &fp, RoomLoadParams &params) {
+ if (!m_initialized || !m_settings) {
+ m_settings.reset(new QSettings(toQString(fp), QSettings::IniFormat));
+ }
setPath(fp);
-
DockLayout *layout = dockLayout();
- settings.beginGroup("room");
- QStringList itemsList = settings.childGroups();
+ m_settings->beginGroup("room");
+ QStringList itemsList = m_settings->childGroups();
+
+ QString roomName = m_settings->value("name").toString();
+ setName(roomName);
+
+ if (params.activeRoomName.isEmpty()) params.activeRoomName = roomName;
+
+ if (!params.forceBuildGui && params.activeRoomName != roomName) {
+ m_settings->endGroup();
+ return;
+ }
std::vector<QRect> geometries;
unsigned int i;
@@ -300,13 +351,13 @@ void Room::load(const TFilePath &fp) {
// NOTE: Panels have to be retrieved in the precise order they were saved.
// settings.beginGroup(itemsList[i]); //NO! itemsList has lexicographical
// ordering!!
- settings.beginGroup("pane_" + QString::number(i));
+ m_settings->beginGroup("pane_" + QString::number(i));
TPanel *pane = 0;
QString paneObjectName;
// Retrieve panel name
- QVariant name = settings.value("name");
+ QVariant name = m_settings->value("name");
if (name.canConvert(QVariant::String)) {
// Allocate panel
paneObjectName = name.toString();
@@ -314,7 +365,7 @@ void Room::load(const TFilePath &fp) {
pane = TPanelFactory::createPanel(this, paneObjectName);
if (SaveLoadQSettings *persistent =
dynamic_cast<SaveLoadQSettings *>(pane->widget()))
- persistent->load(settings);
+ persistent->load(*m_settings);
}
if (!pane) {
@@ -335,29 +386,30 @@ void Room::load(const TFilePath &fp) {
addDockWidget(pane);
// Store its geometry
- geometries.push_back(settings.value("geometry").toRect());
+ geometries.push_back(m_settings->value("geometry").toRect());
// Restore view type if present
- if (settings.contains("viewtype"))
- pane->setViewType(settings.value("viewtype").toInt());
+ if (m_settings->contains("viewtype"))
+ pane->setViewType(m_settings->value("viewtype").toInt());
// Restore flipbook pool indices
if (paneObjectName == "FlipBook") {
- int index = settings.value("index").toInt();
+ int index = m_settings->value("index").toInt();
dynamic_cast<FlipBook *>(pane->widget())->setPoolIndex(index);
}
- settings.endGroup();
+ m_settings->endGroup();
}
// resolve resize events here to avoid unwanted minimize of floating viewer
qApp->processEvents();
- DockLayout::State state(geometries, settings.value("hierarchy").toString());
+ DockLayout::State state(geometries,
+ m_settings->value("hierarchy").toString());
layout->restoreState(state);
- setName(settings.value("name").toString());
+ m_initialized = true;
}
//=============================================================================
@@ -416,8 +468,12 @@ centralWidget->setLayout(centralWidgetLayout);*/
QTabBar *roomTabWidget = m_topBar->getRoomTabWidget();
connect(m_stackedWidget, SIGNAL(currentChanged(int)),
SLOT(onCurrentRoomChanged(int)));
- connect(roomTabWidget, SIGNAL(currentChanged(int)), m_stackedWidget,
- SLOT(setCurrentIndex(int)));
+
+ QObject::connect(roomTabWidget, &QTabBar::currentChanged, [this](int index) {
+ Room *dstRoom = getRoom(index);
+ if (dstRoom->notInitialized()) dstRoom->initialize();
+ this->m_stackedWidget->setCurrentIndex(index);
+ });
/*-- タイトルバーにScene名を表示する --*/
connect(TApp::instance()->getCurrentScene(), SIGNAL(nameSceneChanged()), this,
@@ -579,12 +635,22 @@ void MainWindow::readSettings(const QString &argumentLayoutFileName) {
}
}
- int i;
- for (i = 0; i < (int)roomPaths.size(); i++) {
+ // Get Current Room
+ TFilePath fp = ToonzFolder::getRoomsFile(currentRoomFileName);
+ Tifstream is(fp);
+ std::string name;
+ is >> name;
+
+ QString currentRoomName = QString::fromUtf8(name.c_str());
+ Room::RoomLoadParams params;
+ params.activeRoomName = currentRoomName;
+ params.forceBuildGui = !Preferences::instance()->isLazyLoadRoomsEnabled();
+
+ for (int i = 0; i < (int)roomPaths.size(); i++) {
TFilePath roomPath = roomPaths[i];
if (TFileStatus(roomPath).doesExist()) {
Room *room = new Room(this);
- room->load(roomPath);
+ room->load(roomPath, params);
m_stackedWidget->addWidget(room);
roomTabWidget->addTab(room->getName());
@@ -649,16 +715,12 @@ void MainWindow::readSettings(const QString &argumentLayoutFileName) {
makePrivate(rooms);
writeRoomList(rooms);
- // Imposto la stanza corrente
- TFilePath fp = ToonzFolder::getRoomsFile(currentRoomFileName);
- Tifstream is(fp);
- std::string currentRoomName;
- is >> currentRoomName;
+ // Set Current Room
if (currentRoomName != "") {
int count = m_stackedWidget->count();
int index;
for (index = 0; index < count; index++)
- if (getRoom(index)->getName().toStdString() == currentRoomName) break;
+ if (getRoom(index)->getName() == currentRoomName) break;
if (index < count) {
m_oldRoomIndex = index;
roomTabWidget->setCurrentIndex(index);
@@ -1148,8 +1210,9 @@ void MainWindow::seeThroughWindow() {
//-----------------------------------------------------------------------------
void MainWindow::onCurrentRoomChanged(int newRoomIndex) {
- Room *oldRoom = getRoom(m_oldRoomIndex);
- Room *newRoom = getRoom(newRoomIndex);
+ Room *oldRoom = getRoom(m_oldRoomIndex);
+ Room *newRoom = getRoom(newRoomIndex);
+
QList<TPanel *> paneList = oldRoom->findChildren<TPanel *>();
// Change the parent of all the floating dockWidgets
@@ -2988,8 +3051,8 @@ void MainWindow::defineActions() {
VB_FlipX, QT_TR_NOOP("Flip Viewer Horizontally"), "fliphoriz");
createVisualizationButtonAction(
VB_FlipY, QT_TR_NOOP("Flip Viewer Vertically"), "flipvert");
- createVisualizationButtonAction(
- VB_RotateLeft, QT_TR_NOOP("Rotate View Left"), "rotateleft");
+ createVisualizationButtonAction(VB_RotateLeft, QT_TR_NOOP("Rotate View Left"),
+ "rotateleft");
createVisualizationButtonAction(
VB_RotateRight, QT_TR_NOOP("Rotate View Right"), "rotateright");
diff --git a/toonz/sources/toonz/mainwindow.h b/toonz/sources/toonz/mainwindow.h
index b7ecd87a1..a58033246 100644
--- a/toonz/sources/toonz/mainwindow.h
+++ b/toonz/sources/toonz/mainwindow.h
@@ -10,9 +10,12 @@
#include <map>
#include <QAction>
#include <QString>
+#include <QSettings>
#include "../toonzqt/tdockwindows.h"
+#include <memory>
+
class QStackedWidget;
class TPanel;
class UpdateChecker;
@@ -25,13 +28,26 @@ class Room final : public TMainWindow {
TFilePath m_path;
QString m_name;
+ std::unique_ptr<QSettings> m_settings;
+
+ // For lazy loading
+ bool m_initialized;
public:
Room(QWidget *parent = 0, Qt::WindowFlags flags = Qt::WindowFlags())
- : TMainWindow(parent, flags) {}
+ : TMainWindow(parent, flags)
+ , m_path()
+ , m_name()
+ , m_settings(nullptr)
+ , m_initialized(false) {}
~Room() {}
+ struct RoomLoadParams {
+ QString activeRoomName;
+ bool forceBuildGui = false;
+ };
+
TFilePath getPath() const { return m_path; }
void setPath(TFilePath path) { m_path = path; }
@@ -39,7 +55,14 @@ public:
void setName(QString name) { m_name = name; }
void save();
- void load(const TFilePath &fp);
+ void load(const TFilePath &fp, RoomLoadParams &params);
+
+ bool notInitialized() const { return !m_initialized; }
+ void initialize() {
+ RoomLoadParams params;
+ params.forceBuildGui = true;
+ load(m_path, params);
+ }
};
//-----------------------------------------------------------------------------
@@ -190,7 +213,8 @@ private:
QAction *createMiscAction(const char *id, const char *name,
const char *defaultShortcut);
QAction *createToolOptionsAction(const char *id, const char *name,
- const QString &defaultShortcut, const char *iconSVGName = "");
+ const QString &defaultShortcut,
+ const char *iconSVGName = "");
QAction *createStopMotionAction(const char *id, const char *name,
const QString &defaultShortcut,
const char *iconSVGName = "");
diff --git a/toonz/sources/toonz/preferencespopup.cpp b/toonz/sources/toonz/preferencespopup.cpp
index 85e72fb0d..ae7610e43 100644
--- a/toonz/sources/toonz/preferencespopup.cpp
+++ b/toonz/sources/toonz/preferencespopup.cpp
@@ -1220,6 +1220,7 @@ QString PreferencesPopup::getUIString(PreferencesItemId id) {
//{ projectRoot, tr("") },
{customProjectRoot, tr("Custom Project Path(s):")},
{pathAliasPriority, tr("Path Alias Priority:")},
+ {lazyLoadRooms, tr("Lazy Load Rooms")},
// Interface
{CurrentStyleSheetName, tr("Theme:")},
@@ -1668,6 +1669,7 @@ QWidget* PreferencesPopup::createGeneralPage() {
}
insertUI(pathAliasPriority, lay, getComboItemList(pathAliasPriority));
+ insertUI(lazyLoadRooms, lay);
lay->setRowStretch(lay->rowCount(), 1);
insertFootNote(lay);
@@ -1697,6 +1699,19 @@ QWidget* PreferencesPopup::createGeneralPage() {
"Project -> project folder aliases (+drawing...)");
pathAliasPriorityCB->setItemData(3, autoBySceneToolTip, Qt::ToolTipRole);
+ QCheckBox* lazyLoadRoomsCheckBox = getUI<QCheckBox*>(lazyLoadRooms);
+ connect(lazyLoadRoomsCheckBox, &QCheckBox::stateChanged,
+ [lazyLoadRoomsCheckBox](int state) {
+ QString status = Preferences::instance()->isLazyLoadRoomsEnabled()
+ ? tr("enabled") : tr("disabled");
+ QString description = Preferences::instance()->isLazyLoadRoomsEnabled()
+ ? tr("rooms will load on demand")
+ : tr("all rooms load at startup");
+ QString lazyLoadRoomsToolTip = tr("Lazy loading %1 - %2").arg(status).arg(description);
+ lazyLoadRoomsCheckBox->setToolTip(lazyLoadRoomsToolTip);
+ });
+ lazyLoadRoomsCheckBox->stateChanged(Preferences::instance()->isLazyLoadRoomsEnabled());
+
m_onEditedFuncMap.insert(autosaveEnabled,
&PreferencesPopup::onAutoSaveChanged);
m_onEditedFuncMap.insert(autosaveSceneEnabled,
diff --git a/toonz/sources/toonzlib/preferences.cpp b/toonz/sources/toonzlib/preferences.cpp
index edfad435f..2f47ec4c0 100644
--- a/toonz/sources/toonzlib/preferences.cpp
+++ b/toonz/sources/toonzlib/preferences.cpp
@@ -383,6 +383,7 @@ void Preferences::definePreferenceItems() {
(int)ProjectFolderOnly);
setCallBack(undoMemorySize, &Preferences::setUndoMemorySize);
+ define(lazyLoadRooms, "lazyLoadRooms", QMetaType::Bool, true);
// Interface
define(CurrentStyleSheetName, "CurrentStyleSheetName", QMetaType::QString,
diff --git a/toonz/sources/toonzlib/tpalettehandle.cpp b/toonz/sources/toonzlib/tpalettehandle.cpp
index 1fbaa5646..aa9201520 100644
--- a/toonz/sources/toonzlib/tpalettehandle.cpp
+++ b/toonz/sources/toonzlib/tpalettehandle.cpp
@@ -52,7 +52,7 @@ public:
//-----------------------------------------------------------------------------
TPaletteHandle::TPaletteHandle()
- : m_palette(0), m_styleIndex(-1), m_styleParamIndex(-1) {
+ : m_palette(nullptr), m_styleIndex(-1), m_styleParamIndex(-1) {
connectBroadcasts(this);
}