Update licenseheader text in source files for qtbase Qt module
[profile/ivi/qtbase.git] / src / gui / widgets / 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 #ifndef QT_NO_LINEEDIT
59 #include "private/qwidget_p.h"
60 #include "QtGui/qlineedit.h"
61 #include "QtGui/qtextlayout.h"
62 #include "QtGui/qstyleoption.h"
63 #include "QtCore/qpointer.h"
64 #include "QtGui/qclipboard.h"
65 #include "QtCore/qpoint.h"
66 #include "QtGui/qcompleter.h"
67 #include "QtGui/qaccessible.h"
68
69 QT_BEGIN_HEADER
70
71 QT_BEGIN_NAMESPACE
72
73 QT_MODULE(Gui)
74
75 class Q_GUI_EXPORT QLineControl : public QObject
76 {
77     Q_OBJECT
78
79 public:
80     QLineControl(const QString &txt = QString())
81         : m_cursor(0), m_preeditCursor(0), m_cursorWidth(0), m_layoutDirection(Qt::LayoutDirectionAuto),
82         m_hideCursor(false), m_separator(0), m_readOnly(0),
83         m_dragEnabled(0), m_echoMode(0), m_textDirty(0), m_selDirty(0),
84         m_validInput(1), m_blinkStatus(0), m_blinkPeriod(0), m_blinkTimer(0), m_deleteAllTimer(0),
85         m_ascent(0), m_maxLength(32767), m_lastCursorPos(-1),
86         m_tripleClickTimer(0), m_maskData(0), m_modifiedState(0), m_undoState(0),
87         m_selstart(0), m_selend(0), m_passwordEchoEditing(false)
88     {
89         init(txt);
90     }
91
92     ~QLineControl()
93     {
94         delete [] m_maskData;
95     }
96
97     int nextMaskBlank(int pos)
98     {
99         int c = findInMask(pos, true, false);
100         m_separator |= (c != pos);
101         return (c != -1 ?  c : m_maxLength);
102     }
103
104     int prevMaskBlank(int pos)
105     {
106         int c = findInMask(pos, false, false);
107         m_separator |= (c != pos);
108         return (c != -1 ? c : 0);
109     }
110
111     bool isUndoAvailable() const { return !m_readOnly && m_undoState; }
112     bool isRedoAvailable() const { return !m_readOnly && m_undoState < (int)m_history.size(); }
113     void clearUndo() { m_history.clear(); m_modifiedState = m_undoState = 0; }
114
115     bool isModified() const { return m_modifiedState != m_undoState; }
116     void setModified(bool modified) { m_modifiedState = modified ? -1 : m_undoState; }
117
118     bool allSelected() const { return !m_text.isEmpty() && m_selstart == 0 && m_selend == (int)m_text.length(); }
119     bool hasSelectedText() const { return !m_text.isEmpty() && m_selend > m_selstart; }
120
121     int width() const { return qRound(m_textLayout.lineAt(0).width()) + 1; }
122     int height() const { return qRound(m_textLayout.lineAt(0).height()) + 1; }
123     int ascent() const { return m_ascent; }
124     qreal naturalTextWidth() const { return m_textLayout.lineAt(0).naturalTextWidth(); }
125
126     void setSelection(int start, int length);
127
128     inline QString selectedText() const { return hasSelectedText() ? m_text.mid(m_selstart, m_selend - m_selstart) : QString(); }
129     QString textBeforeSelection() const { return hasSelectedText() ? m_text.left(m_selstart) : QString(); }
130     QString textAfterSelection() const { return hasSelectedText() ? m_text.mid(m_selend) : QString(); }
131
132     int selectionStart() const { return hasSelectedText() ? m_selstart : -1; }
133     int selectionEnd() const { return hasSelectedText() ? m_selend : -1; }
134     bool inSelection(int x) const
135     {
136         if (m_selstart >= m_selend)
137             return false;
138         int pos = xToPos(x, QTextLine::CursorOnCharacter);
139         return pos >= m_selstart && pos < m_selend;
140     }
141
142     void removeSelection()
143     {
144         int priorState = m_undoState;
145         removeSelectedText();
146         finishChange(priorState);
147     }
148
149     int start() const { return 0; }
150     int end() const { return m_text.length(); }
151
152 #ifndef QT_NO_CLIPBOARD
153     void copy(QClipboard::Mode mode = QClipboard::Clipboard) const;
154     void paste(QClipboard::Mode mode = QClipboard::Clipboard);
155 #endif
156
157     int cursor() const{ return m_cursor; }
158     int preeditCursor() const { return m_preeditCursor; }
159
160     int cursorWidth() const { return m_cursorWidth; }
161     void setCursorWidth(int value) { m_cursorWidth = value; }
162
163     Qt::CursorMoveStyle cursorMoveStyle() const { return m_textLayout.cursorMoveStyle(); }
164     void setCursorMoveStyle(Qt::CursorMoveStyle style) { m_textLayout.setCursorMoveStyle(style); }
165
166     void moveCursor(int pos, bool mark = false);
167     void cursorForward(bool mark, int steps)
168     {
169         int c = m_cursor;
170         if (steps > 0) {
171             while (steps--)
172                 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.rightCursorPosition(c)
173                                                              : m_textLayout.nextCursorPosition(c);
174         } else if (steps < 0) {
175             while (steps++)
176                 c = cursorMoveStyle() == Qt::VisualMoveStyle ? m_textLayout.leftCursorPosition(c)
177                                                              : m_textLayout.previousCursorPosition(c);
178         }
179         moveCursor(c, mark);
180     }
181
182     void cursorWordForward(bool mark) { moveCursor(m_textLayout.nextCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
183     void cursorWordBackward(bool mark) { moveCursor(m_textLayout.previousCursorPosition(m_cursor, QTextLayout::SkipWords), mark); }
184
185     void home(bool mark) { moveCursor(0, mark); }
186     void end(bool mark) { moveCursor(text().length(), mark); }
187
188     int xToPos(int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters) const;
189     QRect cursorRect() const;
190
191     qreal cursorToX(int cursor) const { return m_textLayout.lineAt(0).cursorToX(cursor); }
192     qreal cursorToX() const
193     {
194         int cursor = m_cursor;
195         if (m_preeditCursor != -1)
196             cursor += m_preeditCursor;
197         return cursorToX(cursor);
198     }
199
200     bool isReadOnly() const { return m_readOnly; }
201     void setReadOnly(bool enable) { m_readOnly = enable; }
202
203     QString text() const
204     {
205         QString res = m_maskData ? stripString(m_text) : m_text;
206         return (res.isNull() ? QString::fromLatin1("") : res);
207     }
208     void setText(const QString &txt) { internalSetText(txt, -1, false); }
209     QString displayText() const { return m_textLayout.text(); }
210
211     void backspace();
212     void del();
213     void deselect() { internalDeselect(); finishChange(); }
214     void selectAll() { m_selstart = m_selend = m_cursor = 0; moveCursor(m_text.length(), true); }
215
216     void insert(const QString &);
217     void clear();
218     void undo() { internalUndo(); finishChange(-1, true); }
219     void redo() { internalRedo(); finishChange(); }
220     void selectWordAtPos(int);
221
222     uint echoMode() const { return m_echoMode; }
223     void setEchoMode(uint mode)
224     {
225         m_echoMode = mode;
226         m_passwordEchoEditing = false;
227         updateDisplayText();
228     }
229
230     int maxLength() const { return m_maxLength; }
231     void setMaxLength(int maxLength)
232     {
233         if (m_maskData)
234             return;
235         m_maxLength = maxLength;
236         setText(m_text);
237     }
238
239 #ifndef QT_NO_VALIDATOR
240     const QValidator *validator() const { return m_validator; }
241     void setValidator(const QValidator *v) { m_validator = const_cast<QValidator*>(v); }
242 #endif
243
244 #ifndef QT_NO_COMPLETER
245     QCompleter *completer() const { return m_completer; }
246     /* Note that you must set the widget for the completer separately */
247     void setCompleter(const QCompleter *c) { m_completer = const_cast<QCompleter*>(c); }
248     void complete(int key);
249 #endif
250
251     int cursorPosition() const { return m_cursor; }
252     void setCursorPosition(int pos) { if (pos <= m_text.length()) moveCursor(qMax(0, pos)); }
253
254     bool hasAcceptableInput() const { return hasAcceptableInput(m_text); }
255     bool fixup();
256
257     QString inputMask() const { return m_maskData ? m_inputMask + QLatin1Char(';') + m_blank : QString(); }
258     void setInputMask(const QString &mask)
259     {
260         parseInputMask(mask);
261         if (m_maskData)
262             moveCursor(nextMaskBlank(0));
263     }
264
265     // input methods
266 #ifndef QT_NO_IM
267     bool composeMode() const { return !m_textLayout.preeditAreaText().isEmpty(); }
268     void setPreeditArea(int cursor, const QString &text) { m_textLayout.setPreeditArea(cursor, text); }
269 #endif
270
271     QString preeditAreaText() const { return m_textLayout.preeditAreaText(); }
272
273     void updatePasswordEchoEditing(bool editing);
274     bool passwordEchoEditing() const { return m_passwordEchoEditing; }
275
276     QChar passwordCharacter() const { return m_passwordCharacter; }
277     void setPasswordCharacter(const QChar &character) { m_passwordCharacter = character; updateDisplayText(); }
278
279     Qt::LayoutDirection layoutDirection() const {
280         if (m_layoutDirection == Qt::LayoutDirectionAuto) {
281             if (m_text.isEmpty())
282                 return QApplication::keyboardInputDirection();
283             return m_text.isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight;
284         }
285         return m_layoutDirection;
286     }
287     void setLayoutDirection(Qt::LayoutDirection direction)
288     {
289         if (direction != m_layoutDirection) {
290             m_layoutDirection = direction;
291             updateDisplayText();
292         }
293     }
294
295     void setFont(const QFont &font) { m_textLayout.setFont(font); updateDisplayText(); }
296
297     void processInputMethodEvent(QInputMethodEvent *event);
298     void processMouseEvent(QMouseEvent* ev);
299     void processKeyEvent(QKeyEvent* ev);
300
301     int cursorBlinkPeriod() const { return m_blinkPeriod; }
302     void setCursorBlinkPeriod(int msec);
303     void resetCursorBlinkTimer();
304
305     bool cursorBlinkStatus() const { return m_blinkStatus; }
306
307     QString cancelText() const { return m_cancelText; }
308     void setCancelText(const QString &text) { m_cancelText = text; }
309
310     const QPalette &palette() const { return m_palette; }
311     void setPalette(const QPalette &p) { m_palette = p; }
312
313     enum DrawFlags {
314         DrawText = 0x01,
315         DrawSelections = 0x02,
316         DrawCursor = 0x04,
317         DrawAll = DrawText | DrawSelections | DrawCursor
318     };
319     void draw(QPainter *, const QPoint &, const QRect &, int flags = DrawAll);
320
321     bool processEvent(QEvent *ev);
322
323     QTextLayout *textLayout()
324     {
325         return &m_textLayout;
326     }
327
328 private:
329     void init(const QString &txt);
330     void removeSelectedText();
331     void internalSetText(const QString &txt, int pos = -1, bool edited = true);
332     void updateDisplayText(bool forceUpdate = false);
333
334     void internalInsert(const QString &s);
335     void internalDelete(bool wasBackspace = false);
336     void internalRemove(int pos);
337
338     inline void internalDeselect()
339     {
340         m_selDirty |= (m_selend > m_selstart);
341         m_selstart = m_selend = 0;
342     }
343
344     void internalUndo(int until = -1);
345     void internalRedo();
346
347     QString m_text;
348     QPalette m_palette;
349     int m_cursor;
350     int m_preeditCursor;
351     int m_cursorWidth;
352     Qt::LayoutDirection m_layoutDirection;
353     uint m_hideCursor : 1; // used to hide the m_cursor inside preedit areas
354     uint m_separator : 1;
355     uint m_readOnly : 1;
356     uint m_dragEnabled : 1;
357     uint m_echoMode : 2;
358     uint m_textDirty : 1;
359     uint m_selDirty : 1;
360     uint m_validInput : 1;
361     uint m_blinkStatus : 1;
362     int m_blinkPeriod; // 0 for non-blinking cursor
363     int m_blinkTimer;
364     int m_deleteAllTimer;
365     int m_ascent;
366     int m_maxLength;
367     int m_lastCursorPos;
368     QList<int> m_transactions;
369     QPoint m_tripleClick;
370     int m_tripleClickTimer;
371     QString m_cancelText;
372
373     void emitCursorPositionChanged();
374
375     bool finishChange(int validateFromState = -1, bool update = false, bool edited = true);
376
377 #ifndef QT_NO_VALIDATOR
378     QPointer<QValidator> m_validator;
379 #endif
380     QPointer<QCompleter> m_completer;
381 #ifndef QT_NO_COMPLETER
382     bool advanceToEnabledItem(int dir);
383 #endif
384
385     struct MaskInputData {
386         enum Casemode { NoCaseMode, Upper, Lower };
387         QChar maskChar; // either the separator char or the inputmask
388         bool separator;
389         Casemode caseMode;
390     };
391     QString m_inputMask;
392     QChar m_blank;
393     MaskInputData *m_maskData;
394
395     // undo/redo handling
396     enum CommandType { Separator, Insert, Remove, Delete, RemoveSelection, DeleteSelection, SetSelection };
397     struct Command {
398         inline Command() {}
399         inline Command(CommandType t, int p, QChar c, int ss, int se) : type(t),uc(c),pos(p),selStart(ss),selEnd(se) {}
400         uint type : 4;
401         QChar uc;
402         int pos, selStart, selEnd;
403     };
404     int m_modifiedState;
405     int m_undoState;
406     QVector<Command> m_history;
407     void addCommand(const Command& cmd);
408
409     inline void separate() { m_separator = true; }
410
411     // selection
412     int m_selstart;
413     int m_selend;
414
415     // masking
416     void parseInputMask(const QString &maskFields);
417     bool isValidInput(QChar key, QChar mask) const;
418     bool hasAcceptableInput(const QString &text) const;
419     QString maskString(uint pos, const QString &str, bool clear = false) const;
420     QString clearString(uint pos, uint len) const;
421     QString stripString(const QString &str) const;
422     int findInMask(int pos, bool forward, bool findSeparator, QChar searchChar = QChar()) const;
423
424     // complex text layout
425     QTextLayout m_textLayout;
426
427     bool m_passwordEchoEditing;
428     QChar m_passwordCharacter;
429
430 Q_SIGNALS:
431     void cursorPositionChanged(int, int);
432     void selectionChanged();
433
434     void displayTextChanged(const QString &);
435     void textChanged(const QString &);
436     void textEdited(const QString &);
437
438     void resetInputContext();
439     void updateMicroFocus();
440
441     void accepted();
442     void editingFinished();
443     void updateNeeded(const QRect &);
444
445 #ifdef QT_KEYPAD_NAVIGATION
446     void editFocusChange(bool);
447 #endif
448 protected:
449     virtual void timerEvent(QTimerEvent *event);
450
451 private Q_SLOTS:
452     void _q_clipboardChanged();
453     void _q_deleteSelected();
454
455 };
456
457 QT_END_NAMESPACE
458
459 QT_END_HEADER
460
461 #endif // QT_NO_LINEEDIT
462
463 #endif // QLINECONTROL_P_H