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 // In earlier Qt versions, we roll our own button
145 #if QT_VERSION < 0x050200
146 setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
147 setButtonVisible(FancyLineEdit::Right, visible);
148 setAutoHideButton(FancyLineEdit::Right, true);
150 setClearButtonEnabled(visible);
155 void FancyLineEdit::setButtonVisible(Side side, bool visible)
157 m_d->m_iconbutton[side]->setVisible(visible);
158 m_d->m_iconEnabled[side] = visible;
163 bool FancyLineEdit::isButtonVisible(Side side) const
165 return m_d->m_iconEnabled[side];
169 void FancyLineEdit::iconClicked()
171 IconButton *button = qobject_cast<IconButton *>(sender());
173 for (int i = 0; i < 2; ++i)
174 if (m_d->m_iconbutton[i] == button)
178 if (m_d->m_menu[index]) {
179 m_d->m_menu[index]->exec(QCursor::pos());
181 buttonClicked((Side)index);
184 else if (index == Right)
185 rightButtonClicked();
190 void FancyLineEdit::updateMargins()
192 bool leftToRight = (layoutDirection() == Qt::LeftToRight);
193 Side realLeft = (leftToRight ? Left : Right);
194 Side realRight = (leftToRight ? Right : Left);
197 // Consider device/pixel ratio (HiDPI)
198 dpr = devicePixelRatio();
199 int leftMargin = (m_d->m_iconbutton[realLeft]->pixmap().width() / dpr ) + 8;
200 int rightMargin = (m_d->m_iconbutton[realRight]->pixmap().width() / dpr) + 8;
201 // Note KDE does not reserve space for the highlight color
202 if (style()->inherits("OxygenStyle")) {
203 leftMargin = qMax(24, leftMargin);
204 rightMargin = qMax(24, rightMargin);
207 QMargins margins((m_d->m_iconEnabled[realLeft] ? leftMargin : 0), 0,
208 (m_d->m_iconEnabled[realRight] ? rightMargin : 0), 0);
210 setTextMargins(margins);
214 void FancyLineEdit::updateButtonPositions()
216 QRect contentRect = rect();
217 for (int i = 0; i < 2; ++i) {
218 Side iconpos = (Side)i;
219 if (layoutDirection() == Qt::RightToLeft)
220 iconpos = (iconpos == Left ? Right : Left);
222 if (iconpos == FancyLineEdit::Right) {
223 const int iconoffset = textMargins().right() + 4;
224 m_d->m_iconbutton[i]->setGeometry(
225 contentRect.adjusted(width() - iconoffset,
228 const int iconoffset = textMargins().left() + 4;
229 m_d->m_iconbutton[i]->setGeometry(
230 contentRect.adjusted(0, 0,
231 -width() + iconoffset, 0));
237 void FancyLineEdit::resizeEvent(QResizeEvent *)
239 updateButtonPositions();
243 void FancyLineEdit::keyPressEvent(QKeyEvent * e)
245 if (e->type() == QEvent::KeyPress && e->key() == Qt::Key_Down)
246 Q_EMIT downPressed();
248 QLineEdit::keyPressEvent(e);
252 void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
254 m_d->m_iconbutton[side]->setPixmap(buttonPixmap);
256 updateButtonPositions();
261 QPixmap FancyLineEdit::buttonPixmap(Side side) const
263 return m_d->m_pixmap[side];
267 void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
269 m_d->m_menu[side] = buttonMenu;
270 m_d->m_iconbutton[side]->setIconOpacity(1.0);
273 QMenu *FancyLineEdit::buttonMenu(Side side) const
275 return m_d->m_menu[side];
279 bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const
281 return m_d->m_menuTabFocusTrigger[side];
285 void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v)
287 if (m_d->m_menuTabFocusTrigger[side] == v)
290 m_d->m_menuTabFocusTrigger[side] = v;
291 m_d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
295 bool FancyLineEdit::hasAutoHideButton(Side side) const
297 return m_d->m_iconbutton[side]->hasAutoHide();
301 void FancyLineEdit::setAutoHideButton(Side side, bool h)
303 m_d->m_iconbutton[side]->setAutoHide(h);
305 m_d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
307 m_d->m_iconbutton[side]->setIconOpacity(1.0);
311 void FancyLineEdit::setButtonToolTip(Side side, const QString &tip)
313 m_d->m_iconbutton[side]->setToolTip(tip);
317 void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
319 m_d->m_iconbutton[side]->setFocusPolicy(policy);
323 ////////////////////////////////////////////////////////////////////////
325 // IconButton - helper class to represent a clickable icon
327 ////////////////////////////////////////////////////////////////////////
329 IconButton::IconButton(QWidget *parent)
330 : QAbstractButton(parent), m_iconOpacity(0.0), m_autoHide(false)
332 setCursor(Qt::ArrowCursor);
333 setFocusPolicy(Qt::NoFocus);
337 void IconButton::paintEvent(QPaintEvent *)
339 // Consider device/pixel ratio (HiDPI)
340 QWindow * window = this->window()->windowHandle();
341 qreal const dpr = window->devicePixelRatio();
342 QRect pixmapRect(QPoint(), m_pixmap.size() / dpr);
343 pixmapRect.moveCenter(rect().center());
344 QPixmap pm = m_pixmap;
346 QPainter painter(this);
348 painter.setOpacity(m_iconOpacity);
350 painter.drawPixmap(pixmapRect, pm);
354 void IconButton::animateShow(bool visible)
357 QPropertyAnimation *animation =
358 new QPropertyAnimation(this, "iconOpacity");
359 animation->setDuration(FADE_TIME);
360 animation->setEndValue(1.0);
361 animation->start(QAbstractAnimation::DeleteWhenStopped);
363 QPropertyAnimation *animation =
364 new QPropertyAnimation(this, "iconOpacity");
365 animation->setDuration(FADE_TIME);
366 animation->setEndValue(0.0);
367 animation->start(QAbstractAnimation::DeleteWhenStopped);
371 } // namespace frontend
375 #include "moc_FancyLineEdit.cpp"