Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/menubarcommand.h"
Toshihiro Shimizu 890ddd
//#include "menubarcommandids.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/gutil.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzfolders.h"
shun_iwasawa cf18d8
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
#include <qobject></qobject>
Toshihiro Shimizu 890ddd
#include <qaction></qaction>
Toshihiro Shimizu 890ddd
#include <qsettings></qsettings>
Toshihiro Shimizu 890ddd
#include <qkeysequence></qkeysequence>
Toshihiro Shimizu 890ddd
#include <qapplication></qapplication>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace DVGui;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void updateToolTip(QAction *action) {
Shinya Kitaoka 120a6e
  QString tooltip  = action->text();
Shinya Kitaoka 120a6e
  QString shortcut = action->shortcut().toString();
Shinya Kitaoka 120a6e
  if (shortcut != "") tooltip += " (" + shortcut + ")";
Shinya Kitaoka 120a6e
  action->setToolTip(tooltip);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
AuxActionsCreator::AuxActionsCreator() {
Shinya Kitaoka 120a6e
  AuxActionsCreatorManager::instance()->addAuxActionsCreator(this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
AuxActionsCreatorManager::AuxActionsCreatorManager()
Shinya Kitaoka 120a6e
    : m_auxActionsCreated(false) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
AuxActionsCreatorManager *AuxActionsCreatorManager::instance() {
Shinya Kitaoka 120a6e
  static AuxActionsCreatorManager _instance;
Shinya Kitaoka 120a6e
  return &_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void AuxActionsCreatorManager::addAuxActionsCreator(
Shinya Kitaoka 120a6e
    AuxActionsCreator *auxActionsCreator) {
Shinya Kitaoka 120a6e
  m_auxActionsCreators.push_back(auxActionsCreator);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void AuxActionsCreatorManager::createAuxActions(QObject *parent) {
Shinya Kitaoka 120a6e
  if (m_auxActionsCreated) return;
Shinya Kitaoka 120a6e
  m_auxActionsCreated = true;
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)m_auxActionsCreators.size(); i++)
Shinya Kitaoka 120a6e
    m_auxActionsCreators[i]->createActions(parent);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
CommandManager::CommandManager() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
CommandManager::~CommandManager() {
Shinya Kitaoka 120a6e
  std::map<std::string, *="" node="">::iterator it;</std::string,>
Shinya Kitaoka 120a6e
  for (it = m_idTable.begin(); it != m_idTable.end(); ++it) delete it->second;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
CommandManager *CommandManager::instance() {
Shinya Kitaoka 120a6e
  static CommandManager _instance;
Shinya Kitaoka 120a6e
  return &_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// command id => command
Toshihiro Shimizu 890ddd
//
Shinya Kitaoka 120a6e
CommandManager::Node *CommandManager::getNode(CommandId id,
Shinya Kitaoka 120a6e
                                              bool createIfNeeded) {
Shinya Kitaoka 120a6e
  AuxActionsCreatorManager::instance()->createAuxActions(qApp);
Shinya Kitaoka 120a6e
  std::map<std::string, *="" node="">::iterator it = m_idTable.find(id);</std::string,>
Shinya Kitaoka 120a6e
  if (it != m_idTable.end()) return it->second;
Shinya Kitaoka 120a6e
  if (createIfNeeded) {
Shinya Kitaoka 120a6e
    Node *node    = new Node(id);
Shinya Kitaoka 120a6e
    m_idTable[id] = node;
Shinya Kitaoka 120a6e
    return node;
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CommandManager::setShortcut(CommandId id, QAction *action,
Shinya Kitaoka 120a6e
                                 std::string shortcutString) {
Shinya Kitaoka 120a6e
  if (shortcutString != "")
Shinya Kitaoka 120a6e
    action->setShortcut(QKeySequence(QString::fromStdString(shortcutString)));
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    action->setShortcut(QKeySequence());
Shinya Kitaoka 120a6e
  TFilePath fp = ToonzFolder::getMyModuleDir() + TFilePath("shortcuts.ini");
Shinya Kitaoka 120a6e
  QSettings settings(toQString(fp), QSettings::IniFormat);
Shinya Kitaoka 120a6e
  settings.beginGroup("shortcuts");
Shinya Kitaoka 120a6e
  settings.setValue(QString(id), QString::fromStdString(shortcutString));
Shinya Kitaoka 120a6e
  settings.endGroup();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CommandManager::define(CommandId id, CommandType type,
Shinya Kitaoka 120a6e
                            std::string defaultShortcutString,
Shinya Kitaoka 120a6e
                            QAction *qaction) {
Shinya Kitaoka 120a6e
  assert(type != UndefinedCommandType);
Shinya Kitaoka 120a6e
  assert(qaction != 0);
Shinya Kitaoka 120a6e
  assert(m_qactionTable.count(qaction) == 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Node *node = getNode(id);
Shinya Kitaoka 120a6e
  if (node->m_type != UndefinedCommandType) {
Shinya Kitaoka 120a6e
    assert(!"Duplicate command id");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  node->m_type    = type;
Shinya Kitaoka 120a6e
  node->m_qaction = qaction;
Shinya Kitaoka 120a6e
  node->m_qaction->setEnabled(
Rozhuk Ivan 823a31
      (node->m_enabled &&
Rozhuk Ivan 823a31
          (node->m_handler || node->m_qaction->actionGroup() != 0)) ||
Shinya Kitaoka 120a6e
      node->m_type == MiscCommandType ||
Shinya Kitaoka 120a6e
      node->m_type == ToolModifierCommandType);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_qactionTable[qaction] = node;
Shinya Kitaoka 120a6e
  qaction->setShortcutContext(Qt::ApplicationShortcut);
shun_iwasawa cf18d8
  // user defined shortcuts will be loaded afterwards in loadShortcuts()
Shinya Kitaoka 120a6e
  QString defaultShortcutQString =
Shinya Kitaoka 120a6e
      QString::fromStdString(defaultShortcutString);
shun_iwasawa cf18d8
  if (!defaultShortcutQString.isEmpty()) {
shun_iwasawa cf18d8
    qaction->setShortcut(QKeySequence(defaultShortcutQString));
shun_iwasawa cf18d8
    m_shortcutTable[defaultShortcutString] = node;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (type == ToolCommandType) updateToolTip(qaction);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// set handler (id, handler)
Toshihiro Shimizu 890ddd
//   possibly changes enable/disable qaction state
Toshihiro Shimizu 890ddd
//
Shinya Kitaoka 120a6e
void CommandManager::setHandler(CommandId id,
Shinya Kitaoka 120a6e
                                CommandHandlerInterface *handler) {
Shinya Kitaoka 120a6e
  Node *node = getNode(id);
Shinya Kitaoka 120a6e
  if (node->m_handler != handler) {
Shinya Kitaoka 120a6e
    delete node->m_handler;
Shinya Kitaoka 120a6e
    node->m_handler = handler;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (node->m_qaction) {
Shinya Kitaoka 120a6e
    node->m_qaction->setEnabled(
Shinya Kitaoka 120a6e
        node->m_enabled &&
Shinya Kitaoka 120a6e
        (!!node->m_handler || node->m_qaction->actionGroup() != 0));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// qaction -> command; execute
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CommandManager::execute(QAction *qaction) {
Shinya Kitaoka 120a6e
  assert(qaction);
Shinya Kitaoka 120a6e
  std::map<qaction *="" *,="" node="">::iterator it = m_qactionTable.find(qaction);</qaction>
Shinya Kitaoka 120a6e
  assert(it != m_qactionTable.end());
Shinya Kitaoka 120a6e
  if (it != m_qactionTable.end() && it->second->m_handler) {
Shinya Kitaoka 120a6e
    it->second->m_handler->execute();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CommandManager::execute(QAction *action, QAction *menuAction) {
Shinya Kitaoka 120a6e
  std::map<qaction *="" *,="" node="">::iterator it = m_qactionTable.find(action);</qaction>
Shinya Kitaoka 120a6e
  if (it != m_qactionTable.end())
Shinya Kitaoka 120a6e
    execute(action);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    execute(menuAction);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CommandManager::execute(CommandId id) {
Shinya Kitaoka 120a6e
  Node *node = getNode(id, false);
Shinya Kitaoka 120a6e
  if (node && node->m_handler) {
Shinya Kitaoka 120a6e
    QAction *action = node->m_qaction;
Shinya Kitaoka 120a6e
    if (action && action->isCheckable()) {
Shinya Kitaoka 120a6e
      // principalmente per i tool
Shinya Kitaoka 120a6e
      action->setChecked(true);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    node->m_handler->execute();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CommandManager::getActions(CommandType type,
Shinya Kitaoka 120a6e
                                std::vector<qaction *=""> &actions) {</qaction>
Shinya Kitaoka 120a6e
  AuxActionsCreatorManager::instance()->createAuxActions(qApp);
Shinya Kitaoka 120a6e
  std::map<qaction *="" *,="" node="">::iterator it;</qaction>
Shinya Kitaoka 120a6e
  for (it = m_qactionTable.begin(); it != m_qactionTable.end(); ++it)
Shinya Kitaoka 120a6e
    if (it->second->m_type == type) actions.push_back(it->first);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QAction *CommandManager::getActionFromShortcut(std::string shortcutString) {
Shinya Kitaoka 120a6e
  std::map<std::string, *="" node="">::iterator it =</std::string,>
Shinya Kitaoka 120a6e
      m_shortcutTable.find(shortcutString);
Shinya Kitaoka 120a6e
  return it != m_shortcutTable.end() ? it->second->m_qaction : 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::string CommandManager::getShortcutFromAction(QAction *action) {
Shinya Kitaoka 120a6e
  std::map<std::string, *="" node="">::iterator it = m_shortcutTable.begin();</std::string,>
Shinya Kitaoka 120a6e
  for (; it != m_shortcutTable.end(); ++it) {
Shinya Kitaoka 120a6e
    if (it->second->m_qaction == action) return it->first;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::string CommandManager::getShortcutFromId(const char *id) {
Shinya Kitaoka 120a6e
  QAction *action = getAction(id);
Shinya Kitaoka 120a6e
  assert(action);
Shinya Kitaoka 120a6e
  if (!action) return "";
Shinya Kitaoka 120a6e
  return getShortcutFromAction(action);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int CommandManager::getKeyFromShortcut(const std::string &shortcut) {
Shinya Kitaoka 120a6e
  QString qShortcut = QString::fromStdString(shortcut);
Shinya Kitaoka 120a6e
  if (qShortcut == "") return 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QKeySequence ks(qShortcut);
Shinya Kitaoka 120a6e
  assert(ks.count() == 1);
Shinya Kitaoka 120a6e
  return ks[0];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int CommandManager::getKeyFromId(const char *id) {
Shinya Kitaoka 120a6e
  return getKeyFromShortcut(getShortcutFromId(id));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Jeremy Bullock e8903b
void CommandManager::setShortcut(QAction *action, std::string shortcutString,
Jeremy Bullock e8903b
                                 bool keepDefault) {
Shinya Kitaoka 120a6e
  QString shortcut = QString::fromStdString(shortcutString);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string oldShortcutString = action->shortcut().toString().toStdString();
Shinya Kitaoka 120a6e
  if (oldShortcutString == shortcutString) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Cerco il nodo corrispondente ad action. Deve esistere
Shinya Kitaoka 120a6e
  std::map<qaction *="" *,="" node="">::iterator it = m_qactionTable.find(action);</qaction>
Shinya Kitaoka 120a6e
  Node *node = it != m_qactionTable.end() ? it->second : 0;
Shinya Kitaoka 120a6e
  assert(node);
Shinya Kitaoka 120a6e
  assert(node->m_qaction == action);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QKeySequence ks(shortcut);
Shinya Kitaoka 120a6e
  assert(ks.count() == 1 || ks.count() == 0 && shortcut == "");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (node->m_type == ZoomCommandType && ks.count() > 1) {
Shinya Kitaoka 120a6e
    DVGui::warning(
Jeremy Bullock e8903b
        QObject::tr("It is not possible to assign a shortcut with modifiers to "
Shinya Kitaoka 120a6e
                    "the visualization commands."));
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // lo shortcut e' gia' assegnato?
Shinya Kitaoka 120a6e
  QString oldActionId;
Shinya Kitaoka 120a6e
  std::map<std::string, *="" node="">::iterator sit =</std::string,>
Shinya Kitaoka 120a6e
      m_shortcutTable.find(shortcutString);
Shinya Kitaoka 120a6e
  if (sit != m_shortcutTable.end()) {
Shinya Kitaoka 120a6e
    // la vecchia azione non ha piu' shortcut
Shinya Kitaoka 120a6e
    oldActionId = QString::fromStdString(sit->second->m_id);
Shinya Kitaoka 120a6e
    sit->second->m_qaction->setShortcut(QKeySequence());
Shinya Kitaoka 120a6e
    if (sit->second->m_type == ToolCommandType)
Shinya Kitaoka 120a6e
      updateToolTip(sit->second->m_qaction);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // assegno lo shortcut all'azione
Shinya Kitaoka 120a6e
  action->setShortcut(
Shinya Kitaoka 120a6e
      QKeySequence::fromString(QString::fromStdString(shortcutString)));
Shinya Kitaoka 120a6e
  if (node->m_type == ToolCommandType) updateToolTip(action);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Aggiorno la tabella shortcut -> azioni
Shinya Kitaoka 120a6e
  // Cancello il riferimento all'eventuale vecchio shortcut di action
Shinya Kitaoka 120a6e
  if (oldShortcutString != "") m_shortcutTable.erase(oldShortcutString);
Shinya Kitaoka 120a6e
  // e aggiungo il nuovo legame
Shinya Kitaoka 120a6e
  m_shortcutTable[shortcutString] = node;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // registro il tutto
Shinya Kitaoka 120a6e
  TFilePath fp = ToonzFolder::getMyModuleDir() + TFilePath("shortcuts.ini");
Shinya Kitaoka 120a6e
  QSettings settings(toQString(fp), QSettings::IniFormat);
Shinya Kitaoka 120a6e
  settings.beginGroup("shortcuts");
Shinya Kitaoka 120a6e
  settings.setValue(QString::fromStdString(node->m_id),
Shinya Kitaoka 120a6e
                    QString::fromStdString(shortcutString));
Jeremy Bullock e8903b
  if (keepDefault) {
Jeremy Bullock e8903b
    if (oldActionId != "") settings.remove(oldActionId);
Jeremy Bullock e8903b
  }
Shinya Kitaoka 120a6e
  settings.endGroup();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CommandManager::enable(CommandId id, bool enabled) {
Shinya Kitaoka 120a6e
  Node *node = getNode(id, false);
Shinya Kitaoka 120a6e
  if (!node) return;
Shinya Kitaoka 120a6e
  if (node->m_enabled == enabled) return;
Shinya Kitaoka 120a6e
  node->m_enabled = enabled;
Shinya Kitaoka 120a6e
  if (node->m_qaction)
Shinya Kitaoka 120a6e
    node->m_qaction->setEnabled(
Shinya Kitaoka 120a6e
        node->m_enabled &&
Shinya Kitaoka 120a6e
        (!!node->m_handler || node->m_qaction->actionGroup() != 0));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CommandManager::setChecked(CommandId id, bool checked) {
Shinya Kitaoka 120a6e
  Node *node = getNode(id, false);
Shinya Kitaoka 120a6e
  if (!node) return;
Shinya Kitaoka 120a6e
  if (node->m_qaction) {
Shinya Kitaoka 120a6e
    node->m_qaction->setChecked(checked);
Shinya Kitaoka 120a6e
    if (node->m_handler) node->m_handler->execute();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QAction *CommandManager::getAction(CommandId id, bool createIfNeeded) {
Shinya Kitaoka 120a6e
  Node *node = getNode(id, createIfNeeded);
Shinya Kitaoka 120a6e
  if (node) {
Shinya Kitaoka 120a6e
    return node->m_qaction;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QAction *CommandManager::createAction(CommandId id, QObject *parent,
Shinya Kitaoka 120a6e
                                      bool state) {
Shinya Kitaoka 120a6e
  Node *node = getNode(id, false);
Shinya Kitaoka 120a6e
  if (!node) return 0;
Shinya Kitaoka 120a6e
  QAction *refAction = node->m_qaction;
Shinya Kitaoka 120a6e
  if (!refAction) return 0;
Shinya Kitaoka 120a6e
  QString text = refAction->text();
Shinya Kitaoka 120a6e
  if (node->m_onText != "" && node->m_offText != "")
Shinya Kitaoka 120a6e
    text          = state ? node->m_onText : node->m_offText;
Shinya Kitaoka 120a6e
  QAction *action = new QAction(text, parent);
Shinya Kitaoka 120a6e
  action->setShortcut(refAction->shortcut());
Shinya Kitaoka 120a6e
  return action;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CommandManager::setToggleTexts(CommandId id, const QString &onText,
Shinya Kitaoka 120a6e
                                    const QString &offText) {
Shinya Kitaoka 120a6e
  Node *node = getNode(id, false);
Shinya Kitaoka 120a6e
  if (node) {
Shinya Kitaoka 120a6e
    node->m_onText  = onText;
Shinya Kitaoka 120a6e
    node->m_offText = offText;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun_iwasawa dde155
//---------------------------------------------------------
shun_iwasawa dde155
Shinya Kitaoka 120a6e
std::string CommandManager::getIdFromAction(QAction *action) {
Shinya Kitaoka 120a6e
  std::map<qaction *="" *,="" node="">::iterator it = m_qactionTable.find(action);</qaction>
Shinya Kitaoka 120a6e
  if (it != m_qactionTable.end())
Shinya Kitaoka 120a6e
    return it->second->m_id;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return "";
shun_iwasawa dde155
}
shun_iwasawa dde155
shun_iwasawa cf18d8
//---------------------------------------------------------
shun_iwasawa cf18d8
// load user defined shortcuts
shun_iwasawa cf18d8
shun_iwasawa cf18d8
void CommandManager::loadShortcuts() {
shun_iwasawa cf18d8
  TFilePath fp = ToonzFolder::getMyModuleDir() + TFilePath("shortcuts.ini");
shun_iwasawa cf18d8
  if (!TFileStatus(fp).doesExist()) {
shun_iwasawa cf18d8
    // if user shortcut file does not exist, then try to load from template
shun_iwasawa cf18d8
    TFilePath tmplFp =
shun_iwasawa cf18d8
        ToonzFolder::getTemplateModuleDir() + TFilePath("shortcuts.ini");
shun_iwasawa cf18d8
    if (TFileStatus(tmplFp).doesExist()) TSystem::copyFile(fp, tmplFp);
shun_iwasawa cf18d8
    // if neither settings exist, do nothing and return
shun_iwasawa cf18d8
    else
shun_iwasawa cf18d8
      return;
shun_iwasawa cf18d8
  }
shun_iwasawa cf18d8
shun_iwasawa cf18d8
  QSettings settings(toQString(fp), QSettings::IniFormat);
shun_iwasawa cf18d8
  settings.beginGroup("shortcuts");
shun_iwasawa cf18d8
  QStringList ids = settings.allKeys();
shun_iwasawa cf18d8
  for (int i = 0; i < ids.size(); i++) {
shun_iwasawa cf18d8
    std::string id   = ids.at(i).toStdString();
shun_iwasawa cf18d8
    QString shortcut = settings.value(ids.at(i), "").toString();
shun_iwasawa cf18d8
    QAction *action  = getAction(&id[0], false);
shun_iwasawa cf18d8
    if (action) {
shun_iwasawa cf18d8
      QString oldShortcut = action->shortcut().toString();
shun_iwasawa cf18d8
      if (oldShortcut == shortcut) continue;
shun_iwasawa cf18d8
      if (!oldShortcut.isEmpty())
shun_iwasawa cf18d8
        m_shortcutTable.erase(oldShortcut.toStdString());
shun_iwasawa cf18d8
      if (!shortcut.isEmpty()) {
shun_iwasawa cf18d8
        QAction *other = getActionFromShortcut(shortcut.toStdString());
shun_iwasawa cf18d8
        if (other) other->setShortcut(QKeySequence());
shun_iwasawa cf18d8
        m_shortcutTable[shortcut.toStdString()] = getNode(&id[0]);
shun_iwasawa cf18d8
      }
shun_iwasawa cf18d8
      action->setShortcut(QKeySequence(shortcut));
shun_iwasawa cf18d8
    }
shun_iwasawa cf18d8
  }
shun_iwasawa cf18d8
  settings.endGroup();
shun_iwasawa cf18d8
}
shun_iwasawa cf18d8
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString CommandManager::getFullText(CommandId id, bool state)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
  std::map<std::string, node*="">::iterator it;</std::string,>
Toshihiro Shimizu 890ddd
  it = m_idTable.find(id);
Toshihiro Shimizu 890ddd
  if(it == m_idTable.end()) return "";
Toshihiro Shimizu 890ddd
  Node *node = it->second;
Toshihiro Shimizu 890ddd
  QAction *action = it->second->m_qaction;
Toshihiro Shimizu 890ddd
  QString text = action->text();
Toshihiro Shimizu 890ddd
  if(node->m_onText != "" && node->m_offText != "")
Toshihiro Shimizu 890ddd
    text = state ? node->m_onText : node->m_offText;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  QString shortcut = QString::fromStdString(getShortcutFromAction(action));
Toshihiro Shimizu 890ddd
  if(shortcut != "") text += " " + shortcut;
Toshihiro Shimizu 890ddd
  return text;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
MenuItemHandler::MenuItemHandler(const char *cmdId) {
Shinya Kitaoka 120a6e
  CommandManager::instance()->setHandler(
Shinya Kitaoka 120a6e
      cmdId, new CommandHandlerHelper<menuitemhandler>(</menuitemhandler>
Shinya Kitaoka 120a6e
                 this, &MenuItemHandler::execute));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// DVAction
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DVAction::DVAction(const QString &text, QObject *parent)
Shinya Kitaoka 120a6e
    : QAction(text, parent) {
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(triggered()), this, SLOT(onTriggered()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DVAction::DVAction(const QIcon &icon, const QString &text, QObject *parent)
Shinya Kitaoka 120a6e
    : QAction(icon, text, parent) {
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(triggered()), this, SLOT(onTriggered()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DVAction::onTriggered() { CommandManager::instance()->execute(this); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// DVMenuAction
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! It is a menu' with action defined in \b actions.
Shinya Kitaoka 120a6e
                Actions can contain CommandId or simple action name.
Shinya Kitaoka 120a6e
                In first case action triggered is connected with action command
Shinya Kitaoka 120a6e
   execute directly.
Shinya Kitaoka 120a6e
                In second case action triggered is connected with menu action
Shinya Kitaoka 120a6e
   command execute;
Shinya Kitaoka 120a6e
                is helpful to use \b m_triggeredActionIndex to distingue action.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
DVMenuAction::DVMenuAction(const QString &text, QWidget *parent,
Shinya Kitaoka 120a6e
                           QList<qstring> actions)</qstring>
Shinya Kitaoka 120a6e
    : QMenu(text, parent), m_triggeredActionIndex(-1) {
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < actions.size(); i++) addAction(actions.at(i));
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(triggered(QAction *)), this,
Shinya Kitaoka 120a6e
          SLOT(onTriggered(QAction *)));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set a new list of action to menu'.
Shinya Kitaoka 120a6e
                NB. If action list is composed by action menaged and action to
Shinya Kitaoka 120a6e
   create pay
Shinya Kitaoka 120a6e
                attention to inserted order.*/
Shinya Kitaoka 120a6e
void DVMenuAction::setActions(QList<qstring> actions) {</qstring>
Shinya Kitaoka 120a6e
  if (m_triggeredActionIndex != -1)  // Sta facendo l'onTriggered
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  clear();
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < actions.size(); i++) {
Shinya Kitaoka 120a6e
    QString actionId = actions.at(i);
Shinya Kitaoka 120a6e
    // Se l'azione e' definita da un CommandId la aggiungo.
Shinya Kitaoka 120a6e
    QAction *action =
Shinya Kitaoka 120a6e
        CommandManager::instance()->getAction(actionId.toStdString().c_str());
Shinya Kitaoka 120a6e
    if (action) {
Shinya Kitaoka 120a6e
      addAction(action);
Shinya Kitaoka 120a6e
      continue;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    // Altrimenti creo una nuova azione e la aggiungo.
Shinya Kitaoka 120a6e
    action = addAction(actionId);
Shinya Kitaoka 120a6e
    action->setData(QVariant(i));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
QString changeStringNumber(QString str, int index) {
Shinya Kitaoka 120a6e
  QString newStr     = str;
Shinya Kitaoka 120a6e
  int n              = 3;
Shinya Kitaoka 120a6e
  if (index >= 10) n = 4;
Shinya Kitaoka 120a6e
  QString number;
Shinya Kitaoka 120a6e
  newStr.replace(0, n, number.number(index + 1) + QString(". "));
Shinya Kitaoka 120a6e
  return newStr;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DVMenuAction::onTriggered(QAction *action) {
Shinya Kitaoka 120a6e
  QVariant data                              = action->data();
Shinya Kitaoka 120a6e
  if (data.isValid()) m_triggeredActionIndex = data.toInt();
Shinya Kitaoka 120a6e
  CommandManager::instance()->execute(action, menuAction());
Shinya Kitaoka 120a6e
  int oldIndex = m_triggeredActionIndex;
Shinya Kitaoka 120a6e
  if (m_triggeredActionIndex != -1) m_triggeredActionIndex = -1;
Shinya Kitaoka 120a6e
  QString str                                              = data.toString();
Shinya Kitaoka 120a6e
  QAction *tableAction =
Shinya Kitaoka 120a6e
      CommandManager::instance()->getAction(str.toStdString().c_str());
Shinya Kitaoka 120a6e
  if (tableAction || oldIndex == 0) return;
Shinya Kitaoka 120a6e
  QList<qaction *=""> acts = actions();</qaction>
Shinya Kitaoka 120a6e
  removeAction(action);
Shinya Kitaoka 120a6e
  insertAction(acts[0], action);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  acts = actions();
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i <= oldIndex; i++) {
Shinya Kitaoka 120a6e
    QAction *a     = acts.at(i);
Shinya Kitaoka 120a6e
    QString newTxt = changeStringNumber(a->text(), i);
Shinya Kitaoka 120a6e
    a->setText(newTxt);
Shinya Kitaoka 120a6e
    a->setData(QVariant(i));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_triggeredActionIndex = -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------