appevent.cpp 8.6 KB

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