PowerMaster/source/dashboard.cpp

169 lines
4.6 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 "customTab.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_pDisplayAreaLayout = nullptr;
//m_pDisplayArea->setStyleSheet("QWidget #displayArea {background-color: rgb(18, 25, 30);}");
//tab,自适应text内容
m_pTab = new CustomTab();
//m_pTabButton->setIcon(QIcon(":/images/btn_float_default.png"));
connect(m_pTab, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(contextMenu_tab(const QPoint&)));
//右键菜单
m_pTabMenu = new QMenu();
m_pTabMenu->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_pTabMenu->addAction(QString::fromWCharArray(L"删除"), this, SLOT(onAction_remove()));
m_pTabMenu->addAction(QString::fromWCharArray(L"重命名"), this, SLOT(onAction_rename()));
m_nPanenlNameNumber = 0;
setName(strName);
}
Dashboard::~Dashboard()
{
//在这里m_pDisplayArea和m_pTabButton不需要进行手动析构因为QObject建立起的父子结构会自动析构自动析构时不会将其置为nullptr所以if判断无效会引起'SIGSEGV'(存储器段错误)报错
if(m_pTabMenu)
{
delete m_pTabMenu;
m_pTabMenu = nullptr;
}
}
void Dashboard::deleteSubWidgets()
{
if(m_pTab)
{
delete m_pTab;
m_pTab = nullptr;
}
if(m_pDisplayArea)
{
delete m_pDisplayArea;
m_pDisplayArea = nullptr;
}
}
const QString& Dashboard::getName()
{
return m_strName;
}
void Dashboard::setName(const QString& strName)
{
m_strName = strName;
m_pTab->setText(strName);
}
void Dashboard::setDisplayAreaLayout(QHBoxLayout* pLayout)
{
m_pDisplayAreaLayout = pLayout;
}
CustomTab* Dashboard::tab()
{
return m_pTab;
}
QWidget* Dashboard::displayArea()
{
return m_pDisplayArea;
}
void Dashboard::setActive(bool bActive)
{
if(bActive && m_pDisplayAreaLayout)
{
m_pDisplayAreaLayout->addWidget(m_pDisplayArea);
m_pDisplayArea->show();
}
else if(!bActive && m_pDisplayAreaLayout)
{
m_pDisplayAreaLayout->removeWidget(m_pDisplayArea);
m_pDisplayArea->hide();
}
m_pTab->setActive(bActive);
}
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_tab(const QPoint& pos)
{
QPoint originPoint = m_pTab->mapToGlobal(QPoint(0, 0));
m_pTabMenu->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);
}