From d412002af2596f659345b9350219c7713b54e025 Mon Sep 17 00:00:00 2001 From: johanneshilden Date: Tue, 3 Oct 2017 10:08:46 +0300 Subject: [PATCH] Add Drawer clickOutsideToClose boolean property --- components/qtmaterialdrawer.cpp | 52 +++++++++++++++++++++++++++++---- components/qtmaterialdrawer.h | 3 ++ components/qtmaterialdrawer_p.h | 1 + 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/components/qtmaterialdrawer.cpp b/components/qtmaterialdrawer.cpp index 3c95a13..d19262d 100644 --- a/components/qtmaterialdrawer.cpp +++ b/components/qtmaterialdrawer.cpp @@ -2,17 +2,24 @@ #include "qtmaterialdrawer_p.h" #include #include +#include #include #include #include #include #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(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); } diff --git a/components/qtmaterialdrawer.h b/components/qtmaterialdrawer.h index 5af542a..2ceb1f9 100644 --- a/components/qtmaterialdrawer.h +++ b/components/qtmaterialdrawer.h @@ -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(); diff --git a/components/qtmaterialdrawer_p.h b/components/qtmaterialdrawer_p.h index dcb6e19..671ee3b 100644 --- a/components/qtmaterialdrawer_p.h +++ b/components/qtmaterialdrawer_p.h @@ -18,6 +18,7 @@ public: QtMaterialDrawerStateMachine *const stateMachine; QWidget *const window; int width; + bool clickToClose; }; #endif // DRAWER_P_H