Qt 4.8
qlineedit_p.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qlineedit.h"
43 #include "qlineedit_p.h"
44 
45 #ifndef QT_NO_LINEEDIT
46 
47 #include "qabstractitemview.h"
48 #include "qclipboard.h"
49 #ifndef QT_NO_ACCESSIBILITY
50 #include "qaccessible.h"
51 #endif
52 #ifndef QT_NO_IM
53 #include "qinputcontext.h"
54 #include "qlist.h"
55 #endif
56 
58 
61 
63 {
64  QRect widgetRect = !rect.isEmpty() ? rect : q_func()->rect();
66  int cix = cr.x() - hscroll + horizontalMargin;
67  return widgetRect.translated(QPoint(cix, vscroll));
68 }
69 
71 {
73  x-= cr.x() - hscroll + horizontalMargin;
74  return control->xToPos(x, betweenOrOn);
75 }
76 
78 {
80 }
81 
82 #ifndef QT_NO_COMPLETER
83 
85 {
86  Q_Q(QLineEdit);
88  q->setText(newText);
89  } else {
90  int c = control->cursor();
91  QString text = control->text();
92  q->setText(text.left(c) + newText.mid(c));
93  control->moveCursor(control->end(), false);
94  control->moveCursor(c, true);
95  }
96 }
97 
98 #endif // QT_NO_COMPLETER
99 
101 {
102  Q_Q(QLineEdit);
103  if (!q->hasFocus() && control->hasSelectedText())
104  control->deselect();
105 }
106 
108 {
109  Q_Q(QLineEdit);
110  emit q->textEdited(text);
111 #ifndef QT_NO_COMPLETER
112  if (control->completer()
114  control->complete(-1); // update the popup on cut/paste/del
115 #endif
116 }
117 
119 {
120  Q_Q(QLineEdit);
121  q->update();
122  emit q->cursorPositionChanged(from, to);
123 }
124 
125 #ifdef QT_KEYPAD_NAVIGATION
126 void QLineEditPrivate::_q_editFocusChange(bool e)
127 {
128  Q_Q(QLineEdit);
129  q->setEditFocus(e);
130 }
131 #endif
132 
134 {
135  Q_Q(QLineEdit);
136  if (control->preeditAreaText().isEmpty()) {
138  q->initStyleOption(&opt);
139  bool showCursor = control->hasSelectedText() ?
140  q->style()->styleHint(QStyle::SH_BlinkCursorWhenTextSelected, &opt, q):
141  q->hasFocus();
142  setCursorVisible(showCursor);
143  }
144 
145  emit q->selectionChanged();
146 #ifndef QT_NO_ACCESSIBILITY
148 #endif
149 }
150 
152 {
153  q_func()->update(adjustedControlRect(rect));
154 }
155 
157 {
158  Q_Q(QLineEdit);
159  control = new QLineControl(txt);
160  control->setParent(q);
161  control->setFont(q->font());
162  QObject::connect(control, SIGNAL(textChanged(QString)),
163  q, SIGNAL(textChanged(QString)));
164  QObject::connect(control, SIGNAL(textEdited(QString)),
165  q, SLOT(_q_textEdited(QString)));
166  QObject::connect(control, SIGNAL(cursorPositionChanged(int,int)),
167  q, SLOT(_q_cursorPositionChanged(int,int)));
168  QObject::connect(control, SIGNAL(selectionChanged()),
169  q, SLOT(_q_selectionChanged()));
170  QObject::connect(control, SIGNAL(accepted()),
171  q, SIGNAL(returnPressed()));
173  q, SIGNAL(editingFinished()));
174 #ifdef QT_KEYPAD_NAVIGATION
175  QObject::connect(control, SIGNAL(editFocusChange(bool)),
176  q, SLOT(_q_editFocusChange(bool)));
177 #endif
178  QObject::connect(control, SIGNAL(cursorPositionChanged(int,int)),
179  q, SLOT(updateMicroFocus()));
180 
181  QObject::connect(control, SIGNAL(textChanged(const QString &)),
182  q, SLOT(updateMicroFocus()));
183 
184  // for now, going completely overboard with updates.
185  QObject::connect(control, SIGNAL(selectionChanged()),
186  q, SLOT(update()));
187 
188  QObject::connect(control, SIGNAL(displayTextChanged(QString)),
189  q, SLOT(update()));
190 
191  QObject::connect(control, SIGNAL(updateNeeded(QRect)),
192  q, SLOT(_q_updateNeeded(QRect)));
193 
195  q->initStyleOption(&opt);
196  control->setPasswordCharacter(q->style()->styleHint(QStyle::SH_LineEdit_PasswordCharacter, &opt, q));
197 #ifndef QT_NO_CURSOR
198  q->setCursor(Qt::IBeamCursor);
199 #endif
200  q->setFocusPolicy(Qt::StrongFocus);
201  q->setAttribute(Qt::WA_InputMethodEnabled);
202  // Specifies that this widget can use more, but is able to survive on
203  // less, horizontal space; and is fixed vertically.
205  q->setBackgroundRole(QPalette::Base);
206  q->setAttribute(Qt::WA_KeyCompression);
207  q->setMouseTracking(true);
208  q->setAcceptDrops(true);
209 
210  q->setAttribute(Qt::WA_MacShowFocusRect);
211 }
212 
214 {
215  Q_Q(const QLineEdit);
217  q->initStyleOption(&opt);
218  QRect r = q->style()->subElementRect(QStyle::SE_LineEditContents, &opt, q);
219  r.setX(r.x() + leftTextMargin);
220  r.setY(r.y() + topTextMargin);
221  r.setRight(r.right() - rightTextMargin);
223  return r;
224 }
225 
227 {
228  Q_Q(QLineEdit);
229  if ((bool)cursorVisible == visible)
230  return;
232  if (control->inputMask().isEmpty())
233  q->update(cursorRect());
234  else
235  q->update();
236 }
237 
239 {
240  Q_Q(QLineEdit);
243 }
244 
251 {
252 #if !defined QT_NO_IM
253  Q_Q(QLineEdit);
254  if ( control->composeMode() ) {
255  int tmp_cursor = xToPos(e->pos().x());
256  int mousePos = tmp_cursor - control->cursor();
257  if ( mousePos < 0 || mousePos > control->preeditAreaText().length() ) {
258  mousePos = -1;
259  // don't send move events outside the preedit area
260  if ( e->type() == QEvent::MouseMove )
261  return true;
262  }
263 
264  QInputContext *qic = q->inputContext();
265  if ( qic )
266  // may be causing reset() in some input methods
267  qic->mouseHandler(mousePos, e);
268  if (!control->preeditAreaText().isEmpty())
269  return true;
270  }
271 #else
272  Q_UNUSED(e);
273 #endif
274 
275  return false;
276 }
277 
278 #ifndef QT_NO_DRAGANDDROP
280 {
281  Q_Q(QLineEdit);
282  dndTimer.stop();
283  QMimeData *data = new QMimeData;
284  data->setText(control->selectedText());
285  QDrag *drag = new QDrag(q);
286  drag->setMimeData(data);
287  Qt::DropAction action = drag->start();
288  if (action == Qt::MoveAction && !control->isReadOnly() && drag->target() != q)
290 }
291 
292 #endif // QT_NO_DRAGANDDROP
293 
295 
296 #endif
static void updateAccessibility(QObject *, int who, Event reason)
Notifies accessibility clients about a change in object&#39;s accessibility information.
void _q_completionHighlighted(QString)
Definition: qlineedit_p.cpp:84
void setBottom(int pos)
Sets the bottom edge of the rectangle to the given y coordinate.
Definition: qrect.h:267
int cursor() const
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QDrag class provides support for MIME-based drag and drop data transfer.
Definition: qdrag.h:61
void setMimeData(QMimeData *data)
Sets the data to be sent to the given MIME data.
Definition: qdrag.cpp:142
void _q_selectionChanged()
QString text() const
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QCompleter * completer() const
#define SLOT(a)
Definition: qobjectdefs.h:226
QRect translated(int dx, int dy) const
Returns a copy of the rectangle that is translated dx along the x axis and dy along the y axis...
Definition: qrect.h:328
void setY(int y)
Sets the top edge of the rectangle to the given y coordinate.
Definition: qrect.h:285
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
The QString class provides a Unicode character string.
Definition: qstring.h:83
void updatePasswordEchoEditing(bool)
QRect cursorRect() const
Definition: qlineedit_p.cpp:77
CompletionMode completionMode
how the completions are provided to the user
Definition: qcompleter.h:69
bool composeMode() const
QString preeditAreaText() const
QRect adjustedControlRect(const QRect &) const
Definition: qlineedit_p.cpp:62
const QPoint & pos() const
Returns the position of the mouse cursor, relative to the widget that received the event...
Definition: qevent.h:95
void setParent(QObject *)
Makes the object a child of parent.
Definition: qobject.cpp:1950
#define Q_Q(Class)
Definition: qglobal.h:2483
QWidgetData data
Definition: qwidget_p.h:755
void update()
Updates the widget unless updates are disabled or the widget is hidden.
Definition: qwidget.cpp:10883
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void setCursorVisible(bool visible)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool visible
whether the widget is visible
Definition: qwidget.h:191
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int xToPos(int x, QTextLine::CursorPosition=QTextLine::CursorBetweenCharacters) const
Definition: qlineedit_p.cpp:70
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QRect adjustedContentsRect() const
void _q_handleWindowActivate()
#define emit
Definition: qobjectdefs.h:76
void setRight(int pos)
Sets the right edge of the rectangle to the given x coordinate.
Definition: qrect.h:264
void editingFinished()
This signal is emitted editing is finished.
static const int horizontalMargin
Definition: qlineedit_p.h:120
int xToPos(int x, QTextLine::CursorPosition=QTextLine::CursorBetweenCharacters) const
Returns the cursor position of the given x pixel value in relation to the displayed text...
DropAction
Definition: qnamespace.h:1597
QRect cursorRect() const
Returns the bounds of the current cursor, as defined as a between characters cursor.
void updateMicroFocus()
Updates the widget&#39;s micro focus.
Definition: qwidget.cpp:11820
virtual void mouseHandler(int x, QMouseEvent *event)
This function can be reimplemented in a subclass to handle mouse press, release, double-click, and move events within the preedit text.
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
void setX(int x)
Sets the left edge of the rectangle to the given x coordinate.
Definition: qrect.h:282
QRect rect() const
static const int verticalMargin
Definition: qlineedit_p.h:119
void removeSelection()
void _q_textEdited(const QString &)
void stop()
Stops the timer.
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
QString inputMask() const
bool isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
Definition: qrect.h:234
bool shouldEnableInputMethod() const
Definition: qlineedit_p.h:104
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
bool isReadOnly() const
void moveCursor(int pos, bool mark=false)
Moves the cursor to the given position pos.
QString selectedText() const
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
int x() const
bool sendMouseEventToInputContext(QMouseEvent *e)
This function is not intended as polymorphic usage.
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
void setFont(const QFont &font)
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void setPasswordCharacter(const QChar &character)
void _q_updateNeeded(const QRect &)
Qt::DropAction start(Qt::DropActions supportedActions=Qt::CopyAction)
Starts the drag and drop operation and returns a value indicating the requested drop action when it i...
Definition: qdrag.cpp:317
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
The QStyleOptionFrameV2 class is used to describe the parameters necessary for drawing a frame in Qt ...
Definition: qstyleoption.h:134
The QLineEdit widget is a one-line text editor.
Definition: qlineedit.h:66
void setText(const QString &text)
Sets text as the plain text (MIME type text/plain) used to represent the data.
Definition: qmimedata.cpp:377
void updatePasswordEchoEditing(bool editing)
Sets the password echo editing to editing.
QLineControl * control
Definition: qlineedit_p.h:89
bool hasSelectedText() const
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
The QInputContext class abstracts the input method dependent data and composing state.
Definition: qinputcontext.h:83
void complete(int key)
void init(const QString &)
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
int end() const
void _q_cursorPositionChanged(int, int)
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QBasicTimer dndTimer
Definition: qlineedit_p.h:139
#define text
Definition: qobjectdefs.h:80
QWidget * target() const
Returns the target of the drag and drop operation.
Definition: qdrag.cpp:219