2 * \file fancylineedit.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Nokia Corporation (qt-info@nokia.com)
8 * Full author contact details are available in file CREDITS.
12 // Code taken from the Qt Creator project and customized a little
16 #include "FancyLineEdit.h"
18 #if QT_VERSION < 0x050200
19 #include "GuiApplication.h"
25 #include <QPropertyAnimation>
26 #include <QApplication>
28 #include <QMouseEvent>
30 #include <QAbstractButton>
33 #include <QPaintEvent>
38 #define ICONBUTTON_HEIGHT 18
45 ////////////////////////////////////////////////////////////////////////
47 // FancyLineEditPrivate
49 ////////////////////////////////////////////////////////////////////////
51 class FancyLineEditPrivate : public QObject {
53 explicit FancyLineEditPrivate(FancyLineEdit *parent);
55 bool eventFilter(QObject *obj, QEvent *event) override;
57 FancyLineEdit *m_lineEdit;
60 bool m_menuTabFocusTrigger[2];
61 IconButton *m_iconbutton[2];
62 bool m_iconEnabled[2];
66 FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent)
67 : QObject(parent), m_lineEdit(parent)
69 for (int i = 0; i < 2; ++i) {
71 m_menuTabFocusTrigger[i] = false;
72 m_iconbutton[i] = new IconButton(parent);
73 m_iconbutton[i]->installEventFilter(this);
74 m_iconbutton[i]->hide();
75 m_iconbutton[i]->setAutoHide(false);
76 m_iconEnabled[i] = false;
81 bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
84 for (int i = 0; i < 2; ++i) {
85 if (obj == m_iconbutton[i]) {
90 if (buttonIndex == -1)
91 return QObject::eventFilter(obj, event);
92 switch (event->type()) {
94 if (m_menuTabFocusTrigger[buttonIndex] && m_menu[buttonIndex]) {
95 m_lineEdit->setFocus();
96 m_menu[buttonIndex]->exec(m_iconbutton[buttonIndex]->mapToGlobal(
97 m_iconbutton[buttonIndex]->rect().center()));
103 return QObject::eventFilter(obj, event);
107 ////////////////////////////////////////////////////////////////////////
111 ////////////////////////////////////////////////////////////////////////
113 FancyLineEdit::FancyLineEdit(QWidget *parent) :
115 m_d(new FancyLineEditPrivate(this))
120 connect(this, SIGNAL(textChanged(QString)),
121 this, SLOT(checkButtons(QString)));
122 connect(m_d->m_iconbutton[Left], SIGNAL(clicked()),
123 this, SLOT(iconClicked()));
124 connect(m_d->m_iconbutton[Right], SIGNAL(clicked()),
125 this, SLOT(iconClicked()));
129 void FancyLineEdit::checkButtons(const QString &text)
131 if (m_oldText.isEmpty() || text.isEmpty()) {
132 for (int i = 0; i < 2; ++i) {
133 if (m_d->m_iconbutton[i]->hasAutoHide())
134 m_d->m_iconbutton[i]->animateShow(!text.isEmpty());
141 void FancyLineEdit::setClearButton(bool visible)
143 // QLineEdit::setClearButtonEnabled() has been implemented in Qt 5.2.
144 // This is now the minimum required version
145 setClearButtonEnabled(visible);
149 void FancyLineEdit::setButtonVisible(Side side, bool visible)
151 m_d->m_iconbutton[side]->setVisible(visible);
152 m_d->m_iconEnabled[side] = visible;
157 bool FancyLineEdit::isButtonVisible(Side side) const
159 return m_d->m_iconEnabled[side];
163 void FancyLineEdit::iconClicked()
165 IconButton *button = qobject_cast<IconButton *>(sender());
167 for (int i = 0; i < 2; ++i)
168 if (m_d->m_iconbutton[i] == button)
172 if (m_d->m_menu[index]) {
173 m_d->m_menu[index]->exec(QCursor::pos());
175 buttonClicked((Side)index);
178 else if (index == Right)
179 rightButtonClicked();
184 void FancyLineEdit::updateMargins()
186 bool leftToRight = (layoutDirection() == Qt::LeftToRight);
187 Side realLeft = (leftToRight ? Left : Right);
188 Side realRight = (leftToRight ? Right : Left);
191 // Consider device/pixel ratio (HiDPI)
192 dpr = devicePixelRatio();
193 int leftMargin = (m_d->m_iconbutton[realLeft]->pixmap().width() / dpr ) + 8;
194 int rightMargin = (m_d->m_iconbutton[realRight]->pixmap().width() / dpr) + 8;
195 // Note KDE does not reserve space for the highlight color
196 if (style()->inherits("OxygenStyle")) {
197 leftMargin = qMax(24, leftMargin);
198 rightMargin = qMax(24, rightMargin);
201 QMargins margins((m_d->m_iconEnabled[realLeft] ? leftMargin : 0), 0,
202 (m_d->m_iconEnabled[realRight] ? rightMargin : 0), 0);
204 setTextMargins(margins);
208 void FancyLineEdit::updateButtonPositions()
210 QRect contentRect = rect();
211 for (int i = 0; i < 2; ++i) {
212 Side iconpos = (Side)i;
213 if (layoutDirection() == Qt::RightToLeft)
214 iconpos = (iconpos == Left ? Right : Left);
216 if (iconpos == FancyLineEdit::Right) {
217 const int iconoffset = textMargins().right() + 4;
218 m_d->m_iconbutton[i]->setGeometry(
219 contentRect.adjusted(width() - iconoffset,
222 const int iconoffset = textMargins().left() + 4;
223 m_d->m_iconbutton[i]->setGeometry(
224 contentRect.adjusted(0, 0,
225 -width() + iconoffset, 0));
231 void FancyLineEdit::resizeEvent(QResizeEvent *)
233 updateButtonPositions();
237 void FancyLineEdit::keyPressEvent(QKeyEvent * e)
239 if (e->type() == QEvent::KeyPress && e->key() == Qt::Key_Down)
240 Q_EMIT downPressed();
242 QLineEdit::keyPressEvent(e);
246 void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
248 m_d->m_iconbutton[side]->setPixmap(buttonPixmap);
250 updateButtonPositions();
255 QPixmap FancyLineEdit::buttonPixmap(Side side) const
257 return m_d->m_pixmap[side];
261 void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
263 m_d->m_menu[side] = buttonMenu;
264 m_d->m_iconbutton[side]->setIconOpacity(1.0);
267 QMenu *FancyLineEdit::buttonMenu(Side side) const
269 return m_d->m_menu[side];
273 bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const
275 return m_d->m_menuTabFocusTrigger[side];
279 void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v)
281 if (m_d->m_menuTabFocusTrigger[side] == v)
284 m_d->m_menuTabFocusTrigger[side] = v;
285 m_d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
289 bool FancyLineEdit::hasAutoHideButton(Side side) const
291 return m_d->m_iconbutton[side]->hasAutoHide();
295 void FancyLineEdit::setAutoHideButton(Side side, bool h)
297 m_d->m_iconbutton[side]->setAutoHide(h);
299 m_d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
301 m_d->m_iconbutton[side]->setIconOpacity(1.0);
305 void FancyLineEdit::setButtonToolTip(Side side, const QString &tip)
307 m_d->m_iconbutton[side]->setToolTip(tip);
311 void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
313 m_d->m_iconbutton[side]->setFocusPolicy(policy);
317 ////////////////////////////////////////////////////////////////////////
319 // IconButton - helper class to represent a clickable icon
321 ////////////////////////////////////////////////////////////////////////
323 IconButton::IconButton(QWidget *parent)
324 : QAbstractButton(parent), m_iconOpacity(0.0), m_autoHide(false)
326 setCursor(Qt::ArrowCursor);
327 setFocusPolicy(Qt::NoFocus);
331 void IconButton::paintEvent(QPaintEvent *)
333 // Consider device/pixel ratio (HiDPI)
334 QWindow * window = this->window()->windowHandle();
335 qreal const dpr = window->devicePixelRatio();
336 QRect pixmapRect(QPoint(), m_pixmap.size() / dpr);
337 pixmapRect.moveCenter(rect().center());
338 QPixmap pm = m_pixmap;
340 QPainter painter(this);
342 painter.setOpacity(m_iconOpacity);
344 painter.drawPixmap(pixmapRect, pm);
348 void IconButton::animateShow(bool visible)
351 QPropertyAnimation *animation =
352 new QPropertyAnimation(this, "iconOpacity");
353 animation->setDuration(FADE_TIME);
354 animation->setEndValue(1.0);
355 animation->start(QAbstractAnimation::DeleteWhenStopped);
357 QPropertyAnimation *animation =
358 new QPropertyAnimation(this, "iconOpacity");
359 animation->setDuration(FADE_TIME);
360 animation->setEndValue(0.0);
361 animation->start(QAbstractAnimation::DeleteWhenStopped);
365 } // namespace frontend
369 #include "moc_FancyLineEdit.cpp"