| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtWidgets 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QLINEEDIT_P_H |
| 41 | #define QLINEEDIT_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
| 55 | |
| 56 | #include "private/qwidget_p.h" |
| 57 | #include "QtWidgets/qlineedit.h" |
| 58 | #if QT_CONFIG(toolbutton) |
| 59 | #include "QtWidgets/qtoolbutton.h" |
| 60 | #endif |
| 61 | #include "QtGui/qtextlayout.h" |
| 62 | #include "QtGui/qicon.h" |
| 63 | #include "QtWidgets/qstyleoption.h" |
| 64 | #include "QtCore/qbasictimer.h" |
| 65 | #if QT_CONFIG(completer) |
| 66 | #include "QtWidgets/qcompleter.h" |
| 67 | #endif |
| 68 | #include "QtCore/qpointer.h" |
| 69 | #include "QtCore/qmimedata.h" |
| 70 | #include <QtCore/qmargins.h> |
| 71 | |
| 72 | #include "private/qwidgetlinecontrol_p.h" |
| 73 | |
| 74 | #include <algorithm> |
| 75 | |
| 76 | QT_REQUIRE_CONFIG(lineedit); |
| 77 | |
| 78 | QT_BEGIN_NAMESPACE |
| 79 | |
| 80 | class QLineEditPrivate; |
| 81 | |
| 82 | // QLineEditIconButton: This is a simple helper class that represents clickable icons that fade in with text |
| 83 | #if QT_CONFIG(toolbutton) |
| 84 | class Q_AUTOTEST_EXPORT QLineEditIconButton : public QToolButton |
| 85 | { |
| 86 | Q_OBJECT |
| 87 | Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity) |
| 88 | public: |
| 89 | explicit QLineEditIconButton(QWidget *parent = nullptr); |
| 90 | |
| 91 | qreal opacity() const { return m_opacity; } |
| 92 | void setOpacity(qreal value); |
| 93 | #if QT_CONFIG(animation) |
| 94 | void animateShow(bool visible); |
| 95 | |
| 96 | bool shouldHideWithText() const; |
| 97 | void setHideWithText(bool hide); |
| 98 | bool needsSpace() const { |
| 99 | if (m_fadingOut) |
| 100 | return false; |
| 101 | return isVisibleTo(parentWidget()); |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | protected: |
| 106 | void actionEvent(QActionEvent *e) override; |
| 107 | void paintEvent(QPaintEvent *event) override; |
| 108 | |
| 109 | private slots: |
| 110 | void updateCursor(); |
| 111 | |
| 112 | #if QT_CONFIG(animation) |
| 113 | void onAnimationFinished(); |
| 114 | #endif |
| 115 | |
| 116 | private: |
| 117 | #if QT_CONFIG(animation) |
| 118 | void startOpacityAnimation(qreal endValue); |
| 119 | #endif |
| 120 | QLineEditPrivate *lineEditPrivate() const; |
| 121 | |
| 122 | qreal m_opacity; |
| 123 | |
| 124 | #if QT_CONFIG(animation) |
| 125 | bool m_hideWithText = false; |
| 126 | bool m_fadingOut = false; |
| 127 | #endif |
| 128 | |
| 129 | }; |
| 130 | #endif // QT_CONFIG(toolbutton) |
| 131 | |
| 132 | class Q_AUTOTEST_EXPORT QLineEditPrivate : public QWidgetPrivate |
| 133 | { |
| 134 | Q_DECLARE_PUBLIC(QLineEdit) |
| 135 | public: |
| 136 | enum SideWidgetFlag { |
| 137 | SideWidgetFadeInWithText = 0x1, |
| 138 | SideWidgetCreatedByWidgetAction = 0x2, |
| 139 | SideWidgetClearButton = 0x4 |
| 140 | }; |
| 141 | |
| 142 | struct SideWidgetEntry { |
| 143 | explicit SideWidgetEntry(QWidget *w = nullptr, QAction *a = nullptr, int _flags = 0) : widget(w), action(a), flags(_flags) {} |
| 144 | |
| 145 | QWidget *widget; |
| 146 | QAction *action; |
| 147 | int flags; |
| 148 | }; |
| 149 | typedef std::vector<SideWidgetEntry> SideWidgetEntryList; |
| 150 | |
| 151 | struct SideWidgetParameters { |
| 152 | int iconSize; |
| 153 | int widgetWidth; |
| 154 | int widgetHeight; |
| 155 | int margin; |
| 156 | }; |
| 157 | |
| 158 | QLineEditPrivate() |
| 159 | : control(nullptr), frame(1), contextMenuEnabled(1), cursorVisible(0), |
| 160 | dragEnabled(0), clickCausedFocus(0), edited(0), hscroll(0), vscroll(0), |
| 161 | alignment(Qt::AlignLeading | Qt::AlignVCenter), |
| 162 | textMargins{0, 0, 0, 0}, |
| 163 | lastTextSize(0), mouseYThreshold(0) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | ~QLineEditPrivate() |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | QWidgetLineControl *control; |
| 172 | |
| 173 | #ifndef QT_NO_CONTEXTMENU |
| 174 | QPointer<QAction> selectAllAction; |
| 175 | #endif |
| 176 | void init(const QString&); |
| 177 | void initMouseYThreshold(); |
| 178 | |
| 179 | QRect adjustedControlRect(const QRect &) const; |
| 180 | |
| 181 | int xToPos(int x, QTextLine::CursorPosition = QTextLine::CursorBetweenCharacters) const; |
| 182 | bool inSelection(int x) const; |
| 183 | QRect cursorRect() const; |
| 184 | void setCursorVisible(bool visible); |
| 185 | void setText(const QString& text); |
| 186 | |
| 187 | void updatePasswordEchoEditing(bool); |
| 188 | |
| 189 | void resetInputMethod(); |
| 190 | |
| 191 | inline bool shouldEnableInputMethod() const |
| 192 | { |
| 193 | return !control->isReadOnly(); |
| 194 | } |
| 195 | inline bool shouldShowPlaceholderText() const |
| 196 | { |
| 197 | return control->text().isEmpty() && control->preeditAreaText().isEmpty() |
| 198 | && !((alignment & Qt::AlignHCenter) && q_func()->hasFocus()); |
| 199 | } |
| 200 | |
| 201 | static inline QLineEditPrivate *get(QLineEdit *lineEdit) { |
| 202 | return lineEdit->d_func(); |
| 203 | } |
| 204 | |
| 205 | QPoint tripleClick; |
| 206 | QBasicTimer tripleClickTimer; |
| 207 | uint frame : 1; |
| 208 | uint : 1; |
| 209 | uint cursorVisible : 1; |
| 210 | uint dragEnabled : 1; |
| 211 | uint clickCausedFocus : 1; |
| 212 | uint edited : 1; |
| 213 | int hscroll; |
| 214 | int vscroll; |
| 215 | uint alignment; |
| 216 | static const int verticalMargin; |
| 217 | static const int horizontalMargin; |
| 218 | |
| 219 | bool sendMouseEventToInputContext(QMouseEvent *e); |
| 220 | |
| 221 | QRect adjustedContentsRect() const; |
| 222 | |
| 223 | void _q_handleWindowActivate(); |
| 224 | void _q_textEdited(const QString &); |
| 225 | void _q_cursorPositionChanged(int, int); |
| 226 | #ifdef QT_KEYPAD_NAVIGATION |
| 227 | void _q_editFocusChange(bool); |
| 228 | #endif |
| 229 | void _q_selectionChanged(); |
| 230 | void _q_updateNeeded(const QRect &); |
| 231 | #if QT_CONFIG(completer) |
| 232 | void _q_completionHighlighted(const QString &); |
| 233 | #endif |
| 234 | QPoint mousePressPos; |
| 235 | #if QT_CONFIG(draganddrop) |
| 236 | QBasicTimer dndTimer; |
| 237 | void drag(); |
| 238 | #endif |
| 239 | void _q_textChanged(const QString &); |
| 240 | void _q_clearButtonClicked(); |
| 241 | |
| 242 | QMargins textMargins; // use effectiveTextMargins() in case of icon. |
| 243 | |
| 244 | QString placeholderText; |
| 245 | |
| 246 | QWidget *addAction(QAction *newAction, QAction *before, QLineEdit::ActionPosition, int flags = 0); |
| 247 | void removeAction(QAction *action); |
| 248 | SideWidgetParameters sideWidgetParameters() const; |
| 249 | QIcon clearButtonIcon() const; |
| 250 | void setClearButtonEnabled(bool enabled); |
| 251 | void positionSideWidgets(); |
| 252 | inline bool hasSideWidgets() const { return !leadingSideWidgets.empty() || !trailingSideWidgets.empty(); } |
| 253 | inline const SideWidgetEntryList &leftSideWidgetList() const |
| 254 | { return q_func()->layoutDirection() == Qt::LeftToRight ? leadingSideWidgets : trailingSideWidgets; } |
| 255 | inline const SideWidgetEntryList &rightSideWidgetList() const |
| 256 | { return q_func()->layoutDirection() == Qt::LeftToRight ? trailingSideWidgets : leadingSideWidgets; } |
| 257 | |
| 258 | QMargins effectiveTextMargins() const; |
| 259 | |
| 260 | private: |
| 261 | struct SideWidgetLocation { |
| 262 | QLineEdit::ActionPosition position; |
| 263 | int index; |
| 264 | |
| 265 | bool isValid() const { return index >= 0; } |
| 266 | }; |
| 267 | friend class QTypeInfo<SideWidgetLocation>; |
| 268 | |
| 269 | SideWidgetLocation findSideWidget(const QAction *a) const; |
| 270 | |
| 271 | SideWidgetEntryList leadingSideWidgets; |
| 272 | SideWidgetEntryList trailingSideWidgets; |
| 273 | int lastTextSize; |
| 274 | int mouseYThreshold; |
| 275 | }; |
| 276 | Q_DECLARE_TYPEINFO(QLineEditPrivate::SideWidgetEntry, Q_PRIMITIVE_TYPE); |
| 277 | Q_DECLARE_TYPEINFO(QLineEditPrivate::SideWidgetLocation, Q_PRIMITIVE_TYPE); |
| 278 | |
| 279 | QT_END_NAMESPACE |
| 280 | |
| 281 | #endif // QLINEEDIT_P_H |
| 282 | |