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

374 lines
11 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
** Contact: http://www.qt-project.org/
2008-12-02 12:01:29 +01:00
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** 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.
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
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 "historycompleter.h"
#include "qtcassert.h"
2008-12-02 12:01:29 +01:00
#include <QEvent>
#include <QDebug>
#include <QString>
#include <QPropertyAnimation>
#include <QApplication>
#include <QMenu>
#include <QMouseEvent>
#include <QLabel>
#include <QAbstractButton>
#include <QPainter>
#include <QStyle>
#include <QPaintEvent>
#include <QDesktopWidget>
/*! Opens a menu at the specified widget position.
* This functions computes the position where to show the menu, and opens it with
* QMenu::exec().
* \param menu The menu to open
* \param widget The widget next to which to open the menu
*/
static void execMenuAtWidget(QMenu *menu, QWidget *widget)
{
QPoint p;
QRect screen = qApp->desktop()->availableGeometry(widget);
QSize sh = menu->sizeHint();
QRect rect = widget->rect();
if (widget->isRightToLeft()) {
if (widget->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) {
p = widget->mapToGlobal(rect.bottomRight());
} else {
p = widget->mapToGlobal(rect.topRight() - QPoint(0, sh.height()));
}
p.rx() -= sh.width();
} else {
if (widget->mapToGlobal(QPoint(0, rect.bottom())).y() + sh.height() <= screen.height()) {
p = widget->mapToGlobal(rect.bottomLeft());
} else {
p = widget->mapToGlobal(rect.topLeft() - QPoint(0, sh.height()));
}
}
p.rx() = qMax(screen.left(), qMin(p.x(), screen.right() - sh.width()));
p.ry() += 1;
menu->exec(p);
}
2008-12-02 12:01:29 +01:00
/*!
\class Utils::FancyLineEdit
\brief A line edit with an embedded pixmap on one side that is connected to
a menu.
Additionally, it can display a grayed hintText (like "Type Here to")
when not focused and empty. When connecting to the changed signals and
querying text, one has to be aware that the text is set to that hint
text if isShowingHintText() returns true (that is, does not contain
valid user input).
*/
enum { margin = 6 };
2008-12-02 12:01:29 +01:00
#define ICONBUTTON_HEIGHT 18
#define FADE_TIME 160
2008-12-02 12:01:29 +01:00
namespace Utils {
2008-12-02 12:01:29 +01:00
// --------- FancyLineEditPrivate
class FancyLineEditPrivate : public QObject
{
2008-12-02 12:01:29 +01:00
public:
explicit FancyLineEditPrivate(FancyLineEdit *parent);
2008-12-02 12:01:29 +01:00
virtual bool eventFilter(QObject *obj, QEvent *event);
FancyLineEdit *m_lineEdit;
QPixmap m_pixmap[2];
QMenu *m_menu[2];
bool m_menuTabFocusTrigger[2];
IconButton *m_iconbutton[2];
bool m_iconEnabled[2];
HistoryCompleter *m_historyCompleter;
2008-12-02 12:01:29 +01:00
};
FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) :
QObject(parent), m_lineEdit(parent), m_historyCompleter(0)
2008-12-02 12:01:29 +01:00
{
for (int i = 0; i < 2; ++i) {
m_menu[i] = 0;
m_menuTabFocusTrigger[i] = false;
m_iconbutton[i] = new IconButton(parent);
m_iconbutton[i]->installEventFilter(this);
m_iconbutton[i]->hide();
m_iconbutton[i]->setAutoHide(false);
m_iconEnabled[i] = false;
}
2008-12-02 12:01:29 +01:00
}
bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
{
int buttonIndex = -1;
for (int i = 0; i < 2; ++i) {
if (obj == m_iconbutton[i]) {
buttonIndex = i;
break;
}
}
if (buttonIndex == -1)
2008-12-02 12:01:29 +01:00
return QObject::eventFilter(obj, event);
switch (event->type()) {
case QEvent::FocusIn:
if (m_menuTabFocusTrigger[buttonIndex] && m_menu[buttonIndex]) {
2008-12-02 12:01:29 +01:00
m_lineEdit->setFocus();
execMenuAtWidget(m_menu[buttonIndex], m_iconbutton[buttonIndex]);
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),
d(new FancyLineEditPrivate(this))
2008-12-02 12:01:29 +01:00
{
ensurePolished();
updateMargins();
connect(this, SIGNAL(textChanged(QString)), this, SLOT(checkButtons(QString)));
connect(d->m_iconbutton[Left], SIGNAL(clicked()), this, SLOT(iconClicked()));
connect(d->m_iconbutton[Right], SIGNAL(clicked()), this, SLOT(iconClicked()));
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::checkButtons(const QString &text)
2008-12-02 12:01:29 +01:00
{
if (m_oldText.isEmpty() || text.isEmpty()) {
for (int i = 0; i < 2; ++i) {
if (d->m_iconbutton[i]->hasAutoHide())
d->m_iconbutton[i]->animateShow(!text.isEmpty());
}
m_oldText = text;
2008-12-02 12:01:29 +01:00
}
}
FancyLineEdit::~FancyLineEdit()
2008-12-02 12:01:29 +01:00
{
}
void FancyLineEdit::setButtonVisible(Side side, bool visible)
2008-12-02 12:01:29 +01:00
{
d->m_iconbutton[side]->setVisible(visible);
d->m_iconEnabled[side] = visible;
updateMargins();
}
bool FancyLineEdit::isButtonVisible(Side side) const
{
return d->m_iconEnabled[side];
}
void FancyLineEdit::iconClicked()
{
IconButton *button = qobject_cast<IconButton *>(sender());
int index = -1;
for (int i = 0; i < 2; ++i)
if (d->m_iconbutton[i] == button)
index = i;
if (index == -1)
return;
if (d->m_menu[index]) {
execMenuAtWidget(d->m_menu[index], button);
} else {
emit buttonClicked((Side)index);
if (index == Left)
emit leftButtonClicked();
else if (index == Right)
emit rightButtonClicked();
}
}
void FancyLineEdit::updateMargins()
{
bool leftToRight = (layoutDirection() == Qt::LeftToRight);
Side realLeft = (leftToRight ? Left : Right);
Side realRight = (leftToRight ? Right : Left);
int leftMargin = d->m_iconbutton[realLeft]->pixmap().width() + 8;
int rightMargin = d->m_iconbutton[realRight]->pixmap().width() + 8;
// Note KDE does not reserve space for the highlight color
if (style()->inherits("OxygenStyle")) {
leftMargin = qMax(24, leftMargin);
rightMargin = qMax(24, rightMargin);
2008-12-02 12:01:29 +01:00
}
QMargins margins((d->m_iconEnabled[realLeft] ? leftMargin : 0), 0,
(d->m_iconEnabled[realRight] ? rightMargin : 0), 0);
setTextMargins(margins);
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::updateButtonPositions()
2008-12-02 12:01:29 +01:00
{
QRect contentRect = rect();
for (int i = 0; i < 2; ++i) {
Side iconpos = (Side)i;
if (layoutDirection() == Qt::RightToLeft)
iconpos = (iconpos == Left ? Right : Left);
if (iconpos == FancyLineEdit::Right) {
const int iconoffset = textMargins().right() + 4;
d->m_iconbutton[i]->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
} else {
const int iconoffset = textMargins().left() + 4;
d->m_iconbutton[i]->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
}
}
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::resizeEvent(QResizeEvent *)
{
updateButtonPositions();
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
2008-12-02 12:01:29 +01:00
{
d->m_iconbutton[side]->setPixmap(buttonPixmap);
updateMargins();
updateButtonPositions();
update();
2008-12-02 12:01:29 +01:00
}
QPixmap FancyLineEdit::buttonPixmap(Side side) const
2008-12-02 12:01:29 +01:00
{
return d->m_pixmap[side];
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
2008-12-02 12:01:29 +01:00
{
d->m_menu[side] = buttonMenu;
d->m_iconbutton[side]->setIconOpacity(1.0);
}
2008-12-02 12:01:29 +01:00
QMenu *FancyLineEdit::buttonMenu(Side side) const
2008-12-02 12:01:29 +01:00
{
return d->m_menu[side];
2008-12-02 12:01:29 +01:00
}
bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const
2008-12-02 12:01:29 +01:00
{
return d->m_menuTabFocusTrigger[side];
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v)
2008-12-02 12:01:29 +01:00
{
if (d->m_menuTabFocusTrigger[side] == v)
return;
d->m_menuTabFocusTrigger[side] = v;
d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
2008-12-02 12:01:29 +01:00
}
bool FancyLineEdit::hasAutoHideButton(Side side) const
2008-12-02 12:01:29 +01:00
{
return d->m_iconbutton[side]->hasAutoHide();
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::setHistoryCompleter(const QString &historyKey)
{
QTC_ASSERT(!d->m_historyCompleter, return);
d->m_historyCompleter = new HistoryCompleter(this, historyKey);
QLineEdit::setCompleter(d->m_historyCompleter);
}
void FancyLineEdit::setSpecialCompleter(QCompleter *completer)
{
QTC_ASSERT(!d->m_historyCompleter, return);
QLineEdit::setCompleter(completer);
}
void FancyLineEdit::setAutoHideButton(Side side, bool h)
2008-12-02 12:01:29 +01:00
{
d->m_iconbutton[side]->setAutoHide(h);
if (h)
d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
else
d->m_iconbutton[side]->setIconOpacity(1.0);
2008-12-02 12:01:29 +01:00
}
void FancyLineEdit::setButtonToolTip(Side side, const QString &tip)
2008-12-02 12:01:29 +01:00
{
d->m_iconbutton[side]->setToolTip(tip);
}
2008-12-02 12:01:29 +01:00
void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
{
d->m_iconbutton[side]->setFocusPolicy(policy);
}
// IconButton - helper class to represent a clickable icon
IconButton::IconButton(QWidget *parent)
: QAbstractButton(parent), m_autoHide(false)
{
setCursor(Qt::ArrowCursor);
setFocusPolicy(Qt::NoFocus);
}
void IconButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QRect pixmapRect = QRect(0, 0, m_pixmap.width(), m_pixmap.height());
pixmapRect.moveCenter(rect().center());
if (m_autoHide)
painter.setOpacity(m_iconOpacity);
painter.drawPixmap(pixmapRect, m_pixmap);
}
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