summaryrefslogtreecommitdiffstats
path: root/src/declarative/fx/qfxtextedit.cpp
blob: 35b11734ef494ee58882da8075cc4bc7dd9da1f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include <qfxtextedit.h>
#include "qfxtextedit_p.h"

#include <private/qtextcontrol_p.h>

#if defined(QFX_RENDER_OPENGL2)
#include "glbasicshaders.h"
#endif

#include <qfxperf.h>
#include <QTextLayout>
#include <QTextLine>
#include <QTextDocument>
#include <QGraphicsSceneMouseEvent>

#include <QDebug>


QT_BEGIN_NAMESPACE
QML_DEFINE_TYPE(QFxTextEdit, TextEdit);

/*!
    \qmlclass TextEdit
    \brief The TextEdit item allows you to add editable formatted text to a scene.

    It can display both plain and rich text. For example:

    \qml
TextEdit {
    id: edit
    text: "<b>Hello</b> <i>World!</i>"
    focus: true
    focusable: true
    font.family: "Helvetica"
    font.size: 20
    color: "blue"
    width: 240
}
    \endqml

    \image declarative-textedit.gif

    \sa Text
*/

/*!
    \internal
    \class QFxTextEdit
    \qmlclass TextEdit
    \ingroup group_coreitems

    \brief The QFxTextEdit class provides an editable formatted text item that you can add to a QFxView.

    It can display both plain and rich text.

    \image declarative-textedit.png

    A QFxTextEdit object can be instantiated in Qml using the tag \c &lt;TextEdit&gt;.
*/

/*!
    Constructs a new QFxTextEdit.
*/
QFxTextEdit::QFxTextEdit(QFxItem *parent)
: QFxPaintedItem(*(new QFxTextEditPrivate), parent)
{
    Q_D(QFxTextEdit);
    d->init();
}

/*!
\internal
*/
QFxTextEdit::QFxTextEdit(QFxTextEditPrivate &dd, QFxItem *parent)
    : QFxPaintedItem(dd, parent)
{
    Q_D(QFxTextEdit);
    d->init();
}

QString QFxTextEdit::text() const
{
    Q_D(const QFxTextEdit);

    if (d->richText)
        return d->document->toHtml();
    else
        return d->document->toPlainText();
}

/*!
    \qmlproperty font TextEdit::font

    Set the TextEdit's font attributes.  \c font.size sets the font's point size.
*/

/*!
    \qmlproperty string TextEdit::text
    \default

    The text to display.  If the text format is AutoText the text edit will
    automatically determine whether the text should be treated as
    rich text.  This determination is made using Qt::mightBeRichText().
*/

/*!
    \property QFxTextEdit::text
    \brief the text edit's text

    If the text format is set to AutoText the text edit will try to
    automatically determine whether the text should be treated as
    rich text.  This determination is made using Qt::mightBeRichText().

    \sa textFormat
*/

void QFxTextEdit::setText(const QString &text)
{
    Q_D(QFxTextEdit);
    d->text = text;
    d->richText = d->format == RichText || (d->format == AutoText && Qt::mightBeRichText(text));
    if (d->richText) {
        d->control->setHtml(text);
    } else {
        d->control->setPlainText(text);
    }
    q_textChanged();
    updateSize();
}

/*!
    \qmlproperty enumeration TextEdit::textFormat

    The way the text property should be displayed.

    Supported text formats are \c AutoText, \c PlainText and \c RichText.

    The default is AutoText.  If the text format is AutoText the text edit
    edit will automatically determine whether the text should be treated as
    rich text.  This determination is made using Qt::mightBeRichText().

    \table
    \row
    \o
    \qml
VerticalLayout {
    TextEdit {
        font.size: 24
        text: "<b>Hello</b> <i>World!</i>"
    }
    TextEdit {
        font.size: 24
        textFormat: "RichText"
        text: "<b>Hello</b> <i>World!</i>"
    }
    TextEdit {
        font.size: 24
        textFormat: "PlainText"
        text: "<b>Hello</b> <i>World!</i>"
    }
}
    \endqml
    \o \image declarative-textformat.png
    \endtable
*/

/*!
    \property QFxTextEdit::textFormat
    \brief this property describes how the text set on the text edit
    should be interpreted.

    Valid values are \c AutoText, \c PlainText and \c RichText.  The
    default value is \c AutoText, meaning the text edit will attempt
    to guess how the text should be interpreted using the
    Qt::mightBeRichText() function.

    \sa text
*/

QFxTextEdit::TextFormat QFxTextEdit::textFormat() const
{
    Q_D(const QFxTextEdit);
    return d->format;
}

void QFxTextEdit::setTextFormat(TextFormat format)
{
    Q_D(QFxTextEdit);
    if (format == d->format)
        return;
    bool wasRich = d->richText;
    d->richText = format == RichText || (format == AutoText && Qt::mightBeRichText(d->text));

    if (wasRich && !d->richText) {
        d->control->setPlainText(d->text);
        updateSize();
    } else if (!wasRich && d->richText) {
        d->control->setHtml(d->text);
        updateSize();
    }
    d->format = format;
}

/*!
    \property QFxTextEdit::font
    \brief the text edit's default font
*/

QmlFont *QFxTextEdit::font()
{
    Q_D(QFxTextEdit);
    return &(d->font);
}

/*!
    \qmlproperty color TextEdit::color

    The text color.

    \qml
// green text using hexadecimal notation
TextEdit { color: "#00FF00"; ...  }

// steelblue text using SVG color name
TextEdit { color: "steelblue"; ...  }
    \endqml
*/

/*!
    \property QFxTextEdit::color
    \brief the text edit's default text color
*/
QColor QFxTextEdit::color() const
{
    Q_D(const QFxTextEdit);
    return d->color;
}

void QFxTextEdit::setColor(const QColor &color)
{
    Q_D(QFxTextEdit);
    if (d->color == color)
        return;

    clearCache();
    d->color = color;
    QPalette pal = d->control->palette();
    pal.setColor(QPalette::Text, color);
    d->control->setPalette(pal);
    update();
}

/*!
    \qmlproperty color TextEdit::highlightColor

    The text highlight color, used behind selections.
*/

/*!
    \property QFxTextEdit::highlightColor
    \brief the text edit's default text highlight color
*/
QColor QFxTextEdit::highlightColor() const
{
    Q_D(const QFxTextEdit);
    return d->highlightColor;
}

void QFxTextEdit::setHighlightColor(const QColor &color)
{
    Q_D(QFxTextEdit);
    if (d->highlightColor == color)
        return;

    clearCache();
    d->highlightColor = color;
    QPalette pal = d->control->palette();
    pal.setColor(QPalette::Highlight, color);
    d->control->setPalette(pal);
    update();
}

/*!
    \qmlproperty enumeration TextEdit::hAlign
    \qmlproperty enumeration TextEdit::vAlign

    Sets the horizontal and vertical alignment of the text within the TextEdit items
    width and height.  By default, the text is top-left aligned.

    The valid values for \c hAlign are \c AlignLeft, \c AlignRight and 
    \c AlignHCenter.  The valid values for \c vAlign are \c AlignTop, \c AlignBottom 
    and \c AlignVCenter.
*/

/*!
    \property QFxTextEdit::hAlign
    \brief the horizontal alignment of the text.

    Valid values are \c AlignLeft, \c AlignRight, and \c AlignHCenter. The default value is \c AlignLeft.
*/
QFxTextEdit::HAlignment QFxTextEdit::hAlign() const
{
    Q_D(const QFxTextEdit);
    return d->hAlign;
}

void QFxTextEdit::setHAlign(QFxTextEdit::HAlignment alignment)
{
    Q_D(QFxTextEdit);
    if (alignment == d->hAlign)
        return;
    d->hAlign = alignment;
    d->updateDefaultTextOption();
    updateSize();
}

/*!
    \property QFxTextEdit::vAlign
    \brief the vertical alignment of the text.

    Valid values are \c AlignTop, \c AlignBottom, and \c AlignVCenter. The default value is \c AlignTop.
*/
QFxTextEdit::VAlignment QFxTextEdit::vAlign() const
{
    Q_D(const QFxTextEdit);
    return d->vAlign;
}

void QFxTextEdit::setVAlign(QFxTextEdit::VAlignment alignment)
{
    Q_D(QFxTextEdit);
    if (alignment == d->vAlign)
        return;
    d->vAlign = alignment;
    d->updateDefaultTextOption();
    updateSize();
}

bool QFxTextEdit::wrap() const
{
    Q_D(const QFxTextEdit);
    return d->wrap;
}

/*!
    \qmlproperty bool TextEdit::wrap

    Set this property to wrap the text to the TextEdit item's width.
    The text will only wrap if an explicit width has been set.

    Wrapping is done on word boundaries (i.e. it is a "word-wrap"). Wrapping is off by default.
*/

/*!
    \property QFxTextEdit::wrap
    \brief If true the text edit wraps text based on the width of the
    text edit.
*/
void QFxTextEdit::setWrap(bool w)
{
    Q_D(QFxTextEdit);
    if (w == d->wrap)
        return;
    d->wrap = w;
    d->updateDefaultTextOption();
    updateSize();
}

/*!
    \property QFxTextEdit::cursorVisible
    \brief If true the text edit shows a cursor.

    This property is set and unset when the text edit gets focus, but it can also
    be set directly (useful, for example, if a KeyProxy might forward keys to it).
*/
bool QFxTextEdit::isCursorVisible() const
{
    Q_D(const QFxTextEdit);
    return d->cursorVisible;
}

void QFxTextEdit::setCursorVisible(bool on)
{
    Q_D(QFxTextEdit);
    if (d->cursorVisible == on)
        return;
    d->cursorVisible = on;
    QFocusEvent focusEvent(on ? QEvent::FocusIn : QEvent::FocusOut);
    d->control->processEvent(&focusEvent, QPointF(0, 0));
}

/*!
    \qmlproperty bool TextEdit::focusOnPress

    Whether the TextEdit should gain focus on a mouse press. By default this is
    set to false;
*/
bool QFxTextEdit::focusOnPress() const
{
    Q_D(const QFxTextEdit);
    return d->focusOnPress;
}

void QFxTextEdit::setFocusOnPress(bool on)
{
    Q_D(QFxTextEdit);
    if (d->focusOnPress == on)
        return;
    d->focusOnPress = on;
}

void QFxTextEdit::geometryChanged(const QRectF &newGeometry, 
                                  const QRectF &oldGeometry)
{
    if (newGeometry.width() != oldGeometry.width())
        updateSize();
    QFxPaintedItem::geometryChanged(newGeometry, oldGeometry);
}

/*!
    \internal
*/
void QFxTextEdit::dump(int depth)
{
    QByteArray ba(depth * 4, ' ');
    qWarning() << ba.constData() << propertyInfo();
    QFxPaintedItem::dump(depth);
}

/*!
   \internal
*/
QString QFxTextEdit::propertyInfo() const
{
    Q_D(const QFxTextEdit);
    return QChar(QLatin1Char('\"')) + d->text + QChar(QLatin1Char('\"'));
}

/*!
    Ensures any delayed caching or data loading the class
    needs to performed is complete.
*/
void QFxTextEdit::componentComplete()
{
    Q_D(QFxTextEdit);
    QFxPaintedItem::componentComplete();
    if (d->dirty) {
        updateSize();
        d->dirty = false;
    }
}

/*!
    \qmlproperty bool TextEdit::readOnly

    Whether the user an interact with the TextEdit item.  If this
    property is set to true the text cannot be edited by user interaction.

    By default this property is false.
*/

/*!
    \property QFxTextEdit::readOnly
    \brief If this property is true the text can not be edited by user interaction.

    Changing this property will modify the text interaction flags.  If
    you require more specific control about how user interaction
    with the text edit is handled, use setTextInteractionFlags() instead.

    \sa setTextInteractionFlags()
*/
void QFxTextEdit::setReadOnly(bool r)
{
    Q_D(QFxTextEdit);

    Qt::TextInteractionFlags flags = Qt::NoTextInteraction;
    if (r) {
        flags = Qt::TextSelectableByMouse;
    } else {
        flags = Qt::TextEditorInteraction;
    }
    d->control->setTextInteractionFlags(flags);
    if (!r)
        d->control->moveCursor(QTextCursor::End);
}

bool QFxTextEdit::isReadOnly() const
{
    Q_D(const QFxTextEdit);
    return !(d->control->textInteractionFlags() & Qt::TextEditable);
}

/*!
    Sets how the text edit should interact with user input to the given
    \a flags.
*/
void QFxTextEdit::setTextInteractionFlags(Qt::TextInteractionFlags flags)
{
    Q_D(QFxTextEdit);
    d->control->setTextInteractionFlags(flags);
}

/*!
    Returns the flags specifying how the text edit should interact
    with user input.
*/
Qt::TextInteractionFlags QFxTextEdit::textInteractionFlags() const
{
    Q_D(const QFxTextEdit);
    return d->control->textInteractionFlags();
}

/*!
    Returns the cursor for the point at the given \a pos on the 
    text edit.
*/
QTextCursor QFxTextEdit::cursorForPosition(const QPoint &pos) const
{
    Q_D(const QFxTextEdit);
    return d->control->cursorForPosition(pos);
}

/*!
    Returns the rectangle where the given text \a cursor is rendered
    within the text edit.
*/
QRect QFxTextEdit::cursorRect(const QTextCursor &cursor) const
{
    Q_D(const QFxTextEdit);
    if (cursor.isNull())
        return QRect();

    return d->control->cursorRect(cursor).toRect();
}

/*!
    Returns the rectangle where the text cursor is rendered
    within the text edit.
*/
QRect QFxTextEdit::cursorRect() const
{
    Q_D(const QFxTextEdit);
    return d->control->cursorRect().toRect();
}


/*!
    Sets the text cursor for the text edit to the given \a cursor.
*/
void QFxTextEdit::setTextCursor(const QTextCursor &cursor)
{
    Q_D(QFxTextEdit);
    d->control->setTextCursor(cursor);
}

/*!
    Returns the text cursor for the text edit.
*/
QTextCursor QFxTextEdit::textCursor() const
{
    Q_D(const QFxTextEdit);
    return d->control->textCursor();
}

/*!
Moves the cursor by performing the given \a operation.

If \a mode is QTextCursor::KeepAnchor, the cursor selects the text it moves over. This is the same effect that the user achieves when they hold down the Shift key and move the cursor with the cursor keys.
*/
void QFxTextEdit::moveCursor(QTextCursor::MoveOperation operation, QTextCursor::MoveMode mode)
{
    Q_D(QFxTextEdit);
    d->control->moveCursor(operation, mode);
}

/*!
\overload
Handles the given key \a event.
*/
void QFxTextEdit::keyPressEvent(QKeyEvent *event)
{
    Q_D(QFxTextEdit);
    QTextCursor c = textCursor();
    QTextCursor::MoveOperation op = QTextCursor::NoMove;
    if (event == QKeySequence::MoveToNextChar) {
        op = QTextCursor::Right;
    } else if (event == QKeySequence::MoveToPreviousChar) {
        op = QTextCursor::Left;
    } else if (event == QKeySequence::MoveToNextWord) {
        op = QTextCursor::WordRight;
    } else if (event == QKeySequence::MoveToPreviousWord) {
        op = QTextCursor::WordLeft;
    } else if (event == QKeySequence::MoveToNextLine) {
        op = QTextCursor::Down;
    } else if (event == QKeySequence::MoveToPreviousLine) {
        op = QTextCursor::Up;
    }

    if (op != QTextCursor::NoMove && !c.movePosition(op))
        event->ignore();
    else
        d->control->processEvent(event, QPointF(0, 0));
}

/*!
\overload
Handles the given key \a event.
*/
void QFxTextEdit::keyReleaseEvent(QKeyEvent *event)
{
    Q_D(QFxTextEdit);
    d->control->processEvent(event, QPointF(0, 0));
}

/*!
    \overload
    Handles changing of the focus property.  Focus is applied to the control
    even if the edit does not have active focus.  This is because things
    like KeyProxy can give the behavior of focus even when activeFocus isn't
    true.
*/
void QFxTextEdit::focusChanged(bool hasFocus)
{
    Q_D(QFxTextEdit);
    setCursorVisible(hasFocus);
}

/*!
    Causes all text to be selected.
*/
void QFxTextEdit::selectAll()
{
    Q_D(QFxTextEdit);
    d->control->selectAll();
}

static QMouseEvent *sceneMouseEventToMouseEvent(QGraphicsSceneMouseEvent *e)
{
    QEvent::Type t;
    switch(e->type()) {
    default:
    case QEvent::GraphicsSceneMousePress:
        t = QEvent::MouseButtonPress;
        break;
    case QEvent::GraphicsSceneMouseRelease:
        t = QEvent::MouseButtonRelease;
        break;
    case QEvent::GraphicsSceneMouseMove:
        t = QEvent::MouseMove;
        break;
    case QGraphicsSceneEvent::GraphicsSceneMouseDoubleClick:
        t = QEvent::MouseButtonDblClick;
        break;
    }

    QMouseEvent *me = new QMouseEvent(t, e->pos().toPoint(), e->button(), e->buttons(), 0);
    return me;
}

/*!
\overload
Handles the given mouse \a event.
*/
void QFxTextEdit::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QFxTextEdit);
    if (d->focusOnPress)
        setFocus(true);
    QMouseEvent *me = sceneMouseEventToMouseEvent(event);
    d->control->processEvent(me, QPointF(0, 0));
    event->setAccepted(me->isAccepted());
    delete me;
    if (!event->isAccepted())
        QFxPaintedItem::mousePressEvent(event);
}

/*!
\overload
Handles the given mouse \a event.
*/
void QFxTextEdit::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QFxTextEdit);
    QMouseEvent *me = sceneMouseEventToMouseEvent(event);
    d->control->processEvent(me, QPointF(0, 0));
    event->setAccepted(me->isAccepted());
    delete me;
    if (!event->isAccepted())
        QFxPaintedItem::mousePressEvent(event);
}

/*!
\overload
Handles the given mouse \a event.
*/
void QFxTextEdit::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    Q_D(QFxTextEdit);
    QMouseEvent *me = sceneMouseEventToMouseEvent(event);
    d->control->processEvent(me, QPointF(0, 0));
    event->setAccepted(me->isAccepted());
    delete me;
    if (!event->isAccepted())
        QFxPaintedItem::mousePressEvent(event);
}

/*!
\overload
Handles the given input method \a event.
*/
void QFxTextEdit::inputMethodEvent(QInputMethodEvent *event)
{
    Q_D(QFxTextEdit);
    d->control->processEvent(event, QPointF(0, 0));
}

/*!
\overload
Returns the value of the given \a property.
*/
QVariant QFxTextEdit::inputMethodQuery(Qt::InputMethodQuery property) const
{
    Q_D(const QFxTextEdit);
    return d->control->inputMethodQuery(property);
}

/*!
Draws the contents of the text edit using the given \a painter within
the given \a bounds.
*/
void QFxTextEdit::drawContents(QPainter *painter, const QRect &bounds)
{
    Q_D(QFxTextEdit);

    painter->setRenderHint(QPainter::TextAntialiasing, true);

    d->control->drawContents(painter, bounds);
}

/*!
This signal is emitted when the font of the item changes.
*/
void QFxTextEdit::fontChanged()
{
    Q_D(QFxTextEdit);
    clearCache();
    d->document->setDefaultFont(d->font.font());
    updateSize();
    emit update();
}

void QFxTextEdit::updateImgCache(const QRectF &r)
{
    dirtyCache(r.toRect());
    emit update();
}

void QFxTextEditPrivate::init()
{
    Q_Q(QFxTextEdit);

    q->setSmooth(true);
    q->setAcceptedMouseButtons(Qt::LeftButton);
    q->setOptions(QFxTextEdit::AcceptsInputMethods | QFxTextEdit::SimpleItem
        | QFxTextEdit::HasContents | QFxTextEdit::MouseEvents);

    QObject::connect(&font, SIGNAL(updated()), q, SLOT(fontChanged()));

    control = new QTextControl(q);

    QObject::connect(control, SIGNAL(updateRequest(QRectF)), q, SLOT(updateImgCache(QRectF)));

    QObject::connect(control, SIGNAL(textChanged()), q, SLOT(q_textChanged()));
    QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SIGNAL(cursorPositionChanged()));

    document = control->document();
    document->setDefaultFont(font.font());
    document->setDocumentMargin(0);
    document->setUndoRedoEnabled(false); // flush undo buffer.
    document->setUndoRedoEnabled(true);
    updateDefaultTextOption();
}

void QFxTextEdit::q_textChanged()
{
    emit textChanged(text());
}

void QFxTextEdit::updateSize()
{
    Q_D(QFxTextEdit);
    if (isComponentComplete()) {
        QFontMetrics fm = QFontMetrics(d->font.font());
        int dy = height();
        // ### assumes that if the width is set, the text will fill to edges
        // ### (unless wrap is false, then clipping will occur)
        if (widthValid())
            d->document->setTextWidth(width());
        dy -= (int)d->document->size().height();

        int yoff = 0;
        if (heightValid()) {
            if (d->vAlign == AlignBottom)
                yoff = dy;
            else if (d->vAlign == AlignVCenter)
                yoff = dy/2;
        }
        setBaselineOffset(fm.ascent() + yoff);
        if (!widthValid()) {
            int newWidth = (int)d->document->idealWidth();
            d->document->setTextWidth(newWidth); // ### QTextDoc> Alignment will not work unless textWidth is set
            setImplicitWidth(newWidth);
        }
        if (!heightValid()) {
            if (d->text.isEmpty()) {
                setImplicitHeight(fm.height());
            } else { 
                setImplicitHeight((int)d->document->size().height());
            } 
        }
        setContentsSize(QSize(width(), height()));
    } else {
        d->dirty = true;
    }
    emit update();
}

void QFxTextEditPrivate::updateDefaultTextOption()
{
    QTextOption opt = document->defaultTextOption();
    int oldAlignment = opt.alignment();
    opt.setAlignment((Qt::Alignment)(int)(hAlign | vAlign));
    
    QTextOption::WrapMode oldWrapMode = opt.wrapMode();

    if (wrap)
        opt.setWrapMode(QTextOption::WordWrap);
    else
        opt.setWrapMode(QTextOption::NoWrap);

    if (oldWrapMode == opt.wrapMode() && oldAlignment == opt.alignment())
        return;
    document->setDefaultTextOption(opt);
}

QT_END_NAMESPACE