mainwindow.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. QStatusBar *statusBar = new QStatusBar(this);
  46. setStatusBar(statusBar);
  47. statusBar->addPermanentWidget(Form{"Ip:", ipLabel, noMargin}.emerge());
  48. statusBar->addPermanentWidget(Form{"WebSocket:", wsLabel, noMargin}.emerge());
  49. statusBar->addPermanentWidget(
  50. Form{tr("current default examination questions:"), CurrentExamQuestions, noMargin}.emerge());
  51. statusBar->addPermanentWidget(
  52. Form{tr("current online number:"), currentOnlineNumber, noMargin}.emerge());
  53. statusBar->addPermanentWidget(Form{tr("number of links:"), numberOfLinks, noMargin}.emerge());
  54. setMinimumSize({960, 600});
  55. AppEvent *appEvent = AppEvent::instance();
  56. {
  57. const QString ip = AppEvent::instance()->configValue("serverIP").toString();
  58. const int port = AppEvent::instance()->configValue("serverPort").toInt();
  59. ipLabel->setText(QString("%1:%2").arg(ip).arg(port));
  60. }
  61. {
  62. const QString ip = AppEvent::instance()->configValue("webSocketIP").toString();
  63. const int port = AppEvent::instance()->configValue("webSocketPort").toInt();
  64. wsLabel->setText(QString("%1:%2").arg(ip).arg(port));
  65. }
  66. QObject::connect(appEvent, &AppEvent::loginUserSignal, [this](int) { updateStatusBar(); });
  67. QObject::connect(appEvent, &AppEvent::loginOutUserSignal, [this](int) { updateStatusBar(); });
  68. QObject::connect(appEvent, &AppEvent::examQuestionChengeSignal, [this]() { updateStatusBar(); });
  69. QObject::connect(appEvent, &AppEvent::webSocketUpdataSignal, [this]() { updateStatusBar(); });
  70. updateStatusBar();
  71. }
  72. MainWindow::~MainWindow()
  73. {
  74. AppEvent::instance()->configSave();
  75. }
  76. void MainWindow::updateStatusBar()
  77. {
  78. CWF::SqlQuery qry(storage);
  79. qint64 all = 0;
  80. {
  81. qry.exec("SELECT COUNT(*) AS total FROM user;");
  82. QJsonArray array = qry.toJson();
  83. if (!array.isEmpty()) {
  84. QJsonObject object = array[0].toObject();
  85. all = object["total"].toVariant().toLongLong();
  86. }
  87. }
  88. {
  89. qry.exec("SELECT COUNT(*) AS total FROM user where state = 1;");
  90. QJsonArray array = qry.toJson();
  91. if (!array.isEmpty()) {
  92. QJsonObject object = array[0].toObject();
  93. qint64 total = object["total"].toVariant().toLongLong();
  94. currentOnlineNumber->setText(QString("%1 / %2").arg(total).arg(all));
  95. }
  96. }
  97. // 题目获取]
  98. {
  99. ExamsQuestionModel exams(storage);
  100. CWF::SqlQueryManager queryManager(storage);
  101. queryManager.select("*", exams.getTableName()).where("isSelect = ?");
  102. queryManager.prepare();
  103. queryManager.addBindValue(1);
  104. queryManager.exec();
  105. QJsonArray jsonArray = queryManager.toJson();
  106. if (!jsonArray.isEmpty()) {
  107. QString dir;
  108. QString name;
  109. for (const QJsonValue &json : jsonArray) {
  110. if (json.isObject()) {
  111. QJsonObject obj = json.toObject();
  112. dir = obj["fileDir"].toString();
  113. name = obj["name"].toString();
  114. break;
  115. }
  116. }
  117. CurrentExamQuestions->setText(QString("%1(%2)").arg(name).arg(dir));
  118. }
  119. }
  120. QList<QWebSocket *> clients = WebSocketServer::getInstance()->clients();
  121. numberOfLinks->setText(QString::number(clients.size()));
  122. }