constraintwidget.cc 4.27 KB
Newer Older
1 2 3 4 5
//
// Copyright (c) CNRS
// Authors: Heidy Dallard
//

6 7 8 9 10
#include "hppwidgetsplugin/constraintwidget.hh"

#include <QMessageBox>
#include <QDockWidget>

11
#include <omniORB4/CORBA.h>
12
#include "gepetto/gui/mainwindow.hh"
13

14
#include "hppwidgetsplugin/hppwidgetsplugin.hh"
15
#include "hppwidgetsplugin/transformconstraintwidget.hh"
16
#include "hppwidgetsplugin/numericalconstraintpicker.hh"
17 18
#include "hppwidgetsplugin/ui_constraintwidget.h"

19 20
using gepetto::gui::MainWindow;

21 22 23 24 25 26
namespace hpp {
  namespace gui {
    ConstraintWidget::ConstraintWidget(HppWidgetsPlugin* plugin, QWidget *parent) :
        QWidget(parent),
        ui(new Ui::ConstraintWidget)
    {
27
        haveWidget = false;
28 29 30
        ui->setupUi(this);
        plugin_ = plugin;

31
        connect(ui->createButton, SIGNAL(clicked()), SLOT(createConstraint()));
32 33 34
        connect(ui->resetButton, SIGNAL(clicked()), SLOT(reset()));
        connect(ui->confirmButton, SIGNAL(clicked()), SLOT(confirmNumerical()));
        connect(ui->applyButton, SIGNAL(clicked()), SLOT(applyConstraints()));
35
        connect(ui->constraintTypeSelect, SIGNAL(currentIndexChanged(int)), SLOT(typeChanged(int)));
36 37
	lastInsert_ = 0;
	ui->constraintNameEdit->setText("constraint_0");
38 39
        MainWindow* main = MainWindow::instance();
	connect(main, SIGNAL(refresh()), SLOT(refresh()));
40
	main->registerSlot("addConstraint", this);
41 42 43 44 45 46 47 48 49 50
    }

    ConstraintWidget::~ConstraintWidget()
    {
      funcs_.clear();
      delete ui;
    }

    void ConstraintWidget::addConstraint(IConstraint *constraint)
    {
51
      funcs_.push_back(constraint);
52
      ui->constraintTypeSelect->addItem(constraint->getName());
53 54
      connect(constraint, SIGNAL(constraintCreated(QString)), SLOT(onConstraintCreated(QString)));
      connect(constraint, SIGNAL(finished()), SLOT(onFinished()));
55 56 57 58 59 60 61 62 63 64 65 66 67
    }

    void ConstraintWidget::reset()
    {
      plugin_->client()->problem()->resetConstraints();
    }

    void ConstraintWidget::applyConstraints()
    {
      hpp::floatSeq_var config = plugin_->client()->robot()->getCurrentConfig();
      hpp::floatSeq_var newConfig = NULL;
      double residual;

68
      if (plugin_->client()->problem()->applyConstraints(config.in(), newConfig.out(), residual) == false) {
69
        gepetto::gui::MainWindow::instance()->logError("Could not apply constraints to current configuration");
70 71
      }
      else {
72
        plugin_->client()->robot()->setCurrentConfig(newConfig.in());
73
        gepetto::gui::MainWindow::instance()->requestApplyCurrentConfiguration();
74 75 76 77 78
      }
    }

    void ConstraintWidget::confirmNumerical()
    {
79
      NumericalConstraintPicker* ncp = new NumericalConstraintPicker(plugin_);
80 81

      ncp->show();
82 83 84 85
    }

    void ConstraintWidget::reload()
    {
86 87 88 89 90 91
      for (std::vector<IConstraint *>::iterator it = funcs_.begin(); it != funcs_.end(); ++it) {
        try {
          (*it)->reload();
        }
        catch (hpp::Error& e) {
          gepetto::gui::MainWindow::instance ()->logError(QString(e.msg));
92 93 94 95
        }
      }
    }

96 97 98 99 100 101 102 103 104
    void ConstraintWidget::refresh()
    {
      ui->nameList->clear();
      hpp::Names_t_var nc = plugin_->client()->problem()->getAvailable("NumericalConstraint");
      for (unsigned i = 0; i < nc->length(); i++) {
	ui->nameList->addItem(nc[i].in());
      }
    }

105 106 107 108
    void ConstraintWidget::createConstraint()
    {
      QString name = ui->constraintNameEdit->text();

109
      if (name == "") {
110
        QMessageBox::information(this, "hpp-gui", "You have to give a name and select at least one joint");
111 112
        return ;
      }
Joseph Mirabel's avatar
Joseph Mirabel committed
113
      if (ui->constraintTypeSelect->currentIndex() < int(funcs_.size())) {
114
        (*(this->funcs_[ui->constraintTypeSelect->currentIndex()]))(name);
115 116 117
      }
    }

118
    void ConstraintWidget::onConstraintCreated(QString name)
119 120
    {
      ui->nameList->addItem(name);
121 122 123 124
    }

    void ConstraintWidget::onFinished()
    {
125
      lastInsert_++;
hdallard's avatar
hdallard committed
126
      ui->constraintNameEdit->setText("constraint_" + QString::number(lastInsert_));
127
    }
128 129 130 131 132 133

    void ConstraintWidget::typeChanged(int index)
    {
      QBoxLayout* layoutVar = dynamic_cast<QBoxLayout *>(layout());


Joseph Mirabel's avatar
Joseph Mirabel committed
134
      if (index < int(funcs_.size())) {
135 136 137 138 139 140 141 142 143 144
        if (haveWidget) {
          layout()->takeAt(2)->widget()->hide();
        }
        QWidget* toAdd = funcs_[index]->getWidget();

        toAdd->show();
        layoutVar->insertWidget(2, toAdd);
        haveWidget = true;
      }
    }
145 146
  }
}