Add "defaultText" functionality: when the lineedit does not have focus

and it is empty, the can be "defaultText" that will be displayed.  See 
dict applet for example.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=678514
This commit is contained in:
Thomas Georgiou 2007-06-21 15:42:05 +00:00
parent 6b04a70974
commit c37fabf01c
2 changed files with 53 additions and 2 deletions

View File

@ -21,6 +21,7 @@
#include <QStyleOptionFrameV2>
#include <QTextDocument>
#include <QKeyEvent>
#include <KDebug>
namespace Plasma
{
@ -33,6 +34,9 @@ LineEdit::LineEdit(QGraphicsItem *parent, QGraphicsScene *scene)
: QGraphicsTextItem(parent, scene),
d(new Private())
{
defaultText = QString("");
defaultTextPlain = QString("");
oldText = QString("");
setTextInteractionFlags(Qt::TextEditorInteraction);
}
@ -131,7 +135,30 @@ QSizeF LineEdit::sizeHint() const
return document()->size();
}
void LineEdit::keyPressEvent(QKeyEvent* event)
void LineEdit::setDefaultText(QString text)
{
defaultText = text;
QGraphicsTextItem::setHtml(defaultText);
defaultTextPlain = QGraphicsTextItem::toPlainText();
}
const QString LineEdit::toHtml()
{
if (QGraphicsTextItem::toHtml().simplified() == defaultText.simplified())
return QString("");
else
return QGraphicsTextItem::toHtml();
}
const QString LineEdit::toPlainText()
{
if (QGraphicsTextItem::toHtml().simplified() == defaultText.simplified())
return QString("");
else
return QGraphicsTextItem::toPlainText();
}
void LineEdit::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) {
emit editingFinished();
@ -142,6 +169,22 @@ void LineEdit::keyPressEvent(QKeyEvent* event)
oldText = QGraphicsTextItem::toHtml();
emit textChanged(oldText);
}
// if (QGraphicsTextItem::toPlainText().simplified() == "")
// QGraphicsTextItem::setHtml(defaultText);
}
void LineEdit::focusInEvent(QFocusEvent *event)
{
if (QGraphicsTextItem::toPlainText().simplified() == defaultTextPlain.simplified())
QGraphicsTextItem::setPlainText(QString(""));
QGraphicsTextItem::focusInEvent(event);
}
void LineEdit::focusOutEvent(QFocusEvent *event)
{
if (QGraphicsTextItem::toPlainText().simplified() == "")
QGraphicsTextItem::setHtml(defaultText);
QGraphicsTextItem::focusOutEvent(event);
}
} // namespace Plasma

View File

@ -57,6 +57,10 @@ class PLASMA_EXPORT LineEdit : public QGraphicsTextItem, public LayoutItem
QRectF geometry() const;
void setGeometry(const QRectF& geometry);
QSizeF sizeHint() const;
void setDefaultText(QString text);
const QString toHtml();
const QString toPlainText();
Q_SIGNALS:
void editingFinished();
@ -69,9 +73,13 @@ class PLASMA_EXPORT LineEdit : public QGraphicsTextItem, public LayoutItem
class Private;
Private* const d;
QString oldText;
QString defaultText;
QString defaultTextPlain;
protected:
void keyPressEvent(QKeyEvent* event);
void keyPressEvent(QKeyEvent *event);
void focusInEvent(QFocusEvent *event);
void focusOutEvent(QFocusEvent *event);
};
} // namespace Plasma