Skip to content

Commit

Permalink
ENH: Add support for connect/disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfr committed Aug 28, 2023
1 parent ab2bb48 commit c8cdf68
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 42 deletions.
2 changes: 1 addition & 1 deletion MixedReality/Logic/vtkSlicerMixedRealityLogic.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void vtkSlicerMixedRealityLogic::SetMixedRealityActive(bool activate)
{
if (this->GetMixedRealityConnected()
&& this->GetMixedRealityViewNode()
/* && this->GetMixedRealityViewNode()->HasError() */)
&& this->GetMixedRealityViewNode()->HasError())
{
// If it is connected already but there is an error then disconnect first then reconnect
// as the error may be resolved by reconnecting.
Expand Down
30 changes: 30 additions & 0 deletions MixedReality/MRML/vtkMRMLMixedRealityViewNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,33 @@ bool vtkMRMLMixedRealityViewNode::SetAndObserveReferenceViewNode(vtkMRMLViewNode
this->SetAndObserveReferenceViewNodeID(node->GetID());
return true;
}

//----------------------------------------------------------------------------
bool vtkMRMLMixedRealityViewNode::HasError()
{
return !this->LastErrorMessage.empty();
}

//----------------------------------------------------------------------------
void vtkMRMLMixedRealityViewNode::ClearError()
{
this->SetError("");
}

//----------------------------------------------------------------------------
void vtkMRMLMixedRealityViewNode::SetError(const std::string& errorText)
{
if (this->LastErrorMessage == errorText)
{
// no change
return;
}
this->LastErrorMessage = errorText;
this->Modified();
}

//----------------------------------------------------------------------------
std::string vtkMRMLMixedRealityViewNode::GetError() const
{
return this->LastErrorMessage;
}
14 changes: 14 additions & 0 deletions MixedReality/MRML/vtkMRMLMixedRealityViewNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ class VTK_SLICER_MIXEDREALITY_MODULE_MRML_EXPORT vtkMRMLMixedRealityViewNode
vtkSetMacro(PlayerIPAddress, const std::string);
vtkGetMacro(PlayerIPAddress, std::string);

/// Return true if an error has occurred.
bool HasError();

/// Clear error state.
void ClearError();

/// Set error message. Non-empty string means that an error has occurred.
void SetError(const std::string& errorText);

/// Get error message. Non-empty string means that an error has occurred.
std::string GetError() const;

protected:
vtkMRMLMixedRealityViewNode();
~vtkMRMLMixedRealityViewNode() override;
Expand All @@ -83,6 +95,8 @@ class VTK_SLICER_MIXEDREALITY_MODULE_MRML_EXPORT vtkMRMLMixedRealityViewNode

std::string PlayerIPAddress;

std::string LastErrorMessage;

static const char* ReferenceViewNodeReferenceRole;
};

Expand Down
82 changes: 79 additions & 3 deletions MixedReality/Resources/UI/qSlicerMixedRealityModuleWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,85 @@
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="PlayerIPAddressLabel">
<widget class="QLabel" name="ConnectToHardwareLabel">
<property name="text">
<string>PlayerIPAddress</string>
<string>Connect to hardware:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="PlayerIPAddressLineEdit"/>
<layout class="QHBoxLayout" name="ConnectionLayout">
<item>
<widget class="ctkCheckBox" name="ConnectCheckBox"/>
</item>
<item>
<widget class="QLineEdit" name="PlayerIPAddressLineEdit">
<property name="inputMask">
<string>999.999.999.999</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="ConnectionStatusLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="palette">
<palette>
<active>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="WindowText">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>190</red>
<green>190</green>
<blue>190</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="EnableRenderingLabel">
<property name="text">
<string>Enable rendering:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="ctkCheckBox" name="RenderingEnabledCheckBox"/>
</item>
</layout>
</widget>
Expand All @@ -55,6 +126,11 @@
<header>qSlicerWidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>ctkCheckBox</class>
<extends>QCheckBox</extends>
<header>ctkCheckBox.h</header>
</customwidget>
<customwidget>
<class>ctkCollapsibleButton</class>
<extends>QWidget</extends>
Expand Down
54 changes: 27 additions & 27 deletions MixedReality/Widgets/qMRMLMixedRealityView.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ void qMRMLMixedRealityViewPrivate::updateWidgetFromMRML()
{
this->destroyRenderWindow();
}
// if (this->MRMLMixedRealityViewNode)
// {
// this->MRMLMixedRealityViewNode->ClearError();
// }
if (this->MRMLMixedRealityViewNode)
{
this->MRMLMixedRealityViewNode->ClearError();
}
return;
}

Expand All @@ -296,12 +296,12 @@ void qMRMLMixedRealityViewPrivate::updateWidgetFromMRML()
QApplication::setOverrideCursor(QCursor(Qt::BusyCursor));
this->createRenderWindow();
QApplication::restoreOverrideCursor();
// if (!q->isHardwareConnected())
// {
// this->MRMLMixedRealityViewNode->SetError("Connection failed");
// return;
// }
// this->MRMLMixedRealityViewNode->ClearError();
if (!q->isHardwareConnected())
{
this->MRMLMixedRealityViewNode->SetError("Connection failed");
return;
}
this->MRMLMixedRealityViewNode->ClearError();
}

if (this->DisplayableManagerGroup->GetMRMLDisplayableNode() != this->MRMLMixedRealityViewNode.GetPointer())
Expand Down Expand Up @@ -380,14 +380,14 @@ void qMRMLMixedRealityViewPrivate::updateWidgetFromMRML()
// }
// }

// if (this->MRMLMixedRealityViewNode->GetActive())
// {
if (this->MRMLMixedRealityViewNode->GetActive())
{
this->MixedRealityLoopTimer.start(0);
// }
// else
// {
// this->MixedRealityLoopTimer.stop();
// }
}
else
{
this->MixedRealityLoopTimer.stop();
}
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -731,20 +731,20 @@ void qMRMLMixedRealityView::getDisplayableManagers(vtkCollection* displayableMan
}

//------------------------------------------------------------------------------
//bool qMRMLMixedRealityView::isHardwareConnected()const
//{
// vtkOpenVRRenderWindow* renWin = this->renderWindow();
// if (!renWin)
// {
// return false;
// }
bool qMRMLMixedRealityView::isHardwareConnected()const
{
vtkOpenXRRenderWindow* renWin = this->renderWindow();
if (!renWin)
{
return false;
}
// if (!renWin->GetHMD())
// {
// return false;
// }
// // connected successfully
// return true;
//}
// connected successfully
return true;
}

//------------------------------------------------------------------------------
//void qMRMLMixedRealityView::setGrabObjectsEnabled(bool enable)
Expand Down
2 changes: 1 addition & 1 deletion MixedReality/Widgets/qMRMLMixedRealityView.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Q_SLICER_MODULE_MIXEDREALITY_WIDGETS_EXPORT qMRMLMixedRealityView : public
// Q_INVOKABLE void updateViewFromReferenceViewCamera();

/// Get underlying RenderWindow
// Q_INVOKABLE bool isHardwareConnected()const;
Q_INVOKABLE bool isHardwareConnected()const;

/// Enable/disable grabbing and moving objects in the scene
// Q_INVOKABLE void setGrabObjectsEnabled(bool enable);
Expand Down
46 changes: 37 additions & 9 deletions MixedReality/qSlicerMixedRealityModuleWidget.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ void qSlicerMixedRealityModuleWidget::setup()
d->setupUi(this);
this->Superclass::setup();

connect(d->PlayerIPAddressLineEdit, SIGNAL(textChanged(QString)), this, SLOT(setPlayerIPAddress(QString)));
connect(d->ConnectCheckBox, SIGNAL(toggled(bool)), this, SLOT(setMixedRealityConnected(bool)));
connect(d->RenderingEnabledCheckBox, SIGNAL(toggled(bool)), this, SLOT(setMixedRealityActive(bool)));

this->updateWidgetFromMRML();

// If virtual reality logic is modified it indicates that the view node may changed
qvtkConnect(logic(), vtkCommand::ModifiedEvent, this, SLOT(updateWidgetFromMRML()));
}

//--------------------------------------------------------------------------
Expand All @@ -76,18 +82,40 @@ void qSlicerMixedRealityModuleWidget::updateWidgetFromMRML()
vtkSlicerMixedRealityLogic* xrLogic = vtkSlicerMixedRealityLogic::SafeDownCast(this->logic());
vtkMRMLMixedRealityViewNode* xrViewNode = xrLogic->GetMixedRealityViewNode();

bool wasBlocked = d->PlayerIPAddressLineEdit->blockSignals(true);
d->PlayerIPAddressLineEdit->setText(QString::fromStdString(xrViewNode->GetPlayerIPAddress()));
bool wasBlocked = d->ConnectCheckBox->blockSignals(true);
d->ConnectCheckBox->setChecked(xrViewNode != nullptr && xrViewNode->GetVisibility());
d->ConnectCheckBox->blockSignals(wasBlocked);

wasBlocked = d->PlayerIPAddressLineEdit->blockSignals(true);
if (xrViewNode != nullptr)
{
d->PlayerIPAddressLineEdit->setText(QString::fromStdString(xrViewNode->GetPlayerIPAddress()));
}
d->PlayerIPAddressLineEdit->blockSignals(wasBlocked);

QString errorText;
if (xrViewNode && xrViewNode->HasError())
{
errorText = xrViewNode->GetError().c_str();
}
d->ConnectionStatusLabel->setText(errorText);

wasBlocked = d->RenderingEnabledCheckBox->blockSignals(true);
d->RenderingEnabledCheckBox->setChecked(xrViewNode != nullptr && xrViewNode->GetActive());
d->RenderingEnabledCheckBox->blockSignals(wasBlocked);
}

//-----------------------------------------------------------------------------
void qSlicerMixedRealityModuleWidget::setPlayerIPAddress(const QString& value)
void qSlicerMixedRealityModuleWidget::setMixedRealityConnected(bool connect)
{
Q_D(qSlicerMixedRealityModuleWidget);
vtkSlicerMixedRealityLogic* xrLogic = vtkSlicerMixedRealityLogic::SafeDownCast(this->logic());
vtkMRMLMixedRealityViewNode* xrViewNode = xrLogic->GetMixedRealityViewNode();
if (xrViewNode)
{
xrViewNode->SetPlayerIPAddress(value.toStdString());
}
xrLogic->SetMixedRealityConnected(connect, d->PlayerIPAddressLineEdit->text().toStdString());
}

//-----------------------------------------------------------------------------
void qSlicerMixedRealityModuleWidget::setMixedRealityActive(bool activate)
{
vtkSlicerMixedRealityLogic* xrLogic = vtkSlicerMixedRealityLogic::SafeDownCast(this->logic());
xrLogic->SetMixedRealityActive(activate);
}
3 changes: 2 additions & 1 deletion MixedReality/qSlicerMixedRealityModuleWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class Q_SLICER_QTMODULES_MIXEDREALITY_EXPORT qSlicerMixedRealityModuleWidget :
virtual ~qSlicerMixedRealityModuleWidget();

public slots:
void setPlayerIPAddress(const QString& value);
void setMixedRealityConnected(bool connect);
void setMixedRealityActive(bool activate);

protected slots:
void updateWidgetFromMRML();
Expand Down

0 comments on commit c8cdf68

Please sign in to comment.