支持自定义选择导出聊天记录的日期

This commit is contained in:
shuaikangzhou
2024-01-09 23:55:39 +08:00
parent 63a03858d2
commit fe4e719012
12 changed files with 264 additions and 53 deletions

View File

@@ -3,14 +3,16 @@ from typing import List
from PyQt5 import QtWidgets
from PyQt5.QtCore import QTimer, QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QDialog, QCheckBox, QMessageBox
from PyQt5.QtWidgets import QApplication, QDialog, QCheckBox, QMessageBox, QCalendarWidget
from app.DataBase import micro_msg_db, misc_db
from app.DataBase.output_pc import Output
from app.components import ScrollBar
from app.components.calendar_dialog import CalendarDialog
from app.components.export_contact_item import ContactQListWidgetItem
from app.person import Contact
from app.ui.menu.exportUi import Ui_Dialog
from app.ui.menu.export_time_range import TimeRangeDialog
types = {
'文本': 1,
@@ -77,22 +79,46 @@ class ExportDialog(QDialog, Ui_Dialog):
self.listWidget.itemClicked.connect(self.setCurrentIndex)
self.visited = set()
self.now_index = 0
self.time_range = None
def set_export_date(self):
date_range = self.comboBox_time.currentText()
if date_range == '全部时间':
pass
elif date_range == '最近三个月':
QMessageBox.warning(self,
"别急别急",
"马上就实现该功能"
)
elif date_range == '自定义时间':
QMessageBox.warning(self,
"别急别急",
"马上就实现该功能"
)
from datetime import datetime, timedelta
# 获取今天的日期和时间
today = datetime.now()
# 获取今天的日期
today_date = today.date()
# 获取今天的24:00:00的时间戳
today_midnight = datetime.combine(today_date, datetime.min.time()) + timedelta(days=1)
today_midnight_timestamp = int(today_midnight.timestamp())
# 获取三个月前的日期
three_months_ago = today - timedelta(days=90)
# 获取三个月前的00:00:00的时间戳
three_months_ago_date = three_months_ago.date()
three_months_ago_midnight = datetime.combine(three_months_ago_date, datetime.min.time())
three_months_ago_midnight_timestamp = int(three_months_ago_midnight.timestamp())
self.time_range = (three_months_ago_midnight_timestamp,today_midnight_timestamp)
elif date_range == '自定义时间':
self.time_range_view = TimeRangeDialog(parent=self)
self.time_range_view.date_range_signal.connect(self.set_time_range)
self.time_range_view.show()
self.comboBox_time.setCurrentIndex(0)
# QMessageBox.warning(self,
# "别急别急",
# "马上就实现该功能"
# )
def set_time_range(self,time_range):
self.time_range = time_range
self.comboBox_time.setCurrentIndex(2)
def export_data(self):
self.btn_start.setEnabled(False)
# 在这里获取用户选择的导出数据类型
@@ -115,7 +141,7 @@ class ExportDialog(QDialog, Ui_Dialog):
select_contacts.append(self.contacts[i])
# 在这里根据用户选择的数据类型执行导出操作
print("选择的文件格式:", file_types)
self.worker = Output(select_contacts, type_=Output.Batch, message_types=selected_types, sub_type=file_types)
self.worker = Output(select_contacts, type_=Output.Batch, message_types=selected_types, sub_type=file_types,time_range=self.time_range)
# self.worker.progressSignal.connect(self.update_progress)
self.worker.okSignal.connect(self.export_finished)
self.worker.rangeSignal.connect(self.set_total_msg_num)

View File

@@ -0,0 +1,72 @@
from datetime import datetime
from PyQt5 import QtWidgets
from PyQt5.QtCore import QTimer, QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QDialog, QCheckBox, QMessageBox, QCalendarWidget, QWidget, QVBoxLayout, QLabel
from app.components.calendar_dialog import CalendarDialog
from .time_range import Ui_Dialog
Stylesheet = '''
QToolButton{
color:#000000;
}
'''
class TimeRangeDialog(QDialog, Ui_Dialog):
date_range_signal = pyqtSignal(tuple)
def __init__(self, date_range=None, parent=None):
"""
@param date_range: tuple[Union[QDate, datetime.date],Union[QDate, datetime.date]]
@param parent:
"""
super().__init__(parent)
self.calendar = None
self.setupUi(self)
self.setStyleSheet(Stylesheet)
self.toolButton_start_time.clicked.connect(self.select_date_start)
self.toolButton_end_time.clicked.connect(self.select_date_end)
self.calendar = CalendarDialog(date_range=date_range, parent=self)
self.calendar.selected_date_signal.connect(self.set_date)
self.btn_ok.clicked.connect(self.ok)
self.btn_cancel.clicked.connect(lambda x:self.close())
self.start_time_flag = True
self.start_timestamp = 0
self.end_timestamp = 0
def set_date(self, timestamp):
if self.start_time_flag:
self.start_timestamp = timestamp
date_object = datetime.fromtimestamp(timestamp)
str_start =date_object.strftime("%Y-%m-%d")
self.toolButton_start_time.setText(str_start)
else:
date_object = datetime.fromtimestamp(timestamp)
str_start = date_object.strftime("%Y-%m-%d")
self.end_timestamp = timestamp + 86399
self.toolButton_end_time.setText(str_start)
def ok(self):
date_range = (self.start_timestamp,self.end_timestamp)
self.date_range_signal.emit(date_range)
self.close()
def select_date_start(self):
self.start_time_flag = True
self.calendar.show()
def select_date_end(self):
self.start_time_flag = False
self.calendar.show()
if __name__ == '__main__':
import sys
from datetime import datetime
app = QApplication(sys.argv)
# 设置日期范围
start_date = datetime(2023, 12, 11)
end_date = datetime(2024, 1, 9)
date_range = (start_date.date(), end_date.date())
ex = CalendarDialog(date_range=date_range)
ex.show()
sys.exit(app.exec_())

71
app/ui/menu/time_range.py Normal file
View File

@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'time_range.ui'
#
# Created by: PyQt5 UI code generator 5.15.10
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(280, 200)
Dialog.setMinimumSize(QtCore.QSize(280, 200))
Dialog.setMaximumSize(QtCore.QSize(280, 200))
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(Dialog)
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.toolButton_start_time = QtWidgets.QToolButton(Dialog)
self.toolButton_start_time.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
self.toolButton_start_time.setArrowType(QtCore.Qt.DownArrow)
self.toolButton_start_time.setObjectName("toolButton_start_time")
self.horizontalLayout_2.addWidget(self.toolButton_start_time)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setObjectName("label_3")
self.horizontalLayout.addWidget(self.label_3)
self.toolButton_end_time = QtWidgets.QToolButton(Dialog)
self.toolButton_end_time.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
self.toolButton_end_time.setArrowType(QtCore.Qt.DownArrow)
self.toolButton_end_time.setObjectName("toolButton_end_time")
self.horizontalLayout.addWidget(self.toolButton_end_time)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.btn_ok = QtWidgets.QPushButton(Dialog)
self.btn_ok.setObjectName("btn_ok")
self.horizontalLayout_3.addWidget(self.btn_ok)
self.btn_cancel = QtWidgets.QPushButton(Dialog)
self.btn_cancel.setObjectName("btn_cancel")
self.horizontalLayout_3.addWidget(self.btn_cancel)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "自定义时间"))
self.label_2.setText(_translate("Dialog", "开始日期"))
self.toolButton_start_time.setText(_translate("Dialog", "请选择时间"))
self.label_3.setText(_translate("Dialog", "结束日期"))
self.toolButton_end_time.setText(_translate("Dialog", "请选择时间"))
self.btn_ok.setText(_translate("Dialog", "确定"))
self.btn_cancel.setText(_translate("Dialog", "取消"))