Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
QmitknnInteractiveToolGUI.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef QmitknnInteractiveToolGUI_h
14 #define QmitknnInteractiveToolGUI_h
15 
17 #include <mitkIPreferences.h>
18 #include <mitkLabelSetImage.h>
19 #include <mitknnInteractiveTool.h>
20 #include <mitkWeakPointer.h>
22 
23 #include <QString>
24 
25 #include <memory>
26 #include <optional>
27 #include <vector>
28 
29 class QAbstractButton;
30 class QButtonGroup;
31 class QPushButton;
32 class QTimer;
33 
34 namespace mitk::nnInteractive
35 {
36  struct VersionCheckResult;
37 }
38 
39 namespace Ui
40 {
42 }
43 
66 {
67  Q_OBJECT
68 
69 public:
71  itkFactorylessNewMacro(Self);
72 
73 protected:
76 
79 
83 
87 
96  void InitializeUI(QBoxLayout* mainLayout) override;
97 
101 
105 
110  void EnableInitializeButtons(bool enabled);
111 
121  void OnInitializeButtonToggled(bool checked);
122 
126 
134 
142 
147  void OnAutoRefineCheckBoxToggled(bool checked);
148 
153  void OnAutoZoomCheckBoxToggled(bool checked);
154 
161 
171  void OnInteractorToggled(mitk::nnInteractive::InteractionType interactionType, bool checked);
172 
183 
188  void OnConfirmCleanUp(bool isConfirmed);
189 
196 
206 
216 
229 
238 
248  void UpdateModelLicenseDisplay(const std::optional<std::string>& license);
249 
255 
261  void UncheckOtherInteractorButtons(QPushButton* interactorButton);
262 
270  bool Install();
271 
272 private:
274  bool IsAutoCreateNextLabelEnabled() const;
275 
277  bool IsAutoConfirmEnabled() const;
278 
284  bool IsNamingPromptSkippedOnAutoCreate() const;
285 
287  bool AreShortcutsShownInLabels() const;
288 
295  void ApplyShortcutLabels();
296 
300  void OnPreferenceChangedEvent(const mitk::IPreferences::ChangeEvent& event);
301 
312  bool AutoCreateAndSelectNewLabel();
313 
315  void InvalidateAutoCreatedLabel();
316 
321  void OnAutoCreatedSegmentationDeleted();
322 
324  void ReEnableLastInteractor();
325 
333  void ApplyCapabilityGating();
334 
342  void UpdateUndoButtonState();
343 
356  void MaybeInitializeWithLabelMask(mitk::MultiLabelSegmentation::LabelValueType labelValue);
357 
367  void UpdateConfirmButtonLabel();
368 
376  void UpdateInitializeButtonText();
377 
385  void UncheckInitializeButton();
386 
404  bool OfferInPlaceUpdate(const mitk::nnInteractive::VersionCheckResult& versionCheck, bool clientOnly);
405 
411  bool RunUpdate(bool clientOnly);
412 
422  void MaybePromptModelSwitch(bool localAvailable);
423 
424  struct ShortcutLabel
425  {
426  QPushButton* button;
427  Qt::Key key;
428  QString baseText;
429  };
430 
431  std::unique_ptr<Ui::QmitknnInteractiveToolGUI> m_Ui;
432  QButtonGroup* m_PromptTypeButtonGroup;
433  PromptType m_PromptType;
434  std::unordered_map<InteractionType, QPushButton*> m_InteractorButtons;
435 
436  std::optional<mitk::MultiLabelSegmentation::LabelValueType> m_AutoCreatedLabelValue;
437  std::optional<mitk::MultiLabelSegmentation::LabelValueType> m_PreviousActiveLabelValue;
438  mitk::WeakPointer<mitk::MultiLabelSegmentation> m_AutoCreatedLabelSegmentation;
439  QAbstractButton* m_LastInteractorButton = nullptr;
440  bool m_AutoConfirmInProgress = false;
441 
442  // Set while AutoCreateAndSelectNewLabel() drives the inspector's selection.
443  // The inspector emits CurrentSelectionChanged synchronously, which would
444  // otherwise re-enter OnActiveLabelChanged and reset/re-seed the session
445  // mid-confirm. The slot honours this guard and stays a no-op.
446  bool m_SuppressActiveLabelChanged = false;
447 
448  // Whether the running session reports undo support (nnInteractive >= 2.3.3).
449  // Cached at session start (ApplyCapabilityGating) and used to gate the Undo
450  // button; older versions report no support and the button stays disabled.
451  bool m_SupportsUndo = false;
452 
453  QTimer* m_HeartbeatTimer = nullptr;
454 
455  // Set when a lost remote session is being torn down so a second
456  // SessionExpiredEvent (the tool emits it from both the heartbeat and a
457  // mid-interaction failure) cannot queue a duplicate teardown/dialog. Cleared
458  // in OnSessionEnded once teardown completes.
459  bool m_SessionExpiredHandled = false;
460 
461  mitk::IPreferences* m_Preferences = nullptr;
462  std::vector<ShortcutLabel> m_ShortcutLabels;
463  QString m_PromptTypeBaseTitle;
464  QString m_PreviewLabelBaseText;
465 };
466 
467 #endif
#define MITKPYTHONSEGMENTATIONUI_EXPORT
GUI base class for tools derived from mitk::SegWithPreviewTool.
GUI widget for the nnInteractiveTool segmentation tool.
void OnSessionEnded()
Handles the tool's SessionEndedEvent.
void OnSessionExpired()
Handles the tool's SessionExpiredEvent.
void InitializePromptType()
Initializes the prompt type button group and shortcut.
void UncheckOtherInteractorButtons(QPushButton *interactorButton)
Unchecks all interactor buttons except the specified one.
void OnToolDeactivated()
Handles the tool's DeactivatedEvent.
void OnHeartbeatTimeout()
Sends a keep-alive heartbeat to the remote nnInteractive server.
void OnAutoZoomCheckBoxToggled(bool checked)
Handles auto-zoom checkbox toggle.
~QmitknnInteractiveToolGUI() override
Destructor. Restores any override cursors and disconnects events.
mitk::nnInteractiveTool * GetTool()
Returns the connected nnInteractiveTool.
void OnInteractorToggled(mitk::nnInteractive::InteractionType interactionType, bool checked)
Handles toggling of an interactor button.
void OnPreviewUpdated()
Handles the tool's PreviewUpdatedEvent.
void UpdateModelLicenseDisplay(const std::optional< std::string > &license)
Shows the model checkpoint license below the Initialize button, or clears it when passed std::nullopt...
bool Install()
Checks if nnInteractive is installed and shows the install dialog if not.
void OnActiveLabelChanged(const mitk::MultiLabelSegmentation::LabelValueVectorType &labels)
Reacts to a change of the selected label in the host's label inspector while a session is running.
void InitializeUI(QBoxLayout *mainLayout) override
Initializes the UI widgets and connects signals.
void OnSettingsButtonClicked()
Opens the nnInteractive preferences dialog.
void OnUndoButtonClicked()
Undoes the last interaction and refreshes the Undo button state.
QmitknnInteractiveToolGUI()
Constructs the GUI with confirm segmentation button disabled by default.
void InitializeInteractorButtons()
Initializes the interactor toggle buttons with icons and shortcuts.
void OnPromptTypeChanged()
Handles prompt type changes between positive and negative.
void OnConfirmCleanUp(bool isConfirmed)
Handles cleanup confirmation from the tool.
void EnableInitializeButtons(bool enabled)
Enables or disables the initialize and settings buttons.
void OnResetInteractionsButtonClicked()
Resets all interactions and switches to positive prompt type.
void OnInitializeButtonToggled(bool checked)
Handles the initialize button toggle event.
mitkClassMacro(QmitknnInteractiveToolGUI, QmitkSegWithPreviewToolGUIBase)
void OnAutoRefineCheckBoxToggled(bool checked)
Handles auto-refine checkbox toggle.
Event object sent on IPreferences::OnPropertyChanged events.
Interface to application preferences.
std::vector< LabelValueType > LabelValueVectorType
mitk::Label::PixelType LabelValueType
Segmentation tool integrating the nnInteractive deep learning model.
InteractionType
Specifies the types of interactions available.
PromptType
Specifies the types of prompts used in nnInteractive.
Result of CheckInstalledVersion(): the verdict plus the version strings it was derived from (empty wh...