Export QTextImageHandler and add accessor for image
[profile/ivi/qtbase.git] / src / gui / text / qlinecontrol_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QLINECONTROL_P_H
43 #define QLINECONTROL_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "QtCore/qglobal.h"
57
58 #include "QtGui/qtextlayout.h"
59 #include "QtCore/qpointer.h"
60 #include "QtGui/qclipboard.h"
61 #include "QtGui/qvalidator.h"
62 #include "QtGui/qpalette.h"
63 #include "QtGui/qguiapplication.h"
64 #include "QtCore/qpoint.h"
65
66 QT_BEGIN_HEADER
67
68 QT_BEGIN_NAMESPACE
69
70 QT_MODULE(Gui)
71
72 class Q_GUI_EXPORT QLineControl : public QObject
73 {
74     Q_OBJECT
75
76 public:
77     QLineControl(const QString &txt = QString())
78         : m_cursor(0), m_preeditCursor(0), m_cursorWidth(0), m_layoutDirection(Qt::LayoutDirectionAuto),
79         m_hideCursor(false), m_separator(0), m_readOnly(0),
80         m_dragEnabled(0), m_echoMode(Normal), m_textDirty(0), m_selDirty(0),
81         m_validInput(1), m_blinkStatus(0), m_blinkPeriod(0), m_blinkTimer(0), m_deleteAllTimer(0),
82         m_ascent(0), m_maxLength(32767), m_lastCursorPos(-1),
83         m_tripleClickTimer(0), m_maskData(0), m_modifiedState(0), m_undoState(0),
84         m_selstart(0), m_selend(0), m_passwordEchoEditing(false)
85     {
86         init(txt);
87     }
88
89     enum EchoMode {
90         Normal,
91         NoEcho,
92         Password,
93         PasswordEchoOnEdit
94     };
95
96
97     ~QLineControl()
98     {
99         delete [] m_maskData;
100     }
101
102     int nextMaskBlank(int pos)
103     {
104         int c = findInMask(pos, true, false);
105         m_separator |= (c != pos);
106         return (c != -1 ?  c : m_maxLength);
107     }
108
109     int prevMaskBlank(int pos)
110     {
111         int c = findInMask(pos, false, false);
112         m_separator |= (c != pos);
113         return (c != -1 ? c : 0);
114     }
115
116     bool isUndoAvailable() const { return !m_readOnly && m_undoState; }
117     bool isRedoAvailable() const { return !m_readOnly && m_undoState < (int)m_history.size(); }
118     void clearUndo() { m_history.clear(); m_modifiedState = m_undoState = 0; }
119
120     bool isModified() const { return m_modifiedState != m_undoState; }
121     void setModified(bool modified) { m_modifiedState = modified ? -1 : m_undoState; }
122
123     bool allSelected() const { return !m_text.isEmpty() && m_selstart == 0 && m_selend == (int)m_text.length(); }
124     bool hasSelectedText() const { return !m_text.isEmpty() && m_selend > m_selstart; }
125
126     int width() const { return qRound(m_textLayout.lineAt(0).width()) + 1; }
127     int height() const { return qRound(m_textLayout.lineAt(0).height()) + 1; }
128     int ascent() const { return m_ascent; }
129     qreal naturalTextWidth() const { return m_textLayout.lineAt(0).naturalTextWidth(); }
130
131     void setSelection(int start, int length);
132
133     inline QString selectedText() const { return hasSelectedText() ? m_text.mid(m_selstart, m_selend - m_selstart) : QString(); }
134     QString textBeforeSelection() const { return hasSelectedText() ? m_text.left(m_selstart) : QString(); }
135     QString textAfterSelection() const { return hasSelectedText() ? m_text.mid(m_selend) : QString(); }
136
137     int selectionStart() const { return hasSelectedText() ? m_selstart : -1; }
138     int selectionEnd() const { return hasSelectedText() ? m_selend : -1; }
139     bool inSelection(int x) const
140     {
141         if (m_selstart >= m_selend)
142             return false;
143         int pos = xToPos(x, QTextLine::CursorOnCharacter);
144         return pos >= m_selstart && pos < m_selend;
145     }
146
147     void removeSelection()
148     {
149         int priorState = m_undoState;
150         removeSelectedText();
151         finishChange(priorState);
152     }
153
154     int start() const { return 0; }
155     int end() const { return m_text.length(); }
156
157 #ifndef QT_NO_CLIPBOARD
158     void copy(QClipboard::Mode mode = QClipboard::Clipboard) const;
159     void paste(QClipboard::Mode mode = QClipboard::Clipboard);
160 #endif
161
162     int cursor() const{ return m_cursor; }
163     int preeditCursor() const { return m_preeditCursor; }
164
165     int cursorWidth() const { return m_cursorWidth; }
166     void setCursorWidth(int value) { m_cursorWidth = value; }
167
168     Qt::CursorMoveStyle cursorMoveStyle() const { return m_textLayout.cursorMoveStyle(); }
169     void setCursorMoveStyle(Qt::CursorMoveStyle style) { m_textLayout.setCursorMoveStyle(style); }
170
171     void moveCursor(int pos, bool mark = false);
172     void cursorForward(bool mark, int steps)
173     {
174         int c = m_cursor;
175         if (steps > 0) {
176             while (steps--)
177                 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.rightCursorPosition(c)
178                                                              : m_textLayout.nextCursorPosition(c);
179         } else if (steps < 0) {
180             while (steps++)
181                 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.leftCursorPosition(c)
182                                                              : m_textLayout.previousCursorPosition(c);
183         }
184         moveCursor(c, mark);
185     }
186
187     void cursorWordForward(bool mark) { moveCursor(m_textLayout.nextCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
188     void cursorWordBackward(bool mark) { moveCursor(m_textLayout.previousCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
189
190     void home(bool mark) { moveCursor(0, mark); }
191     void end(bool mark) { moveCursor(text().length(), mark); }
192
193     int xToPos(int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters) const;
194     QRect cursorRect() const;
195
196     qreal cursorToX(int cursor) const { return m_textLayout.lineAt(0).cursorToX(cursor); }
197     qreal cursorToX() const
198     {
199         int cursor = m_cursor;
200         if (m_preeditCursor != -1)
201             cursor += m_preeditCursor;
202         return cursorToX(cursor);
203     }
204
205     bool isReadOnly() const { return m_readOnly; }
206     void setReadOnly(bool enable) { m_readOnly = enable; }
207
208     QString text() const
209     {
210         QString res = m_maskData ? stripString(m_text) : m_text;
211         return (res.isNull() ? QString::fromLatin1("") : res);
212     }
213     void setText(const QString &txt) { internalSetText(txt, -1, false); }
214     QString displayText() const { return m_textLayout.text(); }
215
216     void backspace();
217     void del();
218     void deselect() { internalDeselect(); finishChange(); }
219     void selectAll() { m_selstart = m_selend = m_cursor = 0; moveCursor(m_text.length(), true); }
220
221     void insert(const QString &);
222     void clear();
223     void undo() { internalUndo(); finishChange(-1, true); }
224     void redo() { internalRedo(); finishChange(); }
225     void selectWordAtPos(int);
226
227     EchoMode echoMode() const { return EchoMode(m_echoMode); }
228     void setEchoMode(EchoMode mode)
229     {
230         m_echoMode = mode;
231         m_passwordEchoEditing = false;
232         updateDisplayText();
233     }
234
235     int maxLength() const { return m_maxLength; }
236     void setMaxLength(int maxLength)
237     {
238         if (m_maskData)
239             return;
240         m_maxLength = maxLength;
241         setText(m_text);
242     }
243
244 #ifndef QT_NO_VALIDATOR
245     const QValidator *validator() const { return m_validator; }
246     void setValidator(const QValidator *v) { m_validator = const_cast<QValidator*>(v); }
247 #endif
248
249     int cursorPosition() const { return m_cursor; }
250     void setCursorPosition(int pos) { if (pos <= m_text.length()) moveCursor(qMax(0, pos)); }
251
252     bool hasAcceptableInput() const { return hasAcceptableInput(m_text); }
253     bool fixup();
254
255     QString inputMask() const { return m_maskData ? m_inputMask + QLatin1Char(';') + m_blank : QString(); }
256     void setInputMask(const QString &mask)
257     {
258         parseInputMask(mask);
259         if (m_maskData)
260             moveCursor(nextMaskBlank(0));
261     }
262
263     // input methods
264 #ifndef QT_NO_IM
265     bool composeMode() const { return !m_textLayout.preeditAreaText().isEmpty(); }
266     void setPreeditArea(int cursor, const QString &text) { m_textLayout.setPreeditArea(cursor, text); }
267 #endif
268
269     QString preeditAreaText() const { return m_textLayout.preeditAreaText(); }
270
271     void updatePasswordEchoEditing(bool editing);
272     bool passwordEchoEditing() const { return m_passwordEchoEditing; }
273
274     QChar passwordCharacter() const { return m_passwordCharacter; }
275     void setPasswordCharacter(const QChar &character) { m_passwordCharacter = character; updateDisplayText(); }
276
277     Qt::LayoutDirection layoutDirection() const {
278         if (m_layoutDirection == Qt::LayoutDirectionAuto) {
279             if (m_text.isEmpty())
280                 return QGuiApplication::keyboardInputDirection();
281             return m_text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight;
282         }
283         return m_layoutDirection;
284     }
285     void setLayoutDirection(Qt::LayoutDirection direction)
286     {
287         if (direction != m_layoutDirection) {
288             m_layoutDirection = direction;
289             updateDisplayText();
290         }
291     }
292
293     void setFont(const QFont &font) { m_textLayout.setFont(font); updateDisplayText(); }
294
295     void processInputMethodEvent(QInputMethodEvent *event);
296     void processMouseEvent(QMouseEvent* ev);
297     void processKeyEvent(QKeyEvent* ev);
298
299     int cursorBlinkPeriod() const { return m_blinkPeriod; }
300     void setCursorBlinkPeriod(int msec);
301     void resetCursorBlinkTimer();
302
303     bool cursorBlinkStatus() const { return m_blinkStatus; }
304
305     QString cancelText() const { return m_cancelText; }
306     void setCancelText(const QString &text) { m_cancelText = text; }
307
308     const QPalette &palette() const { return m_palette; }
309     void setPalette(const QPalette &p) { m_palette = p; }
310
311     enum DrawFlags {
312         DrawText = 0x01,
313         DrawSelections = 0x02,
314         DrawCursor = 0x04,
315         DrawAll = DrawText | DrawSelections | DrawCursor
316     };
317     void draw(QPainter *, const QPoint &, const QRect &, int flags = DrawAll);
318
319     bool processEvent(QEvent *ev);
320
321     QTextLayout *textLayout()
322     {
323         return &m_textLayout;
324     }
325
326 private:
327     void init(const QString &txt);
328     void removeSelectedText();
329     void internalSetText(const QString &txt, int pos = -1, bool edited = true);
330     void updateDisplayText(bool forceUpdate = false);
331
332     void internalInsert(const QString &s);
333     void internalDelete(bool wasBackspace = false);
334     void internalRemove(int pos);
335
336     inline void internalDeselect()
337     {
338         m_selDirty |= (m_selend > m_selstart);
339         m_selstart = m_selend = 0;
340     }
341
342     void internalUndo(int until = -1);
343     void internalRedo();
344
345     QString m_text;
346     QPalette m_palette;
347     int m_cursor;
348     int m_preeditCursor;
349     int m_cursorWidth;
350     Qt::LayoutDirection m_layoutDirection;
351     uint m_hideCursor : 1; // used to hide the m_cursor inside preedit areas
352     uint m_separator : 1;
353     uint m_readOnly : 1;
354     uint m_dragEnabled : 1;
355     uint m_echoMode : 2;
356     uint m_textDirty : 1;
357     uint m_selDirty : 1;
358     uint m_validInput : 1;
359     uint m_blinkStatus : 1;
360     int m_blinkPeriod; // 0 for non-blinking cursor
361     int m_blinkTimer;
362     int m_deleteAllTimer;
363     int m_ascent;
364     int m_maxLength;
365     int m_lastCursorPos;
366     QList<int> m_transactions;
367     QPoint m_tripleClick;
368     int m_tripleClickTimer;
369     QString m_cancelText;
370
371     void emitCursorPositionChanged();
372
373     bool finishChange(int validateFromState = -1, bool update = false, bool edited = true);
374
375 #ifndef QT_NO_VALIDATOR
376     QPointer<QValidator> m_validator;
377 #endif
378
379     struct MaskInputData {
380         enum Casemode { NoCaseMode, Upper, Lower };
381         QChar maskChar; // either the separator char or the inputmask
382         bool separator;
383         Casemode caseMode;
384     };
385     QString m_inputMask;
386     QChar m_blank;
387     MaskInputData *m_maskData;
388
389     // undo/redo handling
390     enum CommandType { Separator, Insert, Remove, Delete, RemoveSelection, DeleteSelection, SetSelection };
391     struct Command {
392         inline Command() {}
393         inline Command(CommandType t, int p, QChar c, int ss, int se) : type(t),uc(c),pos(p),selStart(ss),selEnd(se) {}
394         uint type : 4;
395         QChar uc;
396         int pos, selStart, selEnd;
397     };
398     int m_modifiedState;
399     int m_undoState;
400     QVector<Command> m_history;
401     void addCommand(const Command& cmd);
402
403     inline void separate() { m_separator = true; }
404
405     // selection
406     int m_selstart;
407     int m_selend;
408
409     // masking
410     void parseInputMask(const QString &maskFields);
411     bool isValidInput(QChar key, QChar mask) const;
412     bool hasAcceptableInput(const QString &text) const;
413     QString maskString(uint pos, const QString &str, bool clear = false) const;
414     QString clearString(uint pos, uint len) const;
415     QString stripString(const QString &str) const;
416     int findInMask(int pos, bool forward, bool findSeparator, QChar searchChar = QChar()) const;
417
418     // complex text layout
419     QTextLayout m_textLayout;
420
421     bool m_passwordEchoEditing;
422     QChar m_passwordCharacter;
423
424 Q_SIGNALS:
425     void cursorPositionChanged(int, int);
426     void selectionChanged();
427
428     void displayTextChanged(const QString &);
429     void textChanged(const QString &);
430     void textEdited(const QString &);
431
432     void resetInputContext();
433     void updateMicroFocus();
434
435     void accepted();
436     void editingFinished();
437     void updateNeeded(const QRect &);
438
439 #ifdef QT_KEYPAD_NAVIGATION
440     void editFocusChange(bool);
441 #endif
442 protected:
443     virtual void timerEvent(QTimerEvent *event);
444
445 private Q_SLOTS:
446     void _q_clipboardChanged();
447     void _q_deleteSelected();
448
449 };
450
451 QT_END_NAMESPACE
452
453 QT_END_HEADER
454
455 #endif // QLineControl_P_H