Commit 6dc4a039 authored by con's avatar con
Browse files

Refactor fancy line edit to optionally have two buttons.

Also unify some method naming.
Use the new feature to add clear buttons to the Locator,
and the find tool bar.

Task-number: QTCREATORBUG-705
parent 1d887410
......@@ -44,7 +44,7 @@
enum { margin = 6 };
#define ICONBUTTON_SIZE 18
#define ICONBUTTON_HEIGHT 18
#define FADE_TIME 160
namespace Utils {
......@@ -56,41 +56,47 @@ public:
virtual bool eventFilter(QObject *obj, QEvent *event);
const QString m_leftLabelStyleSheet;
const QString m_rightLabelStyleSheet;
FancyLineEdit *m_lineEdit;
QPixmap m_pixmap;
QMenu *m_menu;
FancyLineEdit::Side m_side;
bool m_useLayoutDirection;
bool m_menuTabFocusTrigger;
bool m_autoHideIcon;
IconButton *m_iconbutton;
QPixmap m_pixmap[2];
QMenu *m_menu[2];
bool m_menuTabFocusTrigger[2];
IconButton *m_iconbutton[2];
bool m_iconEnabled[2];
};
FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) :
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))
m_lineEdit(parent)
{
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;
}
}
bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
{
if (obj != m_iconbutton)
int buttonIndex = -1;
for (int i = 0; i < 2; ++i) {
if (obj == m_iconbutton[i]) {
buttonIndex = i;
break;
}
}
if (buttonIndex == -1)
return QObject::eventFilter(obj, event);
switch (event->type()) {
case QEvent::FocusIn:
if (m_menuTabFocusTrigger && m_menu) {
if (m_menuTabFocusTrigger[buttonIndex] && m_menu[buttonIndex]) {
m_lineEdit->setFocus();
m_menu->exec(m_iconbutton->mapToGlobal(m_iconbutton->rect().center()));
m_menu[buttonIndex]->exec(m_iconbutton[buttonIndex]->mapToGlobal(
m_iconbutton[buttonIndex]->rect().center()));
return true;
}
default:
......@@ -105,29 +111,21 @@ 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);
updateMargins();
connect(this, SIGNAL(textChanged(QString)), this, SLOT(checkButton(QString)));
connect(m_d->m_iconbutton, SIGNAL(clicked()), this, SLOT(iconClicked()));
connect(this, SIGNAL(textChanged(QString)), this, SLOT(checkButtons(QString)));
connect(m_d->m_iconbutton[Left], SIGNAL(clicked()), this, SLOT(iconClicked()));
connect(m_d->m_iconbutton[Right], SIGNAL(clicked()), this, SLOT(iconClicked()));
}
void FancyLineEdit::checkButton(const QString &text)
void FancyLineEdit::checkButtons(const QString &text)
{
if (autoHideIcon()) {
if (m_oldText.isEmpty() || text.isEmpty())
m_d->m_iconbutton->animateShow(!text.isEmpty());
if (m_oldText.isEmpty() || text.isEmpty()) {
for (int i = 0; i < 2; ++i) {
if (m_d->m_iconbutton[i]->hasAutoHide())
m_d->m_iconbutton[i]->animateShow(!text.isEmpty());
}
m_oldText = text;
}
}
......@@ -136,127 +134,147 @@ FancyLineEdit::~FancyLineEdit()
{
}
void FancyLineEdit::setSide(Side side)
void FancyLineEdit::setButtonVisible(Side side, bool visible)
{
m_d->m_side = side;
m_d->m_iconbutton[side]->setVisible(visible);
m_d->m_iconEnabled[side] = visible;
updateMargins();
}
Side iconpos = side;
if (layoutDirection() == Qt::RightToLeft)
iconpos = (side == Left ? Right : Left);
bool FancyLineEdit::isButtonVisible(Side side) const
{
return m_d->m_iconEnabled[side];
}
// Make room for icon
void FancyLineEdit::iconClicked()
{
IconButton *button = qobject_cast<IconButton *>(sender());
int index = -1;
for (int i = 0; i < 2; ++i)
if (m_d->m_iconbutton[i] == button)
index = i;
if (index == -1)
return;
if (m_d->m_menu[index]) {
m_d->m_menu[index]->exec(QCursor::pos());
} else {
emit buttonClicked((Side)index);
if (index == Left)
emit leftButtonClicked();
else if (index == Right)
emit rightButtonClicked();
}
}
// Let the style determine minimum height for our widget
QSize size(ICONBUTTON_SIZE + 6, ICONBUTTON_SIZE + 2);
void FancyLineEdit::updateMargins()
{
bool leftToRight = (layoutDirection() == Qt::LeftToRight);
Side realLeft = (leftToRight ? Left : Right);
Side realRight = (leftToRight ? Right : Left);
int leftMargin = m_d->m_iconbutton[realLeft]->pixmap().width() + 8;
int rightMargin = m_d->m_iconbutton[realRight]->pixmap().width() + 8;
// Note KDE does not reserve space for the highlight color
if (style()->inherits("OxygenStyle")) {
size = size.expandedTo(QSize(24, 0));
leftMargin = qMax(24, leftMargin);
rightMargin = qMax(24, rightMargin);
}
QMargins margins;
if (iconpos == Right)
margins.setRight(size.width());
else
margins.setLeft(size.width());
QMargins margins((m_d->m_iconEnabled[realLeft] ? leftMargin : 0), 0,
(m_d->m_iconEnabled[realRight] ? rightMargin : 0), 0);
setTextMargins(margins);
}
void FancyLineEdit::iconClicked()
void FancyLineEdit::updateButtonPositions()
{
if (m_d->m_menu) {
m_d->m_menu->exec(QCursor::pos());
} else {
emit buttonClicked();
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;
m_d->m_iconbutton[i]->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
} else {
const int iconoffset = textMargins().left() + 4;
m_d->m_iconbutton[i]->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
}
}
}
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));
}
updateButtonPositions();
}
void FancyLineEdit::setPixmap(const QPixmap &pixmap)
void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
{
m_d->m_iconbutton->setIcon(pixmap);
updateGeometry();
m_d->m_iconbutton[side]->setPixmap(buttonPixmap);
updateMargins();
updateButtonPositions();
update();
}
QPixmap FancyLineEdit::pixmap() const
QPixmap FancyLineEdit::buttonPixmap(Side side) const
{
return m_d->m_pixmap;
return m_d->m_pixmap[side];
}
void FancyLineEdit::setMenu(QMenu *menu)
void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
{
m_d->m_menu = menu;
m_d->m_iconbutton->setIconOpacity(1.0);
m_d->m_menu[side] = buttonMenu;
m_d->m_iconbutton[side]->setIconOpacity(1.0);
}
QMenu *FancyLineEdit::menu() const
QMenu *FancyLineEdit::buttonMenu(Side side) const
{
return m_d->m_menu;
return m_d->m_menu[side];
}
bool FancyLineEdit::hasMenuTabFocusTrigger() const
bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const
{
return m_d->m_menuTabFocusTrigger;
return m_d->m_menuTabFocusTrigger[side];
}
void FancyLineEdit::setMenuTabFocusTrigger(bool v)
void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v)
{
if (m_d->m_menuTabFocusTrigger == v)
if (m_d->m_menuTabFocusTrigger[side] == v)
return;
m_d->m_menuTabFocusTrigger = v;
m_d->m_iconbutton->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
m_d->m_menuTabFocusTrigger[side] = v;
m_d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
}
bool FancyLineEdit::autoHideIcon() const
bool FancyLineEdit::hasAutoHideButton(Side side) const
{
return m_d->m_autoHideIcon;
return m_d->m_iconbutton[side]->hasAutoHide();
}
void FancyLineEdit::setAutoHideIcon(bool h)
void FancyLineEdit::setAutoHideButton(Side side, bool h)
{
m_d->m_autoHideIcon = h;
m_d->m_iconbutton[side]->setAutoHide(h);
if (h)
m_d->m_iconbutton->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
m_d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
else
m_d->m_iconbutton->setIconOpacity(1.0);
m_d->m_iconbutton[side]->setIconOpacity(1.0);
}
void FancyLineEdit::setButtonToolTip(const QString &tip)
void FancyLineEdit::setButtonToolTip(Side side, const QString &tip)
{
m_d->m_iconbutton->setToolTip(tip);
m_d->m_iconbutton[side]->setToolTip(tip);
}
void FancyLineEdit::setButtonFocusPolicy(Qt::FocusPolicy policy)
void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
{
m_d->m_iconbutton->setFocusPolicy(policy);
m_d->m_iconbutton[side]->setFocusPolicy(policy);
}
// IconButton - helper class to represent a clickable icon
IconButton::IconButton(QWidget *parent)
: QAbstractButton(parent)
: QAbstractButton(parent), m_autoHide(false)
{
setCursor(Qt::ArrowCursor);
setFocusPolicy(Qt::NoFocus);
......@@ -270,15 +288,13 @@ void IconButton::paintEvent(QPaintEvent *)
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());
QRect pixmapRect = QRect(0, 0, m_pixmap.width(), m_pixmap.height());
pixmapRect.moveCenter(rect().center());
if (static_cast<FancyLineEdit*>(parentWidget())->autoHideIcon())
if (m_autoHide)
painter.setOpacity(m_iconOpacity);
painter.drawPixmap(pixmapRect, iconpixmap);
painter.drawPixmap(pixmapRect, m_pixmap);
}
void IconButton::animateShow(bool visible)
......
......@@ -44,15 +44,22 @@ class IconButton: public QAbstractButton
{
Q_OBJECT
Q_PROPERTY(float iconOpacity READ iconOpacity WRITE setIconOpacity)
Q_PROPERTY(bool autoHide READ hasAutoHide WRITE setAutoHide)
public:
IconButton(QWidget *parent = 0);
void paintEvent(QPaintEvent *event);
void setPixmap(const QPixmap &pixmap) { m_pixmap = pixmap; update(); }
QPixmap pixmap() const { return m_pixmap; }
float iconOpacity() { return m_iconOpacity; }
void setIconOpacity(float value) { m_iconOpacity = value; update(); }
void animateShow(bool visible);
void setAutoHide(bool hide) { m_autoHide = hide; }
bool hasAutoHide() const { return m_autoHide; }
private:
float m_iconOpacity;
bool m_autoHide;
QPixmap m_pixmap;
};
......@@ -68,50 +75,49 @@ class QTCREATOR_UTILS_EXPORT FancyLineEdit : public QLineEdit
Q_DISABLE_COPY(FancyLineEdit)
Q_OBJECT
Q_ENUMS(Side)
Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap DESIGNABLE true)
Q_PROPERTY(Side side READ side WRITE setSide DESIGNABLE true)
Q_PROPERTY(bool menuTabFocusTrigger READ hasMenuTabFocusTrigger WRITE setMenuTabFocusTrigger DESIGNABLE true)
Q_PROPERTY(bool autoHideIcon READ autoHideIcon WRITE setAutoHideIcon DESIGNABLE true)
public:
enum Side {Left, Right};
enum Side {Left = 0, Right = 1};
explicit FancyLineEdit(QWidget *parent = 0);
~FancyLineEdit();
QPixmap pixmap() const;
QPixmap buttonPixmap(Side side) const;
void setButtonPixmap(Side side, const QPixmap &pixmap);
void setMenu(QMenu *menu);
QMenu *menu() const;
QMenu *buttonMenu(Side side) const;
void setButtonMenu(Side side, QMenu *menu);
void setSide(Side side);
Side side() const;
void setButtonVisible(Side side, bool visible);
bool isButtonVisible(Side side) const;
void setButtonToolTip(const QString &);
void setButtonFocusPolicy(Qt::FocusPolicy policy);
void setButtonToolTip(Side side, const QString &);
void setButtonFocusPolicy(Side side, Qt::FocusPolicy policy);
// Set whether tabbing in will trigger the menu.
bool hasMenuTabFocusTrigger() const;
void setMenuTabFocusTrigger(bool v);
void setMenuTabFocusTrigger(Side side, bool v);
bool hasMenuTabFocusTrigger(Side side) const;
// Set if icon should be hidden when text is empty
bool autoHideIcon() const;
void setAutoHideIcon(bool h);
void setAutoHideButton(Side side, bool h);
bool hasAutoHideButton(Side side) const;
signals:
void buttonClicked();
void buttonClicked(Utils::FancyLineEdit::Side side);
void leftButtonClicked();
void rightButtonClicked();
public slots:
void setPixmap(const QPixmap &pixmap);
void checkButton(const QString &);
private slots:
void checkButtons(const QString &);
void iconClicked();
protected:
virtual void resizeEvent(QResizeEvent *e);
private:
void updateMargins();
void updateButtonPositions();
friend class Utils::FancyLineEditPrivate;
bool isSideStored() const;
FancyLineEditPrivate *m_d;
QString m_oldText;
......
......@@ -35,11 +35,20 @@ FilterLineEdit::FilterLineEdit(QWidget *parent) :
FancyLineEdit(parent),
m_lastFilterText(text())
{
setSide(Utils::FancyLineEdit::Right);
// 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"))));
setButtonPixmap(Right, icon.pixmap(16));
setButtonVisible(Right, true);
setPlaceholderText(tr("Filter"));
setButtonToolTip(tr("Clear text"));
setAutoHideIcon(true);
connect(this, SIGNAL(buttonClicked()), this, SLOT(clear()));
setButtonToolTip(Right, tr("Clear text"));
setAutoHideButton(Right, true);
connect(this, SIGNAL(rightButtonClicked()), this, SLOT(clear()));
connect(this, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged()));
}
......
......@@ -70,11 +70,10 @@ QWidget *CommandMappings::createPage(QWidget *parent)
QWidget *w = new QWidget(parent);
m_page->setupUi(w);
m_page->resetButton->setIcon(QPixmap(Constants::ICON_RESET));
m_page->targetEdit->setSide(Utils::FancyLineEdit::Right);
m_page->targetEdit->setAutoHideIcon(true);
m_page->targetEdit->setAutoHideButton(Utils::FancyLineEdit::Right, true);
m_page->targetEdit->installEventFilter(this);
connect(m_page->targetEdit, SIGNAL(buttonClicked()),
connect(m_page->targetEdit, SIGNAL(buttonClicked(Utils::FancyLineEdit::Side)),
this, SLOT(removeTargetIdentifier()));
connect(m_page->resetButton, SIGNAL(clicked()),
this, SLOT(resetTargetIdentifier()));
......
......@@ -112,7 +112,7 @@
</widget>
</item>
<item>
<widget class="Utils::FancyLineEdit" name="targetEdit"/>
<widget class="Utils::FilterLineEdit" name="targetEdit"/>
</item>
<item>
<widget class="QToolButton" name="resetButton">
......
......@@ -97,9 +97,11 @@ FindToolBar::FindToolBar(FindPlugin *plugin, CurrentDocumentFind *currentDocumen
m_ui.findEdit->setCompleter(m_findCompleter);
m_ui.replaceEdit->setCompleter(m_replaceCompleter);
m_ui.findEdit->setSide(Utils::FancyLineEdit::Right);
QMenu *lineEditMenu = new QMenu(m_ui.findEdit);
m_ui.findEdit->setMenu(lineEditMenu);
m_ui.findEdit->setButtonMenu(Utils::FancyLineEdit::Left, lineEditMenu);
m_ui.findEdit->setButtonVisible(Utils::FancyLineEdit::Left, true);
m_ui.findEdit->setPlaceholderText(QString());
m_ui.replaceEdit->setPlaceholderText(QString());
connect(m_ui.findEdit, SIGNAL(textChanged(const QString&)), this, SLOT(invokeFindIncremental()));
connect(m_ui.findEdit, SIGNAL(returnPressed()), this, SLOT(invokeFindEnter()));
......@@ -503,27 +505,33 @@ void FindToolBar::updateIcons()
bool casesensitive = effectiveFlags & IFindSupport::FindCaseSensitively;
bool wholewords = effectiveFlags & IFindSupport::FindWholeWords;
bool regexp = effectiveFlags & IFindSupport::FindRegularExpression;
QPixmap pixmap(17, 17);
int width = 0;
if (casesensitive) width += 6;
if (wholewords) width += 6;
if (regexp) width += 6;
if (width == 0) width = 18;
--width;
QPixmap pixmap(width, 17);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
int x = 16;
int x = 0;
if (casesensitive) {
painter.drawPixmap(x - 10, 0, m_casesensitiveIcon);
x -= 6;
painter.drawPixmap(x - 6, 0, m_casesensitiveIcon);
x += 6;
}
if (wholewords) {
painter.drawPixmap(x - 10, 0, m_wholewordsIcon);
x -= 6;
painter.drawPixmap(x - 6, 0, m_wholewordsIcon);
x += 6;
}
if (regexp) {
painter.drawPixmap(x - 10, 0, m_regexpIcon);
painter.drawPixmap(x - 6, 0, m_regexpIcon);
}
if (!casesensitive && !wholewords && !regexp) {
QPixmap mag(Core::Constants::ICON_MAGNIFIER);
painter.drawPixmap(0, (pixmap.height() - mag.height()) / 2, mag);
}
m_ui.findEdit->setPixmap(pixmap);
m_ui.findEdit->setButtonPixmap(Utils::FancyLineEdit::Left, pixmap);
}
IFindSupport::FindFlags FindToolBar::effectiveFindFlags()
......
......@@ -40,7 +40,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="Utils::FancyLineEdit" name="findEdit"/>
<widget class="Utils::FilterLineEdit" name="findEdit"/>
</item>
<item row="0" column="2">
<layout class="QHBoxLayout" name="horizontalLayout_2">
......@@ -100,7 +100,7 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="replaceEdit"/>
<widget class="Utils::FilterLineEdit" name="replaceEdit"/>
</item>
<item row="1" column="2">
<layout class="QHBoxLayout" name="horizontalLayout">
......@@ -162,9 +162,9 @@
</widget>
<customwidgets>
<customwidget>
<class>Utils::FancyLineEdit</class>
<class>Utils::FilterLineEdit</class>
<extends>QLineEdit</extends>
<header location="global">utils/fancylineedit.h</header>
<header location="global">utils/filterlineedit.h</header>
</customwidget>
</customwidgets>
<tabstops>
......
......@@ -46,7 +46,7 @@ QT_END_NAMESPACE
#include <coreplugin/modemanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/fileiconprovider.h>
#include <utils/fancylineedit.h>
#include <utils/filterlineedit.h>
#include <utils/qtcassert.h>
#include <QtCore/QFileInfo>
......@@ -266,7 +266,7 @@ LocatorWidget::LocatorWidget(LocatorPlugin *qop) :
m_filterMenu(new QMenu(this)),
m_refreshAction(new QAction(tr("Refresh"), this)),
m_configureAction(new QAction(tr("Configure..."), this)),
m_fileLineEdit(new Utils::FancyLineEdit)
</