PowerMaster/source/dashboard.cpp

186 lines
6.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "dashboard.h"
#include "dataPanel.h"
#include <QDialog>
#include <QPushButton>
#include <QMenu>
#include <QVBoxLayout>
#include <QKeyEvent>
#define tabButtonHeight 41
Dashboard::Dashboard(const QString& strName, QObject *parent)
: QObject(parent)
{
//displayArea
m_pDisplayArea = new QWidget();
m_pDisplayArea->setObjectName("displayArea");
//m_pDisplayArea->setStyleSheet("QWidget #displayArea {background-color: rgb(18, 25, 30);}");
//tabButton,自适应text内容
m_pTabButton = new QPushButton();
m_pTabButton->setContextMenuPolicy(Qt::CustomContextMenu);
m_pTabButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_pTabButton->setStyleSheet("QPushButton{\n"
" font:600 11pt \"微软雅黑\";\n"
" padding-left:5px;\n"
" padding-right:5px;\n"
"}\n");
connect(m_pTabButton, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(contextMenu_tabButton(const QPoint&)));
//右键菜单
m_pTabBtnMenu = new QMenu();
m_pTabBtnMenu->setStyleSheet("QMenu{\n"
" background-color:rgb(36,43,50);\n"
" border:1px solid rgb(6, 6, 6);\n"
"}\n"
"QMenu:item{\n"
" padding-left:20px;\n"
" padding-right:20px;\n"
" font:9pt \"微软雅黑\";\n"
" color:rgb(220,220,220);\n"
" height:26px;\n"
"}\n"
"QMenu:item:selected{\n"
" background-color: rgba(67,160,249, 80);\n"
"}\n");
m_pTabBtnMenu->addAction(QString::fromWCharArray(L"删除"), this, SLOT(onAction_remove()));
m_pTabBtnMenu->addAction(QString::fromWCharArray(L"重命名"), this, SLOT(onAction_rename()));
m_nPanenlNameNumber = 0;
setName(strName);
}
Dashboard::~Dashboard()
{
//m_pMdiArea和m_pTabButton不需要进行以下手动析构因为QObject建立起的父子结构会自动析构自动析构时不会将其置为nullptr所以if判断无效会引起'SIGSEGV'(存储器段错误)报错
if(m_pTabBtnMenu)
{
delete m_pTabBtnMenu;
m_pTabBtnMenu = nullptr;
}
}
void Dashboard::updateTabButtonSize()
{
QFontMetrics metrics(m_pTabButton->font());
QRect rect = metrics.boundingRect(m_pTabButton->text());
m_pTabButton->setMinimumSize(rect.width() + 30, tabButtonHeight);
m_pTabButton->setMaximumSize(rect.width() + 30, tabButtonHeight);
/*int nWidth = 0;
for(int n=0; n<strName.size(); n++)
{
QChar c = strName.at(n);
ushort uni = c.unicode();
if(uni >= 0x4E00 && uni <= 0x9FA5) //为汉字
nWidth += 12;
else
nWidth += 7;
}
nWidth += 35;
m_pTabButton->setMinimumSize(nWidth, tabButtonHeight);
m_pTabButton->setMaximumSize(nWidth, tabButtonHeight);*/
}
const QString& Dashboard::getName()
{
return m_strName;
}
void Dashboard::setName(const QString& strName)
{
m_strName = strName;
m_pTabButton->setText(strName);
updateTabButtonSize();
}
QPushButton* Dashboard::tabButton()
{
return m_pTabButton;
}
QWidget* Dashboard::displayArea()
{
return m_pDisplayArea;
}
void Dashboard::setSelected(bool bSelected)
{
QString strStyleSheet = "";
if(bSelected)
strStyleSheet = "QPushButton{\n"
" color:rgb(250,250,250);\n"
" font:600 11pt \"微软雅黑\";\n"
" border-bottom:3px solid rgb(67,160,249);\n"
"}\n"
"QPushButton:hover{\n"
" background-color: rgba(60, 60, 60, 150);\n"
"}\n";
else
strStyleSheet = "QPushButton{\n"
" color:rgb(220,220,220);\n"
" font:11pt \"微软雅黑\";\n"
" border-bottom:0px;\n"
"}\n"
"QPushButton:hover{\n"
" background-color: rgba(60, 60, 60, 150);\n"
"}\n";
m_pTabButton->setStyleSheet(strStyleSheet);
updateTabButtonSize();
}
void Dashboard::addPanel(const QString& strType)
{
DataPanel* panel = new DataPanel(m_pDisplayArea);
connect(panel, SIGNAL(sgl_remove(const QString&)), this, SLOT(onSignal_removePanel(const QString&)));
QString strDefaultName = "dataPanel-" + QString::number(m_nPanenlNameNumber);
m_nPanenlNameNumber++;
panel->setName(strDefaultName);
//panel->setStyleSheet("QDialog{background-color:transparent;}");
int nCount = m_dataPanels.count();
//QPoint originPoint = m_pDisplayArea->mapToGlobal(QPoint(0, 0));
int nX = /*originPoint.x() +*/ (m_pDisplayArea->width() - panel->width()) * 0.5 + nCount * 10;
int nY = /*originPoint.y() +*/ (m_pDisplayArea->height() - panel->height()) * 0.5 + nCount * 10;
panel->move(nX, nY);
panel->show();
m_dataPanels.push_back(panel);
}
void Dashboard::removePanel(const QString& strName)
{
int nIndex = -1;
for(int n = 0; n < m_dataPanels.count(); n++)
{
if(m_dataPanels.at(n)->getName() == strName)
{
nIndex = n;
break;
}
}
if(nIndex != -1)
{
DataPanel* panel = m_dataPanels.takeAt(nIndex);
delete panel;
}
}
void Dashboard::contextMenu_tabButton(const QPoint& pos)
{
QPoint originPoint = m_pTabButton->mapToGlobal(QPoint(0, 0));
m_pTabBtnMenu->popup(originPoint + pos);
}
void Dashboard::onAction_remove()
{
emit sgl_remove();
}
void Dashboard::onAction_rename()
{
emit sgl_rename();
}
void Dashboard::onSignal_removePanel(const QString& strName)
{
removePanel(strName);
}