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 >= 0x040600
23 #include <QPropertyAnimation>
24 #include <QApplication>
26 #include <QMouseEvent>
28 #include <QAbstractButton>
31 #include <QPaintEvent>
35 #define ICONBUTTON_HEIGHT 18
42 // --------- FancyLineEditPrivate
43 class FancyLineEditPrivate : public QObject {
45 explicit FancyLineEditPrivate(FancyLineEdit *parent);
47 bool eventFilter(QObject *obj, QEvent *event) override;
49 FancyLineEdit *m_lineEdit;
52 bool m_menuTabFocusTrigger[2];
53 IconButton *m_iconbutton[2];
54 bool m_iconEnabled[2];
58 FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) :
62 for (int i = 0; i < 2; ++i) {
64 m_menuTabFocusTrigger[i] = false;
65 m_iconbutton[i] = new IconButton(parent);
66 m_iconbutton[i]->installEventFilter(this);
67 m_iconbutton[i]->hide();
68 m_iconbutton[i]->setAutoHide(false);
69 m_iconEnabled[i] = false;
73 bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
76 for (int i = 0; i < 2; ++i) {
77 if (obj == m_iconbutton[i]) {
82 if (buttonIndex == -1)
83 return QObject::eventFilter(obj, event);
84 switch (event->type()) {
86 if (m_menuTabFocusTrigger[buttonIndex] && m_menu[buttonIndex]) {
87 m_lineEdit->setFocus();
88 m_menu[buttonIndex]->exec(m_iconbutton[buttonIndex]->mapToGlobal(
89 m_iconbutton[buttonIndex]->rect().center()));
95 return QObject::eventFilter(obj, event);
99 // --------- FancyLineEdit
100 FancyLineEdit::FancyLineEdit(QWidget *parent) :
102 m_d(new FancyLineEditPrivate(this))
107 connect(this, SIGNAL(textChanged(QString)),
108 this, SLOT(checkButtons(QString)));
109 connect(m_d->m_iconbutton[Left], SIGNAL(clicked()),
110 this, SLOT(iconClicked()));
111 connect(m_d->m_iconbutton[Right], SIGNAL(clicked()),
112 this, SLOT(iconClicked()));
115 void FancyLineEdit::checkButtons(const QString &text)
117 if (m_oldText.isEmpty() || text.isEmpty()) {
118 for (int i = 0; i < 2; ++i) {
119 if (m_d->m_iconbutton[i]->hasAutoHide())
120 m_d->m_iconbutton[i]->animateShow(!text.isEmpty());
126 void FancyLineEdit::setButtonVisible(Side side, bool visible)
128 m_d->m_iconbutton[side]->setVisible(visible);
129 m_d->m_iconEnabled[side] = visible;
133 bool FancyLineEdit::isButtonVisible(Side side) const
135 return m_d->m_iconEnabled[side];
138 void FancyLineEdit::iconClicked()
140 IconButton *button = qobject_cast<IconButton *>(sender());
142 for (int i = 0; i < 2; ++i)
143 if (m_d->m_iconbutton[i] == button)
147 if (m_d->m_menu[index]) {
148 m_d->m_menu[index]->exec(QCursor::pos());
150 buttonClicked((Side)index);
153 else if (index == Right)
154 rightButtonClicked();
158 void FancyLineEdit::updateMargins()
160 bool leftToRight = (layoutDirection() == Qt::LeftToRight);
161 Side realLeft = (leftToRight ? Left : Right);
162 Side realRight = (leftToRight ? Right : Left);
164 int leftMargin = m_d->m_iconbutton[realLeft]->pixmap().width() + 8;
165 int rightMargin = m_d->m_iconbutton[realRight]->pixmap().width() + 8;
166 // Note KDE does not reserve space for the highlight color
167 if (style()->inherits("OxygenStyle")) {
168 leftMargin = qMax(24, leftMargin);
169 rightMargin = qMax(24, rightMargin);
172 QMargins margins((m_d->m_iconEnabled[realLeft] ? leftMargin : 0), 0,
173 (m_d->m_iconEnabled[realRight] ? rightMargin : 0), 0);
175 setTextMargins(margins);
178 void FancyLineEdit::updateButtonPositions()
180 QRect contentRect = rect();
181 for (int i = 0; i < 2; ++i) {
182 Side iconpos = (Side)i;
183 if (layoutDirection() == Qt::RightToLeft)
184 iconpos = (iconpos == Left ? Right : Left);
186 if (iconpos == FancyLineEdit::Right) {
187 const int iconoffset = textMargins().right() + 4;
188 m_d->m_iconbutton[i]->
189 setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
191 const int iconoffset = textMargins().left() + 4;
192 m_d->m_iconbutton[i]->
193 setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
198 void FancyLineEdit::resizeEvent(QResizeEvent *)
200 updateButtonPositions();
204 void FancyLineEdit::keyPressEvent(QKeyEvent * e)
206 if (e->type() == QEvent::KeyPress && e->key() == Qt::Key_Down)
207 Q_EMIT downPressed();
209 QLineEdit::keyPressEvent(e);
213 void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
215 m_d->m_iconbutton[side]->setPixmap(buttonPixmap);
217 updateButtonPositions();
221 QPixmap FancyLineEdit::buttonPixmap(Side side) const
223 return m_d->m_pixmap[side];
226 void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
228 m_d->m_menu[side] = buttonMenu;
229 m_d->m_iconbutton[side]->setIconOpacity(1.0);
232 QMenu *FancyLineEdit::buttonMenu(Side side) const
234 return m_d->m_menu[side];
237 bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const
239 return m_d->m_menuTabFocusTrigger[side];
242 void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v)
244 if (m_d->m_menuTabFocusTrigger[side] == v)
247 m_d->m_menuTabFocusTrigger[side] = v;
248 m_d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
251 bool FancyLineEdit::hasAutoHideButton(Side side) const
253 return m_d->m_iconbutton[side]->hasAutoHide();
256 void FancyLineEdit::setAutoHideButton(Side side, bool h)
258 m_d->m_iconbutton[side]->setAutoHide(h);
260 m_d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
262 m_d->m_iconbutton[side]->setIconOpacity(1.0);
265 void FancyLineEdit::setButtonToolTip(Side side, const QString &tip)
267 m_d->m_iconbutton[side]->setToolTip(tip);
270 void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
272 m_d->m_iconbutton[side]->setFocusPolicy(policy);
275 // IconButton - helper class to represent a clickable icon
277 IconButton::IconButton(QWidget *parent)
278 : QAbstractButton(parent), m_iconOpacity(0.0), m_autoHide(false)
280 setCursor(Qt::ArrowCursor);
281 setFocusPolicy(Qt::NoFocus);
284 void IconButton::paintEvent(QPaintEvent *)
286 QPainter painter(this);
287 QRect pixmapRect = QRect(0, 0, m_pixmap.width(), m_pixmap.height());
288 pixmapRect.moveCenter(rect().center());
291 painter.setOpacity(m_iconOpacity);
293 painter.drawPixmap(pixmapRect, m_pixmap);
296 void IconButton::animateShow(bool visible)
299 QPropertyAnimation *animation =
300 new QPropertyAnimation(this, "iconOpacity");
301 animation->setDuration(FADE_TIME);
302 animation->setEndValue(1.0);
303 animation->start(QAbstractAnimation::DeleteWhenStopped);
305 QPropertyAnimation *animation =
306 new QPropertyAnimation(this, "iconOpacity");
307 animation->setDuration(FADE_TIME);
308 animation->setEndValue(0.0);
309 animation->start(QAbstractAnimation::DeleteWhenStopped);
313 } // namespace frontend
317 #endif // QT_VERSION >= 0x040600
319 #include "moc_FancyLineEdit.cpp"