Files
qt-creator/src/libs/utils/fancylineedit.cpp

301 lines
8.2 KiB
C++
Raw Normal View History

/**************************************************************************
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
**
** Contact: Nokia Corporation (qt-info@nokia.com)
2008-12-02 12:01:29 +01:00
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** 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
**
**************************************************************************/
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>
#include <QtGui/QAbstractButton>
#include <QtGui/QPainter>
#include <QtGui/QStyle>
#include <QtCore/QPropertyAnimation>
2008-12-02 12:01:29 +01:00
enum { margin = 6 };
2008-12-02 12:01:29 +01:00
#define ICONBUTTON_SIZE 18
#define FADE_TIME 160
2008-12-02 12:01:29 +01:00
namespace Utils {
2008-12-02 12:01:29 +01:00
// --------- FancyLineEditPrivate
2008-12-02 12:01:29 +01:00
class FancyLineEditPrivate : public QObject {
public:
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;
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;
bool m_autoHideIcon;
IconButton *m_iconbutton;
2008-12-02 12:01:29 +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),
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)
{
if (obj != m_iconbutton)
2008-12-02 12:01:29 +01:00
return QObject::eventFilter(obj, event);
switch (event->type()) {
case QEvent::FocusIn:
if (m_menuTabFocusTrigger && m_menu) {
2008-12-02 12:01:29 +01:00
m_lineEdit->setFocus();
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);
}
2008-12-02 12:01:29 +01:00
// --------- FancyLineEdit
FancyLineEdit::FancyLineEdit(QWidget *parent) :
QLineEdit(parent),
m_d(new FancyLineEditPrivate(this))
{
// 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"),
QIcon::fromTheme("edit-clear", QIcon(QLatin1String(":/core/images/editclear.png"))));
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
}
void FancyLineEdit::checkButton(const QString &text)
2008-12-02 12:01:29 +01: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
}
}
FancyLineEdit::~FancyLineEdit()
2008-12-02 12:01:29 +01:00
{
}
void FancyLineEdit::setSide(Side side)
2008-12-02 12:01:29 +01: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
}
QMargins margins;
if (iconpos == Right)
margins.setRight(size.width());
else
margins.setLeft(size.width());
setTextMargins(margins);
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::iconClicked()
2008-12-02 12:01:29 +01: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 *)
{
QRect contentRect = rect();
Side iconpos = m_d->m_side;
if (layoutDirection() == Qt::RightToLeft)
iconpos = (iconpos == Left ? Right : Left);
if (iconpos == FancyLineEdit::Right) {
const int iconoffset = textMargins().right() + 4;
m_d->m_iconbutton->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
} else {
const int iconoffset = textMargins().left() + 4;
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)
{
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;
m_d->m_iconbutton->setIconOpacity(1.0);
}
2008-12-02 12:01:29 +01:00
QMenu *FancyLineEdit::menu() const
{
return m_d->m_menu;
}
bool FancyLineEdit::hasMenuTabFocusTrigger() const
2008-12-02 12:01:29 +01:00
{
return m_d->m_menuTabFocusTrigger;
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::setMenuTabFocusTrigger(bool v)
2008-12-02 12:01:29 +01: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
}
bool FancyLineEdit::autoHideIcon() const
2008-12-02 12:01:29 +01:00
{
return m_d->m_autoHideIcon;
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::setAutoHideIcon(bool h)
2008-12-02 12:01:29 +01: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
}
void FancyLineEdit::setButtonToolTip(const QString &tip)
2008-12-02 12:01:29 +01:00
{
m_d->m_iconbutton->setToolTip(tip);
}
2008-12-02 12:01:29 +01: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());
pixmapRect.moveCenter(rect().translated(0,-1).center());
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