summaryrefslogtreecommitdiffstats
path: root/src/declarative
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2009-07-08 08:27:58 (GMT)
committerAlan Alpert <alan.alpert@nokia.com>2009-07-08 08:27:58 (GMT)
commit5be447a0f1a7cdd69fc94543e9b83038cc2b77b3 (patch)
tree389d915f07195d578c6a6c333132fd7ab6e362b6 /src/declarative
parent6259c9fbc4719994bd491271eba5c47143df67c6 (diff)
downloadQt-5be447a0f1a7cdd69fc94543e9b83038cc2b77b3.zip
Qt-5be447a0f1a7cdd69fc94543e9b83038cc2b77b3.tar.gz
Qt-5be447a0f1a7cdd69fc94543e9b83038cc2b77b3.tar.bz2
Implement selectedText, selectionStart and selectionEnd properties.
Still to go: Setting the selectionStart/End properties doesn't work and there are no autotests.
Diffstat (limited to 'src/declarative')
-rw-r--r--src/declarative/fx/qfxtextedit.cpp117
-rw-r--r--src/declarative/fx/qfxtextedit.h19
-rw-r--r--src/declarative/fx/qfxtextedit_p.h8
3 files changed, 140 insertions, 4 deletions
diff --git a/src/declarative/fx/qfxtextedit.cpp b/src/declarative/fx/qfxtextedit.cpp
index 9854906..d3b6fab 100644
--- a/src/declarative/fx/qfxtextedit.cpp
+++ b/src/declarative/fx/qfxtextedit.cpp
@@ -544,6 +544,80 @@ void QFxTextEdit::loadCursorDelegate()
}
/*!
+ \qmlproperty int TextEdit::selectionStart
+
+ The cursor position before the first character in the current selection.
+ Setting this and selectionEnd allows you to specify a selection in the
+ text edit.
+
+ Note that if selectionStart == selectionEnd then there is no current
+ selection.
+
+ \sa selectionEnd, cursorPosition, selectedText
+*/
+int QFxTextEdit::selectionStart() const
+{
+ Q_D(const QFxTextEdit);
+ return d->control->textCursor().selectionStart();
+}
+
+void QFxTextEdit::setSelectionStart(int s)
+{
+ Q_D(QFxTextEdit);
+ if(d->lastSelectionStart == s)
+ return;
+ d->lastSelectionStart = s;
+ d->updateSelection();// Will emit the relevant signals
+}
+
+/*!
+ \qmlproperty int TextEdit::selectionEnd
+
+ The cursor position after the last character in the current selection.
+ Setting this and selectionStart allows you to specify a selection in the
+ text edit.
+
+ Note that if selectionStart == selectionEnd then there is no current
+ selection.
+
+ \sa selectionStart, cursorPosition, selectedText
+*/
+int QFxTextEdit::selectionEnd() const
+{
+ Q_D(const QFxTextEdit);
+ return d->control->textCursor().selectionEnd();
+}
+
+void QFxTextEdit::setSelectionEnd(int s)
+{
+ Q_D(QFxTextEdit);
+ if(d->lastSelectionEnd == s)
+ return;
+ d->lastSelectionEnd = s;
+ d->updateSelection();// Will emit the relevant signals
+}
+
+/*!
+ \qmlproperty string TextEdit::selectedText
+
+ This read-only property provides the text currently selected in the
+ text edit.
+
+ It is equivalent to the following snippet, but is faster and easier
+ to use.
+ \code
+ //myTextEdit is the id of the TextEdit
+ myTextEdit.text.toString().substring(myTextEdit.selectionStart,
+ myTextEdit.selectionEnd);
+ \endcode
+*/
+QString QFxTextEdit::selectedText() const
+{
+ Q_D(const QFxTextEdit);
+ return d->control->textCursor().selectedText();
+}
+
+/*!
\qmlproperty bool TextEdit::focusOnPress
Whether the TextEdit should gain focus on a mouse press. By default this is
@@ -969,6 +1043,8 @@ void QFxTextEditPrivate::init()
QObject::connect(control, SIGNAL(updateRequest(QRectF)), q, SLOT(updateImgCache(QRectF)));
QObject::connect(control, SIGNAL(textChanged()), q, SLOT(q_textChanged()));
+ QObject::connect(control, SIGNAL(selectionChanged()), q, SIGNAL(selectionChanged()));
+ QObject::connect(control, SIGNAL(selectionChanged()), q, SLOT(updateSelectionMarkers()));
QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SIGNAL(cursorPositionChanged()));
document = control->document();
@@ -996,6 +1072,47 @@ void QFxTextEdit::moveCursorDelegate()
d->cursor->setY(cursorRect.y());
}
+void QFxTextEditPrivate::updateSelection()
+{
+ Q_Q(QFxTextEdit);
+ bool startChange = (lastSelectionStart != control->textCursor().selectionStart());
+ bool endChange = (lastSelectionEnd != control->textCursor().selectionEnd());
+ if(startChange){
+ //### Does this generate spurious intermediate signals?
+ control->textCursor().setPosition(lastSelectionStart, QTextCursor::MoveAnchor);
+ control->textCursor().setPosition(lastSelectionEnd, QTextCursor::KeepAnchor);
+ }else if(endChange){
+ int n = lastSelectionEnd - control->textCursor().selectionEnd();
+ if(n > 0)
+ control->textCursor().movePosition(QTextCursor::NextCharacter,
+ QTextCursor::KeepAnchor, n);
+ else
+ control->textCursor().movePosition(QTextCursor::PreviousCharacter,
+ QTextCursor::KeepAnchor, -n);
+ }
+ if(startChange)
+ q->selectionStartChanged();
+ if(endChange)
+ q->selectionEndChanged();
+ startChange = (lastSelectionStart != control->textCursor().selectionStart());
+ endChange = (lastSelectionEnd != control->textCursor().selectionEnd());
+ if(startChange || endChange)
+ qWarning() << "QFxTextEditPrivate::updateSelection() has failed you.";
+}
+
+void QFxTextEdit::updateSelectionMarkers()
+{
+ Q_D(QFxTextEdit);
+ if(d->lastSelectionStart != d->control->textCursor().selectionStart()){
+ d->lastSelectionStart = d->control->textCursor().selectionStart();
+ emit selectionStartChanged();
+ }
+ if(d->lastSelectionEnd != d->control->textCursor().selectionEnd()){
+ d->lastSelectionEnd = d->control->textCursor().selectionEnd();
+ emit selectionEndChanged();
+ }
+}
+
//### we should perhaps be a bit smarter here -- depending on what has changed, we shouldn't
// need to do all the calculations each time
void QFxTextEdit::updateSize()
diff --git a/src/declarative/fx/qfxtextedit.h b/src/declarative/fx/qfxtextedit.h
index b29993e..80636e2 100644
--- a/src/declarative/fx/qfxtextedit.h
+++ b/src/declarative/fx/qfxtextedit.h
@@ -78,8 +78,11 @@ class Q_DECLARATIVE_EXPORT QFxTextEdit : public QFxPaintedItem
Q_PROPERTY(TextFormat textFormat READ textFormat WRITE setTextFormat)
Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
Q_PROPERTY(bool cursorVisible READ isCursorVisible WRITE setCursorVisible)
- Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition)
- Q_PROPERTY(QmlComponent* cursorDelegate READ cursorDelegate WRITE setCursorDelegate);
+ Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
+ Q_PROPERTY(QmlComponent* cursorDelegate READ cursorDelegate WRITE setCursorDelegate)
+ Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged)
+ Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
+ Q_PROPERTY(QString selectedText READ selectedText NOTIFY selectionChanged)
Q_PROPERTY(bool focusOnPress READ focusOnPress WRITE setFocusOnPress)
Q_PROPERTY(bool preserveSelection READ preserveSelection WRITE setPreserveSelection)
Q_PROPERTY(qreal textMargin READ textMargin WRITE setTextMargin)
@@ -141,6 +144,14 @@ public:
QmlComponent* cursorDelegate() const;
void setCursorDelegate(QmlComponent*);
+ int selectionStart() const;
+ void setSelectionStart(int);
+
+ int selectionEnd() const;
+ void setSelectionEnd(int);
+
+ QString selectedText() const;
+
bool focusOnPress() const;
void setFocusOnPress(bool on);
@@ -176,6 +187,9 @@ public:
Q_SIGNALS:
void textChanged(const QString &);
void cursorPositionChanged();
+ void selectionStartChanged();
+ void selectionEndChanged();
+ void selectionChanged();
public Q_SLOTS:
void selectAll();
@@ -184,6 +198,7 @@ private Q_SLOTS:
void fontChanged();
void updateImgCache(const QRectF &rect);
void q_textChanged();
+ void updateSelectionMarkers();
void moveCursorDelegate();
void loadCursorDelegate();
diff --git a/src/declarative/fx/qfxtextedit_p.h b/src/declarative/fx/qfxtextedit_p.h
index f92dd60..b7d667e 100644
--- a/src/declarative/fx/qfxtextedit_p.h
+++ b/src/declarative/fx/qfxtextedit_p.h
@@ -69,8 +69,9 @@ class QFxTextEditPrivate : public QFxPaintedItemPrivate
public:
QFxTextEditPrivate()
: font(0), color("black"), imgDirty(true), hAlign(QFxTextEdit::AlignLeft), vAlign(QFxTextEdit::AlignTop),
- dirty(false), wrap(false), richText(false), cursorVisible(false), focusOnPress(false), preserveSelection(true),
- textMargin(0.0), cursor(0), cursorComponent(0), format(QFxTextEdit::AutoText), document(0)
+ dirty(false), wrap(false), richText(false), cursorVisible(false), focusOnPress(false),
+ preserveSelection(true), textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0),
+ cursor(0), cursorComponent(0), format(QFxTextEdit::AutoText), document(0)
{
}
@@ -78,6 +79,7 @@ public:
void updateDefaultTextOption();
void relayoutDocument();
+ void updateSelection();
QString text;
QmlFont font;
@@ -101,6 +103,8 @@ public:
bool focusOnPress;
bool preserveSelection;
qreal textMargin;
+ int lastSelectionStart;
+ int lastSelectionEnd;
QmlComponent* cursorComponent;
QFxItem* cursor;
QFxTextEdit::TextFormat format;