Files
qad/qglengine/widgets/primitiveeditor.cpp

163 lines
4.5 KiB
C++

#include "primitiveeditor.h"
#include "ui_primitiveeditor.h"
#include <QMetaEnum>
#include "glprimitives.h"
#include "glmesh.h"
PrimitiveEditor::PrimitiveEditor(QWidget *parent) : QWidget(parent), ui(new Ui::PrimitiveEditor) {
view = 0;
ui->setupUi(this);
editors[Plane] << ui->widgetWidth;
editors[Plane] << ui->widgetLength;
editors[Cube] << ui->widgetWidth;
editors[Cube] << ui->widgetLength;
editors[Cube] << ui->widgetHeight;
editors[Ellipsoid] << ui->widgetWidth;
editors[Ellipsoid] << ui->widgetLength;
editors[Ellipsoid] << ui->widgetHeight;
editors[Ellipsoid] << ui->widgetSegments;
editors[Ellipsoid] << ui->widgetSegments2;
editors[Disc] << ui->widgetWidth;
editors[Disc] << ui->widgetLength;
editors[Disc] << ui->widgetSegments;
editors[Disc] << ui->widgetAngle;
editors[Cone] << ui->widgetWidth;
editors[Cone] << ui->widgetLength;
editors[Cone] << ui->widgetHeight;
editors[Cone] << ui->widgetSegments;
editors[Cylinder] << ui->widgetWidth;
editors[Cylinder] << ui->widgetLength;
editors[Cylinder] << ui->widgetHeight;
editors[Cylinder] << ui->widgetSegments;
editors[Torus] << ui->widgetRadius1;
editors[Torus] << ui->widgetRadius2;
editors[Torus] << ui->widgetSegments;
editors[Torus] << ui->widgetSegments2;
editors[Torus] << ui->widgetAngle;
QSet<QWidget *> all;
QMetaEnum me = metaObject()->enumerator(0);
for (int i=0; i<me.keyCount(); ++i) {
ui->comboPrimitives->addItem(me.key(i));
all.unite(editors[(PrimitiveType)me.value(i)].toSet());
}
all_editors = all.toList();
foreach (QWidget * w, all_editors) {
if (w->layout()) w->layout()->setContentsMargins(0, layout()->spacing(), 0, 0);
}
ui->comboPrimitives->setCurrentIndex(Plane);
}
PrimitiveEditor::~PrimitiveEditor() {
delete ui;
}
void PrimitiveEditor::assignQGLView(QGLView * v) {
view = v;
connect(view, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
selectionChanged();
}
Mesh * PrimitiveEditor::createMesh() {
Mesh * m = 0;
PrimitiveType pt = (PrimitiveType)ui->comboPrimitives->currentIndex();
switch (pt) {
case Plane:
m = Primitive::plane(ui->spinWidth->value(),
ui->spinLength->value());
break;
case Cube:
m = Primitive::cube(ui->spinWidth->value(),
ui->spinLength->value(),
ui->spinHeight->value());
break;
case Ellipsoid:
m = Primitive::ellipsoid(ui->spinSegments->value(),
ui->spinSegments2->value(),
ui->spinWidth->value(),
ui->spinLength->value(),
ui->spinHeight->value());
break;
case Disc:
m = Primitive::disc(ui->spinSegments->value(),
ui->spinWidth->value(),
ui->spinLength->value(),
true,
ui->spinAngle->value());
break;
case Cone:
m = Primitive::cone(ui->spinSegments->value(),
ui->spinWidth->value(),
ui->spinLength->value(),
ui->spinHeight->value());
break;
case Cylinder:
m = Primitive::cylinder(ui->spinSegments->value(),
ui->spinWidth->value(),
ui->spinLength->value(),
ui->spinHeight->value());
break;
case Torus:
m = Primitive::torus(ui->spinSegments->value(),
ui->spinSegments2->value(),
ui->spinRadius->value(),
ui->spinRadius2->value(),
ui->spinAngle->value());
break;
default: return 0;
}
if (ui->flipNormals->isChecked())
m->flipNormals();
return m;
}
void PrimitiveEditor::showEditors() {
foreach (QWidget * w, all_editors) w->hide();
PrimitiveType pt = (PrimitiveType)ui->comboPrimitives->currentIndex();
QList<QWidget *> wds = editors[pt];
foreach (QWidget * w, wds) w->show();
}
void PrimitiveEditor::selectionChanged() {
ObjectBase * so = view->selectedObject();
bool has_1 = so;
ui->buttonReplace->setEnabled(has_1);
}
void PrimitiveEditor::replaceMesh() {
if (!view) return;
if (!ui->buttonReplace->isEnabled() || !ui->buttonReplace->isChecked()) return;
ObjectBase * so = view->selectedObject();
if (!so) return;
Mesh * m = createMesh();
if (!m) return;
so->setMesh(m);
delete m;
}
void PrimitiveEditor::on_buttonAdd_clicked() {
if (!view) return;
Mesh * m = createMesh();
if (!m) return;
ObjectBase * o = new ObjectBase(m);
o->setColor(ui->colorButton->color());
o->setName(ui->comboPrimitives->currentText());
view->scene()->addObject(o);
view->scene()->selectObject(o);
delete m;
}
void PrimitiveEditor::on_comboPrimitives_currentIndexChanged(int index) {
showEditors();
}