#include "frmmain.h" #include "ui_frmmain.h" #include "quihelper.h" #include "appinit.h" #include "framelesshelper.h" #include "frmview.h" #include "frmdata.h" #include "frmconfig.h" frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain) { ui->setupUi(this); this->initForm(); this->initText(); this->initNav(); this->initIcon(); this->initAction(); QUIHelper::setFormInCenter(this); //on_btnMenu_Max_clicked(); } frmMain::~frmMain() { delete ui; } bool frmMain::eventFilter(QObject *watched, QEvent *event) { if (event->type() == QEvent::MouseButtonDblClick) { //双击标题栏最大化 if (watched == ui->widgetTop) { on_btnMenu_Max_clicked(); } } return QWidget::eventFilter(watched, event); } void frmMain::initForm() { ui->widgetBtn->setProperty("flag", "btnNavTop"); ui->widgetMain->setProperty("form", true); ui->widgetTop->setProperty("form", "title"); ui->widgetTop->setFixedHeight(80); ui->bottomWidget->setFixedHeight(25); //设置无边框 QUIHelper::setFramelessForm(this); ui->widgetTop->installEventFilter(this); ui->labLogoBg->installEventFilter(this); //设置右上角菜单,图形字体 IconHelper::setIcon(ui->btnMenu_Min, QUIConfig::IconMin); IconHelper::setIcon(ui->btnMenu_Max, QUIConfig::IconNormal); IconHelper::setIcon(ui->btnMenu_Close, QUIConfig::IconClose, QUIConfig::FontSize + 2); //无边框可拉伸类 frameless = new FramelessHelper(this); //激活当前窗体 frameless->activateOn(this); //设置窗体的标题栏高度 frameless->setTitleHeight(ui->widgetTop->height()); //设置窗体可移动 frameless->setWidgetMovable(true); //设置窗体可缩放 frameless->setWidgetResizable(true); //设置底部状态栏 ui->bottomWidget->setVersion("V20211010"); ui->bottomWidget->setCurrentUserName("刘典武"); ui->bottomWidget->setCurrentUserType("管理员"); ui->bottomWidget->setFontName(QUIConfig::FontName); int fontSize = (QUIConfig::FontSize > 12 ? QUIConfig::FontSize - 1 : QUIConfig::FontSize); ui->bottomWidget->setFontSize(fontSize); ui->bottomWidget->setLineFixedWidth(true); ui->bottomWidget->start(); } void frmMain::initNav() { //不同的样式边距和图标大小不一样,可以自行调整三个值看效果 int topMargin, otherMargin; if (AppNav::NavMainLeft) { topMargin = ui->widgetMenu->height() + 6; otherMargin = 9; } else { topMargin = 3; otherMargin = 3; } ui->layoutNav->setContentsMargins(otherMargin, topMargin, otherMargin, otherMargin); frmView *view = new frmView; ui->stackedWidget->addWidget(view); frmData *data = new frmData; ui->stackedWidget->addWidget(data); frmConfig *config = new frmConfig; ui->stackedWidget->addWidget(config); connect(this, SIGNAL(setIndex(int)), view, SLOT(setIndex(int))); connect(AppEvent::Instance(), SIGNAL(changeStyle()), this, SLOT(initIcon())); connect(AppEvent::Instance(), SIGNAL(changeStyle()), data, SLOT(initIcon())); connect(AppEvent::Instance(), SIGNAL(changeStyle()), config, SLOT(initIcon())); connect(AppEvent::Instance(), SIGNAL(fullScreen(bool)), this, SLOT(fullScreen(bool))); QList names, texts; names << "btnMainDevice" << "btnMainMap" << "btnMainData" << "btnData" << "btnConfig"; texts << "设备监控" << "地图监控" << "数据监控" << "数据查询" << "系统设置"; icons << 0xea00 << 0xe695 << 0xe60a << 0xe699 << 0xe706; //根据设定实例化导航按钮对象 for (int i = 0; i < texts.count(); i++) { QToolButton *btn = new QToolButton; if (AppNav::NavMainLeft) { btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); btn->setMinimumWidth(115); } else { btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); btn->setMinimumWidth(80); } btn->setObjectName(names.at(i)); btn->setText(texts.at(i)); btn->setCheckable(true); btn->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClicked())); ui->layoutNav->addWidget(btn); btns << btn; } btns.at(0)->click(); } void frmMain::initIcon() { //如果logo图片不存在则隐藏标签,否则显示logo图片 ui->labLogoBg->setAlignment(Qt::AlignCenter); QPixmap pixLogo(QUIHelper::appPath() + "/" + "bg.png"); if (pixLogo.isNull()) { //ui->labLogoBg->setVisible(false); //设置图形字体作为logo int size = 50; ui->labLogoBg->setFixedWidth(size + 20); IconHelper::setIcon(ui->labLogoBg, 0xe6f2, size); } else { int size = 150; ui->labLogoBg->setFixedWidth(size + 20); pixLogo = pixLogo.scaled(size, size, Qt::KeepAspectRatio, Qt::SmoothTransformation); ui->labLogoBg->setPixmap(pixLogo); } int count = btns.count(); for (int i = 0; i < count; ++i) { AppNav::initNavBtnIcon(btns.at(i), icons.at(i), false); } } void frmMain::buttonClicked() { QAbstractButton *btn = (QAbstractButton *)sender(); if (btn == btns.at(0)) { emit setIndex(0); ui->stackedWidget->setCurrentIndex(0); } else if (btn == btns.at(1)) { emit setIndex(1); ui->stackedWidget->setCurrentIndex(0); } else if (btn == btns.at(2)) { emit setIndex(2); ui->stackedWidget->setCurrentIndex(0); } else if (btn == btns.at(3)) { ui->stackedWidget->setCurrentIndex(1); } else if (btn == btns.at(4)) { ui->stackedWidget->setCurrentIndex(2); } //取消其他按钮选中 foreach (QAbstractButton *b, btns) { b->setChecked(b == btn); } } void frmMain::initText() { //从配置文件读取logo名称 ui->labLogoCn->setText("通用应用程序框架平台"); ui->labLogoEn->setText("General application framework platform"); this->setWindowTitle(ui->labLogoCn->text()); //设置标题+版本+版权 ui->bottomWidget->setTitle(ui->labLogoCn->text()); ui->bottomWidget->setCopyright("上海物联网技术研究中心"); } void frmMain::initAction() { //设置右键菜单模式 this->setContextMenuPolicy(Qt::ActionsContextMenu); //添加换肤菜单 QStringList styleNames, styleFiles; QUIStyle::getStyle(styleNames, styleFiles); //放入动作组产生互斥效果 QActionGroup *actionGroup = new QActionGroup(this); for (int i = 0; i < styleNames.count(); i++) { QAction *action = new QAction(this); action->setCheckable(true); action->setText(styleNames.at(i)); action->setData(styleFiles.at(i)); connect(action, SIGNAL(triggered(bool)), this, SLOT(doAction())); this->addAction(action); actionGroup->addAction(action); //默认样式 if (styleFiles.at(i) == ":/qss/blackvideo.css") { action->setChecked(true); } } } void frmMain::doAction() { QAction *action = (QAction *)sender(); QString qssFile = action->data().toString(); AppInit::Instance()->initStyle(qssFile); } void frmMain::fullScreen(bool full) { if (full) { ui->widgetTop->setVisible(false); ui->bottomWidget->setVisible(false); this->showFullScreen(); //this->showMaximized(); } else { ui->widgetTop->setVisible(true); ui->bottomWidget->setVisible(true); this->showNormal(); } } void frmMain::on_btnMenu_Min_clicked() { this->showMinimized(); } void frmMain::on_btnMenu_Max_clicked() { static bool max = false; static QRect location = this->geometry(); if (max) { this->setGeometry(location); } else { location = this->geometry(); this->setGeometry(QUIHelper::getScreenRect()); } IconHelper::setIcon(ui->btnMenu_Max, max ? QUIConfig::IconNormal : QUIConfig::IconMax); this->setProperty("canMove", max); max = !max; frameless->setWidgetMovable(!max); frameless->setWidgetResizable(!max); } void frmMain::on_btnMenu_Close_clicked() { this->close(); }