Dominik Demuth 067857eda2 minor fixes
2023-09-19 12:22:33 +02:00

159 lines
5.1 KiB
Python

from ..Qt import QtWidgets, QtCore
from .._py.guidelinewidget import Ui_Form
class DrawingsWidget(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.connected_figure=None
self.setupUi(self)
def __call__(self, graphs):
for gid, name in graphs:
self.graph_comboBox.addItem(name, userData=gid)
def clear(self):
self.graph_comboBox.clear()
@QtCore.pyqtSlot(int, name='on_mode_comboBox_currentIndexChanged')
def change_draw_type(self, idx: int):
self.stackedWidget.setCurrentIndex(idx)
"""
self.lines = {}
self.comments = {}
self.vh_pos_lineEdit.setValidator(QtGui.QDoubleValidator())
self.tableWidget.installEventFilter(self)
@QtCore.pyqtSlot(name='on_pushButton_clicked')
def make_line(self):
invalid = True
idx = self.mode_comboBox.currentIndex()
try:
pos = float(self.vh_pos_lineEdit.text())
# Vertical: idx=0; horizontal: idx = 1
angle = 90*abs(1-idx)
invalid = False
except ValueError:
pos = None
angle = None
pass
if invalid:
QtWidgets.QMessageBox().information(self, 'Invalid input', 'Input is not a valid number')
return
qcolor = QtGui.QColor.fromRgb(*self.color_comboBox.value.rgb())
comment = self.comment_lineEdit.text()
line = LogInfiniteLine(pos=pos, angle=angle, movable=self.drag_checkBox.isChecked(), pen=qcolor)
line.sigPositionChanged.connect(self.move_line)
self.make_table_row(pos, angle, qcolor, comment)
graph_id = self.graph_comboBox.currentData()
try:
self.lines[graph_id].append(line)
self.comments[graph_id].append(comment)
except KeyError:
self.lines[graph_id] = [line]
self.comments[graph_id] = [comment]
self.line_created.emit(line, graph_id)
def set_graphs(self, graphs: list):
for graph_id, name in graphs:
self.graph_comboBox.addItem(name, userData=graph_id)
def remove_graph(self, graph_id: str):
idx = self.graph_comboBox.findData(graph_id)
if idx != -1:
self.graph_comboBox.removeItem(idx)
if graph_id in self.lines:
del self.lines[graph_id]
@QtCore.pyqtSlot(int, name='on_graph_comboBox_currentIndexChanged')
def change_graph(self, idx: int):
self.tableWidget.clear()
self.tableWidget.setRowCount(0)
graph_id = self.graph_comboBox.itemData(idx)
if graph_id in self.lines:
lines = self.lines[graph_id]
comments = self.comments[graph_id]
for i, line in enumerate(lines):
self.make_table_row(line.pos(), line.angle, line.pen.color(), comments[i])
def make_table_row(self, position, angle, color, comment):
if angle == 0:
try:
pos_label = 'x = ' + str(position.y())
except AttributeError:
pos_label = 'x = {position}'
elif angle == 90:
try:
pos_label = f'y = {position.x()}'
except AttributeError:
pos_label = f'y = {position}'
else:
raise ValueError('Only horizontal or vertical lines are supported')
item = QtWidgets.QTableWidgetItem(pos_label)
item.setFlags(QtCore.Qt.ItemIsSelectable)
item.setForeground(QtGui.QBrush(QtGui.QColor('black')))
row_count = self.tableWidget.rowCount()
self.tableWidget.setRowCount(row_count+1)
self.tableWidget.setItem(row_count, 0, item)
item2 = QtWidgets.QTableWidgetItem(comment)
self.tableWidget.setItem(row_count, 1, item2)
colitem = QtWidgets.QTableWidgetItem(' ')
colitem.setBackground(QtGui.QBrush(color))
colitem.setFlags(QtCore.Qt.ItemIsSelectable)
self.tableWidget.setVerticalHeaderItem(row_count, colitem)
def eventFilter(self, src: QtCore.QObject, evt: QtCore.QEvent) -> bool:
if evt.type() == QtCore.QEvent.KeyPress:
if evt.key() == QtCore.Qt.Key_Delete:
self.delete_line()
return True
return super().eventFilter(src, evt)
def delete_line(self):
remove_rows = sorted([item.row() for item in self.tableWidget.selectedItems()])
graph_id = self.graph_comboBox.currentData()
current_lines = self.lines[graph_id]
for i in reversed(remove_rows):
self.tableWidget.removeRow(i)
self.line_deleted.emit(current_lines[i], graph_id)
current_lines.pop(i)
self.comments[graph_id].pop(i)
@QtCore.pyqtSlot(object)
def move_line(self, line: InfiniteLine):
current_idx = self.graph_comboBox.currentData()
graphs = self.lines[current_idx]
i = -1
for i, line_i in enumerate(graphs):
if line == line_i:
break
pos = line.value()
text_item = self.tableWidget.item(i, 0)
text_item.setText(text_item.text()[:4]+f'{pos:.4g}')
"""