GCC Code Coverage Report


Directory: plugins/
File: plugins/hppwidgetsplugin/constraintwidget.cc
Date: 2024-12-13 15:51:58
Exec Total Coverage
Lines: 0 68 0.0%
Branches: 0 120 0.0%

Line Branch Exec Source
1 //
2 // Copyright (c) CNRS
3 // Authors: Heidy Dallard
4 //
5
6 #include "hppwidgetsplugin/constraintwidget.hh"
7
8 #include <omniORB4/CORBA.h>
9
10 #include <QDockWidget>
11 #include <QMessageBox>
12
13 #include "gepetto/gui/mainwindow.hh"
14 #include "hppwidgetsplugin/hppwidgetsplugin.hh"
15 #include "hppwidgetsplugin/numericalconstraintpicker.hh"
16 #include "hppwidgetsplugin/transformconstraintwidget.hh"
17 #include "hppwidgetsplugin/ui_constraintwidget.h"
18
19 using gepetto::gui::MainWindow;
20
21 namespace hpp {
22 namespace gui {
23 ConstraintWidget::ConstraintWidget(HppWidgetsPlugin* plugin, QWidget* parent)
24 : QWidget(parent), ui(new Ui::ConstraintWidget) {
25 haveWidget = false;
26 ui->setupUi(this);
27 plugin_ = plugin;
28
29 connect(ui->createButton, SIGNAL(clicked()), SLOT(createConstraint()));
30 connect(ui->resetButton, SIGNAL(clicked()), SLOT(reset()));
31 connect(ui->confirmButton, SIGNAL(clicked()), SLOT(confirmNumerical()));
32 connect(ui->applyButton, SIGNAL(clicked()), SLOT(applyConstraints()));
33 connect(ui->constraintTypeSelect, SIGNAL(currentIndexChanged(int)),
34 SLOT(typeChanged(int)));
35 lastInsert_ = 0;
36 ui->constraintNameEdit->setText("constraint_0");
37 MainWindow* main = MainWindow::instance();
38 connect(main, SIGNAL(refresh()), SLOT(refresh()));
39 main->registerSlot("addConstraint", this);
40 }
41
42 ConstraintWidget::~ConstraintWidget() {
43 funcs_.clear();
44 delete ui;
45 }
46
47 void ConstraintWidget::addConstraint(IConstraint* constraint) {
48 funcs_.push_back(constraint);
49 ui->constraintTypeSelect->addItem(constraint->getName());
50 connect(constraint, SIGNAL(constraintCreated(QString)),
51 SLOT(onConstraintCreated(QString)));
52 connect(constraint, SIGNAL(finished()), SLOT(onFinished()));
53 }
54
55 void ConstraintWidget::reset() {
56 plugin_->client()->problem()->resetConstraints();
57 }
58
59 void ConstraintWidget::applyConstraints() {
60 hpp::floatSeq_var config = plugin_->client()->robot()->getCurrentConfig();
61 hpp::floatSeq_var newConfig = NULL;
62 double residual;
63
64 if (plugin_->client()->problem()->applyConstraints(
65 config.in(), newConfig.out(), residual) == false) {
66 gepetto::gui::MainWindow::instance()->logError(
67 "Could not apply constraints to current configuration");
68 } else {
69 plugin_->client()->robot()->setCurrentConfig(newConfig.in());
70 gepetto::gui::MainWindow::instance()->requestApplyCurrentConfiguration();
71 }
72 }
73
74 void ConstraintWidget::confirmNumerical() {
75 NumericalConstraintPicker* ncp = new NumericalConstraintPicker(plugin_);
76
77 ncp->show();
78 }
79
80 void ConstraintWidget::reload() {
81 for (std::vector<IConstraint*>::iterator it = funcs_.begin();
82 it != funcs_.end(); ++it) {
83 try {
84 (*it)->reload();
85 } catch (hpp::Error& e) {
86 gepetto::gui::MainWindow::instance()->logError(QString(e.msg));
87 }
88 }
89 }
90
91 void ConstraintWidget::refresh() {
92 ui->nameList->clear();
93 hpp::Names_t_var nc =
94 plugin_->client()->problem()->getAvailable("NumericalConstraint");
95 for (unsigned i = 0; i < nc->length(); i++) {
96 ui->nameList->addItem(nc[i].in());
97 }
98 }
99
100 void ConstraintWidget::createConstraint() {
101 QString name = ui->constraintNameEdit->text();
102
103 if (name == "") {
104 QMessageBox::information(
105 this, "hpp-gui",
106 "You have to give a name and select at least one joint");
107 return;
108 }
109 if (ui->constraintTypeSelect->currentIndex() < int(funcs_.size())) {
110 (*(this->funcs_[ui->constraintTypeSelect->currentIndex()]))(name);
111 }
112 }
113
114 void ConstraintWidget::onConstraintCreated(QString name) {
115 ui->nameList->addItem(name);
116 }
117
118 void ConstraintWidget::onFinished() {
119 lastInsert_++;
120 ui->constraintNameEdit->setText("constraint_" + QString::number(lastInsert_));
121 }
122
123 void ConstraintWidget::typeChanged(int index) {
124 QBoxLayout* layoutVar = dynamic_cast<QBoxLayout*>(layout());
125
126 if (index < int(funcs_.size())) {
127 if (haveWidget) {
128 layout()->takeAt(2)->widget()->hide();
129 }
130 QWidget* toAdd = funcs_[index]->getWidget();
131
132 toAdd->show();
133 layoutVar->insertWidget(2, toAdd);
134 haveWidget = true;
135 }
136 }
137 } // namespace gui
138 } // namespace hpp
139