xref: /OK3568_Linux_fs/app/forlinx/flapp/src/libs/ui/messagebox.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include "messagebox.h"
2 #include <QEventLoop>
3 #include <QPushButton>
4 #include <QLabel>
5 #include <QHBoxLayout>
6 #include <QVBoxLayout>
7 #include <QPainter>
8 #include <QMessageBox>
9 #include <QApplication>
10 #include <QDesktopWidget>
11 #include <QMouseEvent>
12 #include <QDebug>
13 #include <QTimer>
14 #include <QPropertyAnimation>
15 
MessageBox(QWidget * parent,const QString & text,Button button)16 MessageBox::MessageBox(QWidget *parent, const QString &text, Button button) : QWidget(parent)
17 {
18     this->resize(300, 200);
19     m_pLoop = new QEventLoop(this);
20     QLabel *label =new QLabel;
21     label->setTextFormat(Qt::RichText);
22     label->setWordWrap(true);
23     label->setAlignment(Qt::AlignCenter);
24     label->setText(text);
25 
26     QVBoxLayout *VLayout = new QVBoxLayout(this);
27     VLayout->addSpacing(30);
28     VLayout->addWidget(label);
29 
30     QHBoxLayout *hLayout = new QHBoxLayout;
31     QPushButton *yes = new QPushButton(tr("OK"), this);
32     connect(yes, &QPushButton::clicked, this, [=](){
33         m_pLoop->exit(Button::yes);
34     });
35 
36     QPushButton *no = new QPushButton(tr("Cancel"), this);
37     connect(no, &QPushButton::clicked, this, [=](){
38         m_pLoop->exit(Button::no);
39     });
40 
41     switch (button) {
42     case Button::yes:
43     {
44         yes->setVisible(true);
45         no->setVisible(false);
46     }
47 
48         break;
49     case Button::no:
50     {
51         yes->setVisible(false);
52         no->setVisible(false);
53     }
54         break;
55     case Button::yes_no:
56     {
57     yes->setVisible(true);
58     yes->setVisible(true);
59     }break;
60 
61     default:
62         break;
63     }
64     hLayout->addStretch();
65     hLayout->addWidget(yes);
66     hLayout->addWidget(no);
67     VLayout->addLayout(hLayout);
68     setAttribute(Qt::WA_DeleteOnClose);
69 }
70 
MessageBox(QWidget * parent)71 MessageBox::MessageBox(QWidget *parent):QWidget(parent)
72 {
73   setAttribute(Qt::WA_DeleteOnClose);
74 }
75 
exec()76 int MessageBox::exec()
77 {
78     QWidget *parentWidget = dynamic_cast<QWidget*>(parent());
79     if(parentWidget != Q_NULLPTR)
80     {
81         int x = (parentWidget->width()-width())/2;
82         int y = (parentWidget->height()-height())/2;
83         move(x, y);
84         filterChild(parentWidget);
85 
86     }else{
87         this->setWindowFlags(Qt::FramelessWindowHint);
88         int x = (qApp->desktop()->width()-width())/2;
89         int y = (qApp->desktop()->height()-height())/2;
90         move(x, y);
91 
92     }
93     this->setVisible(true);
94     int ret = m_pLoop->exec();
95 
96     if(parentWidget != Q_NULLPTR)
97     {
98         //解除其它窗口部件操作.
99         foreach (QObject *o, m_object) {
100             o->removeEventFilter(this);
101         }
102         m_object.clear();
103     }
104 
105     delete m_pLoop;
106     return ret;
107 }
108 
paintEvent(QPaintEvent * e)109 void MessageBox::paintEvent(QPaintEvent *e)
110 {
111     Q_UNUSED(e)
112     QWidget::paintEvent(e);
113     QPainter painter(this);
114 
115     painter.fillRect(this->rect(), QColor(251, 251, 251));
116     painter.setPen(Qt::blue);
117     painter.drawText(20, 20, "Message");
118     QPen pen(Qt::blue);
119     pen.setWidthF(2.0);
120     painter.setPen(pen);
121     painter.drawLine(QPoint(20, 25), QPoint(this->width()-20, 25));
122 }
123 
eventFilter(QObject * watched,QEvent * event)124 bool MessageBox::eventFilter(QObject *watched, QEvent *event)
125 {
126     //过滤所非消息窗口的鼠标点击和聚集事件,使只能操作当强消息窗口.
127     QInputEvent  *pMouse = dynamic_cast<QInputEvent*>(event);
128     QFocusEvent *pFocus = dynamic_cast<QFocusEvent*>(event);
129     if(m_object.contains(watched)  && (pMouse|| pFocus))
130     {
131         event->ignore();
132         return true;
133     }
134 
135     return QWidget::eventFilter(watched, event);
136 }
137 
filterChild(QObject * o)138 void MessageBox::filterChild(QObject *o)
139 {
140     foreach (QObject *child, o->children()) {
141          if( child !=this)
142          {
143              m_object << child;
144              child->installEventFilter(this);
145              filterChild(child);
146          }
147     }
148 }
149 
showMessage(QWidget * parent,const QString & text,Button button)150 int MessageBox::showMessage(QWidget *parent, const QString &text,  Button button)
151 {
152     MessageBox tip(parent, text,  button);
153     return tip.exec();
154 }
155 
showDuringMessage(QWidget * parent,const QString & text,int duringSeconds)156 int MessageBox::showDuringMessage(QWidget *parent, const QString &text, int duringSeconds)
157 {
158     MessageBox tip(parent, text,  MessageBox::no);
159     QPropertyAnimation pro(&tip, "windowOpacity");
160     pro.setStartValue(0.1);
161     pro.setEndValue(1.0);
162     pro.setDuration(duringSeconds*1000);
163     connect(&pro, &QPropertyAnimation::finished, &tip, [=, &tip](){
164         tip.m_pLoop->exit();;
165     });
166     pro.start();
167     return tip.exec();
168 }
169