2010-07-20 00:03:03 +02:00
|
|
|
#include "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>
|
2010-07-20 05:40:21 +02:00
|
|
|
#include <QtGlobal>
|
2010-07-20 00:03:03 +02:00
|
|
|
|
|
|
|
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();
|
2010-07-20 05:40:21 +02:00
|
|
|
Q_ASSERT( m_renderer->load(QString(":/gcscontrol/images/joystickBackground.svg")) );
|
|
|
|
|
|
|
|
m_background = new QGraphicsSvgItem(QString(":/gcscontrol/images/joystickBackground.svg"));
|
2010-07-20 00:03:03 +02:00
|
|
|
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);
|
2010-07-20 05:40:21 +02:00
|
|
|
l_scene->setSceneRect(m_background->boundingRect());
|
2010-07-20 00:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2010-07-20 05:40:21 +02:00
|
|
|
|
2010-07-20 00:03:03 +02:00
|
|
|
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;
|
|
|
|
}
|
2010-07-20 05:40:21 +02:00
|
|
|
|
|
|
|
QGraphicsView::paintEvent(event);
|
2010-07-20 00:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JoystickControl::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
fitInView(m_background, Qt::KeepAspectRatio );
|
|
|
|
}
|