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;
14using namespace Qt::Literals::StringLiterals;
15
17 : TextCustomEditor::RichTextEditor(parent)
18{
19 setSpellCheckingConfigFileName(configFile);
20 setSearchSupport(false);
21 setAllowTabSupport(false);
22 setAcceptRichText(false);
24 // widget may not be resized vertically
29 setCheckSpellingEnabled(true);
30 document()->adjustSize();
31
32 document()->setDocumentMargin(2);
33}
34
36
37void SpellCheckLineEdit::keyPressEvent(QKeyEvent *e)
38{
39 if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return || e->key() == Qt::Key_Down) {
40 Q_EMIT focusDown();
41 return;
42 } else if (e->key() == Qt::Key_Up) {
44 return;
45 }
46 RichTextEditor::keyPressEvent(e);
47}
48
49QSize SpellCheckLineEdit::sizeHint() const
50{
51 QFontMetrics fm(font());
52
53 const int h = document()->size().toSize().height() - fm.descent() + 2 * frameWidth();
54
56 opt.initFrom(this);
57 opt.rect = QRect(0, 0, 100, h);
58 opt.lineWidth = lineWidth();
59 opt.midLineWidth = 0;
60 opt.state |= QStyle::State_Sunken;
61
62 QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h), this);
63
64 return s;
65}
66
67QSize SpellCheckLineEdit::minimumSizeHint() const
68{
69 return sizeHint();
70}
71
72void SpellCheckLineEdit::insertFromMimeData(const QMimeData *source)
73{
74 if (!source) {
75 return;
76 }
77
78 setFocus();
79
80 // Copy text from the clipboard (paste)
81 QString pasteText = source->text();
82
83 // is there any text in the clipboard?
84 if (!pasteText.isEmpty()) {
85 // replace \r with \n to make xterm pastes happy
86 pasteText.replace(QLatin1Char('\r'), QLatin1Char('\n'));
87 // remove blank lines
88 while (pasteText.contains("\n\n"_L1)) {
89 pasteText.replace("\n\n"_L1, "\n"_L1);
90 }
91
92 static const QRegularExpression reTopSpace(QStringLiteral("^ *\n"));
93 while (pasteText.contains(reTopSpace)) {
94 pasteText.remove(reTopSpace);
95 }
96
97 static const QRegularExpression reBottomSpace(QStringLiteral("\n *$"));
98 while (pasteText.contains(reBottomSpace)) {
99 pasteText.remove(reBottomSpace);
100 }
101
102 // does the text contain at least one newline character?
103 pasteText.replace(QLatin1Char('\n'), QLatin1Char(' '));
104
105 insertPlainText(pasteText);
107 return;
108 } else {
109 RichTextEditor::insertFromMimeData(source);
110 }
111}
112
113#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
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy)
void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy)
int key() const const
QString text() const const
Q_EMITQ_EMIT
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)
virtual QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const const=0
void initFrom(const QWidget *widget)
Key_Enter
ScrollBarAlwaysOff
void setAcceptRichText(bool accept)
void ensureCursorVisible()
void insertPlainText(const QString &text)
void setLineWrapMode(LineWrapMode mode)
void setTabChangesFocus(bool b)
void setFocus()
void setSizePolicy(QSizePolicy)
QStyle * style() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Aug 30 2024 11:45:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.