Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/studiopaletteviewer.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzQt includes
Toshihiro Shimizu 890ddd
#include "toonzqt/menubarcommand.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/paletteviewer.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/trepetitionguard.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/gutil.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/icongenerator.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/intfield.h"
Toshihiro Shimizu 890ddd
#include "palettesscanpopup.h"
Toshihiro Shimizu 890ddd
#include "palettedata.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzLib includes
Toshihiro Shimizu 890ddd
#include "toonz/studiopalettecmd.h"
Toshihiro Shimizu 890ddd
#include "toonz/tpalettehandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshsimplelevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/sceneproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheethandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevelhandle.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tundo.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "../toonz/menubarcommandids.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qheaderview></qheaderview>
Toshihiro Shimizu 890ddd
#include <qcontextmenuevent></qcontextmenuevent>
Toshihiro Shimizu 890ddd
#include <qmenu></qmenu>
Toshihiro Shimizu 890ddd
#include <qurl></qurl>
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
#include <qvboxlayout></qvboxlayout>
Toshihiro Shimizu 890ddd
#include <qtoolbar></qtoolbar>
Toshihiro Shimizu 890ddd
#include <qinputdialog></qinputdialog>
Toshihiro Shimizu 890ddd
#include <qpushbutton></qpushbutton>
Toshihiro Shimizu 890ddd
#include <qdrag></qdrag>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <time.h></time.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
using namespace PaletteViewerGUI;
Toshihiro Shimizu 890ddd
using namespace DVGui;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return true if path is in folder \b rootPath of \b StudioPalette.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
bool isInStudioPaletteFolder(TFilePath path, TFilePath rootPath) {
Shinya Kitaoka 120a6e
  if (path.getType() != "tpl") return false;
Shinya Kitaoka 120a6e
  StudioPalette *studioPalette = StudioPalette::instance();
Shinya Kitaoka 120a6e
  std::vector<tfilepath> childrenPath;</tfilepath>
Shinya Kitaoka 120a6e
  studioPalette->getChildren(childrenPath, rootPath);
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)childrenPath.size(); i++) {
Shinya Kitaoka 120a6e
    if (path == childrenPath[i])
Shinya Kitaoka 120a6e
      return true;
Shinya Kitaoka 120a6e
    else if (isInStudioPaletteFolder(path, childrenPath[i]))
Shinya Kitaoka 120a6e
      return true;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return true if path is in a \b StudioPalette folder.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
bool isInStudioPalette(TFilePath path) {
Shinya Kitaoka 120a6e
  if (path.getType() != "tpl") return false;
Shinya Kitaoka 120a6e
  StudioPalette *studioPalette = StudioPalette::instance();
Shinya Kitaoka 120a6e
  if (isInStudioPaletteFolder(path, studioPalette->getLevelPalettesRoot()))
Shinya Kitaoka 120a6e
    return true;
Shinya Kitaoka 120a6e
  if (isInStudioPaletteFolder(path, studioPalette->getProjectPalettesRoot()))
Shinya Kitaoka 120a6e
    return true;
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// StudioPaletteTreeViewer
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StudioPaletteTreeViewer::StudioPaletteTreeViewer(
Shinya Kitaoka 120a6e
    QWidget *parent, TPaletteHandle *studioPaletteHandle,
Shinya Kitaoka 120a6e
    TPaletteHandle *levelPaletteHandle, TXsheetHandle *xsheetHandle,
Shinya Kitaoka 120a6e
    TXshLevelHandle *currentLevelHandle)
Shinya Kitaoka 120a6e
    : QTreeWidget(parent)
Shinya Kitaoka 120a6e
    , m_dropItem(0)
Shinya Kitaoka 120a6e
    , m_studioPaletteHandle(studioPaletteHandle)
Shinya Kitaoka 120a6e
    , m_levelPaletteHandle(levelPaletteHandle)
Shinya Kitaoka 120a6e
    , m_currentLevelHandle(currentLevelHandle)
Shinya Kitaoka 120a6e
    , m_xsheetHandle(xsheetHandle)
Shinya Kitaoka 120a6e
    , m_folderIcon(QIcon())
Shinya Kitaoka 120a6e
    , m_levelPaletteIcon(QIcon())
Shinya Kitaoka 120a6e
    , m_studioPaletteIcon(QIcon()) {
Shinya Kitaoka 120a6e
  setIndentation(14);
Shinya Kitaoka 120a6e
  setAlternatingRowColors(true);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  header()->close();
Shinya Kitaoka 120a6e
  setUniformRowHeights(true);
Shinya Kitaoka 120a6e
  setIconSize(QSize(21, 17));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> paletteItems;</qtreewidgetitem>
Shinya Kitaoka 120a6e
shun_iwasawa 3cc41f
  QString open  = QString(":Resources/folder_close.svg");
shun_iwasawa 3cc41f
  QString close = QString(":Resources/folder_open.svg");
Shinya Kitaoka 120a6e
  m_folderIcon.addFile(close, QSize(21, 17), QIcon::Normal, QIcon::On);
Shinya Kitaoka 120a6e
  m_folderIcon.addFile(open, QSize(21, 17), QIcon::Normal, QIcon::Off);
Shinya Kitaoka 120a6e
shun_iwasawa 3cc41f
  QString levelPaletteIcon = QString(":Resources/palette.svg");
Shinya Kitaoka 120a6e
  m_levelPaletteIcon.addPixmap(levelPaletteIcon, QIcon::Normal, QIcon::On);
shun_iwasawa 3cc41f
  QString studioPaletteIcon = QString(":Resources/studiopalette.svg");
Shinya Kitaoka 120a6e
  m_studioPaletteIcon.addPixmap(studioPaletteIcon, QIcon::Normal, QIcon::On);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  StudioPalette *studioPalette = StudioPalette::instance();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TFilePath levelPalettePath = studioPalette->getLevelPalettesRoot();
Shinya Kitaoka 120a6e
  paletteItems.append(createRootItem(levelPalettePath));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TFilePath projectPalettePath = studioPalette->getProjectPalettesRoot();
Shinya Kitaoka 120a6e
  if (TSystem::doesExistFileOrLevel(projectPalettePath))
Shinya Kitaoka 120a6e
    paletteItems.append(createRootItem(projectPalettePath));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  insertTopLevelItems(0, paletteItems);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool ret = connect(this, SIGNAL(itemChanged(QTreeWidgetItem *, int)),
Shinya Kitaoka 120a6e
                     SLOT(onItemChanged(QTreeWidgetItem *, int)));
Shinya Kitaoka 120a6e
  ret = ret && connect(this, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
Shinya Kitaoka 120a6e
                       SLOT(onItemClicked(QTreeWidgetItem *, int)));
Shinya Kitaoka 120a6e
  ret =
Shinya Kitaoka 120a6e
      ret &&
Shinya Kitaoka 120a6e
      connect(this,
Shinya Kitaoka 120a6e
              SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
Shinya Kitaoka 120a6e
              SLOT(onCurrentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
Shinya Kitaoka 120a6e
  ret = ret && connect(this, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
Shinya Kitaoka 120a6e
                       SLOT(onTreeItemExpanded(QTreeWidgetItem *)));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // refresh tree with shortcut key
Shinya Kitaoka 120a6e
  QAction *refreshAct = CommandManager::instance()->getAction(MI_RefreshTree);
Shinya Kitaoka 120a6e
  ret                 = ret && connect(refreshAct, SIGNAL(triggered()), this,
Shinya Kitaoka 120a6e
                       SLOT(onRefreshTreeShortcutTriggered()));
Shinya Kitaoka 120a6e
  addAction(refreshAct);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_palettesScanPopup = new PalettesScanPopup();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  setAcceptDrops(true);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Per la selezione multipla
Shinya Kitaoka 120a6e
  setSelectionMode(QAbstractItemView::ExtendedSelection);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  StudioPalette::instance()->addListener(this);
Shinya Kitaoka 120a6e
  TProjectManager::instance()->addListener(this);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  refresh();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(ret);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StudioPaletteTreeViewer::~StudioPaletteTreeViewer() {
Shinya Kitaoka 120a6e
  StudioPalette::instance()->removeListener(this);
Shinya Kitaoka 120a6e
  TProjectManager::instance()->removeListener(this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::setCurrentLevelHandle(
Shinya Kitaoka 120a6e
    TXshLevelHandle *currentLevelHandle) {
Shinya Kitaoka 120a6e
  m_currentLevelHandle = currentLevelHandle;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::setLevelPaletteHandle(
Shinya Kitaoka 120a6e
    TPaletteHandle *paletteHandle) {
Shinya Kitaoka 120a6e
  m_levelPaletteHandle = paletteHandle;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::setStdPaletteHandle(
Shinya Kitaoka 120a6e
    TPaletteHandle *studioPaletteHandle) {
Shinya Kitaoka 120a6e
  m_studioPaletteHandle = studioPaletteHandle;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QTreeWidgetItem *StudioPaletteTreeViewer::createRootItem(TFilePath path) {
Shinya Kitaoka 120a6e
  QString rootName = QString::fromStdWString(path.getWideName());
Jeremy Bullock 946edb
  if (rootName != "Global Palettes") rootName = "Project Palettes";
Shinya Kitaoka 120a6e
  QTreeWidgetItem *rootItem =
Shinya Kitaoka 120a6e
      new QTreeWidgetItem((QTreeWidget *)0, QStringList(rootName));
Shinya Kitaoka 120a6e
  rootItem->setIcon(0, m_folderIcon);
Shinya Kitaoka 120a6e
  rootItem->setData(1, Qt::UserRole, toQString(path));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  refreshItem(rootItem);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return rootItem;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool StudioPaletteTreeViewer::isRootItem(QTreeWidgetItem *item) {
Shinya Kitaoka 120a6e
  assert(item);
Shinya Kitaoka 120a6e
  TFilePath path = getItemPath(item);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  StudioPalette *studioPalette = StudioPalette::instance();
Shinya Kitaoka 120a6e
  if (path == studioPalette->getLevelPalettesRoot() ||
Shinya Kitaoka 120a6e
      path == studioPalette->getProjectPalettesRoot())
Shinya Kitaoka 120a6e
    return true;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QTreeWidgetItem *StudioPaletteTreeViewer::createItem(const TFilePath path) {
Shinya Kitaoka 120a6e
  StudioPalette *studioPalette = StudioPalette::instance();
Shinya Kitaoka 120a6e
  QString itemName             = toQString(TFilePath(path.getWideName()));
Shinya Kitaoka 120a6e
  QTreeWidgetItem *item =
Shinya Kitaoka 120a6e
      new QTreeWidgetItem((QTreeWidget *)0, QStringList(itemName));
shun-iwasawa ff99e1
  item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |
shun-iwasawa ff99e1
                 Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
Shinya Kitaoka 120a6e
  if (studioPalette->isPalette(path)) {
Shinya Kitaoka 120a6e
    if (studioPalette->hasGlobalName(path))
Shinya Kitaoka 120a6e
      item->setIcon(0, m_studioPaletteIcon);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      item->setIcon(0, m_levelPaletteIcon);
shun-iwasawa ff99e1
    item->setFlags(item->flags() | Qt::ItemNeverHasChildren);
shun-iwasawa ff99e1
  } else if (studioPalette->isFolder(path)) {
Shinya Kitaoka 120a6e
    item->setIcon(0, m_folderIcon);
shun-iwasawa ff99e1
    item->setFlags(item->flags() | Qt::ItemIsDropEnabled);
shun-iwasawa ff99e1
  }
Shinya Kitaoka 120a6e
  item->setData(1, Qt::UserRole, toQString(path));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return item;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath StudioPaletteTreeViewer::getItemPath(QTreeWidgetItem *item) {
Shinya Kitaoka 120a6e
  TFilePath path =
Shinya Kitaoka 120a6e
      (item) ? TFilePath(item->data(1, Qt::UserRole).toString().toStdWString())
Shinya Kitaoka 120a6e
             : TFilePath();
Shinya Kitaoka 120a6e
  return path;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath StudioPaletteTreeViewer::getCurrentFolderPath() {
Shinya Kitaoka 120a6e
  return getItemPath(currentItem());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QTreeWidgetItem *StudioPaletteTreeViewer::getItem(const TFilePath path) {
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> oldItems =</qtreewidgetitem>
Shinya Kitaoka 120a6e
      findItems(QString(""), Qt::MatchContains, 0);
Shinya Kitaoka 120a6e
  if (oldItems.isEmpty()) return 0;
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)oldItems.size(); i++) {
Shinya Kitaoka 120a6e
    TFilePath oldItemPath(
Shinya Kitaoka 120a6e
        oldItems[i]->data(1, Qt::UserRole).toString().toStdWString());
Shinya Kitaoka 120a6e
    if (oldItemPath == path)
Shinya Kitaoka 120a6e
      return oldItems[i];
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      QTreeWidgetItem *item = getFolderItem(oldItems[i], path);
Shinya Kitaoka 120a6e
      if (item) return item;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QTreeWidgetItem *StudioPaletteTreeViewer::getFolderItem(QTreeWidgetItem *parent,
Shinya Kitaoka 120a6e
                                                        const TFilePath path) {
Shinya Kitaoka 120a6e
  int childrenCount = parent->childCount();
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < childrenCount; i++) {
Shinya Kitaoka 120a6e
    QTreeWidgetItem *item = parent->child(i);
Shinya Kitaoka 120a6e
    if (getItemPath(item) == path)
Shinya Kitaoka 120a6e
      return item;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      item = getFolderItem(item, path);
Shinya Kitaoka 120a6e
      if (item) return item;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::resetDropItem() {
Shinya Kitaoka 120a6e
  if (!m_dropItem) return;
Shinya Kitaoka 120a6e
  m_dropItem->setTextColor(0, Qt::black);
Shinya Kitaoka 120a6e
  m_dropItem = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::refresh() {
Shinya Kitaoka 120a6e
  m_openedItems.clear();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  StudioPalette *studioPalette = StudioPalette::instance();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TFilePath levelPalettePath = studioPalette->getLevelPalettesRoot();
Shinya Kitaoka 120a6e
  refreshItem(getItem(levelPalettePath));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TFilePath projectPalettePath = studioPalette->getProjectPalettesRoot();
Shinya Kitaoka 120a6e
  if (!TSystem::doesExistFileOrLevel(projectPalettePath)) return;
Shinya Kitaoka 120a6e
  refreshItem(getItem(projectPalettePath));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // refresh all expanded items
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> items =</qtreewidgetitem>
Shinya Kitaoka 120a6e
      findItems(QString(""), Qt::MatchContains | Qt::MatchRecursive, 0);
Shinya Kitaoka 120a6e
  if (items.isEmpty()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)items.size(); i++)
Shinya Kitaoka 120a6e
    if (items[i]->isExpanded()) refreshItem(items[i]);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
/*!When expand a tree, prepare the child items of it
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::onTreeItemExpanded(QTreeWidgetItem *item) {
Shinya Kitaoka 120a6e
  if (!item) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // if this item was not yet opened, then get the children of this item
Shinya Kitaoka 120a6e
  if (!m_openedItems.contains(item)) refreshItem(item);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // expand the item
Shinya Kitaoka 120a6e
  item->setExpanded(!item->isExpanded());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Refresh tree only when this widget has focus
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::onRefreshTreeShortcutTriggered() {
Shinya Kitaoka 120a6e
  if (hasFocus()) refresh();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Update the content of item
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::refreshItem(QTreeWidgetItem *item) {
shun-iwasawa ff99e1
  struct Locals {
shun-iwasawa ff99e1
    bool isUpper(const TFilePath &fp1, const TFilePath &fp2) {
shun-iwasawa ff99e1
      bool fp1IsFolder = StudioPalette::instance()->isFolder(fp1);
shun-iwasawa ff99e1
      bool fp2IsFolder = StudioPalette::instance()->isFolder(fp2);
shun-iwasawa ff99e1
      if (fp1IsFolder == fp2IsFolder)
shun-iwasawa ff99e1
        return fp1 < fp2;
shun-iwasawa ff99e1
      else
shun-iwasawa ff99e1
        return fp1IsFolder;
shun-iwasawa ff99e1
    }
shun-iwasawa ff99e1
  } locals;
shun-iwasawa ff99e1
Shinya Kitaoka 120a6e
  TFilePath folderPath = getItemPath(item);
Shinya Kitaoka 120a6e
  assert(folderPath != TFilePath());
Shinya Kitaoka 120a6e
  // correct only tpl files and folders
Shinya Kitaoka 120a6e
  std::vector<tfilepath> childrenPath;</tfilepath>
Shinya Kitaoka 120a6e
  StudioPalette::instance()->getChildren(childrenPath, folderPath);
Shinya Kitaoka 120a6e
  int currentChildCount = item->childCount();
Shinya Kitaoka 120a6e
  std::vector<qtreewidgetitem *=""> currentChildren;</qtreewidgetitem>
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < currentChildCount; i++)
Shinya Kitaoka 120a6e
    currentChildren.push_back(item->child(i));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int childrenPathCount = childrenPath.size();
Shinya Kitaoka 120a6e
  int itemIndex         = 0;
Shinya Kitaoka 120a6e
  int pathIndex         = 0;
Shinya Kitaoka 120a6e
  while (itemIndex < currentChildCount || pathIndex < childrenPathCount) {
Shinya Kitaoka 120a6e
    TFilePath path =
Shinya Kitaoka 120a6e
        (pathIndex < childrenPathCount) ? childrenPath[pathIndex] : TFilePath();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    QTreeWidgetItem *currentItem =
Shinya Kitaoka 120a6e
        (itemIndex < currentChildCount) ? currentChildren[itemIndex] : 0;
Shinya Kitaoka 120a6e
    TFilePath currentItemPath = getItemPath(currentItem);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (path == currentItemPath) {
Shinya Kitaoka 120a6e
      itemIndex++;
Shinya Kitaoka 120a6e
      pathIndex++;
shun-iwasawa ff99e1
    } else if ((!path.isEmpty() && locals.isUpper(path, currentItemPath)) ||
Shinya Kitaoka 120a6e
               currentItemPath.isEmpty()) {
Shinya Kitaoka 120a6e
      currentItem = createItem(path);
shun-iwasawa ff99e1
      item->insertChild(pathIndex, currentItem);
Shinya Kitaoka 120a6e
      pathIndex++;
Shinya Kitaoka 120a6e
    } else {
shun-iwasawa ff99e1
      assert(locals.isUpper(currentItemPath, path) || path.isEmpty());
Shinya Kitaoka 120a6e
      assert(currentItem);
Shinya Kitaoka 120a6e
      item->removeChild(currentItem);
Shinya Kitaoka 120a6e
      itemIndex++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_openedItems.insert(item);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::resetProjectPaletteFolder() {
Shinya Kitaoka 120a6e
  int projectPaletteIndex = 1;
Shinya Kitaoka 120a6e
  TFilePath projectPalettePath =
Shinya Kitaoka 120a6e
      StudioPalette::instance()->getProjectPalettesRoot();
Shinya Kitaoka 120a6e
  // Prendo l'item del project palette.
Shinya Kitaoka 120a6e
  QTreeWidgetItem *projectPaletteItem = topLevelItem(projectPaletteIndex);
Shinya Kitaoka 120a6e
  if (projectPaletteItem) {
Shinya Kitaoka 120a6e
    // Se il path dell'item e' uguale a quello del project palette corrente
Shinya Kitaoka 120a6e
    // ritorno.
Shinya Kitaoka 120a6e
    if (getItemPath(projectPaletteItem) == projectPalettePath) return;
Shinya Kitaoka 120a6e
    // Altrimenti lo rimuovo.
Shinya Kitaoka 120a6e
    removeItemWidget(projectPaletteItem, 0);
Shinya Kitaoka 120a6e
    delete projectPaletteItem;
Shinya Kitaoka 120a6e
    // clear the item list in order to search the folder from scratch
Shinya Kitaoka 120a6e
    m_openedItems.clear();
Shinya Kitaoka 120a6e
    // Toonz Palette is not changed, so resurrect the ToonzPaletteRoot
Shinya Kitaoka 120a6e
    m_openedItems.insert(topLevelItem(0));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!TSystem::doesExistFileOrLevel(projectPalettePath)) return;
Shinya Kitaoka 120a6e
  // Creo il nuovo item con il nuovo project folder e lo inserisco nell'albero.
Shinya Kitaoka 120a6e
  // Items in the ProjectPaletteRoot are refreshed here. Stored in openedItems
Shinya Kitaoka 120a6e
  // as well.
Shinya Kitaoka 120a6e
  QTreeWidgetItem *newProjectPaletteItem = createRootItem(projectPalettePath);
Shinya Kitaoka 120a6e
  insertTopLevelItem(projectPaletteIndex, newProjectPaletteItem);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  setCurrentItem(0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::onItemClicked(QTreeWidgetItem *item, int column) {
Shinya Kitaoka 120a6e
  if (currentItem() && m_studioPaletteHandle && m_currentPalette.getPointer()) {
Shinya Kitaoka 120a6e
    // if(m_studioPaletteHandle->getPalette() != m_currentPalette.getPointer())
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      m_studioPaletteHandle->setPalette(m_currentPalette.getPointer());
Shinya Kitaoka 120a6e
      m_studioPaletteHandle->notifyPaletteSwitched();
Shinya Kitaoka 120a6e
      StudioPaletteCmd::updateAllLinkedStyles(m_levelPaletteHandle,
Shinya Kitaoka 120a6e
                                              m_xsheetHandle);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::onItemChanged(QTreeWidgetItem *item, int column) {
Shinya Kitaoka 120a6e
  if (item != currentItem() || isRootItem(item)) return;
Shinya Kitaoka 120a6e
  wstring name      = item->text(column).toStdWString();
Shinya Kitaoka 120a6e
  TFilePath oldPath = getCurrentFolderPath();
Shinya Kitaoka 120a6e
  if (oldPath.isEmpty() || name.empty() || oldPath.getWideName() == name)
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  TFilePath newPath(oldPath.getParentDir() +
Shinya Kitaoka 120a6e
                    TFilePath(name + ::to_wstring(oldPath.getDottedType())));
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    StudioPaletteCmd::movePalette(newPath, oldPath);
Shinya Kitaoka 120a6e
  } catch (TException &e) {
Shinya Kitaoka 120a6e
    error(QString(::to_string(e.getMessage()).c_str()));
Shinya Kitaoka 120a6e
    item->setText(column, QString::fromStdWString(oldPath.getWideName()));
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    error("Can't rename file");
Shinya Kitaoka 120a6e
    item->setText(column, QString::fromStdWString(oldPath.getWideName()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  refreshItem(getItem(oldPath.getParentDir()));
Shinya Kitaoka 120a6e
  setCurrentItem(getItem(newPath));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Called when the current palette is switched
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::onCurrentItemChanged(QTreeWidgetItem *current,
Shinya Kitaoka 120a6e
                                                   QTreeWidgetItem *previous) {
Shinya Kitaoka 120a6e
  TFilePath oldPath = getItemPath(previous);
Shinya Kitaoka 120a6e
  TFilePath newPath = getCurrentFolderPath();
Shinya Kitaoka 120a6e
  if (!m_studioPaletteHandle) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_currentPalette.getPointer() && m_currentPalette->getDirtyFlag()) {
Shinya Kitaoka 120a6e
    TFilePath oldPath = StudioPalette::instance()->getPalettePath(
Shinya Kitaoka 120a6e
        m_currentPalette->getGlobalName());
Shinya Kitaoka 120a6e
    if (oldPath == newPath) return;
Shinya Kitaoka 120a6e
    wstring gname = m_currentPalette->getGlobalName();
Shinya Kitaoka 120a6e
    QString question;
Shinya Kitaoka 120a6e
    question = "The current palette " +
Shinya Kitaoka 120a6e
               QString::fromStdWString(oldPath.getWideString()) +
Shinya Kitaoka 120a6e
               " \nin the studio palette has been modified. Do you want to "
Shinya Kitaoka 120a6e
               "save your changes?";
Shinya Kitaoka 120a6e
    int ret = DVGui::MsgBox(question, QObject::tr("Save"),
Shinya Kitaoka 120a6e
                            QObject::tr("Discard"), QObject::tr("Cancel"), 0);
Shinya Kitaoka 120a6e
    if (ret == 3) {
Shinya Kitaoka 120a6e
      setCurrentItem(getItem(oldPath));
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (ret == 1) {
Shinya Kitaoka 120a6e
      // If the palette is level palette (i.e. NOT stdio palette), just
Shinya Kitaoka 120a6e
      // overwrite it
Shinya Kitaoka 120a6e
      if (gname.empty())
Shinya Kitaoka 120a6e
        StudioPalette::instance()->save(oldPath, m_currentPalette.getPointer());
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        StudioPalette::instance()->setPalette(
Shinya Kitaoka 120a6e
            oldPath, m_currentPalette.getPointer(), false);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    m_currentPalette->setDirtyFlag(false);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // load palette here
Shinya Kitaoka 120a6e
  m_currentPalette = StudioPalette::instance()->getPalette(newPath, false);
Shinya Kitaoka 120a6e
  m_studioPaletteHandle->setPalette(m_currentPalette.getPointer());
Shinya Kitaoka 120a6e
  m_studioPaletteHandle->notifyPaletteSwitched();
shun_iwasawa 640862
  StudioPaletteCmd::updateAllLinkedStyles(m_levelPaletteHandle, m_xsheetHandle);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::addNewPalette() {
Shinya Kitaoka 120a6e
  if (!currentItem()) {
Shinya Kitaoka 120a6e
    error("Error: No folder selected.");
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TFilePath newPath;
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    newPath = StudioPaletteCmd::createPalette(getCurrentFolderPath(), "", 0);
Shinya Kitaoka 120a6e
  } catch (TException &e) {
Shinya Kitaoka 120a6e
    error("Can't create palette: " +
Shinya Kitaoka 120a6e
          QString(::to_string(e.getMessage()).c_str()));
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    error("Can't create palette");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  refreshItem(currentItem());
Shinya Kitaoka 120a6e
  setCurrentItem(getItem(newPath));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::addNewFolder() {
Shinya Kitaoka 120a6e
  if (!currentItem()) {
Shinya Kitaoka 120a6e
    error("Error: No folder selected.");
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TFilePath newPath;
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    newPath = StudioPaletteCmd::addFolder(getCurrentFolderPath());
Shinya Kitaoka 120a6e
  } catch (TException &e) {
Shinya Kitaoka 120a6e
    error("Can't create palette folder: " +
Shinya Kitaoka 120a6e
          QString(::to_string(e.getMessage()).c_str()));
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    error("Can't create palette folder");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  refreshItem(currentItem());
Shinya Kitaoka 120a6e
  setCurrentItem(getItem(newPath));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Shinya Kitaoka 120a6e
/*! Convert level palette to studio palette.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::convertToStudioPalette() {
Shinya Kitaoka 120a6e
  TFilePath path               = getItemPath(currentItem());
Shinya Kitaoka 120a6e
  StudioPalette *studioPalette = StudioPalette::instance();
Shinya Kitaoka 120a6e
  if (studioPalette->isPalette(path)) {
Shinya Kitaoka 120a6e
    TPalette *palette = studioPalette->getPalette(path);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (!palette) {
Shinya Kitaoka 120a6e
      error("Can't touch palette");
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (m_currentPalette->getPaletteName() != palette->getPaletteName()) {
Shinya Kitaoka 120a6e
      error("Can't touch palette");
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    QString question;
Shinya Kitaoka 120a6e
    question = QString::fromStdWString(
Shinya Kitaoka 120a6e
        L"Convert " + path.getWideString() +
Shinya Kitaoka 120a6e
        L" to Studio Palette and Overwrite. \nAre you sure ?");
Shinya Kitaoka 120a6e
    int ret = DVGui::MsgBox(question, QObject::tr("Yes"), QObject::tr("No"));
Shinya Kitaoka 120a6e
    if (ret == 0 || ret == 2) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // apply global name
Shinya Kitaoka 120a6e
    time_t ltime;
Shinya Kitaoka 120a6e
    time(<ime);
Shinya Kitaoka 120a6e
    wstring gname = std::to_wstring(ltime) + L"_" + std::to_wstring(rand());
Shinya Kitaoka 120a6e
    m_currentPalette->setGlobalName(gname);
Shinya Kitaoka 120a6e
    studioPalette->setStylesGlobalNames(m_currentPalette.getPointer());
Shinya Kitaoka 120a6e
    studioPalette->save(path, m_currentPalette.getPointer());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_currentPalette->setDirtyFlag(false);
Shinya Kitaoka 120a6e
    currentItem()->setIcon(0, m_studioPaletteIcon);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    error("Can't find palette");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::deleteItem(QTreeWidgetItem *item) {
Shinya Kitaoka 120a6e
  QTreeWidgetItem *parent = item->parent();
Shinya Kitaoka 120a6e
  if (!parent) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (item->childCount() > 0) {
Shinya Kitaoka 120a6e
    QString question;
Shinya Kitaoka 120a6e
    question = tr("This folder is not empty. Delete anyway?");
Shinya Kitaoka 120a6e
    int ret  = DVGui::MsgBox(question, QObject::tr("Yes"), QObject::tr("No"));
Shinya Kitaoka 120a6e
    if (ret == 0 || ret == 2) return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TFilePath path               = getItemPath(item);
Shinya Kitaoka 120a6e
  StudioPalette *studioPalette = StudioPalette::instance();
Shinya Kitaoka 120a6e
  if (studioPalette->isFolder(path)) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      StudioPaletteCmd::deleteFolder(path);
Shinya Kitaoka 120a6e
    } catch (TException &e) {
Shinya Kitaoka 120a6e
      error("Can't delete folder: " +
Shinya Kitaoka 120a6e
            QString(::to_string(e.getMessage()).c_str()));
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      error("Can't delete folder");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    assert(studioPalette->isPalette(path));
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      StudioPaletteCmd::deletePalette(path);
Shinya Kitaoka 120a6e
    } catch (TException &e) {
Shinya Kitaoka 120a6e
      error("Can't delete palette: " +
Shinya Kitaoka 120a6e
            QString(::to_string(e.getMessage()).c_str()));
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      error("Can't delete palette");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  refreshItem(parent);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::deleteItems() {
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> items = selectedItems();</qtreewidgetitem>
Shinya Kitaoka 120a6e
  int count                      = items.size();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (count == 0) {
Shinya Kitaoka 120a6e
    error("Nothing to delete");
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  TUndoManager::manager()->beginBlock();
Shinya Kitaoka 120a6e
  for (i = 0; i < count; i++) deleteItem(items[i]);
Shinya Kitaoka 120a6e
  TUndoManager::manager()->endBlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::searchForPalette() {
Shinya Kitaoka 120a6e
  m_palettesScanPopup->setCurrentFolder(getCurrentFolderPath());
Shinya Kitaoka 120a6e
  int ret = m_palettesScanPopup->exec();
Shinya Kitaoka 120a6e
  if (ret == QDialog::Accepted) refresh();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class InvalidateIconsUndo final : public TUndo {
Shinya Kitaoka 120a6e
  TPaletteP m_targetPalette, m_oldPalette, m_newPalette;
Shinya Kitaoka 120a6e
  TXshLevelHandle *m_levelHandle;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  InvalidateIconsUndo(TXshLevelHandle *levelHandle)
Shinya Kitaoka 120a6e
      : m_levelHandle(levelHandle) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void undo() const override {
Shinya Kitaoka 120a6e
    TXshLevel *level = m_levelHandle->getLevel();
Shinya Kitaoka 120a6e
    if (level) {
Shinya Kitaoka 120a6e
      std::vector<tframeid> fids;</tframeid>
Shinya Kitaoka 120a6e
      level->getFids(fids);
Shinya Kitaoka 120a6e
      invalidateIcons(level, fids);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 473e70
  void redo() const override { undo(); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  int getSize() const override { return sizeof(*this); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class AdjustPaletteDialog final : public DVGui::Dialog {
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  IntField *m_tolerance;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  int getTolerance() { return m_tolerance->getValue(); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  AdjustPaletteDialog()
Shinya Kitaoka 120a6e
      : Dialog(0, true, true, "Adjust Current Level to This Palette") {
Shinya Kitaoka 120a6e
    setWindowTitle(tr("Adjust Current Level to This Palette"));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    beginVLayout();
Shinya Kitaoka 120a6e
    m_tolerance = new IntField(this);
Shinya Kitaoka 120a6e
    m_tolerance->setRange(0, 255);
Shinya Kitaoka 120a6e
    m_tolerance->setValue(0);
Shinya Kitaoka 120a6e
    addWidget(tr("Tolerance"), m_tolerance);
Shinya Kitaoka 120a6e
    endVLayout();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    QPushButton *okBtn = new QPushButton(tr("Apply"), this);
Shinya Kitaoka 120a6e
    okBtn->setDefault(true);
Shinya Kitaoka 120a6e
    QPushButton *cancelBtn = new QPushButton(tr("Cancel"), this);
Shinya Kitaoka 120a6e
    bool ret = connect(okBtn, SIGNAL(clicked()), this, SLOT(accept()));
Shinya Kitaoka 120a6e
    ret = ret && connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
Shinya Kitaoka 120a6e
    assert(ret);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    addButtonBarWidget(okBtn, cancelBtn);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::loadInCurrentPaletteAndAdaptLevel() {
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> items = selectedItems();</qtreewidgetitem>
Shinya Kitaoka 120a6e
  assert(items.size() == 1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPalette *palette = m_levelPaletteHandle->getPalette();
Shinya Kitaoka 120a6e
  if (!palette) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPalette *newPalette =
Shinya Kitaoka 120a6e
      StudioPalette::instance()->getPalette(getItemPath(items[0]), true);
Shinya Kitaoka 120a6e
  if (!newPalette) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  AdjustPaletteDialog apd;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (apd.exec() != QDialog::Accepted) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /* awful patch: since in StudioPaletteCmd(defined in toonzlib) I cannot use
Shinya Kitaoka 120a6e
the invalidateIcons(defined in toonzqt)
Shinya Kitaoka 120a6e
i do invalidate icons from here using a "fake TUndo", named InvalidateIconsUndo.
Shinya Kitaoka 120a6e
And, since I need to refresh icons at the end of the processing, i have to put
Shinya Kitaoka 120a6e
that fake undo twice, one before and one after.
Shinya Kitaoka 120a6e
this way , when the user do either an undo or a redo operation, I am ensured
Shinya Kitaoka 120a6e
that the last operation is the icon refresh...
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
  TUndoManager::manager()->beginBlock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TUndoManager::manager()->add(new InvalidateIconsUndo(m_currentLevelHandle));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  StudioPaletteCmd::loadIntoCurrentPalette(m_levelPaletteHandle, newPalette,
Shinya Kitaoka 120a6e
                                           m_currentLevelHandle,
Shinya Kitaoka 120a6e
                                           apd.getTolerance());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TUndoManager::manager()->add(new InvalidateIconsUndo(m_currentLevelHandle));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TUndoManager::manager()->endBlock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  InvalidateIconsUndo(m_currentLevelHandle).undo();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::loadInCurrentPalette() {
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> items = selectedItems();</qtreewidgetitem>
Shinya Kitaoka 120a6e
  int count                      = items.size();
Shinya Kitaoka 120a6e
  if (count == 0) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPalette *palette = m_levelPaletteHandle->getPalette();
Shinya Kitaoka 120a6e
  if (!palette) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (palette->isLocked()) {
Shinya Kitaoka 120a6e
    DVGui::warning("Palette is Locked!");
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPalette *newPalette =
Shinya Kitaoka 120a6e
      StudioPalette::instance()->getPalette(getItemPath(items[0]), false);
Shinya Kitaoka 120a6e
  if (!newPalette) return;
Shinya Kitaoka 120a6e
  if (m_xsheetHandle) {
Shinya Kitaoka 120a6e
    int ret = DVGui::eraseStylesInDemand(palette, m_xsheetHandle, newPalette);
Shinya Kitaoka 120a6e
    if (ret == 0) return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  StudioPaletteCmd::loadIntoCurrentPalette(m_levelPaletteHandle, newPalette);
Shinya Kitaoka 120a6e
  m_currentLevelHandle->notifyLevelChange();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TXshLevel *level = m_currentLevelHandle->getLevel();
Shinya Kitaoka 120a6e
  if (level) {
Shinya Kitaoka 120a6e
    std::vector<tframeid> fids;</tframeid>
Shinya Kitaoka 120a6e
    level->getFids(fids);
Shinya Kitaoka 120a6e
    invalidateIcons(level, fids);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 1; i < count; i++) {
Shinya Kitaoka 120a6e
    TFilePath path = getItemPath(items[i]);
Shinya Kitaoka 120a6e
    StudioPaletteCmd::mergeIntoCurrentPalette(m_levelPaletteHandle, path);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // in order to update the title bar of palette viewer
Shinya Kitaoka 120a6e
  m_levelPaletteHandle->getPalette()->setDirtyFlag(true);
Shinya Kitaoka 120a6e
  m_levelPaletteHandle->notifyPaletteChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::replaceCurrentPalette() {
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> items = selectedItems();</qtreewidgetitem>
Shinya Kitaoka 120a6e
  int count                      = items.size();
Shinya Kitaoka 120a6e
  if (count == 0) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // exec confirmation dialog
Shinya Kitaoka 120a6e
  TPalette *current = m_levelPaletteHandle->getPalette();
Shinya Kitaoka 120a6e
  if (!current) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QString label;
Shinya Kitaoka 120a6e
  if (count != 1)  // replacing to multiple palettes
Shinya Kitaoka 120a6e
    label = QString::fromStdWString(
Shinya Kitaoka 120a6e
        L"Replacing all selected palettes with the palette \"" +
Shinya Kitaoka 120a6e
        current->getPaletteName() + L"\". \nAre you sure ?");
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TPalette *dstPalette =
Shinya Kitaoka 120a6e
        StudioPalette::instance()->getPalette(getItemPath(items[0]));
Shinya Kitaoka 120a6e
    if (!dstPalette) return;
Shinya Kitaoka 120a6e
    label = QString::fromStdWString(
Shinya Kitaoka 120a6e
        L"Replacing the palette \"" + dstPalette->getPaletteName() +
Shinya Kitaoka 120a6e
        L"\" with the palette \"" + current->getPaletteName() +
Shinya Kitaoka 120a6e
        L"\". \nAre you sure ?");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int ret =
Shinya Kitaoka 120a6e
      DVGui::MsgBox(label, QObject::tr("Replace"), QObject::tr("Cancel"), 1);
Shinya Kitaoka 120a6e
  if (ret == 0 || ret == 2) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TUndoManager::manager()->beginBlock();
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < count; i++)
Shinya Kitaoka 120a6e
    StudioPaletteCmd::replaceWithCurrentPalette(
Shinya Kitaoka 120a6e
        m_levelPaletteHandle, m_studioPaletteHandle, getItemPath(items[i]));
Shinya Kitaoka 120a6e
  TUndoManager::manager()->endBlock();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_currentPalette) m_currentPalette->setDirtyFlag(false);
Shinya Kitaoka 120a6e
  // in order to update display
Shinya Kitaoka 120a6e
  onCurrentItemChanged(currentItem(), currentItem());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::mergeToCurrentPalette() {
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> items = selectedItems();</qtreewidgetitem>
Shinya Kitaoka 120a6e
  int count                      = items.size();
Shinya Kitaoka 120a6e
  if (count == 0) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TUndoManager::manager()->beginBlock();
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < count; i++)
Shinya Kitaoka 120a6e
    StudioPaletteCmd::mergeIntoCurrentPalette(m_levelPaletteHandle,
Shinya Kitaoka 120a6e
                                              getItemPath(items[i]));
Shinya Kitaoka 120a6e
  TUndoManager::manager()->endBlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::paintEvent(QPaintEvent *event) {
Shinya Kitaoka 120a6e
  QTreeWidget::paintEvent(event);
Shinya Kitaoka 120a6e
  QPainter p(viewport());
Shinya Kitaoka 120a6e
  if (m_dropItem) {
Shinya Kitaoka 120a6e
    p.setPen(QColor(50, 105, 200));
Shinya Kitaoka 120a6e
    p.drawRect(visualItemRect(m_dropItem));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::contextMenuEvent(QContextMenuEvent *event) {
Shinya Kitaoka 120a6e
  TFilePath path = getCurrentFolderPath();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  StudioPalette *studioPalette = StudioPalette::instance();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Menu' per la selezione singola
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> items = selectedItems();</qtreewidgetitem>
Shinya Kitaoka 120a6e
  int count                      = items.size();
Shinya Kitaoka 120a6e
  if (count == 1) {
Shinya Kitaoka 120a6e
    // Verify if click position is in a row containing an item.
Shinya Kitaoka 120a6e
    QRect rect = visualItemRect(currentItem());
Shinya Kitaoka 120a6e
    if (!QRect(0, rect.y(), width(), rect.height()).contains(event->pos()))
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    bool isFolder = (studioPalette->isFolder(path));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    QMenu menu(this);
Shinya Kitaoka 120a6e
    if (isFolder) {
Shinya Kitaoka 120a6e
      createMenuAction(menu, "", tr("New Palette"), "addNewPalette()");
Shinya Kitaoka 120a6e
      createMenuAction(menu, "", tr("New Folder"), "addNewFolder()");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (studioPalette->isFolder(path) &&
Shinya Kitaoka 120a6e
        studioPalette->getLevelPalettesRoot() != path &&
Shinya Kitaoka 120a6e
        studioPalette->getProjectPalettesRoot() != path) {
Shinya Kitaoka 120a6e
      menu.addSeparator();
Shinya Kitaoka 120a6e
      createMenuAction(menu, "", tr("Delete Folder"), "deleteItems()");
Shinya Kitaoka 120a6e
    } else if (studioPalette->isPalette(path)) {
Shinya Kitaoka 120a6e
      if (m_studioPaletteHandle->getPalette()) {
Shinya Kitaoka 120a6e
        createMenuAction(menu, "MI_LoadIntoCurrentPalette",
Shinya Kitaoka 120a6e
                         tr("Load into Current Palette"),
Shinya Kitaoka 120a6e
                         "loadInCurrentPalette()");
Shinya Kitaoka 120a6e
        createMenuAction(menu, "MI_AdjustCurrentLevelToPalette",
Shinya Kitaoka 120a6e
                         tr("Adjust Current Level to This Palette"),
Shinya Kitaoka 120a6e
                         "loadInCurrentPaletteAndAdaptLevel()");
Shinya Kitaoka 120a6e
        createMenuAction(menu, "MI_MergeToCurrentPalette",
Shinya Kitaoka 120a6e
                         tr("Merge to Current Palette"),
Shinya Kitaoka 120a6e
                         "mergeToCurrentPalette()");
Shinya Kitaoka 120a6e
        if (!m_studioPaletteHandle->getPalette()->isLocked()) {
Shinya Kitaoka 120a6e
          createMenuAction(menu, "MI_ReplaceWithCurrentPalette",
Shinya Kitaoka 120a6e
                           tr("Replace with Current Palette"),
Shinya Kitaoka 120a6e
                           "replaceCurrentPalette()");
Shinya Kitaoka 120a6e
          menu.addSeparator();
Shinya Kitaoka 120a6e
          createMenuAction(menu, "MI_DeletePalette", tr("Delete Palette"),
Shinya Kitaoka 120a6e
                           "deleteItems()");
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      if (!studioPalette->hasGlobalName(path)) {
Shinya Kitaoka 120a6e
        menu.addSeparator();
Shinya Kitaoka 120a6e
        createMenuAction(menu, "",
Shinya Kitaoka 120a6e
                         tr("Convert to Studio Palette and Overwrite"),
Shinya Kitaoka 120a6e
                         "convertToStudioPalette()");
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (isFolder) {
Shinya Kitaoka 120a6e
      menu.addSeparator();
Shinya Kitaoka 120a6e
      createMenuAction(menu, "", tr("Search for Palettes"),
Shinya Kitaoka 120a6e
                       "searchForPalette()");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    menu.exec(event->globalPos());
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Menu' per la selezione multipla
Shinya Kitaoka 120a6e
  // Verify if click position is in a row containing an item.
Shinya Kitaoka 120a6e
  bool areAllPalette      = true;
Shinya Kitaoka 120a6e
  bool isClickInSelection = false;
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < count; i++) {
Shinya Kitaoka 120a6e
    QTreeWidgetItem *item = items[i];
Shinya Kitaoka 120a6e
    QRect rect            = visualItemRect(item);
Shinya Kitaoka 120a6e
    if (QRect(0, rect.y(), width(), rect.height()).contains(event->pos()))
Shinya Kitaoka 120a6e
      isClickInSelection                             = true;
Shinya Kitaoka 120a6e
    TFilePath path                                   = getItemPath(item);
Shinya Kitaoka 120a6e
    if (studioPalette->isFolder(path)) areAllPalette = false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!isClickInSelection) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QMenu menu(this);
Shinya Kitaoka 120a6e
  if (areAllPalette) {
Shinya Kitaoka 120a6e
    createMenuAction(menu, "", tr("Load into Current Palette"),
Shinya Kitaoka 120a6e
                     "loadInCurrentPalette()");
Shinya Kitaoka 120a6e
    createMenuAction(menu, "", tr("Merge to Current Palette"),
Shinya Kitaoka 120a6e
                     "mergeToCurrentPalette()");
Shinya Kitaoka 120a6e
    createMenuAction(menu, "", tr("Replace with Current Palette"),
Shinya Kitaoka 120a6e
                     "replaceCurrentPalette()");
Shinya Kitaoka 120a6e
    menu.addSeparator();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  createMenuAction(menu, "", tr("Delete"), "deleteItems()");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  menu.exec(event->globalPos());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::createMenuAction(QMenu &menu, const char *id,
Shinya Kitaoka 120a6e
                                               QString name, const char *slot) {
Shinya Kitaoka 120a6e
  QAction *act = menu.addAction(name);
Shinya Kitaoka 120a6e
  string slotName(slot);
Shinya Kitaoka 120a6e
  slotName = string("1") + slotName;
Shinya Kitaoka 120a6e
  connect(act, SIGNAL(triggered()), slotName.c_str());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::mouseMoveEvent(QMouseEvent *event) {
Shinya Kitaoka 120a6e
  // If left button is not pressed return; is not drag event.
Shinya Kitaoka 120a6e
  if (!(event->buttons() & Qt::LeftButton)) return;
Shinya Kitaoka 120a6e
  startDragDrop();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::startDragDrop() {
Shinya Kitaoka 120a6e
  TRepetitionGuard guard;
Shinya Kitaoka 120a6e
  if (!guard.hasLock()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QDrag *drag         = new QDrag(this);
Shinya Kitaoka 120a6e
  QMimeData *mimeData = new QMimeData;
Shinya Kitaoka 120a6e
  QList<qurl> urls;</qurl>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QList<qtreewidgetitem *=""> items = selectedItems();</qtreewidgetitem>
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < items.size(); i++) {
Shinya Kitaoka 120a6e
    // Sposto solo le palette.
Shinya Kitaoka 120a6e
    TFilePath path = getItemPath(items[i]);
Shinya Kitaoka 120a6e
    if (!path.isEmpty() &&
Shinya Kitaoka 120a6e
        (path.getType() == "tpl" || path.getType() == "pli" ||
Shinya Kitaoka 120a6e
         path.getType() == "tlv" || path.getType() == "tnz"))
Shinya Kitaoka 120a6e
      urls.append(pathToUrl(path));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (urls.isEmpty()) return;
Shinya Kitaoka 120a6e
  mimeData->setUrls(urls);
Shinya Kitaoka 120a6e
  drag->setMimeData(mimeData);
Shinya Kitaoka 120a6e
  Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
Shinya Kitaoka 120a6e
  viewport()->update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::dragEnterEvent(QDragEnterEvent *event) {
Shinya Kitaoka 120a6e
  const QMimeData *mimeData      = event->mimeData();
Shinya Kitaoka 120a6e
  const PaletteData *paletteData = dynamic_cast<const *="" palettedata="">(mimeData);</const>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (acceptResourceDrop(mimeData->urls())) {
Shinya Kitaoka 120a6e
    QList<qurl> urls = mimeData->urls();</qurl>
Shinya Kitaoka 120a6e
    int count        = urls.size();
Shinya Kitaoka 120a6e
    if (count == 0) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Controllo che almeno un url del drag sia una palette da spostare.
Shinya Kitaoka 120a6e
    bool isPalette = false;
Shinya Kitaoka 120a6e
    int i;
Shinya Kitaoka 120a6e
    for (i = 0; i < count; i++) {
Shinya Kitaoka 120a6e
      QUrl url = urls[i];
Shinya Kitaoka 120a6e
      TFilePath path(url.toLocalFile().toStdWString());
Shinya Kitaoka 120a6e
      if (!path.isEmpty() &&
Shinya Kitaoka 120a6e
          (path.getType() == "tpl" || path.getType() == "pli" ||
Shinya Kitaoka 120a6e
           path.getType() == "tlv" || path.getType() == "tnz")) {
Shinya Kitaoka 120a6e
        isPalette = true;
Shinya Kitaoka 120a6e
        break;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (!isPalette) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    event->acceptProposedAction();
Shinya Kitaoka 120a6e
  } else if (paletteData && paletteData->hasOnlyPalette())
Shinya Kitaoka 120a6e
    event->acceptProposedAction();
Shinya Kitaoka 120a6e
  viewport()->update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::dragMoveEvent(QDragMoveEvent *event) {
Shinya Kitaoka 120a6e
  QTreeWidgetItem *item = itemAt(event->pos());
Shinya Kitaoka 120a6e
  TFilePath newPath     = getItemPath(item);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_dropItem) m_dropItem->setTextColor(0, Qt::black);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (item) {
shun-iwasawa ff99e1
    // drop will not be executed on the same item
shun-iwasawa ff99e1
    const QMimeData *mimeData = event->mimeData();
shun-iwasawa ff99e1
    if (mimeData->hasUrls() && mimeData->urls().size() == 1) {
shun-iwasawa ff99e1
      TFilePath path =
shun-iwasawa ff99e1
          TFilePath(mimeData->urls()[0].toLocalFile().toStdWString());
shun-iwasawa ff99e1
      if (path == getItemPath(item)) {
shun-iwasawa ff99e1
        m_dropItem = 0;
shun-iwasawa ff99e1
        event->ignore();
shun-iwasawa ff99e1
        viewport()->update();
shun-iwasawa ff99e1
        return;
shun-iwasawa ff99e1
      }
shun-iwasawa ff99e1
    }
shun-iwasawa ff99e1
    // when dragging over other items, drop destination will be the parent
shun-iwasawa ff99e1
    // folder of it
shun-iwasawa ff99e1
    if (item->flags() & Qt::ItemNeverHasChildren) {
shun-iwasawa ff99e1
      item = item->parent();
shun-iwasawa ff99e1
    }
Shinya Kitaoka 120a6e
    m_dropItem = item;
Shinya Kitaoka 120a6e
    event->acceptProposedAction();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    m_dropItem = 0;
Shinya Kitaoka 120a6e
    event->ignore();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  viewport()->update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::dropEvent(QDropEvent *event) {
Shinya Kitaoka 120a6e
  TFilePath newPath = getItemPath(m_dropItem);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  resetDropItem();
Shinya Kitaoka 120a6e
shun_iwasawa 014838
  if (newPath.isEmpty()) return;
shun_iwasawa 014838
Shinya Kitaoka 120a6e
  const QMimeData *mimeData      = event->mimeData();
Shinya Kitaoka 120a6e
  const PaletteData *paletteData = dynamic_cast<const *="" palettedata="">(mimeData);</const>
Shinya Kitaoka 120a6e
  if (paletteData) {
Shinya Kitaoka 120a6e
    if (paletteData->hasOnlyPalette()) {
Shinya Kitaoka 120a6e
      TPalette *palette = paletteData->getPalette();
Shinya Kitaoka 120a6e
      if (!palette) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      try {
Shinya Kitaoka 120a6e
        StudioPaletteCmd::createPalette(
Shinya Kitaoka 120a6e
            newPath, ::to_string(palette->getPaletteName()), palette);
Shinya Kitaoka 120a6e
      } catch (TException &e) {
Shinya Kitaoka 120a6e
        error("Can't create palette: " +
Shinya Kitaoka 120a6e
              QString(::to_string(e.getMessage()).c_str()));
Shinya Kitaoka 120a6e
      } catch (...) {
Shinya Kitaoka 120a6e
        error("Can't create palette");
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!mimeData->hasUrls() || mimeData->urls().size() == 0) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QList<qurl> urls = mimeData->urls();</qurl>
Shinya Kitaoka 120a6e
shun-iwasawa ff99e1
  // make the list of palette paths which will be actually moved
shun-iwasawa ff99e1
  QList<tfilepath> palettePaths;</tfilepath>
shun-iwasawa ff99e1
  for (int i = 0; i < urls.size(); i++) {
shun-iwasawa ff99e1
    TFilePath path = TFilePath(urls[i].toLocalFile().toStdWString());
shun-iwasawa ff99e1
    if (path != newPath && path.getParentDir() != newPath)
shun-iwasawa ff99e1
      palettePaths.append(path);
shun-iwasawa ff99e1
  }
shun-iwasawa ff99e1
  if (palettePaths.isEmpty()) return;
shun-iwasawa ff99e1
shun-iwasawa ff99e1
  // open the confirmation dialog in order to prevent unintended move
shun-iwasawa ff99e1
  QString pltName;
shun-iwasawa ff99e1
  if (palettePaths.size() == 1)
shun-iwasawa ff99e1
    pltName = tr("the palette \"%1\"")
shun-iwasawa ff99e1
                  .arg(QString::fromStdWString(palettePaths[0].getWideName()));
shun-iwasawa ff99e1
  else
shun-iwasawa ff99e1
    pltName       = tr("the selected palettes");
shun-iwasawa ff99e1
  QString dstName = QString::fromStdWString(newPath.getWideName());
shun-iwasawa ff99e1
shun-iwasawa ff99e1
  QString question =
shun-iwasawa ff99e1
      tr("Move %1 to \"%2\". Are you sure ?").arg(pltName).arg(dstName);
shun-iwasawa ff99e1
  int ret = DVGui::MsgBox(question, tr("Move"), tr("Cancel"));
shun-iwasawa ff99e1
  if (ret == 0 || ret == 2) return;
Shinya Kitaoka 120a6e
shun-iwasawa ff99e1
  TUndoManager::manager()->beginBlock();
shun-iwasawa ff99e1
  for (int i = 0; i < palettePaths.size(); i++) {
shun-iwasawa ff99e1
    TFilePath path = palettePaths[i];
Shinya Kitaoka 120a6e
    if (isInStudioPalette(path)) {
Shinya Kitaoka 120a6e
      TFilePath newPalettePath =
Shinya Kitaoka 120a6e
          newPath +
Shinya Kitaoka 120a6e
          TFilePath(path.getWideName() + ::to_wstring(path.getDottedType()));
Shinya Kitaoka 120a6e
      try {
Shinya Kitaoka 120a6e
        StudioPaletteCmd::movePalette(newPalettePath, path);
Shinya Kitaoka 120a6e
      } catch (TException &e) {
Shinya Kitaoka 120a6e
        error("Can't rename palette: " +
Shinya Kitaoka 120a6e
              QString(::to_string(e.getMessage()).c_str()));
Shinya Kitaoka 120a6e
      } catch (...) {
Shinya Kitaoka 120a6e
        error("Can't rename palette");
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TUndoManager::manager()->endBlock();
shun-iwasawa ff99e1
  event->setDropAction(Qt::MoveAction);
Shinya Kitaoka 120a6e
  event->accept();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void StudioPaletteTreeViewer::dragLeaveEvent(QDragLeaveEvent *event) {
Shinya Kitaoka 120a6e
  resetDropItem();
Shinya Kitaoka 120a6e
  update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// StudioPaletteViewer
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
StudioPaletteViewer::StudioPaletteViewer(QWidget *parent,
Shinya Kitaoka 120a6e
                                         TPaletteHandle *studioPaletteHandle,
Shinya Kitaoka 120a6e
                                         TPaletteHandle *levelPaletteHandle,
Shinya Kitaoka 120a6e
                                         TFrameHandle *frameHandle,
Shinya Kitaoka 120a6e
                                         TXsheetHandle *xsheetHandle,
Shinya Kitaoka 120a6e
                                         TXshLevelHandle *currentLevelHandle)
Shinya Kitaoka 120a6e
    : QSplitter(parent) {
Shinya Kitaoka 120a6e
  // style sheet
Shinya Kitaoka 120a6e
  setObjectName("StudioPaletteViewer");
Shinya Kitaoka 120a6e
  setFrameStyle(QFrame::StyledPanel);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  setAcceptDrops(true);
Shinya Kitaoka 120a6e
  setOrientation(Qt::Vertical);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // First Splitter Widget
Shinya Kitaoka 120a6e
  QWidget *treeWidget      = new QWidget(this);
Shinya Kitaoka 120a6e
  QVBoxLayout *treeVLayout = new QVBoxLayout(treeWidget);
Shinya Kitaoka 120a6e
  treeVLayout->setMargin(0);
Shinya Kitaoka 120a6e
  treeVLayout->setSpacing(0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_studioPaletteTreeViewer = new StudioPaletteTreeViewer(
Shinya Kitaoka 120a6e
      treeWidget, studioPaletteHandle, levelPaletteHandle, xsheetHandle,
Shinya Kitaoka 120a6e
      currentLevelHandle);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  treeVLayout->addWidget(m_studioPaletteTreeViewer);
Shinya Kitaoka 120a6e
  treeWidget->setLayout(treeVLayout);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Second Splitter Widget
shun-iwasawa 784640
  m_studioPaletteViewer =
Shinya Kitaoka 120a6e
      new PaletteViewer(this, PaletteViewerGUI::STUDIO_PALETTE);
shun-iwasawa 784640
  m_studioPaletteViewer->setObjectName("PaletteViewerInStudioPalette");
shun-iwasawa 784640
  m_studioPaletteViewer->setXsheetHandle(xsheetHandle);
shun-iwasawa 784640
  m_studioPaletteViewer->setPaletteHandle(studioPaletteHandle);
shun-iwasawa 784640
  m_studioPaletteViewer->setFrameHandle(frameHandle);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  addWidget(treeWidget);
shun-iwasawa 784640
  addWidget(m_studioPaletteViewer);
Shinya Kitaoka 120a6e
shun-iwasawa 784640
  setFocusProxy(m_studioPaletteViewer);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StudioPaletteViewer::~StudioPaletteViewer() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! In order to save current palette from the tool button in the PageViewer.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
TFilePath StudioPaletteViewer::getCurrentItemPath() {
Shinya Kitaoka 120a6e
  return m_studioPaletteTreeViewer->getCurrentItemPath();
Toshihiro Shimizu 890ddd
}
shun-iwasawa 784640
shun-iwasawa 784640
//-----------------------------------------------------------------------------
shun-iwasawa 784640
shun-iwasawa 784640
int StudioPaletteViewer::getViewMode() const {
shun-iwasawa 784640
  return m_studioPaletteViewer->getViewMode();
shun-iwasawa 784640
}
shun-iwasawa 784640
shun-iwasawa 784640
//-----------------------------------------------------------------------------
shun-iwasawa 784640
shun-iwasawa 784640
void StudioPaletteViewer::setViewMode(int mode) {
shun-iwasawa 784640
  m_studioPaletteViewer->setViewMode(
shun-iwasawa 784640
      (PaletteViewerGUI::PageViewer::ViewMode)mode);
shun-iwasawa 784640
}