appevent.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "appevent.h"
  2. #include "qdir.h"
  3. #include "qglobal.h"
  4. #include "qjsonobject.h"
  5. #include "qobject.h"
  6. #include <QByteArray>
  7. #include <QCryptographicHash>
  8. #include <QDateTime>
  9. #include <QFile>
  10. #include <QGuiApplication>
  11. #include <QJsonDocument>
  12. #include <QPainter>
  13. #include <QPixmap>
  14. #include <QScreen>
  15. #include "thardware/thardware.h"
  16. static AppEvent *g_instance = nullptr;
  17. static QString computeMD5(const QString &input)
  18. {
  19. // Convert the input string to a QByteArray
  20. QByteArray byteArray = input.toUtf8();
  21. // Compute the MD5 hash using QCryptographicHash
  22. QByteArray hash = QCryptographicHash::hash(byteArray, QCryptographicHash::Md5);
  23. // Convert the QByteArray hash to a hexadecimal string
  24. QString hexHash = hash.toHex();
  25. return hexHash;
  26. }
  27. class AppEventPrivate
  28. {
  29. public:
  30. AppEventPrivate() {}
  31. QString token;
  32. qint64 accessExpire;
  33. QJsonObject jsonConfig;
  34. qint64 maxExamTime;
  35. QString machineCode;
  36. };
  37. AppEvent::AppEvent(QObject *parent)
  38. : QObject{parent}
  39. , d(new AppEventPrivate)
  40. {
  41. const std::string code = TC::THardWare::machineCode();
  42. d->machineCode = computeMD5(QString::fromStdString(code));
  43. // 考题存放路径
  44. d->jsonConfig.insert("serverIP", "127.0.0.1");
  45. d->jsonConfig.insert("serverPort", "8080");
  46. d->jsonConfig.insert("webSocketIP", "127.0.0.1");
  47. d->jsonConfig.insert("webSocketPort", "8081");
  48. }
  49. AppEvent::~AppEvent()
  50. {
  51. delete d;
  52. }
  53. AppEvent *AppEvent::instance()
  54. {
  55. if (!g_instance) {
  56. g_instance = new AppEvent();
  57. }
  58. return g_instance;
  59. }
  60. qint64 AppEvent::time()
  61. {
  62. QDateTime currentDateTime = QDateTime::currentDateTime();
  63. qint64 timestamp = currentDateTime.toSecsSinceEpoch();
  64. timestamp /= 1000; // 转换为10位时间戳
  65. return timestamp;
  66. }
  67. QString AppEvent::formatElapsedTime(qint64 ms)
  68. {
  69. // 构建QTime对象
  70. QTime time(0, 0, 0);
  71. time = time.addMSecs(ms);
  72. // 格式化成时:分:秒:毫秒格式
  73. return time.toString("hh:mm:ss");
  74. }
  75. void AppEvent::captureDesktop(QIODevice *device, const char *format, int quality)
  76. {
  77. QList<QScreen *> screens = QGuiApplication::screens();
  78. if (screens.isEmpty()) {
  79. qWarning("No screens found.");
  80. return;
  81. }
  82. // 计算整个桌面的大小
  83. QRect fullDesktopRect;
  84. for (QScreen *screen : screens) {
  85. fullDesktopRect = fullDesktopRect.united(screen->geometry());
  86. }
  87. // 创建一个空白的 QPixmap,大小为整个桌面的大小
  88. QPixmap fullDesktop(fullDesktopRect.size());
  89. fullDesktop.fill(Qt::transparent);
  90. // 使用 QPainter 将每个屏幕的内容绘制到 fullDesktop 上
  91. QPainter painter(&fullDesktop);
  92. for (QScreen *screen : screens) {
  93. // 计算当前屏幕在 fullDesktop 中的位置
  94. QPoint screenPos = screen->geometry().topLeft() - fullDesktopRect.topLeft();
  95. painter.drawPixmap(screenPos, screen->grabWindow(0));
  96. }
  97. painter.end();
  98. fullDesktop.save(device, format, quality);
  99. }
  100. void AppEvent::captureDesktop(const QString &path)
  101. {
  102. QList<QScreen *> screens = QGuiApplication::screens();
  103. if (screens.isEmpty()) {
  104. qWarning("No screens found.");
  105. return;
  106. }
  107. // 计算整个桌面的大小
  108. QRect fullDesktopRect;
  109. for (QScreen *screen : screens) {
  110. fullDesktopRect = fullDesktopRect.united(screen->geometry());
  111. }
  112. // 创建一个空白的 QPixmap,大小为整个桌面的大小
  113. QPixmap fullDesktop(fullDesktopRect.size());
  114. fullDesktop.fill(Qt::transparent);
  115. // 使用 QPainter 将每个屏幕的内容绘制到 fullDesktop 上
  116. QPainter painter(&fullDesktop);
  117. for (QScreen *screen : screens) {
  118. // 计算当前屏幕在 fullDesktop 中的位置
  119. QPoint screenPos = screen->geometry().topLeft() - fullDesktopRect.topLeft();
  120. painter.drawPixmap(screenPos, screen->grabWindow(0));
  121. }
  122. painter.end();
  123. // 保存截图到文件(可选)
  124. QString fileName = "/desktop_screenshot_"
  125. + QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss") + ".png";
  126. fullDesktop.save(path + fileName, "PNG");
  127. }
  128. void AppEvent::saveTimeFile(qint64 elapsed, const QString &path)
  129. {
  130. QFile file(path + "/time.txt");
  131. if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  132. QTextStream stream(&file);
  133. QString time = formatElapsedTime(elapsed); // Assuming formatElapsedTime is defined somewhere
  134. stream << "time: " << time << Qt::endl;
  135. stream << "elapsed(ns): " << elapsed << Qt::endl;
  136. file.close();
  137. }
  138. }
  139. QString AppEvent::machineCode() const
  140. {
  141. return d->machineCode;
  142. }
  143. bool AppEvent::isLogin()
  144. {
  145. return !d->token.isEmpty();
  146. }
  147. void AppEvent::setJwtToken(const QString &token)
  148. {
  149. d->token = token;
  150. }
  151. QString AppEvent::jwtToken() const
  152. {
  153. return d->token;
  154. }
  155. bool AppEvent::isRefreshToken() const
  156. {
  157. if (time() > d->accessExpire && !d->token.isEmpty()) {
  158. return true;
  159. }
  160. return false;
  161. }
  162. void AppEvent::setRefreshTime(qint64 time) const
  163. {
  164. d->accessExpire = time;
  165. }
  166. void AppEvent::startExam(int time)
  167. {
  168. emit startExamSignals(time);
  169. }
  170. qint64 AppEvent::maxExamTime()
  171. {
  172. return d->maxExamTime;
  173. }
  174. void AppEvent::setMaxExamTime(qint64 time)
  175. {
  176. d->maxExamTime = time;
  177. }
  178. bool AppEvent::setConfigValue(const QString &key, const QVariant &value)
  179. {
  180. if (d->jsonConfig.contains(key)) {
  181. d->jsonConfig[key] = value.toJsonValue();
  182. } else {
  183. d->jsonConfig.insert(key, value.toJsonValue());
  184. }
  185. return true;
  186. }
  187. QVariant AppEvent::configValue(const QString &key)
  188. {
  189. return d->jsonConfig.value(key).toVariant();
  190. }
  191. QVariant AppEvent::configReadValue(const QString &key, bool *isok)
  192. {
  193. QFile file("config.json");
  194. QByteArray data;
  195. if (file.open(QFile::ReadOnly | QFile::Text)) {
  196. data = file.readAll();
  197. file.close();
  198. }
  199. QJsonParseError jsonError;
  200. QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
  201. if (jsonError.error != QJsonParseError::NoError) {
  202. if (isok) {
  203. *isok = false;
  204. }
  205. return QVariant();
  206. }
  207. const QJsonObject object = jsonDoc.object();
  208. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  209. if (it.key() == key) {
  210. if (isok) {
  211. *isok = true;
  212. }
  213. return object[key];
  214. }
  215. }
  216. if (isok) {
  217. *isok = false;
  218. }
  219. return QVariant();
  220. }
  221. bool AppEvent::configLoadJson(const QJsonObject &object)
  222. {
  223. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  224. const QString key = it.key();
  225. if (d->jsonConfig.contains(key)) {
  226. d->jsonConfig[key] = object[key];
  227. } else {
  228. d->jsonConfig.insert(key, object[key]);
  229. }
  230. }
  231. qDebug() << d->jsonConfig;
  232. return true;
  233. }
  234. bool AppEvent::configLoad()
  235. {
  236. //
  237. QFile file("config.json");
  238. QByteArray data;
  239. if (file.open(QFile::ReadOnly | QFile::Text)) {
  240. data = file.readAll();
  241. file.close();
  242. }
  243. QJsonParseError jsonError;
  244. QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
  245. if (jsonError.error != QJsonParseError::NoError) {
  246. return false;
  247. }
  248. const QJsonObject object = jsonDoc.object();
  249. for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
  250. const QString key = it.key();
  251. if (d->jsonConfig.contains(key)) {
  252. d->jsonConfig[key] = object[key];
  253. } else {
  254. d->jsonConfig.insert(key, object[key]);
  255. }
  256. }
  257. return true;
  258. }
  259. bool AppEvent::configSave()
  260. {
  261. QJsonDocument jsonDoc(d->jsonConfig);
  262. QString jsonString = jsonDoc.toJson(QJsonDocument::Indented);
  263. QFile file("config.json");
  264. QByteArray data;
  265. if (file.open(QFile::WriteOnly | QFile::Text)) {
  266. file.write(jsonString.toUtf8());
  267. file.close();
  268. }
  269. return true;
  270. }
  271. QJsonObject AppEvent::config() const
  272. {
  273. return d->jsonConfig;
  274. }
  275. void AppEvent::serverLink(bool b)
  276. {
  277. emit serverLinkSignals(b);
  278. }
  279. void AppEvent::unLockScreen()
  280. {
  281. emit unLockScreenSignals();
  282. }