575 lines
18 KiB
C++

#include "CAlarmCompare.h"
#include "ui_CAlarmCompare.h"
CAlarmCompare::CAlarmCompare(QWidget *parent) :
QWidget(parent),
ui(new Ui::CAlarmCompare),
m_pChart(Q_NULLPTR)
{
ui->setupUi(this);
initVariate();
initUi();
initBar();
initLevelChoose();
}
CAlarmCompare::~CAlarmCompare()
{
delete ui;
delete m_pLabelValue;
m_pLabelValue = NULL;
delete m_pAlarmLevelComboBox;
m_pAlarmLevelComboBox = NULL;
delete m_pProcessDialog;
m_pProcessDialog = NULL;
delete m_pQueryThread;
m_pQueryThread = NULL;
qDeleteAll( m_listAlarmLevelCheckBox );
m_listAlarmLevelCheckBox.clear();
}
void CAlarmCompare::setHiddenOptColumn(bool hide)
{
if(m_pQueryThread)
{
m_pQueryThread->setHiddenOptColumn(hide);
}
}
void CAlarmCompare::initVariate()
{
m_pLabelValue = new QLabel(this);
m_pLabelValue->setObjectName( "value2" );
m_pLabelValue->hide();
m_bAfterMsgBox = false;
m_pAlarmLevelComboBox = new CAlarmLevelComboBox( ui->comboBox_level );
m_pProcessDialog = new CProcessDialog();
m_pProcessDialog->hide();
m_pQueryThread = new CCompareQueryThread();
m_nCurrentLocationIndex = 0;
}
void CAlarmCompare::initUi()
{
this->setWindowTitle("告警比对");
this->setWindowFlags( ( this->windowFlags() & ~Qt::WindowMinimizeButtonHint ) | Qt::WindowStaysOnTopHint | Qt::Dialog );
ui->splitter_2->setStretchFactor( 0, 3 );
ui->splitter_2->setStretchFactor( 1, 7 );
ui->splitter->setStretchFactor( 0, 5 );
ui->splitter->setStretchFactor( 1, 5 );
ui->comboBox_level->setMinimumWidth( 120 );
ui->dateEdit_start->setDate( QDate::currentDate().addDays(-6) );
ui->dateEdit_end->setDate( QDate::currentDate() );
ui->tableView_alarm->setModel( m_pQueryThread->m_pAlarmModel );
ui->tableView_alarm->setColumnHidden( 4, true );
ui->tableView_alarm->setObjectName( "alarm2" );
ui->tableView_alarm->setEditTriggers( QAbstractItemView::NoEditTriggers );
ui->tableView_alarm->setSelectionBehavior( QAbstractItemView::SelectRows );
ui->tableView_alarm->setSelectionMode( QAbstractItemView::SingleSelection );
ui->rb_day->setChecked( true );
ui->toolBox->setMinimumWidth( 200 );
// 初始化左侧复选框(stackWidget 第0页)
QVBoxLayout *pFrmLayout = new QVBoxLayout();
pFrmLayout->setMargin( 0 );
pFrmLayout->setSpacing( 0 );
for ( int i=0; i<m_pQueryThread->m_listLocation.count(); i++ )
{
SLocation* pLocation = m_pQueryThread->m_listLocation.at(i);
QCheckBox* pCheckBox = new QCheckBox();
pCheckBox->setText( pLocation->sName );
pCheckBox->setChecked( false );
pCheckBox->setFixedHeight( 32 );
pCheckBox->setProperty( "id", pLocation->nId );
m_listCheckBoxLocation << pCheckBox;
QVBoxLayout *pLayout = new QVBoxLayout();
pLayout->setMargin( 0 );
pLayout->addWidget( pCheckBox );
QFrame* pFrm = new QFrame();
pFrm->setLayout( pLayout );
pFrmLayout->addWidget( pFrm );
}
QSpacerItem* pSpacerItem = new QSpacerItem(1,1,QSizePolicy::Minimum,QSizePolicy::Expanding );
pFrmLayout->addSpacerItem( pSpacerItem );
ui->frame_location->setLayout( pFrmLayout );
// 初始化左侧复选框(stackWidget 第1页)
for ( int i=0; i<m_pQueryThread->m_listLocation.count(); i++ )
{
SLocation* pLocation = m_pQueryThread->m_listLocation.at(i);
QVBoxLayout *pLayout = new QVBoxLayout();
for ( int l=0; l<pLocation->listDeviceGroup.count(); l++ )
{
SDeviceGroup* pDeviceGroup = pLocation->listDeviceGroup.at(l);
QCheckBox* pCheckBox = new QCheckBox();
pCheckBox->setText( pDeviceGroup->sName );
pCheckBox->setChecked( false );
pCheckBox->setProperty( "id", pDeviceGroup->sTagName );
m_listCheckBoxDeviceGroup << pCheckBox;
pLayout->addWidget( pCheckBox );
// 最后一个时添加spacer
if ( l == pLocation->listDeviceGroup.count()-1 )
{
//pLayout->addSpacerItem( new QSpacerItem(1,1,QSizePolicy::Minimum,QSizePolicy::Expanding) );
pLayout->addStretch();
}
}
QGroupBox *pGroupBox = new QGroupBox( ui->toolBox );
pGroupBox->setLayout( pLayout );
ui->toolBox->addItem( pGroupBox, pLocation->sName );
}
// 删除原始第一页
ui->toolBox->widget(0)->hide();
ui->toolBox->removeItem( 0 );
m_pAlarmLevelComboBox->init( m_pQueryThread->m_listAlarmLevel );
connect( ui->pb_query, SIGNAL(clicked(bool)), this, SLOT(slotPushButtonQueryClicked(bool)) );
connect( m_pQueryThread, SIGNAL(signalQueryPercent(int)), m_pProcessDialog, SLOT(slotSetValue(int)) );
connect( m_pQueryThread, SIGNAL(signalQueryEnd()), this, SLOT(slotQueryEnd()) );
connect( m_pProcessDialog, SIGNAL(signalCancel()), m_pQueryThread, SLOT(slotCancel()) );;
}
void CAlarmCompare::initBar()
{
m_pChart = new QChart();
m_pChart->setContentsMargins( 0,0,0,0 );
m_pChart->setMargins(QMargins(0,0,0,0));
m_pChart->setBackgroundRoundness( 0 );
m_pChart->legend()->setVisible(false);
m_pChart->setBackgroundBrush( QBrush(QColor(3,22,36)) );
QChartView *pChartView = new QChartView( ui->frame_bar );
pChartView->setChart( m_pChart );
pChartView->setRenderHint(QPainter::Antialiasing);
pChartView->setMinimumHeight( 200 );
QLayout* pLayout = ui->frame_bar->layout();
pLayout->addWidget( pChartView );
ui->frame_bar->setLayout( pLayout );
}
void CAlarmCompare::initLevelChoose()
{
for ( int i=0; i<m_pQueryThread->m_listAlarmLevel.count(); i++ )
{
SAlarmLevel* pAlarmLevel = m_pQueryThread->m_listAlarmLevel.at(i);
QCheckBox* pCheckBox = new QCheckBox( pAlarmLevel->sName );
pCheckBox->setObjectName( QString("checkBox_level%1").arg(pAlarmLevel->nPriority) );
pCheckBox->setProperty( "id", pAlarmLevel->nId );
pCheckBox->setChecked( true );
ui->horizontalLayout_2->addWidget( pCheckBox );
if ( i < m_pQueryThread->m_listAlarmLevelColor.count() )
{
QColor tmpColor = m_pQueryThread->m_listAlarmLevelColor.at(i);
pCheckBox->setStyleSheet( QString("QCheckBox::indicator:checked{background-color: rgb(%1,%2,%3);}")
.arg(tmpColor.red()).arg(tmpColor.green()).arg(tmpColor.blue()) );
}
m_listAlarmLevelCheckBox.append( pCheckBox );
connect( pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(slotCheckBoxStateChanged(int)) );
}
}
void CAlarmCompare::resetUi()
{
// 清空柱状图
m_pChart->removeAllSeries();
// 清空查询线程
m_pQueryThread->clear();
}
void CAlarmCompare::setCurrentLocationIndex( int nIndex )
{
m_nCurrentLocationIndex = nIndex;
if ( m_nCurrentLocationIndex == 0 )
ui->stackedWidget->setCurrentIndex( 0 );
else
ui->stackedWidget->setCurrentIndex( 1 );
}
QColor CAlarmCompare::chartBackground()
{
return m_chartBackground;
}
void CAlarmCompare::setChartBackground(const QColor& color)
{
m_chartBackground = color;
if(m_pChart != Q_NULLPTR)
{
m_pChart->setBackgroundBrush( QBrush(m_chartBackground) );
}
}
QColor CAlarmCompare::chartLabelColor()
{
return m_chartLabelColor;
}
void CAlarmCompare::setChartLabelColor(const QColor &color)
{
m_chartLabelColor = color;
if(m_pChart == Q_NULLPTR)
{
return;
}
if(m_pChart->axisX())
{
m_pChart->axisX()->setLabelsBrush( QBrush(m_chartLabelColor));
}
if(m_pChart->axisY())
{
m_pChart->axisY()->setLabelsBrush( QBrush(m_chartLabelColor));
}
}
QColor CAlarmCompare::chartLineColor()
{
return m_chartLineColor;
}
void CAlarmCompare::setChartLineColor(const QColor &color)
{
m_chartLineColor = color;
if(m_pChart == Q_NULLPTR)
{
return;
}
if(m_pChart->axisX())
{
m_pChart->axisX()->setLinePenColor( m_chartLineColor);
}
if(m_pChart->axisY())
{
m_pChart->axisY()->setLinePenColor( m_chartLineColor);
}
}
void CAlarmCompare::showBar()
{
// 清空柱状图
m_pChart->removeAllSeries();
// 清空barset列表
m_listBarSet.clear();
// 没有设备或者没有数据是返回
if ( m_pQueryThread->m_listAlarm.count() <= 0 || m_pQueryThread->m_listAlarm[0].count() <= 0 )
return;
QBarCategoryAxis *pAxis = new QBarCategoryAxis();
for ( int m=0; m<m_pQueryThread->m_listAlarm.count(); m++ )
{
QList<QBarSet*> tmpListBarSet;
QList<SAlarmList*> tmpListAlarm = m_pQueryThread->m_listAlarm.at(m);
QStackedBarSeries* pBarSeries = new QStackedBarSeries();
connect( pBarSeries, SIGNAL(hovered(bool,int,QBarSet*)), this, SLOT(slotBarMouseMoveHovered(bool,int,QBarSet*)) );
connect( pBarSeries, SIGNAL(clicked(int,QBarSet*)), this, SLOT(slotBarClicked(int,QBarSet*)) );
for ( int i=0; i<m_pQueryThread->m_listAlarmLevel.count(); i++ )
{
QBarSet *pSet0 = new QBarSet(QString("%1_%2").arg(m).arg(i+1));
tmpListBarSet.append( pSet0 );
pBarSeries->append( pSet0 );
}
for ( int i=0; i<tmpListAlarm.count(); i++ )
{
SAlarmList* pAlarmList = tmpListAlarm.at(i);
if ( m == 0 )
pAxis->append( pAlarmList->sName );
for ( int l=0; l<pAlarmList->listLevelCount.count(); l++ )
{
if ( m_listAlarmLevelCheckBox.at(l)->isChecked() )
*tmpListBarSet[l] << pAlarmList->listLevelCount.at(l);
else
*tmpListBarSet[l] << 0;
}
}
m_pChart->addSeries( pBarSeries );
m_listBarSet << tmpListBarSet;
}
m_pChart->createDefaultAxes();
m_pChart->setAxisX( pAxis );
if ( m_pChart->axisX() )
{
m_pChart->axisX()->setLabelsBrush( QBrush(m_chartLabelColor));
m_pChart->axisX()->setLinePenColor( m_chartLineColor);
m_pChart->axisX()->setGridLineVisible( false );
}
if ( m_pChart->axisY() )
{
m_pChart->axisY()->setLabelsBrush( QBrush(m_chartLabelColor));
m_pChart->axisY()->setLinePenColor( m_chartLineColor);
m_pChart->axisY()->setGridLineVisible( false );
}
QList<QAbstractSeries*> listSeries = m_pChart->series();
for ( int i=0; i<listSeries.count(); i++ )
{
QStackedBarSeries* pBarSeries = dynamic_cast<QStackedBarSeries*>(listSeries.at(i));
if ( pBarSeries == NULL )
continue;
QList<QBarSet*> listBar = pBarSeries->barSets();
for ( int l=0; l<listBar.count(); l++ )
listBar.at(l)->setBrush( QBrush(m_pQueryThread->m_listAlarmLevelColor.at(l)) );
}
}
void CAlarmCompare::slotPushButtonQueryClicked( bool )
{
// 清空界面
resetUi();
// 清空线程
m_pQueryThread->clear();
// 获取告警级别选择的字符串
QList<int> listLevelId;
m_pAlarmLevelComboBox->getSelectData( listLevelId );
if ( listLevelId.count() <= 0 )
{
showMsg( QMessageBox::Warning, tr("警告"), tr("未选择告警等级") );
return;
}
for ( int i=0; i<listLevelId.count(); i++ )
m_pQueryThread->m_sSelectedLevel = m_pQueryThread->m_sSelectedLevel + QString::number(listLevelId.at(i)) + ",";
m_pQueryThread->m_sSelectedLevel = m_pQueryThread->m_sSelectedLevel.left( m_pQueryThread->m_sSelectedLevel.count()-1 );
// 开始时间结束时间
QDate dataStart = ui->dateEdit_start->date();
QDate dataEnd = ui->dateEdit_end->date();
if ( dataStart > dataEnd )
{
showMsg( QMessageBox::Warning, tr("警告"), tr("开始时间需小于等于结束时间") );
return;
}
else if ( ui->rb_day->isChecked() )
{
if ( dataStart.addDays(7-1) < dataEnd )
{
showMsg( QMessageBox::Warning, tr("警告"), tr("当前类型最多七天") );
return;
}
else
m_pQueryThread->m_eDateType = CCompareQueryThread::EN_DateType_Day;
}
else if ( ui->rb_month->isChecked() )
{
QDate tmpDate = dataStart.addMonths(6-1);
if ( tmpDate < dataEnd )
{
showMsg( QMessageBox::Warning, tr("警告"), tr("当前类型最多六个月") );
return;
}
else
m_pQueryThread->m_eDateType = CCompareQueryThread::EN_DateType_Month;
}
else
{
return;
}
m_pQueryThread->m_nStartMs = ui->dateEdit_start->dateTime().toMSecsSinceEpoch();
m_pQueryThread->m_nEndMs = ui->dateEdit_end->dateTime().addDays(1).toMSecsSinceEpoch();
// 当前站
m_pQueryThread->m_nCurrentLocationIndex = m_nCurrentLocationIndex;
// 置复选框
if ( m_nCurrentLocationIndex == 0 )
{
for ( int i=0; i<m_listCheckBoxLocation.count(); i++ )
{
if ( m_listCheckBoxLocation.at(i)->isChecked() )
m_pQueryThread->m_listCheckBox << m_listCheckBoxLocation.at(i);
}
if ( m_pQueryThread->m_listCheckBox.count() == 0 )
{
showMsg( QMessageBox::Warning,tr("警告"), tr("请选择位置"));
return;
}
else if ( m_pQueryThread->m_listCheckBox.count() < 2 )
{
showMsg( QMessageBox::Warning, tr("警告"), tr("位置最少选择2个") );
return;
}
else if ( m_pQueryThread->m_listCheckBox.count() > CN_MAXBARCOUNT )
{
showMsg( QMessageBox::Warning, tr("警告"), tr("位置最多选择%1个").arg(CN_MAXBARCOUNT) );
return;
}
}
else
{
for ( int i=0; i<m_listCheckBoxDeviceGroup.count(); i++ )
{
if ( m_listCheckBoxDeviceGroup.at(i)->isChecked() )
m_pQueryThread->m_listCheckBox << m_listCheckBoxDeviceGroup.at(i);
}
if ( m_pQueryThread->m_listCheckBox.count() == 0 )
{
showMsg( QMessageBox::Warning, tr("警告"), tr("请选择设备组") );
return;
}
else if ( m_pQueryThread->m_listCheckBox.count() < 2 )
{
showMsg( QMessageBox::Warning, tr("警告"), tr("位置最少选择2个") );
return;
}
else if ( m_pQueryThread->m_listCheckBox.count() > CN_MAXBARCOUNT )
{
showMsg( QMessageBox::Warning, tr("警告"), tr("设备组最多选择%1个").arg(CN_MAXBARCOUNT) );
return;
}
}
// 查询关键字
m_pQueryThread->m_sKeyWord = ui->lineEdit_keyWord->text();
// 开始在线程中查询
m_pProcessDialog->reset();
m_pProcessDialog->show();
this->setCursor(Qt::WaitCursor);
m_pQueryThread->start();
}
void CAlarmCompare::slotQueryEnd()
{
// 显示柱状图
showBar();
// 隐藏进度条,恢复鼠标形状
m_pProcessDialog->hide();
this->setCursor(Qt::ArrowCursor);
// 显示告警详细信息
if ( m_listBarSet.count() > 0 && m_listBarSet.at(0).count() > 0 )
slotBarClicked( 0, m_listBarSet.at(0).at(0) );
}
void CAlarmCompare::slotCheckBoxStateChanged( int )
{
showBar();
}
void CAlarmCompare::slotBarMouseMoveHovered( bool bStatus, int nIndex, QBarSet* pBarset )
{
if ( m_bAfterMsgBox )
{
m_bAfterMsgBox = false;
return;
}
if ( !bStatus )
{
m_pLabelValue->hide();//进行隐藏
return;
}
int nDeviceIndex = pBarset->label().split("_").at(0).toInt();
if ( nDeviceIndex < 0 )
return;
QString sShow = m_pQueryThread->m_listCheckBox.at(nDeviceIndex)->text() + "\n";
for ( int i=0; i<m_pQueryThread->m_listAlarmLevel.count(); i++ )
{
sShow = sShow + m_pQueryThread->m_listAlarmLevel.at(i)->sName + " : " +
QString::number(m_listBarSet.at(nDeviceIndex).at(i)->at(nIndex));
if ( i != m_pQueryThread->m_listAlarmLevel.count() - 1 )
sShow += "\n";
}
m_pLabelValue->setText( sShow );
QPoint curPos = mapFromGlobal( QCursor::pos() );
m_pLabelValue->move( curPos.x()-100, curPos.y()+10 );//移动数值
m_pLabelValue->show();//显示出来
m_pLabelValue->adjustSize();
}
void CAlarmCompare::slotBarClicked( int nIndex, QBarSet* pBarset )
{
m_pQueryThread->m_pAlarmModel->beginReset();
m_pQueryThread->m_pAlarmModel->clearAlarmList();
m_pQueryThread->m_pAlarmModel->endReset();
int nDeviceIndex = pBarset->label().split("_").at(0).toInt();
if ( nDeviceIndex < 0 )
return;
m_pQueryThread->m_pAlarmModel->beginReset();
m_pQueryThread->m_pAlarmModel->setAlarmList( m_pQueryThread->m_listAlarm.at(nDeviceIndex).at(nIndex)->listAlarmInfo );
m_pQueryThread->m_pAlarmModel->endReset();
resizeColumnsToContents( ui->tableView_alarm );
ui->label_name->setText( m_pQueryThread->m_listCheckBox.at(nDeviceIndex)->text() + " " +
m_pQueryThread->m_listAlarm.at(nDeviceIndex).at(nIndex)->sName );
if ( m_pQueryThread->m_listAlarm.at(nDeviceIndex).at(nIndex)->listAlarmInfo.count() >= CN_ALARMINFO_MAXROW )
{
m_pLabelValue->hide();
showMsg( QMessageBox::Information, tr("提示"), QString(tr("告警条数过多,仅显示前 %1 条")).arg(CN_ALARMINFO_MAXROW) );
m_bAfterMsgBox = true;
}
}
void CAlarmCompare::resizeEvent( QResizeEvent* )
{
resizeColumnsToContents( ui->tableView_alarm );
}
void CAlarmCompare::paintEvent(QPaintEvent *pEvent)
{
//< 用于qss设置背景图片
Q_UNUSED(pEvent)
QStyleOption op;
op.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &op, &p, this);
}