Skip to content

Commit

Permalink
Add inactive text feature to LineEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
icefox committed Feb 1, 2009
1 parent beaed2e commit a98153f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
32 changes: 1 addition & 31 deletions src/searchlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ SearchLineEdit::SearchLineEdit(QWidget *parent)
m_searchButton = new SearchButton(this);
updateGeometries();
addWidget(m_searchButton, LeftSide);
m_inactiveText = tr("Search");
setInactiveText(tr("Search"));

QSizePolicy policy = sizePolicy();
setSizePolicy(QSizePolicy::Preferred, policy.verticalPolicy());
Expand All @@ -212,26 +212,6 @@ SearchLineEdit::SearchLineEdit(QWidget *parent)
setUpdatesEnabled(true);
}

void SearchLineEdit::paintEvent(QPaintEvent *event)
{
if (text().isEmpty() && !hasFocus() && !m_inactiveText.isEmpty()) {
LineEdit::paintEvent(event);
QStyleOptionFrameV2 panel;
initStyleOption(&panel);
QRect textRect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
#if QT_VERSION >= 0x040500
int left = textMargin(LineEdit::LeftSide);
int right = textMargin(LineEdit::RightSide);
textRect.adjust(left, 0, -right, 0);
#endif
QPainter painter(this);
painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, m_inactiveText);
} else {
LineEdit::paintEvent(event);
}
}

void SearchLineEdit::resizeEvent(QResizeEvent *event)
{
updateGeometries();
Expand All @@ -249,16 +229,6 @@ void SearchLineEdit::updateGeometries()
updateTextMargins();
}

QString SearchLineEdit::inactiveText() const
{
return m_inactiveText;
}

void SearchLineEdit::setInactiveText(const QString &text)
{
m_inactiveText = text;
}

void SearchLineEdit::setMenu(QMenu *menu)
{
if (m_searchButton->m_menu)
Expand Down
5 changes: 0 additions & 5 deletions src/searchlineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,16 @@ class SearchLineEdit : public LineEdit
public:
SearchLineEdit(QWidget *parent = 0);

QString inactiveText() const;
void setInactiveText(const QString &text);

QMenu *menu() const;
void setMenu(QMenu *menu);

protected:
void resizeEvent(QResizeEvent *event);
void paintEvent(QPaintEvent *event);

private:
void updateGeometries();

SearchButton *m_searchButton;
QString m_inactiveText;
};

#endif // SEARCHLINEEDIT_H
Expand Down
31 changes: 31 additions & 0 deletions src/utils/lineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <qevent.h>
#include <qlayout.h>
#include <qstyleoption.h>
#include <qpainter.h>

#include <qdebug.h>

Expand Down Expand Up @@ -213,3 +214,33 @@ void LineEdit::resizeEvent(QResizeEvent *event)
QLineEdit::resizeEvent(event);
}

QString LineEdit::inactiveText() const
{
return m_inactiveText;
}

void LineEdit::setInactiveText(const QString &text)
{
m_inactiveText = text;
}

void LineEdit::paintEvent(QPaintEvent *event)
{
QLineEdit::paintEvent(event);
if (text().isEmpty() && !m_inactiveText.isEmpty() && !hasFocus()) {
QStyleOptionFrameV2 panel;
initStyleOption(&panel);
QRect textRect = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this);
int horizontalMargin = 2;
textRect.adjust(horizontalMargin, 0, 0, 0);
#if QT_VERSION >= 0x040500
int left = textMargin(LineEdit::LeftSide);
int right = textMargin(LineEdit::RightSide);
textRect.adjust(left, 0, -right, 0);
#endif
QPainter painter(this);
painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color());
painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, m_inactiveText);
}
}

6 changes: 5 additions & 1 deletion src/utils/lineedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class LineEdit : public QLineEdit
void setWidgetSpacing(int spacing);
int widgetSpacing() const;
int textMargin(WidgetPosition position) const;
QString inactiveText() const;
void setInactiveText(const QString &text);

void paintEvent(QPaintEvent *event);

protected:
void resizeEvent(QResizeEvent *event);
Expand All @@ -78,7 +82,7 @@ protected slots:
SideWidget *m_rightWidget;
QHBoxLayout *m_leftLayout;
QHBoxLayout *m_rightLayout;

QString m_inactiveText;
};

#endif // LINEEDIT_H
Expand Down

0 comments on commit a98153f

Please sign in to comment.