Ignore:
Timestamp:
May 5, 2011, 5:36:53 AM (14 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/gui/widgets/qlinecontrol_p.h

    r769 r846  
    11/****************************************************************************
    22**
    3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
    44** All rights reserved.
    55** Contact: Nokia Corporation (qt-info@nokia.com)
     
    7979public:
    8080    QLineControl(const QString &txt = QString())
    81         : m_cursor(0), m_preeditCursor(0), m_cursorWidth(0), m_layoutDirection(Qt::LeftToRight),
     81        : m_cursor(0), m_preeditCursor(0), m_cursorWidth(0), m_layoutDirection(Qt::LayoutDirectionAuto),
    8282        m_hideCursor(false), m_separator(0), m_readOnly(0),
    8383        m_dragEnabled(0), m_echoMode(0), m_textDirty(0), m_selDirty(0),
     
    9595    }
    9696
    97     int nextMaskBlank(int pos);
    98     int prevMaskBlank(int pos);
    99 
    100     bool isUndoAvailable() const;
    101     bool isRedoAvailable() const;
    102     void clearUndo();
    103     bool isModified() const;
    104     void setModified(bool modified);
    105 
    106     bool allSelected() const;
    107     bool hasSelectedText() const;
    108 
    109     int width() const;
    110     int height() const;
    111     int ascent() const;
    112     qreal naturalTextWidth() const;
     97    int nextMaskBlank(int pos)
     98    {
     99        int c = findInMask(pos, true, false);
     100        m_separator |= (c != pos);
     101        return (c != -1 ?  c : m_maxLength);
     102    }
     103
     104    int prevMaskBlank(int pos)
     105    {
     106        int c = findInMask(pos, false, false);
     107        m_separator |= (c != pos);
     108        return (c != -1 ? c : 0);
     109    }
     110
     111    bool isUndoAvailable() const { return !m_readOnly && m_undoState; }
     112    bool isRedoAvailable() const { return !m_readOnly && m_undoState < (int)m_history.size(); }
     113    void clearUndo() { m_history.clear(); m_modifiedState = m_undoState = 0; }
     114
     115    bool isModified() const { return m_modifiedState != m_undoState; }
     116    void setModified(bool modified) { m_modifiedState = modified ? -1 : m_undoState; }
     117
     118    bool allSelected() const { return !m_text.isEmpty() && m_selstart == 0 && m_selend == (int)m_text.length(); }
     119    bool hasSelectedText() const { return !m_text.isEmpty() && m_selend > m_selstart; }
     120
     121    int width() const { return qRound(m_textLayout.lineAt(0).width()) + 1; }
     122    int height() const { return qRound(m_textLayout.lineAt(0).height()) + 1; }
     123    int ascent() const { return m_ascent; }
     124    qreal naturalTextWidth() const { return m_textLayout.lineAt(0).naturalTextWidth(); }
    113125
    114126    void setSelection(int start, int length);
    115127
    116     QString selectedText() const;
    117     QString textBeforeSelection() const;
    118     QString textAfterSelection() const;
    119 
    120     int selectionStart() const;
    121     int selectionEnd() const;
    122     bool inSelection(int x) const;
    123 
    124     void removeSelection();
    125 
    126     int start() const;
    127     int end() const;
     128    inline QString selectedText() const { return hasSelectedText() ? m_text.mid(m_selstart, m_selend - m_selstart) : QString(); }
     129    QString textBeforeSelection() const { return hasSelectedText() ? m_text.left(m_selstart) : QString(); }
     130    QString textAfterSelection() const { return hasSelectedText() ? m_text.mid(m_selend) : QString(); }
     131
     132    int selectionStart() const { return hasSelectedText() ? m_selstart : -1; }
     133    int selectionEnd() const { return hasSelectedText() ? m_selend : -1; }
     134    bool inSelection(int x) const
     135    {
     136        if (m_selstart >= m_selend)
     137            return false;
     138        int pos = xToPos(x, QTextLine::CursorOnCharacter);
     139        return pos >= m_selstart && pos < m_selend;
     140    }
     141
     142    void removeSelection()
     143    {
     144        int priorState = m_undoState;
     145        removeSelectedText();
     146        finishChange(priorState);
     147    }
     148
     149    int start() const { return 0; }
     150    int end() const { return m_text.length(); }
    128151
    129152#ifndef QT_NO_CLIPBOARD
    130153    void copy(QClipboard::Mode mode = QClipboard::Clipboard) const;
    131     void paste();
    132 #endif
    133 
    134     int cursor() const;
    135     int preeditCursor() const;
    136 
    137     int cursorWidth() const;
    138     void setCursorWidth(int value);
     154    void paste(QClipboard::Mode mode = QClipboard::Clipboard);
     155#endif
     156
     157    int cursor() const{ return m_cursor; }
     158    int preeditCursor() const { return m_preeditCursor; }
     159
     160    int cursorWidth() const { return m_cursorWidth; }
     161    void setCursorWidth(int value) { m_cursorWidth = value; }
     162
    139163
    140164    void moveCursor(int pos, bool mark = false);
    141     void cursorForward(bool mark, int steps);
    142     void cursorWordForward(bool mark);
    143     void cursorWordBackward(bool mark);
    144     void home(bool mark);
    145     void end(bool mark);
     165    void cursorForward(bool mark, int steps)
     166    {
     167        int c = m_cursor;
     168        if (steps > 0) {
     169            while (steps--)
     170                c = m_textLayout.nextCursorPosition(c);
     171        } else if (steps < 0) {
     172            while (steps++)
     173                c = m_textLayout.previousCursorPosition(c);
     174        }
     175        moveCursor(c, mark);
     176    }
     177
     178    void cursorWordForward(bool mark) { moveCursor(m_textLayout.nextCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
     179    void cursorWordBackward(bool mark) { moveCursor(m_textLayout.previousCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
     180
     181    void home(bool mark) { moveCursor(0, mark); }
     182    void end(bool mark) { moveCursor(text().length(), mark); }
    146183
    147184    int xToPos(int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters) const;
    148185    QRect cursorRect() const;
    149186
    150     qreal cursorToX(int cursor) const;
    151     qreal cursorToX() const;
    152 
    153     bool isReadOnly() const;
    154     void setReadOnly(bool enable);
    155 
    156     QString text() const;
    157     void setText(const QString &txt);
    158 
    159     QString displayText() const;
     187    qreal cursorToX(int cursor) const { return m_textLayout.lineAt(0).cursorToX(cursor); }
     188    qreal cursorToX() const
     189    {
     190        int cursor = m_cursor;
     191        if (m_preeditCursor != -1)
     192            cursor += m_preeditCursor;
     193        return cursorToX(cursor);
     194    }
     195
     196    bool isReadOnly() const { return m_readOnly; }
     197    void setReadOnly(bool enable) { m_readOnly = enable; }
     198
     199    QString text() const
     200    {
     201        QString res = m_maskData ? stripString(m_text) : m_text;
     202        return (res.isNull() ? QString::fromLatin1("") : res);
     203    }
     204    void setText(const QString &txt) { internalSetText(txt, -1, false); }
     205    QString displayText() const { return m_textLayout.text(); }
    160206
    161207    void backspace();
    162208    void del();
    163     void deselect();
    164     void selectAll();
     209    void deselect() { internalDeselect(); finishChange(); }
     210    void selectAll() { m_selstart = m_selend = m_cursor = 0; moveCursor(m_text.length(), true); }
     211
    165212    void insert(const QString &);
    166213    void clear();
    167     void undo();
    168     void redo();
     214    void undo() { internalUndo(); finishChange(-1, true); }
     215    void redo() { internalRedo(); finishChange(); }
    169216    void selectWordAtPos(int);
    170217
    171     uint echoMode() const;
    172     void setEchoMode(uint mode);
    173 
    174     void setMaxLength(int maxLength);
    175     int maxLength() const;
     218    uint echoMode() const { return m_echoMode; }
     219    void setEchoMode(uint mode)
     220    {
     221        m_echoMode = mode;
     222        m_passwordEchoEditing = false;
     223        updateDisplayText();
     224    }
     225
     226    int maxLength() const { return m_maxLength; }
     227    void setMaxLength(int maxLength)
     228    {
     229        if (m_maskData)
     230            return;
     231        m_maxLength = maxLength;
     232        setText(m_text);
     233    }
    176234
    177235#ifndef QT_NO_VALIDATOR
    178     const QValidator *validator() const;
    179     void setValidator(const QValidator *);
     236    const QValidator *validator() const { return m_validator; }
     237    void setValidator(const QValidator *v) { m_validator = const_cast<QValidator*>(v); }
    180238#endif
    181239
    182240#ifndef QT_NO_COMPLETER
    183     QCompleter *completer() const;
    184     void setCompleter(const QCompleter*);
     241    QCompleter *completer() const { return m_completer; }
     242    /* Note that you must set the widget for the completer separately */
     243    void setCompleter(const QCompleter *c) { m_completer = const_cast<QCompleter*>(c); }
    185244    void complete(int key);
    186245#endif
    187246
    188     void setCursorPosition(int pos);
    189     int cursorPosition() const;
    190 
    191     bool hasAcceptableInput() const;
     247    int cursorPosition() const { return m_cursor; }
     248    void setCursorPosition(int pos) { if (pos <= m_text.length()) moveCursor(qMax(0, pos)); }
     249
     250    bool hasAcceptableInput() const { return hasAcceptableInput(m_text); }
    192251    bool fixup();
    193252
    194     QString inputMask() const;
    195     void setInputMask(const QString &mask);
     253    QString inputMask() const { return m_maskData ? m_inputMask + QLatin1Char(';') + m_blank : QString(); }
     254    void setInputMask(const QString &mask)
     255    {
     256        parseInputMask(mask);
     257        if (m_maskData)
     258            moveCursor(nextMaskBlank(0));
     259    }
    196260
    197261    // input methods
    198262#ifndef QT_NO_IM
    199     bool composeMode() const;
    200     void setPreeditArea(int cursor, const QString &text);
    201 #endif
    202 
    203     QString preeditAreaText() const;
     263    bool composeMode() const { return !m_textLayout.preeditAreaText().isEmpty(); }
     264    void setPreeditArea(int cursor, const QString &text) { m_textLayout.setPreeditArea(cursor, text); }
     265#endif
     266
     267    QString preeditAreaText() const { return m_textLayout.preeditAreaText(); }
    204268
    205269    void updatePasswordEchoEditing(bool editing);
    206     bool passwordEchoEditing() const;
    207 
    208     QChar passwordCharacter() const;
    209     void setPasswordCharacter(const QChar &character);
    210 
    211     Qt::LayoutDirection layoutDirection() const;
    212     void setLayoutDirection(Qt::LayoutDirection direction);
    213     void setFont(const QFont &font);
     270    bool passwordEchoEditing() const { return m_passwordEchoEditing; }
     271
     272    QChar passwordCharacter() const { return m_passwordCharacter; }
     273    void setPasswordCharacter(const QChar &character) { m_passwordCharacter = character; updateDisplayText(); }
     274
     275    Qt::LayoutDirection layoutDirection() const {
     276        if (m_layoutDirection == Qt::LayoutDirectionAuto) {
     277            if (m_text.isEmpty())
     278                return QApplication::keyboardInputDirection();
     279            return m_text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight;
     280        }
     281        return m_layoutDirection;
     282    }
     283    void setLayoutDirection(Qt::LayoutDirection direction)
     284    {
     285        if (direction != m_layoutDirection) {
     286            m_layoutDirection = direction;
     287            updateDisplayText();
     288        }
     289    }
     290
     291    void setFont(const QFont &font) { m_textLayout.setFont(font); updateDisplayText(); }
    214292
    215293    void processInputMethodEvent(QInputMethodEvent *event);
     
    217295    void processKeyEvent(QKeyEvent* ev);
    218296
    219     int cursorBlinkPeriod() const;
     297    int cursorBlinkPeriod() const { return m_blinkPeriod; }
    220298    void setCursorBlinkPeriod(int msec);
    221 
    222     QString cancelText() const;
    223     void setCancelText(const QString &text);
    224 
    225     const QPalette &palette() const;
    226     void setPalette(const QPalette &);
     299    void resetCursorBlinkTimer();
     300
     301    QString cancelText() const { return m_cancelText; }
     302    void setCancelText(const QString &text) { m_cancelText = text; }
     303
     304    const QPalette &palette() const { return m_palette; }
     305    void setPalette(const QPalette &p) { m_palette = p; }
    227306
    228307    enum DrawFlags {
     
    364443};
    365444
    366 inline int QLineControl::nextMaskBlank(int pos)
    367 {
    368     int c = findInMask(pos, true, false);
    369     m_separator |= (c != pos);
    370     return (c != -1 ?  c : m_maxLength);
    371 }
    372 
    373 inline int QLineControl::prevMaskBlank(int pos)
    374 {
    375     int c = findInMask(pos, false, false);
    376     m_separator |= (c != pos);
    377     return (c != -1 ? c : 0);
    378 }
    379 
    380 inline bool QLineControl::isUndoAvailable() const
    381 {
    382     return !m_readOnly && m_undoState;
    383 }
    384 
    385 inline bool QLineControl::isRedoAvailable() const
    386 {
    387     return !m_readOnly && m_undoState < (int)m_history.size();
    388 }
    389 
    390 inline void QLineControl::clearUndo()
    391 {
    392     m_history.clear();
    393     m_modifiedState = m_undoState = 0;
    394 }
    395 
    396 inline bool QLineControl::isModified() const
    397 {
    398     return m_modifiedState != m_undoState;
    399 }
    400 
    401 inline void QLineControl::setModified(bool modified)
    402 {
    403     m_modifiedState = modified ? -1 : m_undoState;
    404 }
    405 
    406 inline bool QLineControl::allSelected() const
    407 {
    408     return !m_text.isEmpty() && m_selstart == 0 && m_selend == (int)m_text.length();
    409 }
    410 
    411 inline bool QLineControl::hasSelectedText() const
    412 {
    413     return !m_text.isEmpty() && m_selend > m_selstart;
    414 }
    415 
    416 inline int QLineControl::width() const
    417 {
    418     return qRound(m_textLayout.lineAt(0).width()) + 1;
    419 }
    420 
    421 inline qreal QLineControl::naturalTextWidth() const
    422 {
    423     return m_textLayout.lineAt(0).naturalTextWidth();
    424 }
    425 
    426 inline int QLineControl::height() const
    427 {
    428     return qRound(m_textLayout.lineAt(0).height()) + 1;
    429 }
    430 
    431 inline int QLineControl::ascent() const
    432 {
    433     return m_ascent;
    434 }
    435 
    436 inline QString QLineControl::selectedText() const
    437 {
    438     if (hasSelectedText())
    439         return m_text.mid(m_selstart, m_selend - m_selstart);
    440     return QString();
    441 }
    442 
    443 inline QString QLineControl::textBeforeSelection() const
    444 {
    445     if (hasSelectedText())
    446         return m_text.left(m_selstart);
    447     return QString();
    448 }
    449 
    450 inline QString QLineControl::textAfterSelection() const
    451 {
    452     if (hasSelectedText())
    453         return m_text.mid(m_selend);
    454     return QString();
    455 }
    456 
    457 inline int QLineControl::selectionStart() const
    458 {
    459     return hasSelectedText() ? m_selstart : -1;
    460 }
    461 
    462 inline int QLineControl::selectionEnd() const
    463 {
    464     return hasSelectedText() ? m_selend : -1;
    465 }
    466 
    467 inline int QLineControl::start() const
    468 {
    469     return 0;
    470 }
    471 
    472 inline int QLineControl::end() const
    473 {
    474     return m_text.length();
    475 }
    476 
    477 inline void QLineControl::removeSelection()
    478 {
    479     int priorState = m_undoState;
    480     removeSelectedText();
    481     finishChange(priorState);
    482 }
    483 
    484 inline bool QLineControl::inSelection(int x) const
    485 {
    486     if (m_selstart >= m_selend)
    487         return false;
    488     int pos = xToPos(x, QTextLine::CursorOnCharacter);
    489     return pos >= m_selstart && pos < m_selend;
    490 }
    491 
    492 inline int QLineControl::cursor() const
    493 {
    494     return m_cursor;
    495 }
    496 
    497 inline int QLineControl::preeditCursor() const
    498 {
    499     return m_preeditCursor;
    500 }
    501 
    502 inline int QLineControl::cursorWidth() const
    503 {
    504     return m_cursorWidth;
    505 }
    506 
    507 inline void QLineControl::setCursorWidth(int value)
    508 {
    509     m_cursorWidth = value;
    510 }
    511 
    512 inline void QLineControl::cursorForward(bool mark, int steps)
    513 {
    514     int c = m_cursor;
    515     if (steps > 0) {
    516         while (steps--)
    517             c = m_textLayout.nextCursorPosition(c);
    518     } else if (steps < 0) {
    519         while (steps++)
    520             c = m_textLayout.previousCursorPosition(c);
    521     }
    522     moveCursor(c, mark);
    523 }
    524 
    525 inline void QLineControl::cursorWordForward(bool mark)
    526 {
    527     moveCursor(m_textLayout.nextCursorPosition(m_cursor, QTextLayout::SkipWords), mark);
    528 }
    529 
    530 inline void QLineControl::home(bool mark)
    531 {
    532     moveCursor(0, mark);
    533 }
    534 
    535 inline void QLineControl::end(bool mark)
    536 {
    537     moveCursor(text().length(), mark);
    538 }
    539 
    540 inline void QLineControl::cursorWordBackward(bool mark)
    541 {
    542     moveCursor(m_textLayout.previousCursorPosition(m_cursor, QTextLayout::SkipWords), mark);
    543 }
    544 
    545 inline qreal QLineControl::cursorToX(int cursor) const
    546 {
    547     return m_textLayout.lineAt(0).cursorToX(cursor);
    548 }
    549 
    550 inline qreal QLineControl::cursorToX() const
    551 {
    552     int cursor = m_cursor;
    553     if (m_preeditCursor != -1)
    554         cursor += m_preeditCursor;
    555     return cursorToX(cursor);
    556 }
    557 
    558 inline bool QLineControl::isReadOnly() const
    559 {
    560     return m_readOnly;
    561 }
    562 
    563 inline void QLineControl::setReadOnly(bool enable)
    564 {
    565     m_readOnly = enable;
    566 }
    567 
    568 inline QString QLineControl::text() const
    569 {
    570     QString res = m_maskData ? stripString(m_text) : m_text;
    571     return (res.isNull() ? QString::fromLatin1("") : res);
    572 }
    573 
    574 inline void QLineControl::setText(const QString &txt)
    575 {
    576     internalSetText(txt, -1, false);
    577 }
    578 
    579 inline QString QLineControl::displayText() const
    580 {
    581     return m_textLayout.text();
    582 }
    583 
    584 inline void QLineControl::deselect()
    585 {
    586     internalDeselect();
    587     finishChange();
    588 }
    589 
    590 inline void QLineControl::selectAll()
    591 {
    592     m_selstart = m_selend = m_cursor = 0;
    593     moveCursor(m_text.length(), true);
    594 }
    595 
    596 inline void QLineControl::undo()
    597 {
    598     internalUndo();
    599     finishChange(-1, true);
    600 }
    601 
    602 inline void QLineControl::redo()
    603 {
    604     internalRedo();
    605     finishChange();
    606 }
    607 
    608 inline uint QLineControl::echoMode() const
    609 {
    610     return m_echoMode;
    611 }
    612 
    613 inline void QLineControl::setEchoMode(uint mode)
    614 {
    615     m_echoMode = mode;
    616     m_passwordEchoEditing = false;
    617     updateDisplayText();
    618 }
    619 
    620 inline void QLineControl::setMaxLength(int maxLength)
    621 {
    622     if (m_maskData)
    623         return;
    624     m_maxLength = maxLength;
    625     setText(m_text);
    626 }
    627 
    628 inline int QLineControl::maxLength() const
    629 {
    630     return m_maxLength;
    631 }
    632 
    633 #ifndef QT_NO_VALIDATOR
    634 inline const QValidator *QLineControl::validator() const
    635 {
    636     return m_validator;
    637 }
    638 
    639 inline void QLineControl::setValidator(const QValidator *v)
    640 {
    641     m_validator = const_cast<QValidator*>(v);
    642 }
    643 #endif
    644 
    645 #ifndef QT_NO_COMPLETER
    646 inline QCompleter *QLineControl::completer() const
    647 {
    648     return m_completer;
    649 }
    650 
    651 /* Note that you must set the widget for the completer seperately */
    652 inline void QLineControl::setCompleter(const QCompleter* c)
    653 {
    654     m_completer = const_cast<QCompleter*>(c);
    655 }
    656 #endif
    657 
    658 inline void QLineControl::setCursorPosition(int pos)
    659 {
    660     if (pos < 0)
    661         pos = 0;
    662     if (pos <= m_text.length())
    663         moveCursor(pos);
    664 }
    665 
    666 inline int QLineControl::cursorPosition() const
    667 {
    668     return m_cursor;
    669 }
    670 
    671 inline bool QLineControl::hasAcceptableInput() const
    672 {
    673     return hasAcceptableInput(m_text);
    674 }
    675 
    676 inline QString QLineControl::inputMask() const
    677 {
    678     return m_maskData ? m_inputMask + QLatin1Char(';') + m_blank : QString();
    679 }
    680 
    681 inline void QLineControl::setInputMask(const QString &mask)
    682 {
    683     parseInputMask(mask);
    684     if (m_maskData)
    685         moveCursor(nextMaskBlank(0));
    686 }
    687 
    688 // input methods
    689 #ifndef QT_NO_IM
    690 inline bool QLineControl::composeMode() const
    691 {
    692     return !m_textLayout.preeditAreaText().isEmpty();
    693 }
    694 
    695 inline void QLineControl::setPreeditArea(int cursor, const QString &text)
    696 {
    697     m_textLayout.setPreeditArea(cursor, text);
    698 }
    699 #endif
    700 
    701 inline QString QLineControl::preeditAreaText() const
    702 {
    703     return m_textLayout.preeditAreaText();
    704 }
    705 
    706 inline bool QLineControl::passwordEchoEditing() const
    707 {
    708     return m_passwordEchoEditing;
    709 }
    710 
    711 inline QChar QLineControl::passwordCharacter() const
    712 {
    713     return m_passwordCharacter;
    714 }
    715 
    716 inline void QLineControl::setPasswordCharacter(const QChar &character)
    717 {
    718     m_passwordCharacter = character;
    719     updateDisplayText();
    720 }
    721 
    722 inline Qt::LayoutDirection QLineControl::layoutDirection() const
    723 {
    724     return m_layoutDirection;
    725 }
    726 
    727 inline void QLineControl::setLayoutDirection(Qt::LayoutDirection direction)
    728 {
    729     if (direction != m_layoutDirection) {
    730         m_layoutDirection = direction;
    731         updateDisplayText();
    732     }
    733 }
    734 
    735 inline void QLineControl::setFont(const QFont &font)
    736 {
    737     m_textLayout.setFont(font);
    738     updateDisplayText();
    739 }
    740 
    741 inline int QLineControl::cursorBlinkPeriod() const
    742 {
    743     return m_blinkPeriod;
    744 }
    745 
    746 inline QString QLineControl::cancelText() const
    747 {
    748     return m_cancelText;
    749 }
    750 
    751 inline void QLineControl::setCancelText(const QString &text)
    752 {
    753     m_cancelText = text;
    754 }
    755 
    756 inline const QPalette & QLineControl::palette() const
    757 {
    758     return m_palette;
    759 }
    760 
    761 inline void QLineControl::setPalette(const QPalette &p)
    762 {
    763     m_palette = p;
    764 }
    765 
    766445QT_END_NAMESPACE
    767446
Note: See TracChangeset for help on using the changeset viewer.