PowerMaster/source/dashboard.cpp

220 lines
6.2 KiB
C++
Raw Normal View History

2024-10-10 16:59:51 +08:00
#include "dashboard.h"
2024-11-07 12:08:56 +08:00
#include "dashboardFrame.h"
2024-10-10 16:59:51 +08:00
#include "dataPanel.h"
#include "customTab.h"
2024-10-10 16:59:51 +08:00
#include <QDialog>
#include <QPushButton>
#include <QMenu>
#include <QVBoxLayout>
#include <QKeyEvent>
#include <QDateTime>
2024-10-10 16:59:51 +08:00
#define tabButtonHeight 41
Dashboard::Dashboard(const QString& strName, QObject *parent)
: QObject(parent)
{
2024-11-07 12:08:56 +08:00
m_pFrame = nullptr;
2024-10-10 16:59:51 +08:00
//displayArea
m_pDisplayArea = new QWidget();
2024-11-07 12:08:56 +08:00
//m_pDisplayArea->setObjectName("displayArea");
2024-10-10 16:59:51 +08:00
//m_pDisplayArea->setStyleSheet("QWidget #displayArea {background-color: rgb(18, 25, 30);}");
2024-11-07 12:08:56 +08:00
m_pDisplayAreaLayout = nullptr;
//tab,自适应text内容
m_pTab = new CustomTab();
2024-11-08 17:09:56 +08:00
m_pTab->setDashboard(this);
//m_pTabButton->setIcon(QIcon(":/images/btn_float_default.png"));
connect(m_pTab, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(contextMenu_tab(const QPoint&)));
2024-10-10 16:59:51 +08:00
//右键菜单
m_pTabMenu = new QMenu();
m_pTabMenu->setStyleSheet("QMenu{\n"
2024-10-10 16:59:51 +08:00
" 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_pTabMenu->addAction(QString::fromWCharArray(L"删除"), this, SLOT(onAction_remove()));
m_pTabMenu->addAction(QString::fromWCharArray(L"重命名"), this, SLOT(onAction_rename()));
2024-10-10 16:59:51 +08:00
m_nPanenlNameNumber = 0;
setName(strName);
}
Dashboard::~Dashboard()
{
//在这里m_pDisplayArea和m_pTabButton不需要进行手动析构因为QObject建立起的父子结构会自动析构自动析构时不会将其置为nullptr所以if判断无效会引起'SIGSEGV'(存储器段错误)报错
if(m_pTabMenu)
2024-10-10 16:59:51 +08:00
{
delete m_pTabMenu;
m_pTabMenu = nullptr;
2024-10-10 16:59:51 +08:00
}
}
void Dashboard::deleteSubWidgets()
2024-10-10 16:59:51 +08:00
{
if(m_pTab)
{
delete m_pTab;
m_pTab = nullptr;
}
2024-10-10 16:59:51 +08:00
if(m_pDisplayArea)
2024-10-10 16:59:51 +08:00
{
delete m_pDisplayArea;
m_pDisplayArea = nullptr;
2024-10-10 16:59:51 +08:00
}
}
2024-11-14 09:19:14 +08:00
const QString& Dashboard::name()
2024-10-10 16:59:51 +08:00
{
return m_strName;
}
void Dashboard::setName(const QString& strName)
{
m_strName = strName;
m_pTab->setText(strName);
}
2024-11-07 12:08:56 +08:00
void Dashboard::setFrame(DashboardFrame* frame)
{
m_pFrame = frame;
setParent(m_pFrame);
}
DashboardFrame* Dashboard::frame()
{
return m_pFrame;
}
void Dashboard::setDisplayAreaLayout(QHBoxLayout* pLayout)
{
m_pDisplayAreaLayout = pLayout;
2024-10-10 16:59:51 +08:00
}
CustomTab* Dashboard::tab()
2024-10-10 16:59:51 +08:00
{
return m_pTab;
2024-10-10 16:59:51 +08:00
}
QWidget* Dashboard::displayArea()
{
return m_pDisplayArea;
}
void Dashboard::setActive(bool bActive)
2024-10-10 16:59:51 +08:00
{
if(bActive && m_pDisplayAreaLayout)
{
m_pDisplayAreaLayout->addWidget(m_pDisplayArea);
m_pDisplayArea->show();
for(int n = 0; n < m_dataPanels.count(); n++)
{
//让所有dataPanel根据当前area的size进行同步缩放
m_dataPanels.at(n)->resizeByNewSize(m_pDisplayArea->size());
}
}
else if(!bActive && m_pDisplayAreaLayout)
{
m_pDisplayAreaLayout->removeWidget(m_pDisplayArea);
m_pDisplayArea->hide();
for(int n = 0; n < m_dataPanels.count(); n++)
{
//让所有dataPanel记录area当前的size当再次被激活时若size发生变化让所有dataPanel进行同步缩放
m_dataPanels.at(n)->setDisplayAreaSize(m_pDisplayArea->size());
}
}
m_pTab->setActive(bActive);
2024-10-10 16:59:51 +08:00
}
void Dashboard::addPanel(DataPanelType type)
2024-10-10 16:59:51 +08:00
{
DataPanel* panel = new DataPanel(m_pDisplayArea, type);
2024-10-10 16:59:51 +08:00
connect(panel, SIGNAL(sgl_remove(const QString&)), this, SLOT(onSignal_removePanel(const QString&)));
2025-01-14 18:39:52 +08:00
connect(panel, SIGNAL(sgl_openCofigurationDialog(DataPanel*)), this, SLOT(onSignal_openConfigurationDlg(DataPanel*)));
2024-10-10 16:59:51 +08:00
QString strDefaultName = "dataPanel-" + QString::number(m_nPanenlNameNumber);
m_nPanenlNameNumber++;
panel->setName(strDefaultName);
2024-11-27 16:39:25 +08:00
int nPanelWidth = m_pDisplayArea->width() * 0.3;
int nPanelHeight = nPanelWidth * 0.56; //16:9宽高比
panel->setInitialSize(QSize(nPanelWidth, nPanelHeight));
2024-10-10 16:59:51 +08:00
int nCount = m_dataPanels.count();
//QPoint originPoint = m_pDisplayArea->mapToGlobal(QPoint(0, 0));
int nX = /*originPoint.x() +*/ (m_pDisplayArea->width() - nPanelWidth) * 0.5 + nCount * 10;
int nY = /*originPoint.y() +*/ (m_pDisplayArea->height() - nPanelHeight) * 0.5 + nCount * 10;
2024-10-10 16:59:51 +08:00
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::resizePanel(double ratioX, double ratioY)
{
for(int n = 0; n < m_dataPanels.count(); n++)
m_dataPanels.at(n)->resizeByRatio(ratioX, ratioY);
}
void Dashboard::setDateTime(const QDateTime& dateTime)
{
for(int n = 0; n < m_dataPanels.count(); n++)
m_dataPanels.at(n)->setDateTime(dateTime);
}
void Dashboard::setTimeRange(TimeUnit unit)
{
for(int n = 0; n < m_dataPanels.count(); n++)
m_dataPanels.at(n)->setTimeRange(unit);
}
void Dashboard::contextMenu_tab(const QPoint& pos)
2024-10-10 16:59:51 +08:00
{
QPoint originPoint = m_pTab->mapToGlobal(QPoint(0, 0));
m_pTabMenu->popup(originPoint + pos);
2024-10-10 16:59:51 +08:00
}
void Dashboard::onAction_remove()
{
emit sgl_remove();
}
void Dashboard::onAction_rename()
{
emit sgl_rename();
}
void Dashboard::onSignal_removePanel(const QString& strName)
{
removePanel(strName);
}
2025-01-14 18:39:52 +08:00
void Dashboard::onSignal_openConfigurationDlg(DataPanel* pPanel)
{
if(m_pFrame)
m_pFrame->openPanelConfigurationDialog(pPanel);
}