Skip to content

Commit

Permalink
Mouse Coordinates (#53)
Browse files Browse the repository at this point in the history
* Signal which emits 2D axes coordinates on mouse position changed.

* Mouse coord shows correctly in fixed anchor mode, mouse anchor mode is WIP.

* MouseAnchor mouse coords mode is working.

* Round to 3 d.p.

* Stop using magic numbers.

* Default MouseCoordStyle to FixedAnchor.

* Comprehensive commenting.

* Do all mouse coordinate anchoring in mouseMoveEvent. Also added slot for setting MouseCoordStyle.

* Updated basic example to offer different mouse coordinate styles.

* Clang format.

* Update src/mouse.cpp

Co-authored-by: Tristan Youngs <[email protected]>

* Update src/mouse.cpp

Co-authored-by: Tristan Youngs <[email protected]>

* Update src/widget.h

Co-authored-by: Tristan Youngs <[email protected]>

* Throw runtime error for unhandled coordinate display styles.

* Use new enum class name.

* Fix bug with mouse coordinates being hidden near upper extents.

* Clang format.

* Include stdexcept.

* Stop doing dirty round.

* Format.

* Update src/mouse.cpp

Co-authored-by: Tristan Youngs <[email protected]>

Co-authored-by: Tristan Youngs <[email protected]>
  • Loading branch information
jswift-stfc and trisyoungs authored May 24, 2022
1 parent baf2d30 commit 5350af8
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 5 deletions.
7 changes: 7 additions & 0 deletions examples/basic/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class MainWindow : public QMainWindow
/*
* UI
*/
private slots:
void on_mouseCoordStyleCombo_currentIndexChanged(int index);

public slots:
// Set mouse coordinates in external label.
void setExternalMouseCoordinatesText(QPointF p);

private:
// Main form declaration
Ui::MainWindow ui_;
Expand Down
32 changes: 31 additions & 1 deletion examples/basic/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,36 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Mouse Coordinates</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Style</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="mouseCoordStyleCombo"/>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="externalMouseCoordLabel">
<property name="text">
<string>External</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
Expand All @@ -211,7 +241,7 @@
<x>0</x>
<y>0</y>
<width>956</width>
<height>23</height>
<height>22</height>
</rect>
</property>
</widget>
Expand Down
36 changes: 36 additions & 0 deletions examples/basic/mainwindow_funcs.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "mainwindow.h"
#include "material.h"
#include "ui_mainwindow.h"
#include "widget.h"
#include <stdexcept>

MainWindow::MainWindow() : QMainWindow()
{
Expand All @@ -26,6 +28,13 @@ MainWindow::MainWindow() : QMainWindow()
ui_.ZVisibleCheck->setChecked(ui_.TestingWidget->zAxis()->isEnabled());
connect(ui_.ZVisibleCheck, SIGNAL(toggled(bool)), ui_.TestingWidget->zAxis(), SLOT(setEnabled(bool)));

// Mouse Coordinates
ui_.mouseCoordStyleCombo->addItem(QString("Fixed Anchor"));
ui_.mouseCoordStyleCombo->addItem(QString("Anchor at Mouse"));
ui_.mouseCoordStyleCombo->addItem(QString("Use 'External' Slot"));
ui_.externalMouseCoordLabel->setVisible(false);
connect(ui_.TestingWidget, SIGNAL(mouseCoordChanged(QPointF)), this, SLOT(setExternalMouseCoordinatesText(QPointF)));

// Data
const auto nPoints = 1000;
const auto delta = 2.0 * M_PI / nPoints;
Expand All @@ -43,3 +52,30 @@ MainWindow::MainWindow() : QMainWindow()

ui_.TestingWidget->showAllData();
};

void MainWindow::on_mouseCoordStyleCombo_currentIndexChanged(int index)
{
switch (index)
{
case 0:
ui_.TestingWidget->setMouseCoordStyle(Mildred::CoordinateDisplayStyle::FixedAnchor);
ui_.externalMouseCoordLabel->setVisible(false);
break;
case 1:
ui_.TestingWidget->setMouseCoordStyle(Mildred::CoordinateDisplayStyle::MouseAnchor);
ui_.externalMouseCoordLabel->setVisible(false);
break;
case 2:
ui_.TestingWidget->setMouseCoordStyle(Mildred::CoordinateDisplayStyle::None);
ui_.externalMouseCoordLabel->setVisible(true);
break;
default:
throw(std::runtime_error("Unhandled coordinate display style.\n"));
break;
}
}

void MainWindow::setExternalMouseCoordinatesText(QPointF p)
{
ui_.externalMouseCoordLabel->setText(QString("%1 %2").arg(QString::number(p.x()), QString::number(p.y())));
}
2 changes: 1 addition & 1 deletion src/entities/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void TextEntity::setAnchorPoint(MildredMetrics::AnchorPoint anchor)
updateTranslation();
}

// 1 Set anchor position
//! Set anchor position
void TextEntity::setAnchorPosition(QVector3D p)
{
anchorPosition_ = p;
Expand Down
53 changes: 50 additions & 3 deletions src/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,54 @@ void MildredWidget::mousePositionChanged(Qt3DInput::QMouseEvent *event)
}
}

updateShaderParameters();

lastMousePosition_ = QPoint(event->x(), event->y());

if (flatView_)
{
// Ensure that mouse is within plot area.
if ((event->x() >= metrics_.displayVolumeOrigin().x()) &&
(event->x() <= (metrics_.displayVolumeExtent().x() + metrics_.displayVolumeOrigin().x())) &&
(height() - event->y() >= metrics_.displayVolumeOrigin().y()) &&
(height() - event->y() <= (metrics_.displayVolumeExtent().y() + metrics_.displayVolumeOrigin().y())))
{
// Convert mouse position to 2D axes value.
auto coords = toAxes2D(QPoint(event->x(), height() - event->y()));

// Emit signal indicating that mouse coordinates have been changed.
emit mouseCoordChanged(coords);

// Update the mouse coordinates in the text entity.
mouseCoordEntity_->setText(QString("%1 %2").arg(coords.x(), 0, 'g', 4).arg(coords.y(), 0, 'g', 4));

// Enable the text entity, to ensure that it is visible.
mouseCoordEntity_->setEnabled(true);

if (mouseCoordStyle_ == CoordinateDisplayStyle::FixedAnchor)
{
// Anchor the text entity at the bottom left of the widget.
mouseCoordEntity_->setAnchorPosition(
{-metrics_.displayVolumeOrigin().x(), -metrics_.displayVolumeOrigin().y(), 0.1});
}
else if (mouseCoordStyle_ == CoordinateDisplayStyle::MouseAnchor)
{
// Anchor the text entity at the mouse cursor.
mouseCoordEntity_->setAnchorPosition({float(event->x()) - metrics_.displayVolumeOrigin().x(),
height() - float(event->y()) - metrics_.displayVolumeOrigin().y(), 0});
}
else if (mouseCoordStyle_ == CoordinateDisplayStyle::None)
{
// Hide the text entity.
mouseCoordEntity_->setEnabled(false);
}
}
else
{
// Hide the text entity.
mouseCoordEntity_->setEnabled(false);
}
}

updateShaderParameters();
}

void MildredWidget::mouseButtonPressed(Qt3DInput::QMouseEvent *event) {}
Expand Down Expand Up @@ -97,4 +142,6 @@ void MildredWidget::mouseWheeled(Qt3DInput::QWheelEvent *event)

updateTransforms();
}
}
}

void MildredWidget::setMouseCoordStyle(CoordinateDisplayStyle style) { mouseCoordStyle_ = style; }
11 changes: 11 additions & 0 deletions src/scenegraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ void MildredWidget::createSceneGraph()
dataEntityParent_ = new Qt3DCore::QEntity(dataRootEntity_);
dataOriginTransform_ = new Qt3DCore::QTransform(dataEntityParent_);
dataEntityParent_->addComponent(dataOriginTransform_);

// Create mouse coord entity
auto *mouseCoordLabelMaterial =
createMaterial(sceneObjectsEntity_, RenderableMaterial::VertexShaderType::Unclipped,
RenderableMaterial::GeometryShaderType::None, RenderableMaterial::FragmentShaderType::Monochrome);
mouseCoordLabelMaterial->setAmbient(QColor(0, 0, 0, 255));
mouseCoordEntity_ = new TextEntity(sceneObjectsEntity_, QString("0.0, 0.0"));
mouseCoordEntity_->addComponent(mouseCoordLabelMaterial);
mouseCoordEntity_->setFont(QFont("monospace", 10.0));
mouseCoordEntity_->setAnchorPoint(MildredMetrics::AnchorPoint::BottomLeft);
mouseCoordEntity_->setEnabled(false);
}

//! Convert widget position to 2D (flat) coordinates
Expand Down
18 changes: 18 additions & 0 deletions src/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@

namespace Mildred
{
// Coordinate Display
enum class CoordinateDisplayStyle
{
None,
MouseAnchor,
FixedAnchor
};

//! The Mildred widget is the core class of Mildred.
/*!
* The Mildred widget is a standard QWidget displaying a Qt3D-based subwidget providing full 2D (flat) and 3D data
Expand Down Expand Up @@ -156,13 +164,23 @@ class MildredWidget : public QWidget
private:
// Last recorded mouse position
QPoint lastMousePosition_;
// Mouse coordinate style
CoordinateDisplayStyle mouseCoordStyle_{CoordinateDisplayStyle::FixedAnchor};
// Mouse coordinate entity
TextEntity *mouseCoordEntity_{nullptr};

private slots:
void mousePositionChanged(Qt3DInput::QMouseEvent *event);
void mouseButtonPressed(Qt3DInput::QMouseEvent *event);
void mouseButtonReleased(Qt3DInput::QMouseEvent *event);
void mouseWheeled(Qt3DInput::QWheelEvent *event);

public slots:
void setMouseCoordStyle(CoordinateDisplayStyle style);

signals:
void mouseCoordChanged(QPointF pos);

/*
* Keyboard Handling / Interaction
*/
Expand Down

0 comments on commit 5350af8

Please sign in to comment.