Add Drawer clickOutsideToClose boolean property
This commit is contained in:
parent
154928a01a
commit
d412002af2
|
@ -2,17 +2,24 @@
|
||||||
#include "qtmaterialdrawer_p.h"
|
#include "qtmaterialdrawer_p.h"
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QEvent>
|
#include <QEvent>
|
||||||
|
#include <QMouseEvent>
|
||||||
#include <QtWidgets/QApplication>
|
#include <QtWidgets/QApplication>
|
||||||
#include <QtWidgets/QLayout>
|
#include <QtWidgets/QLayout>
|
||||||
#include <QLinearGradient>
|
#include <QLinearGradient>
|
||||||
#include <QtWidgets/QVBoxLayout>
|
#include <QtWidgets/QVBoxLayout>
|
||||||
#include "qtmaterialdrawer_internal.h"
|
#include "qtmaterialdrawer_internal.h"
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \class QtMaterialDrawerPrivate
|
||||||
|
* \internal
|
||||||
|
*/
|
||||||
|
|
||||||
QtMaterialDrawerPrivate::QtMaterialDrawerPrivate(QtMaterialDrawer *q)
|
QtMaterialDrawerPrivate::QtMaterialDrawerPrivate(QtMaterialDrawer *q)
|
||||||
: q_ptr(q),
|
: q_ptr(q),
|
||||||
stateMachine(new QtMaterialDrawerStateMachine(q)),
|
stateMachine(new QtMaterialDrawerStateMachine(q)),
|
||||||
window(new QWidget),
|
window(new QWidget),
|
||||||
width(250)
|
width(250),
|
||||||
|
clickToClose(false)
|
||||||
{
|
{
|
||||||
QVBoxLayout *layout = new QVBoxLayout;
|
QVBoxLayout *layout = new QVBoxLayout;
|
||||||
layout->addWidget(window);
|
layout->addWidget(window);
|
||||||
|
@ -25,6 +32,10 @@ QtMaterialDrawerPrivate::QtMaterialDrawerPrivate(QtMaterialDrawer *q)
|
||||||
QCoreApplication::processEvents();
|
QCoreApplication::processEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \class QtMaterialDrawer
|
||||||
|
*/
|
||||||
|
|
||||||
QtMaterialDrawer::QtMaterialDrawer(QWidget *parent)
|
QtMaterialDrawer::QtMaterialDrawer(QWidget *parent)
|
||||||
: QtMaterialOverlayWidget(parent),
|
: QtMaterialOverlayWidget(parent),
|
||||||
d_ptr(new QtMaterialDrawerPrivate(this))
|
d_ptr(new QtMaterialDrawerPrivate(this))
|
||||||
|
@ -65,6 +76,20 @@ QLayout *QtMaterialDrawer::drawerLayout() const
|
||||||
return d->window->layout();
|
return d->window->layout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QtMaterialDrawer::setClickOutsideToClose(bool state)
|
||||||
|
{
|
||||||
|
Q_D(QtMaterialDrawer);
|
||||||
|
|
||||||
|
d->clickToClose = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool QtMaterialDrawer::clickOutsideToClose() const
|
||||||
|
{
|
||||||
|
Q_D(const QtMaterialDrawer);
|
||||||
|
|
||||||
|
return d->clickToClose;
|
||||||
|
}
|
||||||
|
|
||||||
void QtMaterialDrawer::openDrawer()
|
void QtMaterialDrawer::openDrawer()
|
||||||
{
|
{
|
||||||
Q_D(QtMaterialDrawer);
|
Q_D(QtMaterialDrawer);
|
||||||
|
@ -82,14 +107,29 @@ void QtMaterialDrawer::closeDrawer()
|
||||||
|
|
||||||
bool QtMaterialDrawer::eventFilter(QObject *obj, QEvent *event)
|
bool QtMaterialDrawer::eventFilter(QObject *obj, QEvent *event)
|
||||||
{
|
{
|
||||||
const QEvent::Type type = event->type();
|
Q_D(QtMaterialDrawer);
|
||||||
|
|
||||||
if (QEvent::Move == type || QEvent::Resize == type) {
|
switch (event->type())
|
||||||
QLayout *t = layout();
|
{
|
||||||
if (t && 16 != t->contentsMargins().right()) {
|
case QEvent::MouseButtonPress: {
|
||||||
t->setContentsMargins(0, 0, 16, 0);
|
QMouseEvent *mouseEvent;
|
||||||
|
if ((mouseEvent = static_cast<QMouseEvent *>(event))) {
|
||||||
|
if (!geometry().contains(mouseEvent->pos()) && d->clickToClose) {
|
||||||
|
closeDrawer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case QEvent::Move:
|
||||||
|
case QEvent::Resize: {
|
||||||
|
QLayout *lyut = layout();
|
||||||
|
if (lyut && 16 != lyut->contentsMargins().right()) {
|
||||||
|
lyut->setContentsMargins(0, 0, 16, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
return QtMaterialOverlayWidget::eventFilter(obj, event);
|
return QtMaterialOverlayWidget::eventFilter(obj, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,9 @@ public:
|
||||||
void setDrawerLayout(QLayout *layout);
|
void setDrawerLayout(QLayout *layout);
|
||||||
QLayout *drawerLayout() const;
|
QLayout *drawerLayout() const;
|
||||||
|
|
||||||
|
void setClickOutsideToClose(bool state);
|
||||||
|
bool clickOutsideToClose() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void openDrawer();
|
void openDrawer();
|
||||||
void closeDrawer();
|
void closeDrawer();
|
||||||
|
|
|
@ -18,6 +18,7 @@ public:
|
||||||
QtMaterialDrawerStateMachine *const stateMachine;
|
QtMaterialDrawerStateMachine *const stateMachine;
|
||||||
QWidget *const window;
|
QWidget *const window;
|
||||||
int width;
|
int width;
|
||||||
|
bool clickToClose;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DRAWER_P_H
|
#endif // DRAWER_P_H
|
||||||
|
|
Loading…
Reference in New Issue