#include "mathmatrixedit.h"

#include "matrixedit.h"
#include "qad_types.h"
#include "qvariantedit_custom.h"

#include <QBoxLayout>


MathMatrixEdit::MathMatrixEdit(QWidget * parent): QWidget(parent) {
	edit = new MatrixEdit();
	setLayout(new QBoxLayout(QBoxLayout::LeftToRight));
	layout()->setContentsMargins(0, 0, 0, 0);
	layout()->addWidget(edit);
	connect(edit, SIGNAL(changed()), this, SIGNAL(valueChanged()));
}


MathMatrixEdit::~MathMatrixEdit() {
	delete edit;
}


QVariant MathMatrixEdit::value() const {
	return QVariant::fromValue(QAD::MathMatrix(edit->matrix()));
}


bool MathMatrixEdit::isReadOnly() const {
	return edit->isReadOnly();
}


void MathMatrixEdit::setValue(const QVariant & v) {
	edit->setMatrix(v.value<QAD::MathMatrix>().m);
}


void MathMatrixEdit::setReadOnly(bool yes) {
	edit->setReadOnly(yes);
}


class MathMatrixEditFactory: public QVariantEditorFactoryBase {
public:
	MathMatrixEditFactory() {}
	QWidget * createEditor() override { return new MathMatrixEdit(); }
};


__MathMatrixEditRegistrator__::__MathMatrixEditRegistrator__() {
	QVariantEditorFactories::registerEditorFactory(qMetaTypeId<QAD::MathMatrix>(), new MathMatrixEditFactory());
	//__QADTypesRegistrator__::instance()->toString_funcs.insert(qMetaTypeId<QAD::IODevice>(), &QAD_IODevice_toString);
}
