31 #include <QMouseEvent>
37 : QGraphicsView(parent)
39 setMinimumSize(64, 64);
40 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
41 setScene(
new QGraphicsScene(
this));
42 setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
44 m_renderer =
new QSvgRenderer();
45 bool test = m_renderer->load(QString(
":/gcscontrol/images/joystick.svg"));
49 m_background =
new QGraphicsSvgItem();
50 m_background->setSharedRenderer(m_renderer);
51 m_background->setElementId(QString(
"background"));
53 m_joystickEnd =
new QGraphicsSvgItem();
54 m_joystickEnd->setSharedRenderer(m_renderer);
55 m_joystickEnd->setElementId(QString(
"joystickEnd"));
57 m_joystickArea =
new QGraphicsSvgItem();
58 m_joystickArea->setSharedRenderer(m_renderer);
59 m_joystickArea->setElementId(QString(
"joystickArea"));
60 m_joystickArea->setPos(
61 (m_background->boundingRect().width() - m_joystickArea->boundingRect().width()) * 0.5,
62 (m_background->boundingRect().height() - m_joystickArea->boundingRect().height()) * 0.5);
63 m_joystickArea->setVisible(
false);
65 QGraphicsScene *l_scene = scene();
67 l_scene->addItem(m_background);
68 l_scene->addItem(m_joystickArea);
69 l_scene->addItem(m_joystickEnd);
70 l_scene->setSceneRect(m_background->boundingRect());
85 QRectF areaSize = m_joystickArea->boundingRect();
86 QPointF point(((1.0 + x) * areaSize.width() - m_joystickEnd->boundingRect().width()) * 0.5,
87 ((1.0 - y) * areaSize.height() - m_joystickEnd->boundingRect().height()) * 0.5);
88 m_joystickEnd->setPos(m_joystickArea->mapToScene(point));
96 QPointF point = m_joystickArea->mapFromScene(mapToScene(event->pos()));
97 QSizeF areaSize = m_joystickArea->boundingRect().size();
99 double y = -(point.y() / areaSize.height() - 0.5) * 2.0;
100 double x = (point.x() / areaSize.width() - 0.5) * 2.0;
118 if (event->button() == Qt::LeftButton) {
131 if (!m_renderer->isValid()) {
132 qDebug() <<
"Image file not loaded, not rendering";
135 QGraphicsView::paintEvent(event);
141 fitInView(m_background, Qt::KeepAspectRatio);
void mouseMoveEvent(QMouseEvent *event)
Redirect mouse move events to control position.
void positionClicked(double x, double y)
JoystickControl(QWidget *parent=nullptr)
Constructor for JoystickControl widget. Sets up the image of a joystick.
void paintEvent(QPaintEvent *event)
void mousePressEvent(QMouseEvent *event)
Redirect mouse move clicks to control position.
void resizeEvent(QResizeEvent *event)
void changePosition(double x, double y)
Update the displayed position based on an MCC update.