Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 214 additions & 0 deletions examples/Gallery for siui/components/page_refactor/example_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
from contextlib import contextmanager

from PyQt5.QtWidgets import QAction, QActionGroup, QWidget

from siui.components.combobox_ import ComboboxItemWidget
from siui.components.menu_ import SiRoundedMenu
from siui.core import SiGlobal


def exampleSiRoundedMenu(parent: QWidget) -> SiRoundedMenu:
@contextmanager
def useMenu(menu: SiRoundedMenu):
yield menu

main_menu = SiRoundedMenu(parent)

sub_menu_1 = SiRoundedMenu(main_menu)
sub_menu_1.setTitle("纯文字菜单")
sub_menu_1.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_text_regular"))
with useMenu(sub_menu_1) as menu:
menu: SiRoundedMenu
menu.addAction(QAction("名称"))
menu.addAction(QAction("大小"))
menu.addAction(QAction("项目类型"))
menu.addAction(QAction("修改日期"))

sub_menu_2 = SiRoundedMenu(main_menu)
sub_menu_2.setTitle("带图标的菜单")
sub_menu_2.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_image_regular"))
with useMenu(sub_menu_2) as menu:
menu: SiRoundedMenu
action1 = QAction("名称")
action1.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_rename_regular"))
action2 = QAction("大小")
action2.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_resize_regular"))
action3 = QAction("项目类型")
action3.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_class_regular"))
action4 = QAction("修改日期")
action4.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_calendar_date_regular"))

menu.addAction(action1)
menu.addAction(action2)
menu.addAction(action3)
menu.addAction(action4)

sub_menu_3 = SiRoundedMenu(main_menu)
sub_menu_3.setTitle("图标与快捷键")
sub_menu_3.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_keyboard_regular"))
with useMenu(sub_menu_3) as menu:
menu: SiRoundedMenu
action1 = QAction("复制")
action1.setShortcut("Ctrl+C")
action1.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_copy_regular"))
action2 = QAction("剪切")
action2.setShortcut("Ctrl+X")
action2.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_cut_regular"))
action3 = QAction("粘贴")
action3.setShortcut("Ctrl+V")
action3.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_clipboard_paste_regular"))
action4 = QAction("撤销")
action4.setShortcut("Ctrl+Z")
action4.setEnabled(False)
action4.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_arrow_undo_regular"))

menu.addAction(action1)
menu.addAction(action2)
menu.addAction(action3)
menu.addSeparator()
menu.addAction(action4)

sub_menu_4 = SiRoundedMenu(main_menu)
sub_menu_4.setTitle("可选择")
sub_menu_4.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_table_checker_filled"))
with useMenu(sub_menu_4) as menu:
menu: SiRoundedMenu

action1 = QAction("命令行模式")
action1.setCheckable(True)
action1.setChecked(True)
action2 = QAction("窗口模式")
action2.setCheckable(True)

group1 = QActionGroup(parent)
group1.addAction(action1)
group1.addAction(action2)
group1.setExclusionPolicy(group1.ExclusionPolicy.Exclusive)

action1 = QAction("自动求和运算")
action1.setCheckable(True)
action1.setChecked(True)
action2 = QAction("自动翻译")
action2.setCheckable(True)
action2.setChecked(True)

group2 = QActionGroup(parent)
group2.addAction(action1)
group2.addAction(action2)
group2.setExclusionPolicy(group2.ExclusionPolicy.None_)

action3 = QAction("自动保存")
action3.setCheckable(True)
action3.setChecked(True)

menu.addActions(group1.actions())
menu.addSeparator()
menu.addActions(group2.actions())
menu.addSeparator()
menu.addAction(action3)

sub_menu_5 = SiRoundedMenu(main_menu)
sub_menu_5.setTitle("可选择且带图标")
sub_menu_5.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_table_checker_filled"))
with useMenu(sub_menu_5) as menu:
menu: SiRoundedMenu

action1 = QAction("命令行模式")
action1.setCheckable(True)
action1.setChecked(True)
action1.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_window_console_regular"))
action2 = QAction("窗口模式")
action2.setCheckable(True)
action2.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_window_bullet_list_regular"))

group1 = QActionGroup(parent)
group1.addAction(action1)
group1.addAction(action2)
group1.setExclusionPolicy(group1.ExclusionPolicy.Exclusive)

action1 = QAction("自动求和运算")
action1.setCheckable(True)
action1.setChecked(True)
action1.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_autosum_regular"))
action2 = QAction("自动翻译")
action2.setCheckable(True)
action2.setChecked(True)
action2.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_translate_auto_regular"))

group2 = QActionGroup(parent)
group2.addAction(action1)
group2.addAction(action2)
group2.setExclusionPolicy(group2.ExclusionPolicy.None_)

action3 = QAction("自动保存")
action3.setCheckable(True)
action3.setChecked(True)
action3.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_save_regular"))

menu.addActions(group1.actions())
menu.addSeparator()
menu.addActions(group2.actions())
menu.addSeparator()
menu.addAction(action3)

sub_menu_6 = SiRoundedMenu(main_menu)
sub_menu_6.setTitle("多层子菜单")
sub_menu_6.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_stack_regular"))
with useMenu(sub_menu_6) as menu1:
menu1: SiRoundedMenu

sub_1 = SiRoundedMenu(menu1)
sub_1.setTitle("子菜单1")
sub_1.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_stack_regular"))
with useMenu(sub_1) as menu2:
menu2: SiRoundedMenu

sub_2 = SiRoundedMenu(menu2)
sub_2.setTitle("子菜单2")
sub_2.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_stack_regular"))
with useMenu(sub_2) as menu:
menu: SiRoundedMenu
menu.addAction(QAction("名称"))
menu.addAction(QAction("大小"))
menu.addAction(QAction("项目类型"))
menu.addAction(QAction("修改日期"))

menu2.addMenu(sub_2)
menu1.addMenu(sub_1)

sub_menu_7 = SiRoundedMenu(main_menu)
sub_menu_7.setTitle("长菜单")
sub_menu_7.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_table_checker_filled"))
with useMenu(sub_menu_7) as menu:
menu: SiRoundedMenu

for i in range(1, 50):
menu.addAction(f"Action {i}")

sub_menu_8 = SiRoundedMenu(main_menu)
sub_menu_8.setTitle("Combobox 控件测试")
sub_menu_8.setIcon(SiGlobal.siui.iconpack.toIcon("ic_fluent_table_checker_filled"))
with useMenu(sub_menu_8) as menu:
group = QActionGroup(parent)
group.setExclusive(True)
for i in range(10):
new_action = QAction(f"选项 {i}")
new_action.setCheckable(True)
group.addAction(new_action)

for action in group.actions():
menu.addCustomWidget(action, ComboboxItemWidget)

main_menu.addMenu(sub_menu_1)
main_menu.addMenu(sub_menu_2)
main_menu.addMenu(sub_menu_3)
main_menu.addSeparator()
main_menu.addMenu(sub_menu_4)
main_menu.addMenu(sub_menu_5)
main_menu.addSeparator()
main_menu.addMenu(sub_menu_6)
main_menu.addSeparator()
main_menu.addMenu(sub_menu_7)
main_menu.addMenu(sub_menu_8)

return main_menu
115 changes: 115 additions & 0 deletions examples/Gallery for siui/components/page_refactor/example_popover.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from contextlib import contextmanager
from typing import Callable, List, Iterable

from PyQt5.QtCore import QPoint
from PyQt5.QtWidgets import QAbstractButton, QWidget

from siui.components.button import SiPushButtonRefactor
from siui.components.graphic import SiGraphicWrapperWidget
from siui.components.popover import SiPopover, SiPopoverDatePicker, SiPopoverStackedWidget, SiPopoverCalenderPicker


def _createWrapper(widget: QWidget, animation: Iterable[Callable]) -> SiGraphicWrapperWidget:
wrapper = SiGraphicWrapperWidget()
wrapper.setWidget(widget)
wrapper.setMergeAnimations(*animation)
return wrapper


def _createButtonSlot(button: QAbstractButton, popover: SiPopover) -> Callable:
def slot():
pos = button.mapToGlobal(button.pos()) - QPoint(0, popover.sizeHint().height())
popover.wrapper().playMergeAnimations()
popover.popup(pos)

return slot


def exampleDatePickerPopover(parent: QWidget) -> SiPushButtonRefactor:

# 本示例将使用 SiPopover 创建一个 popover
# 包含一个类似 QStackedWidget 的页面堆叠控件 SiPopoverStackedWidget
# 其具有一个页面,用于展示日期选择器 SiPopoverDatePicker
# 最后,示例创建一个按钮,用于弹出这个 popover

# 本示例完整地展示了 popover 的使用方法,并提供详细的注释供参阅。
# 下面有更多示例,但不再带有详细注释。

# 创建 popover
popover = SiPopover()

# 创建堆叠控件 SiPopoverStackedWidget
# 这是接下来要显示在 popover 中的控件
stack_widget = SiPopoverStackedWidget(popover)

# 创建 SiPopoverDatePicker
# 这是接下来要添加到 stack_widget 中的控件
date_picker = SiPopoverDatePicker()

# 创建 "日期选择" 页面所需的图形包装器,并将 date_picker 添加到包装器里。
# 使用图形包装器,我们可以实现更强大的视觉变换效果,如仿射变换,透明度等等
# 这里,我们仅利用其实现页面的弹出动画
page_wrapper = SiGraphicWrapperWidget()
page_wrapper.setWidget(date_picker)

# SiGraphicWrapperWidget 提供了强大且丰富的动画预设,它们可以直接组合在一起
# 更多动画可以在 SiGraphicWrapperWidget.TransitionAnimations 下获取,
# 部分支持的动画已经给出,你可以尝试移除注释组合他们
# 你也可以仿照这些动画的实现,创造你自己的动画。不过对于大多数场景,预设动画及其组合已经足够
page_wrapper.setMergeAnimations(
# SiGraphicWrapperWidget.TransitionAnimations.scaleUp,
# SiGraphicWrapperWidget.TransitionAnimations.fadeIn,
SiGraphicWrapperWidget.TransitionAnimations.floatUp,
# SiGraphicWrapperWidget.TransitionAnimations.floatLeftIn,
)

# 将构建的 page_wrapper 添加到 stack_widget 中
stack_widget.addPage(page_wrapper, "日期选择")

# 最后,将 stack_widget 添加到 popover 的 wrapper 中
# 需要说明的是,SiPopover 的内容同样是 Graphic View Framework 驱动的
# 因此,同样也可以给它设置弹出动画。
popover.wrapper().setWidget(stack_widget)
popover.wrapper().setMergeAnimations(
SiGraphicWrapperWidget.TransitionAnimations.floatUp,
)

# 以上的构建过程实际上可以使用 with 语句简化代码。但此处展示具体步骤而展开编写。
# 总体来说,在本示例中,控件之间的关系为:
# SiPopover -(wrapper)-> SiPopoverStackedWidget -(wrapper)-> SiPopoverDatePicker

# 创建按钮
button = SiPushButtonRefactor(parent)
button.setText("打开 日期选择器 示例")
button_slot = _createButtonSlot(button, popover)

button.clicked.connect(button_slot)

return button


def exampleCalenderPickerPopover(parent: QWidget) -> SiPushButtonRefactor:
popover = SiPopover()
stack_widget = SiPopoverStackedWidget(popover)

page1 = _createWrapper(
widget=SiPopoverCalenderPicker(parent),
animation=(
SiGraphicWrapperWidget.TransitionAnimations.floatUp,
)
)

stack_widget.addPage(page1, "日历选择器")

popover.wrapper().setWidget(stack_widget)
popover.wrapper().setMergeAnimations(
SiGraphicWrapperWidget.TransitionAnimations.floatUp,
)

button = SiPushButtonRefactor(parent)
button.setText("打开 日历选择器 示例")
button_slot = _createButtonSlot(button, popover)

button.clicked.connect(button_slot)

return button
Loading
Loading