MainWindow.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include "MainWindow.h"
  2. #include "CustomTitleBar.h"
  3. #include "HomeView.h"
  4. #include "ProjectView.h"
  5. #include "ExpertManageView.h"
  6. #include "EvaluateView.h"
  7. #include "LoginView.h"
  8. #include "AboutView.h"
  9. #include "SettingView.h"
  10. #include "UserView.h"
  11. #include "ProjectManager.h"
  12. #include "dbService/ClassSet.h"
  13. #include "QFDConfig.h"
  14. #include "common/QFDIcon.h"
  15. #include "QFDAlert.h"
  16. #include <QFramelessWindow.h>
  17. #include <Navigation/NavigationInterface.h>
  18. #include <Widgets/StackedWidget.h>
  19. #include <QApplication>
  20. #include <QDebug>
  21. StackedWidget::StackedWidget(QWidget *parent) : QFrame(parent)
  22. {
  23. m_hBoxLayout = new QHBoxLayout(this);
  24. m_view = new PopUpAniStackedWidget(this);
  25. m_hBoxLayout->setContentsMargins(0, 0, 0, 0);
  26. m_hBoxLayout->addWidget(m_view);
  27. connect(m_view, &PopUpAniStackedWidget::currentChanged, this,
  28. [this](int index) { emit currentWidgetChanged(m_view->widget(index)); });
  29. QPalette pal(palette());
  30. pal.setColor(QPalette::Background, QColor("#fffff8"));
  31. setAutoFillBackground(true);
  32. setPalette(pal);
  33. }
  34. void StackedWidget::addWidget(QWidget *widget)
  35. {
  36. m_view->addWidget(widget);
  37. }
  38. QWidget *StackedWidget::widget(int index) const
  39. {
  40. return m_view->widget(index);
  41. }
  42. void StackedWidget::removeWidget(QWidget *widget)
  43. {
  44. m_view->removeWidget(widget);
  45. }
  46. ///
  47. /// @todo widget.verticalScrollBar().setValue(0)
  48. ///
  49. void StackedWidget::setCurrentWidget(QWidget *widget, bool popOut)
  50. {
  51. if (popOut) {
  52. m_view->setCurrentWidget(widget, true, false, 200, QEasingCurve::InQuad);
  53. } else {
  54. m_view->setCurrentWidget(widget, false, true, 300);
  55. }
  56. }
  57. void StackedWidget::setCurrentIndex(int index, bool popOut)
  58. {
  59. if (index < 0 || index >= view()->count()) {
  60. return;
  61. }
  62. setCurrentWidget(m_view->widget(index), popOut);
  63. }
  64. PopUpAniStackedWidget *StackedWidget::view() const
  65. {
  66. return m_view;
  67. }
  68. int StackedWidget::index(const QString routeKey)
  69. {
  70. for (int i = 0; i < view()->count(); i++) {
  71. QWidget *w = view()->widget(i);
  72. if (w->objectName() == routeKey) {
  73. return i;
  74. }
  75. }
  76. return -1;
  77. }
  78. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  79. {
  80. m_titleBar = new CustomTitleBar(this);
  81. QFramelessHelper *helper = new QFramelessHelper(this);
  82. helper->setTitleBar(m_titleBar);
  83. setWindowTitle("伪装防护评估系统软件");
  84. QIcon icon = QIcon(":/resource/logo.png");
  85. qApp->setWindowIcon(icon);
  86. m_titleBar->setIcon(icon);
  87. m_mainStackedWidget = new QStackedWidget(this);
  88. setCentralWidget(m_mainStackedWidget);
  89. initLoginPage();
  90. setMinimumSize(QSize(1200, 900));
  91. }
  92. MainWindow::~MainWindow() { }
  93. void MainWindow::initLoginPage()
  94. {
  95. m_loginView = new LoginView(this);
  96. m_mainStackedWidget->addWidget(m_loginView);
  97. connect(m_loginView, &LoginView::signalLogin, this, &MainWindow::slotLogin);
  98. connect(m_loginView, &LoginView::signalCancelLogin, this, &MainWindow::slotCancelLogin);
  99. }
  100. void MainWindow::initMainPage()
  101. {
  102. m_mainWidget = new QWidget(this);
  103. m_hBoxLayout = new QHBoxLayout(m_mainWidget);
  104. m_widgetLayout = new QHBoxLayout();
  105. m_stackWidget = new StackedWidget(m_mainWidget);
  106. m_naviInterface = new NavigationInterface(true, true, m_mainWidget);
  107. m_homeView = new HomeView(m_mainWidget);
  108. m_aboutView = new AboutView(m_mainWidget);
  109. m_settingView = new SettingView(m_mainWidget);
  110. m_userView = new UserView(m_mainWidget);
  111. m_mainStackedWidget->addWidget(m_mainWidget);
  112. m_hBoxLayout->setSpacing(0);
  113. m_hBoxLayout->setContentsMargins(0, 0, 0, 0);
  114. m_hBoxLayout->addWidget(m_naviInterface);
  115. m_hBoxLayout->addLayout(m_widgetLayout);
  116. m_hBoxLayout->setStretchFactor(m_widgetLayout, 1);
  117. m_widgetLayout->addWidget(m_stackWidget);
  118. m_widgetLayout->setContentsMargins(0, 32, 0, 0);
  119. initNavigation();
  120. m_naviInterface->panel->expand();
  121. connect(m_naviInterface, &NavigationInterface::displayModeChanged, m_titleBar, &StandardTitleBar::raise);
  122. connect(m_userView, &UserView::signalLogout, this, &MainWindow::slotLogout);
  123. connect(m_homeView, &HomeView::sigOpenProject, this, &MainWindow::slotOpenProject);
  124. connect(m_homeView, &HomeView::sigDeleteProject, this, &MainWindow::slotDeleteProject);
  125. }
  126. void MainWindow::initNavigation()
  127. {
  128. m_homeView->setObjectName("homeView");
  129. m_stackWidget->addWidget(m_homeView);
  130. m_naviInterface->addItem(m_homeView->objectName(), NEWFLICON(FluentIcon, HOME), "评估项目管理", this,
  131. SLOT(slotNaviItemClicked()));
  132. m_settingView->setObjectName("settingView");
  133. m_stackWidget->addWidget(m_settingView);
  134. m_naviInterface->addItem(m_settingView->objectName(), NEWFLICON(FluentIcon, SETTING), "系统资源管理", this,
  135. SLOT(slotNaviItemClicked()));
  136. m_naviInterface->addSeparator();
  137. m_userView->setObjectName("userView");
  138. m_stackWidget->addWidget(m_userView);
  139. m_naviInterface->addItem(m_userView->objectName(), NEWFLICON(QFDIcon, User), "用户", this,
  140. SLOT(slotNaviItemClicked()), true, NavigationItemPosition::BOTTOM);
  141. m_aboutView->setObjectName("aboutView");
  142. m_stackWidget->addWidget(m_aboutView);
  143. m_naviInterface->addItem(m_aboutView->objectName(), NEWFLICON(FluentIcon, INFO), "关于", this,
  144. SLOT(slotNaviItemClicked()), true, NavigationItemPosition::BOTTOM);
  145. connect(m_stackWidget, &StackedWidget::currentWidgetChanged, this,
  146. [this](QWidget *w) { m_naviInterface->setCurrentItem(w->objectName()); });
  147. m_naviInterface->setDefaultRouteKey(m_homeView->objectName());
  148. m_naviInterface->setCurrentItem(m_homeView->objectName());
  149. m_stackWidget->setCurrentIndex(0);
  150. }
  151. void MainWindow::setNaviInterfaceHidden(bool hidden)
  152. {
  153. m_naviInterface->setHidden(hidden);
  154. m_titleBar->setGeometry(QRect(titleBarIndent(), 0, width() - titleBarIndent(), m_titleBar->height()));
  155. }
  156. int MainWindow::titleBarIndent() const
  157. {
  158. if (m_mainStackedWidget->currentIndex() > 0 && m_naviInterface != nullptr && m_naviInterface->isHidden() == false) {
  159. return 48;
  160. }
  161. return 0;
  162. }
  163. void MainWindow::resizeEvent(QResizeEvent *event)
  164. {
  165. Q_UNUSED(event)
  166. m_titleBar->resize(width() - titleBarIndent(), m_titleBar->height());
  167. }
  168. void MainWindow::slotLogin()
  169. {
  170. initMainPage();
  171. m_mainStackedWidget->setCurrentWidget(m_mainWidget);
  172. showMaximized();
  173. m_titleBar->setGeometry(QRect(titleBarIndent(), 0, width() - titleBarIndent(), m_titleBar->height()));
  174. }
  175. void MainWindow::slotLogout()
  176. {
  177. qfReloadHomeProjectsAtShow = true;
  178. m_mainStackedWidget->setCurrentWidget(m_loginView);
  179. m_mainStackedWidget->removeWidget(m_mainWidget);
  180. delete m_mainWidget;
  181. m_mainWidget = nullptr;
  182. showNormal();
  183. m_titleBar->setGeometry(QRect(titleBarIndent(), 0, width() - titleBarIndent(), m_titleBar->height()));
  184. }
  185. void MainWindow::slotCancelLogin()
  186. {
  187. exit(0);
  188. }
  189. void MainWindow::slotNaviItemClicked()
  190. {
  191. int index = m_stackWidget->index(sender()->property("routeKey").toString());
  192. m_stackWidget->setCurrentIndex(index);
  193. }
  194. void MainWindow::slotOpenProject(ProjectInfo *proj)
  195. {
  196. QList<ProjectManager::EvalType> l = ProjectManager::evalTypeList(*proj);
  197. if (l.count() <= 0) {
  198. QFDAlert::showAlertWithCode(QF_CODE_EVAL_NOT_SUPPORTED, this);
  199. return;
  200. }
  201. QString n = QString("project_%1").arg(proj->id);
  202. int index = m_stackWidget->index(n);
  203. if (index >= 0) {
  204. m_stackWidget->setCurrentIndex(index);
  205. } else {
  206. ProjectView *p = new ProjectView(proj, m_mainWidget);
  207. p->setObjectName(n);
  208. m_stackWidget->addWidget(p);
  209. QString t = QString("打开项目-%1").arg(proj->projectName);
  210. m_naviInterface->addItem(p->objectName(), NEWFLICON(QFDIcon, Project1), t, this, SLOT(slotNaviItemClicked()),
  211. true, NavigationItemPosition::SCROLL);
  212. m_stackWidget->setCurrentWidget(p);
  213. connect(p, &ProjectView::sigClose, this, &MainWindow::slotCloseProject);
  214. }
  215. }
  216. void MainWindow::slotCloseProject()
  217. {
  218. ProjectView *p = dynamic_cast<ProjectView *>(sender());
  219. m_stackWidget->removeWidget(p);
  220. m_naviInterface->removeWidget(sender()->objectName());
  221. m_stackWidget->setCurrentIndex(0);
  222. }
  223. void MainWindow::slotDeleteProject(ProjectInfo *proj)
  224. {
  225. QString n = QString("project_%1").arg(proj->id);
  226. int index = m_stackWidget->index(n);
  227. if (index >= 0) {
  228. QWidget *w = m_stackWidget->widget(index);
  229. m_stackWidget->removeWidget(w);
  230. m_naviInterface->removeWidget(n);
  231. m_stackWidget->setCurrentIndex(0);
  232. }
  233. }