Skip to content

Commit

Permalink
Merge pull request 3dsman#1 from laserpants/master
Browse files Browse the repository at this point in the history
Syncing from original
  • Loading branch information
hunt978 committed Oct 27, 2017
2 parents 696dc2e + 36060c9 commit 5329119
Show file tree
Hide file tree
Showing 27 changed files with 377 additions and 28 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Qt Material Design Desktop Widgets
# Qt Material Design Desktop Widgets [![Language](https://img.shields.io/badge/language-c++-brightgreen.svg)]() [![Join the chat at https://gitter.im/qt-material-widgets/Lobby](https://badges.gitter.im/qt-material-widgets/Lobby.svg)](https://gitter.im/qt-material-widgets/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

[![Join the chat at https://gitter.im/qt-material-widgets/Lobby](https://badges.gitter.im/qt-material-widgets/Lobby.svg)](https://gitter.im/qt-material-widgets/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

## :hatched_chick: Update!

I finally took the time to go through and clean up this code. The build includes an executable with a preview, and some settings to play around with for each widget. A YouTube video preview is [available here](http://www.youtube.com/watch?v=21UMeNVBPU4).
YouTube video preview [available here](http://www.youtube.com/watch?v=21UMeNVBPU4).

<table>
<tbody>
Expand All @@ -24,6 +20,19 @@ I finally took the time to go through and clean up this code. The build includes
<img src="gifs/appbar.gif" />
</td>
</tr>
<tr>
<td>
Auto Complete
</td>
<td>
<code>QtMaterialAutoComplete</code>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<img src="gifs/autocomplete.gif" />
</td>
</tr>
<tr>
<td>
Avatar
Expand Down Expand Up @@ -264,6 +273,7 @@ I finally took the time to go through and clean up this code. The build includes
#### Implemented components

- [x] App Bar
- [x] Auto Complete
- [x] Avatar
- [x] Badge
- [x] Check Box
Expand All @@ -285,10 +295,10 @@ I finally took the time to go through and clean up this code. The build includes

#### Work in progress

- [x] Autocomplete
- [ ] Divider
- [ ] List
- [ ] List Item
- [ ] Menu
- [ ] Paper
- [ ] Snackbar Layout
- [ ] Table
Expand All @@ -300,7 +310,6 @@ I finally took the time to go through and clean up this code. The build includes
- [ ] Discrete Slider
- [ ] Grid List
- [ ] Icon Menu
- [ ] Menu
- [ ] Search Field
- [ ] Select Field
- [ ] Stepper
Expand Down
17 changes: 15 additions & 2 deletions components/components.pro
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ SOURCES = \
qtmaterialautocomplete.cpp \
qtmaterialpaper.cpp \
qtmaterialtable.cpp \
layouts/qtmaterialsnackbarlayout.cpp
layouts/qtmaterialsnackbarlayout.cpp \
qtmaterialautocomplete_internal.cpp \
qtmaterialmenu.cpp \
qtmaterialmenu_internal.cpp \
qtmateriallist.cpp \
qtmateriallistitem.cpp
HEADERS = \
qtmaterialavatar_p.h \
qtmaterialavatar.h \
Expand Down Expand Up @@ -112,6 +117,14 @@ HEADERS = \
qtmaterialtable.h \
qtmaterialtable_p.h \
layouts/qtmaterialsnackbarlayout.h \
layouts/qtmaterialsnackbarlayout_p.h
layouts/qtmaterialsnackbarlayout_p.h \
qtmaterialautocomplete_internal.h \
qtmaterialmenu.h \
qtmaterialmenu_p.h \
qtmaterialmenu_internal.h \
qtmateriallist.h \
qtmateriallist_p.h \
qtmateriallistitem.h \
qtmateriallistitem_p.h
RESOURCES += \
resources.qrc
3 changes: 3 additions & 0 deletions components/lib/qtmaterialrippleoverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ void QtMaterialRippleOverlay::addRipple(QtMaterialRipple *ripple)
ripple->setOverlay(this);
m_ripples.push_back(ripple);
ripple->start();

connect(this, SIGNAL(destroyed(QObject*)), ripple, SLOT(stop()));
connect(this, SIGNAL(destroyed(QObject*)), ripple, SLOT(deleteLater()));
}

void QtMaterialRippleOverlay::addRipple(const QPoint &position, qreal radius)
Expand Down
95 changes: 86 additions & 9 deletions components/qtmaterialautocomplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <QtWidgets/QGraphicsDropShadowEffect>
#include <QtWidgets/QVBoxLayout>
#include <QEvent>
#include <QTimer>
#include <QPainter>
#include <QDebug>
#include "qtmaterialautocomplete_internal.h"
#include "qtmaterialflatbutton.h"

/*!
Expand Down Expand Up @@ -32,25 +36,35 @@ void QtMaterialAutoCompletePrivate::init()
{
Q_Q(QtMaterialAutoComplete);

menu = new QWidget;
menuLayout = new QVBoxLayout;
maxWidth = 0;
menu = new QWidget;
frame = new QWidget;
stateMachine = new QtMaterialAutoCompleteStateMachine(menu);
menuLayout = new QVBoxLayout;
maxWidth = 0;

menu->setParent(q->parentWidget());
frame->setParent(q->parentWidget());

menu->installEventFilter(q);
frame->installEventFilter(q);

QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
effect->setBlurRadius(11);
effect->setColor(QColor(0, 0, 0, 50));
effect->setOffset(0, 3);

menu->setGraphicsEffect(effect);
frame->setGraphicsEffect(effect);
frame->setVisible(false);

menu->setLayout(menuLayout);
menu->setVisible(false);

menuLayout->setContentsMargins(0, 0, 0, 0);
menuLayout->setSpacing(0);

QObject::connect(q, SIGNAL(textChanged(QString)), q, SLOT(updateResults(QString)));
QObject::connect(q, SIGNAL(textEdited(QString)), q, SLOT(updateResults(QString)));

stateMachine->start();
}

/*!
Expand Down Expand Up @@ -86,7 +100,7 @@ void QtMaterialAutoComplete::updateResults(QString text)
QString lookup(trimmed.toLower());
QStringList::iterator i;
for (i = d->dataSource.begin(); i != d->dataSource.end(); ++i) {
if (i->toLower().startsWith(lookup)) {
if (i->toLower().indexOf(lookup) != -1) {
results.push_back(*i);
}
}
Expand All @@ -103,7 +117,9 @@ void QtMaterialAutoComplete::updateResults(QString text)
item->setCornerRadius(0);
item->setHaloVisible(false);
item->setFixedHeight(50);
item->setOverlayStyle(Material::TintedOverlay);
d->menuLayout->addWidget(item);
item->installEventFilter(this);
}
} else if (diff < 0) {
for (int c = 0; c < -diff; c++) {
Expand All @@ -130,9 +146,9 @@ void QtMaterialAutoComplete::updateResults(QString text)
}

if (!results.count()) {
d->menu->hide();
} else if (d->menu->isHidden()) {
d->menu->show();
emit d->stateMachine->shouldClose();
} else {
emit d->stateMachine->shouldOpen();
}

d->menu->setFixedHeight(results.length()*50);
Expand All @@ -155,6 +171,7 @@ bool QtMaterialAutoComplete::QtMaterialAutoComplete::event(QEvent *event)
QWidget *widget = static_cast<QWidget *>(parent());
if (widget) {
d->menu->setParent(widget);
d->frame->setParent(widget);
}
break;
}
Expand All @@ -163,3 +180,63 @@ bool QtMaterialAutoComplete::QtMaterialAutoComplete::event(QEvent *event)
}
return QtMaterialTextField::event(event);
}

bool QtMaterialAutoComplete::eventFilter(QObject *watched, QEvent *event)
{
Q_D(QtMaterialAutoComplete);

if (d->frame == watched)
{
switch (event->type())
{
case QEvent::Paint: {
QPainter painter(d->frame);
painter.fillRect(d->frame->rect(), Qt::white);
break;
}
default:
break;
}
}
else if (d->menu == watched)
{
switch (event->type())
{
case QEvent::Resize:
case QEvent::Move: {
d->frame->setGeometry(d->menu->geometry());
break;
}
case QEvent::Show: {
d->frame->show();
d->menu->raise();
break;
}
case QEvent::Hide: {
d->frame->hide();
break;
}
default:
break;
}
}
else
{
switch (event->type())
{
case QEvent::MouseButtonPress: {
emit d->stateMachine->shouldFade();
QtMaterialFlatButton *widget;
if ((widget = static_cast<QtMaterialFlatButton *>(watched))) {
QString text(widget->text());
setText(text);
emit itemSelected(text);
}
break;
}
default:
break;
}
}
return QtMaterialTextField::eventFilter(watched, event);
}
4 changes: 4 additions & 0 deletions components/qtmaterialautocomplete.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ class QtMaterialAutoComplete : public QtMaterialTextField

void setDataSource(const QStringList &data);

signals:
void itemSelected(QString);

protected slots:
void updateResults(QString text);

protected:
bool event(QEvent *event) Q_DECL_OVERRIDE;
bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE;

private:
Q_DISABLE_COPY(QtMaterialAutoComplete)
Expand Down
69 changes: 69 additions & 0 deletions components/qtmaterialautocomplete_internal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "qtmaterialautocomplete_internal.h"
#include <QSignalTransition>
#include <QPropertyAnimation>
#include <QtWidgets/QGraphicsOpacityEffect>
#include <QDebug>

/*!
* \class QtMaterialAutoCompleteStateMachine
* \internal
*/

/*!
* \internal
*/
QtMaterialAutoCompleteStateMachine::QtMaterialAutoCompleteStateMachine(QWidget *menu)
: QStateMachine(menu),
m_menu(menu),
m_closedState(new QState),
m_openState(new QState),
m_closingState(new QState)
{
Q_ASSERT(menu);

addState(m_closedState);
addState(m_openState);
addState(m_closingState);
setInitialState(m_closedState);

QSignalTransition *transition;

transition = new QSignalTransition(this, SIGNAL(shouldOpen()));
transition->setTargetState(m_openState);
m_closedState->addTransition(transition);

transition = new QSignalTransition(this, SIGNAL(shouldClose()));
transition->setTargetState(m_closedState);
m_openState->addTransition(transition);

transition = new QSignalTransition(this, SIGNAL(shouldFade()));
transition->setTargetState(m_closingState);
m_openState->addTransition(transition);

m_closedState->assignProperty(menu, "visible", false);
m_openState->assignProperty(menu, "visible", true);

QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect;
menu->setGraphicsEffect(effect);

m_openState->assignProperty(effect, "opacity", 1);
m_closingState->assignProperty(effect, "opacity", 0);
m_closedState->assignProperty(effect, "opacity", 0);

QPropertyAnimation *animation;

animation = new QPropertyAnimation(effect, "opacity", this);
animation->setDuration(240);
addDefaultAnimation(animation);

transition = new QSignalTransition(animation, SIGNAL(finished()));
transition->setTargetState(m_closedState);
m_closingState->addTransition(transition);
}

/*!
* \internal
*/
QtMaterialAutoCompleteStateMachine::~QtMaterialAutoCompleteStateMachine()
{
}
29 changes: 29 additions & 0 deletions components/qtmaterialautocomplete_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef QTMATERIALAUTOCOMPLETESTATEMACHINE_H
#define QTMATERIALAUTOCOMPLETESTATEMACHINE_H

#include <QStateMachine>
#include "qtmaterialautocomplete.h"

class QtMaterialAutoCompleteStateMachine : public QStateMachine
{
Q_OBJECT

public:
explicit QtMaterialAutoCompleteStateMachine(QWidget *menu);
~QtMaterialAutoCompleteStateMachine();

signals:
void shouldOpen();
void shouldClose();
void shouldFade();

private:
Q_DISABLE_COPY(QtMaterialAutoCompleteStateMachine)

QWidget *const m_menu;
QState *const m_closedState;
QState *const m_openState;
QState *const m_closingState;
};

#endif // QTMATERIALAUTOCOMPLETESTATEMACHINE_H
11 changes: 7 additions & 4 deletions components/qtmaterialautocomplete_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class QWidget;
class QVBoxLayout;
class QtMaterialAutoCompleteOverlay;
class QtMaterialAutoCompleteStateMachine;

class QtMaterialAutoCompletePrivate : public QtMaterialTextFieldPrivate
{
Expand All @@ -18,10 +19,12 @@ class QtMaterialAutoCompletePrivate : public QtMaterialTextFieldPrivate

void init();

QWidget *menu;
QVBoxLayout *menuLayout;
QStringList dataSource;
int maxWidth;
QWidget *menu;
QWidget *frame;
QtMaterialAutoCompleteStateMachine *stateMachine;
QVBoxLayout *menuLayout;
QStringList dataSource;
int maxWidth;
};

#endif // QTMATERIALAUTOCOMPLETE_P_H
Loading

0 comments on commit 5329119

Please sign in to comment.