Skip to content

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

C

create custom menubar from xml file

shun_iwasawa7a9c88552bae
diff --git a/toonz/sources/toonz/mainwindow.cpp b/toonz/sources/toonz/mainwindow.cpp
index 2cd87e8b2..d373f3fe0 100644
--- a/toonz/sources/toonz/mainwindow.cpp
+++ b/toonz/sources/toonz/mainwindow.cpp
@@ -518,7 +518,9 @@ void MainWindow::readSettings(const QString &argumentLayoutFileName)
m_stackedWidget->addWidget(room);
roomTabWidget->addTab(room->getName());
- stackedMenuBar->createMenuBarByName(room->getName());
+ /*- ここでMenuBarファイルをロードする -*/
+ std::string mbFileName = roomPath.getName() + "_menubar.xml";
+ stackedMenuBar->loadAndAddMenubar(ToonzFolder::getModuleFile(mbFileName));
//room->setDockOptions(QMainWindow::DockOptions(
// (QMainWindow::AnimatedDocks | QMainWindow::AllowNestedDocks) & ~QMainWindow::AllowTabbedDocks));
@@ -529,6 +531,7 @@ void MainWindow::readSettings(const QString &argumentLayoutFileName)
//Read the flipbook history
FlipBookPool::instance()->load(ToonzFolder::getMyModuleDir() + TFilePath("fliphistory.ini"));
+ /*- レイアウト設定ファイルが見つからなかった場合、初期Roomの生成 -*/
//Se leggendo i settings non ho inizializzato le stanze lo faccio ora.
// Puo' accadere se si buttano i file di inizializzazione.
if (rooms.empty()) {
@@ -567,11 +570,12 @@ void MainWindow::readSettings(const QString &argumentLayoutFileName)
m_stackedWidget->addWidget(browserRoom);
rooms.push_back(browserRoom);
stackedMenuBar->createMenuBarByName(browserRoom->getName());
-
- makePrivate(rooms);
- writeRoomList(rooms);
}
+ /*- If the layout files were loaded from template, then save them as private ones -*/
+ makePrivate(rooms);
+ writeRoomList(rooms);
+
// Imposto la stanza corrente
fp = ToonzFolder::getModuleFile(currentRoomFileName);
Tifstream is(fp);
diff --git a/toonz/sources/toonz/menubar.cpp b/toonz/sources/toonz/menubar.cpp
index fa829f93c..94181881d 100644
--- a/toonz/sources/toonz/menubar.cpp
+++ b/toonz/sources/toonz/menubar.cpp
@@ -11,6 +11,7 @@
#include "toonzqt/tselectionhandle.h"
#include "toonzqt/dvdialog.h"
#include "toonzqt/menubarcommand.h"
+#include "toonzqt/gutil.h"
// TnzLib includes
#include "toonz/toonzscene.h"
@@ -38,6 +39,8 @@
#include <QShortcut>
#include <QDesktopServices>
#include <QCheckBox>
+#include <QtDebug>
+#include <QXmlStreamReader>
void UrlOpener::open()
{
@@ -236,6 +239,116 @@ void StackedMenuBar::createMenuBarByName(const QString &roomName)
addWidget(createFullMenuBar());
#endif
}
+//---------------------------------------------------------------------------------
+
+void StackedMenuBar::loadAndAddMenubar(const TFilePath & fp)
+{
+ std::wcout << fp.getWideString() << std::endl;
+
+ QFile file(toQString(fp));
+ if (!file.open(QFile::ReadOnly | QFile::Text)){
+ qDebug() << "Cannot read file" << file.errorString();
+ addWidget(createFullMenuBar());
+ return;
+ }
+
+ QXmlStreamReader reader(&file);
+
+ QMenuBar* menuBar = new QMenuBar(this);
+ if (reader.readNextStartElement())
+ {
+ if (reader.name() == "menubar")
+ {
+ while (reader.readNextStartElement())
+ {
+ if (reader.name() == "menu")
+ {
+ QString title = reader.attributes().value("title").toString();
+ /*- Menu title will be translated if the title is registered in translation file -*/
+ QMenu* menu = new QMenu(tr(title.toStdString().c_str()));
+
+ if (readMenuRecursive(reader, menu))
+ menuBar->addMenu(menu);
+ else
+ {
+ reader.raiseError(tr("Failed to load menu %1").arg(title));
+ delete menu;
+ }
+
+ }
+ else if (reader.name() == "command")
+ {
+ QString cmdName = reader.readElementText();
+
+ QAction *action = CommandManager::instance()->getAction(cmdName.toStdString().c_str());
+ if (action)
+ menuBar->addAction(action);
+ else
+ reader.raiseError(tr("Failed to add command %1").arg(cmdName));
+ }
+ else
+ reader.skipCurrentElement();
+ }
+ }
+ else
+ reader.raiseError(QObject::tr("Incorrect file"));
+ }
+
+ if (reader.hasError())
+ {
+ delete menuBar;
+ addWidget(createFullMenuBar());
+ }
+ else
+ addWidget(menuBar);
+}
+
+//---------------------------------------------------------------------------------
+
+bool StackedMenuBar::readMenuRecursive( QXmlStreamReader& reader, QMenu* menu)
+{
+ while (reader.readNextStartElement())
+ {
+ if (reader.name() == "menu")
+ {
+ QString title = reader.attributes().value("title").toString();
+ QMenu* subMenu = new QMenu(tr(title.toStdString().c_str()));
+
+ if (readMenuRecursive(reader, subMenu))
+ menu->addMenu(subMenu);
+ else
+ {
+ reader.raiseError(tr("Failed to load menu %1").arg(title));
+ delete subMenu;
+ return false;
+ }
+
+ }
+ else if (reader.name() == "command")
+ {
+ QString cmdName = reader.readElementText();
+ addMenuItem(menu, cmdName.toStdString().c_str());
+ }
+ else if (reader.name() == "command_debug")
+ {
+#ifndef NDEBUG
+ QString cmdName = reader.readElementText();
+ addMenuItem(menu, cmdName.toStdString().c_str());
+#else
+ reader.skipCurrentElement();
+#endif
+ }
+ else if (reader.name() == "separator")
+ {
+ menu->addSeparator();
+ reader.skipCurrentElement();
+ }
+ else
+ reader.skipCurrentElement();
+ }
+
+ return !reader.hasError();
+}
//---------------------------------------------------------------------------------
diff --git a/toonz/sources/toonz/menubar.h b/toonz/sources/toonz/menubar.h
index 329cf5f9c..b941f064b 100644
--- a/toonz/sources/toonz/menubar.h
+++ b/toonz/sources/toonz/menubar.h
@@ -24,6 +24,7 @@ class QPainterEvent;
class QHBoxLayout;
class SubXsheetRoomTabContainer;
class QCheckBox;
+class QXmlStreamReader;
//-----------------------------------------------------------------------------
@@ -154,6 +155,8 @@ public:
~StackedMenuBar(){};
void createMenuBarByName(const QString &roomName);
+ void loadAndAddMenubar(const TFilePath & fp);
+ bool readMenuRecursive( QXmlStreamReader&, QMenu*);
QMenu *addMenu(const QString &, QMenuBar *);
void addMenuItem(QMenu *, const char *);