#include "mathvectoredit.h"

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

#include <QBoxLayout>


MathVectorEdit::MathVectorEdit(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()));
}


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


QVariant MathVectorEdit::value() const {
	return QVariant::fromValue(QAD::MathVector(edit->vector()));
}


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


void MathVectorEdit::setValue(const QVariant & v) {
	edit->setVector(v.value<QAD::MathVector>().v);
}


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


class MathVectorEditFactory: public QVariantEditorFactoryBase {
public:
	MathVectorEditFactory() {}
	virtual QWidget * createEditor() override { return new MathVectorEdit(); }
};


__MathVectorEditRegistrator__::__MathVectorEditRegistrator__() {
	QVariantEditorFactories::registerEditorFactory(qMetaTypeId<QAD::MathVector>(), new MathVectorEditFactory());
}
