qt-material-widgets/components/dialog.cpp

205 lines
4.6 KiB
C++
Raw Normal View History

2016-03-19 08:32:49 +00:00
#include "dialog.h"
2016-06-19 14:27:21 +00:00
#include "dialog_p.h"
2016-06-19 14:13:37 +00:00
#include <QPainter>
2016-06-21 09:18:51 +00:00
#include <QApplication>
#include <QStateMachine>
#include <QSignalTransition>
#include <QPropertyAnimation>
2016-06-20 15:47:17 +00:00
#include <QEvent>
#include <QPushButton>
#include <QGraphicsDropShadowEffect>
#include "lib/transparencyproxy.h"
2016-06-19 14:13:37 +00:00
#include "dialog_internal.h"
DialogPrivate::DialogPrivate(Dialog *q)
2016-06-21 09:18:51 +00:00
: q_ptr(q),
machine(new QStateMachine(q)),
window(new DialogWindow(q)),
proxy(new TransparencyProxy)
2016-06-19 14:13:37 +00:00
{
}
void DialogPrivate::init()
{
Q_Q(Dialog);
2016-06-20 15:47:17 +00:00
QVBoxLayout *layout = new QVBoxLayout;
q->setLayout(layout);
2016-06-21 09:55:38 +00:00
q->setAttribute(Qt::WA_TransparentForMouseEvents);
2016-06-20 15:47:17 +00:00
QWidget *widget = new QWidget;
widget->setLayout(proxy);
layout->addWidget(widget);
layout->setAlignment(widget, Qt::AlignCenter);
widget->setMinimumWidth(400);
proxy->setWidget(window);
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
effect->setColor(QColor(0, 0, 0, 200));
effect->setBlurRadius(64);
effect->setOffset(0, 13);
widget->setGraphicsEffect(effect);
2016-06-21 09:18:51 +00:00
//
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
QState *hiddenState = new QState;
QState *visibleState = new QState;
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
machine->addState(hiddenState);
machine->addState(visibleState);
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
machine->setInitialState(hiddenState);
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
QSignalTransition *transition;
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
transition = new QSignalTransition(window, SIGNAL(dialogActivated()));
transition->setTargetState(visibleState);
hiddenState->addTransition(transition);
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
transition = new QSignalTransition(window, SIGNAL(dialogDeactivated()));
transition->setTargetState(hiddenState);
visibleState->addTransition(transition);
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
visibleState->assignProperty(proxy, "opacity", 1);
visibleState->assignProperty(effect, "color", QColor(0, 0, 0, 200));
visibleState->assignProperty(window, "offset", 0);
hiddenState->assignProperty(proxy, "opacity", 0);
hiddenState->assignProperty(effect, "color", QColor(0, 0, 0, 0));
hiddenState->assignProperty(window, "offset", 200);
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
QObject::connect(proxy, SIGNAL(opacityChanged()), q, SLOT(update()));
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
QPropertyAnimation *animation;
2016-06-20 15:47:17 +00:00
2016-06-21 10:22:22 +00:00
animation = new QPropertyAnimation(proxy, "opacity", q);
2016-06-21 09:18:51 +00:00
animation->setDuration(280);
machine->addDefaultAnimation(animation);
2016-06-20 15:47:17 +00:00
2016-06-21 10:22:22 +00:00
animation = new QPropertyAnimation(effect, "color", q);
2016-06-21 09:18:51 +00:00
animation->setDuration(280);
machine->addDefaultAnimation(animation);
2016-06-20 15:47:17 +00:00
2016-06-21 10:22:22 +00:00
animation = new QPropertyAnimation(window, "offset", q);
2016-06-21 09:18:51 +00:00
animation->setDuration(280);
animation->setEasingCurve(QEasingCurve::OutCirc);
machine->addDefaultAnimation(animation);
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
QObject::connect(visibleState, SIGNAL(propertiesAssigned()), q, SLOT(acceptMouseEvents()));
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
machine->start();
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
QCoreApplication::processEvents();
2016-06-19 14:13:37 +00:00
}
2016-06-17 11:43:04 +00:00
Dialog::Dialog(QWidget *parent)
2016-06-19 14:13:37 +00:00
: QWidget(parent),
d_ptr(new DialogPrivate(this))
2016-06-17 11:43:04 +00:00
{
2016-06-20 15:47:17 +00:00
d_func()->init();
2016-06-17 11:43:04 +00:00
}
Dialog::~Dialog()
{
}
2016-06-19 14:13:37 +00:00
2016-06-20 15:47:17 +00:00
QLayout *Dialog::windowLayout() const
{
Q_D(const Dialog);
return d->window->layout();
}
void Dialog::setWindowLayout(QLayout *layout)
{
Q_D(Dialog);
d->window->setLayout(layout);
}
2016-06-21 09:18:51 +00:00
void Dialog::showDialog()
2016-06-20 15:47:17 +00:00
{
Q_D(Dialog);
2016-06-21 09:18:51 +00:00
emit d->window->dialogActivated();
2016-06-20 15:47:17 +00:00
}
2016-06-21 09:18:51 +00:00
void Dialog::hideDialog()
2016-06-20 15:47:17 +00:00
{
Q_D(Dialog);
2016-06-21 09:18:51 +00:00
emit d->window->dialogDeactivated();
setAttribute(Qt::WA_TransparentForMouseEvents);
}
void Dialog::acceptMouseEvents()
{
setAttribute(Qt::WA_TransparentForMouseEvents, false);
2016-06-20 15:47:17 +00:00
}
2016-06-19 14:13:37 +00:00
bool Dialog::event(QEvent *event)
{
switch (event->type())
{
case QEvent::ParentChange:
{
if (!parent())
break;
parent()->installEventFilter(this);
QWidget *widget;
if ((widget = parentWidget())) {
setGeometry(widget->rect());
}
break;
}
case QEvent::ParentAboutToChange:
{
if (!parent())
break;
parent()->removeEventFilter(this);
break;
}
default:
break;
}
return QWidget::event(event);
}
bool Dialog::eventFilter(QObject *obj, QEvent *event)
{
QEvent::Type type = event->type();
if (QEvent::Move == type || QEvent::Resize == type)
{
QWidget *widget;
if ((widget = parentWidget())) {
setGeometry(widget->rect());
}
}
return QWidget::eventFilter(obj, event);
}
void Dialog::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
2016-06-21 09:18:51 +00:00
Q_D(Dialog);
2016-06-20 15:47:17 +00:00
2016-06-21 09:18:51 +00:00
QPainter painter(this);
2016-06-20 15:47:17 +00:00
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::black);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
2016-06-21 09:18:51 +00:00
painter.setOpacity(d->proxy->opacity()/2);
2016-06-19 14:13:37 +00:00
painter.drawRect(rect());
}