appevent.cpp 8.9 KB

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