def _get_monitor_size(monitor_index): """Utility function to automatically detect the SLM panel size :param monitor_index: int. Monitor number :return: tuple of two integers, width and height in pixels """ app = get_qt_app() desktop = app.desktop() slm_screen = desktop.screen(monitor_index) return [slm_screen.width(), slm_screen.height()]
def set_image(self, phase, slm_monitor=None): """Sets an array on the QLabel.Pixmap :param phase: np.array from 0 to 2 pi :param slm_monitor: int. Optional. If given, it will move the SLM widget to the specified monitor :return: """ phase = self.LUT(phase) img = phase.ravel() img_slm = np.dstack((img, img, img, img)).astype(np.uint8) self._QImage = QtGui.QImage(img_slm, phase.shape[1], phase.shape[0], QtGui.QImage.Format_RGB32) self._QLabel.setPixmap(QtGui.QPixmap(self._QImage)) if slm_monitor is not None: app = get_qt_app() desktop = app.desktop() slm_screen = desktop.screen(slm_monitor) assert isinstance(slm_monitor, int) assert desktop.screenCount() > slm_monitor >= 0 self.move(slm_screen.x(), slm_screen.y())
#i = self.axis_names.index(axis) #if relative: # self._position[i] += position #else: # self._position[i] = position # print "stage now at", self._position #def move_rel(self, position, axis=None): # self.move(position, relative=True) def get_position(self, axis=None): return self.get_axis_param(lambda axis: self._position[self.axis_names.index(axis)], axis) position = property(get_position) if __name__ == '__main__': import sys from nplab.utils.gui import get_qt_app stage = DummyStage() print stage.move(2e-6, axis=('x1', 'x2')) print stage.get_position() print stage.get_position('x1') print stage.get_position(['x1', 'y1']) app = get_qt_app() ui = stage.get_qt_ui() ui.show() sys.exit(app.exec_())
def show_gui(self, blocking=None, block=None, force_new_window=False): """Display a GUI window for the class. You may override this method to display a window to control the object. However, it's better (and less work) to provide a method `get_qt_ui()`. This shoudl return a QWidget subclass that is the GUI for the object. This method will take care of ensuring there's a Qt application object and displaying the GUI. If you are using traitsui, then edit_traits/configure_traits methods exist, and this method will simply pop up a traits window for the object. If you use blocking=False, it will return immediately - this allows you to continue using the console, assuming there's already a Qt application running (usually the case if you're running from Spyder). NB you may want to retain the return value if using this mode, as otherwise the GUI may be garbage-collected and disappear. For compatibility, this function accepts either ``block`` or ``blocking`` as a keyword argument - if either is not None it will use that value, otherwise it defaults to ``True``. In the future, blocking=False may spawn a Qt application object in a background thread - but that's not currently done so we rely on a Qt application running already (e.g. via the "input hook"). When using Qt, we default to only creating one UI, and return a handle to it each time this is called. If ``force_new_window`` is set to `True`, a new widget will be created regardless. This may cause issues if the retained reference to the GUI in the object is the only one existing - the previous window may disappear. """ if blocking is None and block is not None: blocking = block # Allow the use of either argument name if blocking is None: blocking = True # We default to True. if hasattr(self, 'get_qt_ui'): # NB this dynamic import is important to avoid saddling all of # nplab with dependencies on Qt. from nplab.utils.gui import QtCore, QtGui, QtWidgets, get_qt_app app = get_qt_app() if force_new_window or not isinstance(self.__gui_instance, QtWidgets.QWidget): # create the widget if it doesn't exist already, or if we've been # told to make a new one self.__gui_instance = self.get_qt_ui() ui = self.__gui_instance ui.show() ui.activateWindow() #flash the taskbar entry to make it obvious if blocking: print( "Running GUI, this will block the command line until the window is closed." ) ui.windowModality = QtCore.Qt.ApplicationModal try: return app.exec_() except: print( "Could not run the Qt application: perhaps it is already running?" ) return else: return ui else: try: if blocking: self.configure_traits() else: self.edit_traits() except AttributeError: raise NotImplementedError("It looks like the show_gui \ method hasn't been subclassed, there isn't a \ get_qt_ui() method, and the instrument is not \ using traitsui.")