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)), this, SLOT(checkButtons(QString)));
108 connect(m_d->m_iconbutton[Left], SIGNAL(clicked()), this, SLOT(iconClicked()));
109 connect(m_d->m_iconbutton[Right], SIGNAL(clicked()), this, SLOT(iconClicked()));
112 void FancyLineEdit::checkButtons(const QString &text)
114 if (m_oldText.isEmpty() || text.isEmpty()) {
115 for (int i = 0; i < 2; ++i) {
116 if (m_d->m_iconbutton[i]->hasAutoHide())
117 m_d->m_iconbutton[i]->animateShow(!text.isEmpty());
123 FancyLineEdit::~FancyLineEdit()
127 void FancyLineEdit::setButtonVisible(Side side, bool visible)
129 m_d->m_iconbutton[side]->setVisible(visible);
130 m_d->m_iconEnabled[side] = visible;
134 bool FancyLineEdit::isButtonVisible(Side side) const
136 return m_d->m_iconEnabled[side];
139 void FancyLineEdit::iconClicked()
141 IconButton *button = qobject_cast<IconButton *>(sender());
143 for (int i = 0; i < 2; ++i)
144 if (m_d->m_iconbutton[i] == button)
148 if (m_d->m_menu[index]) {
149 m_d->m_menu[index]->exec(QCursor::pos());
151 buttonClicked((Side)index);
154 else if (index == Right)
155 rightButtonClicked();
159 void FancyLineEdit::updateMargins()
161 bool leftToRight = (layoutDirection() == Qt::LeftToRight);
162 Side realLeft = (leftToRight ? Left : Right);
163 Side realRight = (leftToRight ? Right : Left);
165 int leftMargin = m_d->m_iconbutton[realLeft]->pixmap().width() + 8;
166 int rightMargin = m_d->m_iconbutton[realRight]->pixmap().width() + 8;
167 // Note KDE does not reserve space for the highlight color
168 if (style()->inherits("OxygenStyle")) {
169 leftMargin = qMax(24, leftMargin);
170 rightMargin = qMax(24, rightMargin);
173 QMargins margins((m_d->m_iconEnabled[realLeft] ? leftMargin : 0), 0,
174 (m_d->m_iconEnabled[realRight] ? rightMargin : 0), 0);
176 setTextMargins(margins);
179 void FancyLineEdit::updateButtonPositions()
181 QRect contentRect = rect();
182 for (int i = 0; i < 2; ++i) {
183 Side iconpos = (Side)i;
184 if (layoutDirection() == Qt::RightToLeft)
185 iconpos = (iconpos == Left ? Right : Left);
187 if (iconpos == FancyLineEdit::Right) {
188 const int iconoffset = textMargins().right() + 4;
189 m_d->m_iconbutton[i]->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
191 const int iconoffset = textMargins().left() + 4;
192 m_d->m_iconbutton[i]->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
197 void FancyLineEdit::resizeEvent(QResizeEvent *)
199 updateButtonPositions();
203 void FancyLineEdit::keyPressEvent(QKeyEvent * e)
205 if (e->type() == QEvent::KeyPress && e->key() == Qt::Key_Down)
206 Q_EMIT downPressed();
208 QLineEdit::keyPressEvent(e);
212 void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
214 m_d->m_iconbutton[side]->setPixmap(buttonPixmap);
216 updateButtonPositions();
220 QPixmap FancyLineEdit::buttonPixmap(Side side) const
222 return m_d->m_pixmap[side];
225 void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
227 m_d->m_menu[side] = buttonMenu;
228 m_d->m_iconbutton[side]->setIconOpacity(1.0);
231 QMenu *FancyLineEdit::buttonMenu(Side side) const
233 return m_d->m_menu[side];
236 bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const
238 return m_d->m_menuTabFocusTrigger[side];
241 void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v)
243 if (m_d->m_menuTabFocusTrigger[side] == v)
246 m_d->m_menuTabFocusTrigger[side] = v;
247 m_d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
250 bool FancyLineEdit::hasAutoHideButton(Side side) const
252 return m_d->m_iconbutton[side]->hasAutoHide();
255 void FancyLineEdit::setAutoHideButton(Side side, bool h)
257 m_d->m_iconbutton[side]->setAutoHide(h);
259 m_d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
261 m_d->m_iconbutton[side]->setIconOpacity(1.0);
264 void FancyLineEdit::setButtonToolTip(Side side, const QString &tip)
266 m_d->m_iconbutton[side]->setToolTip(tip);
269 void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
271 m_d->m_iconbutton[side]->setFocusPolicy(policy);
274 // IconButton - helper class to represent a clickable icon
276 IconButton::IconButton(QWidget *parent)
277 : QAbstractButton(parent), m_iconOpacity(0.0), m_autoHide(false)
279 setCursor(Qt::ArrowCursor);
280 setFocusPolicy(Qt::NoFocus);
283 void IconButton::paintEvent(QPaintEvent *)
285 QPainter painter(this);
286 QRect pixmapRect = QRect(0, 0, m_pixmap.width(), m_pixmap.height());
287 pixmapRect.moveCenter(rect().center());
290 painter.setOpacity(m_iconOpacity);
292 painter.drawPixmap(pixmapRect, m_pixmap);
295 void IconButton::animateShow(bool visible)
298 QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity");
299 animation->setDuration(FADE_TIME);
300 animation->setEndValue(1.0);
301 animation->start(QAbstractAnimation::DeleteWhenStopped);
303 QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity");
304 animation->setDuration(FADE_TIME);
305 animation->setEndValue(0.0);
306 animation->start(QAbstractAnimation::DeleteWhenStopped);
314 #endif // QT_VERSION >= 0x040600
316 #include "moc_FancyLineEdit.cpp"