Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/validatedchoicedialog.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qlabel></qlabel>
Toshihiro Shimizu 890ddd
#include <qpushbutton></qpushbutton>
Toshihiro Shimizu 890ddd
#include <qbuttongroup></qbuttongroup>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
//    ValidatedChoiceDialog  implementation
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
DVGui::ValidatedChoiceDialog::ValidatedChoiceDialog(QWidget *parent,
Shinya Kitaoka 120a6e
                                                    Options opts)
Shinya Kitaoka 120a6e
    : Dialog(parent, true, false) {
Shinya Kitaoka 120a6e
  setModal(true);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_buttonGroup = new QButtonGroup(this);
Shinya Kitaoka 120a6e
  m_buttonGroup->setExclusive(true);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool ret = true;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QPushButton *okBtn = new QPushButton(QString(tr("Apply")), this);
Shinya Kitaoka 120a6e
  ret                = ret && connect(okBtn, SIGNAL(clicked()), SLOT(accept()));
Shinya Kitaoka 120a6e
  addButtonBarWidget(okBtn);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QPushButton *okToAllBtn = new QPushButton(QString(tr("Apply to All")), this);
Shinya Kitaoka 120a6e
  ret = ret && connect(okToAllBtn, SIGNAL(clicked()), SLOT(onApplyToAll()));
Shinya Kitaoka 120a6e
  addButtonBarWidget(okToAllBtn);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QPushButton *cancelBtn = new QPushButton(QString(tr("Cancel")), this);
Shinya Kitaoka 120a6e
  ret = ret && connect(cancelBtn, SIGNAL(clicked()), SLOT(reject()));
Shinya Kitaoka 120a6e
  addButtonBarWidget(cancelBtn);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(ret);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  reset();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Start layout
Shinya Kitaoka 120a6e
  beginVLayout();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_label = new QLabel(this);
Shinya Kitaoka 120a6e
  addWidget(m_label);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // enbVLayout() must be invoked by derived classes
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DVGui::ValidatedChoiceDialog::reset() {
Shinya Kitaoka 120a6e
  m_appliedToAllRes = NO_REQUIRED_RESOLUTION;
Shinya Kitaoka 120a6e
  m_appliedToAll    = false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString validate(void *obj);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int DVGui::ValidatedChoiceDialog::execute(void *obj) {
Shinya Kitaoka 120a6e
  struct Resol {
Shinya Kitaoka 120a6e
    int m_res;
Shinya Kitaoka 120a6e
    bool m_all;
Shinya Kitaoka 120a6e
    Resol(int res, bool all) : m_res(res), m_all(all) {}
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Resol curResolution(NO_REQUIRED_RESOLUTION, false),
Shinya Kitaoka 120a6e
      newResolution(m_appliedToAll ? m_appliedToAllRes : NO_REQUIRED_RESOLUTION,
Shinya Kitaoka 120a6e
                    m_appliedToAll);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool initialize = true;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Loop until a resolution gets accepted
Shinya Kitaoka 120a6e
  do {
Shinya Kitaoka 120a6e
    QString err =
Shinya Kitaoka 120a6e
        acceptResolution(obj, curResolution.m_res, curResolution.m_all);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (err.isEmpty()) break;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (newResolution.m_res == NO_REQUIRED_RESOLUTION) {
Shinya Kitaoka 120a6e
      // No new resolution selected - prompt for user interaction
Shinya Kitaoka 120a6e
      m_label->setText(err);
Shinya Kitaoka 120a6e
      m_applyToAll = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if (initialize) initializeUserInteraction(obj), initialize = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if (exec() == QDialog::Rejected) {
Shinya Kitaoka 120a6e
        curResolution.m_res = CANCEL;
Shinya Kitaoka 120a6e
        break;
Shinya Kitaoka 120a6e
      }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      newResolution = Resol(m_buttonGroup->checkedId(), m_applyToAll);
Shinya Kitaoka 120a6e
      assert(newResolution.m_res >= DEFAULT_RESOLUTIONS_COUNT);
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Substitute resolution and retry
Shinya Kitaoka 120a6e
    curResolution = newResolution,
Shinya Kitaoka 120a6e
    newResolution = Resol(NO_REQUIRED_RESOLUTION, false);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  } while (true);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return curResolution.m_res;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DVGui::ValidatedChoiceDialog::onApplyToAll() {
Shinya Kitaoka 120a6e
  m_appliedToAllRes = m_buttonGroup->checkedId();
Shinya Kitaoka 120a6e
  m_applyToAll = m_appliedToAll = true;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(m_appliedToAllRes >= DEFAULT_RESOLUTIONS_COUNT);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  accept();
Toshihiro Shimizu 890ddd
}