OpenToonz - An open-source full-featured 2D animation creation software
C
Modernize signals/slots and migrate to QRegularExpression
andeona2e249aa286a
diff --git a/toonz/sources/toonz/commandbarpopup.cpp b/toonz/sources/toonz/commandbarpopup.cppindex c89e2ccc1..353cf9503 100644--- a/toonz/sources/toonz/commandbarpopup.cpp+++ b/toonz/sources/toonz/commandbarpopup.cpp@@ -1,3 +1,5 @@++#include "commandbarpopup.h"// Tnz includes@@ -31,6 +33,9 @@#include <QPainter>#include <QApplication>#include <QLabel>+#include <QLineEdit>+#include <QMenu>+#include <QRegularExpression>//=============================================================================// CommandItem@@ -40,11 +45,14 @@ CommandItem::CommandItem(QTreeWidgetItem* parent, QAction* action): QTreeWidgetItem(parent, UserType), m_action(action) {setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled |Qt::ItemNeverHasChildren);+QString tempText = m_action->text();- // removing accelerator key indicator- tempText = tempText.replace(QRegExp("&([^& ])"), "\\1");- // removing doubled &s+ // Removing accelerator key indicator+ QRegularExpression accelRegex("&([^& ])");+ tempText = tempText.replace(accelRegex, "\\1");+ // Removing doubled &stempText = tempText.replace("&&", "&");+setText(0, tempText);setToolTip(0, QObject::tr("[Drag] to move position"));}@@ -67,7 +75,7 @@ SeparatorItem::SeparatorItem(QTreeWidgetItem* parent)CommandListTree::CommandListTree(const QString& dropTargetString,QWidget* parent, bool withSeparator)- : m_dropTargetString(dropTargetString), QTreeWidget(parent) {+ : QTreeWidget(parent), m_dropTargetString(dropTargetString) {setObjectName("SolidLineFrame");setAlternatingRowColors(true);setDragEnabled(true);@@ -82,7 +90,7 @@ CommandListTree::CommandListTree(const QString& dropTargetString,QTreeWidgetItem* menuCommandFolder = new QTreeWidgetItem(this);menuCommandFolder->setFlags(Qt::ItemIsEnabled);menuCommandFolder->setText(- 0, "1"); // set tentative name for "Menu Commands" folder+ 0, "1"); // Set tentative name for "Menu Commands" foldermenuCommandFolder->setExpanded(true);menuCommandFolder->setIcon(0, invisibleRootItem()->icon(0));@@ -104,12 +112,12 @@ CommandListTree::CommandListTree(const QString& dropTargetString,addFolder(ShortcutTree::tr("SubMenu Commands"), MenuCommandType,menuCommandFolder);- // set tentative name for "Tools" folder+ // Set tentative name for "Tools" folderQTreeWidgetItem* toolsFolder = addFolder("2", ToolCommandType);QTreeWidgetItem* advancedFolder = new QTreeWidgetItem(this);advancedFolder->setFlags(Qt::ItemIsEnabled);- advancedFolder->setText(0, "3"); // set tentative name for "Advanced" folder+ advancedFolder->setText(0, "3"); // Set tentative name for "Advanced" folderadvancedFolder->setIcon(0, invisibleRootItem()->icon(0));addFolder(ShortcutTree::tr("Fill"), FillCommandType, advancedFolder);@@ -125,13 +133,13 @@ CommandListTree::CommandListTree(const QString& dropTargetString,addFolder(ShortcutTree::tr("RGBA Channels"), RGBACommandType, advancedFolder);sortItems(0, Qt::AscendingOrder);- // set the actual names after sorting items+ // Set the actual names after sorting itemsmenuCommandFolder->setText(0, ShortcutTree::tr("Menu Commands"));toolsFolder->setText(0, ShortcutTree::tr("Tools"));advancedFolder->setText(0, ShortcutTree::tr("Advanced"));if (withSeparator) {- SeparatorItem* sep = new SeparatorItem(0);+ SeparatorItem* sep = new SeparatorItem(nullptr);sep->setToolTip(0, QObject::tr("[Drag&Drop] to copy separator to %1").arg(m_dropTargetString));addTopLevelItem(sep);@@ -143,19 +151,19 @@ CommandListTree::CommandListTree(const QString& dropTargetString,QTreeWidgetItem* CommandListTree::addFolder(const QString& title,int commandType,QTreeWidgetItem* parentFolder) {- QTreeWidgetItem* folder;- if (!parentFolder)- folder = new QTreeWidgetItem(this);- else- folder = new QTreeWidgetItem(parentFolder);+ QTreeWidgetItem* folder = (parentFolder) ? new QTreeWidgetItem(parentFolder)+ : new QTreeWidgetItem(this);assert(folder);+folder->setText(0, title);folder->setIcon(0, invisibleRootItem()->icon(0));std::vector<QAction*> actions;- CommandManager::instance()->getActions((CommandType)commandType, actions);- for (int i = 0; i < (int)actions.size(); i++) {- CommandItem* item = new CommandItem(folder, actions[i]);+ CommandManager::instance()->getActions(static_cast<CommandType>(commandType),+ actions);++ for (QAction* action : actions) {+ CommandItem* item = new CommandItem(folder, action);item->setToolTip(0, QObject::tr("[Drag&Drop] to copy command to %1").arg(m_dropTargetString));}@@ -166,18 +174,20 @@ QTreeWidgetItem* CommandListTree::addFolder(const QString& title,void CommandListTree::mousePressEvent(QMouseEvent* event) {setCurrentItem(itemAt(event->pos()));- CommandItem* commandItem = dynamic_cast<CommandItem*>(itemAt(event->pos()));- SeparatorItem* separatorItem =- dynamic_cast<SeparatorItem*>(itemAt(event->pos()));+ QTreeWidgetItem* clickedItem = itemAt(event->pos());++ CommandItem* commandItem = dynamic_cast<CommandItem*>(clickedItem);+ SeparatorItem* separatorItem = dynamic_cast<SeparatorItem*>(clickedItem);if (commandItem || separatorItem) {std::string dragStr;QString dragPixmapTxt;+if (commandItem) {dragStr =CommandManager::instance()->getIdFromAction(commandItem->getAction());dragPixmapTxt = commandItem->getAction()->text();- dragPixmapTxt.remove("&");+ dragPixmapTxt.remove('&');} else {dragStr = "separator";dragPixmapTxt = tr("----Separator----");@@ -188,10 +198,13 @@ void CommandListTree::mousePressEvent(QMouseEvent* event) {QFontMetrics fm(QApplication::font());QPixmap pix(fm.boundingRect(dragPixmapTxt).adjusted(-2, -2, 2, 2).size());- QPainter painter(&pix);- painter.fillRect(pix.rect(), Qt::white);- painter.setPen(Qt::black);- painter.drawText(pix.rect(), Qt::AlignCenter, dragPixmapTxt);++ {+ QPainter painter(&pix);+ painter.fillRect(pix.rect(), Qt::white);+ painter.setPen(Qt::black);+ painter.drawText(pix.rect(), Qt::AlignCenter, dragPixmapTxt);+ }QDrag* drag = new QDrag(this);drag->setMimeData(mimeData);@@ -206,7 +219,7 @@ void CommandListTree::mousePressEvent(QMouseEvent* event) {//-----------------------------------------------------------------------------void CommandListTree::displayAll(QTreeWidgetItem* item) {- int childCount = item->childCount();+ const int childCount = item->childCount();for (int i = 0; i < childCount; ++i) {displayAll(item->child(i));}@@ -217,7 +230,7 @@ void CommandListTree::displayAll(QTreeWidgetItem* item) {//-------------------------------------------------------------------void CommandListTree::hideAll(QTreeWidgetItem* item) {- int childCount = item->childCount();+ const int childCount = item->childCount();for (int i = 0; i < childCount; ++i) {hideAll(item->child(i));}@@ -228,34 +241,39 @@ void CommandListTree::hideAll(QTreeWidgetItem* item) {//-----------------------------------------------------------------------------void CommandListTree::searchItems(const QString& searchWord) {- // if search word is empty, show all items+ // If search word is empty, show all itemsif (searchWord.isEmpty()) {- int itemCount = topLevelItemCount();+ const int itemCount = topLevelItemCount();for (int i = 0; i < itemCount; ++i) {displayAll(topLevelItem(i));}- // revert to the initial state - expanding "Menu Commands" tree- findItems(ShortcutTree::tr("Menu Commands"), Qt::MatchExactly)[0]- ->setExpanded(true);+ // Revert to the initial state - expanding "Menu Commands" tree+ auto menuCommandItems =+ findItems(ShortcutTree::tr("Menu Commands"), Qt::MatchExactly);+ if (!menuCommandItems.isEmpty()) {+ menuCommandItems.first()->setExpanded(true);+ }+update();return;}- // hide all items first- int itemCount = topLevelItemCount();+ // Hide all items first+ const int itemCount = topLevelItemCount();for (int i = 0; i < itemCount; ++i) {hideAll(topLevelItem(i));}QList<QTreeWidgetItem*> foundItems =findItems(searchWord, Qt::MatchContains | Qt::MatchRecursive, 0);- if (foundItems.isEmpty()) { // if nothing is found, do nothing but update++ if (foundItems.isEmpty()) { // If nothing is found, do nothing but updateupdate();return;}- // for each item found, show it and show its parent+ // For each item found, show it and show its parentfor (auto item : foundItems) {while (item) {item->setHidden(false);@@ -286,9 +304,9 @@ CommandBarTree::CommandBarTree(TFilePath& path, QWidget* parent)/*- Load path if it does exist. If not, then load from the template. -*/TFilePath fp;- if (TFileStatus(path).isWritable())+ if (TFileStatus(path).isWritable()) {fp = path;- else {+ } else {if (path.getName() == "xsheettoolbar") {fp = ToonzFolder::getTemplateModuleDir() + TFilePath("xsheettoolbar.xml");} else {@@ -311,30 +329,32 @@ void CommandBarTree::loadMenuTree(const TFilePath& fp) {QXmlStreamReader reader(&file);if (reader.readNextStartElement()) {- if (reader.name() == "commandbar") {+ if (reader.name() == QStringLiteral("commandbar")) {while (reader.readNextStartElement()) {- if (reader.name() == "command") {+ if (reader.name() == QStringLiteral("command")) {QString cmdName = reader.readElementText();QAction* action = CommandManager::instance()->getAction(cmdName.toStdString().c_str());if (action) {- CommandItem* item = new CommandItem(0, action);+ CommandItem* item = new CommandItem(nullptr, action);addTopLevelItem(item);}- } else if (reader.name() == "separator") {- SeparatorItem* sep = new SeparatorItem(0);+ } else if (reader.name() == QStringLiteral("separator")) {+ SeparatorItem* sep = new SeparatorItem(nullptr);addTopLevelItem(sep);reader.skipCurrentElement();- } else+ } else {reader.skipCurrentElement();+ }}- } else+ } else {reader.raiseError(QObject::tr("Incorrect file"));+ }}if (reader.hasError()) {- qDebug() << "Cannot read menubar xml";+ qDebug() << "Cannot read menubar xml:" << reader.errorString();}}@@ -343,25 +363,33 @@ void CommandBarTree::loadMenuTree(const TFilePath& fp) {void CommandBarTree::loadMenuRecursive(QXmlStreamReader& reader,QTreeWidgetItem* parentItem) {while (reader.readNextStartElement()) {- if (reader.name() == "command") {+ if (reader.name() == QStringLiteral("command")) {QString cmdName = reader.readElementText();QAction* action =CommandManager::instance()->getAction(cmdName.toStdString().c_str());- if (action) CommandItem* item = new CommandItem(parentItem, action);- } else if (reader.name() == "command_debug") {+ if (action) {+ CommandItem* item = new CommandItem(parentItem, action);+ Q_UNUSED(item);+ }+ } else if (reader.name() == QStringLiteral("command_debug")) {#ifndef NDEBUGQString cmdName = reader.readElementText();QAction* action =CommandManager::instance()->getAction(cmdName.toStdString().c_str());- if (action) CommandItem* item = new CommandItem(parentItem, action);+ if (action) {+ CommandItem* item = new CommandItem(parentItem, action);+ Q_UNUSED(item);+ }#elsereader.skipCurrentElement();#endif- } else if (reader.name() == "separator") {+ } else if (reader.name() == QStringLiteral("separator")) {SeparatorItem* sep = new SeparatorItem(parentItem);+ Q_UNUSED(sep);reader.skipCurrentElement();- } else+ } else {reader.skipCurrentElement();+ }}}@@ -370,7 +398,7 @@ void CommandBarTree::loadMenuRecursive(QXmlStreamReader& reader,void CommandBarTree::saveMenuTree(TFilePath& path) {QFile file(toQString(path));if (!file.open(QFile::WriteOnly | QFile::Text)) {- qDebug() << "Cannot read file" << file.errorString();+ qDebug() << "Cannot write file" << file.errorString();return;}@@ -379,7 +407,7 @@ void CommandBarTree::saveMenuTree(TFilePath& path) {writer.writeStartDocument();writer.writeStartElement("commandbar");- { saveMenuRecursive(writer, invisibleRootItem()); }+ saveMenuRecursive(writer, invisibleRootItem());writer.writeEndElement();writer.writeEndDocument();@@ -389,17 +417,18 @@ void CommandBarTree::saveMenuTree(TFilePath& path) {void CommandBarTree::saveMenuRecursive(QXmlStreamWriter& writer,QTreeWidgetItem* parentItem) {- for (int c = 0; c < parentItem->childCount(); c++) {+ for (int c = 0; c < parentItem->childCount(); ++c) {CommandItem* command = dynamic_cast<CommandItem*>(parentItem->child(c));SeparatorItem* sep = dynamic_cast<SeparatorItem*>(parentItem->child(c));- if (command)+ if (command) {writer.writeTextElement("command",QString::fromStdString(CommandManager::instance()->getIdFromAction(command->getAction())));- else if (sep)+ } else if (sep) {writer.writeEmptyElement("separator");+ }}}@@ -408,22 +437,28 @@ void CommandBarTree::saveMenuRecursive(QXmlStreamWriter& writer,bool CommandBarTree::dropMimeData(QTreeWidgetItem* parent, int index,const QMimeData* data,Qt::DropAction action) {+ Q_UNUSED(action);+if (data->hasText()) {- QString txt = data->text();- QTreeWidgetItem* item;- if (txt == "separator")- item = new SeparatorItem(0);- else {+ QString txt = data->text();+ QTreeWidgetItem* item = nullptr;++ if (txt == "separator") {+ item = new SeparatorItem(nullptr);+ } else {QAction* act =CommandManager::instance()->getAction(txt.toStdString().c_str());- if (!act) return false;- item = new CommandItem(0, act);+ if (!act) {+ return false;+ }+ item = new CommandItem(nullptr, act);}- if (parent)+ if (parent) {parent->insertChild(index, item);- else+ } else {insertTopLevelItem(index, item);+ }return true;}@@ -435,7 +470,7 @@ bool CommandBarTree::dropMimeData(QTreeWidgetItem* parent, int index,QStringList CommandBarTree::mimeTypes() const {QStringList qstrList;- qstrList.append("text/plain");+ qstrList.append(QStringLiteral("text/plain"));return qstrList;}@@ -443,31 +478,37 @@ QStringList CommandBarTree::mimeTypes() const {void CommandBarTree::contextMenuEvent(QContextMenuEvent* event) {QTreeWidgetItem* item = itemAt(event->pos());- if (item != currentItem()) setCurrentItem(item);- QMenu* menu = new QMenu(this);- QAction* action;+ if (item != currentItem()) {+ setCurrentItem(item);+ }++ QMenu menu(this);if (item) {- action = menu->addAction(tr("Remove \"%1\"").arg(item->text(0)));- connect(action, SIGNAL(triggered()), this, SLOT(removeItem()));+ QAction* removeAction =+ menu.addAction(tr("Remove \"%1\"").arg(item->text(0)));+ connect(removeAction, &QAction::triggered, this,+ &CommandBarTree::removeItem);}- menu->exec(event->globalPos());- delete menu;+ menu.exec(event->globalPos());}//-----------------------------------------------------------------------------void CommandBarTree::removeItem() {QTreeWidgetItem* item = currentItem();- if (!item) return;+ if (!item) {+ return;+ }- if (indexOfTopLevelItem(item) >= 0)- takeTopLevelItem(indexOfTopLevelItem(item));- else+ int topLevelIndex = indexOfTopLevelItem(item);+ if (topLevelIndex >= 0) {+ delete takeTopLevelItem(topLevelIndex);+ } else if (item->parent()) {item->parent()->removeChild(item);-- delete item;+ delete item;+ }}//=============================================================================@@ -558,26 +599,23 @@ CommandBarPopup::CommandBarPopup(bool isXsheetToolbar)}//--- signal/slot connections-- bool ret = connect(okBtn, SIGNAL(clicked()), this, SLOT(onOkPressed()));- ret = ret && connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));- ret = ret && connect(searchEdit, SIGNAL(textChanged(const QString&)), this,- SLOT(onSearchTextChanged(const QString&)));-- assert(ret);+ connect(okBtn, &QPushButton::clicked, this, &CommandBarPopup::onOkPressed);+ connect(cancelBtn, &QPushButton::clicked, this, &QDialog::reject);+ connect(searchEdit, &QLineEdit::textChanged, this,+ &CommandBarPopup::onSearchTextChanged);}//-----------------------------------------------------------------------------void CommandBarPopup::onOkPressed() {m_menuBarTree->saveMenuTree(m_path);-accept();}void CommandBarPopup::onSearchTextChanged(const QString& text) {static bool busy = false;if (busy) return;+busy = true;m_commandListTree->searchItems(text);busy = false;diff --git a/toonz/sources/toonz/commandbarpopup.h b/toonz/sources/toonz/commandbarpopup.hindex c80f60a53..5067f91b1 100644--- a/toonz/sources/toonz/commandbarpopup.h+++ b/toonz/sources/toonz/commandbarpopup.h@@ -21,7 +21,8 @@ class CommandItem final : public QTreeWidgetItem {QAction* m_action;public:- CommandItem(QTreeWidgetItem* parent, QAction* action);+ explicit CommandItem(QTreeWidgetItem* parent, QAction* action);+QAction* getAction() const { return m_action; }};@@ -31,12 +32,12 @@ public:class SeparatorItem final : public QTreeWidgetItem {public:- SeparatorItem(QTreeWidgetItem* parent);+ explicit SeparatorItem(QTreeWidgetItem* parent);};//=============================================================================// CommandListTree-// shared by menubar popup and cutom panel editor popup+// Shared by menubar popup and custom panel editor popup//-----------------------------------------------------------------------------class CommandListTree final : public QTreeWidget {@@ -45,11 +46,12 @@ class CommandListTree final : public QTreeWidget {QString m_dropTargetString;QTreeWidgetItem* addFolder(const QString& title, int commandType,- QTreeWidgetItem* parentFolder = 0);+ QTreeWidgetItem* parentFolder = nullptr);public:- CommandListTree(const QString& dropTargetString, QWidget* parent = 0,- bool withSeparator = true);+ explicit CommandListTree(const QString& dropTargetString,+ QWidget* parent = nullptr,+ bool withSeparator = true);void searchItems(const QString& searchWord = QString());@@ -58,7 +60,7 @@ private:void hideAll(QTreeWidgetItem* item);protected:- void mousePressEvent(QMouseEvent*) override;+ void mousePressEvent(QMouseEvent* event) override;};//=============================================================================@@ -73,15 +75,19 @@ class CommandBarTree final : public QTreeWidget {void saveMenuRecursive(QXmlStreamWriter& writer, QTreeWidgetItem* parentItem);public:- CommandBarTree(TFilePath& path, QWidget* parent = 0);+ explicit CommandBarTree(TFilePath& path, QWidget* parent = nullptr);+void saveMenuTree(TFilePath& path);protected:bool dropMimeData(QTreeWidgetItem* parent, int index, const QMimeData* data,Qt::DropAction action) override;+QStringList mimeTypes() const override;+void contextMenuEvent(QContextMenuEvent* event) override;-protected slots:++private slots:void removeItem();};@@ -91,15 +97,17 @@ protected slots:class CommandBarPopup final : public DVGui::Dialog {Q_OBJECT+CommandListTree* m_commandListTree;CommandBarTree* m_menuBarTree;TFilePath m_path;public:- CommandBarPopup(bool isXsheetToolbar = false);-protected slots:+ explicit CommandBarPopup(bool isXsheetToolbar = false);++private slots:void onOkPressed();void onSearchTextChanged(const QString& text);};-#endif\ No newline at end of file+#endif // COMMANDBARPOPUP_H