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
20 #include <QtCore/QEvent>
21 #include <QtCore/QDebug>
22 #include <QtCore/QString>
23 #include <QtCore/QPropertyAnimation>
24 #include <QtGui/QApplication>
25 #include <QtGui/QMenu>
26 #include <QtGui/QMouseEvent>
27 #include <QtGui/QLabel>
28 #include <QtGui/QAbstractButton>
29 #include <QtGui/QPainter>
30 #include <QtGui/QStyle>
31 #include <QtGui/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();
202 void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
204 m_d->m_iconbutton[side]->setPixmap(buttonPixmap);
206 updateButtonPositions();
210 QPixmap FancyLineEdit::buttonPixmap(Side side) const
212 return m_d->m_pixmap[side];
215 void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
217 m_d->m_menu[side] = buttonMenu;
218 m_d->m_iconbutton[side]->setIconOpacity(1.0);
221 QMenu *FancyLineEdit::buttonMenu(Side side) const
223 return m_d->m_menu[side];
226 bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const
228 return m_d->m_menuTabFocusTrigger[side];
231 void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v)
233 if (m_d->m_menuTabFocusTrigger[side] == v)
236 m_d->m_menuTabFocusTrigger[side] = v;
237 m_d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
240 bool FancyLineEdit::hasAutoHideButton(Side side) const
242 return m_d->m_iconbutton[side]->hasAutoHide();
245 void FancyLineEdit::setAutoHideButton(Side side, bool h)
247 m_d->m_iconbutton[side]->setAutoHide(h);
249 m_d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
251 m_d->m_iconbutton[side]->setIconOpacity(1.0);
254 void FancyLineEdit::setButtonToolTip(Side side, const QString &tip)
256 m_d->m_iconbutton[side]->setToolTip(tip);
259 void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
261 m_d->m_iconbutton[side]->setFocusPolicy(policy);
264 // IconButton - helper class to represent a clickable icon
266 IconButton::IconButton(QWidget *parent)
267 : QAbstractButton(parent), m_autoHide(false)
269 setCursor(Qt::ArrowCursor);
270 setFocusPolicy(Qt::NoFocus);
273 void IconButton::paintEvent(QPaintEvent *)
275 QPainter painter(this);
276 // Note isDown should really use the active state but in most styles
277 // this has no proper feedback
278 QIcon::Mode state = QIcon::Disabled;
280 state = isDown() ? QIcon::Selected : QIcon::Normal;
281 QRect pixmapRect = QRect(0, 0, m_pixmap.width(), m_pixmap.height());
282 pixmapRect.moveCenter(rect().center());
285 painter.setOpacity(m_iconOpacity);
287 painter.drawPixmap(pixmapRect, m_pixmap);
290 void IconButton::animateShow(bool visible)
293 QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity");
294 animation->setDuration(FADE_TIME);
295 animation->setEndValue(1.0);
296 animation->start(QAbstractAnimation::DeleteWhenStopped);
298 QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity");
299 animation->setDuration(FADE_TIME);
300 animation->setEndValue(0.0);
301 animation->start(QAbstractAnimation::DeleteWhenStopped);
309 #endif // QT_VERSION >= 0x040600
311 #include "moc_FancyLineEdit.cpp"