1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-14 21:23:52 +01:00
LibrePilot/ground/src/plugins/gcscontrol/joystickcontrol.cpp

91 lines
2.5 KiB
C++
Raw Normal View History

#include "joystickcontrol.h"
#include "ui_joystickcontrol.h"
#include "extensionsystem/pluginmanager.h"
#include <QDebug>
#include <QStringList>
#include <QtGui/QWidget>
#include <QtGui/QTextEdit>
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QMessageBox>
#include <QMouseEvent>
JoystickControl::JoystickControl(QWidget *parent) :
QGraphicsView(parent)
{
setMinimumSize(64,64);
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
setScene(new QGraphicsScene(this));
setRenderHints(QPainter::Antialiasing);
m_renderer = new QSvgRenderer();
m_renderer->load(QString(":/gcscontrol/images/joystickBackground.svg"));
m_background = new QGraphicsSvgItem();
m_background->setSharedRenderer(m_renderer);
QGraphicsScene *l_scene = scene();
l_scene->clear(); // This also deletes all items contained in the scene.
l_scene->addItem(m_background);
}
JoystickControl::~JoystickControl()
{
}
ManualControlCommand* JoystickControl::getMCC()
{
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
ManualControlCommand* obj = dynamic_cast<ManualControlCommand*>( objManager->getObject(QString("ManualControlCommand")) );
return obj;
}
void JoystickControl::mousePressEvent(QMouseEvent *event)
{
Qt::MouseButton button = event->button();
QPoint point = event->pos();
QSize widgetSize = this->size();
double x = 2 * ( ((double)point.x()) / ((double)widgetSize.width()) - .5 );
double y = 2 * ( ((double)point.y()) / ((double)widgetSize.height()) - .5);
if( button == Qt::LeftButton && this->objectName() == QString("widgetLeftStick"))
{
ManualControlCommand::DataFields data = getMCC()->getData();
data.Pitch = x;
data.Yaw = y;
getMCC()->setData(data);
}
if( button == Qt::LeftButton && this->objectName() == QString("widgetRightStick"))
{
ManualControlCommand::DataFields data = getMCC()->getData();
data.Throttle = x;
data.Roll = y;
getMCC()->setData(data);
}
}
void JoystickControl::paint()
{
update();
}
void JoystickControl::paintEvent(QPaintEvent *event)
{
// Skip painting until the dial file is loaded
if (! m_renderer->isValid()) {
qDebug()<<"Dial file not loaded, not rendering";
// return;
}
QGraphicsView::paintEvent(event);
}
void JoystickControl::resizeEvent(QResizeEvent *event)
{
fitInView(m_background, Qt::KeepAspectRatio );
}