appevent.cpp 8.3 KB

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