123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- #include "QFDApp.h"
- #include "ui_QFDApp.h"
- #include "CustomGrip.h"
- #include <QPropertyAnimation>
- #include <QTimer>
- #include <QDebug>
- #include <QParallelAnimationGroup>
- class Settings
- {
- public:
- static bool ENABLE_CUSTOM_TITLE_BAR;
- static int MENU_WIDTH;
- static int LEFT_BOX_WIDTH;
- static int RIGHT_BOX_WIDTH;
- static int TIME_ANIMATION;
- // BTNS LEFT AND RIGHT BOX COLORS
- static QString BTN_LEFT_BOX_COLOR;
- static QString BTN_RIGHT_BOX_COLOR;
- // MENU SELECTED STYLESHEET
- static QString MENU_SELECTED_STYLESHEET;
- };
- bool Settings::ENABLE_CUSTOM_TITLE_BAR = true;
- int Settings::MENU_WIDTH = 240;
- int Settings::LEFT_BOX_WIDTH = 240;
- int Settings::RIGHT_BOX_WIDTH = 240;
- int Settings::TIME_ANIMATION = 500;
- QString Settings::BTN_LEFT_BOX_COLOR = "background-color: rgb(44, 49, 58);";
- QString Settings::BTN_RIGHT_BOX_COLOR = "background-color: #ff79c6;";
- QString Settings::MENU_SELECTED_STYLESHEET =
- "border-left: 22px solid qlineargradient(spread:pad, x1:0.034, y1:0, x2:0.216, y2:0, stop:0.499 rgba(255, 121, "
- "198, 255), stop:0.5 rgba(85, 170, 255, 0)); background-color: rgb(40, 44, 52);";
- static bool GLOBAL_STATE = false;
- static bool GLOBAL_TITLE_BAR = true;
- QFDApp::QFDApp(QWidget *parent) : QMainWindow(parent), ui(new Ui::QFDApp)
- {
- ui->setupUi(this);
- init();
- }
- QFDApp::~QFDApp()
- {
- delete ui;
- }
- /**
- * @brief TOGGLE MENU
- * @param checked
- */
- void QFDApp::toggleMenu()
- {
- // GET WIDTH
- int width = ui->leftMenuBg->width();
- // qDebug() << width;
- int maxExtend = Settings::MENU_WIDTH;
- int widthExtended = 60; // default
- // SET MAX WIDTH
- if (width == 60) {
- widthExtended = maxExtend;
- }
- QPropertyAnimation *animation = new QPropertyAnimation(ui->leftMenuBg, QByteArray("minimumWidth"));
- animation->setDuration(Settings::TIME_ANIMATION);
- animation->setStartValue(width);
- animation->setEndValue(widthExtended);
- animation->setEasingCurve(QEasingCurve::InOutQuart);
- animation->start(QAbstractAnimation::DeleteWhenStopped);
- }
- void QFDApp::maximizeRestore()
- {
- if (!GLOBAL_STATE) {
- this->showMaximized();
- GLOBAL_STATE = true;
- ui->appMargins->setContentsMargins(0, 0, 0, 0);
- ui->maximizeRestoreAppBtn->setToolTip("向下还原");
- ui->maximizeRestoreAppBtn->setIcon(QIcon(":/res/icons/icon_restore.png"));
- ui->frame_size_grip->hide();
- this->m_leftGrip->hide();
- this->m_rightGrip->hide();
- this->m_topGrip->hide();
- this->m_bottomGrip->hide();
- } else {
- GLOBAL_STATE = false;
- this->showNormal();
- this->resize(this->width() + 1, this->height() + 1);
- ui->appMargins->setContentsMargins(10, 10, 10, 10);
- ui->maximizeRestoreAppBtn->setToolTip("最大化");
- ui->maximizeRestoreAppBtn->setIcon(QIcon(":/res/icons/icon_maximize.png"));
- ui->frame_size_grip->show();
- this->m_leftGrip->show();
- this->m_rightGrip->show();
- this->m_topGrip->show();
- this->m_bottomGrip->show();
- }
- }
- void QFDApp::leftMenuButtonClick()
- {
- QPushButton *btn = qobject_cast<QPushButton *>(sender());
- QString btnName = btn->objectName();
- if (btnName == "btn_home") {
- ui->stackedWidget->setCurrentWidget(ui->home);
- this->resetStyle(btnName);
- btn->setStyleSheet(this->selectMenu(btn->styleSheet()));
- }
- if (btnName == "btn_widgets") {
- ui->stackedWidget->setCurrentWidget(ui->widgets);
- this->resetStyle(btnName);
- btn->setStyleSheet(this->selectMenu(btn->styleSheet()));
- }
- if (btnName == "btn_new") {
- ui->stackedWidget->setCurrentWidget(ui->new_page);
- this->resetStyle(btnName);
- btn->setStyleSheet(this->selectMenu(btn->styleSheet()));
- }
- if (btnName == "btn_save") {
- qDebug() << "Save BTN clicked!";
- }
- qDebug() << "Button " + btnName + " clicked!";
- }
- void QFDApp::openCloseLeftBox()
- {
- // GET WIDTH
- int width = ui->extraLeftBox->width();
- int widthRightBox = ui->extraRightBox->width();
- // int maxExtend = Settings::LEFT_BOX_WIDTH;
- QString color = Settings::BTN_LEFT_BOX_COLOR;
- // int standard = 0;
- // GET BTN STYLE
- QString style = ui->toggleLeftBox->styleSheet();
- // SET MAX WIDTH
- if (width == 0) {
- // SELECT BTN
- ui->toggleLeftBox->setStyleSheet(style + color);
- if (widthRightBox != 0) {
- style = ui->settingsTopBtn->styleSheet();
- ui->settingsTopBtn->setStyleSheet(style.replace(Settings::BTN_LEFT_BOX_COLOR, ""));
- }
- } else {
- // RESET BTN
- ui->toggleLeftBox->setStyleSheet(style.replace(color, ""));
- }
- this->startBoxAnimation(width, widthRightBox, "left");
- }
- void QFDApp::openCloseRightBox()
- { // GET WIDTH
- int width = ui->extraRightBox->width();
- int widthLeftBox = ui->extraLeftBox->width();
- int maxExtend = Settings::RIGHT_BOX_WIDTH;
- QString color = Settings::BTN_RIGHT_BOX_COLOR;
- int standard = 0;
- // GET BTN STYLE
- QString style = ui->settingsTopBtn->styleSheet();
- // SET MAX WIDTH
- if (width == 0) {
- // SELECT BTN
- ui->settingsTopBtn->setStyleSheet(style + color);
- if (widthLeftBox != 0) {
- style = ui->toggleLeftBox->styleSheet();
- ui->toggleLeftBox->setStyleSheet(style.replace(Settings::BTN_LEFT_BOX_COLOR, ""));
- }
- } else {
- // RESET BTN
- ui->settingsTopBtn->setStyleSheet(style.replace(color, ""));
- }
- this->startBoxAnimation(widthLeftBox, width, "right");
- }
- void QFDApp::init()
- {
- // USE CUSTOM TITLE BAR | USE AS "False" FOR MAC OR LINUX
- Settings::ENABLE_CUSTOM_TITLE_BAR = true;
- // APP NAME
- QString title = "QFD - 专家决策软件";
- QString description = "QFD APP - Dracula主题风格.";
- this->setWindowTitle(title);
- ui->titleRightInfo->setText(description);
- // TOGGLE MENU
- connect(ui->toggleButton, &QPushButton::clicked, this, &QFDApp::toggleMenu);
- uiDefinitions();
- // QTableWidget PARAMETERS
- ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
- // LEFT MENUS
- connect(ui->btn_home, &QPushButton::clicked, this, &QFDApp::leftMenuButtonClick);
- connect(ui->btn_widgets, &QPushButton::clicked, this, &QFDApp::leftMenuButtonClick);
- connect(ui->btn_new, &QPushButton::clicked, this, &QFDApp::leftMenuButtonClick);
- connect(ui->btn_save, &QPushButton::clicked, this, &QFDApp::leftMenuButtonClick);
- // EXTRA LEFT BOX
- connect(ui->toggleLeftBox, &QPushButton::clicked, this, &QFDApp::openCloseLeftBox);
- connect(ui->extraCloseColumnBtn, &QPushButton::clicked, this, &QFDApp::openCloseLeftBox);
- // EXTRA RIGHT BOX
- connect(ui->settingsTopBtn, &QPushButton::clicked, this, &QFDApp::openCloseRightBox);
- // SET CUSTOM THEME
- bool useCustomTheme = false;
- QString themeFile = "themes\\py_dracula_light.qss";
- // SET THEME AND HACKS
- if (useCustomTheme) {
- // LOAD AND APPLY STYLE
- this->loadTheme(themeFile, true);
- // SET HACKS
- this->setThemeHack();
- }
- // SET HOME PAGE AND SELECT MENU
- ui->stackedWidget->setCurrentWidget(ui->home);
- ui->btn_home->setStyleSheet(this->selectMenu(ui->btn_home->styleSheet()));
- }
- void QFDApp::loadTheme(const QString &file, bool useCustomTheme)
- {
- if (useCustomTheme) {
- QFile qssFile(file);
- if (qssFile.open(QIODevice::ReadOnly)) {
- QTextStream in(&qssFile);
- in.setCodec("UTF-8");
- this->setStyle(nullptr);
- this->setStyleSheet(in.readAll());
- qssFile.close();
- }
- }
- }
- void QFDApp::setThemeHack()
- {
- Settings::BTN_LEFT_BOX_COLOR = "background-color: #495474;";
- Settings::BTN_RIGHT_BOX_COLOR = "background-color: #495474;";
- Settings::MENU_SELECTED_STYLESHEET =
- "border-left: 22px solid qlineargradient(spread:pad, x1:0.034, y1:0, x2:0.216, y2:0, stop:0.499 rgba(255, "
- "121, 198, 255), stop:0.5 rgba(85, 170, 255, 0)); background-color: #566388;";
- // SET MANUAL STYLES
- ui->lineEdit->setStyleSheet("background-color: #6272a4;");
- ui->pushButton->setStyleSheet("background-color: #6272a4;");
- ui->plainTextEdit->setStyleSheet("background-color: #6272a4;");
- ui->tableWidget->setStyleSheet(
- "QScrollBar:vertical { background: #6272a4; } QScrollBar:horizontal { background: #6272a4; }");
- ui->scrollArea->setStyleSheet(
- "QScrollBar:vertical { background: #6272a4; } QScrollBar:horizontal { background: #6272a4; }");
- ui->comboBox->setStyleSheet("background-color: #6272a4;");
- ui->horizontalScrollBar->setStyleSheet("background-color: #6272a4;");
- ui->verticalScrollBar->setStyleSheet("background-color: #6272a4;");
- ui->commandLinkButton->setStyleSheet("color: #ff79c6;");
- }
- void QFDApp::uiDefinitions()
- {
- ui->settingsTopBtn->setToolTip("右侧设置");
- ui->minimizeAppBtn->setToolTip("最小化");
- ui->maximizeRestoreAppBtn->setToolTip("最大化");
- ui->closeAppBtn->setToolTip("关闭");
- ui->toggleLeftBox->setToolTip("设置");
- ui->titleRightInfo->installEventFilter(this);
- if (Settings::ENABLE_CUSTOM_TITLE_BAR) {
- // STANDARD TITLE BAR
- this->setWindowFlags(Qt::FramelessWindowHint);
- this->setAttribute(Qt::WA_TranslucentBackground);
- // CUSTOM GRIPS
- this->m_leftGrip = new CustomGrip(this, Qt::LeftEdge, true);
- this->m_rightGrip = new CustomGrip(this, Qt::RightEdge, true);
- this->m_topGrip = new CustomGrip(this, Qt::TopEdge, true);
- this->m_bottomGrip = new CustomGrip(this, Qt::BottomEdge, true);
- } else {
- ui->appMargins->setContentsMargins(0, 0, 0, 0);
- ui->minimizeAppBtn->hide();
- ui->maximizeRestoreAppBtn->hide();
- ui->closeAppBtn->hide();
- ui->frame_size_grip->hide();
- }
- // DROP SHADOW
- this->shadow = new QGraphicsDropShadowEffect(this);
- this->shadow->setBlurRadius(17);
- this->shadow->setXOffset(0);
- this->shadow->setYOffset(0);
- this->shadow->setColor(QColor(0, 0, 0, 150));
- ui->bgApp->setGraphicsEffect(this->shadow);
- // RESIZE WINDOW
- this->sizegrip = new QSizeGrip(ui->frame_size_grip);
- this->sizegrip->setStyleSheet("width: 20px; height: 20px; margin 0px; padding: 0px;");
- // MINIMIZE
- connect(ui->minimizeAppBtn, &QPushButton::clicked, this, &QFDApp::showMinimized);
- // MAXIMIZE/RESTORE
- connect(ui->maximizeRestoreAppBtn, &QPushButton::clicked, this, &QFDApp::maximizeRestore);
- // CLOSE APPLICATION
- connect(ui->closeAppBtn, &QPushButton::clicked, this, &QFDApp::close);
- }
- bool QFDApp::returStatus()
- {
- return GLOBAL_STATE;
- }
- void QFDApp::setStatus(bool status)
- {
- GLOBAL_STATE = status;
- }
- QString QFDApp::selectMenu(const QString &getStyle)
- {
- QString select = getStyle + Settings::MENU_SELECTED_STYLESHEET;
- return select;
- }
- QString QFDApp::deselectMenu(const QString &getStyle)
- {
- QString style = getStyle;
- QString deselect = style.replace(Settings::MENU_SELECTED_STYLESHEET, "");
- return deselect;
- }
- /**
- * @brief START SELECTION
- * @param widget
- */
- void QFDApp::selectStandardMenu(const QString &widget)
- {
- for (auto w : ui->topMenu->findChildren<QPushButton *>()) {
- if (w->objectName() != widget) {
- w->setStyleSheet(this->selectMenu(w->styleSheet()));
- }
- }
- }
- /**
- * @brief RESET SELECTION
- * @param widget
- */
- void QFDApp::resetStyle(const QString &widget)
- {
- for (auto w : ui->topMenu->findChildren<QPushButton *>()) {
- if (w->objectName() != widget) {
- w->setStyleSheet(this->deselectMenu(w->styleSheet()));
- }
- }
- }
- void QFDApp::resizeGrips()
- {
- if (Settings::ENABLE_CUSTOM_TITLE_BAR) {
- this->m_leftGrip->setGeometry(0, 10, 10, this->height());
- this->m_rightGrip->setGeometry(this->width() - 10, 10, 10, this->height());
- this->m_topGrip->setGeometry(0, 0, this->width(), 10);
- this->m_bottomGrip->setGeometry(0, this->height() - 10, this->width(), 10);
- }
- }
- void QFDApp::startBoxAnimation(int leftBoxWidth, int rightBoxWidth, const QString &direction)
- {
- int rightWidth = 0;
- int leftWidth = 0;
- // Check values
- if (leftBoxWidth == 0 && direction == "left") {
- leftWidth = 240;
- }
- if (rightBoxWidth == 0 && direction == "right") {
- rightWidth = 240;
- }
- // ANIMATION LEFT BOX
- QPropertyAnimation *leftBox = new QPropertyAnimation(ui->extraLeftBox, QByteArray("minimumWidth"));
- leftBox->setDuration(Settings::TIME_ANIMATION);
- leftBox->setStartValue(leftBoxWidth);
- leftBox->setEndValue(leftWidth);
- leftBox->setEasingCurve(QEasingCurve::InOutQuart);
- // ANIMATION RIGHT BOX
- QPropertyAnimation *rightBox = new QPropertyAnimation(ui->extraRightBox, QByteArray("minimumWidth"));
- rightBox->setDuration(Settings::TIME_ANIMATION);
- rightBox->setStartValue(rightBoxWidth);
- rightBox->setEndValue(rightWidth);
- rightBox->setEasingCurve(QEasingCurve::InOutQuart);
- // GROUP ANIMATION
- QParallelAnimationGroup *group = new QParallelAnimationGroup(this);
- group->addAnimation(leftBox);
- group->addAnimation(rightBox);
- group->start(QAbstractAnimation::DeleteWhenStopped);
- }
- void QFDApp::mousePressEvent(QMouseEvent *event)
- {
- // SET DRAG POS WINDOW
- this->dragPos = event->globalPos();
- // PRINT MOUSE EVENTS
- if (event->buttons() == Qt::LeftButton) {
- qDebug() << "Mouse click: LEFT CLICK";
- } else if (event->buttons() == Qt::RightButton) {
- qDebug() << "Mouse click: RIGHT CLICK";
- }
- }
- void QFDApp::resizeEvent(QResizeEvent *event)
- {
- resizeGrips();
- }
- bool QFDApp::eventFilter(QObject *watched, QEvent *event)
- {
- if (watched == ui->titleRightInfo) {
- if (event->type() == QEvent::MouseButtonDblClick) {
- QTimer::singleShot(250, this, &QFDApp::maximizeRestore);
- // return true;
- }
- if (event->type() == QEvent::MouseMove) {
- QMouseEvent *moveEvent = static_cast<QMouseEvent *>(event);
- // IF MAXIMIZED CHANGE TO NORMAL
- if (returStatus()) {
- maximizeRestore();
- }
- // MOVE WINDOW
- if (moveEvent->buttons() == Qt::LeftButton) {
- this->move(this->pos() + moveEvent->globalPos() - this->dragPos);
- this->dragPos = moveEvent->globalPos();
- // event->accept();
- // return true;
- }
- }
- }
- return QMainWindow::eventFilter(watched, event);
- }
|