pyqt-todolist/main/main.py

259 lines
9.7 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
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import configparser
from utils.BasicUtils import get_todo_list, remove_todo_list
from utils.CreateToDo import CreateToDo
from utils.QSSLoader import QSSLoader
from view.ImportantView import Important
from view.MyListWidget import MyListWidget
from view.ToDoListView import ToDoList
from view.UserLabel import User
from view.SelfListWidgetItem import SelfListWidgetItem
from view.AddListLabel import AddListAction
from view.MyDayView import MyDay
#
#
# class SelfListWidgetItem(QListWidgetItem):
# """
# :param item_name: 列表名称
# :param uid: 当前列表的uuid
# :param todo_count: 设置剩余代办数量,默认为零
# :param show_icon: 设置显示的图标路径,默认为空
# """
#
# def __init__(self, item_name, todo_count=0, uid=None, show_icon=None):
# super(SelfListWidgetItem, self).__init__()
# layout = QHBoxLayout()
# # print(show_icon)
# self.item_name = item_name
#
# self.todo_count = todo_count
# self.show_icon = show_icon
# # 布局
# self.widget = QWidget()
# self.widget.setLayout(layout)
#
# # self.list_item = QLabel('')
#
# # 添加左边的图标和右边的气泡
# if self.show_icon:
# self.icon_label = QLabel('')
# self.icon_label.setPixmap(
# QPixmap(self.show_icon).scaled(30, 30, Qt.IgnoreAspectRatio, Qt.SmoothTransformation))
# layout.addWidget(self.icon_label, 1)
# # 添加
# self.item_name_label = QLabel(self.item_name)
# # print(self.item_name)
# self.item_name_label.setObjectName('item_name_label')
#
# layout.addWidget(self.item_name_label, 3)
#
# if self.todo_count:
# # 打印出代办数目
# print('todo_count:', self.todo_count)
# self.todo_count_label = QLabel(str(self.todo_count))
# self.todo_count_label.setFixedSize(16, 16)
# self.todo_count_label.setScaledContents(True)
# self.todo_count_label.setObjectName('todo_count_label')
# self.todo_count_label.setAlignment(Qt.AlignCenter)
# layout.addWidget(self.todo_count_label, 1)
# # 设置自定义的QListWidgetItem的sizeHint不然无法显示
# self.setSizeHint(self.widget.sizeHint())
# MainWidget().system_listWidget.change_list_name.connect(self.change_name)
#
# def change_name(self):
# pass
#
class MainWidget(QWidget):
def __init__(self):
super(MainWidget, self).__init__()
self.user_label = User()
# 主布局,左右两侧
layout = QHBoxLayout()
# 子布局,左边的部分
self.sub_layout = QVBoxLayout()
self.sub_layout.spacing()
self.sub_layout.addWidget(self.user_label, 2)
# 左侧列表
self.system_listWidget = MyListWidget(self)
self.system_listWidget.setFrameShape(QListWidget.NoFrame)
self.sub_layout.addWidget(self.system_listWidget, 15)
# 添加最下边的QLabel
self.add_list_action = AddListAction()
self.add_list_action.create_todo_list.connect(self.create_item)
# self.add_item_label.setPixmap(QPixmap())
self.sub_layout.addWidget(self.add_list_action, 1)
# 禁止双击可编辑
self.system_listWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
# 右键菜单
self.system_listWidget.setContextMenuPolicy(Qt.CustomContextMenu)
self.system_listWidget.setObjectName('custom')
self.system_listWidget.customContextMenuRequested.connect(self.myListWidgetContext)
# 右侧层叠窗口
self.my_day = MyDay()
self.important = Important()
self.stackedWidget = QStackedWidget(self)
self.stackedWidget.addWidget(self.my_day)
self.stackedWidget.addWidget(self.important)
layout.addLayout(self.sub_layout, 2)
layout.addWidget(self.stackedWidget, 6)
self.one_day = SelfListWidgetItem('我的一天', 0, None, os.getcwd() + '/../images/sun.svg')
self.system_listWidget.setCurrentRow(1)
# self.one_day.setSelected(True)
self.important_item = SelfListWidgetItem('重要', 0, None, os.getcwd() + '/../images/star.svg')
self.system_listWidget.currentRowChanged.connect(self.stackedWidget.setCurrentIndex)
self.setLayout(layout)
self.initUI()
def initUI(self):
# 系统默认的两个 item
self.system_listWidget.addItem(self.one_day)
self.system_listWidget.setItemWidget(self.one_day, self.one_day.widget)
self.system_listWidget.addItem(self.important_item)
self.system_listWidget.setItemWidget(self.important_item, self.important_item.widget)
# todo 通过信号修改显示的标题名字
self.system_listWidget.change_list_name.connect(self.change_list)
# 加载列表
self.load_list = get_todo_list()
# print(load_list)
if self.load_list:
for load in self.load_list:
self.load_item(load[0], load[1])
# self.system_listWidget.addScrollBarWidget(False)
# 测试添加下面的
# self.two_day = SelfListWidgetItem('我的一天', 1, os.getcwd() + '/../images/sun.svg')
# self.custom.addItem(self.two_day)
# self.custom.setItemWidget(self.two_day, self.two_day.widget)
# self.custom.
def change_list(self):
print(self.system_listWidget.currentItem().text())
# print(name)
# todo 仔细研究右键菜单
def myListWidgetContext(self, position):
if self.system_listWidget.currentRow() > 1:
# 设置右键菜单
pop_menu = QMenu(self)
pop_menu.setObjectName('lift_menu')
pop_menu.setAttribute(Qt.WA_TranslucentBackground)
# 无边框、去掉自带阴影
pop_menu.setWindowFlags(
pop_menu.windowFlags() | Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
rename_action = QAction(u'重命名', self)
copy_action = QAction("复制分组", self)
del_action = QAction("删除分组", self)
# 查看右键时是否在item上面,如果不在.就不显示删除和修改.
pop_menu.addAction(rename_action)
if self.system_listWidget.itemAt(position):
pop_menu.addAction(del_action)
pop_menu.addAction(rename_action)
rename_action.triggered.connect(self.rename_item)
copy_action.triggered.connect(self.create_item)
del_action.triggered.connect(self.delete_item)
pop_menu.exec_(self.system_listWidget.mapToGlobal(position))
def load_item(self, list_name, uid):
item = SelfListWidgetItem(list_name, 0, uid)
item.setTextAlignment(Qt.AlignCenter)
# 使得item是可以编辑的.
item.setFlags(item.flags() | Qt.ItemIsEditable)
self.system_listWidget.addItem(item)
todo_list = ToDoList(list_name, uid)
# 信号测试成功!!
todo_list.update_signal.connect(self.important.refresh_action)
self.important.update_signal.connect(todo_list.refresh_action)
# 创建后就可以编辑item,用户自己起名字.
# self.system_listWidget.editItem(item)
self.stackedWidget.addWidget(todo_list)
self.system_listWidget.setItemWidget(item, item.widget)
# 创建新的分组
def create_item(self, new_list='新建列表', uid=None):
# 创建一个没有名字的item
# new_list = '新建列表'
if not uid:
create_todo_uid = CreateToDo('ToDoList', new_list)
item = SelfListWidgetItem(new_list, 0, create_todo_uid)
item.setTextAlignment(Qt.AlignCenter)
# 使得item是可以编辑的.
item.setFlags(item.flags() | Qt.ItemIsEditable)
self.system_listWidget.addItem(item)
todo_list = ToDoList(new_list, create_todo_uid)
self.stackedWidget.addWidget(todo_list)
# 创建后就可以编辑item,用户自己起名字.
# self.system_listWidget.editItem(item)
self.system_listWidget.setItemWidget(item, item.widget)
# 删除分组
def delete_item(self):
# print(self.load_list[self.system_listWidget.currentRow() - 2][1])
remove_todo_list(self.load_list[self.system_listWidget.currentRow() - 2][1])
self.system_listWidget.takeItem(self.system_listWidget.currentRow())
# 重命名分组
def rename_item(self):
curRow = self.system_listWidget.currentRow()
item = self.system_listWidget.item(curRow)
item.setFlags(item.flags() | Qt.ItemIsEditable)
self.system_listWidget.editItem(item)
self.system_listWidget.itemChanged.connect(lambda: self.ChangeItem(item))
def ChangeItem(self, item):
print(item)
print("test")
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.resize(1150, 850)
self.setWindowTitle("待办事项")
# 主布局,用来显示主页面,设置页面等
self.layout = QStackedLayout(self)
self.main_widget = MainWidget()
# self.main_widget.resize(50, 200)
self.layout.addWidget(self.main_widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainWindow()
main.setWindowTitle('PyQtToDoList')
# print(os.path.abspath('../'))
style_sheet = QSSLoader.read_qss_file('../resource/current.qss')
main.setStyleSheet(style_sheet)
app.setWindowIcon(QIcon(os.path.abspath('../') + '/images/todo.svg'))
main.show()
sys.exit(app.exec_())