Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "palettesscanpopup.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/gutil.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qpushbutton></qpushbutton>
Toshihiro Shimizu 890ddd
#include <qlabel></qlabel>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
/*! \class PalettesScanPopup
Shinya Kitaoka 120a6e
                \brief The PalettesScanPopup class provides a modal dialog to
Shinya Kitaoka 120a6e
   import
Shinya Kitaoka 120a6e
                                         palette in StudioPalette folder.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                Inherits \b Dialog.
Shinya Kitaoka 120a6e
                This object is composed of three sub-object: a \b FileField to
Shinya Kitaoka 120a6e
   choose a
Shinya Kitaoka 120a6e
                folder, a label \b QLabel, generally empty, to show palette
Shinya Kitaoka 120a6e
   folder during
Shinya Kitaoka 120a6e
                them import, and a button bar to start import or cancel command.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                The class provides a method, \b setCurrentFolder(TFilePath
Shinya Kitaoka 120a6e
   path), to set
Shinya Kitaoka 120a6e
                folder wherein import palettes.
Shinya Kitaoka 120a6e
                Protected function allows to manage palette import.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
PalettesScanPopup::PalettesScanPopup()
Shinya Kitaoka 120a6e
    : Dialog(0, true, true, "PalettesScan")
Shinya Kitaoka 120a6e
    , m_folderPath(TFilePath())
Shinya Kitaoka 120a6e
    , m_timerId(0) {
Shinya Kitaoka 120a6e
  setWindowTitle(tr("Search for Palettes"));
Shinya Kitaoka 120a6e
  setFixedWidth(250);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_field = new DVGui::FileField();
Shinya Kitaoka 120a6e
  addWidget(m_field);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_label = new QLabel();
Shinya Kitaoka 120a6e
  m_label->setFixedWidth(200);
Shinya Kitaoka 120a6e
  addWidget(m_label);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QPushButton *okBtn = new QPushButton(tr("Ok"), this);
Shinya Kitaoka 120a6e
  okBtn->setDefault(true);
Shinya Kitaoka 120a6e
  QPushButton *cancelBtn = new QPushButton(tr("Cancel"), this);
Shinya Kitaoka 120a6e
  connect(okBtn, SIGNAL(clicked()), this, SLOT(onOkBtnClicked()));
Shinya Kitaoka 120a6e
  connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
Shinya Kitaoka 120a6e
  addButtonBarWidget(okBtn, cancelBtn);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set current folder path to \b path.
shun-iwasawa 7f18c9
 */
Shinya Kitaoka 120a6e
void PalettesScanPopup::setCurrentFolder(TFilePath path) {
Shinya Kitaoka 120a6e
  m_folderPath = path;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Return current folder path.
shun-iwasawa 7f18c9
 */
Shinya Kitaoka 120a6e
TFilePath PalettesScanPopup::getCurrentFolder() { return m_folderPath; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Start palette import. Add \b FileField path to directories stack and start
Shinya Kitaoka 120a6e
                a timer.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void PalettesScanPopup::onOkBtnClicked() {
Shinya Kitaoka 120a6e
  m_timerId = startTimer(3);
Shinya Kitaoka 120a6e
  TFilePathSet fps;
Shinya Kitaoka 120a6e
  std::wstring s = m_field->getPath().toStdWString();
Shinya Kitaoka 120a6e
  int i = 0, len = s.length();
Shinya Kitaoka 120a6e
  while (i < len) {
Shinya Kitaoka 120a6e
    while (i < len && (s[i] == ' ' || s[i] == '\t')) i++;
Shinya Kitaoka 120a6e
    if (i >= len) break;
Shinya Kitaoka 120a6e
    int j = i;
Shinya Kitaoka 120a6e
    while (j < len && s[j] != ',') j++;
Shinya Kitaoka 120a6e
    int k = j;
Shinya Kitaoka 120a6e
    while (k > i && (s[k - 1] == ' ' || s[k - 1] == '\t')) k--;
Shinya Kitaoka 120a6e
    std::wstring token = s.substr(i, k - i);
Shinya Kitaoka 120a6e
    fps.push_back(TFilePath(token));
Shinya Kitaoka 120a6e
    i = j + 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  push(fps);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Set label text to path \b fp.
shun-iwasawa 7f18c9
 */
Shinya Kitaoka 120a6e
void PalettesScanPopup::setLabel(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  QString elideStr =
Shinya Kitaoka 120a6e
      elideText(toQString(fp), m_label->font(), m_label->width());
Shinya Kitaoka 120a6e
  m_label->setText(elideStr);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Proceed in palette import recalling \b step(); if step() return false
Shinya Kitaoka 120a6e
                stop timer, clear directories stack, and close popup sending
Shinya Kitaoka 120a6e
   accept event.
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void PalettesScanPopup::timerEvent(QTimerEvent *event) {
Shinya Kitaoka 120a6e
  bool ret = step();
Shinya Kitaoka 120a6e
  if (!ret) {
Shinya Kitaoka 120a6e
    killTimer(m_timerId);
Shinya Kitaoka 120a6e
    clearStack();
Shinya Kitaoka 120a6e
    QDialog::accept();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Push TFilePath \b fp to the top of directories stack. Set label text to fp.
shun-iwasawa 7f18c9
 */
Shinya Kitaoka 120a6e
void PalettesScanPopup::push(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  setLabel(fp);
Shinya Kitaoka 120a6e
  Directory *dir = new Directory();
Shinya Kitaoka 120a6e
  m_stack.push_back(dir);
Shinya Kitaoka 120a6e
  dir->m_path  = fp;
Shinya Kitaoka 120a6e
  dir->m_files = TSystem::readDirectory(fp);
Shinya Kitaoka 120a6e
  dir->m_it    = dir->m_files.begin();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Push TFilePathSet \b fs to the top of directories stack. Set label text to
Shinya Kitaoka 120a6e
                "<files>".</files>
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void PalettesScanPopup::push(const TFilePathSet &fs) {
Shinya Kitaoka 120a6e
  m_label->setText(tr("<files>"));</files>
Shinya Kitaoka 120a6e
  Directory *dir = new Directory();
Shinya Kitaoka 120a6e
  m_stack.push_back(dir);
Shinya Kitaoka 120a6e
  dir->m_path  = TFilePath();
Shinya Kitaoka 120a6e
  dir->m_files = fs;
Shinya Kitaoka 120a6e
  dir->m_it    = dir->m_files.begin();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Removes the top item from the stack. If stack is empty return immediately.
shun-iwasawa 7f18c9
 */
Shinya Kitaoka 120a6e
void PalettesScanPopup::pop() {
Shinya Kitaoka 120a6e
  if (m_stack.empty()) return;
Shinya Kitaoka 120a6e
  Directory *dir = m_stack.back();
Shinya Kitaoka 120a6e
  delete dir;
Shinya Kitaoka 120a6e
  m_stack.pop_back();
Shinya Kitaoka 120a6e
  if (!m_stack.empty()) {
Shinya Kitaoka 120a6e
    dir = m_stack.back();
Shinya Kitaoka 120a6e
    setLabel(dir->m_path);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    m_label->setText(tr(""));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
luz paz 6454c4
/*! Return true if directories stack is not empty; otherwise return false.
Toshihiro Shimizu 890ddd
\n	If stack is not empty check current directory:
Toshihiro Shimizu 890ddd
\li	if all files are checked recall \b pop();
Toshihiro Shimizu 890ddd
\li	if its path is a directory recall \b push(const TFilePath &fp);
Shinya Kitaoka 120a6e
\li	if its path is a file check its extension and recall \b onPlt(const
Shinya Kitaoka 120a6e
TFilePath &fp).
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
bool PalettesScanPopup::step() {
Shinya Kitaoka 120a6e
  if (m_stack.empty()) return false;
Shinya Kitaoka 120a6e
  Directory *dir = m_stack.back();
Shinya Kitaoka 120a6e
  if (dir->m_it == dir->m_files.end())
Shinya Kitaoka 120a6e
    pop();
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TFilePath fp = *(dir->m_it)++;
Shinya Kitaoka 120a6e
    if (TFileStatus(fp).isDirectory())
Shinya Kitaoka 120a6e
      push(fp);
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      setLabel(fp);
Shinya Kitaoka 120a6e
      std::string ext = fp.getType();
Shinya Kitaoka 120a6e
      if (ext == "plt" || ext == "tpl" || ext == "pli") onPlt(fp);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Resets the content of the directories stack and set label text empty.
shun-iwasawa 7f18c9
 */
Shinya Kitaoka 120a6e
void PalettesScanPopup::clearStack() {
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)m_stack.size(); i++) delete m_stack[i];
Shinya Kitaoka 120a6e
  m_stack.clear();
Shinya Kitaoka 120a6e
  m_label->setText(tr(""));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Import palette, defined in path \b fp, in current \b StudioPalette folder.
shun-iwasawa 7f18c9
 */
Shinya Kitaoka 120a6e
void PalettesScanPopup::onPlt(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  TFilePath root(m_field->getPath().toStdString());
Shinya Kitaoka 120a6e
  assert(root.isAncestorOf(fp));
Shinya Kitaoka 120a6e
  TFilePath q = fp.getParentDir() - root;
shun-iwasawa 7f18c9
  try {
shun-iwasawa 7f18c9
    StudioPalette::instance()->importPalette(m_folderPath + q, fp);
shun-iwasawa 7f18c9
  } catch (TSystemException &se) {
shun-iwasawa 7f18c9
    DVGui::warning(QString::fromStdWString(se.getMessage()));
shun-iwasawa 7f18c9
  } catch (...) {
shun-iwasawa 7f18c9
    DVGui::warning(QString::fromStdWString(fp.getWideString() + L"\n") +
shun-iwasawa 7f18c9
                   tr("Failed to import palette."));
shun-iwasawa 7f18c9
  }
Toshihiro Shimizu 890ddd
}