class PlotMainWindow(QWidget): """Base class for plot main windows.""" def __init__(self, U, plot, length=1, title=None): super().__init__() layout = QVBoxLayout() if title: title = QLabel('<b>' + title + '</b>') title.setAlignment(Qt.AlignHCenter) layout.addWidget(title) layout.addWidget(plot) plot.set(U, 0) if length > 1: hlayout = QHBoxLayout() self.slider = QSlider(Qt.Horizontal) self.slider.setMinimum(0) self.slider.setMaximum(length - 1) self.slider.setTickPosition(QSlider.TicksBelow) hlayout.addWidget(self.slider) lcd = QLCDNumber(m.ceil(m.log10(length))) lcd.setDecMode() lcd.setSegmentStyle(QLCDNumber.Flat) hlayout.addWidget(lcd) layout.addLayout(hlayout) hlayout = QHBoxLayout() toolbar = QToolBar() self.a_play = QAction( self.style().standardIcon(QStyle.SP_MediaPlay), 'Play', self) self.a_play.setCheckable(True) self.a_rewind = QAction( self.style().standardIcon(QStyle.SP_MediaSeekBackward), 'Rewind', self) self.a_toend = QAction( self.style().standardIcon(QStyle.SP_MediaSeekForward), 'End', self) self.a_step_backward = QAction( self.style().standardIcon(QStyle.SP_MediaSkipBackward), 'Step Back', self) self.a_step_forward = QAction( self.style().standardIcon(QStyle.SP_MediaSkipForward), 'Step', self) self.a_loop = QAction( self.style().standardIcon(QStyle.SP_BrowserReload), 'Loop', self) self.a_loop.setCheckable(True) toolbar.addAction(self.a_play) toolbar.addAction(self.a_rewind) toolbar.addAction(self.a_toend) toolbar.addAction(self.a_step_backward) toolbar.addAction(self.a_step_forward) toolbar.addAction(self.a_loop) if hasattr(self, 'save'): self.a_save = QAction( self.style().standardIcon(QStyle.SP_DialogSaveButton), 'Save', self) toolbar.addAction(self.a_save) self.a_save.triggered.connect(self.save) hlayout.addWidget(toolbar) self.speed = QSlider(Qt.Horizontal) self.speed.setMinimum(0) self.speed.setMaximum(100) hlayout.addWidget(QLabel('Speed:')) hlayout.addWidget(self.speed) layout.addLayout(hlayout) self.timer = QTimer() self.timer.timeout.connect(self.update_solution) self.slider.valueChanged.connect(self.slider_changed) self.slider.valueChanged.connect(lcd.display) self.speed.valueChanged.connect(self.speed_changed) self.a_play.toggled.connect(self.toggle_play) self.a_rewind.triggered.connect(self.rewind) self.a_toend.triggered.connect(self.to_end) self.a_step_forward.triggered.connect(self.step_forward) self.a_step_backward.triggered.connect(self.step_backward) self.speed.setValue(50) elif hasattr(self, 'save'): hlayout = QHBoxLayout() toolbar = QToolBar() self.a_save = QAction( self.style().standardIcon(QStyle.SP_DialogSaveButton), 'Save', self) toolbar.addAction(self.a_save) hlayout.addWidget(toolbar) layout.addLayout(hlayout) self.a_save.triggered.connect(self.save) self.setLayout(layout) self.plot = plot self.U = U self.length = length def slider_changed(self, ind): self.plot.set(self.U, ind) def speed_changed(self, val): self.timer.setInterval(val * 20) def update_solution(self): ind = self.slider.value() + 1 if ind >= self.length: if self.a_loop.isChecked(): ind = 0 else: self.a_play.setChecked(False) return self.slider.setValue(ind) def toggle_play(self, checked): if checked: if self.slider.value() + 1 == self.length: self.slider.setValue(0) self.timer.start() else: self.timer.stop() def rewind(self): self.slider.setValue(0) def to_end(self): self.a_play.setChecked(False) self.slider.setValue(self.length - 1) def step_forward(self): self.a_play.setChecked(False) ind = self.slider.value() + 1 if ind == self.length and self.a_loop.isChecked(): ind = 0 if ind < self.length: self.slider.setValue(ind) def step_backward(self): self.a_play.setChecked(False) ind = self.slider.value() - 1 if ind == -1 and self.a_loop.isChecked(): ind = self.length - 1 if ind >= 0: self.slider.setValue(ind)
class PlotMainWindow(QWidget): """Base class for plot main windows.""" def __init__(self, U, plot, length=1, title=None): super().__init__() layout = QVBoxLayout() if title: title = QLabel('<b>' + title + '</b>') title.setAlignment(Qt.AlignHCenter) layout.addWidget(title) layout.addWidget(plot) plot.set(U, 0) if length > 1: hlayout = QHBoxLayout() self.slider = QSlider(Qt.Horizontal) self.slider.setMinimum(0) self.slider.setMaximum(length - 1) self.slider.setTickPosition(QSlider.TicksBelow) hlayout.addWidget(self.slider) lcd = QLCDNumber(m.ceil(m.log10(length))) lcd.setDecMode() lcd.setSegmentStyle(QLCDNumber.Flat) hlayout.addWidget(lcd) layout.addLayout(hlayout) hlayout = QHBoxLayout() toolbar = QToolBar() self.a_play = QAction(self.style().standardIcon(QStyle.SP_MediaPlay), 'Play', self) self.a_play.setCheckable(True) self.a_rewind = QAction(self.style().standardIcon(QStyle.SP_MediaSeekBackward), 'Rewind', self) self.a_toend = QAction(self.style().standardIcon(QStyle.SP_MediaSeekForward), 'End', self) self.a_step_backward = QAction(self.style().standardIcon(QStyle.SP_MediaSkipBackward), 'Step Back', self) self.a_step_forward = QAction(self.style().standardIcon(QStyle.SP_MediaSkipForward), 'Step', self) self.a_loop = QAction(self.style().standardIcon(QStyle.SP_BrowserReload), 'Loop', self) self.a_loop.setCheckable(True) toolbar.addAction(self.a_play) toolbar.addAction(self.a_rewind) toolbar.addAction(self.a_toend) toolbar.addAction(self.a_step_backward) toolbar.addAction(self.a_step_forward) toolbar.addAction(self.a_loop) if hasattr(self, 'save'): self.a_save = QAction(self.style().standardIcon(QStyle.SP_DialogSaveButton), 'Save', self) toolbar.addAction(self.a_save) self.a_save.triggered.connect(self.save) hlayout.addWidget(toolbar) self.speed = QSlider(Qt.Horizontal) self.speed.setMinimum(0) self.speed.setMaximum(100) hlayout.addWidget(QLabel('Speed:')) hlayout.addWidget(self.speed) layout.addLayout(hlayout) self.timer = QTimer() self.timer.timeout.connect(self.update_solution) self.slider.valueChanged.connect(self.slider_changed) self.slider.valueChanged.connect(lcd.display) self.speed.valueChanged.connect(self.speed_changed) self.a_play.toggled.connect(self.toggle_play) self.a_rewind.triggered.connect(self.rewind) self.a_toend.triggered.connect(self.to_end) self.a_step_forward.triggered.connect(self.step_forward) self.a_step_backward.triggered.connect(self.step_backward) self.speed.setValue(50) elif hasattr(self, 'save'): hlayout = QHBoxLayout() toolbar = QToolBar() self.a_save = QAction(self.style().standardIcon(QStyle.SP_DialogSaveButton), 'Save', self) toolbar.addAction(self.a_save) hlayout.addWidget(toolbar) layout.addLayout(hlayout) self.a_save.triggered.connect(self.save) self.setLayout(layout) self.plot = plot self.U = U self.length = length def slider_changed(self, ind): self.plot.set(self.U, ind) def speed_changed(self, val): self.timer.setInterval(val * 20) def update_solution(self): ind = self.slider.value() + 1 if ind >= self.length: if self.a_loop.isChecked(): ind = 0 else: self.a_play.setChecked(False) return self.slider.setValue(ind) def toggle_play(self, checked): if checked: if self.slider.value() + 1 == self.length: self.slider.setValue(0) self.timer.start() else: self.timer.stop() def rewind(self): self.slider.setValue(0) def to_end(self): self.a_play.setChecked(False) self.slider.setValue(self.length - 1) def step_forward(self): self.a_play.setChecked(False) ind = self.slider.value() + 1 if ind == self.length and self.a_loop.isChecked(): ind = 0 if ind < self.length: self.slider.setValue(ind) def step_backward(self): self.a_play.setChecked(False) ind = self.slider.value() - 1 if ind == -1 and self.a_loop.isChecked(): ind = self.length - 1 if ind >= 0: self.slider.setValue(ind)
class View(QFrame): graphicsView = None label = None label2 = None selectModeButton = None dragModeButton = None openGlButton = None antialiasButton = None resetButton = None zoomSlider = None rotateSlider = None def __init__(self, name, parent=None): super(View, self).__init__(parent) self.init_ui(name) def init_ui(self, name): self.setFrameStyle(QFrame.Sunken | QFrame.StyledPanel) self.graphicsView = GraphicsView(self) self.graphicsView.setRenderHint(QPainter.Antialiasing, False) self.graphicsView.setDragMode(QGraphicsView.RubberBandDrag) self.graphicsView.setOptimizationFlags(QGraphicsView.DontSavePainterState) self.graphicsView.setViewportUpdateMode(QGraphicsView.SmartViewportUpdate) self.graphicsView.setTransformationAnchor(QGraphicsView.AnchorUnderMouse) size = self.style().pixelMetric(QStyle.PM_ToolBarIconSize) iconSize = QSize(size, size) zoomInIcon = QToolButton() zoomInIcon.setAutoRepeat(True) zoomInIcon.setAutoRepeatInterval(33) zoomInIcon.setAutoRepeatDelay(0) zoomInIcon.setIcon(QIcon(":/zoomin.png")) zoomInIcon.setIconSize(iconSize) zoomOutIcon = QToolButton() zoomOutIcon.setAutoRepeat(True) zoomOutIcon.setAutoRepeatInterval(33) zoomOutIcon.setAutoRepeatDelay(0) zoomOutIcon.setIcon(QIcon(":/zoomout.png")) zoomOutIcon.setIconSize(iconSize) self.zoomSlider = QSlider() self.zoomSlider.setMinimum(0) self.zoomSlider.setMaximum(500) self.zoomSlider.setValue(250) self.zoomSlider.setTickPosition(QSlider.TicksRight) # Zoom slider layout zoomSliderLayout = QVBoxLayout() zoomSliderLayout.addWidget(zoomInIcon) zoomSliderLayout.addWidget(self.zoomSlider) zoomSliderLayout.addWidget(zoomOutIcon) rotateLeftIcon = QToolButton() rotateLeftIcon.setIcon(QIcon(":/rotateleft.png")) rotateLeftIcon.setIconSize(iconSize) rotateRightIcon = QToolButton() rotateRightIcon.setIcon(QIcon(":/rotateright.png")) rotateRightIcon.setIconSize(iconSize) self.rotateSlider = QSlider() self.rotateSlider.setOrientation(Qt.Horizontal) self.rotateSlider.setMinimum(-360) self.rotateSlider.setMaximum(360) self.rotateSlider.setValue(0) self.rotateSlider.setTickPosition(QSlider.TicksBelow) # Rotate slider layout rotateSliderLayout = QHBoxLayout() rotateSliderLayout.addWidget(rotateLeftIcon) rotateSliderLayout.addWidget(self.rotateSlider) rotateSliderLayout.addWidget(rotateRightIcon) self.resetButton = QToolButton() self.resetButton.setText("0") self.resetButton.setEnabled(False) # Label layout labelLayout = QHBoxLayout() self.label = QLabel(name) self.label2 = QLabel("Pointer Mode") self.selectModeButton = QToolButton() self.selectModeButton.setText("Select") self.selectModeButton.setCheckable(True) self.selectModeButton.setChecked(True) self.dragModeButton = QToolButton() self.dragModeButton.setText("Drag") self.dragModeButton.setCheckable(True) self.dragModeButton.setChecked(False) self.antialiasButton = QToolButton() self.antialiasButton.setText("Antialiasing") self.antialiasButton.setCheckable(True) self.antialiasButton.setChecked(False) self.openGlButton = QToolButton() self.openGlButton.setText("OpenGL") self.openGlButton.setCheckable(True) self.openGlButton.setEnabled(QGLFormat.hasOpenGL()) pointerModeGroup = QButtonGroup() pointerModeGroup.setExclusive(True) pointerModeGroup.addButton(self.selectModeButton) pointerModeGroup.addButton(self.dragModeButton) labelLayout.addWidget(self.label) labelLayout.addStretch() labelLayout.addWidget(self.label2) labelLayout.addWidget(self.selectModeButton) labelLayout.addWidget(self.dragModeButton) labelLayout.addStretch() labelLayout.addWidget(self.antialiasButton) labelLayout.addWidget(self.openGlButton) topLayout = QGridLayout() topLayout.addLayout(labelLayout, 0, 0) topLayout.addWidget(self.graphicsView, 1, 0) topLayout.addLayout(zoomSliderLayout, 1, 1) topLayout.addLayout(rotateSliderLayout, 2, 0) topLayout.addWidget(self.resetButton, 2, 1) self.setLayout(topLayout) self.resetButton.clicked.connect(self.resetView) self.zoomSlider.valueChanged.connect(self.setupTransform) self.rotateSlider.valueChanged.connect(self.setupTransform) self.graphicsView.verticalScrollBar().valueChanged.connect(self.setResetButtonEnabled) self.graphicsView.horizontalScrollBar().valueChanged.connect(self.setResetButtonEnabled) self.selectModeButton.toggled.connect(self.togglePointerMode) self.dragModeButton.toggled.connect(self.togglePointerMode) self.antialiasButton.toggled.connect(self.toggleAntialiasing) self.openGlButton.toggled.connect(self.toggleOpenGL) rotateLeftIcon.clicked.connect(self.rotateLeft) rotateRightIcon.clicked.connect(self.rotateRight) zoomInIcon.clicked.connect(self.zoomIn) zoomOutIcon.clicked.connect(self.zoomOut) self.setupTransform() def view(self): return self.graphicsView @Slot() def zoomIn(self): self.zoomSlider.setValue(self.zoomSlider.value() + 1) @Slot() def zoomOut(self): self.zoomSlider.setValue(self.zoomSlider.value() - 1) @Slot() def resetView(self): self.zoomSlider.setValue(250) self.rotateSlider.setValue(0) self.setupTransform() self.graphicsView.ensureVisible(QRectF(0, 0, 0, 0)) self.resetButton.setEnabled(False) @Slot() def setResetButtonEnabled(self): self.resetButton.setEnabled(True) @Slot() def setupTransform(self): scale = pow(2.0, (self.zoomSlider.value() - 250) / 50.0) trans = QTransform() trans.scale(scale, scale) trans.rotate(self.rotateSlider.value()) self.graphicsView.setTransform(trans) self.setResetButtonEnabled() @Slot() def togglePointerMode(self): self.graphicsView.setDragMode( QGraphicsView.RubberBandDrag if self.selectModeButton.isChecked() else QGraphicsView.ScrollHandDrag) self.graphicsView.setInteractive(self.selectModeButton.isChecked()) @Slot() def toggleOpenGL(self): self.graphicsView.setViewport( QGLWidget(QGLFormat(QGL.SampleBuffers)) if self.openGlButton.isChecked() else QWidget()) @Slot() def toggleAntialiasing(self): self.graphicsView.setRenderHint(QPainter.Antialiasing, self.antialiasButton.isChecked()) @Slot() def rotateLeft(self): self.rotateSlider.setValue(self.rotateSlider.value() - 10) @Slot() def rotateRight(self): self.rotateSlider.setValue(self.rotateSlider.value() + 10)