Commit c7707b1a by Alisa Jung

consistency between selection in scene graph and automatically selecting…

consistency between selection in scene graph and automatically selecting primitives after adding/deleting
parent 1ad003b8
......@@ -36,6 +36,10 @@ void Controller::initViewWidgets(MyGLWidget *allViews[4], QWidget *two, QWidget
viewLayout = layout;
}
void Controller::initTreeView(QTreeView *treeView){
this->treeView = treeView;
}
void Controller::showSingleView(){
viewLayout = new QGridLayout();
viewLayout->removeWidget(quadViewWidget);
......@@ -228,22 +232,28 @@ QQuaternion Controller::computeRotation(QPoint newPosition){
void Controller::addSphere(){
selectTransform(scene->addSphere(tesselation));
treeView->setCurrentIndex(scene->getLastAddedIndex());
}
void Controller::addBox(){
selectTransform(scene->addBox(tesselation));
treeView->setCurrentIndex(scene->getLastAddedIndex());
}
void Controller::addCylinder(){
selectTransform(scene->addCylinder(tesselation));
treeView->setCurrentIndex(scene->getLastAddedIndex());
}
void Controller::addCone(){
selectTransform(scene->addCone(tesselation));
treeView->setCurrentIndex(scene->getLastAddedIndex());
}
void Controller::addTorus(){
selectTransform(scene->addTorus(tesselation));
treeView->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
treeView->setCurrentIndex(scene->getLastAddedIndex());
}
void Controller::deletePrimitive(){
......@@ -254,8 +264,15 @@ void Controller::deletePrimitive(){
void Controller::selectTransform(RigidBodyTransformation *rbt){
currentTransform = rbt;
if (currentTransform){
statusBar->showMessage(currentTransform->getChildPrimitive()->getName());
if (currentTransform->getChildrenCount()>1){
QString n = "Group selected: ";
n.append(currentTransform->getName());
statusBar->showMessage(n);
}else{
statusBar->showMessage(currentTransform->getChild(0)->getName());
}
}
else {
statusBar->showMessage("None selected");
......@@ -272,3 +289,14 @@ void Controller::itemSelected(QModelIndex q){
selectTransform(static_cast<RigidBodyTransformation*>(t));
qDebug() << "selected name " << t->data(0);
}
void Controller::itemDoubleClicked(QModelIndex q){
qDebug() << "item Double clicked. Row: " << q.row() << ", column: " << q.column();
selectedItem = scene->getItem(q);
}
void Controller::changeItemName(QString q){
//TODO this is not working.
qDebug() << "Name entered: " << q;
selectedItem->setName(q);
}
......@@ -18,6 +18,7 @@
#include <rigidbodytransformation.h>
#include <QStatusBar>
#include <QModelIndex>
#include <QTreeView>
#include <math.h>
......@@ -34,6 +35,7 @@ public:
void initActions(QAction* cam, QAction* manipulate);
void initViewWidgets(MyGLWidget* views[4], QWidget* doubleViews, QWidget* quadViews, QGridLayout* layout);
void initTreeView(QTreeView* treeView);
//processes input and might call follow up functions in widget
void processMousePressEvent(QMouseEvent* event, MyGLWidget* widget);
......@@ -47,7 +49,6 @@ public:
SceneGraph* getSceneGraph();
RigidBodyTransformation* currentTransform;
private:
QAction *modeCameraAction;
QAction *modeManipulateAction;
......@@ -74,10 +75,16 @@ private:
int tesselation;
RigidBodyTransformation* currentTransform;
TreeItem* selectedItem;//might equal currentTransform. Or not.
SceneGraph* scene;
void selectTransform(RigidBodyTransformation* rbt);
QTreeView* treeView;
public slots:
void switchToModeCamera();
void switchToModeManipulate();
......@@ -97,6 +104,8 @@ public slots:
void deletePrimitive();
void itemSelected(QModelIndex q);
void itemDoubleClicked(QModelIndex q);
void changeItemName(QString q);
};
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.5.1, 2015-11-22T14:19:27. -->
<!-- Written by QtCreator 3.5.1, 2015-11-23T01:20:22. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......
......@@ -256,8 +256,12 @@ MainWindow::MainWindow(QWidget *parent)
connect(treeView,SIGNAL(clicked(QModelIndex)),controller, SLOT(itemSelected(QModelIndex)));
connect(treeView,SIGNAL(doubleClicked(QModelIndex)),controller,SLOT(itemDoubleClicked(QModelIndex)));
connect(treeView, SIGNAL(objectNameChanged(QString)),controller,SLOT(changeItemName(QString)));
}
controller->initTreeView(treeView);
}
......
......@@ -170,7 +170,9 @@ void MyGLWidget::paintGL(){
glMultMatrixf(m.data());
//draw primitive
Primitive* p = (*i)->getChildPrimitive();
for (int j = 0; j < (*i)->getChildrenCount(); j++){
qDebug() << "Draw child " << j <<" for " << (*i)->getName();
Primitive* p = (*i)->getChildAsPrimitive(j);
int tesselation = p->getTesselation();
Primitive::Type type = p->getType();
if (type == Primitive::Type::SPHERE){
......@@ -186,6 +188,7 @@ void MyGLWidget::paintGL(){
}else {
qDebug() << "Weird Type." << type;
}
}
//reset transformation for next primitive
glPopMatrix();
......
......@@ -92,20 +92,46 @@ TreeItem* MyItemModel::getItem(const QModelIndex& index) const{
}
void MyItemModel::addNewTransformToRoot(TreeItem *rbt, TreeItem* prim){
beginInsertRows(QModelIndex(), 0, 0);
QModelIndex MyItemModel::addNewTransformToRoot(TreeItem *rbt, TreeItem* prim){
int ind = root->getChildrenCount();
beginInsertRows(QModelIndex(), ind, ind);
qDebug() << "Add child " << rbt->data(0);
root->addChild(rbt);
root->addChild(rbt, ind);
endInsertRows();
QModelIndex i = index(0,0,QModelIndex());
QModelIndex i = index(ind,0,QModelIndex());
beginInsertRows(i,0,0);
qDebug() << "Add primitive " << prim->data(0) << " at " << i;
rbt->addChild(prim);
rbt->addChild(prim,0);
endInsertRows();
return i;
// return true;
}
QModelIndex MyItemModel::getIndex(TreeItem* item){
if (item == root){
return QModelIndex();
}
else{
for (int i = 0; i < root->getChildrenCount(); i++){
TreeItem* c = root->getChild(i);
QModelIndex p = index(i,0,QModelIndex());
if (c == item){
return p;
}else{//Hartgecoded auf tiefe 2.
for (int j = 0; j < c->getChildrenCount(); j++){
TreeItem* d = c->getChild(j);
if (d == item){
return index(j,0,p);
}
}
}
}
}
qDebug() << "Could not find " << item->getName();
return QModelIndex();
}
void MyItemModel::removeTransformFromRoot(TreeItem *rbt){
int row = -1;
for (int i = 0; i < root->getChildren().length(); i++){
......
......@@ -22,7 +22,8 @@ public:
QModelIndex index(int row, int column, const QModelIndex &parent) const Q_DECL_OVERRIDE;
QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
void addNewTransformToRoot(TreeItem* rbt, TreeItem* prim);
QModelIndex addNewTransformToRoot(TreeItem* rbt, TreeItem* prim);
QModelIndex getIndex(TreeItem* item);
void removeTransformFromRoot(TreeItem* rbt);
TreeItem* getItem(const QModelIndex &index) const;
......
......@@ -10,10 +10,6 @@ Primitive::~Primitive(){
}
QString Primitive::getName(){
return name;
}
Primitive::Primitive(QString name, int ID, int tesselation, Type t){
this->name = name;
this->ID = ID;
......
......@@ -19,8 +19,6 @@ public:
int getTesselation();
Primitive::Type getType();
QString getName();
QVariant data(int column) override;
protected:
......
......@@ -43,10 +43,10 @@ void RigidBodyTransformation::setRotation(QQuaternion newRotation){
rotation = newRotation;
}
Primitive* RigidBodyTransformation::getChildPrimitive(){
if (children.length() > 0){
return static_cast<Primitive*>(children[0]);//TODO aufpassen falls höhere tiefe
Primitive* RigidBodyTransformation::getChildAsPrimitive(int row){
if (row < children.length()){
return static_cast<Primitive*>(children[row]);//TODO aufpassen falls höhere tiefe
} else{
qDebug("WAAAH - RBT has no chilren.");
qDebug() << "WAAAH - RBT has not enough chilren for row " << row;
}
}
......@@ -18,12 +18,11 @@ public:
const QQuaternion getRotation();
const QVector3D getTranslation();
Primitive* getChildPrimitive();
void drawChild();
QVariant data(int column) override;
Primitive* getChildAsPrimitive(int row);
private :
//translation
QVector3D translation;
......
......@@ -33,7 +33,6 @@ RigidBodyTransformation* SceneGraph::addSphere(int tesselation){
int t = tesselation;
if (t < 3) t = 3;
Primitive* p = new Primitive(nextSphereName, idCounter, t, Primitive::Type::SPHERE);
idCounter++;
sphereIndex++;
qDebug() << "New Sphere added in scenegraph." << nextSphereName;
......@@ -46,7 +45,6 @@ RigidBodyTransformation* SceneGraph::addBox(int tesselation){
nextBoxName = "Box ";
nextBoxName.append(QString::number(boxIndex));
Primitive* p = new Primitive(nextBoxName, idCounter, tesselation, Primitive::Type::BOX);
idCounter++;
boxIndex++;
qDebug("New box added in scenegraph");
......@@ -60,7 +58,6 @@ RigidBodyTransformation* SceneGraph::addCylinder(int tesselation){
int t = tesselation;
if (t < 3) t = 3;
Primitive* p = new Primitive(nextCylName, idCounter, t, Primitive::Type::CYLINDER);
idCounter++;
cylIndex++;
qDebug("added Cylinder to scenegraph");
return addPrimitive(p);
......@@ -73,7 +70,6 @@ RigidBodyTransformation* SceneGraph::addCone(int tesselation){
int t = tesselation;
if (t < 3) t = 3;
Primitive* p = new Primitive(nextConeName, idCounter, t, Primitive::Type::CONE);
idCounter++;
coneIndex++;
return addPrimitive(p);
}
......@@ -85,15 +81,18 @@ RigidBodyTransformation* SceneGraph::addTorus(int tesselation){
int t = tesselation;
if (t < 3) t = 3;
Primitive* p = new Primitive(nextTorusName, idCounter, t, Primitive::Type::TORUS);
idCounter++;
torusIndex++;
return addPrimitive(p);
}
RigidBodyTransformation* SceneGraph::addPrimitive(Primitive* p){
RigidBodyTransformation* r = new RigidBodyTransformation();
QString n = "RBT Group ";
n.append(QString::number(idCounter));
idCounter++;
r->setName(n);
nodes.append(r);
model->addNewTransformToRoot(r,p);
lastAddedIndex = model->addNewTransformToRoot(r,p);
qDebug() << "added primitive to root. childlength: " << r->getChildren().length();
// model->insertRows(1,1,QModelIndex());
notifyViews();
......@@ -114,6 +113,15 @@ RigidBodyTransformation* SceneGraph::deleteNode(RigidBodyTransformation *r){
else return 0;
}
QModelIndex SceneGraph::getIndex(TreeItem *item){
return model->getIndex(item);
}
QModelIndex SceneGraph::getLastAddedIndex(){
qDebug() << "last index " << lastAddedIndex;
return lastAddedIndex;
}
//transform something
void SceneGraph::addRotation(RigidBodyTransformation *r, QQuaternion diff){
r->addRotation(diff);
......
......@@ -35,6 +35,8 @@ public:
MyItemModel* getModel();
TreeItem* getItem(QModelIndex q);
QModelIndex getIndex(TreeItem* item);
QModelIndex getLastAddedIndex();//Index of last added tree item
//Hallo. Todos:
//Log selected name in status bar
......@@ -64,6 +66,8 @@ private:
MyItemModel* model;
QModelIndex lastAddedIndex;
};
......
......@@ -14,6 +14,15 @@ TreeItem::~TreeItem(){
}
}
void TreeItem::setName(QString newName){
name = newName;
qDebug() << "Name set to " << name;
}
QString TreeItem::getName(){
return name;
}
void TreeItem::setParent(TreeItem *parent){
this->parent = parent;
}
......@@ -26,9 +35,13 @@ int TreeItem::getChildrenCount(){
return children.length();
}
//adds to child list AND sets itself as parent.
void TreeItem::addChild(TreeItem *item){
//adds to child list before [index] AND sets itself as parent.
void TreeItem::addChild(TreeItem *item, int index){
if (index >= children.length()){
children.append(item);
}else{
children.insert(index,item);
}
item->setParent(this);
}
......
......@@ -18,10 +18,13 @@ public:
virtual QVariant data(int column); //returns QVariant for name string
void addChild(TreeItem* item);
void addChild(TreeItem* item, int row);
bool removeChild(TreeItem* item);
void setParent(TreeItem* parent);
void setName(QString newName);
QString getName();
int childNumber();
protected:
TreeItem* parent;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment