#include "MMMPlayerGuiPlugin.h" #include "MMMPlayerConfigDialog.h" #include "ui_MMMPlayerConfigDialog.h" #include <ArmarXCore/core/system/ArmarXDataPath.h> #include <ArmarXCore/observers/variant/SingleTypeVariantList.h> #include <ArmarXCore/util/variants/eigen3/MatrixVariant.h> #include <ArmarXCore/observers/variant/TimestampVariant.h> #include <IceUtil/Time.h> // Qt headers #include <QInputDialog> #include <Qt> #include <QtGlobal> #include <QtGui/QPushButton> #include <QtGui/QComboBox> #include <QtGui/QMenu> #include <boost/filesystem.hpp> using namespace armarx; #define MMMPLAYER_DEFAULT_NAME "MMMPlayer" #define PROJECT_DEFAULT "Armar4" #define CONFIG_FILE_DEFAULT "data/MMM/armar4.walking.mmm.xml" MMMPlayerGuiPlugin::MMMPlayerGuiPlugin() { addWidget<MMMPlayerWidget>(); } MMMPlayerWidget::MMMPlayerWidget() { // init gui ui.setupUi(getWidget()); fileDialog2 = new QFileDialog(); fileDialog2->setModal(true); fileDialog2->setFileMode(QFileDialog::ExistingFiles); QStringList fileTypes; fileTypes << tr("XML (*.xml)") << tr("All Files (*.*)"); fileDialog2->setNameFilters(fileTypes); jointList = new QListWidget(); updateTimer = new QTimer(this); } /* QPointer<QDialog> MMMPlayerWidget::getConfigDialog(QWidget* parent) { if(!dialog) { dialog = new MMMPlayerConfigDialog(parent); boost::filesystem::path dir(CONFIG_FILE_DEFAULT); dialog->fileDialog->setDirectory(QString::fromStdString(dir.remove_filename().string())); dialog->ui->editConfigFile->setText(QString::fromStdString(CONFIG_FILE_DEFAULT)); } return qobject_cast<MMMPlayerConfigDialog*>(dialog); } void MMMPlayerWidget::configured() { ARMARX_VERBOSE << "MMMPlayerWidget::configured()"; configFile = dialog->ui->editConfigFile->text().toStdString(); ARMARX_VERBOSE << "config file " + configFile; } */ void MMMPlayerWidget::onInitComponent() { usingProxy(MMMPLAYER_DEFAULT_NAME); // ui.frameSlider->setEnabled(false); connectSlots(); } void MMMPlayerWidget::onConnectComponent() { MMMPlayer = getProxy<MMMPlayerInterfacePrx>(MMMPLAYER_DEFAULT_NAME); configFile = MMMPlayer->getMotionPath(); ui.editConfigFile->setText(QString::fromStdString(configFile)); boost::filesystem::path dir(configFile); fileDialog2->setDirectory(QString::fromStdString(dir.remove_filename().string())); setupJointList(); ui.frameSlider->blockSignals(true); ui.frameSlider->setMaximum(MMMPlayer->getNumberOfFrames()); on_loopPlayback_toggled(false); } void MMMPlayerWidget::onExitComponent() { } void MMMPlayerWidget::onDisconnectComponent() { } void MMMPlayerWidget::loadSettings(QSettings* settings) { } void MMMPlayerWidget::saveSettings(QSettings* settings) { } void MMMPlayerWidget::connectSlots() { connect(ui.initializeButton, SIGNAL(clicked()), this, SLOT(on_initializeButton_clicked())); connect(ui.startButton, SIGNAL(clicked()), this, SLOT(on_startButton_clicked())); connect(ui.pauseButton, SIGNAL(clicked()), this, SLOT(on_pauseButton_clicked())); connect(ui.stopButton, SIGNAL(clicked()), this, SLOT(on_stopButton_clicked())); connect(ui.btnSelectConfig, SIGNAL(clicked()), fileDialog2, SLOT(show())); connect(ui.selectJoints, SIGNAL(clicked()), jointList, SLOT(show())); connect(fileDialog2, SIGNAL(accepted()), this, SLOT(configFileSelected())); connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateSlider())); // connect(ui.frameSlider, SIGNAL(valueChanged(int)), this, SLOT(setFrameNumber(int))); connect(ui.loopPlayback, SIGNAL(toggled(bool)), this, SLOT(on_loopPlayback_toggled(bool))); connect(ui.spinBoxFPS, SIGNAL(valueChanged(int)), this, SLOT(on_spinBoxFPS_valueChanged(int))); connect(ui.controlMode, SIGNAL(currentIndexChanged(int)), this, SLOT(on_controlMode_changed(int))); connect(jointList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(jointListChanged(QListWidgetItem*))); } void armarx::MMMPlayerWidget::on_initializeButton_clicked() { // set position control mode and move to first frame as initial pose on_controlMode_changed(0); MMMPlayer->setCurrentFrame(0); ui.startButton->setEnabled(true); } void armarx::MMMPlayerWidget::on_startButton_clicked() { bool started = MMMPlayer->startMMMPlayer(); if (started) { updateTimer->start(100); ui.initializeButton->setDisabled(started); ui.startButton->setDisabled(started); ui.controlMode->setDisabled(started); ui.pauseButton->setEnabled(started); ui.pauseButton->setText("Pause"); ui.btnSelectConfig->setEnabled(false); ui.spinBoxFPS->setDisabled(started); ui.stopButton->setEnabled(started); ui.frameSlider->setDisabled(started); ui.frameSlider->blockSignals(true); } } void MMMPlayerWidget::on_pauseButton_clicked() { bool paused = MMMPlayer->pauseMMMPlayer(); if (paused) { // updateTimer->stop(); ui.pauseButton->setText("Resume"); } else { // updateTimer->start(100); ui.pauseButton->setText("Pause"); } ui.controlMode->setEnabled(paused); ui.frameSlider->setEnabled(paused); ui.spinBoxFPS->setEnabled(paused); ui.frameSlider->blockSignals(!paused); } void armarx::MMMPlayerWidget::on_stopButton_clicked() { bool stopped = MMMPlayer->stopMMMPlayer(); if (stopped) { updateTimer->stop(); ui.initializeButton->setEnabled(stopped); ui.startButton->setEnabled(stopped); ui.pauseButton->setText("Pause"); ui.btnSelectConfig->setEnabled(true); ui.controlMode->setEnabled(stopped); ui.spinBoxFPS->setEnabled(stopped); ui.frameSlider->blockSignals(false); ui.pauseButton->setDisabled(stopped); ui.stopButton->setDisabled(stopped); } } void MMMPlayerWidget::setFrameNumber(int frameNumber) { MMMPlayer->setCurrentFrame(frameNumber); ui.currentFrame->display(frameNumber); } void MMMPlayerWidget::configFileSelected() { configFile = (this->fileDialog2->selectedFiles()[0]).toStdString(); ui.editConfigFile->setText(this->fileDialog2->selectedFiles()[0]); MMMPlayer->loadTrajectory(configFile, ""); ui.frameSlider->setMaximum(MMMPlayer->getNumberOfFrames()); ARMARX_INFO << "loaded new trajectory " + configFile; setupJointList(); } void MMMPlayerWidget::setupJointList() { StringList jointNames = MMMPlayer->getJointNames(); jointList->blockSignals(true); jointList->clear(); for (size_t i = 0; i < jointNames.size(); ++i) { std::string jointName = jointNames.at(i); QListWidgetItem* joint = new QListWidgetItem(QString::fromStdString(jointName), jointList); joint->setCheckState(Qt::Checked); } jointList->blockSignals(false); } void MMMPlayerWidget::updateSlider() { int currentFrame = MMMPlayer->getCurrentFrame(); ui.frameSlider->blockSignals(true); ui.frameSlider->setSliderPosition(currentFrame); if (!(ui.loopPlayback->isChecked())) if (currentFrame >= ui.frameSlider->maximum() || currentFrame < 0) { on_stopButton_clicked(); } ui.frameSlider->blockSignals(false); ui.currentFrame->display(currentFrame); } void MMMPlayerWidget::jointListChanged(QListWidgetItem* joint) { bool jointState; if (joint->checkState() == Qt::Checked) { jointState = MMMPlayer->setJointsInUse(joint->text().toStdString(), true); } else { jointState = MMMPlayer->setJointsInUse(joint->text().toStdString(), false); } if (jointState) { joint->setCheckState(Qt::Checked); } else { joint->setCheckState(Qt::Unchecked); } } void MMMPlayerWidget::on_spinBoxFPS_valueChanged(int value) { MMMPlayer->setFPS(value); } void MMMPlayerWidget::on_loopPlayback_toggled(bool state) { ui.loopPlayback->setChecked(MMMPlayer->setLoopPlayback(state)); } void MMMPlayerWidget::on_controlMode_changed(int controlMode) { ui.controlMode->setCurrentIndex(MMMPlayer->setUsePID(controlMode)); } Q_EXPORT_PLUGIN2(robotapi_gui_MMMPlayerGuiPlugin, MMMPlayerGuiPlugin)