From b9f484ae5d415d95df897e18059c054527111374 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Mon, 1 Jun 2009 13:25:08 +1000 Subject: Add textMargin property and more flexible key handling to TextEdit --- src/declarative/fx/qfxtextedit.cpp | 34 ++++++++++++++++++++++++++++++++-- src/declarative/fx/qfxtextedit.h | 5 ++++- src/declarative/fx/qfxtextedit_p.h | 3 ++- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/declarative/fx/qfxtextedit.cpp b/src/declarative/fx/qfxtextedit.cpp index 1c70abf..c7a7700 100644 --- a/src/declarative/fx/qfxtextedit.cpp +++ b/src/declarative/fx/qfxtextedit.cpp @@ -49,6 +49,7 @@ #endif #include +#include "qfxevents_p.h" #include #include #include @@ -474,6 +475,21 @@ void QFxTextEdit::setPreserveSelection(bool on) d->preserveSelection = on; } +qreal QFxTextEdit::textMargin() const +{ + Q_D(const QFxTextEdit); + return d->textMargin; +} + +void QFxTextEdit::setTextMargin(qreal margin) +{ + Q_D(QFxTextEdit); + if (d->textMargin == margin) + return; + d->textMargin = margin; + d->document->setDocumentMargin(d->textMargin); +} + void QFxTextEdit::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) { @@ -659,6 +675,13 @@ Handles the given key \a event. void QFxTextEdit::keyPressEvent(QKeyEvent *event) { Q_D(QFxTextEdit); + //### experiment with allowing 'overrides' to key events + QFxKeyEvent ke(*event); + emit keyPress(&ke); + event->setAccepted(ke.isAccepted()); + if (event->isAccepted()) + return; + QTextCursor c = textCursor(); QTextCursor::MoveOperation op = QTextCursor::NoMove; if (event == QKeySequence::MoveToNextChar) { @@ -688,6 +711,13 @@ Handles the given key \a event. void QFxTextEdit::keyReleaseEvent(QKeyEvent *event) { Q_D(QFxTextEdit); + //### experiment with allowing 'overrides' to key events + QFxKeyEvent ke(*event); + emit keyRelease(&ke); + event->setAccepted(ke.isAccepted()); + if (event->isAccepted()) + return; + d->control->processEvent(event, QPointF(0, 0)); } @@ -821,7 +851,7 @@ void QFxTextEditPrivate::init() document = control->document(); document->setDefaultFont(font.font()); - document->setDocumentMargin(0); + document->setDocumentMargin(textMargin); document->setUndoRedoEnabled(false); // flush undo buffer. document->setUndoRedoEnabled(true); updateDefaultTextOption(); @@ -851,7 +881,7 @@ void QFxTextEdit::updateSize() else if (d->vAlign == AlignVCenter) yoff = dy/2; } - setBaselineOffset(fm.ascent() + yoff); + setBaselineOffset(fm.ascent() + yoff + d->textMargin); if (!widthValid()) { int newWidth = (int)d->document->idealWidth(); d->document->setTextWidth(newWidth); // ### QTextDoc> Alignment will not work unless textWidth is set diff --git a/src/declarative/fx/qfxtextedit.h b/src/declarative/fx/qfxtextedit.h index 37bc327..e30b9ed 100644 --- a/src/declarative/fx/qfxtextedit.h +++ b/src/declarative/fx/qfxtextedit.h @@ -79,7 +79,7 @@ class Q_DECLARATIVE_EXPORT QFxTextEdit : public QFxPaintedItem Q_PROPERTY(bool cursorVisible READ isCursorVisible WRITE setCursorVisible) Q_PROPERTY(bool focusOnPress READ focusOnPress WRITE setFocusOnPress) Q_PROPERTY(bool preserveSelection READ preserveSelection WRITE setPreserveSelection) - Q_CLASSINFO("DefaultProperty", "text") + Q_PROPERTY(qreal textMargin READ textMargin WRITE setTextMargin) public: QFxTextEdit(QFxItem *parent=0); @@ -135,6 +135,9 @@ public: bool preserveSelection() const; void setPreserveSelection(bool on); + qreal textMargin() const; + void setTextMargin(qreal margin); + virtual void dump(int depth); virtual QString propertyInfo() const; diff --git a/src/declarative/fx/qfxtextedit_p.h b/src/declarative/fx/qfxtextedit_p.h index 9f26b10..f733a4c 100644 --- a/src/declarative/fx/qfxtextedit_p.h +++ b/src/declarative/fx/qfxtextedit_p.h @@ -70,7 +70,7 @@ 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), - format(QFxTextEdit::AutoText), document(0) + textMargin(0.0), format(QFxTextEdit::AutoText), document(0) { } @@ -99,6 +99,7 @@ public: bool cursorVisible; bool focusOnPress; bool preserveSelection; + qreal textMargin; QFxTextEdit::TextFormat format; QTextDocument *document; QTextControl *control; -- cgit v0.12