Skip to content
Snippets Groups Projects
MMMPlayerGuiPlugin.cpp 6.39 KiB
Newer Older
#include "MMMPlayerGuiPlugin.h"
#include "MMMPlayerConfigDialog.h"
#include "ui_MMMPlayerConfigDialog.h"

#include <Core/core/system/ArmarXDataPath.h>
#include <Core/observers/variant/SingleTypeVariantList.h>
#include <Core/util/variants/eigen3/MatrixVariant.h>
#include <Core/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 CONFIG_FILE_DEFAULT "/home/SMBAD/grube/armarx/Armar4/data/Armar4/motions/armar4.walking.mmm.xml"
#define PROJECT_DEFAULT "Armar4"
#define CONFIG_FILE_DEFAULT "Armar4/motions/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);
}


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()));

    int maxFrames = MMMPlayer->loadTrajectory(configFile, "");
    ui.frameSlider->setMaximum(maxFrames);
    on_loopPlayback_toggled(false);

    connectSlots();
}

void MMMPlayerWidget::onExitComponent()
{
}

void MMMPlayerWidget::onDisconnectComponent()
{
}

void MMMPlayerWidget::loadSettings(QSettings *settings)
{
}


void MMMPlayerWidget::saveSettings(QSettings *settings)
{
}


void MMMPlayerWidget::connectSlots()
{
    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(jointList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(jointListChanged(QListWidgetItem)));
}

void armarx::MMMPlayerWidget::on_startButton_clicked()
{
    bool started = MMMPlayer->startMMMPlayer();
    if (started)
    {
        updateTimer->start(100);
        ui.startButton->setDisabled(started);
        ui.pauseButton->setEnabled(started);
        ui.pauseButton->setText("Pause");
        ui.btnSelectConfig->setEnabled(false);
        ui.stopButton->setEnabled(started);
    }
}

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.frameSlider->setEnabled(paused);
}

void armarx::MMMPlayerWidget::on_stopButton_clicked()
{
    bool stopped = MMMPlayer->stopMMMPlayer();
    if(stopped)
    {
        updateTimer->stop();
        ui.startButton->setEnabled(stopped);
        ui.pauseButton->setDisabled(stopped);
        ui.pauseButton->setText("Pause");
        ui.btnSelectConfig->setEnabled(true);
        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]);
    int maxFrames = MMMPlayer->loadTrajectory(configFile, "");
    ui.frameSlider->setMaximum(maxFrames);
    ARMARX_INFO << "loaded new trajectory " + configFile;
    StringList jointNames = MMMPlayer->getJointNames();
    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->addItem(QString::fromStdString(jointName));
    }
}

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::on_loopPlayback_toggled(bool state)
{
    bool loop = MMMPlayer->setLoopPlayback(state);
    ui.loopPlayback->setChecked(loop);
}

void MMMPlayerWidget::jointListChanged(QListWidgetItem joint)
{
//    ui.selectedJointIndex->display(jointList->currentRow());

}


Q_EXPORT_PLUGIN2(robotapi_gui_MMMPlayerGuiPlugin, MMMPlayerGuiPlugin)