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 virtual bool eventFilter(QObject *obj, QEvent *event);
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 FancyLineEdit::~FancyLineEdit()
130 void FancyLineEdit::setButtonVisible(Side side, bool visible)
132 m_d->m_iconbutton[side]->setVisible(visible);
133 m_d->m_iconEnabled[side] = visible;
137 bool FancyLineEdit::isButtonVisible(Side side) const
139 return m_d->m_iconEnabled[side];
142 void FancyLineEdit::iconClicked()
144 IconButton *button = qobject_cast<IconButton *>(sender());
146 for (int i = 0; i < 2; ++i)
147 if (m_d->m_iconbutton[i] == button)
151 if (m_d->m_menu[index]) {
152 m_d->m_menu[index]->exec(QCursor::pos());
154 buttonClicked((Side)index);
157 else if (index == Right)
158 rightButtonClicked();
162 void FancyLineEdit::updateMargins()
164 bool leftToRight = (layoutDirection() == Qt::LeftToRight);
165 Side realLeft = (leftToRight ? Left : Right);
166 Side realRight = (leftToRight ? Right : Left);
168 int leftMargin = m_d->m_iconbutton[realLeft]->pixmap().width() + 8;
169 int rightMargin = m_d->m_iconbutton[realRight]->pixmap().width() + 8;
170 // Note KDE does not reserve space for the highlight color
171 if (style()->inherits("OxygenStyle")) {
172 leftMargin = qMax(24, leftMargin);
173 rightMargin = qMax(24, rightMargin);
176 QMargins margins((m_d->m_iconEnabled[realLeft] ? leftMargin : 0), 0,
177 (m_d->m_iconEnabled[realRight] ? rightMargin : 0), 0);
179 setTextMargins(margins);
182 void FancyLineEdit::updateButtonPositions()
184 QRect contentRect = rect();
185 for (int i = 0; i < 2; ++i) {
186 Side iconpos = (Side)i;
187 if (layoutDirection() == Qt::RightToLeft)
188 iconpos = (iconpos == Left ? Right : Left);
190 if (iconpos == FancyLineEdit::Right) {
191 const int iconoffset = textMargins().right() + 4;
192 m_d->m_iconbutton[i]->
193 setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
195 const int iconoffset = textMargins().left() + 4;
196 m_d->m_iconbutton[i]->
197 setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
202 void FancyLineEdit::resizeEvent(QResizeEvent *)
204 updateButtonPositions();
208 void FancyLineEdit::keyPressEvent(QKeyEvent * e)
210 if (e->type() == QEvent::KeyPress && e->key() == Qt::Key_Down)
211 Q_EMIT downPressed();
213 QLineEdit::keyPressEvent(e);
217 void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
219 m_d->m_iconbutton[side]->setPixmap(buttonPixmap);
221 updateButtonPositions();
225 QPixmap FancyLineEdit::buttonPixmap(Side side) const
227 return m_d->m_pixmap[side];
230 void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
232 m_d->m_menu[side] = buttonMenu;
233 m_d->m_iconbutton[side]->setIconOpacity(1.0);
236 QMenu *FancyLineEdit::buttonMenu(Side side) const
238 return m_d->m_menu[side];
241 bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const
243 return m_d->m_menuTabFocusTrigger[side];
246 void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v)
248 if (m_d->m_menuTabFocusTrigger[side] == v)
251 m_d->m_menuTabFocusTrigger[side] = v;
252 m_d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
255 bool FancyLineEdit::hasAutoHideButton(Side side) const
257 return m_d->m_iconbutton[side]->hasAutoHide();
260 void FancyLineEdit::setAutoHideButton(Side side, bool h)
262 m_d->m_iconbutton[side]->setAutoHide(h);
264 m_d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
266 m_d->m_iconbutton[side]->setIconOpacity(1.0);
269 void FancyLineEdit::setButtonToolTip(Side side, const QString &tip)
271 m_d->m_iconbutton[side]->setToolTip(tip);
274 void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
276 m_d->m_iconbutton[side]->setFocusPolicy(policy);
279 // IconButton - helper class to represent a clickable icon
281 IconButton::IconButton(QWidget *parent)
282 : QAbstractButton(parent), m_iconOpacity(0.0), m_autoHide(false)
284 setCursor(Qt::ArrowCursor);
285 setFocusPolicy(Qt::NoFocus);
288 void IconButton::paintEvent(QPaintEvent *)
290 QPainter painter(this);
291 QRect pixmapRect = QRect(0, 0, m_pixmap.width(), m_pixmap.height());
292 pixmapRect.moveCenter(rect().center());
295 painter.setOpacity(m_iconOpacity);
297 painter.drawPixmap(pixmapRect, m_pixmap);
300 void IconButton::animateShow(bool visible)
303 QPropertyAnimation *animation =
304 new QPropertyAnimation(this, "iconOpacity");
305 animation->setDuration(FADE_TIME);
306 animation->setEndValue(1.0);
307 animation->start(QAbstractAnimation::DeleteWhenStopped);
309 QPropertyAnimation *animation =
310 new QPropertyAnimation(this, "iconOpacity");
311 animation->setDuration(FADE_TIME);
312 animation->setEndValue(0.0);
313 animation->start(QAbstractAnimation::DeleteWhenStopped);
321 #endif // QT_VERSION >= 0x040600
323 #include "moc_FancyLineEdit.cpp"