提交 b96aa215 编辑于 作者: ddlfighter's avatar ddlfighter
浏览文件

裁剪+保存画布完结

上级 32d72654
......@@ -6,7 +6,7 @@ import cg_algorithms as alg
import copy
from typing import Optional
from PyQt5.QtWidgets import (
QLabel,
QFileDialog,
QColorDialog,
QApplication,
QMainWindow,
......@@ -44,9 +44,10 @@ class MyCanvas(QGraphicsView):
self.temp_move = []
self.p_list = []
self.last_click_button = None
self.center_p = []
self.base_scale = 1
self.r = 0
self.center_p = [] # 缩放,旋转所需点
self.base_scale = 1 # 缩放倍数
self.r = 0 # 旋转角度
self.windows = [] # 窗口左上和右下坐标
def reset_canvas(self):
......@@ -55,6 +56,16 @@ class MyCanvas(QGraphicsView):
self.scene().clear()
self.status = ''
def save_canvas(self, filename, w, h):
painter = QPainter()
pixmap = QPixmap(w, h) # Size
pixmap.fill(QColor(255, 255, 255)) # 背景填充
painter.begin(pixmap)
for i in self.item_dict:
self.item_dict[i].paint(painter, QStyleOptionGraphicsItem)
painter.end()
pixmap.save(filename)
def start_draw_line(self, algorithm, item_id):
self.status = 'line'
self.temp_algorithm = algorithm
......@@ -101,9 +112,11 @@ class MyCanvas(QGraphicsView):
self.temp_id = self.selected_id
def start_clip(self, algorithm):
if self.selected_id == '':
if self.selected_id == '' or \
self.item_dict[self.selected_id].item_type != 'line':
self.status = ''
return
self.status = 'clip'
self.temp_id = self.selected_id
self.temp_algorithm = algorithm
......@@ -203,9 +216,6 @@ class MyCanvas(QGraphicsView):
'''
Version 1.0: 直接判断press点和release点之间的dx,dy,进行平移操作
'''
if self.temp_id == '':
print('未选择图元')
return
self.temp_item = self.item_dict[self.temp_id]
if event.button() == Qt.LeftButton:
self.last_click_button = Qt.LeftButton
......@@ -215,9 +225,6 @@ class MyCanvas(QGraphicsView):
'''
Version 1.0: 鼠标左键确定点,滑轮确定缩放倍数;必须先确定点,再进行缩放
'''
if self.temp_id == '':
print('未选择图元')
return
self.temp_item = self.item_dict[self.temp_id]
if event.button() == Qt.LeftButton:
self.p_list = copy.deepcopy(self.temp_item.p_list)
......@@ -227,8 +234,11 @@ class MyCanvas(QGraphicsView):
self.last_click_button = Qt.RightButton
elif self.status == 'clip':
pass
self.temp_item = self.item_dict[self.temp_id]
if event.button() == Qt.LeftButton:
self.p_list = copy.deepcopy(self.temp_item.p_list)
self.last_click_button = Qt.LeftButton
self.windows = [[x, y], [x, y]]
self.updateScene([self.sceneRect()])
super().mousePressEvent(event)
......@@ -251,14 +261,19 @@ class MyCanvas(QGraphicsView):
self.temp_item.p_list[len(self.temp_item.p_list) - 1] = [x, y]
elif self.status == 'translate':
if self.last_click_button == Qt.LeftButton:
self.temp_item = self.item_dict[self.temp_id]
# self.temp_item = self.item_dict[self.temp_id]
self.temp_move[1] = [x, y]
dx = self.temp_move[1][0] - self.temp_move[0][0]
dy = self.temp_move[1][1] - self.temp_move[0][1]
self.p_list = copy.deepcopy(self.temp_item.p_list)
self.temp_item.p_list = alg.translate(self.p_list, dx, dy)
self.temp_move[0] = [x, y]
print(self.temp_item.p_list)
elif self.status == 'clip':
if self.last_click_button == Qt.LeftButton:
self.windows[1] = [x, y]
p0, p1 = self.windows[0], self.windows[1]
temp_list = copy.deepcopy(self.p_list)
self.temp_item.p_list = alg.clip(temp_list, p0[0], p0[1], p1[0], p1[1], self.temp_algorithm)
self.updateScene([self.sceneRect()])
super().mouseMoveEvent(event)
......@@ -303,6 +318,14 @@ class MyCanvas(QGraphicsView):
self.temp_item = None
self.last_click_button = None
self.selected_id = ''
elif self.status == 'clip':
if self.last_click_button == Qt.LeftButton:
self.finish_draw()
self.windows = []
self.p_list = []
self.temp_item = None
self.last_click_button = None
self.selected_id = ''
super().mouseReleaseEvent(event)
......@@ -431,6 +454,7 @@ class MainWindow(QMainWindow):
file_menu = menubar.addMenu('文件')
set_pen_act = file_menu.addAction('设置画笔')
reset_canvas_act = file_menu.addAction('重置画布')
save_canvas_act = file_menu.addAction('保存画布')
exit_act = file_menu.addAction('退出')
draw_menu = menubar.addMenu('绘制')
line_menu = draw_menu.addMenu('线段')
......@@ -458,6 +482,7 @@ class MainWindow(QMainWindow):
set_pen_act.triggered.connect(self.set_pen_action)
# Canvas_signal
reset_canvas_act.triggered.connect(self.reset_canvas_action)
save_canvas_act.triggered.connect(self.save_canvas_action)
# Line_signal
line_naive_act.triggered.connect(self.line_naive_action)
line_dda_act.triggered.connect(self.line_dda_action)
......@@ -515,6 +540,16 @@ class MainWindow(QMainWindow):
self.canvas_widget.reset_canvas()
self.statusBar().showMessage("重置画布")
def save_canvas_action(self):
self.statusBar().showMessage("保存画布")
dialog = QFileDialog()
file_name = dialog.getSaveFileName(filter="Image Files(*.jpg *.png *.bmp)")
if file_name[0]:
self.canvas_widget.save_canvas(file_name[0], 600, 600)
# Line Action
def line_naive_action(self):
self.canvas_widget.start_draw_line('Naive', self.get_id())
......@@ -572,7 +607,7 @@ class MainWindow(QMainWindow):
self.statusBar().showMessage('平移')
self.list_widget.clearSelection()
self.canvas_widget.clear_selection()
print(self.canvas_widget.selected_id)
def rotate_action(self):
self.canvas_widget.start_rotate()
......@@ -592,11 +627,9 @@ class MainWindow(QMainWindow):
self.list_widget.clearSelection()
self.canvas_widget.clear_selection()
def clip_liang_barsky_action(self):
self.canvas_widget.start_clip('Cohen-Sutherland')
self.statusBar().showMessage('Cohen-Sutherland算法裁剪')
self.canvas_widget.start_clip('Liang-Barsky')
self.statusBar().showMessage('Liang-Barsky算法裁剪')
self.list_widget.clearSelection()
self.canvas_widget.clear_selection()
......
支持 Markdown
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册