QFDApp.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #include "QFDApp.h"
  2. #include "ui_QFDApp.h"
  3. #include "CustomGrip.h"
  4. #include <QPropertyAnimation>
  5. #include <QTimer>
  6. #include <QDebug>
  7. #include <QParallelAnimationGroup>
  8. class Settings
  9. {
  10. public:
  11. static bool ENABLE_CUSTOM_TITLE_BAR;
  12. static int MENU_WIDTH;
  13. static int LEFT_BOX_WIDTH;
  14. static int RIGHT_BOX_WIDTH;
  15. static int TIME_ANIMATION;
  16. // BTNS LEFT AND RIGHT BOX COLORS
  17. static QString BTN_LEFT_BOX_COLOR;
  18. static QString BTN_RIGHT_BOX_COLOR;
  19. // MENU SELECTED STYLESHEET
  20. static QString MENU_SELECTED_STYLESHEET;
  21. };
  22. bool Settings::ENABLE_CUSTOM_TITLE_BAR = true;
  23. int Settings::MENU_WIDTH = 240;
  24. int Settings::LEFT_BOX_WIDTH = 240;
  25. int Settings::RIGHT_BOX_WIDTH = 240;
  26. int Settings::TIME_ANIMATION = 500;
  27. QString Settings::BTN_LEFT_BOX_COLOR = "background-color: rgb(44, 49, 58);";
  28. QString Settings::BTN_RIGHT_BOX_COLOR = "background-color: #ff79c6;";
  29. QString Settings::MENU_SELECTED_STYLESHEET =
  30. "border-left: 22px solid qlineargradient(spread:pad, x1:0.034, y1:0, x2:0.216, y2:0, stop:0.499 rgba(255, 121, "
  31. "198, 255), stop:0.5 rgba(85, 170, 255, 0)); background-color: rgb(40, 44, 52);";
  32. static bool GLOBAL_STATE = false;
  33. static bool GLOBAL_TITLE_BAR = true;
  34. QFDApp::QFDApp(QWidget *parent) : QMainWindow(parent), ui(new Ui::QFDApp)
  35. {
  36. ui->setupUi(this);
  37. init();
  38. }
  39. QFDApp::~QFDApp()
  40. {
  41. delete ui;
  42. }
  43. /**
  44. * @brief TOGGLE MENU
  45. * @param checked
  46. */
  47. void QFDApp::toggleMenu()
  48. {
  49. // GET WIDTH
  50. int width = ui->leftMenuBg->width();
  51. // qDebug() << width;
  52. int maxExtend = Settings::MENU_WIDTH;
  53. int widthExtended = 60; // default
  54. // SET MAX WIDTH
  55. if (width == 60) {
  56. widthExtended = maxExtend;
  57. }
  58. QPropertyAnimation *animation = new QPropertyAnimation(ui->leftMenuBg, QByteArray("minimumWidth"));
  59. animation->setDuration(Settings::TIME_ANIMATION);
  60. animation->setStartValue(width);
  61. animation->setEndValue(widthExtended);
  62. animation->setEasingCurve(QEasingCurve::InOutQuart);
  63. animation->start(QAbstractAnimation::DeleteWhenStopped);
  64. }
  65. void QFDApp::maximizeRestore()
  66. {
  67. if (!GLOBAL_STATE) {
  68. this->showMaximized();
  69. GLOBAL_STATE = true;
  70. ui->appMargins->setContentsMargins(0, 0, 0, 0);
  71. ui->maximizeRestoreAppBtn->setToolTip("向下还原");
  72. ui->maximizeRestoreAppBtn->setIcon(QIcon(":/res/icons/icon_restore.png"));
  73. ui->frame_size_grip->hide();
  74. this->m_leftGrip->hide();
  75. this->m_rightGrip->hide();
  76. this->m_topGrip->hide();
  77. this->m_bottomGrip->hide();
  78. } else {
  79. GLOBAL_STATE = false;
  80. this->showNormal();
  81. this->resize(this->width() + 1, this->height() + 1);
  82. ui->appMargins->setContentsMargins(10, 10, 10, 10);
  83. ui->maximizeRestoreAppBtn->setToolTip("最大化");
  84. ui->maximizeRestoreAppBtn->setIcon(QIcon(":/res/icons/icon_maximize.png"));
  85. ui->frame_size_grip->show();
  86. this->m_leftGrip->show();
  87. this->m_rightGrip->show();
  88. this->m_topGrip->show();
  89. this->m_bottomGrip->show();
  90. }
  91. }
  92. void QFDApp::leftMenuButtonClick()
  93. {
  94. QPushButton *btn = qobject_cast<QPushButton *>(sender());
  95. QString btnName = btn->objectName();
  96. if (btnName == "btn_home") {
  97. ui->stackedWidget->setCurrentWidget(ui->home);
  98. this->resetStyle(btnName);
  99. btn->setStyleSheet(this->selectMenu(btn->styleSheet()));
  100. }
  101. if (btnName == "btn_widgets") {
  102. ui->stackedWidget->setCurrentWidget(ui->widgets);
  103. this->resetStyle(btnName);
  104. btn->setStyleSheet(this->selectMenu(btn->styleSheet()));
  105. }
  106. if (btnName == "btn_new") {
  107. ui->stackedWidget->setCurrentWidget(ui->new_page);
  108. this->resetStyle(btnName);
  109. btn->setStyleSheet(this->selectMenu(btn->styleSheet()));
  110. }
  111. if (btnName == "btn_save") {
  112. qDebug() << "Save BTN clicked!";
  113. }
  114. qDebug() << "Button " + btnName + " clicked!";
  115. }
  116. void QFDApp::openCloseLeftBox()
  117. {
  118. // GET WIDTH
  119. int width = ui->extraLeftBox->width();
  120. int widthRightBox = ui->extraRightBox->width();
  121. // int maxExtend = Settings::LEFT_BOX_WIDTH;
  122. QString color = Settings::BTN_LEFT_BOX_COLOR;
  123. // int standard = 0;
  124. // GET BTN STYLE
  125. QString style = ui->toggleLeftBox->styleSheet();
  126. // SET MAX WIDTH
  127. if (width == 0) {
  128. // SELECT BTN
  129. ui->toggleLeftBox->setStyleSheet(style + color);
  130. if (widthRightBox != 0) {
  131. style = ui->settingsTopBtn->styleSheet();
  132. ui->settingsTopBtn->setStyleSheet(style.replace(Settings::BTN_LEFT_BOX_COLOR, ""));
  133. }
  134. } else {
  135. // RESET BTN
  136. ui->toggleLeftBox->setStyleSheet(style.replace(color, ""));
  137. }
  138. this->startBoxAnimation(width, widthRightBox, "left");
  139. }
  140. void QFDApp::openCloseRightBox()
  141. { // GET WIDTH
  142. int width = ui->extraRightBox->width();
  143. int widthLeftBox = ui->extraLeftBox->width();
  144. int maxExtend = Settings::RIGHT_BOX_WIDTH;
  145. QString color = Settings::BTN_RIGHT_BOX_COLOR;
  146. int standard = 0;
  147. // GET BTN STYLE
  148. QString style = ui->settingsTopBtn->styleSheet();
  149. // SET MAX WIDTH
  150. if (width == 0) {
  151. // SELECT BTN
  152. ui->settingsTopBtn->setStyleSheet(style + color);
  153. if (widthLeftBox != 0) {
  154. style = ui->toggleLeftBox->styleSheet();
  155. ui->toggleLeftBox->setStyleSheet(style.replace(Settings::BTN_LEFT_BOX_COLOR, ""));
  156. }
  157. } else {
  158. // RESET BTN
  159. ui->settingsTopBtn->setStyleSheet(style.replace(color, ""));
  160. }
  161. this->startBoxAnimation(widthLeftBox, width, "right");
  162. }
  163. void QFDApp::init()
  164. {
  165. // USE CUSTOM TITLE BAR | USE AS "False" FOR MAC OR LINUX
  166. Settings::ENABLE_CUSTOM_TITLE_BAR = true;
  167. // APP NAME
  168. QString title = "QFD - 专家决策软件";
  169. QString description = "QFD APP - Dracula主题风格.";
  170. this->setWindowTitle(title);
  171. ui->titleRightInfo->setText(description);
  172. // TOGGLE MENU
  173. connect(ui->toggleButton, &QPushButton::clicked, this, &QFDApp::toggleMenu);
  174. uiDefinitions();
  175. // QTableWidget PARAMETERS
  176. ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  177. // LEFT MENUS
  178. connect(ui->btn_home, &QPushButton::clicked, this, &QFDApp::leftMenuButtonClick);
  179. connect(ui->btn_widgets, &QPushButton::clicked, this, &QFDApp::leftMenuButtonClick);
  180. connect(ui->btn_new, &QPushButton::clicked, this, &QFDApp::leftMenuButtonClick);
  181. connect(ui->btn_save, &QPushButton::clicked, this, &QFDApp::leftMenuButtonClick);
  182. // EXTRA LEFT BOX
  183. connect(ui->toggleLeftBox, &QPushButton::clicked, this, &QFDApp::openCloseLeftBox);
  184. connect(ui->extraCloseColumnBtn, &QPushButton::clicked, this, &QFDApp::openCloseLeftBox);
  185. // EXTRA RIGHT BOX
  186. connect(ui->settingsTopBtn, &QPushButton::clicked, this, &QFDApp::openCloseRightBox);
  187. // SET CUSTOM THEME
  188. bool useCustomTheme = false;
  189. QString themeFile = "themes\\py_dracula_light.qss";
  190. // SET THEME AND HACKS
  191. if (useCustomTheme) {
  192. // LOAD AND APPLY STYLE
  193. this->loadTheme(themeFile, true);
  194. // SET HACKS
  195. this->setThemeHack();
  196. }
  197. // SET HOME PAGE AND SELECT MENU
  198. ui->stackedWidget->setCurrentWidget(ui->home);
  199. ui->btn_home->setStyleSheet(this->selectMenu(ui->btn_home->styleSheet()));
  200. }
  201. void QFDApp::loadTheme(const QString &file, bool useCustomTheme)
  202. {
  203. if (useCustomTheme) {
  204. QFile qssFile(file);
  205. if (qssFile.open(QIODevice::ReadOnly)) {
  206. QTextStream in(&qssFile);
  207. in.setCodec("UTF-8");
  208. this->setStyle(nullptr);
  209. this->setStyleSheet(in.readAll());
  210. qssFile.close();
  211. }
  212. }
  213. }
  214. void QFDApp::setThemeHack()
  215. {
  216. Settings::BTN_LEFT_BOX_COLOR = "background-color: #495474;";
  217. Settings::BTN_RIGHT_BOX_COLOR = "background-color: #495474;";
  218. Settings::MENU_SELECTED_STYLESHEET =
  219. "border-left: 22px solid qlineargradient(spread:pad, x1:0.034, y1:0, x2:0.216, y2:0, stop:0.499 rgba(255, "
  220. "121, 198, 255), stop:0.5 rgba(85, 170, 255, 0)); background-color: #566388;";
  221. // SET MANUAL STYLES
  222. ui->lineEdit->setStyleSheet("background-color: #6272a4;");
  223. ui->pushButton->setStyleSheet("background-color: #6272a4;");
  224. ui->plainTextEdit->setStyleSheet("background-color: #6272a4;");
  225. ui->tableWidget->setStyleSheet(
  226. "QScrollBar:vertical { background: #6272a4; } QScrollBar:horizontal { background: #6272a4; }");
  227. ui->scrollArea->setStyleSheet(
  228. "QScrollBar:vertical { background: #6272a4; } QScrollBar:horizontal { background: #6272a4; }");
  229. ui->comboBox->setStyleSheet("background-color: #6272a4;");
  230. ui->horizontalScrollBar->setStyleSheet("background-color: #6272a4;");
  231. ui->verticalScrollBar->setStyleSheet("background-color: #6272a4;");
  232. ui->commandLinkButton->setStyleSheet("color: #ff79c6;");
  233. }
  234. void QFDApp::uiDefinitions()
  235. {
  236. ui->settingsTopBtn->setToolTip("右侧设置");
  237. ui->minimizeAppBtn->setToolTip("最小化");
  238. ui->maximizeRestoreAppBtn->setToolTip("最大化");
  239. ui->closeAppBtn->setToolTip("关闭");
  240. ui->toggleLeftBox->setToolTip("设置");
  241. ui->titleRightInfo->installEventFilter(this);
  242. if (Settings::ENABLE_CUSTOM_TITLE_BAR) {
  243. // STANDARD TITLE BAR
  244. this->setWindowFlags(Qt::FramelessWindowHint);
  245. this->setAttribute(Qt::WA_TranslucentBackground);
  246. // CUSTOM GRIPS
  247. this->m_leftGrip = new CustomGrip(this, Qt::LeftEdge, true);
  248. this->m_rightGrip = new CustomGrip(this, Qt::RightEdge, true);
  249. this->m_topGrip = new CustomGrip(this, Qt::TopEdge, true);
  250. this->m_bottomGrip = new CustomGrip(this, Qt::BottomEdge, true);
  251. } else {
  252. ui->appMargins->setContentsMargins(0, 0, 0, 0);
  253. ui->minimizeAppBtn->hide();
  254. ui->maximizeRestoreAppBtn->hide();
  255. ui->closeAppBtn->hide();
  256. ui->frame_size_grip->hide();
  257. }
  258. // DROP SHADOW
  259. this->shadow = new QGraphicsDropShadowEffect(this);
  260. this->shadow->setBlurRadius(17);
  261. this->shadow->setXOffset(0);
  262. this->shadow->setYOffset(0);
  263. this->shadow->setColor(QColor(0, 0, 0, 150));
  264. ui->bgApp->setGraphicsEffect(this->shadow);
  265. // RESIZE WINDOW
  266. this->sizegrip = new QSizeGrip(ui->frame_size_grip);
  267. this->sizegrip->setStyleSheet("width: 20px; height: 20px; margin 0px; padding: 0px;");
  268. // MINIMIZE
  269. connect(ui->minimizeAppBtn, &QPushButton::clicked, this, &QFDApp::showMinimized);
  270. // MAXIMIZE/RESTORE
  271. connect(ui->maximizeRestoreAppBtn, &QPushButton::clicked, this, &QFDApp::maximizeRestore);
  272. // CLOSE APPLICATION
  273. connect(ui->closeAppBtn, &QPushButton::clicked, this, &QFDApp::close);
  274. }
  275. bool QFDApp::returStatus()
  276. {
  277. return GLOBAL_STATE;
  278. }
  279. void QFDApp::setStatus(bool status)
  280. {
  281. GLOBAL_STATE = status;
  282. }
  283. QString QFDApp::selectMenu(const QString &getStyle)
  284. {
  285. QString select = getStyle + Settings::MENU_SELECTED_STYLESHEET;
  286. return select;
  287. }
  288. QString QFDApp::deselectMenu(const QString &getStyle)
  289. {
  290. QString style = getStyle;
  291. QString deselect = style.replace(Settings::MENU_SELECTED_STYLESHEET, "");
  292. return deselect;
  293. }
  294. /**
  295. * @brief START SELECTION
  296. * @param widget
  297. */
  298. void QFDApp::selectStandardMenu(const QString &widget)
  299. {
  300. for (auto w : ui->topMenu->findChildren<QPushButton *>()) {
  301. if (w->objectName() != widget) {
  302. w->setStyleSheet(this->selectMenu(w->styleSheet()));
  303. }
  304. }
  305. }
  306. /**
  307. * @brief RESET SELECTION
  308. * @param widget
  309. */
  310. void QFDApp::resetStyle(const QString &widget)
  311. {
  312. for (auto w : ui->topMenu->findChildren<QPushButton *>()) {
  313. if (w->objectName() != widget) {
  314. w->setStyleSheet(this->deselectMenu(w->styleSheet()));
  315. }
  316. }
  317. }
  318. void QFDApp::resizeGrips()
  319. {
  320. if (Settings::ENABLE_CUSTOM_TITLE_BAR) {
  321. this->m_leftGrip->setGeometry(0, 10, 10, this->height());
  322. this->m_rightGrip->setGeometry(this->width() - 10, 10, 10, this->height());
  323. this->m_topGrip->setGeometry(0, 0, this->width(), 10);
  324. this->m_bottomGrip->setGeometry(0, this->height() - 10, this->width(), 10);
  325. }
  326. }
  327. void QFDApp::startBoxAnimation(int leftBoxWidth, int rightBoxWidth, const QString &direction)
  328. {
  329. int rightWidth = 0;
  330. int leftWidth = 0;
  331. // Check values
  332. if (leftBoxWidth == 0 && direction == "left") {
  333. leftWidth = 240;
  334. }
  335. if (rightBoxWidth == 0 && direction == "right") {
  336. rightWidth = 240;
  337. }
  338. // ANIMATION LEFT BOX
  339. QPropertyAnimation *leftBox = new QPropertyAnimation(ui->extraLeftBox, QByteArray("minimumWidth"));
  340. leftBox->setDuration(Settings::TIME_ANIMATION);
  341. leftBox->setStartValue(leftBoxWidth);
  342. leftBox->setEndValue(leftWidth);
  343. leftBox->setEasingCurve(QEasingCurve::InOutQuart);
  344. // ANIMATION RIGHT BOX
  345. QPropertyAnimation *rightBox = new QPropertyAnimation(ui->extraRightBox, QByteArray("minimumWidth"));
  346. rightBox->setDuration(Settings::TIME_ANIMATION);
  347. rightBox->setStartValue(rightBoxWidth);
  348. rightBox->setEndValue(rightWidth);
  349. rightBox->setEasingCurve(QEasingCurve::InOutQuart);
  350. // GROUP ANIMATION
  351. QParallelAnimationGroup *group = new QParallelAnimationGroup(this);
  352. group->addAnimation(leftBox);
  353. group->addAnimation(rightBox);
  354. group->start(QAbstractAnimation::DeleteWhenStopped);
  355. }
  356. void QFDApp::mousePressEvent(QMouseEvent *event)
  357. {
  358. // SET DRAG POS WINDOW
  359. this->dragPos = event->globalPos();
  360. // PRINT MOUSE EVENTS
  361. if (event->buttons() == Qt::LeftButton) {
  362. qDebug() << "Mouse click: LEFT CLICK";
  363. } else if (event->buttons() == Qt::RightButton) {
  364. qDebug() << "Mouse click: RIGHT CLICK";
  365. }
  366. }
  367. void QFDApp::resizeEvent(QResizeEvent *event)
  368. {
  369. resizeGrips();
  370. }
  371. bool QFDApp::eventFilter(QObject *watched, QEvent *event)
  372. {
  373. if (watched == ui->titleRightInfo) {
  374. if (event->type() == QEvent::MouseButtonDblClick) {
  375. QTimer::singleShot(250, this, &QFDApp::maximizeRestore);
  376. // return true;
  377. }
  378. if (event->type() == QEvent::MouseMove) {
  379. QMouseEvent *moveEvent = static_cast<QMouseEvent *>(event);
  380. // IF MAXIMIZED CHANGE TO NORMAL
  381. if (returStatus()) {
  382. maximizeRestore();
  383. }
  384. // MOVE WINDOW
  385. if (moveEvent->buttons() == Qt::LeftButton) {
  386. this->move(this->pos() + moveEvent->globalPos() - this->dragPos);
  387. this->dragPos = moveEvent->globalPos();
  388. // event->accept();
  389. // return true;
  390. }
  391. }
  392. }
  393. return QMainWindow::eventFilter(watched, event);
  394. }