-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstandalone_viewer.py
More file actions
173 lines (139 loc) · 5.3 KB
/
standalone_viewer.py
File metadata and controls
173 lines (139 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sys
from functools import partial
import vtk
from ccpi.viewer import viewer2D, viewer3D
from ccpi.viewer.ui.main_windows import TwoViewersMainWindow
from qtpy import QtWidgets
from qtpy.QtWidgets import QAction, QCheckBox
import logging
import argparse
class StandaloneViewerMainWindow(TwoViewersMainWindow):
"""
A main window for displaying two viewers side by side, with a menu bar
for selecting the images to be displayed.
The first image selected will be displayed on both viewers. The second
is an image overlay which is displayed on the 2D viewer/s only.
A dock widget is created which contains widgets for displaying the
file name of the image shown on the viewer, and the level of downsampling
of the image displayed on the viewer.
"""
def __init__(
self,
title="StandaloneViewer",
app_name="Standalone Viewer",
settings_name=None,
organisation_name=None,
viewer1_type="2D",
viewer2_type="3D",
):
super(StandaloneViewerMainWindow, self).__init__(
title,
app_name,
settings_name,
organisation_name,
viewer1_type,
viewer2_type,
)
self.addToMenu()
self.addToViewerCoordsDockWidget()
self.image_overlay = vtk.vtkImageData()
def addToMenu(self):
"""
Adds actions to the menu bar for selecting the images to be displayed
"""
file_menu = self.menus["File"]
# insert image selection as first action in file menu:
image2_action = QAction("Select Image Overlay", self)
image2_action.triggered.connect(lambda: self.setViewersInputFromDialog(self.viewers, input_num=2))
file_menu.insertAction(file_menu.actions()[0], image2_action)
image1_action = QAction("Select Image", self)
image1_action.triggered.connect(lambda: self.setViewersInputFromDialog(self.viewers))
file_menu.insertAction(file_menu.actions()[0], image1_action)
def addToViewerCoordsDockWidget(self):
"""
Adds widgets to the viewer coords dock widget for displaying the
image overlay, and for showing/hiding the 2D and 3D viewers.
"""
checkbox = QCheckBox("Show Image Overlay")
checkbox.setChecked(True)
self.viewer_coords_dock.widget().addSpanningWidget(checkbox, "image_overlay")
checkbox.stateChanged.connect(partial(self.showHideImageOverlay))
checkbox2 = QCheckBox("Show 2D Viewer")
checkbox2.setChecked(True)
checkbox2.stateChanged.connect(partial(self.showHideViewer, 0))
checkbox3 = QCheckBox("Show 3D Viewer")
checkbox3.setChecked(True)
self.viewer_coords_dock.widget().addWidget(checkbox3, checkbox2, "show_viewer")
checkbox3.stateChanged.connect(partial(self.showHideViewer, 1))
def showHideImageOverlay(self, show=True):
"""
Shows or hides the image overlay/s on the viewers
"""
for viewer in self.viewer_coords_dock.viewers:
if isinstance(viewer, viewer2D):
if show:
if hasattr(self, "image_overlay"):
viewer.setInputData2(self.image_overlay)
else:
self.image_overlay = viewer.image2
viewer.setInputData2(vtk.vtkImageData())
class standalone_viewer(object):
"""
Launches a StandaloneViewerMainWindow instance.
Parameters:
------------
title: str
title of the window
viewer1_type: '2D' or '3D'
viewer2_type: '2D', '3D' or None
if None, only one viewer is displayed
"""
def __init__(self, title="", viewer1_type="2D", viewer2_type="3D", *args, **kwargs):
"""Creator
Parameters:
------------
title: str
title of the window
viewer1_type: '2D' or '3D'
viewer2_type: '2D', '3D' or None
if None, only one viewer is displayed
"""
app = QtWidgets.QApplication(sys.argv)
self.app = app
self.set_up(title, viewer1_type, viewer2_type, *args, **kwargs)
def set_up(self, title, viewer1_type, viewer2_type=None, *args, **kwargs):
"""
Sets up the standalone viewer.
Parameters:
------------
title: str
title of the window
viewer1_type: '2D' or '3D'
viewer2_type: '2D', '3D' or None
if None, only one viewer is displayed
"""
window = StandaloneViewerMainWindow(title, viewer1_type, viewer2_type)
self.window = window
self.has_run = None
window.show()
def show(self):
"""
Shows the window
"""
if self.has_run is None:
self.has_run = self.app.exec_()
else:
print("No instance can be run interactively again. Delete and re-instantiate.")
def __del__(self):
"""destructor"""
self.app.exit()
def main():
# Run a standalone viewer with a 2D and a 3D viewer:
err = vtk.vtkFileOutputWindow()
err.SetFileName("viewer.log")
vtk.vtkOutputWindow.SetInstance(err)
standalone_viewer_instance = standalone_viewer("Standalone Viewer", viewer1_type="2D", viewer2_type="3D")
standalone_viewer_instance.show()
return 0
if __name__ == "__main__":
main()