2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "fancylineedit.h"
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QEvent>
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
#include <QtCore/QString>
|
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
|
#include <QtGui/QMenu>
|
|
|
|
|
#include <QtGui/QMouseEvent>
|
|
|
|
|
#include <QtGui/QLabel>
|
2010-04-12 19:45:36 +02:00
|
|
|
#include <QtGui/QAbstractButton>
|
|
|
|
|
#include <QtGui/QPainter>
|
|
|
|
|
#include <QtGui/QStyle>
|
|
|
|
|
#include <QtCore/QPropertyAnimation>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
enum { margin = 6 };
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
#define ICONBUTTON_SIZE 18
|
|
|
|
|
#define FADE_TIME 160
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
namespace Utils {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
// --------- FancyLineEditPrivate
|
2008-12-02 12:01:29 +01:00
|
|
|
class FancyLineEditPrivate : public QObject {
|
|
|
|
|
public:
|
2009-11-27 13:54:27 +01:00
|
|
|
explicit FancyLineEditPrivate(FancyLineEdit *parent);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
virtual bool eventFilter(QObject *obj, QEvent *event);
|
|
|
|
|
|
|
|
|
|
const QString m_leftLabelStyleSheet;
|
|
|
|
|
const QString m_rightLabelStyleSheet;
|
|
|
|
|
|
2009-11-27 13:54:27 +01:00
|
|
|
FancyLineEdit *m_lineEdit;
|
2008-12-02 12:01:29 +01:00
|
|
|
QPixmap m_pixmap;
|
|
|
|
|
QMenu *m_menu;
|
|
|
|
|
FancyLineEdit::Side m_side;
|
|
|
|
|
bool m_useLayoutDirection;
|
|
|
|
|
bool m_menuTabFocusTrigger;
|
2010-04-12 19:45:36 +02:00
|
|
|
bool m_autoHideIcon;
|
|
|
|
|
IconButton *m_iconbutton;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2009-11-27 13:54:27 +01:00
|
|
|
FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) :
|
2008-12-02 12:01:29 +01:00
|
|
|
QObject(parent),
|
|
|
|
|
m_lineEdit(parent),
|
|
|
|
|
m_menu(0),
|
|
|
|
|
m_side(FancyLineEdit::Left),
|
|
|
|
|
m_useLayoutDirection(false),
|
2010-04-12 19:45:36 +02:00
|
|
|
m_menuTabFocusTrigger(false),
|
|
|
|
|
m_autoHideIcon(false),
|
|
|
|
|
m_iconbutton(new IconButton(parent))
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
if (obj != m_iconbutton)
|
2008-12-02 12:01:29 +01:00
|
|
|
return QObject::eventFilter(obj, event);
|
|
|
|
|
switch (event->type()) {
|
|
|
|
|
case QEvent::FocusIn:
|
2009-11-27 13:54:27 +01:00
|
|
|
if (m_menuTabFocusTrigger && m_menu) {
|
2008-12-02 12:01:29 +01:00
|
|
|
m_lineEdit->setFocus();
|
2010-04-12 19:45:36 +02:00
|
|
|
m_menu->exec(m_iconbutton->mapToGlobal(m_iconbutton->rect().center()));
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return QObject::eventFilter(obj, event);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
// --------- FancyLineEdit
|
|
|
|
|
FancyLineEdit::FancyLineEdit(QWidget *parent) :
|
|
|
|
|
QLineEdit(parent),
|
|
|
|
|
m_d(new FancyLineEditPrivate(this))
|
|
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
// KDE has custom icons for this. Notice that icon namings are counter intuitive
|
|
|
|
|
// If these icons are not avaiable we use the freedesktop standard name before
|
|
|
|
|
// falling back to a bundled resource
|
|
|
|
|
QIcon icon = QIcon::fromTheme(layoutDirection() == Qt::LeftToRight ?
|
|
|
|
|
QLatin1String("edit-clear-locationbar-rtl") :
|
|
|
|
|
QLatin1String("edit-clear-locationbar-ltr"),
|
2010-04-13 12:07:00 +02:00
|
|
|
QIcon::fromTheme("edit-clear", QIcon(QLatin1String(":/core/images/editclear.png"))));
|
2010-04-12 19:45:36 +02:00
|
|
|
|
|
|
|
|
m_d->m_iconbutton->installEventFilter(m_d);
|
|
|
|
|
m_d->m_iconbutton->setIcon(icon);
|
|
|
|
|
|
|
|
|
|
ensurePolished();
|
|
|
|
|
setSide(Left);
|
|
|
|
|
|
|
|
|
|
connect(this, SIGNAL(textChanged(QString)), this, SLOT(checkButton(QString)));
|
|
|
|
|
connect(m_d->m_iconbutton, SIGNAL(clicked()), this, SLOT(iconClicked()));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
void FancyLineEdit::checkButton(const QString &text)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
if (autoHideIcon()) {
|
|
|
|
|
static QString oldtext;
|
|
|
|
|
if (oldtext.isEmpty() || text.isEmpty())
|
|
|
|
|
m_d->m_iconbutton->animateShow(!text.isEmpty());
|
|
|
|
|
oldtext = text;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
FancyLineEdit::~FancyLineEdit()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
void FancyLineEdit::setSide(Side side)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
m_d->m_side = side;
|
|
|
|
|
|
|
|
|
|
Side iconpos = side;
|
|
|
|
|
if (layoutDirection() == Qt::RightToLeft)
|
|
|
|
|
iconpos = (side == Left ? Right : Left);
|
|
|
|
|
|
|
|
|
|
// Make room for icon
|
|
|
|
|
|
|
|
|
|
// Let the style determine minimum height for our widget
|
|
|
|
|
QSize size(ICONBUTTON_SIZE + 6, ICONBUTTON_SIZE + 2);
|
|
|
|
|
|
|
|
|
|
// Note KDE does not reserve space for the highlight color
|
|
|
|
|
if (style()->inherits("OxygenStyle")) {
|
|
|
|
|
size = size.expandedTo(QSize(24, 0));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-04-12 19:45:36 +02:00
|
|
|
|
|
|
|
|
QMargins margins;
|
|
|
|
|
if (iconpos == Right)
|
|
|
|
|
margins.setRight(size.width());
|
|
|
|
|
else
|
|
|
|
|
margins.setLeft(size.width());
|
|
|
|
|
|
|
|
|
|
setTextMargins(margins);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
void FancyLineEdit::iconClicked()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
if (m_d->m_menu) {
|
|
|
|
|
m_d->m_menu->exec(QCursor::pos());
|
|
|
|
|
} else {
|
|
|
|
|
emit buttonClicked();
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FancyLineEdit::Side FancyLineEdit::side() const
|
|
|
|
|
{
|
|
|
|
|
return m_d->m_side;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FancyLineEdit::resizeEvent(QResizeEvent *)
|
|
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
QRect contentRect = rect();
|
|
|
|
|
Side iconpos = m_d->m_side;
|
|
|
|
|
if (layoutDirection() == Qt::RightToLeft)
|
|
|
|
|
iconpos = (iconpos == Left ? Right : Left);
|
|
|
|
|
|
|
|
|
|
if (iconpos == FancyLineEdit::Right) {
|
2010-04-13 12:07:00 +02:00
|
|
|
const int iconoffset = textMargins().right() + 4;
|
2010-04-12 19:45:36 +02:00
|
|
|
m_d->m_iconbutton->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
|
|
|
|
|
} else {
|
2010-04-13 12:07:00 +02:00
|
|
|
const int iconoffset = textMargins().left() + 4;
|
2010-04-12 19:45:36 +02:00
|
|
|
m_d->m_iconbutton->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FancyLineEdit::setPixmap(const QPixmap &pixmap)
|
|
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
m_d->m_iconbutton->setIcon(pixmap);
|
|
|
|
|
updateGeometry();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QPixmap FancyLineEdit::pixmap() const
|
|
|
|
|
{
|
|
|
|
|
return m_d->m_pixmap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FancyLineEdit::setMenu(QMenu *menu)
|
|
|
|
|
{
|
|
|
|
|
m_d->m_menu = menu;
|
2010-04-12 19:45:36 +02:00
|
|
|
m_d->m_iconbutton->setIconOpacity(1.0);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
QMenu *FancyLineEdit::menu() const
|
|
|
|
|
{
|
|
|
|
|
return m_d->m_menu;
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
bool FancyLineEdit::hasMenuTabFocusTrigger() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
return m_d->m_menuTabFocusTrigger;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
void FancyLineEdit::setMenuTabFocusTrigger(bool v)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
if (m_d->m_menuTabFocusTrigger == v)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_d->m_menuTabFocusTrigger = v;
|
|
|
|
|
m_d->m_iconbutton->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
bool FancyLineEdit::autoHideIcon() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
return m_d->m_autoHideIcon;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
void FancyLineEdit::setAutoHideIcon(bool h)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
m_d->m_autoHideIcon = h;
|
|
|
|
|
if (h)
|
|
|
|
|
m_d->m_iconbutton->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
|
|
|
|
|
else
|
|
|
|
|
m_d->m_iconbutton->setIconOpacity(1.0);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
void FancyLineEdit::setButtonToolTip(const QString &tip)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-04-12 19:45:36 +02:00
|
|
|
m_d->m_iconbutton->setToolTip(tip);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-04-12 19:45:36 +02:00
|
|
|
void FancyLineEdit::setButtonFocusPolicy(Qt::FocusPolicy policy)
|
|
|
|
|
{
|
|
|
|
|
m_d->m_iconbutton->setFocusPolicy(policy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IconButton - helper class to represent a clickable icon
|
|
|
|
|
|
|
|
|
|
IconButton::IconButton(QWidget *parent)
|
|
|
|
|
: QAbstractButton(parent)
|
|
|
|
|
{
|
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IconButton::paintEvent(QPaintEvent *)
|
|
|
|
|
{
|
|
|
|
|
QPainter painter(this);
|
|
|
|
|
// Note isDown should really use the active state but in most styles
|
|
|
|
|
// this has no proper feedback
|
|
|
|
|
QIcon::Mode state = QIcon::Disabled;
|
|
|
|
|
if (isEnabled())
|
|
|
|
|
state = isDown() ? QIcon::Selected : QIcon::Normal;
|
|
|
|
|
QPixmap iconpixmap = icon().pixmap(QSize(ICONBUTTON_SIZE, ICONBUTTON_SIZE),
|
|
|
|
|
state, QIcon::Off);
|
|
|
|
|
QRect pixmapRect = QRect(0, 0, iconpixmap.width(), iconpixmap.height());
|
2010-04-13 12:07:00 +02:00
|
|
|
pixmapRect.moveCenter(rect().translated(0,-1).center());
|
2010-04-12 19:45:36 +02:00
|
|
|
|
|
|
|
|
if (static_cast<FancyLineEdit*>(parentWidget())->autoHideIcon())
|
|
|
|
|
painter.setOpacity(m_iconOpacity);
|
|
|
|
|
|
|
|
|
|
painter.drawPixmap(pixmapRect, iconpixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IconButton::animateShow(bool visible)
|
|
|
|
|
{
|
|
|
|
|
if (visible) {
|
|
|
|
|
QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity");
|
|
|
|
|
animation->setDuration(FADE_TIME);
|
|
|
|
|
animation->setEndValue(1.0);
|
|
|
|
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
|
|
|
|
} else {
|
|
|
|
|
QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity");
|
|
|
|
|
animation->setDuration(FADE_TIME);
|
|
|
|
|
animation->setEndValue(0.0);
|
|
|
|
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Utils
|