From 7dc0404481215df93bd88e52a9b3273a759e4eab Mon Sep 17 00:00:00 2001 From: chenhuijun Date: Fri, 19 Apr 2024 14:08:44 +0800 Subject: [PATCH] Add WarningLabel and WarningMessageWidget to App, and add some reference event to EventCenter --- src/components/WarningLabel.cpp | 55 ++++++ src/components/WarningLabel.h | 24 +++ src/components/WarningMessageWidget.cpp | 223 ++++++++++++++++++++++++ src/components/WarningMessageWidget.h | 43 +++++ src/event/EventCenter.h | 6 + src/stylesheet/Dark2.css | 115 +++++++++++- 6 files changed, 458 insertions(+), 8 deletions(-) create mode 100644 src/components/WarningLabel.cpp create mode 100644 src/components/WarningLabel.h create mode 100644 src/components/WarningMessageWidget.cpp create mode 100644 src/components/WarningMessageWidget.h diff --git a/src/components/WarningLabel.cpp b/src/components/WarningLabel.cpp new file mode 100644 index 0000000..2171e8a --- /dev/null +++ b/src/components/WarningLabel.cpp @@ -0,0 +1,55 @@ +#include "WarningLabel.h" + +#include +#include +#include +#include +#include + + + +WarningLabel::WarningLabel(QWidget *aParent, WarningType aType, const QString& aMessageTime, const QString& content):QWidget(aParent) +{ + setObjectName("WarningLabelWidget"); + auto layout = new QVBoxLayout(this); + layout->setMargin(3); + layout->setSpacing(3); + + auto titleLayout = new QHBoxLayout; + titleLayout->setMargin(0); + layout->addLayout(titleLayout); + auto icon = new QLabel; + icon->setObjectName("WarningLabelIcon"); + titleLayout->addWidget(icon); + + auto title = new QLabel; + titleLayout->addWidget(title); + title->setObjectName("WarningLabelTitle"); + + titleLayout->addSpacerItem(new QSpacerItem(2,2,QSizePolicy::Expanding)); + + + auto contentText = new QLabel; + contentText->setText(content); + layout->addWidget(contentText); + contentText->setObjectName("WarningLabelText"); + + if(aType==ERROR){ + QPixmap pixmap(":/icons/dicom/echo_fail.png"); + icon->setPixmap(pixmap.scaledToHeight(20, Qt::SmoothTransformation)); + title->setText(aMessageTime); + } + else{ + QPixmap pixmap(":/icons/dicom/warning.png"); + icon->setPixmap(pixmap.scaledToHeight(20, Qt::SmoothTransformation)); + title->setText(aMessageTime); + } +} + +void WarningLabel::paintEvent(QPaintEvent * aEvent) +{ + QStyleOption opt; + opt.init(this); + QPainter p(this); + style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); +} diff --git a/src/components/WarningLabel.h b/src/components/WarningLabel.h new file mode 100644 index 0000000..2424142 --- /dev/null +++ b/src/components/WarningLabel.h @@ -0,0 +1,24 @@ +#ifndef _GUI_WARNINGLABEL_H__ +#define _GUI_WARNINGLABEL_H__ +#include +#include + + +enum WarningType{ + ERROR, WARNING +}; + +class WarningLabel: public QWidget { +Q_OBJECT +public: + explicit WarningLabel(QWidget *aParent, WarningType aType,const QString& aMessageTime, const QString& content); + ~WarningLabel() =default; +protected: + void paintEvent(QPaintEvent * aEvent); + + +private: + +}; + +#endif // __WARNINGLABEL_H__ \ No newline at end of file diff --git a/src/components/WarningMessageWidget.cpp b/src/components/WarningMessageWidget.cpp new file mode 100644 index 0000000..43936c1 --- /dev/null +++ b/src/components/WarningMessageWidget.cpp @@ -0,0 +1,223 @@ +#include "WarningMessageWidget.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#include "SlideWidget.h" +#include "WarningLabel.h" +#include "event/EventCenter.h" + + +WarningMessageWidget::WarningMessageWidget(QWidget *parent):QWidget(parent) +, mWarnWidget(new SlideWidget(this)) +, mErrorsWidget(new SlideWidget(this)) +, mScrollArea(new QScrollArea(this)) +, mScrollAreaW(new QScrollArea(this)) +, mErrorCount(0) +, mWarningCount(0) +, mErrorEmptyMessage(new QLabel(tr("System is working properly."))) +, mWarningEmptyMessage(new QLabel(tr("No message."))) +{ + // setWindowFlags(windowFlags()|Qt::FramelessWindowHint|Qt::Popup); + setWindowFlags(windowFlags()|Qt::FramelessWindowHint); + setWindowOpacity(1); + setAttribute(Qt::WA_TranslucentBackground); + setObjectName("WarningPanel"); + auto layout = new QVBoxLayout(this); + + //error panel + auto errorPanel = new QWidget; + errorPanel->setObjectName("ErrorPanelContent"); + layout->addWidget(errorPanel); + auto errorLayout = new QVBoxLayout(errorPanel); + auto errorTitle = new QLabel; + errorTitle->setObjectName("WarningPanelTitle"); + errorTitle->setText(tr("System Status")); + errorLayout->addWidget(errorTitle); + errorLayout->setMargin(0); + errorLayout->addWidget(mErrorEmptyMessage,0, Qt::AlignCenter | Qt::AlignHCenter); + + //error slide widget + mScrollArea = new QScrollArea; + mScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + errorLayout->addWidget(mScrollArea); + mErrorsWidget = new SlideWidget(this); + mScrollArea->setWidget(mErrorsWidget); + mScrollArea->setWidgetResizable(true); + mErrorsWidget->setVerticalScrollBar(mScrollArea->verticalScrollBar()); + { + auto l = new QVBoxLayout(mErrorsWidget); + l->setMargin(3); + l->addSpacerItem(new QSpacerItem(2,2,QSizePolicy::Expanding,QSizePolicy::Expanding)); + } + mScrollArea->setVisible(false); + + //warning panel + auto warningPanel = new QWidget(this); + warningPanel->setObjectName("WarningPanelContent"); + + layout->addWidget(warningPanel); + auto warningLayout = new QVBoxLayout(warningPanel); + auto warningTitle = new QLabel; + warningTitle->setObjectName("WarningPanelTitle"); + warningTitle->setText(tr("System Notifications")); + warningLayout->addWidget(warningTitle); + warningLayout->setMargin(0); + warningLayout->addWidget(mWarningEmptyMessage, 0,Qt::AlignCenter | Qt::AlignHCenter); + + mScrollAreaW->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + warningLayout->addWidget(mScrollAreaW); + mScrollAreaW->setVisible(false); + + mScrollAreaW->setWidget(mWarnWidget); + mScrollAreaW->setWidgetResizable(true); + mWarnWidget->setVerticalScrollBar(mScrollAreaW->verticalScrollBar()); + { + auto l = new QVBoxLayout(mWarnWidget); + l->setMargin(3); + l->addSpacerItem(new QSpacerItem(2,2,QSizePolicy::Expanding,QSizePolicy::Expanding)); + } + + mScrollAreaW->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); + + auto hl = new QHBoxLayout; + hl->setMargin(0); + warningLayout->addLayout(hl); + auto clearBtn= new QToolButton(this); + clearBtn->setText(tr("Clear")); + clearBtn->setObjectName("WarningPanelBtn"); + hl->addWidget(clearBtn); + connect(clearBtn, &QToolButton::clicked, this, &WarningMessageWidget::clearWarning); + +} + +void WarningMessageWidget::setPopupArgs(int aHeight, int aX, int aY) +{ + if (nullptr != showAnimation) return; + showAnimation = new QPropertyAnimation(this,"pos",this); + showAnimation->setDuration(250); + showAnimation->setStartValue(QPoint(aX,aY-10)); + showAnimation->setEndValue(QPoint(aX-358,aY-10)); + this->setFixedHeight(aHeight -aY -50 ); + connect(EventCenter::Default(),&EventCenter::WarningMessageRaise,this,&WarningMessageWidget::addWarningProxySlot,Qt::QueuedConnection); + connect(EventCenter::Default(),&EventCenter::ErrorStateUnactive,this,&WarningMessageWidget::unactiveErrorProxySlot,Qt::QueuedConnection); + +} + +void WarningMessageWidget::addWarning(const QString& aDateTime, const QString& aContent) +{ + QMutexLocker locker(&mWarningMutex); + auto label = new WarningLabel(mWarnWidget,WARNING,aDateTime,aContent); + qobject_cast(mWarnWidget->layout())->insertWidget(0,label); + if(mWarningCount==0){ + mWarningEmptyMessage->setVisible(false); + mScrollAreaW->setVisible(true); + } + mWarningCount++; +} + +void WarningMessageWidget::addWarningProxySlot(QObject*, QObject* aData) +{ + if (!aData){ + qDebug()<<"addLabelProxy with null aData"; + return; + } + QString str = *((QString*)aData); + activeError(601, str); + addWarning(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"), str); +} + +void WarningMessageWidget::activeErrorProxySlot(QObject*, QObject* aData) +{ + if (!aData){ + qDebug()<<"activeErrorProxy with null aData"; + return; + } + //TODO: real active error logic + QString str = "Test Error"; + activeError(601, str); +} + +void WarningMessageWidget::unactiveErrorProxySlot(QObject*, QObject* aData) +{ + //TODO: real unactive error logic + // if (!aData){ + // qDebug()<<"unactiveErrorProxy with null aData"; + // return; + // } + unactiveError(601); +} + +void WarningMessageWidget::clearWarning() +{ + QMutexLocker locker(&mWarningMutex); + auto lbls = mWarnWidget->children(); + for (QObject* item: lbls) + { + WarningLabel* lbl = qobject_cast(item); + if (!lbl) continue; + mWarnWidget->layout()->removeWidget(lbl); + lbl->deleteLater(); + } + mWarningCount=0; + mWarningEmptyMessage->setVisible(true); + mScrollAreaW->setVisible(false); +} + +void WarningMessageWidget::activeError(int aCode, const QString&aContent) +{ + QMutexLocker locker(&mErrorMutex); + auto lbls = mErrorsWidget->children(); + for (QObject* item: lbls) + { + WarningLabel* lbl = qobject_cast(item); + if (!lbl) continue; + if (lbl->property("code").toInt() == aCode) return; + } + auto label = new WarningLabel(mErrorsWidget,ERROR,QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),aContent); + label->setProperty("code",aCode); + qobject_cast(mErrorsWidget->layout())->insertWidget(0,label); + if(mErrorCount==0){ + mErrorEmptyMessage->setVisible(false); + mScrollArea->setVisible(true); + } + mErrorCount++; +} + +void WarningMessageWidget::unactiveError(int aCode) +{ + QMutexLocker locker(&mErrorMutex); + auto lbls = mErrorsWidget->children(); + for (QObject* item: lbls) + { + WarningLabel* lbl = qobject_cast(item); + if (!lbl) continue; + if (lbl->property("code").toInt() == aCode){ + mErrorsWidget->layout()->removeWidget(lbl); + lbl->deleteLater(); + mErrorCount--; + break; + } + } + if (mErrorCount==0){ + mErrorEmptyMessage->setVisible(true); + mScrollArea->setVisible(false); + } +} + +void WarningMessageWidget::showEvent(QShowEvent* aEvent) +{ + if (nullptr == showAnimation)return; + showAnimation->start(); + QWidget::showEvent(aEvent); +} + \ No newline at end of file diff --git a/src/components/WarningMessageWidget.h b/src/components/WarningMessageWidget.h new file mode 100644 index 0000000..67794cd --- /dev/null +++ b/src/components/WarningMessageWidget.h @@ -0,0 +1,43 @@ +#ifndef _CUI_WARNINGMESSAGEWIDGET_H__ +#define _CUI_WARNINGMESSAGEWIDGET_H__ +#include +#include +#include +class QPropertyAnimation; +class SlideWidget; +class QScrollArea; +class QLabel; +class WarningMessageWidget: public QWidget { +Q_OBJECT +public: + explicit WarningMessageWidget(QWidget *parent = nullptr); + ~WarningMessageWidget() =default; + void setPopupArgs(int aHeight, int aX, int aY); + void addWarning(const QString& aDateTime, const QString& aContent); + void clearWarning(); + void activeError(int aCode, const QString&aContent); + void unactiveError(int aCode); +protected: + void showEvent(QShowEvent* aEvent); +protected slots: + void addWarningProxySlot(QObject*, QObject* aData); + void activeErrorProxySlot(QObject*, QObject* aData); + void unactiveErrorProxySlot(QObject*, QObject* aData); + +private: + QPropertyAnimation* showAnimation = nullptr; + SlideWidget * mErrorsWidget; + SlideWidget * mWarnWidget; + QScrollArea* mScrollArea; + QScrollArea* mScrollAreaW; + QLabel* mErrorEmptyMessage; + QLabel* mWarningEmptyMessage; + QMutex mWarningMutex; + QMutex mErrorMutex; + + int mErrorCount; + int mWarningCount; + +}; + +#endif // __WARNINGMESSAGEWIDGET_H__ \ No newline at end of file diff --git a/src/event/EventCenter.h b/src/event/EventCenter.h index 1a5b4cd..3dd2de2 100644 --- a/src/event/EventCenter.h +++ b/src/event/EventCenter.h @@ -41,6 +41,12 @@ ADD_EVENT_VALUE(RequestScreenSaver)\ ADD_EVENT_VALUE(ReconConnectionUpdated)\ ADD_EVENT_VALUE(InputWorkListSearchValue)\ ADD_EVENT_VALUE(DoWorkListSearch)\ +ADD_EVENT_VALUE(WarningMessageRaise)\ +ADD_EVENT_VALUE(ErrorStateActive)\ +ADD_EVENT_VALUE(ErrorStateUnactive)\ + + + enum GUIEvents { #define ADD_EVENT_VALUE(val) val, diff --git a/src/stylesheet/Dark2.css b/src/stylesheet/Dark2.css index e0633c0..f394d57 100644 --- a/src/stylesheet/Dark2.css +++ b/src/stylesheet/Dark2.css @@ -258,6 +258,33 @@ QLabel#AlertDialogTitle border-bottom: 1px solid grey; } +/*------Global Scrollbar----------------------------------------------------------*/ +QScrollBar:vertical { + background: #3c3c3c; + width: 12px; + margin-left: 1px; + + margin-right: 2px; + margin-bottom: 5px; +} +QScrollBar::handle:vertical { + background: silver; + min-height: 20px; + border-radius: 3px; + padding: 2px 2px 2px 2px; + +} +QScrollBar::add-line:vertical { + display: none; + background: none; +} + +QScrollBar::sub-line:vertical { + display: none; + background: none; +} + + /*------LoginWindow----------------------------------------------------------*/ QFrame#loginFrame { min-width: 700px; @@ -394,14 +421,6 @@ QLabel#link { qproperty-scaledContents:true; } -QLabel#ready { - min-width: 28px; - max-width: 28px; - min-height: 28px; - max-height: 28px; - qproperty-pixmap:url(":/icons/ready.png"); - qproperty-scaledContents:true; -} QLabel#company { min-width: 150px; @@ -412,6 +431,86 @@ QLabel#systemMsgBar { min-width: 500px; } +QToolButton#ready { + min-width: 28px; + max-width: 28px; + min-height: 28px; + max-height: 28px; + background: transparent; + qproperty-icon:url(":/icons/dicom/echo_suc.png"); + qproperty-toolButtonStyle:ToolButtonTextBesideIcon; + qproperty-iconSize:26px 26px; +} + +/*------WarningMessageWidget-------------------------------------------------*/ +QWidget#WarningPanel{ + min-width: 360px; + max-width: 500px; + /* background: darkgray; */ + +} +QWidget#WarningLabelWidget{ + background: #505050; + border-radius: 5px; + padding: 5px; + padding-right: 0px; + margin-left: 2px; +} +QLabel#WarningLabelIcon{ + min-height: 20px; + max-height: 20px; + margin-left: 3px; +} +QLabel#WarningLabelTitle{ + font-size: 12px; + +} +QLabel#WarningPanelTitle{ + font-size: 20px; + border-bottom: 1px solid darkgoldenrod; + margin-left: 5px; + margin-right: 5px; + min-height: 30px; + max-height:30px; +} +QWidget#ErrorPanelContent QLabel#WarningLabelText{ + font-size: 12px; + qproperty-wordWrap:true; + margin-left: 1px; + border-top: 1px solid darkgoldenrod ; + padding-top: 3px; +} +QWidget#WarningPanelContent QLabel#WarningLabelText{ + font-size: 12px; + qproperty-wordWrap:true; + margin-left: 1px; + border-top: 1px solid darkgoldenrod; + padding-top: 3px; +} +QWidget#ErrorPanelContent{ + border: 1px solid darkgoldenrod; + border-radius: 5px; + min-height: 300px; + max-height: 300px; + background: #323232; +} +QWidget#WarningPanelContent{ + border: 1px solid darkgoldenrod; + border-radius: 5px; + min-height: 600px; + max-height: 800px; + background: #323232; +} +QToolButton#WarningPanelBtn{ + font-size: 18px; + min-height: 30px; + max-height: 30px; + min-width: 120px; + max-width:120px; + border:2px solid #505050; + margin-bottom: 10px; +} + /*------TabFormWidget--------------------------------------------------------*/ TabFormWidget { border-top: 1px solid #505050;