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