Pimcommon

spellchecklineedit.cpp
1/*
2 * SPDX-FileCopyrightText: 2011-2024 Laurent Montel <montel@kde.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "spellchecklineedit.h"
8#include <QKeyEvent>
9#include <QMimeData>
10#include <QStyle>
11#include <QStyleOptionFrame>
12
13using namespace PimCommon;
14
16 : TextCustomEditor::RichTextEditor(parent)
17{
18 setSpellCheckingConfigFileName(configFile);
19 setSearchSupport(false);
20 setAllowTabSupport(false);
21 setAcceptRichText(false);
22 setTabChangesFocus(true);
23 // widget may not be resized vertically
25 setLineWrapMode(NoWrap);
26 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
27 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
28 setCheckSpellingEnabled(true);
29 document()->adjustSize();
30
31 document()->setDocumentMargin(2);
32}
33
35
36void SpellCheckLineEdit::keyPressEvent(QKeyEvent *e)
37{
38 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return || e->key() == Qt::Key_Down) {
39 Q_EMIT focusDown();
40 return;
41 } else if (e->key() == Qt::Key_Up) {
42 Q_EMIT focusUp();
43 return;
44 }
45 RichTextEditor::keyPressEvent(e);
46}
47
48QSize SpellCheckLineEdit::sizeHint() const
49{
50 QFontMetrics fm(font());
51
52 const int h = document()->size().toSize().height() - fm.descent() + 2 * frameWidth();
53
55 opt.initFrom(this);
56 opt.rect = QRect(0, 0, 100, h);
57 opt.lineWidth = lineWidth();
58 opt.midLineWidth = 0;
59 opt.state |= QStyle::State_Sunken;
60
61 QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h), this);
62
63 return s;
64}
65
66QSize SpellCheckLineEdit::minimumSizeHint() const
67{
68 return sizeHint();
69}
70
71void SpellCheckLineEdit::insertFromMimeData(const QMimeData *source)
72{
73 if (!source) {
74 return;
75 }
76
77 setFocus();
78
79 // Copy text from the clipboard (paste)
80 QString pasteText = source->text();
81
82 // is there any text in the clipboard?
83 if (!pasteText.isEmpty()) {
84 // replace \r with \n to make xterm pastes happy
85 pasteText.replace(QLatin1Char('\r'), QLatin1Char('\n'));
86 // remove blank lines
87 while (pasteText.contains(QLatin1StringView("\n\n"))) {
88 pasteText.replace(QLatin1StringView("\n\n"), QLatin1StringView("\n"));
89 }
90
91 static const QRegularExpression reTopSpace(QStringLiteral("^ *\n"));
92 while (pasteText.contains(reTopSpace)) {
93 pasteText.remove(reTopSpace);
94 }
95
96 static const QRegularExpression reBottomSpace(QStringLiteral("\n *$"));
97 while (pasteText.contains(reBottomSpace)) {
98 pasteText.remove(reBottomSpace);
99 }
100
101 // does the text contain at least one newline character?
102 pasteText.replace(QLatin1Char('\n'), QLatin1Char(' '));
103
104 insertPlainText(pasteText);
105 ensureCursorVisible();
106 return;
107 } else {
108 RichTextEditor::insertFromMimeData(source);
109 }
110}
111
112#include "moc_spellchecklineedit.cpp"
void focusUp()
Emitted when the user uses the up arrow in the first line.
~SpellCheckLineEdit() override
Destructor.
SpellCheckLineEdit(QWidget *parent, const QString &configFile)
Constructs a SpellCheckLineEdit object.
folderdialogacltab.h
int key() const const
QString text() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
void initFrom(const QWidget *widget)
Key_Enter
ScrollBarAlwaysOff
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:23 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.