Set svn:eol-style.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@38958 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Vincent van Ravesteijn 2011-06-05 17:52:15 +00:00
parent cc81d48b18
commit 1744b6f9ed
2 changed files with 416 additions and 416 deletions

View File

@ -1,305 +1,305 @@
/** /**
* \file fancylineedit.cpp * \file fancylineedit.cpp
* This file is part of LyX, the document processor. * This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING. * Licence details can be found in the file COPYING.
* *
* \author Nokia Corporation (qt-info@nokia.com) * \author Nokia Corporation (qt-info@nokia.com)
* *
* Full author contact details are available in file CREDITS. * Full author contact details are available in file CREDITS.
* *
*/ */
// Code taken from the Qt Creator project and customized a little // Code taken from the Qt Creator project and customized a little
#include "FancyLineEdit.h" #include "FancyLineEdit.h"
#include <QtCore/QEvent> #include <QtCore/QEvent>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QString> #include <QtCore/QString>
#include <QtCore/QPropertyAnimation> #include <QtCore/QPropertyAnimation>
#include <QtGui/QApplication> #include <QtGui/QApplication>
#include <QtGui/QMenu> #include <QtGui/QMenu>
#include <QtGui/QMouseEvent> #include <QtGui/QMouseEvent>
#include <QtGui/QLabel> #include <QtGui/QLabel>
#include <QtGui/QAbstractButton> #include <QtGui/QAbstractButton>
#include <QtGui/QPainter> #include <QtGui/QPainter>
#include <QtGui/QStyle> #include <QtGui/QStyle>
#include <QtGui/QPaintEvent> #include <QtGui/QPaintEvent>
enum { margin = 6 }; enum { margin = 6 };
#define ICONBUTTON_HEIGHT 18 #define ICONBUTTON_HEIGHT 18
#define FADE_TIME 160 #define FADE_TIME 160
namespace lyx { namespace lyx {
namespace frontend { namespace frontend {
// --------- FancyLineEditPrivate // --------- FancyLineEditPrivate
class FancyLineEditPrivate : public QObject { class FancyLineEditPrivate : public QObject {
public: public:
explicit FancyLineEditPrivate(FancyLineEdit *parent); explicit FancyLineEditPrivate(FancyLineEdit *parent);
virtual bool eventFilter(QObject *obj, QEvent *event); virtual bool eventFilter(QObject *obj, QEvent *event);
FancyLineEdit *m_lineEdit; FancyLineEdit *m_lineEdit;
QPixmap m_pixmap[2]; QPixmap m_pixmap[2];
QMenu *m_menu[2]; QMenu *m_menu[2];
bool m_menuTabFocusTrigger[2]; bool m_menuTabFocusTrigger[2];
IconButton *m_iconbutton[2]; IconButton *m_iconbutton[2];
bool m_iconEnabled[2]; bool m_iconEnabled[2];
}; };
FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) : FancyLineEditPrivate::FancyLineEditPrivate(FancyLineEdit *parent) :
QObject(parent), QObject(parent),
m_lineEdit(parent) m_lineEdit(parent)
{ {
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
m_menu[i] = 0; m_menu[i] = 0;
m_menuTabFocusTrigger[i] = false; m_menuTabFocusTrigger[i] = false;
m_iconbutton[i] = new IconButton(parent); m_iconbutton[i] = new IconButton(parent);
m_iconbutton[i]->installEventFilter(this); m_iconbutton[i]->installEventFilter(this);
m_iconbutton[i]->hide(); m_iconbutton[i]->hide();
m_iconbutton[i]->setAutoHide(false); m_iconbutton[i]->setAutoHide(false);
m_iconEnabled[i] = false; m_iconEnabled[i] = false;
} }
} }
bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event) bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
{ {
int buttonIndex = -1; int buttonIndex = -1;
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
if (obj == m_iconbutton[i]) { if (obj == m_iconbutton[i]) {
buttonIndex = i; buttonIndex = i;
break; break;
} }
} }
if (buttonIndex == -1) if (buttonIndex == -1)
return QObject::eventFilter(obj, event); return QObject::eventFilter(obj, event);
switch (event->type()) { switch (event->type()) {
case QEvent::FocusIn: case QEvent::FocusIn:
if (m_menuTabFocusTrigger[buttonIndex] && m_menu[buttonIndex]) { if (m_menuTabFocusTrigger[buttonIndex] && m_menu[buttonIndex]) {
m_lineEdit->setFocus(); m_lineEdit->setFocus();
m_menu[buttonIndex]->exec(m_iconbutton[buttonIndex]->mapToGlobal( m_menu[buttonIndex]->exec(m_iconbutton[buttonIndex]->mapToGlobal(
m_iconbutton[buttonIndex]->rect().center())); m_iconbutton[buttonIndex]->rect().center()));
return true; return true;
} }
default: default:
break; break;
} }
return QObject::eventFilter(obj, event); return QObject::eventFilter(obj, event);
} }
// --------- FancyLineEdit // --------- FancyLineEdit
FancyLineEdit::FancyLineEdit(QWidget *parent) : FancyLineEdit::FancyLineEdit(QWidget *parent) :
QLineEdit(parent), QLineEdit(parent),
m_d(new FancyLineEditPrivate(this)) m_d(new FancyLineEditPrivate(this))
{ {
ensurePolished(); ensurePolished();
updateMargins(); updateMargins();
connect(this, SIGNAL(textChanged(QString)), this, SLOT(checkButtons(QString))); 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[Left], SIGNAL(clicked()), this, SLOT(iconClicked()));
connect(m_d->m_iconbutton[Right], SIGNAL(clicked()), this, SLOT(iconClicked())); connect(m_d->m_iconbutton[Right], SIGNAL(clicked()), this, SLOT(iconClicked()));
} }
void FancyLineEdit::checkButtons(const QString &text) void FancyLineEdit::checkButtons(const QString &text)
{ {
if (m_oldText.isEmpty() || text.isEmpty()) { if (m_oldText.isEmpty() || text.isEmpty()) {
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
if (m_d->m_iconbutton[i]->hasAutoHide()) if (m_d->m_iconbutton[i]->hasAutoHide())
m_d->m_iconbutton[i]->animateShow(!text.isEmpty()); m_d->m_iconbutton[i]->animateShow(!text.isEmpty());
} }
m_oldText = text; m_oldText = text;
} }
} }
FancyLineEdit::~FancyLineEdit() FancyLineEdit::~FancyLineEdit()
{ {
} }
void FancyLineEdit::setButtonVisible(Side side, bool visible) void FancyLineEdit::setButtonVisible(Side side, bool visible)
{ {
m_d->m_iconbutton[side]->setVisible(visible); m_d->m_iconbutton[side]->setVisible(visible);
m_d->m_iconEnabled[side] = visible; m_d->m_iconEnabled[side] = visible;
updateMargins(); updateMargins();
} }
bool FancyLineEdit::isButtonVisible(Side side) const bool FancyLineEdit::isButtonVisible(Side side) const
{ {
return m_d->m_iconEnabled[side]; return m_d->m_iconEnabled[side];
} }
void FancyLineEdit::iconClicked() void FancyLineEdit::iconClicked()
{ {
IconButton *button = qobject_cast<IconButton *>(sender()); IconButton *button = qobject_cast<IconButton *>(sender());
int index = -1; int index = -1;
for (int i = 0; i < 2; ++i) for (int i = 0; i < 2; ++i)
if (m_d->m_iconbutton[i] == button) if (m_d->m_iconbutton[i] == button)
index = i; index = i;
if (index == -1) if (index == -1)
return; return;
if (m_d->m_menu[index]) { if (m_d->m_menu[index]) {
m_d->m_menu[index]->exec(QCursor::pos()); m_d->m_menu[index]->exec(QCursor::pos());
} else { } else {
buttonClicked((Side)index); buttonClicked((Side)index);
if (index == Left) if (index == Left)
leftButtonClicked(); leftButtonClicked();
else if (index == Right) else if (index == Right)
rightButtonClicked(); rightButtonClicked();
} }
} }
void FancyLineEdit::updateMargins() void FancyLineEdit::updateMargins()
{ {
bool leftToRight = (layoutDirection() == Qt::LeftToRight); bool leftToRight = (layoutDirection() == Qt::LeftToRight);
Side realLeft = (leftToRight ? Left : Right); Side realLeft = (leftToRight ? Left : Right);
Side realRight = (leftToRight ? Right : Left); Side realRight = (leftToRight ? Right : Left);
int leftMargin = m_d->m_iconbutton[realLeft]->pixmap().width() + 8; int leftMargin = m_d->m_iconbutton[realLeft]->pixmap().width() + 8;
int rightMargin = m_d->m_iconbutton[realRight]->pixmap().width() + 8; int rightMargin = m_d->m_iconbutton[realRight]->pixmap().width() + 8;
// Note KDE does not reserve space for the highlight color // Note KDE does not reserve space for the highlight color
if (style()->inherits("OxygenStyle")) { if (style()->inherits("OxygenStyle")) {
leftMargin = qMax(24, leftMargin); leftMargin = qMax(24, leftMargin);
rightMargin = qMax(24, rightMargin); rightMargin = qMax(24, rightMargin);
} }
QMargins margins((m_d->m_iconEnabled[realLeft] ? leftMargin : 0), 0, QMargins margins((m_d->m_iconEnabled[realLeft] ? leftMargin : 0), 0,
(m_d->m_iconEnabled[realRight] ? rightMargin : 0), 0); (m_d->m_iconEnabled[realRight] ? rightMargin : 0), 0);
setTextMargins(margins); setTextMargins(margins);
} }
void FancyLineEdit::updateButtonPositions() void FancyLineEdit::updateButtonPositions()
{ {
QRect contentRect = rect(); QRect contentRect = rect();
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
Side iconpos = (Side)i; Side iconpos = (Side)i;
if (layoutDirection() == Qt::RightToLeft) if (layoutDirection() == Qt::RightToLeft)
iconpos = (iconpos == Left ? Right : Left); iconpos = (iconpos == Left ? Right : Left);
if (iconpos == FancyLineEdit::Right) { if (iconpos == FancyLineEdit::Right) {
const int iconoffset = textMargins().right() + 4; const int iconoffset = textMargins().right() + 4;
m_d->m_iconbutton[i]->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0)); m_d->m_iconbutton[i]->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
} else { } else {
const int iconoffset = textMargins().left() + 4; const int iconoffset = textMargins().left() + 4;
m_d->m_iconbutton[i]->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0)); m_d->m_iconbutton[i]->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
} }
} }
} }
void FancyLineEdit::resizeEvent(QResizeEvent *) void FancyLineEdit::resizeEvent(QResizeEvent *)
{ {
updateButtonPositions(); updateButtonPositions();
} }
void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap) void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
{ {
m_d->m_iconbutton[side]->setPixmap(buttonPixmap); m_d->m_iconbutton[side]->setPixmap(buttonPixmap);
updateMargins(); updateMargins();
updateButtonPositions(); updateButtonPositions();
update(); update();
} }
QPixmap FancyLineEdit::buttonPixmap(Side side) const QPixmap FancyLineEdit::buttonPixmap(Side side) const
{ {
return m_d->m_pixmap[side]; return m_d->m_pixmap[side];
} }
void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu) void FancyLineEdit::setButtonMenu(Side side, QMenu *buttonMenu)
{ {
m_d->m_menu[side] = buttonMenu; m_d->m_menu[side] = buttonMenu;
m_d->m_iconbutton[side]->setIconOpacity(1.0); m_d->m_iconbutton[side]->setIconOpacity(1.0);
} }
QMenu *FancyLineEdit::buttonMenu(Side side) const QMenu *FancyLineEdit::buttonMenu(Side side) const
{ {
return m_d->m_menu[side]; return m_d->m_menu[side];
} }
bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const bool FancyLineEdit::hasMenuTabFocusTrigger(Side side) const
{ {
return m_d->m_menuTabFocusTrigger[side]; return m_d->m_menuTabFocusTrigger[side];
} }
void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v) void FancyLineEdit::setMenuTabFocusTrigger(Side side, bool v)
{ {
if (m_d->m_menuTabFocusTrigger[side] == v) if (m_d->m_menuTabFocusTrigger[side] == v)
return; return;
m_d->m_menuTabFocusTrigger[side] = v; m_d->m_menuTabFocusTrigger[side] = v;
m_d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus); m_d->m_iconbutton[side]->setFocusPolicy(v ? Qt::TabFocus : Qt::NoFocus);
} }
bool FancyLineEdit::hasAutoHideButton(Side side) const bool FancyLineEdit::hasAutoHideButton(Side side) const
{ {
return m_d->m_iconbutton[side]->hasAutoHide(); return m_d->m_iconbutton[side]->hasAutoHide();
} }
void FancyLineEdit::setAutoHideButton(Side side, bool h) void FancyLineEdit::setAutoHideButton(Side side, bool h)
{ {
m_d->m_iconbutton[side]->setAutoHide(h); m_d->m_iconbutton[side]->setAutoHide(h);
if (h) if (h)
m_d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0); m_d->m_iconbutton[side]->setIconOpacity(text().isEmpty() ? 0.0 : 1.0);
else else
m_d->m_iconbutton[side]->setIconOpacity(1.0); m_d->m_iconbutton[side]->setIconOpacity(1.0);
} }
void FancyLineEdit::setButtonToolTip(Side side, const QString &tip) void FancyLineEdit::setButtonToolTip(Side side, const QString &tip)
{ {
m_d->m_iconbutton[side]->setToolTip(tip); m_d->m_iconbutton[side]->setToolTip(tip);
} }
void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy) void FancyLineEdit::setButtonFocusPolicy(Side side, Qt::FocusPolicy policy)
{ {
m_d->m_iconbutton[side]->setFocusPolicy(policy); m_d->m_iconbutton[side]->setFocusPolicy(policy);
} }
// IconButton - helper class to represent a clickable icon // IconButton - helper class to represent a clickable icon
IconButton::IconButton(QWidget *parent) IconButton::IconButton(QWidget *parent)
: QAbstractButton(parent), m_autoHide(false) : QAbstractButton(parent), m_autoHide(false)
{ {
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
setFocusPolicy(Qt::NoFocus); setFocusPolicy(Qt::NoFocus);
} }
void IconButton::paintEvent(QPaintEvent *) void IconButton::paintEvent(QPaintEvent *)
{ {
QPainter painter(this); QPainter painter(this);
// Note isDown should really use the active state but in most styles // Note isDown should really use the active state but in most styles
// this has no proper feedback // this has no proper feedback
QIcon::Mode state = QIcon::Disabled; QIcon::Mode state = QIcon::Disabled;
if (isEnabled()) if (isEnabled())
state = isDown() ? QIcon::Selected : QIcon::Normal; state = isDown() ? QIcon::Selected : QIcon::Normal;
QRect pixmapRect = QRect(0, 0, m_pixmap.width(), m_pixmap.height()); QRect pixmapRect = QRect(0, 0, m_pixmap.width(), m_pixmap.height());
pixmapRect.moveCenter(rect().center()); pixmapRect.moveCenter(rect().center());
if (m_autoHide) if (m_autoHide)
painter.setOpacity(m_iconOpacity); painter.setOpacity(m_iconOpacity);
painter.drawPixmap(pixmapRect, m_pixmap); painter.drawPixmap(pixmapRect, m_pixmap);
} }
void IconButton::animateShow(bool visible) void IconButton::animateShow(bool visible)
{ {
if (visible) { if (visible) {
QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity"); QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity");
animation->setDuration(FADE_TIME); animation->setDuration(FADE_TIME);
animation->setEndValue(1.0); animation->setEndValue(1.0);
animation->start(QAbstractAnimation::DeleteWhenStopped); animation->start(QAbstractAnimation::DeleteWhenStopped);
} else { } else {
QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity"); QPropertyAnimation *animation = new QPropertyAnimation(this, "iconOpacity");
animation->setDuration(FADE_TIME); animation->setDuration(FADE_TIME);
animation->setEndValue(0.0); animation->setEndValue(0.0);
animation->start(QAbstractAnimation::DeleteWhenStopped); animation->start(QAbstractAnimation::DeleteWhenStopped);
} }
} }
} }
} }
#include "moc_FancyLineEdit.cpp" #include "moc_FancyLineEdit.cpp"

View File

@ -1,111 +1,111 @@
/** /**
* \file fancylineedit.h * \file fancylineedit.h
* This file is part of LyX, the document processor. * This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING. * Licence details can be found in the file COPYING.
* *
* \author Nokia Corporation (qt-info@nokia.com) * \author Nokia Corporation (qt-info@nokia.com)
* *
* Full author contact details are available in file CREDITS. * Full author contact details are available in file CREDITS.
* *
*/ */
// Code taken from the Qt Creator project and customized a little // Code taken from the Qt Creator project and customized a little
#ifndef FANCYLINEEDIT_H #ifndef FANCYLINEEDIT_H
#define FANCYLINEEDIT_H #define FANCYLINEEDIT_H
#include <QtGui/QLineEdit> #include <QtGui/QLineEdit>
#include <QtGui/QAbstractButton> #include <QtGui/QAbstractButton>
namespace lyx { namespace lyx {
namespace frontend { namespace frontend {
class FancyLineEditPrivate; class FancyLineEditPrivate;
class IconButton: public QAbstractButton class IconButton: public QAbstractButton
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(float iconOpacity READ iconOpacity WRITE setIconOpacity) Q_PROPERTY(float iconOpacity READ iconOpacity WRITE setIconOpacity)
Q_PROPERTY(bool autoHide READ hasAutoHide WRITE setAutoHide) Q_PROPERTY(bool autoHide READ hasAutoHide WRITE setAutoHide)
Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap) Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap)
public: public:
explicit IconButton(QWidget *parent = 0); explicit IconButton(QWidget *parent = 0);
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
void setPixmap(const QPixmap &pixmap) { m_pixmap = pixmap; update(); } void setPixmap(const QPixmap &pixmap) { m_pixmap = pixmap; update(); }
QPixmap pixmap() const { return m_pixmap; } QPixmap pixmap() const { return m_pixmap; }
float iconOpacity() { return m_iconOpacity; } float iconOpacity() { return m_iconOpacity; }
void setIconOpacity(float value) { m_iconOpacity = value; update(); } void setIconOpacity(float value) { m_iconOpacity = value; update(); }
void animateShow(bool visible); void animateShow(bool visible);
void setAutoHide(bool hide) { m_autoHide = hide; } void setAutoHide(bool hide) { m_autoHide = hide; }
bool hasAutoHide() const { return m_autoHide; } bool hasAutoHide() const { return m_autoHide; }
private: private:
float m_iconOpacity; float m_iconOpacity;
bool m_autoHide; bool m_autoHide;
QPixmap m_pixmap; QPixmap m_pixmap;
}; };
/* A line edit with an embedded pixmap on one side that is connected to /* 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") * 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 * 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 * 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 * text if isShowingHintText() returns true (that is, does not contain
* valid user input). * valid user input).
*/ */
class FancyLineEdit : public QLineEdit class FancyLineEdit : public QLineEdit
{ {
Q_DISABLE_COPY(FancyLineEdit) Q_DISABLE_COPY(FancyLineEdit)
Q_OBJECT Q_OBJECT
Q_ENUMS(Side) Q_ENUMS(Side)
public: public:
enum Side {Left = 0, Right = 1}; enum Side {Left = 0, Right = 1};
explicit FancyLineEdit(QWidget *parent = 0); explicit FancyLineEdit(QWidget *parent = 0);
~FancyLineEdit(); ~FancyLineEdit();
QPixmap buttonPixmap(Side side) const; QPixmap buttonPixmap(Side side) const;
void setButtonPixmap(Side side, const QPixmap &pixmap); void setButtonPixmap(Side side, const QPixmap &pixmap);
QMenu *buttonMenu(Side side) const; QMenu *buttonMenu(Side side) const;
void setButtonMenu(Side side, QMenu *menu); void setButtonMenu(Side side, QMenu *menu);
void setButtonVisible(Side side, bool visible); void setButtonVisible(Side side, bool visible);
bool isButtonVisible(Side side) const; bool isButtonVisible(Side side) const;
void setButtonToolTip(Side side, const QString &); void setButtonToolTip(Side side, const QString &);
void setButtonFocusPolicy(Side side, Qt::FocusPolicy policy); void setButtonFocusPolicy(Side side, Qt::FocusPolicy policy);
// Set whether tabbing in will trigger the menu. // Set whether tabbing in will trigger the menu.
void setMenuTabFocusTrigger(Side side, bool v); void setMenuTabFocusTrigger(Side side, bool v);
bool hasMenuTabFocusTrigger(Side side) const; bool hasMenuTabFocusTrigger(Side side) const;
// Set if icon should be hidden when text is empty // Set if icon should be hidden when text is empty
void setAutoHideButton(Side side, bool h); void setAutoHideButton(Side side, bool h);
bool hasAutoHideButton(Side side) const; bool hasAutoHideButton(Side side) const;
Q_SIGNALS: Q_SIGNALS:
void buttonClicked(Side side); void buttonClicked(Side side);
void leftButtonClicked(); void leftButtonClicked();
void rightButtonClicked(); void rightButtonClicked();
private Q_SLOTS: private Q_SLOTS:
void checkButtons(const QString &); void checkButtons(const QString &);
void iconClicked(); void iconClicked();
protected: protected:
virtual void resizeEvent(QResizeEvent *e); virtual void resizeEvent(QResizeEvent *e);
private: private:
void updateMargins(); void updateMargins();
void updateButtonPositions(); void updateButtonPositions();
FancyLineEditPrivate *m_d; FancyLineEditPrivate *m_d;
QString m_oldText; QString m_oldText;
}; };
} }
} }
#endif // FANCYLINEEDIT_H #endif // FANCYLINEEDIT_H