mainwindow.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "mainwindow.h"
  2. #include "appevent.h"
  3. #include "cwf/sqlquery.h"
  4. #include "examquestionpage.h"
  5. #include "examtestpage.h"
  6. #include "gradespage.h"
  7. #include "qwebsocket.h"
  8. #include "settingspage.h"
  9. #include "studentpage.h"
  10. #include "tmodel.h"
  11. #include "websocketserver.h"
  12. #include <QCheckBox>
  13. #include <QComboBox>
  14. #include <QDialog>
  15. #include <QHeaderView>
  16. #include <QLabel>
  17. #include <QLineEdit>
  18. #include <QStatusBar>
  19. #include <QTableView>
  20. #include <QJsonArray>
  21. #include <layoutbuilder.h>
  22. #include <cwf/constants.h>
  23. #include <cwf/cppwebapplication.h>
  24. #include <cwf/filter.h>
  25. #include <cwf/sqldatabasestorage.h>
  26. static CWF::SqlDatabaseStorage storage("QSQLITE", "localhost", "data.db", "", "");
  27. MainWindow::MainWindow(QWidget *parent)
  28. : QMainWindow(parent)
  29. {
  30. setCentralWidget(new QWidget(this));
  31. using namespace Layouting;
  32. Column{TabWidget{Tab{tr("student"), Row{new StudentPage}},
  33. Tab{tr("exam question"), Row{new ExamsQuestionPage}},
  34. Tab{tr("grades"), Row{new GradesPage}},
  35. Tab{tr("settings"), Column{new SettingsPage}}},
  36. new ExamTestPage
  37. }
  38. .attachTo(centralWidget());
  39. // 状态栏
  40. ipLabel = new QLabel;
  41. wsLabel = new QLabel;
  42. CurrentExamQuestions = new QLabel;
  43. currentOnlineNumber = new QLabel;
  44. numberOfLinks = new QLabel;
  45. webLinks = new QLabel;
  46. QStatusBar *statusBar = new QStatusBar(this);
  47. setStatusBar(statusBar);
  48. statusBar->addPermanentWidget(Form{"Ip:", ipLabel, noMargin}.emerge());
  49. statusBar->addPermanentWidget(Form{"WebSocket:", wsLabel, noMargin}.emerge());
  50. statusBar->addPermanentWidget(
  51. Form{tr("current default examination questions:"), CurrentExamQuestions, noMargin}.emerge());
  52. statusBar->addPermanentWidget(
  53. Form{tr("current online number:"), currentOnlineNumber, noMargin}.emerge());
  54. statusBar->addPermanentWidget(Form{tr("number of links:"), numberOfLinks, noMargin}.emerge());
  55. statusBar->addPermanentWidget(Form{tr("web links:"), webLinks, noMargin}.emerge());
  56. setMinimumSize({960, 600});
  57. AppEvent *appEvent = AppEvent::instance();
  58. {
  59. const QString ip = AppEvent::instance()->configValue("serverIP").toString();
  60. const int port = AppEvent::instance()->configValue("serverPort").toInt();
  61. ipLabel->setText(QString("%1:%2").arg(ip).arg(port));
  62. }
  63. {
  64. const QString ip = AppEvent::instance()->configValue("webSocketIP").toString();
  65. const int port = AppEvent::instance()->configValue("webSocketPort").toInt();
  66. wsLabel->setText(QString("%1:%2").arg(ip).arg(port));
  67. }
  68. webLinks->setText(appEvent->isLogin() ? tr("Link") : tr("no Link"));
  69. QObject::connect(appEvent, &AppEvent::loginUserSignal, [this](int) { updateStatusBar(); });
  70. QObject::connect(appEvent, &AppEvent::loginOutUserSignal, [this](int) { updateStatusBar(); });
  71. QObject::connect(appEvent, &AppEvent::examQuestionChengeSignal, [this]() { updateStatusBar(); });
  72. QObject::connect(appEvent, &AppEvent::webSocketUpdataSignal, [this]() { updateStatusBar(); });
  73. updateStatusBar();
  74. }
  75. MainWindow::~MainWindow()
  76. {
  77. AppEvent::instance()->configSave();
  78. }
  79. void MainWindow::updateStatusBar()
  80. {
  81. CWF::SqlQuery qry(storage);
  82. qint64 all = 0;
  83. {
  84. qry.exec("SELECT COUNT(*) AS total FROM user;");
  85. QJsonArray array = qry.toJson();
  86. if (!array.isEmpty()) {
  87. QJsonObject object = array[0].toObject();
  88. all = object["total"].toVariant().toLongLong();
  89. }
  90. }
  91. {
  92. qry.exec("SELECT COUNT(*) AS total FROM user where state = 1;");
  93. QJsonArray array = qry.toJson();
  94. if (!array.isEmpty()) {
  95. QJsonObject object = array[0].toObject();
  96. qint64 total = object["total"].toVariant().toLongLong();
  97. currentOnlineNumber->setText(QString("%1 / %2").arg(total).arg(all));
  98. }
  99. }
  100. // 题目获取]
  101. {
  102. ExamsQuestionModel exams(storage);
  103. CWF::SqlQueryManager queryManager(storage);
  104. queryManager.select("*", exams.getTableName()).where("isSelect = ?");
  105. queryManager.prepare();
  106. queryManager.addBindValue(1);
  107. queryManager.exec();
  108. QJsonArray jsonArray = queryManager.toJson();
  109. if (!jsonArray.isEmpty()) {
  110. QString dir;
  111. QString name;
  112. for (const QJsonValue &json : jsonArray) {
  113. if (json.isObject()) {
  114. QJsonObject obj = json.toObject();
  115. dir = obj["fileDir"].toString();
  116. name = obj["name"].toString();
  117. break;
  118. }
  119. }
  120. CurrentExamQuestions->setText(QString("%1(%2)").arg(name).arg(dir));
  121. }
  122. }
  123. QList<QWebSocket *> clients = WebSocketServer::getInstance()->clients();
  124. numberOfLinks->setText(QString::number(clients.size()));
  125. }