def _init_ui( self, empty_widget: QWidget, ) -> None: layout = QBoxLayout( QBoxLayout.LeftToRight, self, ) self._empty_widget = empty_widget self._tab_widget = self._create_tab_widget() self._auto_show_hide_tabs() layout.addWidget(self._empty_widget, Qt.AlignCenter) layout.addWidget(self._tab_widget) layout.setMargin(0)
class AspectRatioWidget(QWidget): def __init__(self, widget, side_widget, parent): super().__init__(parent) self.setParent(parent) self.setStyleSheet("background:#222; color:#fff; padding:0; margin:0") self.setLayout(QBoxLayout(QBoxLayout.LeftToRight, self)) self.main_widget = widget self.side_widget = side_widget self.centerWrapper = QWidget() self.centerWrapperLayout = QBoxLayout(QBoxLayout.LeftToRight) self.centerWrapperLayout.setMargin(0) self.centerWrapperLayout.setSpacing(0) self.centerWrapper.setLayout(self.centerWrapperLayout) self.centerWrapperLayout.addWidget(self.main_widget) self.centerWrapperLayout.addWidget(self.side_widget) self.layout().setMargin(0) # add spacer, then widget, then spacer self.layout().addItem(QSpacerItem(0, 0)) self.layout().addWidget(self.centerWrapper) # self.layout().addWidget(side_widget) self.layout().addItem(QSpacerItem(0, 0)) def resizeEvent(self, e): w = e.size().width() h = e.size().height() side_width = self.side_widget.width() if w - 100 > h: # too wide self.layout().setDirection(QBoxLayout.LeftToRight) widget_stretch = h + side_width outer_stretch = (w - h - side_width) / 2 else: # too tall self.layout().setDirection(QBoxLayout.TopToBottom) widget_stretch = w - side_width outer_stretch = (h - w + side_width) / 2 self.layout().setStretch(0, outer_stretch) self.layout().setStretch(1, widget_stretch) self.layout().setStretch(2, outer_stretch)
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.buffer = QBuffer() self.buffer.open(QBuffer.ReadWrite) self.setupUI() self.model = QModel(self) self.model.start() """ USER INTERFACE SETUP """ def setupUI(self): # WINDOW SETUP self.setWindowTitle("Keras.QuickDraw") self.setMinimumSize(QSize(800, 600)) self.setFixedSize(QSize(800, 600)) self.setWindowIcon(QIcon("favicon.ico")) # INITIALIZE: WINDOW CENTRAL self.widget_central = QWidget(self) self.widget_central.setObjectName("Window Central") self.widget_central.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.layout_central = QBoxLayout(QBoxLayout.TopToBottom, self.widget_central) self.setCentralWidget(self.widget_central) # INITIALIZE: CENTRAL HEADER self.widget_header = QWidget(self.widget_central) self.widget_header.setObjectName("Widget Header") self.widget_header.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.widget_header_indicater = QLabel(parent = self.widget_header) self.widget_header_caption = QLabel("(TARGET)", self.widget_header) self.widget_header_counter = QLabel(parent = self.widget_header) self.layout_header = QBoxLayout(QBoxLayout.LeftToRight, self.widget_header) self.layout_header.addWidget(self.widget_header_indicater, 0, Qt.AlignLeft) self.layout_header.addWidget(self.widget_header_caption , 1, Qt.AlignCenter) self.layout_header.addWidget(self.widget_header_counter , 0, Qt.AlignRight) # INITIALIZE: CENTRAL CANVAS self.widget_canvas = QCanvas(self.widget_central, self) self.widget_canvas.setObjectName("Widget Canvas") self.widget_canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) # INITIALIZE: CENTRAL BUTTONS self.widget_footer = QWidget(self.widget_central) self.widget_footer.setObjectName("Widget Footer") self.widget_footer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.widget_footer_clear = QPushButton("Clear", self.widget_footer) self.widget_footer_clear.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.widget_footer_clear.clicked.connect(self.widget_canvas.resetCanvas) self.widget_footer_undo = QPushButton("Undo", self.widget_footer) self.widget_footer_undo.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.widget_footer_undo.clicked.connect(self.widget_canvas.undoCanvas) self.layout_footer = QBoxLayout(QBoxLayout.LeftToRight, self.widget_footer) self.layout_footer.addWidget(self.widget_footer_undo, 0) self.layout_footer.addWidget(self.widget_footer_clear, 0) self.layout_footer.setMargin(0) # LAYOUT: HEADER + CANVAS + FOOTER -> CENTRAL WINDOW CENTRAL self.layout_central.addWidget(self.widget_header, 0) self.layout_central.addWidget(self.widget_canvas, 1, Qt.AlignCenter) self.layout_central.addWidget(self.widget_footer, 0) self.show() """ EVENT: SAVING CANVAS (QThread ALTERNATIVE) """ def paintEvent(self, event: QPaintEvent) -> None: # SCREENSHOT WIDGET OF QPaintDevice, SUCH AS QBITMAP self.widget_canvas.render(self.widget_canvas.pixmap()) if self.isVisible: self.buffer.reset() self.widget_canvas.pixmap().save(self.buffer, "BMP") return super().paintEvent(event) """ EVENT: CLOSING MAINWINDOW """ def closeEvent(self, event: QCloseEvent) -> None: self.model.terminate() self.model.wait() self.buffer.close() return super().closeEvent(event)