KTextAddons

textgotolinewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "textgotolinewidget.h"
8
9#include <KLocalizedString>
10#include <QIcon>
11#include <QPushButton>
12
13#include <QHBoxLayout>
14#include <QKeyEvent>
15#include <QLabel>
16#include <QSpinBox>
17#include <QToolButton>
18
19using namespace TextCustomEditor;
20
21class Q_DECL_HIDDEN TextCustomEditor::TextGoToLineWidgetPrivate
22{
23public:
24 TextGoToLineWidgetPrivate() = default;
25
26 QSpinBox *mSpinbox = nullptr;
27 QPushButton *mGoToLine = nullptr;
28};
29
30TextGoToLineWidget::TextGoToLineWidget(QWidget *parent)
31 : QWidget(parent)
32 , d(new TextCustomEditor::TextGoToLineWidgetPrivate)
33{
34 auto hbox = new QHBoxLayout(this);
35 hbox->setContentsMargins(2, 2, 2, 2);
36 auto closeBtn = new QToolButton(this);
37 closeBtn->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
38 closeBtn->setIconSize(QSize(16, 16));
39 closeBtn->setToolTip(i18nc("@info:tooltip", "Close"));
40 closeBtn->setObjectName(QStringLiteral("closebutton"));
41#ifndef QT_NO_ACCESSIBILITY
42 closeBtn->setAccessibleName(i18n("Close"));
43#endif
44
45 closeBtn->setAutoRaise(true);
46 connect(closeBtn, &QToolButton::clicked, this, &TextGoToLineWidget::slotCloseBar);
47 hbox->addWidget(closeBtn);
48
49 auto lab = new QLabel(i18nc("@label:textbox", "Go to Line:"));
50 hbox->addWidget(lab);
51 d->mSpinbox = new QSpinBox(this);
52 d->mSpinbox->setMinimum(1);
53 d->mSpinbox->setObjectName(QStringLiteral("line"));
54 connect(d->mSpinbox, &QSpinBox::editingFinished, this, &TextGoToLineWidget::slotGoToLine);
55 hbox->addWidget(d->mSpinbox);
56 d->mGoToLine = new QPushButton(QIcon::fromTheme(QStringLiteral("go-jump")), i18n("Go"));
57 d->mGoToLine->setFlat(true);
58 connect(d->mGoToLine, &QPushButton::clicked, this, &TextGoToLineWidget::slotGoToLine);
59 d->mGoToLine->setObjectName(QStringLiteral("gotoline"));
60 hbox->addWidget(d->mGoToLine);
61 hbox->addStretch();
62 d->mSpinbox->setFocus();
63 d->mSpinbox->installEventFilter(this);
64}
65
66TextGoToLineWidget::~TextGoToLineWidget()
67{
68 // mSpinbox can emit signals from its dtor, which are connected to this object
69 // so we need to make sure these connections are removed before we destroy ourselves
70 delete d->mSpinbox;
71}
72
73bool TextGoToLineWidget::eventFilter(QObject *obj, QEvent *event)
74{
75 if (obj == d->mSpinbox) {
76 if (event->type() == QEvent::KeyPress) {
77 auto e = static_cast<QKeyEvent *>(event);
78 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
79 slotGoToLine();
80 return true;
81 }
82 }
83 }
84 return QObject::eventFilter(obj, event);
85}
86
87void TextGoToLineWidget::setMaximumLineCount(int max)
88{
89 d->mSpinbox->setMaximum(max);
90}
91
92void TextGoToLineWidget::goToLine()
93{
94 show();
95 d->mSpinbox->setFocus();
96 d->mSpinbox->selectAll();
97}
98
99void TextGoToLineWidget::slotGoToLine()
100{
101 Q_EMIT moveToLine(d->mSpinbox->value());
102}
103
104void TextGoToLineWidget::showEvent(QShowEvent *e)
105{
106 if (!e->spontaneous()) {
107 d->mSpinbox->setFocus();
108 }
110}
111
112void TextGoToLineWidget::slotBlockCountChanged(int numberBlockCount)
113{
114 if (!isHidden()) {
115 setMaximumLineCount(numberBlockCount);
116 }
117}
118
119void TextGoToLineWidget::slotCloseBar()
120{
121 hide();
122 Q_EMIT hideGotoLine();
123}
124
125bool TextGoToLineWidget::event(QEvent *e)
126{
127 // Close the bar when pressing Escape.
128 // Not using a QShortcut for this because it could conflict with
129 // window-global actions (e.g. Emil Sedgh binds Esc to "close tab").
130 // With a shortcut override we can catch this before it gets to kactions.
131 const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
132 if (shortCutOverride || e->type() == QEvent::KeyPress) {
133 auto kev = static_cast<QKeyEvent *>(e);
134 if (kev->key() == Qt::Key_Escape) {
135 e->accept();
136 slotCloseBar();
137 return true;
138 }
139 }
140 return QWidget::event(e);
141}
142
143#include "moc_textgotolinewidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
void clicked(bool checked)
void editingFinished()
void accept()
bool spontaneous() const const
Type type() const const
QIcon fromTheme(const QString &name)
Q_EMITQ_EMIT
virtual bool eventFilter(QObject *watched, QEvent *event)
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
virtual bool event(QEvent *event) override
void hide()
bool isHidden() const const
void show()
virtual void showEvent(QShowEvent *event)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:49 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.