Add Drawer clickOutsideToClose boolean property

This commit is contained in:
johanneshilden 2017-10-03 10:08:46 +03:00
parent 154928a01a
commit d412002af2
3 changed files with 50 additions and 6 deletions

View File

@ -2,17 +2,24 @@
#include "qtmaterialdrawer_p.h"
#include <QPainter>
#include <QEvent>
#include <QMouseEvent>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLayout>
#include <QLinearGradient>
#include <QtWidgets/QVBoxLayout>
#include "qtmaterialdrawer_internal.h"
/*!
* \class QtMaterialDrawerPrivate
* \internal
*/
QtMaterialDrawerPrivate::QtMaterialDrawerPrivate(QtMaterialDrawer *q)
: q_ptr(q),
stateMachine(new QtMaterialDrawerStateMachine(q)),
window(new QWidget),
width(250)
width(250),
clickToClose(false)
{
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(window);
@ -25,6 +32,10 @@ QtMaterialDrawerPrivate::QtMaterialDrawerPrivate(QtMaterialDrawer *q)
QCoreApplication::processEvents();
}
/*!
* \class QtMaterialDrawer
*/
QtMaterialDrawer::QtMaterialDrawer(QWidget *parent)
: QtMaterialOverlayWidget(parent),
d_ptr(new QtMaterialDrawerPrivate(this))
@ -65,6 +76,20 @@ QLayout *QtMaterialDrawer::drawerLayout() const
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()
{
Q_D(QtMaterialDrawer);
@ -82,13 +107,28 @@ void QtMaterialDrawer::closeDrawer()
bool QtMaterialDrawer::eventFilter(QObject *obj, QEvent *event)
{
const QEvent::Type type = event->type();
Q_D(QtMaterialDrawer);
if (QEvent::Move == type || QEvent::Resize == type) {
QLayout *t = layout();
if (t && 16 != t->contentsMargins().right()) {
t->setContentsMargins(0, 0, 16, 0);
switch (event->type())
{
case QEvent::MouseButtonPress: {
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);
}

View File

@ -20,6 +20,9 @@ public:
void setDrawerLayout(QLayout *layout);
QLayout *drawerLayout() const;
void setClickOutsideToClose(bool state);
bool clickOutsideToClose() const;
public slots:
void openDrawer();
void closeDrawer();

View File

@ -18,6 +18,7 @@ public:
QtMaterialDrawerStateMachine *const stateMachine;
QWidget *const window;
int width;
bool clickToClose;
};
#endif // DRAWER_P_H