pyqt-todolist/main/main.py

160 lines
5.6 KiB
Python
Raw Normal View History

2022-04-01 22:25:19 +08:00
import os
import sys
from PyQt5.QtGui import *
2022-03-29 20:28:43 +08:00
from PyQt5.QtWidgets import *
2022-04-01 22:25:19 +08:00
from PyQt5.QtCore import Qt
import configparser
from utils.CreateToDo import CreateToDo
2022-04-01 22:25:19 +08:00
from utils.QSSLoader import QSSLoader
2022-04-12 21:34:18 +08:00
from view.ImportantView import Important
from view.ToDoListView import ToDoList
2022-04-01 22:25:19 +08:00
from view.UserLabel import User
from view.SelfListWidgetItem import SelfListWidgetItem
2022-04-07 19:34:28 +08:00
from view.AddListLabel import AddListAction
2022-04-07 21:11:37 +08:00
from view.MyDayView import MyDay
2022-03-31 21:55:51 +08:00
2022-04-01 22:25:19 +08:00
class MainWidget(QWidget):
2022-03-31 21:55:51 +08:00
def __init__(self):
2022-04-01 22:25:19 +08:00
super(MainWidget, self).__init__()
self.user_label = User()
# 主布局,左右两侧
2022-03-31 21:55:51 +08:00
layout = QHBoxLayout()
2022-04-01 22:25:19 +08:00
# 子布局,左边的部分
self.sub_layout = QVBoxLayout()
self.sub_layout.spacing()
2022-04-07 19:34:28 +08:00
self.sub_layout.addWidget(self.user_label, 2)
2022-04-01 22:25:19 +08:00
# 左侧列表
self.system_listWidget = QListWidget(self)
2022-04-12 21:34:18 +08:00
self.system_listWidget.setFrameShape(QListWidget.NoFrame)
2022-04-07 19:34:28 +08:00
self.sub_layout.addWidget(self.system_listWidget, 15)
2022-04-01 22:25:19 +08:00
2022-04-07 21:11:37 +08:00
# 添加最下边的QLabel
2022-04-07 19:34:28 +08:00
self.add_list_action = AddListAction()
2022-04-16 21:45:29 +08:00
self.add_list_action.create_todo_list.connect(self.CreateNewItem)
# self.add_item_label.setPixmap(QPixmap())
2022-04-07 19:34:28 +08:00
self.sub_layout.addWidget(self.add_list_action, 1)
# 禁止双击可编辑
2022-04-07 19:34:28 +08:00
# self.custom.setEditTriggers(QAbstractItemView.NoEditTriggers)
# 右键菜单
2022-04-07 19:34:28 +08:00
# self.custom.setContextMenuPolicy(Qt.CustomContextMenu)
# self.custom.setObjectName('custom')
# self.custom.customContextMenuRequested.connect(self.myListWidgetContext)
2022-04-01 22:25:19 +08:00
# 右侧层叠窗口
2022-04-07 21:11:37 +08:00
2022-04-12 21:34:18 +08:00
self.my_day = MyDay()
self.important = Important()
2022-04-01 22:25:19 +08:00
self.stackedWidget = QStackedWidget(self)
2022-04-12 21:34:18 +08:00
self.stackedWidget.addWidget(self.my_day)
self.stackedWidget.addWidget(self.important)
2022-04-01 22:25:19 +08:00
2022-04-07 21:11:37 +08:00
layout.addLayout(self.sub_layout, 2)
2022-04-01 22:25:19 +08:00
2022-04-07 21:11:37 +08:00
layout.addWidget(self.stackedWidget, 6)
2022-04-01 22:25:19 +08:00
self.one_day = SelfListWidgetItem('我的一天', 0, os.getcwd() + '/../images/sun.svg')
self.system_listWidget.setCurrentRow(1)
2022-04-01 22:25:19 +08:00
# self.one_day.setSelected(True)
2022-04-12 21:34:18 +08:00
self.important_item = SelfListWidgetItem('重要', 0, os.getcwd() + '/../images/star.svg')
self.system_listWidget.currentRowChanged.connect(self.stackedWidget.setCurrentIndex)
2022-04-01 22:25:19 +08:00
self.setLayout(layout)
self.initUI()
def initUI(self):
# 系统默认的两个 item
2022-04-01 22:25:19 +08:00
self.system_listWidget.addItem(self.one_day)
self.system_listWidget.setItemWidget(self.one_day, self.one_day.widget)
2022-04-12 21:34:18 +08:00
self.system_listWidget.addItem(self.important_item)
self.system_listWidget.setItemWidget(self.important_item, self.important_item.widget)
# self.system_listWidget.addScrollBarWidget(False)
2022-04-01 22:25:19 +08:00
2022-04-07 19:34:28 +08:00
# 测试添加下面的
# 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.
2022-04-01 22:25:19 +08:00
2022-04-07 19:34:28 +08:00
# todo 仔细研究右键菜单
def myListWidgetContext(self, position):
# 设置右键菜单
pop_menu = QMenu(self)
rename_action = QAction(u'重命名', self)
copy_action = QAction("复制分组", self)
del_action = QAction("删除分组", self)
# 查看右键时是否在item上面,如果不在.就不显示删除和修改.
pop_menu.addAction(rename_action)
2022-04-07 19:34:28 +08:00
if self.custom.itemAt(position):
pop_menu.addAction(del_action)
pop_menu.addAction(rename_action)
rename_action.triggered.connect(self.RenameItem)
copy_action.triggered.connect(self.CreateNewItem)
del_action.triggered.connect(self.DeleteItem)
2022-04-07 19:34:28 +08:00
pop_menu.exec_(self.custom.mapToGlobal(position))
# 创建新的分组
def CreateNewItem(self):
# 创建一个没有名字的item
new_list = '新建列表'
create_todo = CreateToDo('ToDoList', new_list)
item = SelfListWidgetItem(new_list)
item.setTextAlignment(Qt.AlignCenter)
# 使得item是可以编辑的.
item.setFlags(item.flags() | Qt.ItemIsEditable)
2022-04-16 21:45:29 +08:00
self.system_listWidget.addItem(item)
todo_list = ToDoList(new_list)
self.stackedWidget.addWidget(todo_list)
# 创建后就可以编辑item,用户自己起名字.
2022-04-16 21:45:29 +08:00
# self.system_listWidget.editItem(item)
self.system_listWidget.setItemWidget(item, item.widget)
# 删除分组
def DeleteItem(self):
2022-04-07 19:34:28 +08:00
self.custom.takeItem(self.custom.currentRow())
# 重命名分组
def RenameItem(self):
2022-04-07 19:34:28 +08:00
curRow = self.custom.currentRow()
item = self.custom.item(curRow)
item.setFlags(item.flags() | Qt.ItemIsEditable)
2022-04-07 19:34:28 +08:00
self.custom.editItem(item)
self.custom.itemChanged.connect(lambda: self.ChangeItem(item))
def ChangeItem(self, item):
print("test")
2022-04-01 22:25:19 +08:00
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.resize(1150, 850)
2022-04-01 22:25:19 +08:00
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')
2022-04-01 22:25:19 +08:00
# 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_())