src/gui/widgets/qlineedit.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   381 {
   381 {
   382     Q_D(QLineEdit);
   382     Q_D(QLineEdit);
   383     d->control->setText(text);
   383     d->control->setText(text);
   384 }
   384 }
   385 
   385 
       
   386 // ### Qt 4.7: remove this #if guard
       
   387 #if (QT_VERSION >= 0x407000) || defined(Q_WS_MAEMO_5)
       
   388 /*!
       
   389     \since 4.7
       
   390 
       
   391     \property QLineEdit::placeholderText
       
   392     \brief the line edit's placeholder text
       
   393 
       
   394     Setting this property makes the line edit display a grayed-out
       
   395     placeholder text as long as the text() is empty and the widget doesn't
       
   396     have focus.
       
   397 
       
   398     By default, this property contains an empty string.
       
   399 
       
   400     \sa text()
       
   401 */
       
   402 QString QLineEdit::placeholderText() const
       
   403 {
       
   404     Q_D(const QLineEdit);
       
   405     return d->placeholderText;
       
   406 }
       
   407 
       
   408 void QLineEdit::setPlaceholderText(const QString& placeholderText)
       
   409 {
       
   410     Q_D(QLineEdit);
       
   411     if (d->placeholderText != placeholderText) {
       
   412         d->placeholderText = placeholderText;
       
   413         if (!hasFocus())
       
   414             update();
       
   415     }
       
   416 }
       
   417 #endif
   386 
   418 
   387 /*!
   419 /*!
   388     \property QLineEdit::displayText
   420     \property QLineEdit::displayText
   389     \brief the displayed text
   421     \brief the displayed text
   390 
   422 
   622 QSize QLineEdit::sizeHint() const
   654 QSize QLineEdit::sizeHint() const
   623 {
   655 {
   624     Q_D(const QLineEdit);
   656     Q_D(const QLineEdit);
   625     ensurePolished();
   657     ensurePolished();
   626     QFontMetrics fm(font());
   658     QFontMetrics fm(font());
   627     int h = qMax(fm.lineSpacing(), 14) + 2*d->verticalMargin
   659     int h = qMax(fm.height(), 14) + 2*d->verticalMargin
   628             + d->topTextMargin + d->bottomTextMargin
   660             + d->topTextMargin + d->bottomTextMargin
   629             + d->topmargin + d->bottommargin;
   661             + d->topmargin + d->bottommargin;
   630     int w = fm.width(QLatin1Char('x')) * 17 + 2*d->horizontalMargin
   662     int w = fm.width(QLatin1Char('x')) * 17 + 2*d->horizontalMargin
   631             + d->leftTextMargin + d->rightTextMargin
   663             + d->leftTextMargin + d->rightTextMargin
   632             + d->leftmargin + d->rightmargin; // "some"
   664             + d->leftmargin + d->rightmargin; // "some"
  1100     updateGeometry();
  1132     updateGeometry();
  1101     update();
  1133     update();
  1102 }
  1134 }
  1103 
  1135 
  1104 /*!
  1136 /*!
       
  1137     \since 4.6
       
  1138     Sets the \a margins around the text inside the frame.
       
  1139 
       
  1140     See also textMargins().
       
  1141 */
       
  1142 void QLineEdit::setTextMargins(const QMargins &margins)
       
  1143 {
       
  1144     setTextMargins(margins.left(), margins.top(), margins.right(), margins.bottom());
       
  1145 }
       
  1146 
       
  1147 /*!
  1105     Returns the widget's text margins for \a left, \a top, \a right, and \a bottom.
  1148     Returns the widget's text margins for \a left, \a top, \a right, and \a bottom.
  1106     \since 4.5
  1149     \since 4.5
  1107 
  1150 
  1108     \sa setTextMargins()
  1151     \sa setTextMargins()
  1109 */
  1152 */
  1116         *top = d->topTextMargin;
  1159         *top = d->topTextMargin;
  1117     if (right)
  1160     if (right)
  1118         *right = d->rightTextMargin;
  1161         *right = d->rightTextMargin;
  1119     if (bottom)
  1162     if (bottom)
  1120         *bottom = d->bottomTextMargin;
  1163         *bottom = d->bottomTextMargin;
       
  1164 }
       
  1165 
       
  1166 /*!
       
  1167     \since 4.6
       
  1168     Returns the widget's text margins.
       
  1169 
       
  1170     \sa setTextMargins()
       
  1171 */
       
  1172 QMargins QLineEdit::textMargins() const
       
  1173 {
       
  1174     Q_D(const QLineEdit);
       
  1175     return QMargins(d->leftTextMargin, d->topTextMargin, d->rightTextMargin, d->bottomTextMargin);
  1121 }
  1176 }
  1122 
  1177 
  1123 /*!
  1178 /*!
  1124     \property QLineEdit::inputMask
  1179     \property QLineEdit::inputMask
  1125     \brief The validation input mask
  1180     \brief The validation input mask
  1386         //d->separate();
  1441         //d->separate();
  1387     } else if (e->type() == QEvent::WindowActivate) {
  1442     } else if (e->type() == QEvent::WindowActivate) {
  1388         QTimer::singleShot(0, this, SLOT(_q_handleWindowActivate()));
  1443         QTimer::singleShot(0, this, SLOT(_q_handleWindowActivate()));
  1389     }else if(e->type() == QEvent::ShortcutOverride){
  1444     }else if(e->type() == QEvent::ShortcutOverride){
  1390         d->control->processEvent(e);
  1445         d->control->processEvent(e);
       
  1446     } else if (e->type() == QEvent::KeyRelease) {
       
  1447         d->control->setCursorBlinkPeriod(QApplication::cursorFlashTime());
       
  1448     } else if (e->type() == QEvent::Show) {
       
  1449         //In order to get the cursor blinking if QComboBox::setEditable is called when the combobox has focus
       
  1450         if (hasFocus()) {
       
  1451             d->control->setCursorBlinkPeriod(QApplication::cursorFlashTime());
       
  1452             QStyleOptionFrameV2 opt;
       
  1453             initStyleOption(&opt);
       
  1454             if ((!hasSelectedText() && d->control->preeditAreaText().isEmpty())
       
  1455                 || style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, this))
       
  1456                 d->setCursorVisible(true);
       
  1457         }
  1391     }
  1458     }
  1392 
  1459 
  1393 #ifdef QT_KEYPAD_NAVIGATION
  1460 #ifdef QT_KEYPAD_NAVIGATION
  1394     if (QApplication::keypadNavigationEnabled()) {
  1461     if (QApplication::keypadNavigationEnabled()) {
  1395         if (e->type() == QEvent::EnterEditFocus) {
  1462         if (e->type() == QEvent::EnterEditFocus) {
  1490             insert(QApplication::clipboard()->text(QClipboard::Selection));
  1557             insert(QApplication::clipboard()->text(QClipboard::Selection));
  1491         }
  1558         }
  1492     }
  1559     }
  1493 #endif
  1560 #endif
  1494 
  1561 
  1495     d->handleSoftwareInputPanel(e->button(), d->clickCausedFocus);
  1562     if (!isReadOnly() && rect().contains(e->pos()))
       
  1563         d->handleSoftwareInputPanel(e->button(), d->clickCausedFocus);
  1496     d->clickCausedFocus = 0;
  1564     d->clickCausedFocus = 0;
  1497 }
  1565 }
  1498 
  1566 
  1499 /*! \reimp
  1567 /*! \reimp
  1500 */
  1568 */
  1569                 if (!hasEditFocus() && !(event->modifiers() & Qt::ControlModifier)) {
  1637                 if (!hasEditFocus() && !(event->modifiers() & Qt::ControlModifier)) {
  1570                     if (!event->text().isEmpty() && event->text().at(0).isPrint()
  1638                     if (!event->text().isEmpty() && event->text().at(0).isPrint()
  1571                         && !isReadOnly())
  1639                         && !isReadOnly())
  1572                     {
  1640                     {
  1573                         setEditFocus(true);
  1641                         setEditFocus(true);
       
  1642 #ifndef Q_OS_SYMBIAN
  1574                         clear();
  1643                         clear();
       
  1644 #endif
  1575                     } else {
  1645                     } else {
  1576                         event->ignore();
  1646                         event->ignore();
  1577                         return;
  1647                         return;
  1578                     }
  1648                     }
  1579                 }
  1649                 }
  1587         if (event->key() == Qt::Key_Select)
  1657         if (event->key() == Qt::Key_Select)
  1588             return; // Just start. No action.
  1658             return; // Just start. No action.
  1589     }
  1659     }
  1590 #endif
  1660 #endif
  1591     d->control->processKeyEvent(event);
  1661     d->control->processKeyEvent(event);
       
  1662     if (event->isAccepted())
       
  1663         d->control->setCursorBlinkPeriod(0);
  1592 }
  1664 }
  1593 
  1665 
  1594 /*!
  1666 /*!
  1595   \since 4.4
  1667   \since 4.4
  1596 
  1668 
  1626     // commit text as they focus out without interfering with focus
  1698     // commit text as they focus out without interfering with focus
  1627     if (QApplication::keypadNavigationEnabled()
  1699     if (QApplication::keypadNavigationEnabled()
  1628         && hasFocus() && !hasEditFocus()
  1700         && hasFocus() && !hasEditFocus()
  1629         && !e->preeditString().isEmpty()) {
  1701         && !e->preeditString().isEmpty()) {
  1630         setEditFocus(true);
  1702         setEditFocus(true);
       
  1703 #ifndef Q_OS_SYMBIAN
  1631         selectAll();        // so text is replaced rather than appended to
  1704         selectAll();        // so text is replaced rather than appended to
       
  1705 #endif
  1632     }
  1706     }
  1633 #endif
  1707 #endif
  1634 
  1708 
  1635     d->control->processInputMethodEvent(e);
  1709     d->control->processInputMethodEvent(e);
  1636 
  1710 
  1685             selectAll();
  1759             selectAll();
  1686     } else if (e->reason() == Qt::MouseFocusReason) {
  1760     } else if (e->reason() == Qt::MouseFocusReason) {
  1687         d->clickCausedFocus = 1;
  1761         d->clickCausedFocus = 1;
  1688     }
  1762     }
  1689 #ifdef QT_KEYPAD_NAVIGATION
  1763 #ifdef QT_KEYPAD_NAVIGATION
  1690     if (!QApplication::keypadNavigationEnabled() || (hasEditFocus() && e->reason() == Qt::PopupFocusReason)){
  1764     if (!QApplication::keypadNavigationEnabled() || (hasEditFocus() && ( e->reason() == Qt::PopupFocusReason
       
  1765 #ifdef Q_OS_SYMBIAN
       
  1766             || e->reason() == Qt::ActiveWindowFocusReason
       
  1767 #endif
       
  1768             ))) {
  1691 #endif
  1769 #endif
  1692     d->control->setCursorBlinkPeriod(QApplication::cursorFlashTime());
  1770     d->control->setCursorBlinkPeriod(QApplication::cursorFlashTime());
  1693     QStyleOptionFrameV2 opt;
  1771     QStyleOptionFrameV2 opt;
  1694     initStyleOption(&opt);
  1772     initStyleOption(&opt);
  1695     if((!hasSelectedText() && d->control->preeditAreaText().isEmpty())
  1773     if((!hasSelectedText() && d->control->preeditAreaText().isEmpty())
  1794          //center
  1872          //center
  1795          d->vscroll = r.y() + (r.height() - fm.height() + 1) / 2;
  1873          d->vscroll = r.y() + (r.height() - fm.height() + 1) / 2;
  1796          break;
  1874          break;
  1797     }
  1875     }
  1798     QRect lineRect(r.x() + d->horizontalMargin, d->vscroll, r.width() - 2*d->horizontalMargin, fm.height());
  1876     QRect lineRect(r.x() + d->horizontalMargin, d->vscroll, r.width() - 2*d->horizontalMargin, fm.height());
       
  1877 
       
  1878     if (d->control->text().isEmpty()) {
       
  1879         if (!hasFocus() && !d->placeholderText.isEmpty()) {
       
  1880             QColor col = pal.text().color();
       
  1881             col.setAlpha(128);
       
  1882             QPen oldpen = p.pen();
       
  1883             p.setPen(col);
       
  1884             p.drawText(lineRect, va, d->placeholderText);
       
  1885             p.setPen(oldpen);
       
  1886             return;
       
  1887         }
       
  1888     }
  1799 
  1889 
  1800     int cix = qRound(d->control->cursorToX());
  1890     int cix = qRound(d->control->cursorToX());
  1801 
  1891 
  1802     // horizontal scrolling. d->hscroll is the left indent from the beginning
  1892     // horizontal scrolling. d->hscroll is the left indent from the beginning
  1803     // of the text line to the left edge of lineRect. we update this value
  1893     // of the text line to the left edge of lineRect. we update this value
  1974 QMenu *QLineEdit::createStandardContextMenu()
  2064 QMenu *QLineEdit::createStandardContextMenu()
  1975 {
  2065 {
  1976     Q_D(QLineEdit);
  2066     Q_D(QLineEdit);
  1977     QMenu *popup = new QMenu(this);
  2067     QMenu *popup = new QMenu(this);
  1978     popup->setObjectName(QLatin1String("qt_edit_menu"));
  2068     popup->setObjectName(QLatin1String("qt_edit_menu"));
  1979 
  2069     QAction *action = 0;
  1980     QAction *action = popup->addAction(QLineEdit::tr("&Undo") + ACCEL_KEY(QKeySequence::Undo));
  2070 
  1981     action->setEnabled(d->control->isUndoAvailable());
  2071     if (!isReadOnly()) {
  1982     connect(action, SIGNAL(triggered()), SLOT(undo()));
  2072         action = popup->addAction(QLineEdit::tr("&Undo") + ACCEL_KEY(QKeySequence::Undo));
  1983 
  2073         action->setEnabled(d->control->isUndoAvailable());
  1984     action = popup->addAction(QLineEdit::tr("&Redo") + ACCEL_KEY(QKeySequence::Redo));
  2074         connect(action, SIGNAL(triggered()), SLOT(undo()));
  1985     action->setEnabled(d->control->isRedoAvailable());
  2075 
  1986     connect(action, SIGNAL(triggered()), SLOT(redo()));
  2076         action = popup->addAction(QLineEdit::tr("&Redo") + ACCEL_KEY(QKeySequence::Redo));
  1987 
  2077         action->setEnabled(d->control->isRedoAvailable());
  1988     popup->addSeparator();
  2078         connect(action, SIGNAL(triggered()), SLOT(redo()));
       
  2079 
       
  2080         popup->addSeparator();
       
  2081     }
  1989 
  2082 
  1990 #ifndef QT_NO_CLIPBOARD
  2083 #ifndef QT_NO_CLIPBOARD
  1991     action = popup->addAction(QLineEdit::tr("Cu&t") + ACCEL_KEY(QKeySequence::Cut));
  2084     if (!isReadOnly()) {
  1992     action->setEnabled(!d->control->isReadOnly() && d->control->hasSelectedText()
  2085         action = popup->addAction(QLineEdit::tr("Cu&t") + ACCEL_KEY(QKeySequence::Cut));
  1993             && d->control->echoMode() == QLineEdit::Normal);
  2086         action->setEnabled(!d->control->isReadOnly() && d->control->hasSelectedText()
  1994     connect(action, SIGNAL(triggered()), SLOT(cut()));
  2087                 && d->control->echoMode() == QLineEdit::Normal);
       
  2088         connect(action, SIGNAL(triggered()), SLOT(cut()));
       
  2089     }
  1995 
  2090 
  1996     action = popup->addAction(QLineEdit::tr("&Copy") + ACCEL_KEY(QKeySequence::Copy));
  2091     action = popup->addAction(QLineEdit::tr("&Copy") + ACCEL_KEY(QKeySequence::Copy));
  1997     action->setEnabled(d->control->hasSelectedText()
  2092     action->setEnabled(d->control->hasSelectedText()
  1998             && d->control->echoMode() == QLineEdit::Normal);
  2093             && d->control->echoMode() == QLineEdit::Normal);
  1999     connect(action, SIGNAL(triggered()), SLOT(copy()));
  2094     connect(action, SIGNAL(triggered()), SLOT(copy()));
  2000 
  2095 
  2001     action = popup->addAction(QLineEdit::tr("&Paste") + ACCEL_KEY(QKeySequence::Paste));
  2096     if (!isReadOnly()) {
  2002     action->setEnabled(!d->control->isReadOnly() && !QApplication::clipboard()->text().isEmpty());
  2097         action = popup->addAction(QLineEdit::tr("&Paste") + ACCEL_KEY(QKeySequence::Paste));
  2003     connect(action, SIGNAL(triggered()), SLOT(paste()));
  2098         action->setEnabled(!d->control->isReadOnly() && !QApplication::clipboard()->text().isEmpty());
  2004 #endif
  2099         connect(action, SIGNAL(triggered()), SLOT(paste()));
  2005 
  2100     }
  2006     action = popup->addAction(QLineEdit::tr("Delete"));
  2101 #endif
  2007     action->setEnabled(!d->control->isReadOnly() && !d->control->text().isEmpty() && d->control->hasSelectedText());
  2102 
  2008     connect(action, SIGNAL(triggered()), d->control, SLOT(_q_deleteSelected()));
  2103     if (!isReadOnly()) {
  2009 
  2104         action = popup->addAction(QLineEdit::tr("Delete"));
  2010     popup->addSeparator();
  2105         action->setEnabled(!d->control->isReadOnly() && !d->control->text().isEmpty() && d->control->hasSelectedText());
       
  2106         connect(action, SIGNAL(triggered()), d->control, SLOT(_q_deleteSelected()));
       
  2107     }
       
  2108 
       
  2109     if (!popup->isEmpty())
       
  2110         popup->addSeparator();
  2011 
  2111 
  2012     action = popup->addAction(QLineEdit::tr("Select All") + ACCEL_KEY(QKeySequence::SelectAll));
  2112     action = popup->addAction(QLineEdit::tr("Select All") + ACCEL_KEY(QKeySequence::SelectAll));
  2013     action->setEnabled(!d->control->text().isEmpty() && !d->control->allSelected());
  2113     action->setEnabled(!d->control->text().isEmpty() && !d->control->allSelected());
  2014     d->selectAllAction = action;
  2114     d->selectAllAction = action;
  2015     connect(action, SIGNAL(triggered()), SLOT(selectAll()));
  2115     connect(action, SIGNAL(triggered()), SLOT(selectAll()));