Skip to content

Commit

Permalink
Update Qt API and add a clipboard option for copied curve.
Browse files Browse the repository at this point in the history
  • Loading branch information
KmolYuan committed Oct 12, 2021
1 parent c55d61e commit 255f6d5
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 121 deletions.
8 changes: 4 additions & 4 deletions pyslvs_ui/graphics/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from enum import auto, unique, IntEnum
from math import radians, sin, cos, atan2, hypot, isnan
from functools import reduce
from qtpy.QtCore import Slot, Qt, QPointF, QRectF, QSizeF, Signal
from qtpy.QtCore import Slot, Qt, QPointF, QRectF, QSizeF, Signal, QLineF
from qtpy.QtWidgets import QWidget, QSizePolicy
from qtpy.QtGui import (
QPolygonF, QPainter, QBrush, QPen, QColor, QFont,
Expand Down Expand Up @@ -217,10 +217,10 @@ def paintEvent(self, event: QPaintEvent) -> None:
self.painter.setPen(pen)
x_l = -self.ox
x_r = self.width() - self.ox
self.painter.drawLine(x_l, 0, x_r, 0)
self.painter.drawLine(QLineF(x_l, 0, x_r, 0))
y_t = self.height() - self.oy
y_b = -self.oy
self.painter.drawLine(0, y_b, 0, y_t)
self.painter.drawLine(QLineF(0, y_b, 0, y_t))

def indexing(v: float) -> int:
"""Draw tick."""
Expand Down Expand Up @@ -378,7 +378,7 @@ def draw_arrow(
y2 = (y1 + y2) / 2 - 7.5 * sin(a)
first_point = QPointF(x2, -y2)
if line:
self.painter.drawLine(x1, -y1, x2, -y2)
self.painter.drawLine(QLineF(x1, -y1, x2, -y2))
self.painter.drawLine(first_point, QPointF(
x2 + 15 * cos(a + radians(20)),
-y2 - 15 * sin(a + radians(20))
Expand Down
4 changes: 2 additions & 2 deletions pyslvs_ui/graphics/nx_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__email__ = "[email protected]"

from typing import Mapping, Tuple, Optional
from qtpy.QtCore import Qt, QSize, QPointF
from qtpy.QtCore import Qt, QSize, QPointF, QLineF
from qtpy.QtGui import QImage, QPainter, QBrush, QPen, QIcon, QPixmap, QFont
from pyslvs import edges_view
from pyslvs.graph import Graph, external_loop_layout
Expand Down Expand Up @@ -107,7 +107,7 @@ def graph2icon(
else:
pen.setColor(Qt.black)
painter.setPen(pen)
painter.drawLine(pos[l1][0], -pos[l1][1], pos[l2][0], -pos[l2][1])
painter.drawLine(QLineF(pos[l1][0], -pos[l1][1], pos[l2][0], -pos[l2][1]))
else:
color = color_qt('dark-gray') if monochrome else LINK_COLOR
color.setAlpha(150)
Expand Down
2 changes: 1 addition & 1 deletion pyslvs_ui/io/format_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def format_name(self):
elif self == ProjectFormat.PICKLE:
return "Pickle"
else:
raise KeyError("invalid format")
raise ValueError("invalid format")


class FormatEditor(QObject, metaclass=QABCMeta):
Expand Down
2 changes: 1 addition & 1 deletion pyslvs_ui/synthesis/collections/configure_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def __set_target(self) -> None:
@Slot(QListWidgetItem)
def __set_parm_bind(self, _=None) -> None:
"""Set parameters binding."""
link_expr_list: List[str] = []
link_expr_list = []
for row, gs in enumerate(list_texts(self.grounded_list)):
try:
link_expr = []
Expand Down
66 changes: 32 additions & 34 deletions pyslvs_ui/widgets/canvas_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,25 +190,25 @@ def __draw_frame(self) -> None:
pos_y = -self.oy
neg_x = -self.ox
neg_y = self.height() - self.oy
self.painter.drawLine(neg_x, pos_y, pos_x, pos_y)
self.painter.drawLine(neg_x, neg_y, pos_x, neg_y)
self.painter.drawLine(neg_x, pos_y, neg_x, neg_y)
self.painter.drawLine(pos_x, pos_y, pos_x, neg_y)
self.painter.drawLine(QLineF(neg_x, pos_y, pos_x, pos_y))
self.painter.drawLine(QLineF(neg_x, neg_y, pos_x, neg_y))
self.painter.drawLine(QLineF(neg_x, pos_y, neg_x, neg_y))
self.painter.drawLine(QLineF(pos_x, pos_y, pos_x, neg_y))

def __draw_point(self, i: int, vpoint: VPoint) -> None:
def __draw_point(self, i: int, vpt: VPoint) -> None:
"""Draw a point."""
connected = len(vpoint.links) - 1
if vpoint.type in {VJoint.P, VJoint.RP}:
pen = QPen(color_qt(vpoint.color))
connected = len(vpt.links) - 1
if vpt.type in {VJoint.P, VJoint.RP}:
pen = QPen(color_qt(vpt.color))
pen.setWidth(2)
# Draw slot point and pin point
for j, (cx, cy) in enumerate(vpoint.c):
for j, (cx, cy) in enumerate(vpt.c):
# Slot point
if j == 0 or vpoint.type == VJoint.P:
if j == 0 or vpt.type == VJoint.P:
if self.monochrome:
color = Qt.black
else:
color = color_qt(vpoint.color)
color = color_qt(vpt.color)
pen.setColor(color)
self.painter.setPen(pen)
cp = QPointF(cx, -cy) * self.zoom
Expand All @@ -223,43 +223,41 @@ def __draw_point(self, i: int, vpoint: VPoint) -> None:
text += f":({cx:.02f}, {cy:.02f})"
self.painter.drawText(cp + rp, text)
else:
grounded = (len(vpoint.c) == len(vpoint.links)
and vpoint.links[j] == VLink.FRAME)
self.draw_point(i, cx, cy, grounded, vpoint.color,
connected)
grounded = (len(vpt.c) == len(vpt.links)
and vpt.links[j] == VLink.FRAME)
self.draw_point(i, cx, cy, grounded, vpt.color, connected)
# Slider line
pen.setColor(color_qt(vpoint.color).darker())
pen.setColor(color_qt(vpt.color).darker())
self.painter.setPen(pen)
qline_m = QLineF(
QPointF(vpoint.c[1, 0], -vpoint.c[1, 1]) * self.zoom,
QPointF(vpoint.c[0, 0], -vpoint.c[0, 1]) * self.zoom
line_m = QLineF(
QPointF(vpt.c[1, 0], -vpt.c[1, 1]) * self.zoom,
QPointF(vpt.c[0, 0], -vpt.c[0, 1]) * self.zoom
)
nv = qline_m.normalVector()
nv = line_m.normalVector()
nv.setLength(self.joint_size)
nv.setPoints(nv.p2(), nv.p1())
qline_1 = nv.normalVector()
qline_1.setLength(qline_m.length())
self.painter.drawLine(qline_1)
line1 = nv.normalVector()
line1.setLength(line_m.length())
self.painter.drawLine(line1)
nv.setLength(nv.length() * 2)
nv.setPoints(nv.p2(), nv.p1())
qline_2 = nv.normalVector()
qline_2.setLength(qline_m.length())
qline_2.setAngle(qline_2.angle() + 180)
self.painter.drawLine(qline_2)
line2 = nv.normalVector()
line2.setLength(line_m.length())
line2.setAngle(line2.angle() + 180)
self.painter.drawLine(line2)
else:
self.draw_point(i, vpoint.cx, vpoint.cy, vpoint.grounded(),
vpoint.color, connected)

self.draw_point(i, vpt.cx, vpt.cy, vpt.grounded(), vpt.color,
connected)
# For selects function
if self.select_mode == SelectMode.JOINT and (i in self.selections):
pen = QPen(QColor(161, 16, 239))
pen.setWidth(3)
self.painter.setPen(pen)
self.painter.drawRect(
vpoint.cx * self.zoom - 12,
vpoint.cy * -self.zoom - 12,
self.painter.drawRect(QRectF(
vpt.cx * self.zoom - 12,
vpt.cy * -self.zoom - 12,
24, 24
)
))

def __draw_link(self, vlink: VLink) -> None:
"""Draw a link."""
Expand Down
23 changes: 15 additions & 8 deletions pyslvs_ui/widgets/inputs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,15 +458,17 @@ def __remove_path(self) -> None:
def __path_dlg(self, item: QListWidgetItem) -> None:
"""View path data."""
name = item.text().split(":", maxsplit=1)[0]
try:
paths = self.__paths[name]
except KeyError:
if name not in self.__paths:
return
points_text = ", ".join(f"Point{i}" for i in range(len(paths)))
paths = self.__paths[name]
if paths:
points_text = ", ".join(f"Point{i}" for i in range(len(paths)))
else:
points_text = "nothing"
if QMessageBox.question(
self,
"Path data",
f"This path data including {points_text}.",
f"This path data includes {points_text}.",
(QMessageBox.Save | QMessageBox.Close),
QMessageBox.Close
) != QMessageBox.Save:
Expand Down Expand Up @@ -506,9 +508,14 @@ def __copy_path_data(self) -> None:
data = self.__paths[self.__current_path_name()]
if not data:
return
QApplication.clipboard().setText('\n'.join(
f"[{x}, {y}]," for x, y in data[self.plot_joint.currentIndex()]
))
index = self.plot_joint.currentIndex()
if self.copy_as_csv.isChecked():
text = '\n'.join(f"{x},{y}" for x, y in data[index])
elif self.copy_as_array.isChecked():
text = '\n'.join(f"[{x}, {y}]," for x, y in data[index])
else:
raise ValueError("invalid option")
QApplication.clipboard().setText(text)

@Slot(name='on_show_btn_clicked')
def __show_path(self) -> None:
Expand Down
111 changes: 64 additions & 47 deletions pyslvs_ui/widgets/inputs/inputs.ui
Original file line number Diff line number Diff line change
Expand Up @@ -565,53 +565,44 @@
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_9">
<item>
<widget class="QPushButton" name="cp_data_btn">
<property name="statusTip">
<string>Copy the data of this joint to clipboard.</string>
</property>
<property name="text">
<string>Copy data</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons:copy.png</normaloff>icons:copy.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="plot_btn">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="statusTip">
<string>Plot the data of this joint.</string>
</property>
<property name="text">
<string>Plot</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons:formula.png</normaloff>icons:formula.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="animate_btn">
<property name="text">
<string>Vector Animate</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons:motor.png</normaloff>icons:motor.png</iconset>
</property>
</widget>
</item>
</layout>
<widget class="QGroupBox" name="copy_format_groupbox">
<property name="title">
<string>Copy</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8">
<item>
<widget class="QRadioButton" name="copy_as_csv">
<property name="text">
<string>Comma-Separated Values</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="copy_as_array">
<property name="text">
<string>Array-like</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cp_data_btn">
<property name="statusTip">
<string>Copy the data of this joint to clipboard.</string>
</property>
<property name="text">
<string>Copy Curve Data</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons:copy.png</normaloff>icons:copy.png</iconset>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="plot_groupbox">
Expand Down Expand Up @@ -755,6 +746,31 @@
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="plot_btn">
<property name="statusTip">
<string>Plot the data of this joint.</string>
</property>
<property name="text">
<string>Plot</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons:formula.png</normaloff>icons:formula.png</iconset>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="animate_btn">
<property name="text">
<string>Vector Animation</string>
</property>
<property name="icon">
<iconset>
<normaloff>icons:motor.png</normaloff>icons:motor.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand All @@ -766,6 +782,7 @@
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>wrt_label</sender>
Expand Down
Loading

0 comments on commit 255f6d5

Please sign in to comment.