diff options
author | David Faure <faure@kde.org> | 2010-02-02 13:29:11 (GMT) |
---|---|---|
committer | Leonardo Sobral Cunha <leo.cunha@nokia.com> | 2010-02-02 13:29:11 (GMT) |
commit | 42e181a6feba241997b041a2a58903f308264559 (patch) | |
tree | d6b0994ce1f73957f0cfea310e21b8ed1ad1ab8c /src/gui/widgets | |
parent | e9b0c9a1d25a2260bd7c0e7ead840cacb31ce424 (diff) | |
download | Qt-42e181a6feba241997b041a2a58903f308264559.zip Qt-42e181a6feba241997b041a2a58903f308264559.tar.gz Qt-42e181a6feba241997b041a2a58903f308264559.tar.bz2 |
QLabel: add setSelection, hasSelectedText, selectedText, selectionStart.
When using TextSelectableByMouse or TextSelectableByKeyboard, this
allows to programmatically control the selection. Needed in KDE's
KSqueezedTextLabel, so that mouseReleaseEvent can get the selection
and copy the un-squeezed text instead of the squeezed one.
Merge-request: 454
Reviewed-by: Leonardo Sobral Cunha <leo.cunha@nokia.com>
Diffstat (limited to 'src/gui/widgets')
-rw-r--r-- | src/gui/widgets/qlabel.cpp | 89 | ||||
-rw-r--r-- | src/gui/widgets/qlabel.h | 7 |
2 files changed, 96 insertions, 0 deletions
diff --git a/src/gui/widgets/qlabel.cpp b/src/gui/widgets/qlabel.cpp index 8428ad7..c779312 100644 --- a/src/gui/widgets/qlabel.cpp +++ b/src/gui/widgets/qlabel.cpp @@ -781,6 +781,95 @@ Qt::TextInteractionFlags QLabel::textInteractionFlags() const return d->textInteractionFlags; } +/*! + Selects text from position \a start and for \a length characters. + + \sa selectedText() + + \bold{Note:} The textInteractionFlags set on the label need to include + either TextSelectableByMouse or TextSelectableByKeyboard. + + \since 4.7 +*/ +void QLabel::setSelection(int start, int length) +{ + Q_D(QLabel); + if (d->control) { + d->ensureTextPopulated(); + QTextCursor cursor = d->control->textCursor(); + cursor.setPosition(start); + cursor.setPosition(start + length, QTextCursor::KeepAnchor); + d->control->setTextCursor(cursor); + } +} + +/*! + \property QLabel::hasSelectedText + \brief whether there is any text selected + + hasSelectedText() returns true if some or all of the text has been + selected by the user; otherwise returns false. + + By default, this property is false. + + \sa selectedText() + + \bold{Note:} The textInteractionFlags set on the label need to include + either TextSelectableByMouse or TextSelectableByKeyboard. + + \since 4.7 +*/ +bool QLabel::hasSelectedText() const +{ + Q_D(const QLabel); + if (d->control) + return d->control->textCursor().hasSelection(); + return false; +} + +/*! + \property QLabel::selectedText + \brief the selected text + + If there is no selected text this property's value is + an empty string. + + By default, this property contains an empty string. + + \sa hasSelectedText() + + \bold{Note:} The textInteractionFlags set on the label need to include + either TextSelectableByMouse or TextSelectableByKeyboard. + + \since 4.7 +*/ +QString QLabel::selectedText() const +{ + Q_D(const QLabel); + if (d->control) + return d->control->textCursor().selectedText(); + return QString(); +} + +/*! + selectionStart() returns the index of the first selected character in the + label or -1 if no text is selected. + + \sa selectedText() + + \bold{Note:} The textInteractionFlags set on the label need to include + either TextSelectableByMouse or TextSelectableByKeyboard. + + \since 4.7 +*/ +int QLabel::selectionStart() const +{ + Q_D(const QLabel); + if (d->control && d->control->textCursor().hasSelection()) + return d->control->textCursor().selectionStart(); + return -1; +} + /*!\reimp */ QSize QLabel::sizeHint() const diff --git a/src/gui/widgets/qlabel.h b/src/gui/widgets/qlabel.h index d916078..54babb1 100644 --- a/src/gui/widgets/qlabel.h +++ b/src/gui/widgets/qlabel.h @@ -65,6 +65,8 @@ class Q_GUI_EXPORT QLabel : public QFrame Q_PROPERTY(int indent READ indent WRITE setIndent) Q_PROPERTY(bool openExternalLinks READ openExternalLinks WRITE setOpenExternalLinks) Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags) + Q_PROPERTY(bool hasSelectedText READ hasSelectedText) + Q_PROPERTY(QString selectedText READ selectedText) public: explicit QLabel(QWidget *parent=0, Qt::WindowFlags f=0); @@ -111,6 +113,11 @@ public: void setTextInteractionFlags(Qt::TextInteractionFlags flags); Qt::TextInteractionFlags textInteractionFlags() const; + void setSelection(int, int); + bool hasSelectedText() const; + QString selectedText() const; + int selectionStart() const; + public Q_SLOTS: void setText(const QString &); void setPixmap(const QPixmap &); |