| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| using namespace DVGui; |
| |
| |
| |
| namespace |
| { |
| |
| void updateToolTip(QAction *action) |
| { |
| QString tooltip = action->text(); |
| QString shortcut = action->shortcut().toString(); |
| if (shortcut != "") |
| tooltip += " (" + shortcut + ")"; |
| action->setToolTip(tooltip); |
| } |
| |
| } |
| |
| |
| |
| AuxActionsCreator::AuxActionsCreator() |
| { |
| AuxActionsCreatorManager::instance()->addAuxActionsCreator(this); |
| } |
| |
| |
| AuxActionsCreatorManager::AuxActionsCreatorManager() |
| : m_auxActionsCreated(false) |
| { |
| } |
| |
| AuxActionsCreatorManager *AuxActionsCreatorManager::instance() |
| { |
| static AuxActionsCreatorManager _instance; |
| return &_instance; |
| } |
| |
| void AuxActionsCreatorManager::addAuxActionsCreator(AuxActionsCreator *auxActionsCreator) |
| { |
| m_auxActionsCreators.push_back(auxActionsCreator); |
| } |
| |
| void AuxActionsCreatorManager::createAuxActions(QObject *parent) |
| { |
| if (m_auxActionsCreated) |
| return; |
| m_auxActionsCreated = true; |
| for (int i = 0; i < (int)m_auxActionsCreators.size(); i++) |
| m_auxActionsCreators[i]->createActions(parent); |
| } |
| |
| |
| |
| CommandManager::CommandManager() |
| { |
| } |
| |
| |
| |
| CommandManager::~CommandManager() |
| { |
| std::map<std::string, Node *>::iterator it; |
| for (it = m_idTable.begin(); it != m_idTable.end(); ++it) |
| delete it->second; |
| } |
| |
| |
| |
| CommandManager *CommandManager::instance() |
| { |
| static CommandManager _instance; |
| return &_instance; |
| } |
| |
| |
| |
| |
| |
| |
| CommandManager::Node *CommandManager::getNode(CommandId id, bool createIfNeeded) |
| { |
| AuxActionsCreatorManager::instance()->createAuxActions(qApp); |
| std::map<std::string, Node *>::iterator it = m_idTable.find(id); |
| if (it != m_idTable.end()) |
| return it->second; |
| if (createIfNeeded) { |
| Node *node = new Node(id); |
| m_idTable[id] = node; |
| return node; |
| } else |
| return 0; |
| } |
| |
| |
| |
| void CommandManager::setShortcut(CommandId id, QAction *action, std::string shortcutString) |
| { |
| if (shortcutString != "") |
| action->setShortcut(QKeySequence(QString::fromStdString(shortcutString))); |
| else |
| action->setShortcut(QKeySequence()); |
| TFilePath fp = ToonzFolder::getMyModuleDir() + TFilePath("shortcuts.ini"); |
| QSettings settings(toQString(fp), QSettings::IniFormat); |
| settings.beginGroup("shortcuts"); |
| settings.setValue(QString(id), QString::fromStdString(shortcutString)); |
| settings.endGroup(); |
| } |
| |
| |
| |
| void CommandManager::define( |
| CommandId id, |
| CommandType type, |
| std::string defaultShortcutString, |
| QAction *qaction) |
| { |
| assert(type != UndefinedCommandType); |
| assert(qaction != 0); |
| assert(m_qactionTable.count(qaction) == 0); |
| |
| Node *node = getNode(id); |
| if (node->m_type != UndefinedCommandType) { |
| assert(!"Duplicate command id"); |
| } |
| node->m_type = type; |
| node->m_qaction = qaction; |
| node->m_qaction->setEnabled( |
| node->m_enabled && (!!node->m_handler || node->m_qaction->actionGroup() != 0) || |
| node->m_type == MiscCommandType || node->m_type == ToolModifierCommandType); |
| |
| m_qactionTable[qaction] = node; |
| qaction->setShortcutContext(Qt::ApplicationShortcut); |
| |
| TFilePath fp = ToonzFolder::getModuleFile("shortcuts.ini"); |
| QSettings settings(toQString(fp), QSettings::IniFormat); |
| settings.beginGroup("shortcuts"); |
| QString defaultShortcutQString = QString::fromStdString(defaultShortcutString); |
| QString shortcutString = settings.value(id).toString(); |
| settings.endGroup(); |
| |
| if (shortcutString != "") { |
| |
| |
| QAction *other = getActionFromShortcut(shortcutString.toStdString()); |
| if (other) |
| other->setShortcut(QKeySequence()); |
| } else if (defaultShortcutQString != "") { |
| |
| QAction *other = getActionFromShortcut(defaultShortcutQString.toStdString()); |
| if (!other) |
| shortcutString = defaultShortcutQString; |
| } |
| |
| if (shortcutString != "") { |
| qaction->setShortcut(QKeySequence(shortcutString)); |
| m_shortcutTable[shortcutString.toStdString()] = node; |
| } |
| |
| if (type == ToolCommandType) |
| updateToolTip(qaction); |
| } |
| |
| |
| |
| |
| |
| |
| |
| void CommandManager::setHandler(CommandId id, CommandHandlerInterface *handler) |
| { |
| Node *node = getNode(id); |
| if (node->m_handler != handler) { |
| delete node->m_handler; |
| node->m_handler = handler; |
| } |
| if (node->m_qaction) { |
| node->m_qaction->setEnabled(node->m_enabled && (!!node->m_handler || node->m_qaction->actionGroup() != 0)); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| void CommandManager::execute(QAction *qaction) |
| { |
| assert(qaction); |
| std::map<QAction *, Node *>::iterator it = m_qactionTable.find(qaction); |
| assert(it != m_qactionTable.end()); |
| if (it != m_qactionTable.end() && it->second->m_handler) { |
| it->second->m_handler->execute(); |
| } |
| } |
| |
| |
| |
| void CommandManager::execute(QAction *action, QAction *menuAction) |
| { |
| std::map<QAction *, Node *>::iterator it = m_qactionTable.find(action); |
| if (it != m_qactionTable.end()) |
| execute(action); |
| else |
| execute(menuAction); |
| } |
| |
| |
| |
| void CommandManager::execute(CommandId id) |
| { |
| Node *node = getNode(id, false); |
| if (node && node->m_handler) { |
| QAction *action = node->m_qaction; |
| if (action && action->isCheckable()) { |
| |
| action->setChecked(true); |
| } |
| node->m_handler->execute(); |
| } |
| } |
| |
| |
| |
| void CommandManager::getActions(CommandType type, std::vector<QAction *> &actions) |
| { |
| AuxActionsCreatorManager::instance()->createAuxActions(qApp); |
| std::map<QAction *, Node *>::iterator it; |
| for (it = m_qactionTable.begin(); it != m_qactionTable.end(); ++it) |
| if (it->second->m_type == type) |
| actions.push_back(it->first); |
| } |
| |
| |
| |
| QAction *CommandManager::getActionFromShortcut(std::string shortcutString) |
| { |
| std::map<std::string, Node *>::iterator it = m_shortcutTable.find(shortcutString); |
| return it != m_shortcutTable.end() ? it->second->m_qaction : 0; |
| } |
| |
| |
| |
| std::string CommandManager::getShortcutFromAction(QAction *action) |
| { |
| std::map<std::string, Node *>::iterator it = m_shortcutTable.begin(); |
| for (; it != m_shortcutTable.end(); ++it) { |
| if (it->second->m_qaction == action) |
| return it->first; |
| } |
| return ""; |
| } |
| |
| |
| |
| std::string CommandManager::getShortcutFromId(const char *id) |
| { |
| QAction *action = getAction(id); |
| assert(action); |
| if (!action) |
| return ""; |
| return getShortcutFromAction(action); |
| } |
| |
| |
| |
| int CommandManager::getKeyFromShortcut(const std::string &shortcut) |
| { |
| QString qShortcut = QString::fromStdString(shortcut); |
| if (qShortcut == "") |
| return 0; |
| |
| QKeySequence ks(qShortcut); |
| assert(ks.count() == 1); |
| return ks[0]; |
| } |
| |
| |
| |
| int CommandManager::getKeyFromId(const char *id) |
| { |
| return getKeyFromShortcut(getShortcutFromId(id)); |
| } |
| |
| |
| |
| void CommandManager::setShortcut(QAction *action, std::string shortcutString) |
| { |
| QString shortcut = QString::fromStdString(shortcutString); |
| |
| std::string oldShortcutString = action->shortcut().toString().toStdString(); |
| if (oldShortcutString == shortcutString) |
| return; |
| |
| |
| std::map<QAction *, Node *>::iterator it = m_qactionTable.find(action); |
| Node *node = it != m_qactionTable.end() ? it->second : 0; |
| assert(node); |
| assert(node->m_qaction == action); |
| |
| QKeySequence ks(shortcut); |
| assert(ks.count() == 1 || ks.count() == 0 && shortcut == ""); |
| |
| if (node->m_type == ZoomCommandType && ks.count() > 1) { |
| MsgBox(WARNING, QObject::tr("It is not possible to assing a shortcut with modifiers to the visualization commands.")); |
| return; |
| } |
| |
| QString oldActionId; |
| std::map<std::string, Node *>::iterator sit = m_shortcutTable.find(shortcutString); |
| if (sit != m_shortcutTable.end()) { |
| |
| oldActionId = QString::fromStdString(sit->second->m_id); |
| sit->second->m_qaction->setShortcut(QKeySequence()); |
| if (sit->second->m_type == ToolCommandType) |
| updateToolTip(sit->second->m_qaction); |
| } |
| |
| action->setShortcut(QKeySequence::fromString(QString::fromStdString(shortcutString))); |
| if (node->m_type == ToolCommandType) |
| updateToolTip(action); |
| |
| |
| |
| if (oldShortcutString != "") |
| m_shortcutTable.erase(oldShortcutString); |
| |
| m_shortcutTable[shortcutString] = node; |
| |
| |
| TFilePath fp = ToonzFolder::getMyModuleDir() + TFilePath("shortcuts.ini"); |
| QSettings settings(toQString(fp), QSettings::IniFormat); |
| settings.beginGroup("shortcuts"); |
| settings.setValue( |
| QString::fromStdString(node->m_id), |
| QString::fromStdString(shortcutString)); |
| if (oldActionId != "") |
| settings.remove(oldActionId); |
| settings.endGroup(); |
| } |
| |
| |
| |
| void CommandManager::enable(CommandId id, bool enabled) |
| { |
| Node *node = getNode(id, false); |
| if (!node) |
| return; |
| if (node->m_enabled == enabled) |
| return; |
| node->m_enabled = enabled; |
| if (node->m_qaction) |
| node->m_qaction->setEnabled( |
| node->m_enabled && (!!node->m_handler || node->m_qaction->actionGroup() != 0)); |
| } |
| |
| |
| |
| void CommandManager::setChecked(CommandId id, bool checked) |
| { |
| Node *node = getNode(id, false); |
| if (!node) |
| return; |
| if (node->m_qaction) { |
| node->m_qaction->setChecked(checked); |
| if (node->m_handler) |
| node->m_handler->execute(); |
| } |
| } |
| |
| |
| |
| QAction *CommandManager::getAction(CommandId id, bool createIfNeeded) |
| { |
| Node *node = getNode(id, createIfNeeded); |
| if (node) { |
| return node->m_qaction; |
| } else { |
| return 0; |
| } |
| } |
| |
| |
| |
| QAction *CommandManager::createAction(CommandId id, QObject *parent, bool state) |
| { |
| Node *node = getNode(id, false); |
| if (!node) |
| return 0; |
| QAction *refAction = node->m_qaction; |
| if (!refAction) |
| return 0; |
| QString text = refAction->text(); |
| if (node->m_onText != "" && node->m_offText != "") |
| text = state ? node->m_onText : node->m_offText; |
| QAction *action = new QAction(text, parent); |
| action->setShortcut(refAction->shortcut()); |
| return action; |
| } |
| |
| |
| |
| void CommandManager::setToggleTexts(CommandId id, const QString &onText, const QString &offText) |
| { |
| Node *node = getNode(id, false); |
| if (node) { |
| node->m_onText = onText; |
| node->m_offText = offText; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| MenuItemHandler::MenuItemHandler(const char *cmdId) |
| { |
| CommandManager::instance()->setHandler(cmdId, |
| new CommandHandlerHelper<MenuItemHandler>(this, &MenuItemHandler::execute)); |
| } |
| |
| |
| |
| |
| |
| DVAction::DVAction(const QString &text, QObject *parent) |
| : QAction(text, parent) |
| { |
| connect(this, SIGNAL(triggered()), this, SLOT(onTriggered())); |
| } |
| |
| |
| |
| DVAction::DVAction(const QIcon &icon, const QString &text, QObject *parent) |
| : QAction(icon, text, parent) |
| { |
| connect(this, SIGNAL(triggered()), this, SLOT(onTriggered())); |
| } |
| |
| |
| |
| void DVAction::onTriggered() |
| { |
| CommandManager::instance()->execute(this); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| DVMenuAction::DVMenuAction(const QString &text, QWidget *parent, QList<QString> actions) |
| : QMenu(text, parent), m_triggeredActionIndex(-1) |
| { |
| int i; |
| for (i = 0; i < actions.size(); i++) |
| addAction(actions.at(i)); |
| connect(this, SIGNAL(triggered(QAction *)), this, SLOT(onTriggered(QAction *))); |
| } |
| |
| |
| |
| |
| |
| void DVMenuAction::setActions(QList<QString> actions) |
| { |
| if (m_triggeredActionIndex != -1) |
| return; |
| clear(); |
| int i; |
| for (i = 0; i < actions.size(); i++) { |
| QString actionId = actions.at(i); |
| |
| QAction *action = CommandManager::instance()->getAction(actionId.toStdString().c_str()); |
| if (action) { |
| addAction(action); |
| continue; |
| } |
| |
| action = addAction(actionId); |
| action->setData(QVariant(i)); |
| } |
| } |
| |
| |
| |
| namespace |
| { |
| QString changeStringNumber(QString str, int index) |
| { |
| QString newStr = str; |
| int n = 3; |
| if (index >= 10) |
| n = 4; |
| QString number; |
| newStr.replace(0, n, number.number(index + 1) + QString(". ")); |
| return newStr; |
| } |
| } |
| |
| |
| |
| void DVMenuAction::onTriggered(QAction *action) |
| { |
| QVariant data = action->data(); |
| if (data.isValid()) |
| m_triggeredActionIndex = data.toInt(); |
| CommandManager::instance()->execute(action, menuAction()); |
| int oldIndex = m_triggeredActionIndex; |
| if (m_triggeredActionIndex != -1) |
| m_triggeredActionIndex = -1; |
| QString str = data.toString(); |
| QAction *tableAction = CommandManager::instance()->getAction(str.toStdString().c_str()); |
| if (tableAction || oldIndex == 0) |
| return; |
| QList<QAction *> acts = actions(); |
| removeAction(action); |
| insertAction(acts[0], action); |
| |
| acts = actions(); |
| int i; |
| for (i = 0; i <= oldIndex; i++) { |
| QAction *a = acts.at(i); |
| QString newTxt = changeStringNumber(a->text(), i); |
| a->setText(newTxt); |
| a->setData(QVariant(i)); |
| } |
| m_triggeredActionIndex = -1; |
| } |
| |
| |
| |