From 5aefcce24d74c9c2b9b31af7f54da2993c8d9098 Mon Sep 17 00:00:00 2001 From: Johannes Hilden Date: Sun, 15 Oct 2017 13:38:53 +0300 Subject: [PATCH] Add state machine for Auto Complete --- components/components.pro | 6 ++- components/qtmaterialautocomplete.cpp | 11 ++++-- .../qtmaterialautocomplete_internal.cpp | 38 +++++++++++++++++++ components/qtmaterialautocomplete_internal.h | 24 ++++++++++++ components/qtmaterialautocomplete_p.h | 10 +++-- 5 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 components/qtmaterialautocomplete_internal.cpp create mode 100644 components/qtmaterialautocomplete_internal.h diff --git a/components/components.pro b/components/components.pro index 62296e0..057048d 100644 --- a/components/components.pro +++ b/components/components.pro @@ -42,7 +42,8 @@ SOURCES = \ qtmaterialautocomplete.cpp \ qtmaterialpaper.cpp \ qtmaterialtable.cpp \ - layouts/qtmaterialsnackbarlayout.cpp + layouts/qtmaterialsnackbarlayout.cpp \ + qtmaterialautocomplete_internal.cpp HEADERS = \ qtmaterialavatar_p.h \ qtmaterialavatar.h \ @@ -112,6 +113,7 @@ HEADERS = \ qtmaterialtable.h \ qtmaterialtable_p.h \ layouts/qtmaterialsnackbarlayout.h \ - layouts/qtmaterialsnackbarlayout_p.h + layouts/qtmaterialsnackbarlayout_p.h \ + qtmaterialautocomplete_internal.h RESOURCES += \ resources.qrc diff --git a/components/qtmaterialautocomplete.cpp b/components/qtmaterialautocomplete.cpp index c8e5fdd..ad0fa16 100644 --- a/components/qtmaterialautocomplete.cpp +++ b/components/qtmaterialautocomplete.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include "qtmaterialautocomplete_internal.h" #include "qtmaterialflatbutton.h" /*! @@ -34,9 +34,10 @@ void QtMaterialAutoCompletePrivate::init() { Q_Q(QtMaterialAutoComplete); - menu = new QWidget; - menuLayout = new QVBoxLayout; - maxWidth = 0; + stateMachine = new QtMaterialAutoCompleteStateMachine(q); + menu = new QWidget; + menuLayout = new QVBoxLayout; + maxWidth = 0; menu->setParent(q->parentWidget()); @@ -53,6 +54,8 @@ void QtMaterialAutoCompletePrivate::init() menuLayout->setSpacing(0); QObject::connect(q, SIGNAL(textEdited(QString)), q, SLOT(updateResults(QString))); + + stateMachine->start(); } /*! diff --git a/components/qtmaterialautocomplete_internal.cpp b/components/qtmaterialautocomplete_internal.cpp new file mode 100644 index 0000000..23b3db7 --- /dev/null +++ b/components/qtmaterialautocomplete_internal.cpp @@ -0,0 +1,38 @@ +#include "qtmaterialautocomplete_internal.h" +#include + +/*! + * \class QtMaterialAutoCompleteStateMachine + * \internal + */ + +/*! + * \internal + */ +QtMaterialAutoCompleteStateMachine::QtMaterialAutoCompleteStateMachine(QtMaterialAutoComplete *parent) + : QStateMachine(parent), + m_autoComplete(parent), + m_closedState(new QState), + m_openState(new QState), + m_closingState(new QState) +{ + Q_ASSERT(parent); + + addState(m_closedState); + addState(m_openState); + addState(m_closingState); + setInitialState(m_closedState); + + QEventTransition *transition; + + //transition = new QEventTransition(parent, QEvent::HoverEnter); + //transition->setTargetState(m_focusState); + //m_blurState->addTransition(transition); +} + +/*! + * \internal + */ +QtMaterialAutoCompleteStateMachine::~QtMaterialAutoCompleteStateMachine() +{ +} diff --git a/components/qtmaterialautocomplete_internal.h b/components/qtmaterialautocomplete_internal.h new file mode 100644 index 0000000..c395eb8 --- /dev/null +++ b/components/qtmaterialautocomplete_internal.h @@ -0,0 +1,24 @@ +#ifndef QTMATERIALAUTOCOMPLETESTATEMACHINE_H +#define QTMATERIALAUTOCOMPLETESTATEMACHINE_H + +#include +#include "qtmaterialautocomplete.h" + +class QtMaterialAutoCompleteStateMachine : public QStateMachine +{ + Q_OBJECT + +public: + QtMaterialAutoCompleteStateMachine(QtMaterialAutoComplete *parent); + ~QtMaterialAutoCompleteStateMachine(); + +private: + Q_DISABLE_COPY(QtMaterialAutoCompleteStateMachine) + + QtMaterialAutoComplete *const m_autoComplete; + QState *m_closedState; + QState *m_openState; + QState *m_closingState; +}; + +#endif // QTMATERIALAUTOCOMPLETESTATEMACHINE_H diff --git a/components/qtmaterialautocomplete_p.h b/components/qtmaterialautocomplete_p.h index 8bec9f9..7d00776 100644 --- a/components/qtmaterialautocomplete_p.h +++ b/components/qtmaterialautocomplete_p.h @@ -6,6 +6,7 @@ class QWidget; class QVBoxLayout; class QtMaterialAutoCompleteOverlay; +class QtMaterialAutoCompleteStateMachine; class QtMaterialAutoCompletePrivate : public QtMaterialTextFieldPrivate { @@ -18,10 +19,11 @@ public: void init(); - QWidget *menu; - QVBoxLayout *menuLayout; - QStringList dataSource; - int maxWidth; + QtMaterialAutoCompleteStateMachine *stateMachine; + QWidget *menu; + QVBoxLayout *menuLayout; + QStringList dataSource; + int maxWidth; }; #endif // QTMATERIALAUTOCOMPLETE_P_H