-setDefaultText no longer clobbers regular text
-fewer string comparisons svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=791584
This commit is contained in:
parent
7ee81565fb
commit
a5ec59b775
@ -41,12 +41,12 @@ class LineEdit::Private
|
||||
{
|
||||
public:
|
||||
Private()
|
||||
: styled(true), multiline(false) {}
|
||||
: showingDefaultText(true), styled(true), multiline(false) {}
|
||||
|
||||
QString defaultText;
|
||||
QString oldText;
|
||||
QString defaultTextPlain;
|
||||
|
||||
bool showingDefaultText;
|
||||
bool styled;
|
||||
bool multiline;
|
||||
|
||||
@ -212,15 +212,14 @@ void LineEdit::setDefaultText(const QString &text)
|
||||
d->defaultText = text.simplified();
|
||||
//FIXME hardcoded colours aren't nice
|
||||
d->defaultText = QString("<font color=\"gray\">") + d->defaultText + QString("</font>");
|
||||
QGraphicsTextItem::setHtml(d->defaultText);
|
||||
d->defaultTextPlain = QGraphicsTextItem::toPlainText();
|
||||
if (d->showingDefaultText) {
|
||||
QGraphicsTextItem::setHtml(d->defaultText);
|
||||
}
|
||||
}
|
||||
|
||||
const QString LineEdit::toHtml()
|
||||
{
|
||||
//note: comparing html doesn't work because QGraphicsTextItem::toHtml() returns
|
||||
//unpredictable text with lots of added html
|
||||
if (QGraphicsTextItem::toPlainText() == d->defaultTextPlain) {
|
||||
if (d->showingDefaultText) {
|
||||
return QString();
|
||||
} else {
|
||||
return QGraphicsTextItem::toHtml();
|
||||
@ -229,13 +228,35 @@ const QString LineEdit::toHtml()
|
||||
|
||||
const QString LineEdit::toPlainText()
|
||||
{
|
||||
if (QGraphicsTextItem::toPlainText() == d->defaultTextPlain) {
|
||||
if (d->showingDefaultText) {
|
||||
return QString();
|
||||
} else {
|
||||
return QGraphicsTextItem::toPlainText();
|
||||
}
|
||||
}
|
||||
|
||||
void LineEdit::setHtml(const QString &text)
|
||||
{
|
||||
if (text.isEmpty()) {
|
||||
d->showingDefaultText = true;
|
||||
QGraphicsTextItem::setHtml(d->defaultText);
|
||||
} else {
|
||||
d->showingDefaultText = false;
|
||||
QGraphicsTextItem::setHtml(text);
|
||||
}
|
||||
}
|
||||
|
||||
void LineEdit::setPlainText(const QString &text)
|
||||
{
|
||||
if (text.isEmpty()) {
|
||||
d->showingDefaultText = true;
|
||||
QGraphicsTextItem::setHtml(d->defaultText);
|
||||
} else {
|
||||
d->showingDefaultText = false;
|
||||
QGraphicsTextItem::setPlainText(text);
|
||||
}
|
||||
}
|
||||
|
||||
void LineEdit::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
if ( !d->multiline && (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) {
|
||||
@ -255,8 +276,9 @@ void LineEdit::keyPressEvent(QKeyEvent *event)
|
||||
|
||||
void LineEdit::focusInEvent(QFocusEvent *event)
|
||||
{
|
||||
if (QGraphicsTextItem::toPlainText() == d->defaultTextPlain) {
|
||||
if (d->showingDefaultText) {
|
||||
QGraphicsTextItem::setHtml(QString());
|
||||
d->showingDefaultText = false;
|
||||
}
|
||||
QGraphicsTextItem::focusInEvent(event);
|
||||
}
|
||||
@ -265,6 +287,7 @@ void LineEdit::focusOutEvent(QFocusEvent *event)
|
||||
{
|
||||
if (QGraphicsTextItem::toPlainText().isEmpty()) {
|
||||
QGraphicsTextItem::setHtml(d->defaultText);
|
||||
d->showingDefaultText = true;
|
||||
}
|
||||
QGraphicsTextItem::focusOutEvent(event);
|
||||
}
|
||||
|
@ -75,11 +75,13 @@ class PLASMA_EXPORT LineEdit : public QGraphicsTextItem, public LayoutItem
|
||||
|
||||
/**
|
||||
* Set text to be displayed when the LineEdit is empty and doesn't have focus.
|
||||
* Warning: this will overwrite any text currently in the item
|
||||
*/
|
||||
void setDefaultText(const QString &text);
|
||||
|
||||
const QString toHtml();
|
||||
const QString toPlainText();
|
||||
void setHtml(const QString &text);
|
||||
void setPlainText(const QString &text);
|
||||
|
||||
/**
|
||||
* Reimplented from QGraphicsItem
|
||||
|
Loading…
x
Reference in New Issue
Block a user