qt-material-widgets/components/qtmaterialdrawer.cpp

235 lines
4.5 KiB
C++
Raw Permalink Normal View History

2017-09-30 00:20:12 +00:00
#include "qtmaterialdrawer.h"
#include "qtmaterialdrawer_p.h"
#include <QPainter>
#include <QEvent>
#include <QDebug>
#include <QMouseEvent>
2017-09-30 00:20:12 +00:00
#include <QtWidgets/QApplication>
#include <QtWidgets/QLayout>
#include <QLinearGradient>
#include <QtWidgets/QVBoxLayout>
#include "qtmaterialdrawer_internal.h"
/*!
* \class QtMaterialDrawerPrivate
* \internal
*/
2017-10-05 08:13:21 +00:00
/*!
* \internal
*/
2017-09-30 00:20:12 +00:00
QtMaterialDrawerPrivate::QtMaterialDrawerPrivate(QtMaterialDrawer *q)
2017-10-05 08:13:21 +00:00
: q_ptr(q)
{
}
/*!
* \internal
*/
QtMaterialDrawerPrivate::~QtMaterialDrawerPrivate()
{
}
/*!
* \internal
*/
void QtMaterialDrawerPrivate::init()
2017-09-30 00:20:12 +00:00
{
2017-10-05 08:13:21 +00:00
Q_Q(QtMaterialDrawer);
widget = new QtMaterialDrawerWidget;
stateMachine = new QtMaterialDrawerStateMachine(widget, q);
2017-10-05 08:13:21 +00:00
window = new QWidget;
width = 250;
clickToClose = false;
autoRaise = true;
closed = true;
overlay = false;
2017-10-05 08:13:21 +00:00
2017-09-30 00:20:12 +00:00
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(window);
widget->setLayout(layout);
widget->setFixedWidth(width+16);
widget->setParent(q);
2017-09-30 00:20:12 +00:00
stateMachine->start();
QCoreApplication::processEvents();
}
/*!
* \class QtMaterialDrawer
*/
2017-09-30 00:20:12 +00:00
QtMaterialDrawer::QtMaterialDrawer(QWidget *parent)
: QtMaterialOverlayWidget(parent),
d_ptr(new QtMaterialDrawerPrivate(this))
{
2017-10-05 08:13:21 +00:00
d_func()->init();
2017-09-30 00:20:12 +00:00
}
QtMaterialDrawer::~QtMaterialDrawer()
{
}
void QtMaterialDrawer::setDrawerWidth(int width)
{
Q_D(QtMaterialDrawer);
d->width = width;
d->stateMachine->updatePropertyAssignments();
d->widget->setFixedWidth(width+16);
2017-09-30 00:20:12 +00:00
}
int QtMaterialDrawer::drawerWidth() const
{
Q_D(const QtMaterialDrawer);
return d->width;
}
void QtMaterialDrawer::setDrawerLayout(QLayout *layout)
{
Q_D(QtMaterialDrawer);
d->window->setLayout(layout);
}
QLayout *QtMaterialDrawer::drawerLayout() const
{
Q_D(const QtMaterialDrawer);
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;
}
2017-10-05 08:13:21 +00:00
void QtMaterialDrawer::setAutoRaise(bool state)
{
Q_D(QtMaterialDrawer);
d->autoRaise = state;
}
bool QtMaterialDrawer::autoRaise() const
{
Q_D(const QtMaterialDrawer);
return d->autoRaise;
}
void QtMaterialDrawer::setOverlayMode(bool value)
{
Q_D(QtMaterialDrawer);
d->overlay = value;
2017-10-12 19:47:10 +00:00
update();
}
bool QtMaterialDrawer::overlayMode() const
{
Q_D(const QtMaterialDrawer);
return d->overlay;
}
2017-09-30 00:20:12 +00:00
void QtMaterialDrawer::openDrawer()
{
Q_D(QtMaterialDrawer);
emit d->stateMachine->signalOpen();
2017-10-05 08:13:21 +00:00
if (d->autoRaise) {
raise();
}
2017-10-12 19:10:43 +00:00
setAttribute(Qt::WA_TransparentForMouseEvents, false);
setAttribute(Qt::WA_NoSystemBackground, false);
2017-09-30 00:20:12 +00:00
}
void QtMaterialDrawer::closeDrawer()
{
Q_D(QtMaterialDrawer);
emit d->stateMachine->signalClose();
2017-10-12 19:10:43 +00:00
if (d->overlay) {
setAttribute(Qt::WA_TransparentForMouseEvents);
setAttribute(Qt::WA_NoSystemBackground);
}
}
bool QtMaterialDrawer::event(QEvent *event)
{
Q_D(QtMaterialDrawer);
switch (event->type())
{
case QEvent::Move:
case QEvent::Resize:
if (!d->overlay) {
setMask(QRegion(d->widget->rect()));
}
break;
default:
break;
}
return QtMaterialOverlayWidget::event(event);
2017-09-30 00:20:12 +00:00
}
bool QtMaterialDrawer::eventFilter(QObject *obj, QEvent *event)
{
Q_D(QtMaterialDrawer);
2017-09-30 00:20:12 +00:00
switch (event->type())
{
case QEvent::MouseButtonPress: {
QMouseEvent *mouseEvent;
if ((mouseEvent = static_cast<QMouseEvent *>(event))) {
const bool canClose = d->clickToClose || d->overlay;
if (!d->widget->geometry().contains(mouseEvent->pos()) && canClose) {
closeDrawer();
}
}
break;
}
case QEvent::Move:
case QEvent::Resize: {
2017-10-12 19:10:43 +00:00
QLayout *lw = d->widget->layout();
if (lw && 16 != lw->contentsMargins().right()) {
lw->setContentsMargins(0, 0, 16, 0);
2017-09-30 00:20:12 +00:00
}
2017-10-12 19:10:43 +00:00
break;
2017-09-30 00:20:12 +00:00
}
default:
break;
}
2017-09-30 00:20:12 +00:00
return QtMaterialOverlayWidget::eventFilter(obj, event);
}
void QtMaterialDrawer::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
Q_D(QtMaterialDrawer);
2017-09-30 00:20:12 +00:00
if (!d->overlay || d->stateMachine->isInClosedState()) {
return;
}
QPainter painter(this);
painter.setOpacity(d->stateMachine->opacity());
painter.fillRect(rect(), Qt::SolidPattern);
2017-09-30 00:20:12 +00:00
}