Commit 894fd9d7 authored by Jens Bache-Wiig's avatar Jens Bache-Wiig
Browse files

Improved fancylineedit and consistency

- fixed some RTL issues. I removed isSideStored
and useLayoutDirection. Instead we simply flip the
sides if RTL is in use.
- added the fader animation and clear icon that we
use in Designer already.
- fixed the double toplevel popup focus issues we
had with the locator menu.
- removed some redundant labels and reset buttons
parent 7d023ef1
......@@ -36,31 +36,20 @@
#include <QtGui/QMenu>
#include <QtGui/QMouseEvent>
#include <QtGui/QLabel>
#include <QtGui/QAbstractButton>
#include <QtGui/QPainter>
#include <QtGui/QStyle>
#include <QtCore/QPropertyAnimation>
enum { margin = 6 };
namespace Utils {
static inline QString sideToStyleSheetString(FancyLineEdit::Side side)
{
return side == FancyLineEdit::Left ? QLatin1String("left") : QLatin1String("right");
}
enum { margin = 6 };
// Format style sheet for the label containing the pixmap. It has a margin on
// the outer side of the whole FancyLineEdit.
static QString labelStyleSheet(FancyLineEdit::Side side)
{
QString rc = QLatin1String("QLabel { margin-");
rc += sideToStyleSheetString(side);
rc += QLatin1String(": ");
rc += QString::number(margin);
rc += QLatin1Char('}');
return rc;
}
#define ICONBUTTON_SIZE 18
#define FADE_TIME 160
// --------- FancyLineEditPrivate as QObject with label
// event filter
namespace Utils {
// --------- FancyLineEditPrivate
class FancyLineEditPrivate : public QObject {
public:
explicit FancyLineEditPrivate(FancyLineEdit *parent);
......@@ -73,45 +62,35 @@ public:
FancyLineEdit *m_lineEdit;
QPixmap m_pixmap;
QMenu *m_menu;
QLabel *m_menuLabel;
FancyLineEdit::Side m_side;
bool m_useLayoutDirection;
bool m_menuTabFocusTrigger;
bool m_autoHideIcon;
IconButton *m_iconbutton;
};
FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) :
QObject(parent),
m_leftLabelStyleSheet(labelStyleSheet(FancyLineEdit::Left)),
m_rightLabelStyleSheet(labelStyleSheet(FancyLineEdit::Right)),
m_lineEdit(parent),
m_menu(0),
m_menuLabel(0),
m_side(FancyLineEdit::Left),
m_useLayoutDirection(false),
m_menuTabFocusTrigger(false)
m_menuTabFocusTrigger(false),
m_autoHideIcon(false),
m_iconbutton(new IconButton(parent))
{
}
bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
{
if (obj != m_menuLabel)
if (obj != m_iconbutton)
return QObject::eventFilter(obj, event);
switch (event->type()) {
case QEvent::MouseButtonPress: {
const QMouseEvent *me = static_cast<QMouseEvent *>(event);
if (m_menu) {
m_menu->exec(me->globalPos());
} else {
emit m_lineEdit->buttonClicked();
}
return true;
}
case QEvent::FocusIn:
if (m_menuTabFocusTrigger && m_menu) {
m_lineEdit->setFocus();
m_menu->exec(m_menuLabel->mapToGlobal(m_menuLabel->rect().center()));
m_menu->exec(m_iconbutton->mapToGlobal(m_iconbutton->rect().center()));
return true;
}
default:
......@@ -120,89 +99,105 @@ bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
return QObject::eventFilter(obj, event);
}
// --------- FancyLineEdit
FancyLineEdit::FancyLineEdit(QWidget *parent) :
QLineEdit(parent),
m_d(new FancyLineEditPrivate(this))
{
m_d->m_menuLabel = new QLabel(this);
m_d->m_menuLabel->installEventFilter(m_d);
updateMenuLabel();
// 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("clear.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()));
}
FancyLineEdit::~FancyLineEdit()
void FancyLineEdit::checkButton(const QString &text)
{
}
// Position the menu label left or right according to size.
// Called when switching side and from resizeEvent.
void FancyLineEdit::positionMenuLabel()
{
switch (side()) {
case Left:
m_d->m_menuLabel->setGeometry(0, 0, m_d->m_pixmap.width()+margin, height());
break;
case Right:
m_d->m_menuLabel->setGeometry(width() - m_d->m_pixmap.width() - margin, 0,
m_d->m_pixmap.width()+margin, height());
break;
if (autoHideIcon()) {
static QString oldtext;
if (oldtext.isEmpty() || text.isEmpty())
m_d->m_iconbutton->animateShow(!text.isEmpty());
oldtext = text;
}
}
void FancyLineEdit::updateStyleSheet(Side side)
FancyLineEdit::~FancyLineEdit()
{
// Udate the LineEdit style sheet. Make room for the label on the
// respective side and set color according to whether we are showing the
// hint text
QString sheet = QLatin1String("QLineEdit{ padding-");
sheet += sideToStyleSheetString(side);
sheet += QLatin1String(": ");
sheet += QString::number(m_d->m_pixmap.width() + margin);
sheet += QLatin1Char(';');
sheet += QLatin1Char('}');
setStyleSheet(sheet);
}
void FancyLineEdit::updateMenuLabel()
void FancyLineEdit::setSide(Side side)
{
m_d->m_menuLabel->setPixmap(m_d->m_pixmap);
const Side s = side();
switch (s) {
case Left:
m_d->m_menuLabel->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
m_d->m_menuLabel->setStyleSheet(m_d->m_leftLabelStyleSheet);
break;
case Right:
m_d->m_menuLabel->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
m_d->m_menuLabel->setStyleSheet(m_d->m_rightLabelStyleSheet);
break;
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));
}
updateStyleSheet(s);
positionMenuLabel();
QMargins margins;
if (iconpos == Right)
margins.setRight(size.width());
else
margins.setLeft(size.width());
setTextMargins(margins);
}
void FancyLineEdit::setSide(Side side)
void FancyLineEdit::iconClicked()
{
m_d->m_side = side;
updateMenuLabel();
if (m_d->m_menu) {
m_d->m_menu->exec(QCursor::pos());
} else {
emit buttonClicked();
}
}
FancyLineEdit::Side FancyLineEdit::side() const
{
if (m_d->m_useLayoutDirection)
return qApp->layoutDirection() == Qt::LeftToRight ? Left : Right;
return m_d->m_side;
}
void FancyLineEdit::resizeEvent(QResizeEvent *)
{
positionMenuLabel();
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() + 8;
m_d->m_iconbutton->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
} else {
const int iconoffset = textMargins().left() + 8;
m_d->m_iconbutton->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
}
}
void FancyLineEdit::setPixmap(const QPixmap &pixmap)
{
m_d->m_pixmap = pixmap;
updateMenuLabel();
m_d->m_iconbutton->setIcon(pixmap);
updateGeometry();
}
QPixmap FancyLineEdit::pixmap() const
......@@ -213,40 +208,93 @@ QPixmap FancyLineEdit::pixmap() const
void FancyLineEdit::setMenu(QMenu *menu)
{
m_d->m_menu = menu;
}
m_d->m_iconbutton->setIconOpacity(1.0);
}
QMenu *FancyLineEdit::menu() const
{
return m_d->m_menu;
}
bool FancyLineEdit::useLayoutDirection() const
bool FancyLineEdit::hasMenuTabFocusTrigger() const
{
return m_d->m_useLayoutDirection;
return m_d->m_menuTabFocusTrigger;
}
void FancyLineEdit::setUseLayoutDirection(bool v)
void FancyLineEdit::setMenuTabFocusTrigger(bool v)
{
m_d->m_useLayoutDirection = v;
if (m_d->m_menuTabFocusTrigger == v)
return;
m_d->m_menuTabFocusTrigger = v;
m_d->m_iconbutton->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
}
bool FancyLineEdit::isSideStored() const
bool FancyLineEdit::autoHideIcon() const
{
return !m_d->m_useLayoutDirection;
return m_d->m_autoHideIcon;
}
bool FancyLineEdit::hasMenuTabFocusTrigger() const
void FancyLineEdit::setAutoHideIcon(bool h)
{
return m_d->m_menuTabFocusTrigger;
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);
}
void FancyLineEdit::setMenuTabFocusTrigger(bool v)
void FancyLineEdit::setButtonToolTip(const QString &tip)
{
if (m_d->m_menuTabFocusTrigger == v)
return;
m_d->m_iconbutton->setToolTip(tip);
}
m_d->m_menuTabFocusTrigger = v;
m_d->m_menuLabel->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
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().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);
}
}
} // namespace Utils
......@@ -33,11 +33,29 @@
#include "utils_global.h"
#include <QtGui/QLineEdit>
#include <QtGui/QPaintEvent>
#include <QtGui/QAbstractButton>
namespace Utils {
class FancyLineEditPrivate;
class IconButton: public QAbstractButton
{
Q_OBJECT
Q_PROPERTY(float iconOpacity READ iconOpacity WRITE setIconOpacity)
public:
IconButton(QWidget *parent = 0);
void paintEvent(QPaintEvent *event);
float iconOpacity() { return m_iconOpacity; }
void setIconOpacity(float value) { m_iconOpacity = value; update(); }
void animateShow(bool visible);
private:
float m_iconOpacity;
};
/* 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
......@@ -51,9 +69,9 @@ class QTCREATOR_UTILS_EXPORT FancyLineEdit : public QLineEdit
Q_OBJECT
Q_ENUMS(Side)
Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap DESIGNABLE true)
Q_PROPERTY(Side side READ side WRITE setSide DESIGNABLE isSideStored STORED isSideStored)
Q_PROPERTY(bool useLayoutDirection READ useLayoutDirection WRITE setUseLayoutDirection 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};
......@@ -69,18 +87,24 @@ public:
void setSide(Side side);
Side side() const;
bool useLayoutDirection() const;
void setUseLayoutDirection(bool v);
void setButtonToolTip(const QString &);
void setButtonFocusPolicy(Qt::FocusPolicy policy);
// Set whether tabbing in will trigger the menu.
bool hasMenuTabFocusTrigger() const;
void setMenuTabFocusTrigger(bool v);
// Set if icon should be hidden when text is empty
bool autoHideIcon() const;
void setAutoHideIcon(bool h);
signals:
void buttonClicked();
public slots:
void setPixmap(const QPixmap &pixmap);
void checkButton(const QString &);
void iconClicked();
protected:
virtual void resizeEvent(QResizeEvent *e);
......@@ -88,9 +112,6 @@ protected:
private:
friend class Utils::FancyLineEditPrivate;
bool isSideStored() const;
void updateMenuLabel();
void positionMenuLabel();
void updateStyleSheet(Side side);
FancyLineEditPrivate *m_d;
};
......
......@@ -36,9 +36,9 @@ FilterLineEdit::FilterLineEdit(QWidget *parent) :
m_lastFilterText(text())
{
setSide(Utils::FancyLineEdit::Right);
setPixmap(QPixmap(QLatin1String(":/core/images/reset.png")));
setPlaceholderText(tr("Type to filter"));
setPlaceholderText(tr("Filter"));
setButtonToolTip(tr("Clear text"));
setAutoHideIcon(true);
connect(this, SIGNAL(buttonClicked()), this, SLOT(clear()));
connect(this, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged()));
}
......@@ -52,4 +52,5 @@ void FilterLineEdit::slotTextChanged()
}
}
} // namespace Utils
......@@ -69,14 +69,14 @@ QWidget *CommandMappings::createPage(QWidget *parent)
m_page = new Ui_CommandMappings();
QWidget *w = new QWidget(parent);
m_page->setupUi(w);
m_page->targetEdit->setPixmap(QPixmap(Constants::ICON_RESET));
m_page->resetButton->setIcon(QPixmap(Constants::ICON_RESET));
m_page->targetEdit->setSide(Utils::FancyLineEdit::Right);
m_page->targetEdit->installEventFilter(this);
connect(m_page->targetEdit, SIGNAL(buttonClicked()),
this, SLOT(resetTargetIdentifier()));
connect(m_page->removeButton, SIGNAL(clicked()),
this, SLOT(removeTargetIdentifier()));
connect(m_page->resetButton, SIGNAL(clicked()),
this, SLOT(resetTargetIdentifier()));
connect(m_page->exportButton, SIGNAL(clicked()),
this, SLOT(exportAction()));
connect(m_page->importButton, SIGNAL(clicked()),
......
......@@ -19,13 +19,6 @@
<layout class="QVBoxLayout">
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="filterLabel">
<property name="text">
<string>Filter:</string>
</property>
</widget>
</item>
<item>
<widget class="Utils::FilterLineEdit" name="filterEdit"/>
</item>
......@@ -119,19 +112,31 @@
</widget>
</item>
<item>
<widget class="Utils::FancyLineEdit" name="targetEdit"/>
<widget class="Utils::FilterLineEdit" name="targetEdit"/>
</item>
<item>
<widget class="QToolButton" name="removeButton">
<widget class="QToolButton" name="resetButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>27</width>
<height>27</height>
</size>
</property>
<property name="toolTip">
<string>Remove</string>
<string>Reset</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../core.qrc">
<normaloff>:/core/images/clear.png</normaloff>:/core/images/clear.png</iconset>
<normaloff>:/core/images/reset.png</normaloff>:/core/images/reset.png</iconset>
</property>
<property name="iconSize">
<size>
......@@ -161,11 +166,6 @@
<extends>QLineEdit</extends>
<header location="global">utils/filterlineedit.h</header>
</customwidget>
<customwidget>
<class>Utils::FancyLineEdit</class>
<extends>QLineEdit</extends>
<header location="global">utils/fancylineedit.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../core.qrc"/>
......
......@@ -151,8 +151,6 @@ QWidget *GeneralSettings::createPage(QWidget *parent)
this, SLOT(resetInterfaceColor()));
connect(m_page->resetEditorButton, SIGNAL(clicked()),
this, SLOT(resetExternalEditor()));
connect(m_page->resetLanguageButton, SIGNAL(clicked()),
this, SLOT(resetLanguage()));
connect(m_page->helpExternalEditorButton, SIGNAL(clicked()),
this, SLOT(showHelpForExternalEditor()));
#ifdef Q_OS_UNIX
......
......@@ -89,20 +89,6 @@
<item>
<widget class="QComboBox" name="languageBox"/>
</item>
<item>
<widget class="QToolButton" name="resetLanguageButton">
<property name="toolTip">
<string>Reset to default</string>
</property>
<property name="text">
<string>R</string>
</property>
<property name="icon">
<iconset resource="core.qrc">
<normaloff>:/core/images/reset.png</normaloff>:/core/images/reset.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
......@@ -296,7 +282,6 @@
<tabstop>colorButton</tabstop>
<tabstop>resetButton</tabstop>
<tabstop>languageBox</tabstop>
<tabstop>resetLanguageButton</tabstop>
<tabstop>terminalEdit</tabstop>
<tabstop>resetTerminalButton</tabstop>
<tabstop>externalEditorEdit</tabstop>
......
......@@ -96,7 +96,7 @@ FindToolBar::FindToolBar(FindPlugin *plugin, CurrentDocumentFind *currentDocumen
m_ui.findEdit->setCompleter(m_findCompleter);
m_ui.replaceEdit->setCompleter(m_replaceCompleter);
m_ui.findEdit->setSide(qApp->layoutDirection() == Qt::LeftToRight ? Utils::FancyLineEdit::Right : Utils::FancyLineEdit::Left);
m_ui.findEdit->setSide(Utils::FancyLineEdit::Right);
QMenu *lineEditMenu = new QMenu(m_ui.findEdit);
m_ui.findEdit->setMenu(lineEditMenu);
......
......@@ -288,9 +288,11 @@ LocatorWidget::LocatorWidget(LocatorPlugin *qop) :
setWindowIcon(QIcon(":/locator/images/locator.png"));
QPixmap image(Core::Constants::ICON_MAGNIFIER);
m_fileLineEdit->setPixmap(image);
m_fileLineEdit->setUseLayoutDirection(true);
m_fileLineEdit->setPlaceholderText(tr("Type to locate"));
m_fileLineEdit->setButtonToolTip(tr("Options"));
m_fileLineEdit->setFocusPolicy(Qt::ClickFocus);
// We set click focus since otherwise you will always get two popups
m_fileLineEdit->setButtonFocusPolicy(Qt::ClickFocus);
m_fileLineEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
m_fileLineEdit->installEventFilter(this);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment