CmvrControlStation/src/urdfviewerwidget.h

146 lines
4.7 KiB
C
Raw Normal View History

2026-09-18 16:22:20 +08:00
#pragma once
#include <QHash>
#include <QPoint>
#include <QString>
#include <QStringList>
#include <QVector>
2026-09-18 16:22:20 +08:00
#include <QVector3D>
#include <QWidget>
class QLabel;
class QComboBox;
2026-09-18 16:22:20 +08:00
class QLineEdit;
class QProgressBar;
class QPushButton;
class QShowEvent;
class QTimer;
class QVBoxLayout;
namespace Qt3DCore {
class QEntity;
class QTransform;
}
namespace Qt3DExtras {
class Qt3DWindow;
}
namespace Qt3DRender {
class QCamera;
class QLayer;
2026-09-18 16:22:20 +08:00
class QScreenRayCaster;
class QViewport;
2026-09-18 16:22:20 +08:00
}
class UrdfViewerWidget : public QWidget
{
Q_OBJECT
public:
explicit UrdfViewerWidget(QWidget *parent = nullptr);
bool modelPreviewEnabled() const { return m_modelPreviewEnabled; }
2026-09-18 16:22:20 +08:00
~UrdfViewerWidget() override;
QString currentFile() const;
void prepareLoad(const QString &filePath);
bool saveDiagnosticViewport(const QString &path) const;
2026-09-18 16:22:20 +08:00
bool loadUrdf(const QString &filePath);
bool loadModelForIdentity(const QString &manufacturer, const QString &model,
const QString &subtype, const QString &descriptionId);
2026-09-18 16:22:20 +08:00
void setJointPositions(const QHash<QString, double> &positions);
QStringList jointNames() const;
QHash<QString, QPair<double, double>> jointLimits() const;
2026-09-18 16:22:20 +08:00
QHash<QString, double> teachingPositions() const;
QString selectedTeachingJoint() const;
void setTeachingEnabled(bool enabled);
void selectTeachingJoint(const QString &jointName);
void setTeachingJointPosition(const QString &jointName, double position);
void restoreTeachingFromTelemetry();
protected:
bool eventFilter(QObject *watched, QEvent *event) override;
void showEvent(QShowEvent *event) override;
signals:
void logMessage(const QString &level, const QString &message);
void modelLoaded(const QString &filePath, int linkCount, int jointCount);
void teachingModeChanged(bool enabled);
void teachingJointSelected(const QString &jointName, double position,
double lower, double upper);
void teachingPositionsChanged(const QHash<QString, double> &positions);
private slots:
void chooseBundledUrdf(int index);
2026-09-18 16:22:20 +08:00
void chooseUrdf();
void reloadUrdf();
void resetView();
private:
bool m_modelPreviewEnabled = false;
2026-09-18 16:22:20 +08:00
struct JointBinding {
int type = 0;
QVector3D axis;
Qt3DCore::QTransform *transform = nullptr;
double lower = 0.0;
double upper = 0.0;
};
void clearModel();
void ensureSceneInitialized();
void setLoading(bool loading, const QString &message = QString());
void orbitCamera(const QPoint &delta);
void panCamera(const QPoint &delta);
void zoomCamera(int angleDelta);
void applyJointPositions(const QHash<QString, double> &positions);
void attachTeachingPicker(Qt3DCore::QEntity *entity, const QString &jointName);
void attachTeachingPickersRecursively(Qt3DCore::QEntity *root,
const QString &jointName);
void emitTeachingSelection();
void setMotionDemoRunning(bool running);
void advanceMotionDemo();
void refreshBundledModels();
void syncModelSelection(const QString &filePath);
2026-09-18 16:22:20 +08:00
QString resolveMeshPath(const QString &uri) const;
void createOrientationIndicator();
void updateOrientationIndicator();
void positionOrientationIndicator();
2026-09-18 16:22:20 +08:00
void scheduleViewAll();
Qt3DExtras::Qt3DWindow *m_view = nullptr;
Qt3DRender::QCamera *m_camera = nullptr;
Qt3DRender::QCamera *m_orientationCamera = nullptr;
Qt3DRender::QLayer *m_orientationLayer = nullptr;
Qt3DRender::QViewport *m_orientationViewport = nullptr;
QVector<Qt3DCore::QTransform *> m_orientationLabelTransforms;
2026-09-18 16:22:20 +08:00
Qt3DRender::QScreenRayCaster *m_teachingRayCaster = nullptr;
Qt3DCore::QEntity *m_rootEntity = nullptr;
Qt3DCore::QEntity *m_modelRoot = nullptr;
QVBoxLayout *m_layout = nullptr;
QWidget *m_viewportPlaceholder = nullptr;
QWidget *m_viewContainer = nullptr;
QLabel *m_loadingTitle = nullptr;
QLabel *m_loadingDetail = nullptr;
QProgressBar *m_loadingProgress = nullptr;
QComboBox *m_modelCombo = nullptr;
2026-09-18 16:22:20 +08:00
QLineEdit *m_pathEdit = nullptr;
QLabel *m_statusLabel = nullptr;
QPushButton *m_reloadButton = nullptr;
QPushButton *m_demoButton = nullptr;
QTimer *m_demoTimer = nullptr;
QHash<QString, JointBinding> m_joints;
QStringList m_jointOrder;
QHash<QString, double> m_jointPositions;
QHash<QString, double> m_latestTelemetryPositions;
QString m_selectedTeachingJoint;
QPoint m_lastMousePosition;
Qt::MouseButton m_dragButton = Qt::NoButton;
bool m_teachingEnabled = false;
bool m_jointDragActive = false;
bool m_sceneInitialized = false;
int m_demoElapsedMs = 0;
QString m_modelSummary;
QString m_currentFile;
QString m_urdfDirectory;
};