Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tools/toolhandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage2.h"
Toshihiro Shimizu 890ddd
#include "tools/tool.h"
Toshihiro Shimizu 890ddd
#include "tools/toolcommandids.h"
Toshihiro Shimizu 890ddd
#include "timage.h"
Toshihiro Shimizu 890ddd
//#include "tapp.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/menubarcommand.h"
Toshihiro Shimizu 890ddd
#include <qaction></qaction>
Toshihiro Shimizu 890ddd
#include <qmap></qmap>
Toshihiro Shimizu 890ddd
#include <qdebug></qdebug>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// ToolHandle
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToolHandle::ToolHandle()
Shinya Kitaoka 120a6e
    : m_tool(0)
Shinya Kitaoka 120a6e
    , m_toolName("")
Shinya Kitaoka 120a6e
    , m_toolTargetType(TTool::NoTarget)
Shinya Kitaoka 120a6e
    , m_storedToolName("")
Shinya Kitaoka 120a6e
    , m_toolIsBusy(false) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ToolHandle::~ToolHandle() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TTool *ToolHandle::getTool() const { return m_tool; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToolHandle::setTool(QString name) {
Shinya Kitaoka 120a6e
  m_oldToolName = m_toolName = name;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TTool *tool = TTool::getTool(m_toolName.toStdString(),
Shinya Kitaoka 120a6e
                               (TTool::ToolTargetType)m_toolTargetType);
Shinya Kitaoka 120a6e
  if (tool == m_tool) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (m_tool) m_tool->onDeactivate();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Camera test uses the automaticly activated CameraTestTool
Shinya Kitaoka 120a6e
  if (name != "T_CameraTest" && CameraTestCheck::instance()->isEnabled())
Shinya Kitaoka 120a6e
    CameraTestCheck::instance()->setIsEnabled(false);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_tool = tool;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (name != T_Hand && CleanupPreviewCheck::instance()->isEnabled()) {
Shinya Kitaoka 120a6e
    // When using a tool, you have to exit from cleanup preview mode
Shinya Kitaoka 120a6e
    QAction *act = CommandManager::instance()->getAction("MI_CleanupPreview");
Shinya Kitaoka 120a6e
    if (act) CommandManager::instance()->execute(act);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (m_tool)  // Should always enter
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    m_tool->onActivate();
Shinya Kitaoka 120a6e
    emit toolSwitched();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToolHandle::storeTool() {
Shinya Kitaoka 120a6e
  m_storedToolName = m_toolName;
Shinya Kitaoka 120a6e
  m_storedToolTime.start();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToolHandle::restoreTool() {
Shinya Kitaoka 120a6e
  // qDebug() << m_storedToolTime.elapsed();
Shinya Kitaoka 120a6e
  if (m_storedToolName != m_toolName && m_storedToolName != "" &&
Shinya Kitaoka 120a6e
      m_storedToolTime.elapsed() > 500) {
Shinya Kitaoka 120a6e
    setTool(m_storedToolName);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToolHandle::setPseudoTool(QString name) {
Shinya Kitaoka 120a6e
  QString oldToolName = m_oldToolName;
Shinya Kitaoka 120a6e
  setTool(name);
Shinya Kitaoka 120a6e
  m_oldToolName = oldToolName;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToolHandle::unsetPseudoTool() {
Shinya Kitaoka 120a6e
  if (m_toolName != m_oldToolName) setTool(m_oldToolName);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToolHandle::setToolBusy(bool value) {
Shinya Kitaoka 120a6e
  m_toolIsBusy = value;
Shinya Kitaoka 120a6e
  if (!m_toolIsBusy) emit toolEditingFinished();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QIcon currentIcon;
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static QIcon getCurrentIcon() { return currentIcon; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void ToolHandle::changeTool(QAction* action)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToolHandle::onImageChanged(TImage::Type imageType) {
Shinya Kitaoka 120a6e
  TTool::ToolTargetType targetType = TTool::NoTarget;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  switch (imageType) {
Shinya Kitaoka 120a6e
  case TImage::RASTER:
Shinya Kitaoka 120a6e
    targetType = TTool::RasterImage;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case TImage::TOONZ_RASTER:
Shinya Kitaoka 120a6e
    targetType = TTool::ToonzImage;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case TImage::VECTOR:
Shinya Kitaoka 120a6e
  default:
Shinya Kitaoka 120a6e
    targetType = TTool::VectorImage;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  case TImage::MESH:
Shinya Kitaoka 120a6e
    targetType = TTool::MeshImage;
Shinya Kitaoka 120a6e
    break;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (targetType != m_toolTargetType) {
Shinya Kitaoka 120a6e
    m_toolTargetType = targetType;
Shinya Kitaoka 120a6e
    setTool(m_toolName);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_tool) {
Shinya Kitaoka 120a6e
    m_tool->updateMatrix();
Shinya Kitaoka 120a6e
    m_tool->onImageChanged();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToolHandle::updateMatrix() {
Shinya Kitaoka 120a6e
  if (m_tool) m_tool->updateMatrix();
Toshihiro Shimizu 890ddd
}