Add MultyMessageDialog.
This commit is contained in:
@@ -7,60 +7,67 @@
|
||||
#include <QApplication>
|
||||
#include <QWidget>
|
||||
#include <QThread>
|
||||
#include <qDebug>
|
||||
#include <QDebug>
|
||||
|
||||
#include "event/EventCenter.h"
|
||||
#include "dialogs/GUIMessageDialog.h"
|
||||
#include "dialogs/ChangePasswordFormDialog.h"
|
||||
#include "dialogs/AccountFormDialog.h"
|
||||
#include "dialogs/MultyMessageDialogManager.h"
|
||||
#include "appvals/AppGlobalValues.h"
|
||||
#include "windows/LoginDialog.h"
|
||||
#include "shimlib/ShimLib.h"
|
||||
|
||||
|
||||
void messageCallback(const char* aMessage,unsigned int aMessageLevel)
|
||||
{
|
||||
DialogManager::Default()->raiseMultyMessageDialog(QString::fromLatin1(aMessage),MessageLevel(aMessageLevel));
|
||||
}
|
||||
|
||||
DialogManager::DialogManager()
|
||||
: QObject()
|
||||
, mFunctionDialog(nullptr)
|
||||
, mMessageDialog(nullptr)
|
||||
, topWidget(nullptr)
|
||||
, mTopWidget(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DialogManager::init() {
|
||||
void DialogManager::init(QWidget* aParent) {
|
||||
connect(EventCenter::Default(), &EventCenter::DeviceErrorRaise,this,&DialogManager::raiseDeviceError);
|
||||
connect(EventCenter::Default(), &EventCenter::InvokeOperationStart,this,&DialogManager::invokeOperationStart);
|
||||
connect(EventCenter::Default(), &EventCenter::InvokeOperationProgress,this,&DialogManager::invokeOperationProgress);
|
||||
connect(EventCenter::Default(), &EventCenter::InvokeOperationPending,this,&DialogManager::invokeOperationPending);
|
||||
connect(EventCenter::Default(), &EventCenter::InvokeOperationEnd,this,&DialogManager::invokeOperationEnd);
|
||||
MultyMessageDialogManager::getInstance()->setDialogParent(aParent);
|
||||
SetMessageCallback(messageCallback);
|
||||
}
|
||||
|
||||
//得考虑多线程的问题
|
||||
void DialogManager::setTopWidget(QWidget* widget) {
|
||||
std::lock_guard<std::mutex> lockGuard(mutex);
|
||||
if (!topWidget&& QApplication::activeWindow()){
|
||||
topWidget = QApplication::activeWindow();
|
||||
std::lock_guard<std::mutex> lockGuard(mMutex);
|
||||
if (nullptr == widget)
|
||||
{
|
||||
return;
|
||||
}
|
||||
qDebug()<<"last top:"<<topWidget->objectName()<<", new top:"<<widget->objectName();
|
||||
widget->setParent(topWidget,widget->windowFlags());
|
||||
topWidget = widget;
|
||||
if (!mTopWidget&& QApplication::activeWindow()){
|
||||
mTopWidget = QApplication::activeWindow();
|
||||
}
|
||||
qDebug()<<"last top:"<<mTopWidget->objectName()<<", new top:"<<widget->objectName();
|
||||
widget->setParent(mTopWidget,widget->windowFlags());
|
||||
mTopWidget = widget;
|
||||
++mDialogCount;
|
||||
}
|
||||
|
||||
void DialogManager::releaseTopWidget(QWidget* expectedTopWidget) {
|
||||
std::lock_guard<std::mutex> lockGuard(mutex);
|
||||
if (topWidget == expectedTopWidget){
|
||||
topWidget = expectedTopWidget->parentWidget();
|
||||
std::lock_guard<std::mutex> lockGuard(mMutex);
|
||||
if (nullptr == expectedTopWidget)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else{
|
||||
while (auto tempWidget = topWidget->parentWidget()){
|
||||
if (tempWidget->parentWidget() == expectedTopWidget){
|
||||
tempWidget->setParent(expectedTopWidget->parentWidget());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (mTopWidget == expectedTopWidget){
|
||||
mTopWidget = expectedTopWidget->parentWidget();
|
||||
--mDialogCount;
|
||||
}
|
||||
--mDialogCount;
|
||||
}
|
||||
|
||||
void DialogManager::requestLogin()
|
||||
@@ -119,7 +126,6 @@ void DialogManager::requestChangePassword() {
|
||||
//考虑以后使用另外的dialog显示错误
|
||||
void DialogManager::raiseDeviceError(QObject *parent, QObject *msg) {
|
||||
clearMessageDialog();
|
||||
//new dialog
|
||||
auto dialog = new GUIMessageDialog;
|
||||
|
||||
if (msg) {
|
||||
@@ -132,6 +138,7 @@ void DialogManager::raiseDeviceError(QObject *parent, QObject *msg) {
|
||||
dialog->showExitButton();
|
||||
setTopWidget(dialog);
|
||||
dialog->setWindowModality(Qt::NonModal);
|
||||
dialog->showFullScreen ();
|
||||
dialog->exec();
|
||||
releaseTopWidget(dialog);
|
||||
dialog->deleteLater();
|
||||
@@ -214,7 +221,6 @@ void DialogManager::invokeOperationEnd(QObject *parent, QObject *msg) {
|
||||
mMessageDialog->accept();
|
||||
}
|
||||
delete mMessageDialog;
|
||||
mMessageDialog = nullptr;
|
||||
AppGlobalValues::setInProcessing(false);
|
||||
}
|
||||
}
|
||||
@@ -223,10 +229,14 @@ void DialogManager::clearMessageDialog() {
|
||||
if (mMessageDialog){
|
||||
if (!mMessageDialog->isHidden()) mMessageDialog->hide();
|
||||
delete mMessageDialog;
|
||||
mMessageDialog = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
DialogManager::~DialogManager() {
|
||||
clearMessageDialog();
|
||||
}
|
||||
|
||||
void DialogManager::raiseMultyMessageDialog(const QString aMessage, MessageLevel aMessageLevel)
|
||||
{
|
||||
MultyMessageDialogManager::getInstance()->raiseDialog(aMessage, aMessageLevel);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define GUI_DIALOGMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
#include <mutex>
|
||||
#include <QStack>
|
||||
#include <atomic>
|
||||
@@ -14,6 +15,7 @@ class QWidget;
|
||||
class QDialog;
|
||||
class GUIMessageDialog;
|
||||
class QSqlTableModel;
|
||||
enum MessageLevel:unsigned int;
|
||||
|
||||
class DialogManager:public QObject {
|
||||
public:
|
||||
@@ -26,7 +28,7 @@ public:
|
||||
|
||||
~DialogManager() override;
|
||||
|
||||
void init();
|
||||
void init(QWidget* aParent);
|
||||
void setTopWidget(QWidget* widget);
|
||||
void releaseTopWidget(QWidget* expectedTopWidget);
|
||||
void requestLogin();
|
||||
@@ -35,6 +37,7 @@ public:
|
||||
int requestEditAdminAccount(const QMap<QString, QVariant>& values);
|
||||
void requestChangePassword();
|
||||
void raiseDeviceError(QObject* parent, QObject* msg);
|
||||
void raiseMultyMessageDialog(const QString aMessage,MessageLevel aMessageLevel);
|
||||
void invokeOperationStart(QObject* parent, QObject* msg);
|
||||
void invokeOperationProgress(QObject* parent, QObject* msg);
|
||||
void invokeOperationPending(QObject* parent, QObject* msg);
|
||||
@@ -43,9 +46,9 @@ private:
|
||||
void clearMessageDialog();
|
||||
|
||||
QDialog* mFunctionDialog;
|
||||
GUIMessageDialog* mMessageDialog;
|
||||
QWidget* topWidget;
|
||||
std::mutex mutex;
|
||||
QPointer<GUIMessageDialog> mMessageDialog;
|
||||
QWidget* mTopWidget;
|
||||
std::mutex mMutex;
|
||||
int mDialogCount = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ GUIMessageDialog::GUIMessageDialog(QWidget *parent)
|
||||
mUI->setupUi(this);
|
||||
this->setObjectName("MessageDialog");
|
||||
this->setWindowFlags (Qt :: FramelessWindowHint | Qt :: Dialog);
|
||||
this->showFullScreen ();
|
||||
initBaseLayout();
|
||||
this->setWindowOpacity(0.6);
|
||||
initLoadingFrameString();
|
||||
|
||||
112
src/dialogs/MultyMessageDialog.cpp
Normal file
112
src/dialogs/MultyMessageDialog.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "MultyMessageDialog.h"
|
||||
#include <QHBoxLayout>
|
||||
|
||||
namespace
|
||||
{
|
||||
const int AUTO_DISAPPEAR_TIME = 5000;
|
||||
const int DIALOG_WIDTH = 240;
|
||||
const int DIALOG_HEIGHT = 50;
|
||||
|
||||
}
|
||||
|
||||
MultyMessageDialog::MultyMessageDialog(const QString& aMessage,MessageLevel aMessageLevel,QWidget* aParent)
|
||||
: QDialog(aParent)
|
||||
, mAutoDisappearTime(AUTO_DISAPPEAR_TIME)
|
||||
, mTimer(new QTimer(this))
|
||||
, mDisappearAnimation(new QPropertyAnimation(this,"pos",this))
|
||||
, mShowAnimation(new QPropertyAnimation(this,"pos",this))
|
||||
, mMessage(new QLabel(aMessage,this))
|
||||
, mIcon(new QLabel(this))
|
||||
, mLevel(aMessageLevel)
|
||||
{
|
||||
setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint| Qt::BypassWindowManagerHint);
|
||||
resize(DIALOG_WIDTH,DIALOG_HEIGHT);
|
||||
mTimer->setSingleShot(true);
|
||||
connect(mTimer,&QTimer::timeout,this,&MultyMessageDialog::startHideAnimation);
|
||||
initializeAnimation();
|
||||
initializeIcon();
|
||||
QHBoxLayout* layout = new QHBoxLayout(this);
|
||||
layout->addWidget(mIcon);
|
||||
layout->addWidget(mMessage);
|
||||
}
|
||||
|
||||
MultyMessageDialog::~MultyMessageDialog()
|
||||
{
|
||||
emit dialogDestroyed();
|
||||
}
|
||||
|
||||
void MultyMessageDialog::initializeAnimation()
|
||||
{
|
||||
mDisappearAnimation->setDuration(100);
|
||||
|
||||
mShowAnimation->setDuration(100);
|
||||
|
||||
connect(mDisappearAnimation, &QPropertyAnimation::finished, this, &MultyMessageDialog::hideDialog);
|
||||
}
|
||||
|
||||
void MultyMessageDialog::initializeIcon()
|
||||
{
|
||||
switch (mLevel)
|
||||
{
|
||||
case Info:
|
||||
mIcon->setObjectName("MultyMessageDialogInfo");
|
||||
break;
|
||||
case Warning:
|
||||
mIcon->setObjectName("MultyMessageDialogWarning");
|
||||
break;
|
||||
case Error:
|
||||
mIcon->setObjectName("MultyMessageDialogError");
|
||||
break;
|
||||
case Sucess:
|
||||
mIcon->setObjectName("MultyMessageDialogSucess");
|
||||
break;
|
||||
default:
|
||||
mIcon->setObjectName("MultyMessageDialogInfo");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void MultyMessageDialog::setMessage(const QString& aMessage)
|
||||
{
|
||||
mMessage->setText(aMessage);
|
||||
}
|
||||
|
||||
void MultyMessageDialog::startHideAnimation()
|
||||
{
|
||||
if (mTimer->isActive())
|
||||
{
|
||||
mTimer->stop();
|
||||
}
|
||||
mDisappearAnimation->setStartValue(pos());
|
||||
mDisappearAnimation->setEndValue(QPoint(pos().x(),pos().y()-30-height()));
|
||||
mDisappearAnimation->start();
|
||||
}
|
||||
|
||||
void MultyMessageDialog::hideDialog()
|
||||
{
|
||||
hide();
|
||||
deleteLater();
|
||||
}
|
||||
|
||||
void MultyMessageDialog::setAutoDisappearTime(int aAutoDisappearTime)
|
||||
{
|
||||
mAutoDisappearTime = aAutoDisappearTime;
|
||||
}
|
||||
|
||||
void MultyMessageDialog::showEvent(QShowEvent* aEvent)
|
||||
{
|
||||
mTimer->start(mAutoDisappearTime);
|
||||
mShowAnimation->setStartValue(QPoint(pos().x(),pos().y()-30-height()));
|
||||
mShowAnimation->setEndValue(pos());
|
||||
mShowAnimation->start();
|
||||
QDialog::showEvent(aEvent);
|
||||
}
|
||||
|
||||
void MultyMessageDialog::stopShowAnimation()
|
||||
{
|
||||
if (QAbstractAnimation::Running == mShowAnimation->state())
|
||||
{
|
||||
mShowAnimation->stop();
|
||||
move(mShowAnimation->endValue().toPoint());
|
||||
}
|
||||
}
|
||||
55
src/dialogs/MultyMessageDialog.h
Normal file
55
src/dialogs/MultyMessageDialog.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef MULTYMESSAGEDIALOG_H
|
||||
#define MULTYMESSAGEDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QTimer>
|
||||
#include <QLabel>
|
||||
#include <QPropertyAnimation>
|
||||
|
||||
enum MessageLevel:unsigned int
|
||||
{
|
||||
Info = 0,
|
||||
Warning,
|
||||
Error,
|
||||
Sucess,
|
||||
};
|
||||
|
||||
class MultyMessageDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
MultyMessageDialog(const QString& aMessage = "",MessageLevel aMessageLevel = MessageLevel::Info,QWidget* aParent = nullptr);
|
||||
~MultyMessageDialog() override;
|
||||
void setAutoDisappearTime(int aAutoDisappearTime);
|
||||
void setMessage(const QString& aMessage);
|
||||
void stopShowAnimation();
|
||||
|
||||
signals:
|
||||
void dialogDestroyed();
|
||||
|
||||
public slots:
|
||||
void hideDialog();
|
||||
void startHideAnimation();
|
||||
|
||||
protected:
|
||||
void showEvent(QShowEvent* aEvent) override;
|
||||
|
||||
private:
|
||||
void initializeAnimation();
|
||||
void initializeIcon();
|
||||
|
||||
private:
|
||||
int mAutoDisappearTime;
|
||||
QTimer* mTimer;
|
||||
QSize mSize;
|
||||
QPropertyAnimation* mDisappearAnimation;
|
||||
QPropertyAnimation* mShowAnimation;
|
||||
QLabel* mMessage;
|
||||
QLabel* mIcon;
|
||||
MessageLevel mLevel;
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // MULTYMESSAGEDIALOG_H
|
||||
71
src/dialogs/MultyMessageDialogManager.cpp
Normal file
71
src/dialogs/MultyMessageDialogManager.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "MultyMessageDialogManager.h"
|
||||
#include "MultyMessageDialog.h"
|
||||
|
||||
#include <QMutexLocker>
|
||||
|
||||
namespace
|
||||
{
|
||||
const int DIALOGS_MARGIN = 30;
|
||||
const int GEOMETY_X = 1620;
|
||||
const int GEOMETY_Y = 30;
|
||||
}
|
||||
MultyMessageDialogManager* MultyMessageDialogManager::getInstance()
|
||||
{
|
||||
static MultyMessageDialogManager instance;
|
||||
return &instance;
|
||||
}
|
||||
|
||||
MultyMessageDialogManager::MultyMessageDialogManager()
|
||||
: QObject()
|
||||
, mDialogParent(nullptr)
|
||||
, mDialogList()
|
||||
, mMutex()
|
||||
{
|
||||
}
|
||||
|
||||
MultyMessageDialogManager::~MultyMessageDialogManager()
|
||||
{
|
||||
}
|
||||
|
||||
void MultyMessageDialogManager::setDialogParent(QWidget* aParent)
|
||||
{
|
||||
mDialogParent = aParent;
|
||||
}
|
||||
|
||||
void MultyMessageDialogManager::raiseDialog(const QString& aMessage,MessageLevel aMessageLevel)
|
||||
{
|
||||
QMutexLocker locker(&mMutex);
|
||||
MultyMessageDialog* dialog = new MultyMessageDialog(aMessage,aMessageLevel,mDialogParent);
|
||||
mDialogList.append(dialog);
|
||||
int yDistance = GEOMETY_Y;
|
||||
for (int i = 0; i < mDialogList.count() - 1; ++i)
|
||||
{
|
||||
yDistance += mDialogList.at(i)->size().height() + DIALOGS_MARGIN;
|
||||
}
|
||||
|
||||
dialog->move(GEOMETY_X,yDistance);
|
||||
dialog->show();
|
||||
connect(dialog,&MultyMessageDialog::dialogDestroyed,this,&MultyMessageDialogManager::removeDestoryedDialog);
|
||||
}
|
||||
|
||||
void MultyMessageDialogManager::removeDestoryedDialog()
|
||||
{
|
||||
QMutexLocker locker(&mMutex);
|
||||
MultyMessageDialog* dialog = qobject_cast<MultyMessageDialog*>(sender());
|
||||
if (nullptr == dialog)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int index = mDialogList.indexOf(dialog);
|
||||
if (index >= 0)
|
||||
{
|
||||
mDialogList.removeAt(index);
|
||||
for (int i = 0; i < mDialogList.count(); ++i)
|
||||
{
|
||||
MultyMessageDialog* target = mDialogList.at(i);
|
||||
target->stopShowAnimation();
|
||||
QPoint position = target->mapToGlobal(QPoint(0, 0));
|
||||
target->move(position.x(),position.y()-dialog->height()-DIALOGS_MARGIN);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
src/dialogs/MultyMessageDialogManager.h
Normal file
30
src/dialogs/MultyMessageDialogManager.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef MULTYMESSAGEDIALOGMANAGER_H
|
||||
#define MULTYMESSAGEDIALOGMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
#include <QMutex>
|
||||
|
||||
class MultyMessageDialog;
|
||||
enum MessageLevel:unsigned int;
|
||||
|
||||
class MultyMessageDialogManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static MultyMessageDialogManager* getInstance();
|
||||
void raiseDialog(const QString& aMessage,MessageLevel aMessageLevel);
|
||||
void setDialogParent(QWidget* aParent);
|
||||
|
||||
private slots:
|
||||
void removeDestoryedDialog();
|
||||
|
||||
private:
|
||||
MultyMessageDialogManager();
|
||||
~MultyMessageDialogManager();
|
||||
QWidget* mDialogParent;
|
||||
QList<MultyMessageDialog*> mDialogList;
|
||||
QMutex mMutex;
|
||||
};
|
||||
|
||||
#endif // MULTYMESSAGEDIALOGMANAGER_H
|
||||
Reference in New Issue
Block a user