class HighScoreDisplayer(QGraphicsObject): def __init__(self, parent=None): super().__init__(parent=parent) self.size = 25 self.icon_size = 25 self.board_size = 250 self.box_pen = QPen() self.box_pen.setColor(Qt.white) self.pen_width = 3 self.box_pen.setWidth(self.pen_width) self.widget_proxy = QGraphicsProxyWidget(parent=self) self.scoreboard_widget = scb.HighScoreBoard(self.board_size, self.board_size) self.widget_proxy.setWidget(self.scoreboard_widget) self.widget_proxy.setPos(-self.board_size, -self.board_size) self.scoreboard_widget.setVisible(False) self.setAcceptHoverEvents(True) self.selected = False def set_disabled(self, state): self.setAcceptHoverEvents(not state) def show_board(self, state): self.scoreboard_widget.setVisible(state) self.scoreboard_widget.show_scores(state) self.prepareGeometryChange() if state: self.size = self.board_size else: self.size = self.icon_size def boundingRect(self): return QRectF(-self.size, -self.size, self.size, self.size) def paint(self, painter, style, widget=None): painter.setPen(self.box_pen) painter.drawRect(self.boundingRect()) if not self.selected: painter.fillRect(-self.icon_size / 4, -self.icon_size / 4, -self.icon_size / 2, -self.icon_size / 2, Qt.white) def hoverEnterEvent(self, ev): self.show_board(True) def hoverLeaveEvent(self, ev): self.show_board(False)
def set_digits(self): """ Draw side number tiles """ for i in range(0, 12): if i <= self.board_size - 1: xpos = Cons.RACK_XTILE[0] ypos = Cons.RACK_YTILE + i * 55 + (12 - self.board_size) * 55 // 2 box = QLabel() box.setGeometry(xpos, ypos, Cons.TILE_WIDTH, Cons.TILE_HEIGHT) box.setStyleSheet("border: 2px solid black") box.setAlignment(Qt.AlignCenter) proxy = QGraphicsProxyWidget() proxy.persistent = False proxy.setWidget(box) proxy.digit = Cons.DIGITS[i] proxy.setPos(xpos, ypos) self.scene.addItem(proxy) proxy.show() tile = Tile(self.digits[i], self.scene) tile.cell = "new" tile.draw_tile(QPoint(xpos, ypos))
def __init__(self, aProductionProcess, aToolkitTypes): super().__init__() self.process = aProductionProcess self.col = 0 self.row = 0 self.inputs = [] self.outputs = [] icon = QGraphicsPixmapItem(aToolkitTypes.GetTypePixmap(self.process.scheme.schemeId, 32), self) icon.setPos(2, 2) width = 250 space = 40 inputOffset = 40 outputOffset = 40 for inp in self.process.inputs: inputOffset = inputOffset + space self.inputs.append(InputGraphic(inp, self, QPointF(0, inputOffset), aToolkitTypes)) for out in self.process.outputs: outputOffset = outputOffset + space self.outputs.append(OutputGraphic(out, self, QPointF(width, outputOffset), aToolkitTypes)) self.rect = QRectF(0, 0, width, max(outputOffset, inputOffset) + space) spinbox = QSpinBox() spinbox.setRange(0, 1000000) spinbox.setValue(self.process.runs) spinbox.valueChanged.connect(self.OnRunChanged) proxy = QGraphicsProxyWidget(self) proxy.setWidget(spinbox) proxy.setPos(QPointF(width / 2 - spinbox.width() / 2, 40)) self.runs = proxy self.spinbox = spinbox
class ServiceSticker(QGraphicsItem): def __init__(self, name, login, pos, color, password, mainWindow, id): super().__init__() self._width = 270.0 self._height = 157.0 self._color = color self._pos = pos self._name = name self._login = login self._password = password self._mainWindow = mainWindow self._id = id self.widget = ServiceCardWidget(name, login, self._color, parent=self) self.proxyWidget = QGraphicsProxyWidget(self) self.proxyWidget.setWidget(self.widget) self.anim = QPropertyAnimation(self.proxyWidget, b'geometry') ''' Method returns rectangle containing a position of the card on graphics scene. ''' def boundingRect(self): # getting rect of graphics view on main window viewRect = QRectF(self.mainWindow().graphicsView.geometry()) min_x_padding = 20.0 # determining how much cards can it be in a row items_per_line, remainder = self.remainder_div( viewRect.width(), self._width + min_x_padding) x_padding = remainder / (items_per_line - 1) y_padding = 40.0 # number of row y_counter = 0 # item's position from top top left corner to bottom right corner # from left to right line_pos = self._pos + 1 while True: if items_per_line - line_pos >= 0: x_counter = line_pos - 1 break else: line_pos -= items_per_line y_counter += 1 return QRectF(x_counter * (self._width + x_padding + min_x_padding), y_counter * (y_padding + self._height), self._width, self._height) ''' This method sets position of a proxy widget ''' def paint(self, painter, option, widget): self.proxyWidget.setPos(self.boundingRect().x(), self.boundingRect().y()) def data(self): return { 'name': self._name, 'id': self._id, 'pos': self._pos, 'login': self._login, 'color': self._color, 'password': self._password } def mainWindow(self): return self._mainWindow def getWidget(self): return self.widget @staticmethod def remainder_div(a, b): return (math.floor(a / b), a % b)
class SelectAreaDialog(QDialog): finish_selecting_area = pyqtSignal(TrimmingData) def __init__(self): super().__init__() self.ui = Ui_SelectAreaDialog() self.ui.setupUi(self) self.width = 200 self.height = 200 self.h, self.w = None, None self.select_area = None self.original_image_scene = None self.size_flag = True self.select_area_label = None self.select_area_label_proxy = None self.start_position = None self.get_ng_sample_image_path() if self.h <= self.height and self.w <= self.width: self.size_flag = False if self.size_flag: self.show_select_area_at_default_position() else: self.ui.notation_label.setText('この画像サイズは十分小さいため, 画像全体でトレーニングを行います.' '\nこのままトレーニング開始ボタンを押してください.') pass self.ui.ok_button.clicked.connect(self.on_clicked_ok_button) self.ui.cancel_button.clicked.connect(self.on_clicked_cancel_button) def get_ng_sample_image_path(self): test_ng_path = str(Dataset.images_path(Dataset.Category.TEST_NG)) test_ng_images = os.listdir(test_ng_path) test_ng_images = [ img for img in test_ng_images if Path(img).suffix in ['.jpg', '.jpeg', '.png', '.gif', '.bmp'] ] if not test_ng_images: return original_image_path = os.path.join(test_ng_path, test_ng_images[0]) original_image = cv2.imread(original_image_path) h, w, c = original_image.shape self.h, self.w = h, w original_image_shape = QSize(w + 2, h + 10) original_image_item = QGraphicsPixmapItem(QPixmap(original_image_path)) original_image_item.setZValue(0) self.original_image_scene = QGraphicsScene() self.original_image_scene.addItem(original_image_item) self.ui.original_image_view.setScene(self.original_image_scene) self.ui.original_image_view.setBaseSize(original_image_shape) self.ui.original_image_view.setMaximumSize(original_image_shape) self.resize(self.w + 32, self.h + 72) def show_select_area_at_default_position(self): trimming_data = Project.latest_trimming_data() if trimming_data.position: self.start_position = QPoint(trimming_data.position[0], trimming_data.position[1]) rect = QRectF(trimming_data.position[0], trimming_data.position[1], self.width, self.height) else: self.start_position = QPoint((self.w - self.width) // 2, (self.h - self.height) // 2) rect = QRectF((self.w - self.width) // 2, (self.h - self.height) // 2, self.width, self.height) self.select_area = QGraphicsRectItem(rect) self.select_area.setZValue(1) pen = QPen(QColor('#ffa00e')) pen.setWidth(4) pen.setJoinStyle(Qt.RoundJoin) self.select_area.setPen(pen) self.select_area.setFlag(QGraphicsItem.ItemIsMovable, True) self.original_image_scene.addItem(self.select_area) self.select_area_label_proxy = QGraphicsProxyWidget(self.select_area) self.select_area_label = SelectAreaLabel() self.select_area_label.set_label() self.select_area_label_proxy.setWidget(self.select_area_label) self.select_area_label_proxy.setPos( self.select_area.boundingRect().left() + 2, self.select_area.boundingRect().bottom() - self.select_area_label.height() - 2) def on_clicked_ok_button(self): if not self.size_flag: trimming_data = TrimmingData(position=(0, 0), size=(self.w, self.h), needs_trimming=False) self.finish_selecting_area.emit(trimming_data) self.close() else: rel_position = self.select_area.pos() position = (self.start_position.x() + rel_position.x(), self.start_position.y() + rel_position.y()) if position[0] < 0 or position[ 0] > self.w - self.width - 1 or position[ 1] < 0 or position[1] > self.h - self.height - 1: print('Error: Please set area contained in the image.') self.ui.notation_label.setText('エラー: 切り取る領域は画像内に収まるようにしてください.') else: trimming_data = TrimmingData(position=position, size=(self.width, self.height), needs_trimming=True) self.finish_selecting_area.emit(trimming_data) self.close() def on_clicked_cancel_button(self): self.close() def closeEvent(self, QCloseEvent): self.close()