Skip to content

Commit

Permalink
feat: the DLineEdit inherit style form the QLineEdit
Browse files Browse the repository at this point in the history
add DStyleOptionLineEdit

Change-Id: I38d491297edb7bfd6c50a3d5c00e6dc4844e15d8
  • Loading branch information
zccrs committed Mar 6, 2018
1 parent 66a323c commit 7b6caec
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/widgets/DLineEdit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dlineedit.h"
1 change: 1 addition & 0 deletions src/widgets/DStyleOptionLineEdit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dstyleoption.h"
84 changes: 47 additions & 37 deletions src/widgets/dlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
#include "dthememanager.h"
#include "private/dlineedit_p.h"
#include "darrowrectangle.h"
#include "dstyleoption.h"

#include <QHBoxLayout>
#include <QResizeEvent>
#include <QWidgetAction>

#define private public
#ifndef slots
#define slots Q_SLOTS
#endif
#include <private/qlineedit_p.h>
#undef private

DWIDGET_BEGIN_NAMESPACE

Expand All @@ -45,8 +54,6 @@ DLineEdit::DLineEdit(QWidget *parent)
: QLineEdit(parent),
DObject(*new DLineEditPrivate(this))
{
DThemeManager::registerWidget(this, QStringList({"alert"}));

Q_D(DLineEdit);
d->init();
}
Expand All @@ -55,8 +62,6 @@ DLineEdit::DLineEdit(DLineEditPrivate &q, QWidget *parent)
: QLineEdit(parent),
DObject(q)
{
DThemeManager::registerWidget(this, QStringList({"alert"}));

Q_D(DLineEdit);
d->init();
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -143,6 +148,22 @@ void DLineEdit::setIconVisible(bool visible)
}

d->m_rightIcon->setVisible(visible);

if (visible) {
QLineEditPrivate *d_d = reinterpret_cast<QLineEditPrivate*>(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);
}
}

/*!
Expand Down Expand Up @@ -214,27 +235,36 @@ 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)
{
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)
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/widgets/dlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
DWIDGET_BEGIN_NAMESPACE

class DLineEditPrivate;
class DStyleOptionLineEdit;
class LIBDTKWIDGETSHARED_EXPORT DLineEdit : public QLineEdit, public DTK_CORE_NAMESPACE::DObject
{
Q_OBJECT
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/widgets/dpasswordedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/widgets/dstyleoption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

#include "dstyleoption.h"

#include "dlineedit.h"
#include "private/dlineedit_p.h"

DWIDGET_BEGIN_NAMESPACE

void DStyleOption::init(QWidget *widget)
{
Expand All @@ -33,3 +37,19 @@ void DStyleOptionSuggestButton::init(QWidget *widget)

features |= QStyleOptionButton::ButtonFeature(SuggestButton);
}

void DStyleOptionLineEdit::init(QWidget *widget)
{
if (const DLineEdit *edit = qobject_cast<DLineEdit*>(widget)) {
if (edit->isAlert()) {
features |= Alert;
}

if (edit->iconVisible()) {
features |= IconButton;
iconButtonRect = edit->d_func()->m_rightIcon->geometry();
}
}
}

DWIDGET_END_NAMESPACE
21 changes: 21 additions & 0 deletions src/widgets/dstyleoption.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
#ifndef DSTYLEOPTION_H
#define DSTYLEOPTION_H

#include <dtkwidget_global.h>

#include <QStyleOption>

DWIDGET_BEGIN_NAMESPACE

class DStyleOption
{
public:
Expand All @@ -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
12 changes: 5 additions & 7 deletions src/widgets/private/dlineedit_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

#include <QHBoxLayout>

QT_BEGIN_NAMESPACE
class QWidgetAction;
QT_END_NAMESPACE

DWIDGET_BEGIN_NAMESPACE

class DArrowRectangle;
Expand All @@ -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;
};

Expand Down
4 changes: 3 additions & 1 deletion src/widgets/widgets.pri
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ includes.files += \
$$PWD/DSuggestButton \
$$PWD/DStyleOption \
$$PWD/DToast \
$$PWD/DFileDialog
$$PWD/DFileDialog \
$$PWD/DLineEdit \
$$PWD/DStyleOptionLineEdit


0 comments on commit 7b6caec

Please sign in to comment.