From 7b6caece0394e80b89b1831ea860cc0d06558929 Mon Sep 17 00:00:00 2001 From: zccrs Date: Tue, 6 Mar 2018 16:35:46 +0800 Subject: [PATCH] feat: the DLineEdit inherit style form the QLineEdit add DStyleOptionLineEdit Change-Id: I38d491297edb7bfd6c50a3d5c00e6dc4844e15d8 --- src/widgets/DLineEdit | 1 + src/widgets/DStyleOptionLineEdit | 1 + src/widgets/dlineedit.cpp | 84 +++++++++++++++++-------------- src/widgets/dlineedit.h | 4 +- src/widgets/dpasswordedit.cpp | 2 - src/widgets/dstyleoption.cpp | 20 ++++++++ src/widgets/dstyleoption.h | 21 ++++++++ src/widgets/private/dlineedit_p.h | 12 ++--- src/widgets/widgets.pri | 4 +- 9 files changed, 101 insertions(+), 48 deletions(-) create mode 100644 src/widgets/DLineEdit create mode 100644 src/widgets/DStyleOptionLineEdit diff --git a/src/widgets/DLineEdit b/src/widgets/DLineEdit new file mode 100644 index 000000000..d4df42865 --- /dev/null +++ b/src/widgets/DLineEdit @@ -0,0 +1 @@ +#include "dlineedit.h" diff --git a/src/widgets/DStyleOptionLineEdit b/src/widgets/DStyleOptionLineEdit new file mode 100644 index 000000000..43f6c0999 --- /dev/null +++ b/src/widgets/DStyleOptionLineEdit @@ -0,0 +1 @@ +#include "dstyleoption.h" diff --git a/src/widgets/dlineedit.cpp b/src/widgets/dlineedit.cpp index c0f5861fe..13e18c012 100644 --- a/src/widgets/dlineedit.cpp +++ b/src/widgets/dlineedit.cpp @@ -19,9 +19,18 @@ #include "dthememanager.h" #include "private/dlineedit_p.h" #include "darrowrectangle.h" +#include "dstyleoption.h" #include #include +#include + +#define private public +#ifndef slots +#define slots Q_SLOTS +#endif +#include +#undef private DWIDGET_BEGIN_NAMESPACE @@ -45,8 +54,6 @@ DLineEdit::DLineEdit(QWidget *parent) : QLineEdit(parent), DObject(*new DLineEditPrivate(this)) { - DThemeManager::registerWidget(this, QStringList({"alert"})); - Q_D(DLineEdit); d->init(); } @@ -55,8 +62,6 @@ DLineEdit::DLineEdit(DLineEditPrivate &q, QWidget *parent) : QLineEdit(parent), DObject(q) { - DThemeManager::registerWidget(this, QStringList({"alert"})); - Q_D(DLineEdit); d->init(); } @@ -101,7 +106,7 @@ void DLineEdit::showAlertMessage(const QString &text, int duration) label->setWordWrap(true); label->setMaximumWidth(width()); d->tooltip->setContent(label); - d->tooltip->setBackgroundColor(DThemeManager::instance()->theme() == "light" ? Qt::white : Qt::black); + d->tooltip->setBackgroundColor(DThemeManager::instance()->theme(this) == "light" ? Qt::white : Qt::black); d->tooltip->setArrowX(15); d->tooltip->setArrowHeight(5); @@ -143,6 +148,22 @@ void DLineEdit::setIconVisible(bool visible) } d->m_rightIcon->setVisible(visible); + + if (visible) { + QLineEditPrivate *d_d = reinterpret_cast(d_ptr.data()); + + addAction(d->m_iconAction, TrailingPosition); + + if (d_d->trailingSideWidgets.size() > 1) { + if ((*(d_d->trailingSideWidgets.end() - 1)).action == d->m_iconAction) { + d_d->trailingSideWidgets.insert(d_d->trailingSideWidgets.begin(), *d_d->trailingSideWidgets.erase(d_d->trailingSideWidgets.end() - 1)); + QResizeEvent resize_event(size(), size()); + qApp->sendEvent(this, &resize_event); + } + } + } else { + removeAction(d->m_iconAction); + } } /*! @@ -214,20 +235,12 @@ void DLineEdit::focusInEvent(QFocusEvent *e) { Q_EMIT focusChanged(true); QLineEdit::focusInEvent(e); - - Q_D(DLineEdit); - - d->clearButton->show(); } void DLineEdit::focusOutEvent(QFocusEvent *e) { Q_EMIT focusChanged(false); QLineEdit::focusOutEvent(e); - - Q_D(DLineEdit); - - d->clearButton->hide(); } void DLineEdit::resizeEvent(QResizeEvent *e) @@ -235,6 +248,23 @@ void DLineEdit::resizeEvent(QResizeEvent *e) QLineEdit::resizeEvent(e); Q_EMIT sizeChanged(e->size()); + + D_D(DLineEdit); + + d->m_rightIcon->setFixedHeight(e->size().height() - 2); +} + +bool DLineEdit::eventFilter(QObject *watched, QEvent *event) +{ + D_D(DLineEdit); + + if (watched == d->m_rightIcon) { + if (event->type() == QEvent::Move) { + d->m_rightIcon->move(width() - d->m_rightIcon->width() - 1, 1); + } + } + + return false; } DLineEditPrivate::DLineEditPrivate(DLineEdit *q) @@ -245,35 +275,15 @@ DLineEditPrivate::DLineEditPrivate(DLineEdit *q) void DLineEditPrivate::init() { Q_Q(DLineEdit); - m_insideFrame = new QFrame(q); - m_insideFrame->setObjectName("LineEditInsideFrame"); m_rightIcon = new DImageButton(q); m_rightIcon->setObjectName("IconButton"); + m_rightIcon->installEventFilter(q); + + m_iconAction = new QWidgetAction(q); + m_iconAction->setDefaultWidget(m_rightIcon); m_rightIcon->hide(); - clearButton = new DImageButton(q); - clearButton->setObjectName("ClearButton"); - clearButton->hide(); - m_centralHLayout = new QHBoxLayout; - m_centralHLayout->addStretch(); - m_centralHLayout->addWidget(clearButton); - m_centralHLayout->addWidget(m_rightIcon); - m_centralHLayout->setSpacing(0); - m_centralHLayout->setContentsMargins(0, 0, 2, 0); - - q->setLayout(m_centralHLayout); - q->setContextMenuPolicy(Qt::NoContextMenu); q->connect(m_rightIcon, &DImageButton::clicked, q, &DLineEdit::iconClicked); - q->connect(q, SIGNAL(sizeChanged(QSize)), q, SLOT(_q_resizeInsideFrame(QSize))); - q->connect(clearButton, &DImageButton::clicked, q, [q] { - q->setText(QString()); - }); -} - -void DLineEditPrivate::_q_resizeInsideFrame(const QSize &size) -{ - m_insideFrame->setFixedHeight(size.height() - 1); - m_insideFrame->setFixedWidth(size.width()); } DWIDGET_END_NAMESPACE diff --git a/src/widgets/dlineedit.h b/src/widgets/dlineedit.h index bf2b20595..8cee358b6 100644 --- a/src/widgets/dlineedit.h +++ b/src/widgets/dlineedit.h @@ -26,6 +26,7 @@ DWIDGET_BEGIN_NAMESPACE class DLineEditPrivate; +class DStyleOptionLineEdit; class LIBDTKWIDGETSHARED_EXPORT DLineEdit : public QLineEdit, public DTK_CORE_NAMESPACE::DObject { Q_OBJECT @@ -69,8 +70,9 @@ public Q_SLOTS: void focusInEvent(QFocusEvent *e) Q_DECL_OVERRIDE; void focusOutEvent(QFocusEvent *e) Q_DECL_OVERRIDE; void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE; + bool eventFilter(QObject *watched, QEvent *event) override; - Q_PRIVATE_SLOT(d_func(), void _q_resizeInsideFrame(const QSize &)) + friend class DStyleOptionLineEdit; }; DWIDGET_END_NAMESPACE diff --git a/src/widgets/dpasswordedit.cpp b/src/widgets/dpasswordedit.cpp index 1bc9c46be..2b88c1dd2 100644 --- a/src/widgets/dpasswordedit.cpp +++ b/src/widgets/dpasswordedit.cpp @@ -81,9 +81,7 @@ void DPasswordEditPrivate::init() { D_Q(DPasswordEdit); - q->layout()->setContentsMargins(0, 0, 1, 0); q->setEchoMode(q->Password); - q->setTextMargins(0, 0, 16 + 22 + 5, 0); q->setIconVisible(true); // FIXME: DPasswordEdit instances that initialized with a parent will fail diff --git a/src/widgets/dstyleoption.cpp b/src/widgets/dstyleoption.cpp index 134987ff7..d8f7704a9 100644 --- a/src/widgets/dstyleoption.cpp +++ b/src/widgets/dstyleoption.cpp @@ -21,6 +21,10 @@ #include "dstyleoption.h" +#include "dlineedit.h" +#include "private/dlineedit_p.h" + +DWIDGET_BEGIN_NAMESPACE void DStyleOption::init(QWidget *widget) { @@ -33,3 +37,19 @@ void DStyleOptionSuggestButton::init(QWidget *widget) features |= QStyleOptionButton::ButtonFeature(SuggestButton); } + +void DStyleOptionLineEdit::init(QWidget *widget) +{ + if (const DLineEdit *edit = qobject_cast(widget)) { + if (edit->isAlert()) { + features |= Alert; + } + + if (edit->iconVisible()) { + features |= IconButton; + iconButtonRect = edit->d_func()->m_rightIcon->geometry(); + } + } +} + +DWIDGET_END_NAMESPACE diff --git a/src/widgets/dstyleoption.h b/src/widgets/dstyleoption.h index d23452224..9fb7edf8c 100644 --- a/src/widgets/dstyleoption.h +++ b/src/widgets/dstyleoption.h @@ -21,8 +21,12 @@ #ifndef DSTYLEOPTION_H #define DSTYLEOPTION_H +#include + #include +DWIDGET_BEGIN_NAMESPACE + class DStyleOption { public: @@ -39,4 +43,21 @@ class DStyleOptionSuggestButton : public QStyleOptionButton, public DStyleOption void init(QWidget *widget) override; }; +class DStyleOptionLineEdit : public DStyleOption +{ +public: + enum LineEditFeature { + Alert = 0x1, + IconButton = 0x2 + }; + Q_DECLARE_FLAGS(LineEditFeatures, LineEditFeature) + + void init(QWidget *widget) override; + + LineEditFeatures features = 0; + QRect iconButtonRect; +}; + +DWIDGET_END_NAMESPACE + #endif // DSTYLEOPTION_H diff --git a/src/widgets/private/dlineedit_p.h b/src/widgets/private/dlineedit_p.h index e75ee3e97..8abbd7005 100644 --- a/src/widgets/private/dlineedit_p.h +++ b/src/widgets/private/dlineedit_p.h @@ -25,6 +25,10 @@ #include +QT_BEGIN_NAMESPACE +class QWidgetAction; +QT_END_NAMESPACE + DWIDGET_BEGIN_NAMESPACE class DArrowRectangle; @@ -37,17 +41,11 @@ class DLineEditPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate void init(); -public Q_SLOTS: - void _q_resizeInsideFrame(const QSize &size); - void _q_resizeInputableRegion(); - public: bool m_isAlert = false; - QFrame *m_insideFrame; DImageButton *m_rightIcon; - DImageButton *clearButton; - QHBoxLayout *m_centralHLayout; + QWidgetAction *m_iconAction; DArrowRectangle *tooltip = Q_NULLPTR; }; diff --git a/src/widgets/widgets.pri b/src/widgets/widgets.pri index 1616824ed..7a3f1efc6 100644 --- a/src/widgets/widgets.pri +++ b/src/widgets/widgets.pri @@ -233,6 +233,8 @@ includes.files += \ $$PWD/DSuggestButton \ $$PWD/DStyleOption \ $$PWD/DToast \ - $$PWD/DFileDialog + $$PWD/DFileDialog \ + $$PWD/DLineEdit \ + $$PWD/DStyleOptionLineEdit