27#include <QStyleOption>
28#include <QApplication>
33 setCursor(Qt::ArrowCursor);
34 setFocusPolicy(Qt::ClickFocus);
39 switch (
event->type()) {
40 case QEvent::LayoutRequest:
44 case QEvent::MouseButtonPress:
45 case QEvent::MouseButtonRelease:
46 case QEvent::MouseButtonDblClick:
47 case QEvent::MouseMove:
55 return QWidget::event(
event);
60 , m_leftLayout(nullptr)
61 , m_rightLayout(nullptr)
64 , m_ignoreMousePress(false)
71 m_leftMargin = margin;
76 mainLayout =
new QHBoxLayout(
this);
77 mainLayout->setContentsMargins(0, 0, 0, 0);
78 mainLayout->setSpacing(0);
81 m_leftWidget->resize(0, 0);
82 m_leftLayout =
new QHBoxLayout(m_leftWidget);
83 m_leftLayout->setContentsMargins(0, 0, 0, 0);
84 m_leftLayout->setDirection(isRightToLeft() ? QBoxLayout::RightToLeft : QBoxLayout::LeftToRight);
87 m_rightWidget->resize(0, 0);
88 m_rightLayout =
new QHBoxLayout(m_rightWidget);
89 m_rightLayout->setDirection(isRightToLeft() ? QBoxLayout::RightToLeft : QBoxLayout::LeftToRight);
90 m_rightLayout->setContentsMargins(0, 0, 2, 0);
92 mainLayout->addWidget(m_leftWidget, 0, Qt::AlignVCenter | Qt::AlignLeft);
93 mainLayout->addItem(
new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
94 mainLayout->addWidget(m_rightWidget, 0, Qt::AlignVCenter | Qt::AlignRight);
95 mainLayout->setDirection(isRightToLeft() ? QBoxLayout::RightToLeft : QBoxLayout::LeftToRight);
102 auto* undoAction =
new QAction(QIcon::fromTheme(
QSL(
"edit-undo")), tr(
"&Undo"),
this);
103 undoAction->setShortcut(QKeySequence(
QSL(
"Ctrl+Z")));
104 undoAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
105 connect(undoAction, &QAction::triggered,
this, &QLineEdit::undo);
107 auto* redoAction =
new QAction(QIcon::fromTheme(
QSL(
"edit-redo")), tr(
"&Redo"),
this);
108 redoAction->setShortcut(QKeySequence(
QSL(
"Ctrl+Shift+Z")));
109 redoAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
110 connect(redoAction, &QAction::triggered,
this, &QLineEdit::redo);
112 auto* cutAction =
new QAction(QIcon::fromTheme(
QSL(
"edit-cut")), tr(
"Cu&t"),
this);
113 cutAction->setShortcut(QKeySequence(
QSL(
"Ctrl+X")));
114 cutAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
115 connect(cutAction, &QAction::triggered,
this, &QLineEdit::cut);
117 auto* copyAction =
new QAction(QIcon::fromTheme(
QSL(
"edit-copy")), tr(
"&Copy"),
this);
118 copyAction->setShortcut(QKeySequence(
QSL(
"Ctrl+C")));
119 copyAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
120 connect(copyAction, &QAction::triggered,
this, &QLineEdit::copy);
122 auto* pasteAction =
new QAction(QIcon::fromTheme(
QSL(
"edit-paste")), tr(
"&Paste"),
this);
123 pasteAction->setShortcut(QKeySequence(
QSL(
"Ctrl+V")));
124 pasteAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
125 connect(pasteAction, &QAction::triggered,
this, &QLineEdit::paste);
127 auto* pasteAndGoAction =
new QAction(
this);
128 pasteAndGoAction->setShortcut(QKeySequence(
QSL(
"Ctrl+Shift+V")));
129 pasteAndGoAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
131 auto* deleteAction =
new QAction(QIcon::fromTheme(
QSL(
"edit-delete")), tr(
"Delete"),
this);
132 connect(deleteAction, &QAction::triggered,
this, &LineEdit::slotDelete);
134 auto* clearAllAction =
new QAction(QIcon::fromTheme(
QSL(
"edit-clear")), tr(
"Clear All"),
this);
135 connect(clearAllAction, &QAction::triggered,
this, &QLineEdit::clear);
137 auto* selectAllAction =
new QAction(QIcon::fromTheme(
QSL(
"edit-select-all")), tr(
"Select All"),
this);
138 selectAllAction->setShortcut(QKeySequence(
QSL(
"Ctrl+A")));
139 selectAllAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
140 connect(selectAllAction, &QAction::triggered,
this, &QLineEdit::selectAll);
142 m_editActions[
Undo] = undoAction;
143 m_editActions[
Redo] = redoAction;
144 m_editActions[
Cut] = cutAction;
145 m_editActions[
Copy] = copyAction;
146 m_editActions[
Paste] = pasteAction;
148 m_editActions[
Delete] = deleteAction;
149 m_editActions[
ClearAll] = clearAllAction;
150 m_editActions[
SelectAll] = selectAllAction;
153 addAction(undoAction);
154 addAction(redoAction);
155 addAction(cutAction);
156 addAction(copyAction);
157 addAction(pasteAction);
158 addAction(pasteAndGoAction);
159 addAction(deleteAction);
160 addAction(clearAllAction);
161 addAction(selectAllAction);
164 connect(
this, &QLineEdit::textChanged,
this, &LineEdit::updateActions);
165 connect(
this, &QLineEdit::selectionChanged,
this, &LineEdit::updateActions);
172 if (
event->type() == QEvent::LayoutDirectionChange) {
174 if (isRightToLeft()) {
175 mainLayout->setDirection(QBoxLayout::RightToLeft);
176 m_leftLayout->setDirection(QBoxLayout::RightToLeft);
177 m_rightLayout->setDirection(QBoxLayout::RightToLeft);
180 mainLayout->setDirection(QBoxLayout::LeftToRight);
181 m_leftLayout->setDirection(QBoxLayout::LeftToRight);
182 m_rightLayout->setDirection(QBoxLayout::LeftToRight);
185 return QLineEdit::event(
event);
188#define ACCEL_KEY(k) QLatin1Char('\t') + QKeySequence(k).toString()
193 auto* popup =
new QMenu(
this);
194 popup->setObjectName(
QSL(
"qt_edit_menu"));
197 popup->addAction(m_editActions[
Undo]);
198 popup->addAction(m_editActions[
Redo]);
199 popup->addSeparator();
200 popup->addAction(m_editActions[
Cut]);
203 popup->addAction(m_editActions[
Copy]);
206 updatePasteActions();
207 popup->addAction(m_editActions[
Paste]);
208 if (!m_editActions[
PasteAndGo]->text().isEmpty()) {
211 popup->addAction(m_editActions[
Delete]);
212 popup->addAction(m_editActions[
ClearAll]);
215 popup->addSeparator();
216 popup->addAction(m_editActions[
SelectAll]);
219 QMenu* tmp = createStandardContextMenu();
220 tmp->setParent(popup);
222 QAction* lastAction = !tmp->actions().isEmpty() ? tmp->actions().constLast() :
nullptr;
224 if (lastAction && lastAction->menu() && lastAction->menu()->inherits(
"QUnicodeControlCharacterMenu")) {
225 popup->addAction(lastAction);
231void LineEdit::updateActions()
233 m_editActions[
Undo]->setEnabled(!isReadOnly() && isUndoAvailable());
234 m_editActions[
Redo]->setEnabled(!isReadOnly() && isRedoAvailable());
235 m_editActions[
Cut]->setEnabled(!isReadOnly() && hasSelectedText() && echoMode() == QLineEdit::Normal);
236 m_editActions[
Copy]->setEnabled(hasSelectedText() && echoMode() == QLineEdit::Normal);
237 m_editActions[
Delete]->setEnabled(!isReadOnly() && hasSelectedText());
238 m_editActions[
SelectAll]->setEnabled(!text().isEmpty() && selectedText() != text());
239 m_editActions[
Paste]->setEnabled(
true);
243void LineEdit::updatePasteActions()
246 bool pasteEnabled = !isReadOnly() && !QApplication::clipboard()->text().isEmpty();
248 m_editActions[
Paste]->setEnabled(pasteEnabled);
249 m_editActions[
PasteAndGo]->setEnabled(pasteEnabled);
252void LineEdit::slotDelete()
254 if (hasSelectedText()) {
265 m_leftLayout->addWidget(widget);
268 m_rightLayout->addWidget(widget);
278 m_leftLayout->removeWidget(widget);
279 m_rightLayout->removeWidget(widget);
285 m_leftLayout->setSpacing(spacing);
286 m_rightLayout->setSpacing(spacing);
292 return m_leftLayout->spacing();
303 QList<QInputMethodEvent::Attribute> attributes;
305 for (
const QTextLayout::FormatRange &fr : format) {
306 QInputMethodEvent::AttributeType type = QInputMethodEvent::TextFormat;
307 int start = fr.start - cursorPosition();
308 int length = fr.length;
309 QVariant
value = fr.format;
310 attributes.append(QInputMethodEvent::Attribute(type, start, length,
value));
313 QInputMethodEvent ev(QString(), attributes);
329 m_minHeight = height;
334 QSize s = QLineEdit::sizeHint();
336 if (s.height() < m_minHeight) {
337 s.setHeight(m_minHeight);
345 return m_editActions[action];
350 int left = m_leftWidget->sizeHint().width();
351 int right = m_rightWidget->sizeHint().width();
355 if (m_leftMargin >= 0) {
359 setTextMargins(left, top, right, bottom);
364 if (
event->reason() == Qt::MouseFocusReason &&
qzSettings->selectAllOnClick) {
365 m_ignoreMousePress =
true;
369 QLineEdit::focusInEvent(
event);
374 if (m_ignoreMousePress) {
375 m_ignoreMousePress =
false;
379 QLineEdit::mousePressEvent(
event);
388 if (!dragEnabled()) {
389 QLineEdit::mouseReleaseEvent(
event);
393 bool wasSelectedText = !selectedText().isEmpty();
395 QLineEdit::mouseReleaseEvent(
event);
397 bool isSelectedText = !selectedText().isEmpty();
399 if (wasSelectedText && !isSelectedText) {
400 QMouseEvent ev(QEvent::MouseButtonPress,
event->position(),
event->globalPosition(),
event->button(),
408 if (
event->buttons() == Qt::LeftButton &&
qzSettings->selectAllOnDoubleClick) {
413 QLineEdit::mouseDoubleClickEvent(
event);
418 QLineEdit::resizeEvent(
event);
420 m_leftWidget->setFixedHeight(height());
421 m_rightWidget->setFixedHeight(height());
QAction * editAction(EditAction action) const
void addWidget(QWidget *widget, WidgetPosition position)
void setTextFormat(const TextFormat &format)
void resizeEvent(QResizeEvent *event) override
QSize sizeHint() const override
void mouseReleaseEvent(QMouseEvent *event) override
QMenu * createContextMenu()
void removeWidget(QWidget *widget)
void setMinHeight(int height)
int widgetSpacing() const
void mousePressEvent(QMouseEvent *event) override
void focusInEvent(QFocusEvent *event) override
void mouseDoubleClickEvent(QMouseEvent *event) override
void setWidgetSpacing(int spacing)
LineEdit(QWidget *parent=nullptr)
bool event(QEvent *event) override
void setLeftMargin(int margin)
QList< QTextLayout::FormatRange > TextFormat
int value(const QColor &c)