implement Dialog window
This commit is contained in:
parent
d876ecc443
commit
76fe517541
|
@ -1,6 +1,10 @@
|
||||||
#include "dialog.h"
|
#include "dialog.h"
|
||||||
#include "dialog_p.h"
|
#include "dialog_p.h"
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QStateMachine>
|
||||||
|
#include <QSignalTransition>
|
||||||
|
#include <QPropertyAnimation>
|
||||||
#include <QEvent>
|
#include <QEvent>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QGraphicsDropShadowEffect>
|
#include <QGraphicsDropShadowEffect>
|
||||||
|
@ -8,7 +12,10 @@
|
||||||
#include "dialog_internal.h"
|
#include "dialog_internal.h"
|
||||||
|
|
||||||
DialogPrivate::DialogPrivate(Dialog *q)
|
DialogPrivate::DialogPrivate(Dialog *q)
|
||||||
: q_ptr(q)
|
: q_ptr(q),
|
||||||
|
machine(new QStateMachine(q)),
|
||||||
|
window(new DialogWindow(q)),
|
||||||
|
proxy(new TransparencyProxy)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,15 +28,12 @@ void DialogPrivate::init()
|
||||||
|
|
||||||
QWidget *widget = new QWidget;
|
QWidget *widget = new QWidget;
|
||||||
|
|
||||||
proxy = new TransparencyProxy;
|
|
||||||
widget->setLayout(proxy);
|
widget->setLayout(proxy);
|
||||||
layout->addWidget(widget);
|
layout->addWidget(widget);
|
||||||
layout->setAlignment(widget, Qt::AlignCenter);
|
layout->setAlignment(widget, Qt::AlignCenter);
|
||||||
|
|
||||||
widget->setMinimumWidth(400);
|
widget->setMinimumWidth(400);
|
||||||
widget->setMinimumHeight(400);
|
|
||||||
|
|
||||||
window = new DialogWindow(q);
|
|
||||||
proxy->setWidget(window);
|
proxy->setWidget(window);
|
||||||
|
|
||||||
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
|
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
|
||||||
|
@ -38,54 +42,55 @@ void DialogPrivate::init()
|
||||||
effect->setOffset(0, 13);
|
effect->setOffset(0, 13);
|
||||||
widget->setGraphicsEffect(effect);
|
widget->setGraphicsEffect(effect);
|
||||||
|
|
||||||
/*
|
|
||||||
{
|
|
||||||
QVBoxLayout *layout = new QVBoxLayout;
|
|
||||||
window->setLayout(layout);
|
|
||||||
|
|
||||||
QPushButton *button = new QPushButton;
|
|
||||||
button->setText("Hello test!");
|
|
||||||
layout->addWidget(button);
|
|
||||||
|
|
||||||
button = new QPushButton;
|
|
||||||
button->setText("Hello test!");
|
|
||||||
layout->addWidget(button);
|
|
||||||
|
|
||||||
button = new QPushButton;
|
|
||||||
button->setText("Hello test!");
|
|
||||||
layout->addWidget(button);
|
|
||||||
|
|
||||||
button = new QPushButton;
|
|
||||||
button->setText("Hello test!");
|
|
||||||
layout->addWidget(button);
|
|
||||||
|
|
||||||
button = new QPushButton;
|
|
||||||
button->setText("Hello test!");
|
|
||||||
layout->addWidget(button);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
proxy->setCurrentIndex(1);
|
QState *hiddenState = new QState;
|
||||||
|
QState *visibleState = new QState;
|
||||||
|
|
||||||
//
|
machine->addState(hiddenState);
|
||||||
|
machine->addState(visibleState);
|
||||||
|
|
||||||
|
machine->setInitialState(hiddenState);
|
||||||
|
|
||||||
|
QSignalTransition *transition;
|
||||||
|
|
||||||
QPushButton *btn;
|
transition = new QSignalTransition(window, SIGNAL(dialogActivated()));
|
||||||
|
transition->setTargetState(visibleState);
|
||||||
|
hiddenState->addTransition(transition);
|
||||||
|
|
||||||
btn = new QPushButton;
|
transition = new QSignalTransition(window, SIGNAL(dialogDeactivated()));
|
||||||
btn->setText("One");
|
transition->setTargetState(hiddenState);
|
||||||
layout->addWidget(btn);
|
visibleState->addTransition(transition);
|
||||||
|
|
||||||
QObject::connect(btn, SIGNAL(pressed()), q, SLOT(pressOne()));
|
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);
|
||||||
|
|
||||||
btn = new QPushButton;
|
QObject::connect(proxy, SIGNAL(opacityChanged()), q, SLOT(update()));
|
||||||
btn->setText("Two");
|
|
||||||
layout->addWidget(btn);
|
|
||||||
|
|
||||||
QObject::connect(btn, SIGNAL(pressed()), q, SLOT(pressTwo()));
|
QPropertyAnimation *animation;
|
||||||
|
|
||||||
|
animation = new QPropertyAnimation(proxy, "opacity");
|
||||||
|
animation->setDuration(280);
|
||||||
|
machine->addDefaultAnimation(animation);
|
||||||
|
|
||||||
|
animation = new QPropertyAnimation(effect, "color");
|
||||||
|
animation->setDuration(280);
|
||||||
|
machine->addDefaultAnimation(animation);
|
||||||
|
|
||||||
|
animation = new QPropertyAnimation(window, "offset");
|
||||||
|
animation->setDuration(280);
|
||||||
|
animation->setEasingCurve(QEasingCurve::OutCirc);
|
||||||
|
machine->addDefaultAnimation(animation);
|
||||||
|
|
||||||
|
QObject::connect(visibleState, SIGNAL(propertiesAssigned()), q, SLOT(acceptMouseEvents()));
|
||||||
|
|
||||||
|
machine->start();
|
||||||
|
|
||||||
|
QCoreApplication::processEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
Dialog::Dialog(QWidget *parent)
|
Dialog::Dialog(QWidget *parent)
|
||||||
|
@ -113,18 +118,24 @@ void Dialog::setWindowLayout(QLayout *layout)
|
||||||
d->window->setLayout(layout);
|
d->window->setLayout(layout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dialog::pressOne()
|
void Dialog::showDialog()
|
||||||
{
|
{
|
||||||
Q_D(Dialog);
|
Q_D(Dialog);
|
||||||
|
|
||||||
d->proxy->setOpaque();
|
emit d->window->dialogActivated();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dialog::pressTwo()
|
void Dialog::hideDialog()
|
||||||
{
|
{
|
||||||
Q_D(Dialog);
|
Q_D(Dialog);
|
||||||
|
|
||||||
d->proxy->setOpacity(0.5);
|
emit d->window->dialogDeactivated();
|
||||||
|
setAttribute(Qt::WA_TransparentForMouseEvents);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Dialog::acceptMouseEvents()
|
||||||
|
{
|
||||||
|
setAttribute(Qt::WA_TransparentForMouseEvents, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Dialog::event(QEvent *event)
|
bool Dialog::event(QEvent *event)
|
||||||
|
@ -176,21 +187,16 @@ void Dialog::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event)
|
Q_UNUSED(event)
|
||||||
|
|
||||||
|
Q_D(Dialog);
|
||||||
|
|
||||||
QPainter painter(this);
|
QPainter painter(this);
|
||||||
|
|
||||||
//QPen pen;
|
|
||||||
//pen.setColor(Qt::blue);
|
|
||||||
//pen.setWidth(3);
|
|
||||||
//painter.setPen(pen);
|
|
||||||
|
|
||||||
//painter.drawRect(rect());
|
|
||||||
|
|
||||||
QBrush brush;
|
QBrush brush;
|
||||||
brush.setStyle(Qt::SolidPattern);
|
brush.setStyle(Qt::SolidPattern);
|
||||||
brush.setColor(Qt::black);
|
brush.setColor(Qt::black);
|
||||||
painter.setBrush(brush);
|
painter.setBrush(brush);
|
||||||
painter.setPen(Qt::NoPen);
|
painter.setPen(Qt::NoPen);
|
||||||
painter.setOpacity(0.5);
|
painter.setOpacity(d->proxy->opacity()/2);
|
||||||
|
|
||||||
painter.drawRect(rect());
|
painter.drawRect(rect());
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,9 @@ public:
|
||||||
void setWindowLayout(QLayout *layout);
|
void setWindowLayout(QLayout *layout);
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
void pressOne();
|
void showDialog();
|
||||||
void pressTwo();
|
void hideDialog();
|
||||||
|
void acceptMouseEvents();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
#include "dialog_internal.h"
|
#include "dialog_internal.h"
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QLayout>
|
||||||
#include "dialog.h"
|
#include "dialog.h"
|
||||||
|
|
||||||
DialogWindow::DialogWindow(QWidget *parent)
|
DialogWindow::DialogWindow(Dialog *dialog, QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent),
|
||||||
|
dialog(dialog)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +13,18 @@ DialogWindow::~DialogWindow()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DialogWindow::setOffset(int offset)
|
||||||
|
{
|
||||||
|
QMargins margins = dialog->layout()->contentsMargins();
|
||||||
|
margins.setBottom(offset);
|
||||||
|
dialog->layout()->setContentsMargins(margins);
|
||||||
|
}
|
||||||
|
|
||||||
|
int DialogWindow::offset() const
|
||||||
|
{
|
||||||
|
return dialog->layout()->contentsMargins().bottom();
|
||||||
|
}
|
||||||
|
|
||||||
void DialogWindow::paintEvent(QPaintEvent *event)
|
void DialogWindow::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event)
|
Q_UNUSED(event)
|
||||||
|
|
|
@ -3,21 +3,33 @@
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
|
class QStateMachine;
|
||||||
class Dialog;
|
class Dialog;
|
||||||
|
|
||||||
class DialogWindow : public QWidget
|
class DialogWindow : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY(int offset WRITE setOffset READ offset)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DialogWindow(QWidget *parent = 0);
|
explicit DialogWindow(Dialog *dialog, QWidget *parent = 0);
|
||||||
~DialogWindow();
|
~DialogWindow();
|
||||||
|
|
||||||
|
void setOffset(int offset);
|
||||||
|
int offset() const;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void dialogActivated();
|
||||||
|
void dialogDeactivated();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Q_DISABLE_COPY(DialogWindow)
|
Q_DISABLE_COPY(DialogWindow)
|
||||||
|
|
||||||
|
Dialog *const dialog;
|
||||||
};
|
};
|
||||||
|
|
||||||
//#include <QWidget>
|
//#include <QWidget>
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
|
class QStateMachine;
|
||||||
class Dialog;
|
class Dialog;
|
||||||
class DialogWindow;
|
class DialogWindow;
|
||||||
class TransparencyProxy;
|
class TransparencyProxy;
|
||||||
|
@ -18,8 +19,9 @@ public:
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
Dialog *const q_ptr;
|
Dialog *const q_ptr;
|
||||||
DialogWindow *window;
|
QStateMachine *const machine;
|
||||||
TransparencyProxy *proxy;
|
DialogWindow *const window;
|
||||||
|
TransparencyProxy *const proxy;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DIALOG_P_H
|
#endif // DIALOG_P_H
|
||||||
|
|
|
@ -61,6 +61,8 @@ void TransparencyProxy::setOpaque()
|
||||||
|
|
||||||
d->proxy->setOpacity(1);
|
d->proxy->setOpacity(1);
|
||||||
setCurrentIndex(0);
|
setCurrentIndex(0);
|
||||||
|
|
||||||
|
emit opacityChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TransparencyProxy::setOpacity(qreal opacity)
|
void TransparencyProxy::setOpacity(qreal opacity)
|
||||||
|
@ -71,6 +73,19 @@ void TransparencyProxy::setOpacity(qreal opacity)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (1 == opacity) {
|
||||||
|
return setOpaque();
|
||||||
|
}
|
||||||
|
|
||||||
d->proxy->setOpacity(opacity);
|
d->proxy->setOpacity(opacity);
|
||||||
setCurrentIndex(1);
|
setCurrentIndex(1);
|
||||||
|
|
||||||
|
emit opacityChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
qreal TransparencyProxy::opacity() const
|
||||||
|
{
|
||||||
|
Q_D(const TransparencyProxy);
|
||||||
|
|
||||||
|
return d->proxy->opacity();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,8 @@ class TransparencyProxy : public QStackedLayout
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY(qreal opacity WRITE setOpacity READ opacity NOTIFY opacityChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TransparencyProxy();
|
TransparencyProxy();
|
||||||
~TransparencyProxy();
|
~TransparencyProxy();
|
||||||
|
@ -18,6 +20,10 @@ public:
|
||||||
|
|
||||||
void setOpaque();
|
void setOpaque();
|
||||||
void setOpacity(qreal opacity);
|
void setOpacity(qreal opacity);
|
||||||
|
qreal opacity() const;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void opacityChanged();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const QScopedPointer<TransparencyProxyPrivate> d_ptr;
|
const QScopedPointer<TransparencyProxyPrivate> d_ptr;
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
ProxyWidget::ProxyWidget(QWidget *widget, QWidget *parent)
|
ProxyWidget::ProxyWidget(QWidget *widget, QWidget *parent)
|
||||||
: QWidget(parent),
|
: QWidget(parent),
|
||||||
_widget(widget),
|
_widget(widget),
|
||||||
_opacity(1.0)
|
_opacity(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,15 +18,17 @@ void ProxyWidget::paintEvent(QPaintEvent *event)
|
||||||
|
|
||||||
QPainter painter(this);
|
QPainter painter(this);
|
||||||
|
|
||||||
//QPen pen;
|
|
||||||
//pen.setColor(Qt::red);
|
|
||||||
//pen.setWidth(5);
|
|
||||||
//painter.setPen(pen);
|
|
||||||
|
|
||||||
//painter.drawRect(rect());
|
|
||||||
|
|
||||||
painter.setOpacity(_opacity);
|
painter.setOpacity(_opacity);
|
||||||
|
|
||||||
QPixmap pixmap = _widget->grab(rect());
|
QPixmap pixmap = _widget->grab(rect());
|
||||||
painter.drawPixmap(rect(), pixmap);
|
painter.drawPixmap(rect(), pixmap);
|
||||||
|
|
||||||
|
#ifdef DEBUG_LAYOUT
|
||||||
|
QPen pen;
|
||||||
|
pen.setColor(Qt::red);
|
||||||
|
pen.setWidth(5);
|
||||||
|
painter.setPen(pen);
|
||||||
|
|
||||||
|
painter.drawRect(rect());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,6 +98,24 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
|
|
||||||
layout->addWidget(btn);
|
layout->addWidget(btn);
|
||||||
layout->addWidget(btn2);
|
layout->addWidget(btn2);
|
||||||
|
|
||||||
|
{
|
||||||
|
QPushButton *btn;
|
||||||
|
|
||||||
|
btn = new QPushButton;
|
||||||
|
btn->setParent(this);
|
||||||
|
btn->setText("Show");
|
||||||
|
btn->setGeometry(190, 80, 140, 40);
|
||||||
|
|
||||||
|
QObject::connect(btn, SIGNAL(pressed()), dialog, SLOT(showDialog()));
|
||||||
|
|
||||||
|
btn = new QPushButton;
|
||||||
|
btn->setParent(this);
|
||||||
|
btn->setText("Hide");
|
||||||
|
btn->setGeometry(370, 80, 140, 40);
|
||||||
|
|
||||||
|
QObject::connect(btn, SIGNAL(pressed()), dialog, SLOT(hideDialog()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue