appevent.cpp 8.7 KB

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