pyqt-todolist/view/ToDoListView.py

182 lines
6.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from utils.BasicUtils import get_todo
from view.AddToDoLabel import AddToDoAction
from view.ToDoItem import ToDoItem
class Menu(QMenu):
def __init__(self):
super(Menu, self).__init__()
self.index = None
def set_index(self, index):
self.index = index
class ToDoList(QWidget):
def __init__(self, list_name, uid):
super(ToDoList, self).__init__()
self.setObjectName('todolist')
self.setAttribute(Qt.WA_StyledBackground)
# todo : 使用网格布局设置背景图
self.setStyleSheet('#todolist{background-image:url("../images/10.jpg");border-radius: 10px;}')
# 布局样式:最外面一个大的垂直布局,里面的最上面标题是一个水平布局,水平布局显示的是一个垂直布局和一个右边的选项,用来更换背景
# 布局样式继承QWidget,里面一个垂直布局最上面一个QWidget用来设置头部布局
# 头部QWidget里面一个水平布局水平布局里面一个QWidget方便设置整体位置然后是一个
layout = QVBoxLayout()
head_layout = QHBoxLayout()
head_widget = QWidget(self)
# head_widget.setMinimumHeight(175)
self.todo_list = QListWidget()
self.todo_list.setObjectName('todo_list')
# 添加ToDo的标签
self.add_todo = AddToDoAction()
self.add_todo.line_edit.returnPressed.connect(lambda: self.line_edit_add(self.add_todo.line_edit.text()))
self.add_todo.setAlignment(Qt.AlignCenter)
# self.add_todo.setPlaceholderText('添加任务')
# head_widget.setMaximumHeight(100)
# head_widget.
# head_widget.setObjectName('head_widget')
head_widget.setAttribute(Qt.WA_StyledBackground)
# self.setStyleSheet('#head_widget{margin:50px}')
# head_widget.setPalette(palette)
head_widget.setLayout(head_layout)
head_layout.setAlignment(Qt.AlignCenter)
# head_left_layout = QVBoxLayout()
# layout.addStretch(0)
layout.addWidget(head_widget, 2, Qt.AlignTop)
# layout.addSpacing(1)
# layout.addStretch(1)
# layout.addStretch(0)
layout.addWidget(self.todo_list, 6, Qt.AlignCenter | Qt.AlignTop)
head_label = QLabel(list_name)
head_label.setObjectName('head_label')
font = QFont()
font.setPointSize(32)
head_label.setFont(font)
layout.addStretch(1)
head_layout.addWidget(head_label, 8)
layout.addStretch(1)
layout.addWidget(self.add_todo, 2, Qt.AlignHCenter)
self.menu_image = QPushButton()
self.menu_image.setObjectName('menu_image')
self.menu_image.setFixedSize(30, 30)
# self.menu_image.setScaledContents(True)
pic_btn = QPixmap(os.getcwd() + "/../images/menu.png")
self.menu_image.setIcon(QIcon(pic_btn))
# self.menu_image.setFixedSize(QPixmap(os.getcwd() + "/../images/menu.png").size())
# head_layout.addLayout(head_left_layout)
head_layout.addWidget(self.menu_image, 2)
head_layout.addStretch(1)
self.setLayout(layout)
# self.menu_image.clicked.connect(self.menu_action)
self.button_menu = QMenu(self)
# 背景透明
self.button_menu.setAttribute(Qt.WA_TranslucentBackground)
# 无边框、去掉自带阴影
self.button_menu.setWindowFlags(
self.button_menu.windowFlags() | Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
self.menu_image.setMenu(self.button_menu)
theme_label = QAction(self)
theme_label.setText('主题')
theme_label.setDisabled(True)
self.button_menu.addAction(theme_label)
self.button_menu.addSeparator()
#
# for i in range(11):
# label_action = QAction(self)
# label_action.setIcon(QIcon('../images/' + str(i) + '.jpg'))
# label_action.setText(str(i))
# label_action.triggered.connect(lambda: self.menu_action(str(i)))
# self.button_menu.addAction(label_action)
# for i in range(11):
label_action1 = QAction(self)
label_action1.setIcon(QIcon('../images/1.jpg'))
label_action1.setText('渐变1')
label_action1.triggered.connect(lambda: self.menu_action(1))
self.button_menu.addAction(label_action1)
label_action2 = QAction(self)
label_action2.setIcon(QIcon('../images/2.jpg'))
label_action2.setText('渐变2')
label_action2.triggered.connect(lambda: self.menu_action(2))
self.button_menu.addAction(label_action2)
label_action3 = QAction(self)
label_action3.setIcon(QIcon('../images/3.jpg'))
label_action3.setText('渐变3')
label_action3.triggered.connect(lambda: self.menu_action(3))
self.button_menu.addAction(label_action3)
label_action4 = QAction(self)
label_action4.setIcon(QIcon('../images/4.jpg'))
label_action4.setText('渐变4')
label_action4.triggered.connect(lambda: self.menu_action(4))
self.button_menu.addAction(label_action4)
label_action5 = QAction(self)
label_action5.setIcon(QIcon('../images/5.jpg'))
label_action5.setText('渐变5')
label_action5.triggered.connect(lambda: self.menu_action(5))
self.button_menu.addAction(label_action5)
# 读取配置文件添加Todo
load_todo = get_todo(uid)
if load_todo:
for load in load_todo:
self.load_todo(load[0], load[1])
# button_menu
# for i in range(11):
# label_action = QAction(self)
# label_action.setIcon(QIcon('../images/' + str(i) + '.jpg'))
# label_action.setText(str(i))
# label_action.triggered.connect(lambda: self.menu_action(str(i)))
# button_menu.addAction(label_action)
def menu_action(self, count):
# 点击按钮
count = str(count)
# self.menu_label = ChangeTheme()
self.setStyleSheet('#todolist{background-image:url("../images/' + count + '.jpg");'
'background-position: center; border-radius: 10px;'
'}')
# print('选中:' + count)
def load_todo(self, name, uid):
todo_item = ToDoItem(name)
self.todo_list.addItem(todo_item)
self.todo_list.setItemWidget(todo_item, todo_item.widget)
pass
def line_edit_add(self, name):
self.add_todo.todo_name.emit(name)
# print('添加的新ToDo' + name)
todo_item = ToDoItem(name)
self.todo_list.addItem(todo_item)
self.todo_list.setItemWidget(todo_item, todo_item.widget)
self.add_todo.line_edit.clear()