src/gui/widgets/qlineedit.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
--- a/src/gui/widgets/qlineedit.cpp	Tue Jan 26 12:42:25 2010 +0200
+++ b/src/gui/widgets/qlineedit.cpp	Tue Feb 02 00:43:10 2010 +0200
@@ -383,6 +383,38 @@
     d->control->setText(text);
 }
 
+// ### Qt 4.7: remove this #if guard
+#if (QT_VERSION >= 0x407000) || defined(Q_WS_MAEMO_5)
+/*!
+    \since 4.7
+
+    \property QLineEdit::placeholderText
+    \brief the line edit's placeholder text
+
+    Setting this property makes the line edit display a grayed-out
+    placeholder text as long as the text() is empty and the widget doesn't
+    have focus.
+
+    By default, this property contains an empty string.
+
+    \sa text()
+*/
+QString QLineEdit::placeholderText() const
+{
+    Q_D(const QLineEdit);
+    return d->placeholderText;
+}
+
+void QLineEdit::setPlaceholderText(const QString& placeholderText)
+{
+    Q_D(QLineEdit);
+    if (d->placeholderText != placeholderText) {
+        d->placeholderText = placeholderText;
+        if (!hasFocus())
+            update();
+    }
+}
+#endif
 
 /*!
     \property QLineEdit::displayText
@@ -624,7 +656,7 @@
     Q_D(const QLineEdit);
     ensurePolished();
     QFontMetrics fm(font());
-    int h = qMax(fm.lineSpacing(), 14) + 2*d->verticalMargin
+    int h = qMax(fm.height(), 14) + 2*d->verticalMargin
             + d->topTextMargin + d->bottomTextMargin
             + d->topmargin + d->bottommargin;
     int w = fm.width(QLatin1Char('x')) * 17 + 2*d->horizontalMargin
@@ -1102,6 +1134,17 @@
 }
 
 /*!
+    \since 4.6
+    Sets the \a margins around the text inside the frame.
+
+    See also textMargins().
+*/
+void QLineEdit::setTextMargins(const QMargins &margins)
+{
+    setTextMargins(margins.left(), margins.top(), margins.right(), margins.bottom());
+}
+
+/*!
     Returns the widget's text margins for \a left, \a top, \a right, and \a bottom.
     \since 4.5
 
@@ -1121,6 +1164,18 @@
 }
 
 /*!
+    \since 4.6
+    Returns the widget's text margins.
+
+    \sa setTextMargins()
+*/
+QMargins QLineEdit::textMargins() const
+{
+    Q_D(const QLineEdit);
+    return QMargins(d->leftTextMargin, d->topTextMargin, d->rightTextMargin, d->bottomTextMargin);
+}
+
+/*!
     \property QLineEdit::inputMask
     \brief The validation input mask
 
@@ -1388,6 +1443,18 @@
         QTimer::singleShot(0, this, SLOT(_q_handleWindowActivate()));
     }else if(e->type() == QEvent::ShortcutOverride){
         d->control->processEvent(e);
+    } else if (e->type() == QEvent::KeyRelease) {
+        d->control->setCursorBlinkPeriod(QApplication::cursorFlashTime());
+    } else if (e->type() == QEvent::Show) {
+        //In order to get the cursor blinking if QComboBox::setEditable is called when the combobox has focus
+        if (hasFocus()) {
+            d->control->setCursorBlinkPeriod(QApplication::cursorFlashTime());
+            QStyleOptionFrameV2 opt;
+            initStyleOption(&opt);
+            if ((!hasSelectedText() && d->control->preeditAreaText().isEmpty())
+                || style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, this))
+                d->setCursorVisible(true);
+        }
     }
 
 #ifdef QT_KEYPAD_NAVIGATION
@@ -1492,7 +1559,8 @@
     }
 #endif
 
-    d->handleSoftwareInputPanel(e->button(), d->clickCausedFocus);
+    if (!isReadOnly() && rect().contains(e->pos()))
+        d->handleSoftwareInputPanel(e->button(), d->clickCausedFocus);
     d->clickCausedFocus = 0;
 }
 
@@ -1571,7 +1639,9 @@
                         && !isReadOnly())
                     {
                         setEditFocus(true);
+#ifndef Q_OS_SYMBIAN
                         clear();
+#endif
                     } else {
                         event->ignore();
                         return;
@@ -1589,6 +1659,8 @@
     }
 #endif
     d->control->processKeyEvent(event);
+    if (event->isAccepted())
+        d->control->setCursorBlinkPeriod(0);
 }
 
 /*!
@@ -1628,7 +1700,9 @@
         && hasFocus() && !hasEditFocus()
         && !e->preeditString().isEmpty()) {
         setEditFocus(true);
+#ifndef Q_OS_SYMBIAN
         selectAll();        // so text is replaced rather than appended to
+#endif
     }
 #endif
 
@@ -1687,7 +1761,11 @@
         d->clickCausedFocus = 1;
     }
 #ifdef QT_KEYPAD_NAVIGATION
-    if (!QApplication::keypadNavigationEnabled() || (hasEditFocus() && e->reason() == Qt::PopupFocusReason)){
+    if (!QApplication::keypadNavigationEnabled() || (hasEditFocus() && ( e->reason() == Qt::PopupFocusReason
+#ifdef Q_OS_SYMBIAN
+            || e->reason() == Qt::ActiveWindowFocusReason
+#endif
+            ))) {
 #endif
     d->control->setCursorBlinkPeriod(QApplication::cursorFlashTime());
     QStyleOptionFrameV2 opt;
@@ -1797,6 +1875,18 @@
     }
     QRect lineRect(r.x() + d->horizontalMargin, d->vscroll, r.width() - 2*d->horizontalMargin, fm.height());
 
+    if (d->control->text().isEmpty()) {
+        if (!hasFocus() && !d->placeholderText.isEmpty()) {
+            QColor col = pal.text().color();
+            col.setAlpha(128);
+            QPen oldpen = p.pen();
+            p.setPen(col);
+            p.drawText(lineRect, va, d->placeholderText);
+            p.setPen(oldpen);
+            return;
+        }
+    }
+
     int cix = qRound(d->control->cursorToX());
 
     // horizontal scrolling. d->hscroll is the left indent from the beginning
@@ -1976,38 +2066,48 @@
     Q_D(QLineEdit);
     QMenu *popup = new QMenu(this);
     popup->setObjectName(QLatin1String("qt_edit_menu"));
+    QAction *action = 0;
 
-    QAction *action = popup->addAction(QLineEdit::tr("&Undo") + ACCEL_KEY(QKeySequence::Undo));
-    action->setEnabled(d->control->isUndoAvailable());
-    connect(action, SIGNAL(triggered()), SLOT(undo()));
+    if (!isReadOnly()) {
+        action = popup->addAction(QLineEdit::tr("&Undo") + ACCEL_KEY(QKeySequence::Undo));
+        action->setEnabled(d->control->isUndoAvailable());
+        connect(action, SIGNAL(triggered()), SLOT(undo()));
 
-    action = popup->addAction(QLineEdit::tr("&Redo") + ACCEL_KEY(QKeySequence::Redo));
-    action->setEnabled(d->control->isRedoAvailable());
-    connect(action, SIGNAL(triggered()), SLOT(redo()));
+        action = popup->addAction(QLineEdit::tr("&Redo") + ACCEL_KEY(QKeySequence::Redo));
+        action->setEnabled(d->control->isRedoAvailable());
+        connect(action, SIGNAL(triggered()), SLOT(redo()));
 
-    popup->addSeparator();
+        popup->addSeparator();
+    }
 
 #ifndef QT_NO_CLIPBOARD
-    action = popup->addAction(QLineEdit::tr("Cu&t") + ACCEL_KEY(QKeySequence::Cut));
-    action->setEnabled(!d->control->isReadOnly() && d->control->hasSelectedText()
-            && d->control->echoMode() == QLineEdit::Normal);
-    connect(action, SIGNAL(triggered()), SLOT(cut()));
+    if (!isReadOnly()) {
+        action = popup->addAction(QLineEdit::tr("Cu&t") + ACCEL_KEY(QKeySequence::Cut));
+        action->setEnabled(!d->control->isReadOnly() && d->control->hasSelectedText()
+                && d->control->echoMode() == QLineEdit::Normal);
+        connect(action, SIGNAL(triggered()), SLOT(cut()));
+    }
 
     action = popup->addAction(QLineEdit::tr("&Copy") + ACCEL_KEY(QKeySequence::Copy));
     action->setEnabled(d->control->hasSelectedText()
             && d->control->echoMode() == QLineEdit::Normal);
     connect(action, SIGNAL(triggered()), SLOT(copy()));
 
-    action = popup->addAction(QLineEdit::tr("&Paste") + ACCEL_KEY(QKeySequence::Paste));
-    action->setEnabled(!d->control->isReadOnly() && !QApplication::clipboard()->text().isEmpty());
-    connect(action, SIGNAL(triggered()), SLOT(paste()));
+    if (!isReadOnly()) {
+        action = popup->addAction(QLineEdit::tr("&Paste") + ACCEL_KEY(QKeySequence::Paste));
+        action->setEnabled(!d->control->isReadOnly() && !QApplication::clipboard()->text().isEmpty());
+        connect(action, SIGNAL(triggered()), SLOT(paste()));
+    }
 #endif
 
-    action = popup->addAction(QLineEdit::tr("Delete"));
-    action->setEnabled(!d->control->isReadOnly() && !d->control->text().isEmpty() && d->control->hasSelectedText());
-    connect(action, SIGNAL(triggered()), d->control, SLOT(_q_deleteSelected()));
+    if (!isReadOnly()) {
+        action = popup->addAction(QLineEdit::tr("Delete"));
+        action->setEnabled(!d->control->isReadOnly() && !d->control->text().isEmpty() && d->control->hasSelectedText());
+        connect(action, SIGNAL(triggered()), d->control, SLOT(_q_deleteSelected()));
+    }
 
-    popup->addSeparator();
+    if (!popup->isEmpty())
+        popup->addSeparator();
 
     action = popup->addAction(QLineEdit::tr("Select All") + ACCEL_KEY(QKeySequence::SelectAll));
     action->setEnabled(!d->control->text().isEmpty() && !d->control->allSelected());