def __calculate_grid_lines(self, grid_rect: "QRect") -> Tuple[List, List]: """Calculates the coordinates of the horizontal/vertical lines to be drawn. Args: grid_rect: Rectangle representing the grid boundaries. Returns: Lists of light lines and dark lines to draw. """ light_lines = [] dark_lines = [] # Calculate horizontal lines for x in range(grid_rect.left(), grid_rect.right(), self.grid_size): if x % (self.grid_size * self.grid_squares) != 0: light_lines.append( QLine(x, grid_rect.top(), x, grid_rect.bottom())) else: dark_lines.append( QLine(x, grid_rect.top(), x, grid_rect.bottom())) # Calculate vertical lines for y in range(grid_rect.top(), grid_rect.bottom(), self.grid_size): if y % (self.grid_size * self.grid_squares) != 0: light_lines.append( QLine(grid_rect.left(), y, grid_rect.right(), y)) else: dark_lines.append( QLine(grid_rect.left(), y, grid_rect.right(), y)) return light_lines, dark_lines
def _build_guidelines(self): self.guidelines = list() for element in self.elements: for p in element.guideline_points(): self.guidelines.append( QLine(0, p.y(), self.rect().width(), p.y())) self.guidelines.append( QLine(p.x(), 0, p.x(), self.rect().height()))
def getLines(cls, self, x1, y1, x2, y2): r = [] if self.border.left: r.append(QLine(x1, y1, x1, y2)) if self.border.top: r.append(QLine(x1, y1, x2, y1)) if self.border.right: r.append(QLine(x2, y1, x2, y2)) if self.border.bottom: r.append(QLine(x1, y2, x2, y2)) return r
def _build_guidelines(self): self.guidelines = list() for element in self.elements: for pin in element.pins(): p = pin.position + element.bounding_box.topLeft() if pin.direction.y() == 0: if pin.direction.x() > 0: self.guidelines.append( QLine(p.x(), p.y(), self.rect().width(), p.y())) else: self.guidelines.append(QLine(p.x(), p.y(), 0, p.y())) self.guidelines.append( QLine(p.x(), 0, p.x(), self.rect().height())) else: if pin.direction.y() > 0: self.guidelines.append( QLine(p.x(), p.y(), p.x(), self.rect().height())) else: self.guidelines.append(QLine(p.x(), p.y(), p.x(), 0)) self.guidelines.append( QLine(0, p.y(), self.rect().width(), p.y())) for wire in self.wires: for p in (wire.p1(), wire.p2()): self.guidelines.append( QLine(0, p.y(), self.rect().width(), p.y())) self.guidelines.append( QLine(p.x(), 0, p.x(), self.rect().height()))
def main(argv): app = QApplication(argv) scene = QGraphicsScene(0, 0, 500, 500) scene.setItemIndexMethod(QGraphicsScene.NoIndex) brush = QBrush(Qt.red) brush_brics = QBrush(QPixmap('brick_texture.jpg')) brush_brics.setStyle(Qt.TexturePattern) pen = QPen() pen.setWidth(5) pen.setStyle(Qt.DashLine) rectItem = RectItem('r1', 20, 10, 200, 100) rectItem.setBrush(brush) rectItem2 = RectItem('r2', 20, 10, 150, 250) rectItem2.setPen(pen) rectItem2.setBrush(brush_brics) rectItem2.setOpacity(70) arrowItem = ArrowItem(QLine(50, 200, 300, 400)) scene.addItem(arrowItem) scene.addItem(rectItem) scene.addItem(rectItem2) view = QGraphicsView(scene) view.show() return app.exec_()
def __init__(self, ui_file, parent=None): self.remaining_time = 10 super(Form, self).__init__(parent) ui_file = QFile(ui_file) ui_file.open(QFile.ReadOnly) loader = QUiLoader() self.window = loader.load(ui_file) ui_file.close() self.btn_clear = self.window.findChild(QPushButton, "btn_clear") self.btn_skip = self.window.findChild(QPushButton, "btn_skip") self.btn_undo = self.window.findChild(QPushButton, "btn_undo") self.label_timer = self.window.findChild(QLabel, "label_timer") self.log = self.window.findChild(QPlainTextEdit, "log") self.picture_holder = self.window.findChild(QLabel, "picture_holder") self.gen_line = QLine() self.timer = QTimer(self) #self.timer.timeout.connect(self.stop_game) self.timer.start(1000) #QTimer.singleShot(1000, self.redraw_label_timer) QObject.connect(self.timer, SIGNAL('timeout()'), self.redraw_label_timer) self.window.show()
def testDrawOverloads(self): '''Calls QPainter.drawLines overloads, if something is wrong Exception and chaos ensues. Bug #395''' self.painter.drawLines([QLine(QPoint(0,0), QPoint(1,1))]) self.painter.drawLines([QPoint(0,0), QPoint(1,1)]) self.painter.drawLines([QPointF(0,0), QPointF(1,1)]) self.painter.drawLines([QLineF(QPointF(0,0), QPointF(1,1))]) self.painter.drawPoints([QPoint(0,0), QPoint(1,1)]) self.painter.drawPoints([QPointF(0,0), QPointF(1,1)]) self.painter.drawConvexPolygon([QPointF(10.0, 80.0), QPointF(20.0, 10.0), QPointF(80.0, 30.0), QPointF(90.0, 70.0)]) self.painter.drawConvexPolygon([QPoint(10.0, 80.0), QPoint(20.0, 10.0), QPoint(80.0, 30.0), QPoint(90.0, 70.0)]) self.painter.drawPolygon([QPointF(10.0, 80.0), QPointF(20.0, 10.0), QPointF(80.0, 30.0), QPointF(90.0, 70.0)]) self.painter.drawPolygon([QPoint(10.0, 80.0), QPoint(20.0, 10.0), QPoint(80.0, 30.0), QPoint(90.0, 70.0)]) self.painter.drawPolyline([QPointF(10.0, 80.0), QPointF(20.0, 10.0), QPointF(80.0, 30.0), QPointF(90.0, 70.0)]) self.painter.drawPolyline([QPoint(10.0, 80.0), QPoint(20.0, 10.0), QPoint(80.0, 30.0), QPoint(90.0, 70.0)])
def paintEvent(self, *args): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setBrush(Qt.black) painter.setPen(QPen(Qt.transparent)) for block in self.blocks: painter.drawRect(block) if self.debug: lines = list() points = list() for p1 in self.graph: for p2 in self.graph[p1]: lines.append(QLine(*p1, *p2)) points.append(QPoint(*p1)) points.append(QPoint(*p2)) painter.setPen(QPen(Qt.black)) painter.drawLines(lines) painter.setPen(QPen(Qt.red, 3)) painter.drawPoints(points) if self.path: painter.setPen(QPen(Qt.green)) painter.setBrush(Qt.transparent) p = QPainterPath() p.moveTo(*self.path[0]) for point in self.path: p.lineTo(*point) painter.drawPath(p)
def paint_single_k_line(self, painter, x, y, width, height, info): delta = info.high - info.low if info.high - info.low else -1 pen = QPen() color = QColor() if info.close > info.open or (info.close >= info.open and info.pct_chg > 0): color.setRgb(217, 58, 24) else: color.setRgb(7, 122, 50) pen.setColor(color) painter.setPen(pen) # 计算并绘制上影线部分 up_delta = info.high - (info.open if info.open > info.close else info.close) up_pct = up_delta / delta up_start_x = x + width / 2 up_start_y = y up_end_y = y + int(height * up_pct) up_line = QLine(up_start_x, up_start_y, up_start_x, up_end_y) painter.drawLine(up_line) # 计算并绘制实体部分 main_delta = info.open - info.close if info.open > info.close else info.close - info.open main_pct = main_delta / delta main_start_y = up_end_y main_height = int(main_pct * height) main_height = 1 if main_height == 0 else main_height # 确保主体部分有内容 main_rect = QRect(x + self.KLINE_PADDING, main_start_y, width - 2 * self.KLINE_PADDING, main_height) painter.drawRect(main_rect) if info.close < info.open: painter.fillRect(main_rect, color) else: # 此处填充一下背景色,以防分割窗口的横线影响K线的显示 fill_rect = QRect(x + self.KLINE_PADDING + 1, main_start_y + 1, width - 2 * self.KLINE_PADDING - 1, main_height - 1) painter.fillRect(fill_rect, PySide2.QtCore.Qt.white) # 计算并绘制下影线部分 down_start_y = main_start_y + main_height down_end_y = y + height down_line = QLine(up_start_x, down_start_y, up_start_x, down_end_y) painter.drawLine(down_line)
def draw(cls, x, y, self: PreparedLine, painter: QPainter): old_pen = painter.pen() pen = QPen(QColor(0)) pen.setWidth(self.size) pen.setStyle(Qt.PenStyle.SolidLine) painter.setPen(pen) tx = self.left + x ty = self.top + y painter.drawLine(QLine(tx, ty, tx + self.width, ty + self.height)) painter.setPen(old_pen)
def get_two_point(self, event, type): if self.cont == 1: self.line_xy.append(event.pos()) print(f'draw {self.line_xy}') if type == 'L': self.map_mem[type][len(self.map_mem[type].keys()) + 1] = {'C': Qt.black, 'W': 2, 'P': QLine(self.line_xy[0],self.line_xy[1])} elif type == 'R': self.map_mem[type][len(self.map_mem[type].keys()) + 1] = {'C': Qt.black, 'W': 2, 'P': QRect(self.line_xy[0], self.line_xy[1])} self.cont = 0 self.line_xy = [] else: self.line_xy.append(event.pos()) self.cont += 1
def draw(self, viewer, qpainter, alpha): """ Function to draw on a View. Args: viewer (Viewer): object which must is drawn on and which must be updated qpainter (QPainter): object which is used to draw alpha (float): opacity to draw """ qpainter.setOpacity(alpha) qpainter.setBrush(QColor(QtCore.Qt.red)) qpainter.setPen(QPen(QColor(QtCore.Qt.red), 2, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin)) points = [viewer.xy2screen(point.x, point.y) for point in self.points] pointsA = points[:-1] pointsB = points[1:] lines = [QLine(pointA[0], pointA[1], pointB[0], pointB[1]) for pointA, pointB in zip(pointsA, pointsB)] qpainter.drawLines(lines)
def mouseMoveEvent(self, e): if self.wiring_mode: self.closest_point = self._closest_assist_point(e.pos()) if self._wire_start is not None and self.closest_point is not None: self._ghost_wire = QLine(self._wire_start, self.closest_point) else: self._ghost_wire = None self.update() else: if self.grabbed_element is not None: self.grabbed_element.bounding_box.moveTopLeft(e.pos() + self.grab_offset) self.moved = True self.update() elif self.select_rect is not None: self.select_rect.setBottomRight(e.pos()) if self.select_rect.size() != QSize(0, 0): self.selected_elements = list() for element in self.elements: if self.select_rect.contains(element.bounding_box): self.selected_elements.append(element) self.update()
def paintEvent(self, event): painter = QPainter(self) if self.frame_nub == 1: painter.setPen(QPen(self.line_color['1'], 3)) painter.drawLine(QLine(37, 30, 37, 106)) painter.setPen(QPen(self.line_color['2'], 3)) painter.drawLine(QLine(37, 106, 37, 161)) painter.setPen(QPen(self.line_color['3'], 3)) painter.drawLine(QLine(37, 161, 37, 295)) painter.setPen(QPen(self.line_color['4'], 3)) painter.drawLine(QLine(37, 295, 37, 493)) elif self.frame_nub == 2: painter.setPen(QPen(self.line_color['1'], 3)) painter.drawLine(QLine(37, 30, 37, 106)) painter.setPen(QPen(self.line_color['2'], 3)) painter.drawLine(QLine(37, 106, 37, 161 + 280)) # 표가 그려지는 부분 ------------------------------------------------- painter.setPen(QPen(self.line_color['3'], 2)) table_counter = 0 for i in range(0, 200, 40): for j in range(0, 450, 150): rect = QRect(70 + j, 200 + i, 150, 40) # 특정 제한치 이상부터 경고 색으로 바뀜 ----------------------- if table_counter in [5, 8, 11, 14]: if table_counter == 5: if int(self.step_info['2'][2.5][table_counter]) >= 160: painter.fillRect(rect, QColor(250, 142, 145)) else: painter.fillRect(rect, QColor(234, 234, 234)) else: if int(self.step_info['2'][2.5][table_counter]) >= 50: painter.fillRect(rect, QColor(250, 142, 145)) else: painter.fillRect(rect, QColor(234, 234, 234)) else: painter.fillRect(rect, QColor(234, 234, 234)) # --------------------------------------------------------- painter.drawRect(rect) painter.drawText(rect, Qt.AlignCenter, self.step_info['2'][2.5][table_counter]) table_counter += 1 # ------------------------------------------------------------------ self.update()
def __init__(self, parent=None): super().__init__(parent) self.setMouseTracking(True) self.setFocusPolicy(Qt.StrongFocus) self.elements = list() self.pins = list() self.wires = list() self.junctions = list() self.guidepoints = list() self.guidelines = list() self.wiring_assistant = False self.selected_elements = list() self.moved = False self.grabbed_element = None self.grab_offset = None self.closest_point = None self.elements.append(NotElement(None)) self.wires.append(QLine(100, 100, 200, 100))
def mouseReleaseEvent(self, e): if self.wiring_mode: if e.button() == Qt.RightButton: self._wire_start = None self.update() elif self.closest_point is not None: if self._wire_start is None: self._wire_start = self.closest_point elif self.closest_point != self._wire_start: wire_end = self.closest_point self.wires.append(QLine(self._wire_start, wire_end)) self._wire_start = None self._build_guidelines() self.update() else: moved = self.moved if self.grabbed_element is not None: self.grabbed_element = None self.moved = False if not moved: self.selected_elements = list() if self.select_rect is not None and self.select_rect.size( ) != QSize(0, 0): for element in self.elements: if self.select_rect.contains(element.bounding_box): self.selected_elements.append(element) else: for element in self.elements: bb = element.bounding_box if bb.contains(e.pos()): self.selected_elements.append(element) break self.select_rect = None self.update()
def testQLineHash(self): l1 = QLine(12, 34, 56, 78) l2 = QLine(12, 34, 56, 78) self.assertFalse(l1 is l2) self.assertEqual(l1, l2) self.assertEqual(hash(l1), hash(l2))
def testQLineToTuple(self): l = QLine(1, 2, 3, 4) self.assertEqual((1, 2, 3, 4), l.toTuple())
def draw_grid(expected): expected.fillRect(0, 0, 360, 240, Connect4Display.background_colour) expected.drawLines([QLine(0, y, 280, y) for y in range(40, 240, 40)]) expected.drawLines([QLine(x, 0, x, 280) for x in range(40, 280, 40)])
def paint_k_line(self, painter, event): batch_k_line_info = self.model.get_current_stock_k_info() # 显示K线区域宽度 line_area_width = event.rect().width() - self.PRICE_AREA_WIDTH line_area_height = event.rect().height() real_each_width = self.each_line_width + self.KLINE_PADDING # 第一步:统计显示多少根K线 show_num = line_area_width // real_each_width # 第二步:统计显示的开始index以及最后index self.calculate_start_last_index(batch_k_line_info, show_num) # 第三步:统计一下最大和最小价差 max_price = 0 min_price = 10000000 for i in range(self.start_index, self.last_index + 1): if batch_k_line_info.info_list[i].high > max_price: max_price = batch_k_line_info.info_list[i].high if batch_k_line_info.info_list[i].low < min_price: min_price = batch_k_line_info.info_list[i].low min_max_delta = max_price - min_price # 第三点一步:绘制一下窗格系统,将窗格横向分割成10个小窗格 pen = QPen() color = QColor() color.setRgb(212, 208, 208) pen.setColor(color) painter.setPen(pen) each_win_height = line_area_height / 10 for i in range(1, 11): temp_line = QLine(0, each_win_height * i, line_area_width, each_win_height * i) painter.drawLine(temp_line) # 第四步:开始绘制单根的K线 x: int = 0 y: int = 0 height: int = 0 for i in range(self.start_index, self.last_index + 1): x = real_each_width * (i - self.start_index) y = (max_price - batch_k_line_info.info_list[i].high ) / min_max_delta * line_area_height height = (batch_k_line_info.info_list[i].high - batch_k_line_info.info_list[i].low) / min_max_delta * \ line_area_height self.paint_single_k_line(painter, x, y, self.each_line_width, height, batch_k_line_info.info_list[i]) # 第五步:最右侧绘制一个显示栏,用于显示价格(分成四个等份) price_font = QFont() price_font.setPointSize(self.FONT_SIZE) painter.setFont(price_font) right_end_line = QLine(line_area_width, 0, line_area_width, event.rect().height()) painter.drawLine(right_end_line) each_price_level_height = event.rect().height() / 4 for i in range(0, 4): cur_price = max_price - min_max_delta * (self.FONT_SIZE + each_price_level_height * i) / \ event.rect().height() painter.drawText(line_area_width + self.PRICE_MARGIN_LEFT, self.FONT_SIZE + each_price_level_height * i, str(cur_price)) # 第六步:绘制一下当前鼠标所在K线的位置,加一个十字线,贯穿整个窗口 k_line_num = self.curr_mouse_position.x() // real_each_width # 处理一下,避免垂直的线越过K线显示区,到了右边价格显示区 if k_line_num > (self.last_index - self.start_index): point_x = line_area_width else: point_x = k_line_num * real_each_width + real_each_width / 2 horizon_line = QLine(0, self.curr_mouse_position.y(), line_area_width, self.curr_mouse_position.y()) painter.drawLine(horizon_line) vertical_line = QLine(point_x, 0, point_x, event.rect().height()) painter.drawLine(vertical_line) # 绘制一下当前的价格 price_back_ground_height = self.FONT_SIZE + 4 mouse_on_price = max_price - min_max_delta * self.curr_mouse_position.y( ) / event.rect().height() mouse_on_price_back = QRect( line_area_width, self.curr_mouse_position.y() - price_back_ground_height + 2, self.PRICE_AREA_WIDTH, price_back_ground_height) painter.drawRect(mouse_on_price_back) price_fill_back = QRect( line_area_width + 1, self.curr_mouse_position.y() - price_back_ground_height + 3, self.PRICE_AREA_WIDTH - 2, price_back_ground_height - 2) painter.fillRect(price_fill_back, QColor(184, 243, 144)) painter.drawText(line_area_width + self.PRICE_MARGIN_LEFT, self.curr_mouse_position.y(), str(mouse_on_price))
def setUp(self): self.original = QLine(1, 2, 3, 4)