You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

134 lines
4.1 KiB

  1. /*******************************
  2. * Copyright (C) 2025-.
  3. *
  4. * File Name: communicationhistory.cpp
  5. * Description: 通信历史记录模块头文件,提供通信数据的保存和读取功能
  6. * Others:
  7. * Version: 1.0.0
  8. * Author: lipengpeng
  9. * Date: 2025-7-25
  10. *******************************/
  11. #include "communicationhistory.h"
  12. /**
  13. * @brief 保存文本内容到指定文件
  14. * @param parent 父窗口指针,用于对话框的模态显示
  15. * @param edit 包含待保存数据的文本编辑框指针
  16. * @return bool 保存成功返回true,用户取消或失败返回false
  17. * @note 功能流程:
  18. * 1. 弹出标准文件保存对话框
  19. * 2. 以追加模式打开文件(保留历史记录)
  20. * 3. 自动处理文件内容分隔(添加换行符)
  21. * 4. 使用UTF-8编码保存文本
  22. * 5. 错误处理包含用户提示
  23. */
  24. bool saveDate(QWidget *parent, QTextEdit *edit)
  25. {
  26. // 弹出文件保存对话框,默认保存到用户主目录的note.txt文件
  27. QString fileName = QFileDialog::getSaveFileName(
  28. parent,
  29. "保存文本", // 对话框标题
  30. QDir::homePath() + "/note.txt", // 默认路径和文件名
  31. "文本文件 (*.txt);;所有文件 (*)"); // 文件过滤器
  32. // 检查用户是否取消操作
  33. if (fileName.isEmpty())
  34. {
  35. return false;
  36. }
  37. // 以追加模式和文本模式打开文件
  38. QFile file(fileName);
  39. if (!file.open(QIODevice::Append | QIODevice::Text))
  40. {
  41. QMessageBox::warning(parent,
  42. "错误",
  43. QString("无法打开文件\n%1").arg(file.errorString()));
  44. return false;
  45. }
  46. // 如果文件已有内容,先追加换行符保持内容分隔
  47. if (file.size() > 0)
  48. {
  49. if (file.write("\n") == -1)
  50. {
  51. // 检查换行符写入是否成功
  52. QMessageBox::warning(parent, "错误", "无法写入分隔符");
  53. file.close();
  54. return false;
  55. }
  56. }
  57. // 设置UTF-8编码并写入文本内容
  58. QTextStream out(&file);
  59. out.setCodec("UTF-8");
  60. out << edit->toPlainText();
  61. // 检查流状态确保写入成功
  62. if (out.status() != QTextStream::Ok)
  63. {
  64. QMessageBox::warning(parent, "错误", "写入文件时发生错误");
  65. file.close();
  66. return false;
  67. }
  68. file.close();
  69. return true;
  70. }
  71. /**
  72. * @brief 从文件读取文本内容
  73. * @param parent 父窗口指针,用于对话框的模态显示
  74. * @param edit 用于显示读取内容的文本编辑框指针
  75. * @return bool 读取成功返回true,用户取消或失败返回false
  76. * @note 功能流程:
  77. * 1. 弹出标准文件打开对话框
  78. * 2. 以只读模式打开文件
  79. * 3. 使用UTF-8编码读取内容
  80. * 4. 错误处理包含用户提示
  81. */
  82. bool readDate(QWidget *parent, QTextEdit *edit)
  83. {
  84. // 弹出文件打开对话框,默认从用户主目录开始浏览
  85. QString fileName = QFileDialog::getOpenFileName(
  86. parent,
  87. "打开文本", // 对话框标题
  88. QDir::homePath(), // 默认路径
  89. "文本文件 (*.txt);;所有文件 (*)"); // 文件过滤器
  90. // 检查用户是否取消操作
  91. if (fileName.isEmpty())
  92. {
  93. return false;
  94. }
  95. // 以只读模式和文本模式打开文件
  96. QFile file(fileName);
  97. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  98. {
  99. QMessageBox::warning(parent,
  100. "错误",
  101. QString("无法读取文件\n%1").arg(file.errorString()));
  102. return false;
  103. }
  104. // 设置UTF-8编码并读取全部内容
  105. QTextStream in(&file);
  106. in.setCodec("UTF-8");
  107. QString content = in.readAll();
  108. // 检查流状态确保读取成功
  109. if (in.status() != QTextStream::Ok)
  110. {
  111. QMessageBox::warning(parent, "错误", "读取文件时发生错误");
  112. file.close();
  113. return false;
  114. }
  115. // 将内容设置到文本编辑框
  116. edit->setPlainText(content);
  117. file.close();
  118. return true;
  119. }